2014-07-17 20:20:11 -04:00
|
|
|
CocoView = require 'views/kinds/CocoView'
|
2014-01-03 13:32:13 -05:00
|
|
|
template = require 'templates/editor/level/scripts_tab'
|
|
|
|
Level = require 'models/Level'
|
|
|
|
Surface = require 'lib/surface/Surface'
|
2014-07-23 10:02:45 -04:00
|
|
|
nodes = require './../treema_nodes'
|
2014-08-13 02:27:04 -04:00
|
|
|
defaultScripts = require 'lib/DefaultScripts'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-17 20:20:11 -04:00
|
|
|
module.exports = class ScriptsTabView extends CocoView
|
2014-06-30 22:16:26 -04:00
|
|
|
id: 'editor-level-scripts-tab-view'
|
2014-01-03 13:32:13 -05:00
|
|
|
template: template
|
|
|
|
className: 'tab-pane'
|
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'level-loaded': 'onLevelLoaded'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@world = options.world
|
|
|
|
@files = options.files
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-16 02:28:59 -04:00
|
|
|
onLoaded: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
onLevelLoaded: (e) ->
|
|
|
|
@level = e.level
|
|
|
|
@dimensions = @level.dimensions()
|
2014-03-18 16:47:51 -04:00
|
|
|
scripts = $.extend(true, [], @level.get('scripts') ? [])
|
2014-08-13 02:27:04 -04:00
|
|
|
if scripts.length is 0
|
|
|
|
scripts = $.extend(true, [], defaultScripts)
|
2014-01-03 13:32:13 -05:00
|
|
|
treemaOptions =
|
2014-04-12 04:35:56 -04:00
|
|
|
schema: Level.schema.properties.scripts
|
2014-01-03 13:32:13 -05:00
|
|
|
data: scripts
|
|
|
|
callbacks:
|
|
|
|
change: @onScriptsChanged
|
|
|
|
select: @onScriptSelected
|
2014-08-11 13:44:57 -04:00
|
|
|
addChild: @onNewScriptAdded
|
|
|
|
removeChild: @onScriptDeleted
|
2014-01-03 13:32:13 -05:00
|
|
|
nodeClasses:
|
2014-08-11 13:44:57 -04:00
|
|
|
array: ScriptsNode
|
2014-01-03 13:32:13 -05:00
|
|
|
object: ScriptNode
|
|
|
|
view: @
|
|
|
|
@scriptsTreema = @$el.find('#scripts-treema').treema treemaOptions
|
|
|
|
@scriptsTreema.build()
|
|
|
|
if @scriptsTreema.childrenTreemas[0]?
|
|
|
|
@scriptsTreema.childrenTreemas[0].select()
|
|
|
|
@scriptsTreema.childrenTreemas[0].broadcastChanges() # can get rid of this after refactoring treema
|
|
|
|
|
|
|
|
onScriptsChanged: (e) =>
|
|
|
|
@level.set 'scripts', @scriptsTreema.data
|
2014-08-11 12:26:36 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onScriptSelected: (e, selected) =>
|
|
|
|
selected = if selected.length > 1 then selected[0].getLastSelectedTreema() else selected[0]
|
|
|
|
unless selected
|
|
|
|
@$el.find('#script-treema').replaceWith($('<div id="script-treema"></div>'))
|
|
|
|
@selectedScriptPath = null
|
|
|
|
return
|
|
|
|
|
|
|
|
thangIDs = @getThangIDs()
|
|
|
|
treemaOptions =
|
|
|
|
world: @world
|
|
|
|
filePath: "db/level/#{@level.get('original')}"
|
|
|
|
files: @files
|
|
|
|
view: @
|
2014-04-12 04:35:56 -04:00
|
|
|
schema: Level.schema.properties.scripts.items
|
2014-01-03 13:32:13 -05:00
|
|
|
data: selected.data
|
|
|
|
thangIDs: thangIDs
|
|
|
|
dimensions: @dimensions
|
|
|
|
supermodel: @supermodel
|
2014-04-24 20:25:16 -04:00
|
|
|
readOnly: me.get('anonymous')
|
2014-01-03 13:32:13 -05:00
|
|
|
callbacks:
|
|
|
|
change: @onScriptChanged
|
|
|
|
nodeClasses:
|
2014-08-11 13:44:57 -04:00
|
|
|
object: PropertiesNode
|
2014-01-03 13:32:13 -05:00
|
|
|
'event-value-chain': EventPropsNode
|
|
|
|
'event-prereqs': EventPrereqsNode
|
|
|
|
'event-prereq': EventPrereqNode
|
|
|
|
'event-channel': ChannelNode
|
|
|
|
'thang': nodes.ThangNode
|
|
|
|
'milliseconds': nodes.MillisecondsNode
|
|
|
|
'seconds': nodes.SecondsNode
|
|
|
|
'point2d': nodes.WorldPointNode
|
|
|
|
'viewport': nodes.WorldViewportNode
|
|
|
|
'bounds': nodes.WorldBoundsNode
|
|
|
|
|
|
|
|
newPath = selected.getPath()
|
|
|
|
return if newPath is @selectedScriptPath
|
|
|
|
@scriptTreema = @$el.find('#script-treema').treema treemaOptions
|
|
|
|
@scriptTreema.build()
|
|
|
|
@scriptTreema.childrenTreemas?.noteChain?.open()
|
|
|
|
@selectedScriptPath = newPath
|
|
|
|
|
|
|
|
getThangIDs: ->
|
2014-02-12 15:27:02 -05:00
|
|
|
(t.id for t in @level.get('thangs') when t.id isnt 'Interface')
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-08-11 13:44:57 -04:00
|
|
|
onNewScriptAdded: (scriptNode) =>
|
|
|
|
return unless scriptNode
|
|
|
|
if scriptNode.data.id is undefined
|
|
|
|
scriptNode.disableTracking()
|
|
|
|
scriptNode.set '/id', 'Script-' + @scriptsTreema.data.length
|
|
|
|
scriptNode.enableTracking()
|
|
|
|
|
|
|
|
onScriptDeleted: =>
|
|
|
|
for key, treema of @scriptsTreema.childrenTreemas
|
|
|
|
key = parseInt(key)
|
|
|
|
treema.disableTracking()
|
|
|
|
if /Script-[0-9]*/.test treema.data.id
|
|
|
|
existingKey = parseInt(treema.data.id.substr(7))
|
|
|
|
if existingKey isnt key+1
|
|
|
|
treema.set 'id', 'Script-' + (key+1)
|
|
|
|
treema.enableTracking()
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onScriptChanged: =>
|
|
|
|
@scriptsTreema.set(@selectedScriptPath, @scriptTreema.data)
|
|
|
|
|
2014-08-11 13:44:57 -04:00
|
|
|
class ScriptsNode extends TreemaArrayNode
|
2014-08-12 19:58:23 -04:00
|
|
|
nodeDescription: 'Script'
|
2014-08-11 13:44:57 -04:00
|
|
|
addNewChild: ->
|
|
|
|
newTreema = super()
|
|
|
|
if @callbacks.addChild
|
|
|
|
@callbacks.addChild newTreema
|
|
|
|
newTreema
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
class ScriptNode extends TreemaObjectNode
|
|
|
|
valueClass: 'treema-script'
|
|
|
|
collection: false
|
2014-08-22 14:11:05 -04:00
|
|
|
buildValueForDisplay: (valEl, data) ->
|
|
|
|
val = data.id or data.channel
|
2014-01-03 13:32:13 -05:00
|
|
|
s = "#{val}"
|
|
|
|
@buildValueForDisplaySimply valEl, s
|
|
|
|
|
|
|
|
onTabPressed: (e) ->
|
|
|
|
@tabToCurrentScript()
|
|
|
|
e.preventDefault()
|
|
|
|
|
2014-08-11 13:44:57 -04:00
|
|
|
onDeletePressed: (e) ->
|
|
|
|
returnVal = super(e)
|
|
|
|
if @callbacks.removeChild
|
|
|
|
@callbacks.removeChild()
|
|
|
|
returnVal
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onRightArrowPressed: ->
|
|
|
|
@tabToCurrentScript()
|
|
|
|
|
|
|
|
tabToCurrentScript: ->
|
|
|
|
@settings.view.scriptTreema?.keepFocus()
|
|
|
|
window.v = @settings.view
|
|
|
|
firstRow = @settings.view.scriptTreema?.$el.find('.treema-node:visible').data('instance')
|
|
|
|
return unless firstRow?
|
|
|
|
firstRow.select()
|
|
|
|
|
2014-08-11 13:44:57 -04:00
|
|
|
class PropertiesNode extends TreemaObjectNode
|
2014-08-12 19:58:23 -04:00
|
|
|
nodeDescription: 'Script Property'
|
2014-08-11 13:44:57 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
class EventPropsNode extends TreemaNode.nodeMap.string
|
|
|
|
valueClass: 'treema-event-props'
|
|
|
|
|
2014-08-22 14:11:05 -04:00
|
|
|
arrayToString: -> (@getData() or []).join('.')
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-08-22 14:11:05 -04:00
|
|
|
buildValueForDisplay: (valEl, data) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
joined = @arrayToString()
|
|
|
|
joined = '(unset)' if not joined.length
|
|
|
|
@buildValueForDisplaySimply valEl, joined
|
|
|
|
|
2014-08-22 14:11:05 -04:00
|
|
|
buildValueForEditing: (valEl, data) ->
|
|
|
|
super(valEl, data)
|
2014-07-27 22:13:18 -04:00
|
|
|
channel = @getRoot().data.channel
|
|
|
|
channelSchema = Backbone.Mediator.channelSchemas[channel]
|
2014-07-28 16:15:59 -04:00
|
|
|
autocompleteValues = []
|
|
|
|
autocompleteValues.push key for key, val of channelSchema?.properties
|
2014-07-29 15:22:26 -04:00
|
|
|
valEl.find('input').autocomplete(source: autocompleteValues, minLength: 0, delay: 0, autoFocus: true).autocomplete('search')
|
2014-07-27 22:13:18 -04:00
|
|
|
valEl
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
saveChanges: (valEl) ->
|
|
|
|
@data = (s for s in $('input', valEl).val().split('.') when s.length)
|
|
|
|
|
|
|
|
class EventPrereqsNode extends TreemaNode.nodeMap.array
|
|
|
|
open: (depth=2) ->
|
|
|
|
super(depth)
|
|
|
|
|
|
|
|
addNewChild: ->
|
|
|
|
newTreema = super(arguments)
|
|
|
|
return unless newTreema?
|
|
|
|
newTreema.open()
|
|
|
|
newTreema.childrenTreemas.eventProps?.edit()
|
|
|
|
|
|
|
|
class EventPrereqNode extends TreemaNode.nodeMap.object
|
2014-08-22 14:11:05 -04:00
|
|
|
buildValueForDisplay: (valEl, data) ->
|
|
|
|
eventProp = (data.eventProps or []).join('.')
|
2014-01-03 13:32:13 -05:00
|
|
|
eventProp = '(unset)' unless eventProp.length
|
|
|
|
statements = []
|
2014-08-22 14:11:05 -04:00
|
|
|
for key, value of data
|
2014-01-03 13:32:13 -05:00
|
|
|
continue if key is 'eventProps'
|
|
|
|
comparison = @schema.properties[key].title
|
|
|
|
value = value.toString()
|
|
|
|
statements.push("#{comparison} #{value}")
|
|
|
|
statements = statements.join(', ')
|
|
|
|
s = "#{eventProp} #{statements}"
|
|
|
|
@buildValueForDisplaySimply valEl, s
|
|
|
|
|
|
|
|
class ChannelNode extends TreemaNode.nodeMap.string
|
2014-08-22 14:11:05 -04:00
|
|
|
buildValueForEditing: (valEl, data) ->
|
|
|
|
super(valEl, data)
|
2014-05-30 06:38:43 -04:00
|
|
|
autocompleteValues = ({label: val?.title or key, value: key} for key, val of Backbone.Mediator.channelSchemas)
|
|
|
|
valEl.find('input').autocomplete(source: autocompleteValues, minLength: 0, delay: 0, autoFocus: true)
|
2014-01-03 13:32:13 -05:00
|
|
|
valEl
|