Fixed the level editor with the new loading system.

This commit is contained in:
Scott Erickson 2014-04-25 19:11:32 -07:00
parent 14d62915f4
commit d5bcec5ad0
8 changed files with 70 additions and 45 deletions

View file

@ -102,7 +102,7 @@ module.exports = class LevelLoader extends CocoClass
for thangID in _.uniq thangIDs
url = "/db/thang.type/#{thangID}/version"
url += "?project=true" if @headless
# url += "?project=true" if @headless
res = @maybeLoadURL url, ThangType, 'thang'
@listenToOnce res.model, 'sync', @buildSpriteSheetsForThangType if res
for obj in objUniq componentVersions

View file

@ -199,4 +199,33 @@ class CocoModel extends Backbone.Model
sum
@getReferencedModel: (data, schema) ->
return null unless schema.links?
linkObject = _.find schema.links, rel: "db"
return null unless linkObject
return null if linkObject.href.match("thang.type") and not @isObjectID(data) # Skip loading hardcoded Thang Types for now (TODO)
# not fully extensible, but we can worry about that later
link = linkObject.href
link = link.replace('{(original)}', data.original)
link = link.replace('{(majorVersion)}', '' + (data.majorVersion ? 0))
link = link.replace('{($)}', data)
@getOrMakeModelFromLink(link)
@getOrMakeModelFromLink: (link) ->
makeUrlFunc = (url) -> -> url
modelUrl = link.split('/')[2]
modelModule = _.string.classify(modelUrl)
modulePath = "models/#{modelModule}"
try
Model = require modulePath
catch e
console.error 'could not load model from link path', link, 'using path', modulePath
return
model = new Model()
model.url = makeUrlFunc(link)
return model
module.exports = CocoModel

View file

@ -20,6 +20,7 @@ module.exports = class SuperModel extends Backbone.Model
return @getModelByURL(m.url())
getModelByURL: (modelURL) ->
modelURL = modelURL() if _.isFunction(modelURL)
return @models[modelURL] or null
getModelByOriginalAndMajorVersion: (ModelClass, original, majorVersion=0) ->
@ -35,6 +36,7 @@ module.exports = class SuperModel extends Backbone.Model
registerModel: (model) ->
url = model.url
url = model.url() if _.isFunction(model.url)
@models[url] = model
getCollection: (collection) ->
@ -64,7 +66,7 @@ module.exports = class SuperModel extends Backbone.Model
return @progress is 1.0 or Object.keys(@resources).length is 0
addModelResource: (modelOrCollection, name, fetchOptions, value=1) ->
modelOrCollection.saveBackups = @shouldSaveBackups()
modelOrCollection.saveBackups = @shouldSaveBackups(modelOrCollection)
@checkName(name)
@registerModel(modelOrCollection)
res = new ModelResource(modelOrCollection, name, fetchOptions, value)

View file

@ -19,21 +19,19 @@ module.exports = class ThangComponentEditView extends CocoView
@callback = options.callback
@componentCollection = @supermodel.getCollection new ComponentsCollection()
@componentCollectionRes = @supermodel.addModelResource(@componentCollection, 'component_collection')
@listenToOnce(@componentCollectionRes, 'loaded', @onComponentsSync)
@componentCollectionRes.load()
onloaded: -> @render()
if not @componentCollection.loaded
@supermodel.addModelResource(@componentCollection, 'component_collection').load()
onLoaded: ->
@supermodel.addCollection @componentCollection
super()
afterRender: ->
super()
return unless @supermodel.finished()
@buildExtantComponentTreema()
@buildAddComponentTreema()
onComponentsSync: (res) ->
return if @destroyed
@supermodel.addCollection @componentCollection
buildExtantComponentTreema: ->
level = new Level()
treemaOptions =

View file

@ -36,10 +36,10 @@ module.exports = class EditorLevelView extends View
constructor: (options, @levelID) ->
super options
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: @levelID, headless: true
@level = @levelLoader.level
@supermodel.shouldSaveBackups = (model) ->
model.constructor.className in ['Level', 'LevelComponent', 'LevelSystem']
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: @levelID, headless: true
@level = @levelLoader.level
@files = new DocumentFiles(@levelLoader.level)
@supermodel.addModelResource(@files, 'file_names').load()
@ -47,6 +47,11 @@ module.exports = class EditorLevelView extends View
$el ?= @$el.find('.outer-content')
super($el)
onLoaded: ->
_.defer =>
@world = @levelLoader.world
@render()
getRenderData: (context={}) ->
context = super(context)
context.level = @level
@ -54,11 +59,6 @@ module.exports = class EditorLevelView extends View
context.anonymous = me.get('anonymous')
context
onLoaded: ->
_.defer =>
@world = @levelLoader.world
@render()
afterRender: ->
super()
return unless @supermodel.finished()

View file

@ -32,11 +32,10 @@ module.exports = class SystemsTabView extends View
url = "/db/level.system/#{system.original}/version/#{system.majorVersion}"
ls = new LevelSystem()
ls.saveBackups = true
lsRes = @supermodel.addModelResource(ls, 'level_system_' + system.original)
lsRes.load()
do (url) -> ls.url = -> url
continue if @supermodel.getModelByURL ls.url
lsRes = @supermodel.addModelResource(ls, 'level_system_' + system.original)
lsRes.load()
@listenToOnce(lsRes, 'loaded', @onSystemLoaded)
++@toLoad
@onDefaultSystemsLoaded() unless @toLoad

View file

@ -51,8 +51,8 @@ module.exports = class ThangsTabView extends View
'click #delete': 'onDeleteClicked'
'click #duplicate': 'onDuplicateClicked'
'click #thangs-container-toggle': 'toggleThangsContainer'
'click #thangs-palette-toggle': 'toggleThangsPalette'
'click .add-thang-palette-icon': 'toggleThangsPalette'
# 'click #thangs-palette-toggle': 'toggleThangsPalette'
# 'click .add-thang-palette-icon': 'toggleThangsPalette'
shortcuts:
'esc': 'selectAddThang'
@ -65,31 +65,22 @@ module.exports = class ThangsTabView extends View
@world = options.world
@thangTypes = @supermodel.getCollection new ThangTypeSearchCollection() # should load depended-on Components, too
@thangTypesRes = @supermodel.addModelResource(@thangTypes, 'thang_type_search_collection')
@listenToOnce(@thangTypesRes, 'loaded', @onThangTypesLoaded)
@thangTypesRes.load()
@supermodel.addModelResource(@thangTypes, 'thang_type_search_collection').load()
# just loading all Components for now: https://github.com/codecombat/codecombat/issues/405
@componentCollection = @supermodel.getCollection new ComponentsCollection()
@supermodel.addModelResource(@componentCollection, 'components_collection').load()
$(document).bind 'contextmenu', @preventDefaultContextMenu
# just loading all Components for now: https://github.com/codecombat/codecombat/issues/405
@componentCollection = @supermodel.getCollection new ComponentsCollection()
@componentCollectionRes = @supermodel.addModelResource(@componentCollection, 'components_collection')
@listenToOnce(@componentCollectionRes, 'loaded', @onComponentsLoaded)
@componentCollectionRes.load()
onThangTypesLoaded: ->
return if @destroyed
onLoaded: ->
@supermodel.addCollection @thangTypes
for model in @thangTypes.models
@supermodel.populateModel(model, model.name)
onComponentsLoaded: ->
return if @destroyed
@supermodel.addCollection @componentCollection
super()
getRenderData: (context={}) ->
context = super(context)
return context unless @supermodel.finished()
thangTypes = (thangType.attributes for thangType in @supermodel.getModels(ThangType))
thangTypes = _.uniq thangTypes, false, 'original'
thangTypes = _.reject thangTypes, kind: 'Mark'
@ -123,11 +114,13 @@ module.exports = class ThangsTabView extends View
afterRender: ->
super()
return unless @supermodel.finished()
$('.tab-content').click @selectAddThang
$('#thangs-list').bind 'mousewheel', @preventBodyScrollingInThangList
@$el.find('#extant-thangs-filter button:first').button('toggle')
$(window).resize @onWindowResize
@addThangsView = @insertSubView new AddThangsView world: @world, supermodel: @supermodel
@onLevelLoaded() # refactor to not have this trigger when this view re-renders?
onFilterExtantThangs: (e) ->
@$el.find('#extant-thangs-filter button.active').button('toggle')
@ -142,7 +135,7 @@ module.exports = class ThangsTabView extends View
e.preventDefault()
onLevelLoaded: (e) ->
@level = e.level
@level = e.level if e
data = $.extend(true, {}, @level.attributes)
treemaOptions =
@ -411,7 +404,6 @@ module.exports = class ThangsTabView extends View
physical.config.pos = x: pos.x, y: pos.y, z: physical.config.pos.z if physical
thang = thangType: thangType.get('original'), id: thangID, components: components
@thangsTreema.insert '', thang
@supermodel.populateModel(thangType, thangType.get('name')) # Make sure we grab any new data for the thang we just added
editThang: (e) ->
if e.target # click event

View file

@ -32,8 +32,13 @@ module.exports = class CocoView extends Backbone.View
constructor: (options) ->
@loadProgress = _.cloneDeep @loadProgress
@supermodel ?= options?.supermodel or new SuperModel()
@options = options
@supermodel ?= new SuperModel()
if options?.supermodel # kind of a hacky way to get each view to store its own progress
@supermodel.models = options.supermodel.models
@supermodel.collections = options.supermodel.collections
@supermodel.shouldSaveBackups = options.supermodel.shouldSaveBackups
@subscriptions = utils.combineAncestralObject(@, 'subscriptions')
@events = utils.combineAncestralObject(@, 'events')
@scope = makeScopeName()