2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/RootView'
|
|
|
|
template = require 'templates/editor/level/edit'
|
|
|
|
Level = require 'models/Level'
|
|
|
|
LevelSystem = require 'models/LevelSystem'
|
|
|
|
World = require 'lib/world/world'
|
|
|
|
DocumentFiles = require 'collections/DocumentFiles'
|
|
|
|
|
|
|
|
ThangsTabView = require './thangs_tab_view'
|
|
|
|
SettingsTabView = require './settings_tab_view'
|
|
|
|
ScriptsTabView = require './scripts_tab_view'
|
|
|
|
ComponentsTabView = require './components_tab_view'
|
|
|
|
SystemsTabView = require './systems_tab_view'
|
|
|
|
LevelSaveView = require './save_view'
|
|
|
|
LevelForkView = require './fork_view'
|
|
|
|
|
|
|
|
module.exports = class EditorLevelView extends View
|
|
|
|
id: "editor-level-view"
|
|
|
|
template: template
|
|
|
|
startsLoading: true
|
|
|
|
cache: false
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click #play-button': 'onPlayLevel'
|
|
|
|
'click #commit-level-start-button': 'startCommittingLevel'
|
|
|
|
'click #fork-level-start-button': 'startForkingLevel'
|
|
|
|
|
|
|
|
constructor: (options, @levelID) ->
|
|
|
|
super options
|
|
|
|
@supermodel.once 'loaded-all', @onAllLoaded
|
|
|
|
|
|
|
|
# load only the level itself and the one it points to, but no others
|
|
|
|
# TODO: this is duplicated in views/play/level_view.coffee; need cleaner method
|
|
|
|
@supermodel.shouldPopulate = (model) ->
|
|
|
|
@levelsLoaded ?= 0
|
|
|
|
@levelsLoaded += 1 if model.constructor.className is "Level"
|
|
|
|
return false if @levelsLoaded > 1
|
|
|
|
return true
|
|
|
|
|
2014-01-26 17:46:25 -05:00
|
|
|
@supermodel.shouldSaveBackups = (model) ->
|
|
|
|
model.constructor.className in ['Level', 'LevelComponent', 'LevelSystem']
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@level = new Level _id: @levelID
|
|
|
|
@level.once 'sync', @onLevelLoaded
|
|
|
|
@supermodel.populateModel @level
|
|
|
|
|
|
|
|
showLoading: ($el) ->
|
|
|
|
$el ?= @$el.find('.tab-content')
|
|
|
|
super($el)
|
|
|
|
|
|
|
|
onLevelLoaded: =>
|
|
|
|
@files = new DocumentFiles(@level)
|
|
|
|
@files.fetch()
|
|
|
|
|
|
|
|
onAllLoaded: =>
|
|
|
|
@level.unset('nextLevel') if _.isString(@level.get('nextLevel'))
|
|
|
|
@initWorld()
|
|
|
|
@startsLoading = false
|
|
|
|
@render() # do it again but without the loading screen
|
|
|
|
|
|
|
|
initWorld: ->
|
|
|
|
@world = new World @level.name
|
|
|
|
|
2014-02-11 17:58:45 -05:00
|
|
|
getRenderData: (context={}) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
context = super(context)
|
|
|
|
context.level = @level
|
|
|
|
context
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
return if @startsLoading
|
|
|
|
super()
|
|
|
|
new LevelSystem # temp; trigger the LevelSystem schema to be loaded, if it isn't already
|
2014-01-30 19:36:36 -05:00
|
|
|
@$el.find('a[data-toggle="tab"]').on 'shown.bs.tab', (e) =>
|
2014-01-03 13:32:13 -05:00
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
|
|
|
@thangsTab = @insertSubView new ThangsTabView world: @world, supermodel: @supermodel
|
|
|
|
@settingsTab = @insertSubView new SettingsTabView world: @world, supermodel: @supermodel
|
|
|
|
@scriptsTab = @insertSubView new ScriptsTabView world: @world, supermodel: @supermodel, files: @files
|
|
|
|
@componentsTab = @insertSubView new ComponentsTabView supermodel: @supermodel
|
|
|
|
@systemsTab = @insertSubView new SystemsTabView supermodel: @supermodel
|
|
|
|
Backbone.Mediator.publish 'level-loaded', level: @level
|
|
|
|
|
|
|
|
onPlayLevel: (e) ->
|
|
|
|
sendLevel = =>
|
|
|
|
@childWindow.Backbone.Mediator.publish 'level-reload-from-data', level: @level, supermodel: @supermodel
|
|
|
|
if @childWindow and not @childWindow.closed
|
|
|
|
# Reset the LevelView's world, but leave the rest of the state alone
|
|
|
|
sendLevel()
|
|
|
|
else
|
|
|
|
# Create a new Window with a blank LevelView
|
|
|
|
scratchLevelID = @level.get('slug') + "?dev=true"
|
|
|
|
@childWindow = window.open("/play/level/#{scratchLevelID}", 'child_window', 'width=1024,height=560,left=10,top=10,location=0,menubar=0,scrollbars=0,status=0,titlebar=0,toolbar=0', true)
|
|
|
|
@childWindow.onPlayLevelViewLoaded = (e) => sendLevel() # still a hack
|
|
|
|
@childWindow.focus()
|
|
|
|
|
|
|
|
startCommittingLevel: (e) ->
|
2014-02-06 13:30:49 -05:00
|
|
|
levelSaveView = new LevelSaveView level: @level, supermodel: @supermodel
|
2014-01-03 13:32:13 -05:00
|
|
|
@openModalView levelSaveView
|
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
|
|
|
|
|
|
|
startForkingLevel: (e) ->
|
|
|
|
levelForkView = new LevelForkView level: @level
|
|
|
|
@openModalView levelForkView
|
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|