codecombat/app/views/editor/thang/edit.coffee

410 lines
13 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
ThangType = require 'models/ThangType'
SpriteParser = require 'lib/sprites/SpriteParser'
SpriteBuilder = require 'lib/sprites/SpriteBuilder'
CocoSprite = require 'lib/surface/CocoSprite'
Camera = require 'lib/surface/Camera'
DocumentFiles = require 'collections/DocumentFiles'
View = require 'views/kinds/RootView'
ThangComponentEditView = require 'views/editor/components/main'
VersionHistoryView = require './versions_view'
2014-01-10 19:48:28 -05:00
ColorsTabView = require './colors_tab_view'
2014-04-11 17:19:17 -04:00
PatchesView = require 'views/editor/patches_view'
SaveVersionModal = require 'views/modal/save_version_modal'
ErrorView = require '../../error_view'
template = require 'templates/editor/thang/edit'
2014-01-10 19:48:28 -05:00
2014-01-03 13:32:13 -05:00
CENTER = {x:200, y:300}
module.exports = class ThangTypeEditView extends View
id: "editor-thang-type-edit-view"
template: template
startsLoading: true
resolution: 4
scale: 3
mockThang:
health: 10.0
maxHealth: 10.0
hudProperties: ['health']
events:
'click #clear-button': 'clearRawData'
'click #upload-button': -> @$el.find('input#real-upload-button').click()
'change #real-upload-button': 'animationFileChosen'
'change #animations-select': 'showAnimation'
2014-01-06 22:33:36 -05:00
'click #marker-button': 'toggleDots'
'click #end-button': 'endAnimation'
2014-03-12 10:22:15 -04:00
'click #history-button': 'showVersionHistory'
'click #save-button': 'openSaveModal'
2014-04-11 17:19:17 -04:00
'click #patches-tab': -> @patchesView.load()
2014-01-03 13:32:13 -05:00
subscriptions:
'save-new-version': 'saveNewThangType'
2014-01-03 13:32:13 -05:00
# init / render
constructor: (options, @thangTypeID) ->
super options
@mockThang = $.extend(true, {}, @mockThang)
2014-01-03 13:32:13 -05:00
@thangType = new ThangType(_id: @thangTypeID)
@thangType.saveBackups = true
@listenToOnce(@thangType, 'error', =>
2014-04-21 14:42:21 -04:00
@hideLoading()
2014-04-21 14:42:21 -04:00
# Hack: editor components appear after calling insertSubView.
# So we need to hide them first.
$(@$el).find('.main-content-area').children('*').not('#error-view').remove()
2014-04-21 14:42:21 -04:00
@insertSubView(new ErrorView())
)
thangRes = @supermodel.addModelResource(@thangType, 'thang_type')
thangRes.load()
@files = new DocumentFiles(@thangType)
thangDocRes = @supermodel.addModelResource(@files, 'thang_document')
thangDocRes.load()
2014-04-08 12:03:03 -04:00
2014-01-03 13:32:13 -05:00
@refreshAnimation = _.debounce @refreshAnimation, 500
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super(context)
context.thangType = @thangType
context.animations = @getAnimationNames()
context.authorized = me.isAdmin() or @thangType.hasWriteAccess(me)
2014-01-03 13:32:13 -05:00
context
getAnimationNames: ->
raw = _.keys(@thangType.get('raw').animations)
raw = ("raw:#{name}" for name in raw)
main = _.keys(@thangType.get('actions') or {})
main.concat(raw)
onLoaded: -> @render()
2014-01-03 13:32:13 -05:00
afterRender: ->
super()
@initStage()
@buildTreema()
@initSliders()
@initComponents()
2014-01-10 19:48:28 -05:00
@insertSubView(new ColorsTabView(@thangType))
2014-04-11 17:19:17 -04:00
@patchesView = @insertSubView(new PatchesView(@thangType), @$el.find('.patches-view'))
@showReadOnly() if me.get('anonymous')
2014-01-03 13:32:13 -05:00
initComponents: =>
options =
components: @thangType.get('components') ? []
supermodel: @supermodel
callback: @onComponentsChanged
@thangComponentEditView = new ThangComponentEditView options
@insertSubView @thangComponentEditView
onComponentsChanged: (components) =>
@thangType.set 'components', components
makeDot: (color) ->
circle = new createjs.Shape()
circle.graphics.beginFill(color).beginStroke('black').drawCircle(0, 0, 5)
circle.x = CENTER.x
circle.y = CENTER.y
circle.scaleY = 0.5
circle
initStage: ->
canvas = @$el.find('#canvas')
@stage = new createjs.Stage(canvas[0])
canvasWidth = parseInt(canvas.attr('width'), 10)
canvasHeight = parseInt(canvas.attr('height'), 10)
2014-02-11 17:58:45 -05:00
@camera?.destroy()
2014-01-03 13:32:13 -05:00
@camera = new Camera canvasWidth, canvasHeight
@torsoDot = @makeDot('blue')
@mouthDot = @makeDot('yellow')
@aboveHeadDot = @makeDot('green')
2014-01-06 22:33:36 -05:00
@groundDot = @makeDot('red')
@stage.addChild(@groundDot, @torsoDot, @mouthDot, @aboveHeadDot)
2014-01-03 13:32:13 -05:00
@updateGrid()
@refreshAnimation()
createjs.Ticker.setFPS(30)
createjs.Ticker.addEventListener("tick", @stage)
2014-01-06 22:33:36 -05:00
toggleDots: ->
@showDots = not @showDots
@updateDots()
2014-01-03 13:32:13 -05:00
updateDots: ->
2014-01-06 22:33:36 -05:00
@stage.removeChild(@torsoDot, @mouthDot, @aboveHeadDot, @groundDot)
2014-01-03 13:32:13 -05:00
return unless @currentSprite
2014-01-06 22:33:36 -05:00
return unless @showDots
2014-01-03 13:32:13 -05:00
torso = @currentSprite.getOffset 'torso'
mouth = @currentSprite.getOffset 'mouth'
aboveHead = @currentSprite.getOffset 'aboveHead'
@torsoDot.x = CENTER.x + torso.x * @scale
@torsoDot.y = CENTER.y + torso.y * @scale
@mouthDot.x = CENTER.x + mouth.x * @scale
@mouthDot.y = CENTER.y + mouth.y * @scale
@aboveHeadDot.x = CENTER.x + aboveHead.x * @scale
@aboveHeadDot.y = CENTER.y + aboveHead.y * @scale
2014-01-06 22:33:36 -05:00
@stage.addChild(@groundDot, @torsoDot, @mouthDot, @aboveHeadDot)
2014-01-03 13:32:13 -05:00
endAnimation: ->
@currentSprite?.queueAction('idle')
2014-01-03 13:32:13 -05:00
updateGrid: ->
grid = new createjs.Container()
line = new createjs.Shape()
width = 1000
2014-01-06 22:33:36 -05:00
line.graphics.beginFill('#666666').drawRect(-width/2, -0.5, width, 0.5)
2014-01-03 13:32:13 -05:00
line.x = CENTER.x
line.y = CENTER.y
y = line.y
step = 10 * @scale
y -= step while y > 0
while y < 500
y += step
newLine = line.clone()
newLine.y = y
grid.addChild(newLine)
x = line.x
x -= step while x > 0
while x < 400
x += step
newLine = line.clone()
newLine.x = x
newLine.rotation = 90
grid.addChild(newLine)
@stage.removeChild(@grid) if @grid
@stage.addChildAt(grid, 0)
@grid = grid
updateSelectBox: ->
names = @getAnimationNames()
select = @$el.find('#animations-select')
return if select.find('option').length is names.length
select.empty()
select.append($('<option></option>').text(name)) for name in names
# upload
animationFileChosen: (e) ->
@file = e.target.files[0]
return unless @file
2014-02-02 19:51:22 -05:00
return unless _.string.endsWith @file.type, 'javascript'
2014-01-09 14:04:22 -05:00
# @$el.find('#upload-button').prop('disabled', true)
2014-01-03 13:32:13 -05:00
@reader = new FileReader()
@reader.onload = @onFileLoad
@reader.readAsText(@file)
onFileLoad: (e) =>
result = @reader.result
parser = new SpriteParser(@thangType)
parser.parse(result)
@treema.set('raw', @thangType.get('raw'))
@updateSelectBox()
@refreshAnimation()
# animation select
refreshAnimation: ->
options = @getSpriteOptions()
@thangType.resetSpriteSheetCache()
spriteSheet = @thangType.buildSpriteSheet(options)
$('#spritesheets').empty()
return unless spriteSheet
2014-01-03 13:32:13 -05:00
for image in spriteSheet._images
$('#spritesheets').append(image)
2014-01-03 13:32:13 -05:00
@showAnimation()
@updatePortrait()
showAnimation: (animationName) ->
animationName = @$el.find('#animations-select').val() unless _.isString animationName
return unless animationName
if animationName.startsWith('raw:')
animationName = animationName[4...]
@showMovieClip(animationName)
else
@showSprite(animationName)
@updateScale()
@updateRotation()
showMovieClip: (animationName) ->
vectorParser = new SpriteBuilder(@thangType)
movieClip = vectorParser.buildMovieClip(animationName)
return unless movieClip
2014-01-03 13:32:13 -05:00
reg = @thangType.get('positions')?.registration
if reg
movieClip.regX = -reg.x
movieClip.regY = -reg.y
@showDisplayObject(movieClip)
2014-01-03 13:32:13 -05:00
getSpriteOptions: -> { resolutionFactor: @resolution, thang: @mockThang}
showSprite: (actionName) ->
options = @getSpriteOptions()
sprite = new CocoSprite(@thangType, options)
sprite.queueAction(actionName)
@currentSprite?.destroy()
@currentSprite = sprite
@showDisplayObject(sprite.displayObject)
2014-01-03 13:32:13 -05:00
updatePortrait: ->
options = @getSpriteOptions()
2014-01-06 15:37:35 -05:00
portrait = @thangType.getPortraitImage(options)
2014-01-03 13:32:13 -05:00
return unless portrait
2014-01-27 22:03:22 -05:00
portrait?.attr('id', 'portrait').addClass('img-thumbnail')
portrait.addClass 'img-thumbnail'
2014-01-03 13:32:13 -05:00
$('#portrait').replaceWith(portrait)
2014-01-03 13:32:13 -05:00
showDisplayObject: (displayObject) ->
@clearDisplayObject()
displayObject.x = CENTER.x
displayObject.y = CENTER.y
@stage.addChildAt(displayObject, 1)
@currentObject = displayObject
@updateDots()
clearDisplayObject: ->
@stage.removeChild(@currentObject) if @currentObject?
# sliders
initSliders: ->
@rotationSlider = @initSlider $("#rotation-slider", @$el), 50, @updateRotation
@scaleSlider = @initSlider $('#scale-slider', @$el), 29, @updateScale
@resolutionSlider = @initSlider $('#resolution-slider', @$el), 39, @updateResolution
2014-01-03 13:32:13 -05:00
@healthSlider = @initSlider $('#health-slider', @$el), 100, @updateHealth
updateRotation: =>
value = parseInt(180 * (@rotationSlider.slider('value') - 50) / 50)
@$el.find('.rotation-label').text " #{value}° "
if @currentSprite
@currentSprite.rotation = value
@currentSprite.update(true)
2014-01-03 13:32:13 -05:00
updateScale: =>
value = (@scaleSlider.slider('value') + 1) / 10
fixed = value.toFixed(1)
@scale = value
@$el.find('.scale-label').text " #{fixed}x "
@currentObject.scaleX = @currentObject.scaleY = value if @currentObject?
@updateGrid()
@updateDots()
updateResolution: =>
value = (@resolutionSlider.slider('value') + 1) / 10
fixed = value.toFixed(1)
@$el.find('.resolution-label').text " #{fixed}x "
@resolution = value
@refreshAnimation()
updateHealth: =>
value = parseInt((@healthSlider.slider('value')) / 10)
@$el.find('.health-label').text " #{value}hp "
@mockThang.health = value
@currentSprite?.update()
# save
saveNewThangType: (e) ->
newThangType = if e.major then @thangType.cloneNewMajorVersion() else @thangType.cloneNewMinorVersion()
newThangType.set('commitMessage', e.commitMessage)
res = newThangType.save()
return unless res
2014-01-06 18:36:35 -05:00
modal = $('#save-version-modal')
2014-01-03 13:32:13 -05:00
@enableModalInProgress(modal)
res.error =>
@disableModalInProgress(modal)
res.success =>
url = "/editor/thang/#{newThangType.get('slug') or newThangType.id}"
2014-01-09 01:30:00 -05:00
newThangType.uploadGenericPortrait ->
2014-01-06 18:36:35 -05:00
document.location.href = url
2014-01-03 13:32:13 -05:00
clearRawData: ->
@thangType.resetRawData()
2014-01-30 19:25:06 -05:00
@thangType.set 'actions', undefined
2014-01-03 13:32:13 -05:00
@clearDisplayObject()
2014-01-30 19:25:06 -05:00
@treema.set('/', @getThangData())
2014-01-30 19:25:06 -05:00
getThangData: ->
data = $.extend(true, {}, @thangType.attributes)
2014-01-30 19:25:06 -05:00
data = _.pick data, (value, key) => not (key in ['components'])
2014-01-03 13:32:13 -05:00
buildTreema: ->
2014-01-30 19:25:06 -05:00
data = @getThangData()
2014-04-12 04:35:56 -04:00
schema = _.cloneDeep ThangType.schema
2014-01-03 13:32:13 -05:00
schema.properties = _.pick schema.properties, (value, key) => not (key in ['components'])
options =
data: data
schema: schema
files: @files
filePath: "db/thang.type/#{@thangType.get('original')}"
readOnly: true unless me.isAdmin() or @thangType.hasWriteAccess(me)
2014-01-03 13:32:13 -05:00
callbacks:
change: @pushChangesToPreview
select: @onSelectNode
el = @$el.find('#thang-type-treema')
@treema = @$el.find('#thang-type-treema').treema(options)
@treema.build()
pushChangesToPreview: =>
# TODO: This doesn't delete old Treema keys you deleted
for key, value of @treema.data
@thangType.set(key, value)
@updateSelectBox()
@refreshAnimation()
@updateDots()
@updatePortrait()
onSelectNode: (e, selected) =>
selected = selected[0]
return @stopShowingSelectedNode() if not selected
path = selected.getPath()
parts = path.split('/')
return @stopShowingSelectedNode() unless parts.length >= 4 and path.startsWith '/raw/'
key = parts[3]
type = parts[2]
vectorParser = new SpriteBuilder(@thangType)
obj = vectorParser.buildMovieClip(key) if type is 'animations'
obj = vectorParser.buildContainerFromStore(key) if type is 'containers'
obj = vectorParser.buildShapeFromStore(key) if type is 'shapes'
if obj?.bounds
obj.regX = obj.bounds.x + obj.bounds.width / 2
obj.regY = obj.bounds.y + obj.bounds.height / 2
2014-01-06 22:33:36 -05:00
else if obj?.frameBounds?[0]
bounds = obj.frameBounds[0]
obj.regX = bounds.x + bounds.width / 2
obj.regY = bounds.y + bounds.height / 2
2014-01-03 13:32:13 -05:00
@showDisplayObject(obj) if obj
obj.y = 200 if obj # truly center the container
@showingSelectedNode = true
@currentSprite?.destroy()
@currentSprite = null
@updateScale()
2014-01-06 22:33:36 -05:00
@grid.alpha = 0.0
2014-01-03 13:32:13 -05:00
stopShowingSelectedNode: ->
return unless @showingSelectedNode
2014-01-06 22:33:36 -05:00
@grid.alpha = 1.0
2014-01-03 13:32:13 -05:00
@showAnimation()
@showingSelectedNode = false
2014-02-11 17:58:45 -05:00
2014-03-12 10:22:15 -04:00
showVersionHistory: (e) ->
versionHistoryView = new VersionHistoryView thangType:@thangType, @thangTypeID
@openModalView versionHistoryView
2014-03-12 11:29:42 -04:00
Backbone.Mediator.publish 'level:view-switched', e
openSaveModal: ->
@openModalView(new SaveVersionModal({model: @thangType}))
destroy: ->
@camera?.destroy()
super()