From 4b9bf22e226abc171e1c7ed289eecdb43f69544a Mon Sep 17 00:00:00 2001 From: Nick Winter Date: Tue, 2 Sep 2014 11:29:24 -0700 Subject: [PATCH] Thang editor now sets some default Components when you change the kind of a ThangType. --- app/models/LevelComponent.coffee | 6 ++- app/schemas/subscriptions/editor.coffee | 3 ++ .../component/ThangComponentsEditView.coffee | 47 +++++++++++++++---- .../editor/level/thangs/ThangsTabView.coffee | 14 ++---- .../editor/thang/ThangTypeEditView.coffee | 4 ++ 5 files changed, 55 insertions(+), 19 deletions(-) diff --git a/app/models/LevelComponent.coffee b/app/models/LevelComponent.coffee index 6353e9684..6c0dbbc65 100644 --- a/app/models/LevelComponent.coffee +++ b/app/models/LevelComponent.coffee @@ -3,10 +3,14 @@ CocoModel = require './CocoModel' module.exports = class LevelComponent extends CocoModel @className: 'LevelComponent' @schema: require 'schemas/models/level_component' - + @EquipsID: '53e217d253457600003e3ebb' @ItemID: '53e12043b82921000051cdf9' @AttacksID: '524b7ba57fc0f6d519000016' + @PhysicalID: '524b75ad7fc0f6d519000001' + @ExistsID: '524b4150ff92f1f4f8000024' + @LandID: '524b7aff7fc0f6d519000006' + @CollidesID: '524b7b857fc0f6d519000012' urlRoot: '/db/level.component' set: (key, val, options) -> diff --git a/app/schemas/subscriptions/editor.coffee b/app/schemas/subscriptions/editor.coffee index 99b47d6db..214404d5c 100644 --- a/app/schemas/subscriptions/editor.coffee +++ b/app/schemas/subscriptions/editor.coffee @@ -45,3 +45,6 @@ module.exports = 'editor:random-terrain-generated': c.object {required: ['thangs']}, thangs: c.array {}, {type: 'object'} + + 'editor:thang-type-kind-changed': c.object {required: ['kind']}, + kind: {type: 'string'} diff --git a/app/views/editor/component/ThangComponentsEditView.coffee b/app/views/editor/component/ThangComponentsEditView.coffee index 45fd6820e..dc8d2e0fb 100644 --- a/app/views/editor/component/ThangComponentsEditView.coffee +++ b/app/views/editor/component/ThangComponentsEditView.coffee @@ -12,6 +12,28 @@ nodes = require '../level/treema_nodes' ThangType = require 'models/ThangType' CocoCollection = require 'collections/CocoCollection' +LC = (componentName, config) -> original: LevelComponent[componentName + 'ID'], majorVersion: 0, config: config +DEFAULT_COMPONENTS = + Unit: [LC('Equips')] + Floor: [ + LC('Exists', stateless: true) + LC('Physical', width: 20, height: 17, depth: 2, shape: 'sheet', pos: {x: 10, y: 8.5, z: 1}) + LC('Land') + ] + Wall: [ + LC('Exists', stateless: true) + LC('Physical', width: 4, height: 4, depth: 12, shape: 'box', pos: {x: 2, y: 2, z: 6}) + LC('Collides', collisionType: 'static', collisionCategory: 'obstacles', mass: 1000, fixedRotation: true, restitution: 1) + ] + Doodad: [ + LC('Exists', stateless: true) + LC('Physical') + LC('Collides', collisionType: 'static', fixedRotation: true) + ] + Misc: [LC('Exists'), LC('Physical')] + Mark: [] + Item: [LC('Item')] + class ItemThangTypeSearchCollection extends CocoCollection url: '/db/thang.type?view=items&project=original,name,version,slug,kind,components' model: ThangType @@ -20,6 +42,9 @@ module.exports = class ThangComponentsEditView extends CocoView id: 'thang-components-edit-view' template: template + subscriptions: + 'editor:thang-type-kind-changed': 'onThangTypeKindChanged' + events: 'click #add-components-button': 'onAddComponentsButtonClicked' @@ -33,11 +58,11 @@ module.exports = class ThangComponentsEditView extends CocoView @world = options.world @level = options.level @loadComponents(@components) - + setThangType: (@thangType) -> return unless componentRefs = @thangType?.get('components') @loadComponents(componentRefs) - + loadComponents: (components) -> for componentRef in components # just to handle if ever somehow the same component is loaded twice, through bad data and alike @@ -73,7 +98,7 @@ module.exports = class ThangComponentsEditView extends CocoView type: 'object' default: defaultValue additionalProperties: Level.schema.properties.thangs.items.properties.components.items - }, + }, data: $.extend true, {}, components callbacks: {select: @onSelectComponent, change: @onComponentsTreemaChanged} nodeClasses: @@ -256,7 +281,7 @@ module.exports = class ThangComponentsEditView extends CocoView thangComponent.config = e.config foundComponent = true break - + if not foundComponent @components.push({ original: e.component.get('original') @@ -269,7 +294,7 @@ module.exports = class ThangComponentsEditView extends CocoView if subview.component.get('original') is e.component.get('original') _.defer -> subview.setIsDefaultComponent(false) break - + @updateComponentsList() @reportChanges() @@ -344,18 +369,24 @@ module.exports = class ThangComponentsEditView extends CocoView @loadComponents(sparseComponents) @components = @components.concat(sparseComponents) @onComponentsChanged() - + + onThangTypeKindChanged: (e) -> + return unless defaultComponents = DEFAULT_COMPONENTS[e.kind] + for component in defaultComponents when not _.find(@components, original: component.original) + @components.push component + @onComponentsAdded() + destroy: -> @componentsTreema?.destroy() super() class ThangComponentsObjectNode extends TreemaObjectNode addNewChild: -> @addNewChildForKey('') # HACK to get the object adding to act more like adding to an array - + getChildren: -> children = super(arguments...) children.sort(@sortFunction) - + sortFunction: (a, b) => a = a.value ? a.defaultData b = b.value ? b.defaultData diff --git a/app/views/editor/level/thangs/ThangsTabView.coffee b/app/views/editor/level/thangs/ThangsTabView.coffee index 8a1e07508..0d5edc1aa 100644 --- a/app/views/editor/level/thangs/ThangsTabView.coffee +++ b/app/views/editor/level/thangs/ThangsTabView.coffee @@ -15,11 +15,6 @@ ComponentsCollection = require 'collections/ComponentsCollection' MOVE_MARGIN = 0.15 MOVE_SPEED = 13 -# Essential component original ids -componentOriginals = - 'existence.Exists': '524b4150ff92f1f4f8000024' - 'physics.Physical': '524b75ad7fc0f6d519000001' - # Let us place these on top of other Thangs overlappableThangTypeNames = ['Torch', 'Chains', 'Bird', 'Cloud 1', 'Cloud 2', 'Cloud 3', 'Waterfall', 'Obstacle'] @@ -229,8 +224,7 @@ module.exports = class ThangsTabView extends CocoView @surface.camera.dragDisabled = false return unless @selectedExtantThang and e.thang?.id is @selectedExtantThang?.id pos = @selectedExtantThang.pos - physicalOriginal = componentOriginals['physics.Physical'] - path = "id=#{@selectedExtantThang.id}/components/original=#{physicalOriginal}" # TODO: hack + path = "id=#{@selectedExtantThang.id}/components/original=#{LevelComponent.PhysicalID}" physical = @thangsTreema.get path return if not physical or (physical.config.pos.x is pos.x and physical.config.pos.y is pos.y) @thangsTreema.set path + '/config/pos', x: pos.x, y: pos.y, z: pos.z @@ -319,11 +313,11 @@ module.exports = class ThangsTabView extends CocoView createEssentialComponents: (defaultComponents) -> physicalConfig = {pos: {x: 10, y: 10, z: 1}} - if physicalOriginal = _.find(defaultComponents ? [], original: componentOriginals['physics.Physical']) + if physicalOriginal = _.find(defaultComponents ? [], original: LevelComponent.PhysicalID) physicalConfig.pos.z = physicalOriginal.config?.pos?.z ? 1 # Get the z right [ - {original: componentOriginals['existence.Exists'], majorVersion: 0, config: {}} - {original: componentOriginals['physics.Physical'], majorVersion: 0, config: physicalConfig} + {original: LevelComponent.ExistsID, majorVersion: 0, config: {}} + {original: LevelComponent.PhysicalID, majorVersion: 0, config: physicalConfig} ] createAddThang: -> diff --git a/app/views/editor/thang/ThangTypeEditView.coffee b/app/views/editor/thang/ThangTypeEditView.coffee index 09b1af817..186304b2d 100644 --- a/app/views/editor/thang/ThangTypeEditView.coffee +++ b/app/views/editor/thang/ThangTypeEditView.coffee @@ -380,6 +380,7 @@ module.exports = class ThangTypeEditView extends RootView el = @$el.find('#thang-type-treema') @treema = @$el.find('#thang-type-treema').treema(options) @treema.build() + @lastKind = data.kind pushChangesToPreview: => # TODO: This doesn't delete old Treema keys you deleted @@ -389,6 +390,9 @@ module.exports = class ThangTypeEditView extends RootView @refreshAnimation() @updateDots() @updatePortrait() + if (kind = @treema.data.kind) isnt @lastKind + @lastKind = kind + Backbone.Mediator.publish 'editor:thang-type-kind-changed', kind: kind onSelectNode: (e, selected) => selected = selected[0]