codecombat/app/views/play/level/tome/tome_view.coffee

169 lines
6.7 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
# There's one TomeView per Level. It has:
# - a CastButtonView, which has
# - a cast button
# - an autocast settings options button
# - for each spell (programmableMethod):
# - a Spell, which has
# - a list of Thangs that share that Spell, with one aether per Thang per Spell
# - a SpellView, which has
# - tons of stuff; the meat
# - a SpellListView, which has
# - for each spell:
# - a SpellListEntryView, which has
# - icons for each Thang
# - the spell name
# - a reload button
# - documentation for that method (in a popover)
# - a SpellPaletteView, which has
# - for each programmableProperty:
# - a SpellPaletteEntryView
#
# The CastButtonView and SpellListView always show.
# The SpellPaletteView shows the entries for the currently selected Programmable Thang.
# The SpellView shows the code and runtime state for the currently selected Spell and, specifically, Thang.
# The SpellView obscures most of the SpellListView when present. We might mess with this.
# You can switch a SpellView to showing the runtime state of another Thang sharing that Spell.
# SpellPaletteViews are destroyed and recreated whenever you switch Thangs.
# The SpellListView shows spells to which your team has read or readwrite access.
# It doubles as a Thang selector, since it's there when nothing is selected.
View = require 'views/kinds/CocoView'
template = require 'templates/play/level/tome/tome'
{me} = require 'lib/auth'
Spell = require './spell'
SpellListView = require './spell_list_view'
ThangListView = require './thang_list_view'
SpellPaletteView = require './spell_palette_view'
CastButtonView = require './cast_button_view'
module.exports = class TomeView extends View
id: 'tome-view'
template: template
controlsEnabled: true
cache: false
subscriptions:
'tome:spell-loaded': "onSpellLoaded"
'tome:cast-spell': "onCastSpell"
'tome:toggle-spell-list': 'onToggleSpellList'
'surface:sprite-selected': 'onSpriteSelected'
'surface:new-thang-added': 'onNewThangAdded'
2014-01-03 13:32:13 -05:00
events:
'click #spell-view': 'onSpellViewClick'
'click': -> Backbone.Mediator.publish 'focus-editor'
afterRender: ->
super()
programmableThangs = _.filter @options.thangs, 'isProgrammable'
2014-01-16 16:18:38 -05:00
if programmableThangs.length
@createSpells programmableThangs # Do before spellList, thangList, and castButton
@spellList = @insertSubView new SpellListView spells: @spells, supermodel: @supermodel
@thangList = @insertSubView new ThangListView spells: @spells, thangs: @options.thangs, supermodel: @supermodel
@castButton = @insertSubView new CastButtonView spells: @spells
2014-01-16 16:18:38 -05:00
else
@cast()
console.log "Warning: There are no Programmable Thangs in this level, which makes it unplayable."
2014-01-03 13:32:13 -05:00
onNewThangAdded: (e) ->
return unless e.thang.isProgrammable and not _.find @thangList.thangs, id: e.thang.id
@createSpells [e.thang]
@spellList.addThang e.thang
@thangList.addThang e.thang
2014-01-03 13:32:13 -05:00
createSpells: (programmableThangs) ->
# If needed, we could make this able to update when programmableThangs changes.
# We haven't done that yet, so call it just once on init.
pathPrefixComponents = ['play', 'level', @options.levelID, @options.session.id, 'code']
@spells ?= {}
@thangSpells ?= {}
2014-01-03 13:32:13 -05:00
for thang in programmableThangs
world = thang.world
continue if @thangSpells[thang.id]?
2014-01-03 13:32:13 -05:00
@thangSpells[thang.id] = []
for methodName, method of thang.programmableMethods
pathComponents = [thang.id, methodName]
if method.cloneOf
pathComponents[0] = method.cloneOf # referencing another Thang's method
pathComponents[0] = _.string.slugify pathComponents[0]
spellKey = pathComponents.join '/'
@thangSpells[thang.id].push spellKey
unless method.cloneOf
2014-01-28 18:24:08 -05:00
spell = @spells[spellKey] = new Spell programmableMethod: method, spellKey: spellKey, pathComponents: pathPrefixComponents.concat(pathComponents), session: @options.session, supermodel: @supermodel, skipFlow: @getQueryVariable("skip_flow") is "true", skipProtectAPI: @getQueryVariable("skip_protect_api") is "true"
2014-01-03 13:32:13 -05:00
for thangID, spellKeys of @thangSpells
thang = world.getThangByID(thangID)
@spells[spellKey].addThang thang for spellKey in spellKeys
null
onSpellLoaded: (e) ->
for spellID, spell of @spells
return unless spell.loaded
@cast()
onCastSpell: (e) ->
# A single spell is cast.
# Hmm; do we need to make sure other spells are all cast here?
@cast()
cast: ->
Backbone.Mediator.publish 'tome:cast-spells', spells: @spells
onToggleSpellList: (e) ->
@spellList.$el.toggle()
onSpellViewClick: (e) ->
@spellList.$el.hide()
clearSpellView: ->
@spellView?.dismiss()
@spellView?.$el.after('<div id="' + @spellView.id + '"></div>').detach()
@spellView = null
@spellTabView?.$el.after('<div id="' + @spellTabView.id + '"></div>').detach()
@spellTabView = null
@removeSubView @spellPaletteView if @spellPaletteView
@castButton?.$el.hide()
@thangList?.$el.show()
2014-01-03 13:32:13 -05:00
onSpriteSelected: (e) ->
thang = e.thang
spellName = e.spellName
@spellList?.$el.hide()
2014-01-03 13:32:13 -05:00
return @clearSpellView() unless thang?.isProgrammable
selectedThangSpells = (@spells[spellKey] for spellKey in @thangSpells[thang.id])
if spellName
spell = _.find selectedThangSpells, {name: spellName}
else
spell = @thangList.topSpellForThang thang
#spell = selectedThangSpells[0] # TODO: remember last selected spell for this thang
return @clearSpellView() unless spell?.canRead()
@spellList.setThangAndSpell thang, spell
2014-01-31 19:16:59 -05:00
@spellView?.setThang thang
@spellTabView?.setThang thang
if @spellPaletteView?.thang isnt thang
@spellPaletteView = @insertSubView new SpellPaletteView thang: thang
@spellPaletteView.toggleControls {}, spell.view.controlsEnabled # TODO: know when palette should have been disabled but didn't exist
2014-01-03 13:32:13 -05:00
return if spell.view is @spellView
@clearSpellView()
@spellView = spell.view
@spellTabView = spell.tabView
@$el.find('#' + @spellView.id).after(@spellView.el).remove()
@$el.find('#' + @spellTabView.id).after(@spellTabView.el).remove()
@spellView.setThang thang
@spellTabView.setThang thang
2014-01-25 18:11:29 -05:00
@castButton.attachTo @spellView
2014-01-03 13:32:13 -05:00
@thangList.$el.hide()
# New, good event
Backbone.Mediator.publish 'tome:spell-shown', thang: thang, spell: spell
# Bad, old one for old scripts (TODO)
Backbone.Mediator.publish 'editor:tab-shown', thang: thang, methodName: spell.name
reloadAllCode: ->
spell.view.reloadCode false for spellKey, spell of @spells
Backbone.Mediator.publish 'tome:cast-spells', spells: @spells
destroy: ->
super()
for spellKey, spell of @spells
spell.view.destroy()