Skip to content

Commit

Permalink
Make fill objects selectable and draggable
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Apr 3, 2012
1 parent 7f410f8 commit 30a5567
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 150 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

## Editor

* BUG: When switching layers sidebar never gets hidden

* FEATURE: Add ability to click on a fill object and delete it just like you can
do with tiles -- or extract this into a common place

Expand Down
76 changes: 57 additions & 19 deletions app/javascripts/common/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ define 'util', ->
#
# Returns a modified version of `target`.
#
extend: (args...) ->
extend = (args...) ->
if typeof args[0] is 'boolean'
deep = args.shift()
else
Expand All @@ -29,7 +29,7 @@ define 'util', ->
for obj in objects
for own prop of obj
if deep and ($.v.is.obj(obj[prop]) or $.v.is.arr(obj[prop]))
target[prop] = @clone obj[prop]
target[prop] = clone obj[prop]
else
target[prop] = obj[prop]

Expand All @@ -41,11 +41,11 @@ define 'util', ->
#
# Returns an Object of the same type as the given Object.
#
clone: (obj) ->
clone = (obj) ->
if $.v.is.arr(obj)
@extend true, [], obj
else if @isPlainObject(obj)
@extend true, {}, obj
extend true, [], obj
else if isPlainObject(obj)
extend true, {}, obj
else
obj

Expand All @@ -55,44 +55,82 @@ define 'util', ->
#
# Returns an Object of the same type as the given Object.
#
dup: (obj) ->
dup = (obj) ->
if $.v.is.arr(obj)
@extend false, [], obj
else if @isPlainObject(obj)
@extend false, {}, obj
extend false, [], obj
else if isPlainObject(obj)
extend false, {}, obj
else
obj

isPlainObject: (obj) ->
isPlainObject = (obj) ->
$.v.is.obj(obj) and obj.constructor is Object

createFromProto: (obj) ->
# DEPRECATED (use Object.create() directly)
createFromProto = (obj) ->
Object.create(obj)

randomItem: (arr) ->
arr[@randomInt(arr.length-1)]
randomItem = (arr) ->
arr[randomInt(arr.length-1)]

randomInt: (args...) ->
randomInt = (args...) ->
if args.length is 1
[min, max] = [0, args[0]]
else
[min, max] = args
Math.floor(Math.random() * (max - min + 1)) + min

capitalize: (str) ->
capitalize = (str) ->
str[0].toUpperCase() + str[1..-1]

ensureArray: (arr) ->
ensureArray = (arr) ->
arr = arr[0] if arr.length is 1 and $.is.arr(arr[0])
return arr

arrayDelete: (arr, item) ->
arrayDelete = (arr, item) ->
arr.splice(item, 1)

cmp: (a, b) ->
cmp = (a, b) ->
if a > b
return 1
else if a < b
return -1
else
return 0

hashWithout = (hash, keys...) ->
hash2 = dup(hash)
delete hash2[key] for key in keys
return hash2

return {
extend: extend
clone: clone
dup: dup
cmp: cmp

array:
delete: arrayDelete
wrap: ensureArray
random: randomItem
int:
random: randomInt
string:
capitalize: capitalize
is:
hash: isPlainObject
hash:
is: isPlainObject
without: hashWithout

# DEPRECATED
isPlainObject: isPlainObject # use is.hash() / hash.is()
randomItem: randomItem # use array.random()
randomInt: randomInt # use int.random
capitalize: capitalize # use string.capitalize()
ensureArray: ensureArray # use array.wrap()
arrayDelete: arrayDelete # use array.delete()

# DEPRECATED
createFromProto: createFromProto
}
2 changes: 1 addition & 1 deletion app/javascripts/editor/core.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ define 'editor.core', ->
@_populateMapObjects()
@_initLayers()
@_initToolbox()
@_changeLayerTo(0) # currentTool is already set
@viewport.loadMap()
@_changeLayerTo(0) # currentTool is already set

# Block backspace from leaving the page
$(window)
Expand Down
12 changes: 7 additions & 5 deletions app/javascripts/editor/drag_object.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ define 'editor.DragObject', ->
that = this

@$elem = $(elem)
@$elem.addClass('editor-drag-object')
@offset = @$elem.offset()

@options = options
if options.dropTarget
$dropTarget = $(options.dropTarget)
@dropTarget = $dropTarget.data('dropTarget')
if not @dropTarget
throw new Error "DragObject#init: Drop target does not exist"
throw new Error "DragObject#init: Drop target not defined. Either the drop target doesn't exist, or you need to call $(...).dropTarget() on it."

@dragOffset = null

Expand All @@ -37,7 +38,7 @@ define 'editor.DragObject', ->

.bind "mousedragstart.#{EVT_NS}", (evt) =>
@_logEvent @$elem, 'elem mousedragstart'
$(document.body).addClass('editor-drag-active')
$(document.body).addClass('editor-drag-object-dragged')
elemOffset = @$elem.offset()
console.log "setting dragOffset on #{@$elem.data('node-uid')}"
@dragOffset =
Expand All @@ -56,7 +57,7 @@ define 'editor.DragObject', ->

@$elem.one "mousedragend.#{EVT_NS}", (evt) =>
@_logEvent @$elem, 'elem mousedragend'
$(document.body).removeClass('editor-drag-active')
$(document.body).removeClass('editor-drag-object-dragged')
@dragOffset = null
if @dropTarget
evt.relatedTarget = @$elem[0]
Expand All @@ -69,8 +70,9 @@ define 'editor.DragObject', ->
@_removeDragEventsWithoutHelper()

destroy: ->
@$elem.unbind(".#{EVT_NS}")
$(window).unbind(".#{EVT_NS}")
@$elem.removeClass('editor-drag-object')
@$elem.unbind ".#{EVT_NS}"
$(window).unbind ".#{EVT_NS}"

position: (evt) ->
$elem = if @options.helper then @$helper else @$elem
Expand Down
Loading

0 comments on commit 30a5567

Please sign in to comment.