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

425 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-06-30 22:16:26 -04:00
CENTER = {x: 200, y: 300}
2014-01-03 13:32:13 -05:00
module.exports = class ThangTypeEditView extends View
2014-06-30 22:16:26 -04:00
id: 'editor-thang-type-edit-view'
2014-01-03 13:32:13 -05:00
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 = @supermodel.loadModel(@thangType, 'thang').model
@thangType.saveBackups = true
@listenToOnce @thangType, 'sync', ->
console.log 'files for?', @thangType.id, @thangType.get 'name'
@files = @supermodel.loadCollection(new DocumentFiles(@thangType), 'files').model
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 = not me.get('anonymous')
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)
2014-01-03 13:32:13 -05:00
afterRender: ->
super()
return unless @supermodel.finished()
2014-01-03 13:32:13 -05:00
@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])
2014-02-11 17:58:45 -05:00
@camera?.destroy()
@camera = new Camera canvas
2014-01-03 13:32:13 -05:00
@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)
2014-06-30 22:16:26 -04:00
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: ->
return @showRasterImage() if @thangType.get('raster')
2014-01-03 13:32:13 -05:00
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()
showRasterImage: ->
sprite = new CocoSprite(@thangType, @getSpriteOptions())
@currentSprite?.destroy()
@currentSprite = sprite
@showImageObject(sprite.imageObject)
@updateScale()
2014-01-03 13:32:13 -05:00
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)
@updateRotation()
@updateScale() # must happen after update rotation, because updateRotation calls the sprite update() method.
2014-01-03 13:32:13 -05:00
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
@showImageObject(movieClip)
2014-06-30 22:16:26 -04:00
getSpriteOptions: -> {resolutionFactor: @resolution, thang: @mockThang}
2014-01-03 13:32:13 -05:00
showSprite: (actionName) ->
options = @getSpriteOptions()
sprite = new CocoSprite(@thangType, options)
sprite.queueAction(actionName)
@currentSprite?.destroy()
@currentSprite = sprite
@showImageObject(sprite.imageObject)
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)
showImageObject: (imageObject) ->
2014-01-03 13:32:13 -05:00
@clearDisplayObject()
imageObject.x = CENTER.x
imageObject.y = CENTER.y
@stage.addChildAt(imageObject, 1)
@currentObject = imageObject
2014-01-03 13:32:13 -05:00
@updateDots()
clearDisplayObject: ->
@stage.removeChild(@currentObject) if @currentObject?
# sliders
initSliders: ->
2014-06-30 22:16:26 -04:00
@rotationSlider = @initSlider $('#rotation-slider', @$el), 50, @updateRotation
2014-01-03 13:32:13 -05:00
@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: =>
resValue = (@resolutionSlider.slider('value') + 1) / 10
scaleValue = (@scaleSlider.slider('value') + 1) / 10
fixed = scaleValue.toFixed(1)
@scale = scaleValue
2014-01-03 13:32:13 -05:00
@$el.find('.scale-label').text " #{fixed}x "
if @currentSprite
@currentSprite.scaleFactor = scaleValue
@currentSprite.updateScale()
else if @currentObject?
@currentObject.scaleX = @currentObject.scaleY = scaleValue / resValue
2014-01-03 13:32:13 -05:00
@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}"
portraitSource = null
if @thangType.get('raster')
image = @currentSprite.imageObject.image
portraitSource = imageToPortrait image
# bit of a hacky way to get that portrait
success = =>
@thangType.clearBackup()
document.location.href = url
newThangType.uploadGenericPortrait success, portraitSource
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: me.get('anonymous')
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
@showImageObject(obj) if obj
2014-01-03 13:32:13 -05:00
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) ->
2014-06-30 22:16:26 -04:00
versionHistoryView = new VersionHistoryView thangType: @thangType, @thangTypeID
2014-03-12 10:22:15 -04:00
@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()
imageToPortrait = (img) ->
2014-06-30 22:16:26 -04:00
canvas = document.createElement('canvas')
canvas.width = 100
canvas.height = 100
2014-06-30 22:16:26 -04:00
ctx = canvas.getContext('2d')
scaleX = 100 / img.width
scaleY = 100 / img.height
ctx.scale scaleX, scaleY
ctx.drawImage img, 0, 0
2014-06-30 22:16:26 -04:00
canvas.toDataURL('image/png')