2014-01-03 13:32:13 -05:00
|
|
|
module.exports = class CoordinateDisplay extends createjs.Container
|
|
|
|
layerPriority: -10
|
|
|
|
subscriptions:
|
|
|
|
'surface:mouse-moved': 'onMouseMove'
|
|
|
|
'surface:mouse-out': 'onMouseOut'
|
|
|
|
'surface:mouse-over': 'onMouseOver'
|
2014-03-12 20:50:59 -04:00
|
|
|
'surface:stage-mouse-down': 'onMouseDown'
|
2014-01-03 13:32:13 -05:00
|
|
|
'camera:zoom-updated': 'onZoomUpdated'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super()
|
|
|
|
@initialize()
|
|
|
|
@camera = options.camera
|
2014-06-30 22:16:26 -04:00
|
|
|
console.error 'CoordinateDisplay needs camera.' unless @camera
|
2014-01-03 13:32:13 -05:00
|
|
|
@build()
|
2014-02-23 12:55:40 -05:00
|
|
|
@show = _.debounce @show, 125
|
2014-01-03 13:32:13 -05:00
|
|
|
Backbone.Mediator.subscribe(channel, @[func], @) for channel, func of @subscriptions
|
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
Backbone.Mediator.unsubscribe(channel, @[func], @) for channel, func of @subscriptions
|
2014-02-22 19:58:54 -05:00
|
|
|
@show = null
|
|
|
|
@destroyed = true
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
build: ->
|
|
|
|
@mouseEnabled = @mouseChildren = false
|
2014-03-28 14:55:24 -04:00
|
|
|
@addChild @background = new createjs.Shape()
|
2014-06-30 22:16:26 -04:00
|
|
|
@addChild @label = new createjs.Text('', 'bold 16px Arial', '#FFFFFF')
|
2014-03-28 14:55:24 -04:00
|
|
|
@label.name = 'Coordinate Display Text'
|
2014-06-30 22:16:26 -04:00
|
|
|
@label.shadow = new createjs.Shadow('#000000', 1, 1, 0)
|
|
|
|
@background.name = 'Coordinate Display Background'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onMouseOver: (e) -> @mouseInBounds = true
|
|
|
|
onMouseOut: (e) -> @mouseInBounds = false
|
|
|
|
|
|
|
|
onMouseMove: (e) ->
|
2014-03-12 21:50:09 -04:00
|
|
|
if @mouseInBounds and key.shift
|
|
|
|
$('#surface').addClass('flag-cursor') unless $('#surface').hasClass('flag-cursor')
|
|
|
|
else if @mouseInBounds
|
|
|
|
$('#surface').removeClass('flag-cursor') if $('#surface').hasClass('flag-cursor')
|
2014-05-12 16:28:46 -04:00
|
|
|
wop = @camera.screenToWorld x: e.x, y: e.y
|
2014-01-03 13:32:13 -05:00
|
|
|
wop.x = Math.round(wop.x)
|
|
|
|
wop.y = Math.round(wop.y)
|
|
|
|
return if wop.x is @lastPos?.x and wop.y is @lastPos?.y
|
|
|
|
@lastPos = wop
|
|
|
|
@hide()
|
|
|
|
@show() # debounced
|
|
|
|
|
2014-03-12 20:50:59 -04:00
|
|
|
onMouseDown: (e) ->
|
|
|
|
return unless key.shift
|
2014-05-12 16:28:46 -04:00
|
|
|
wop = @camera.screenToWorld x: e.x, y: e.y
|
2014-03-12 20:50:59 -04:00
|
|
|
wop.x = Math.round wop.x
|
|
|
|
wop.y = Math.round wop.y
|
|
|
|
Backbone.Mediator.publish 'surface:coordinate-selected', wop
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onZoomUpdated: (e) ->
|
|
|
|
@hide()
|
|
|
|
@show()
|
|
|
|
|
|
|
|
hide: ->
|
|
|
|
return unless @label.parent
|
|
|
|
@removeChild @label
|
2014-03-28 14:55:24 -04:00
|
|
|
@removeChild @background
|
2014-01-03 13:32:13 -05:00
|
|
|
@uncache()
|
|
|
|
|
2014-03-28 14:55:24 -04:00
|
|
|
updateSize: ->
|
2014-05-12 16:28:46 -04:00
|
|
|
margin = 3
|
|
|
|
radius = 2.5
|
2014-03-28 14:55:24 -04:00
|
|
|
width = @label.getMeasuredWidth() + 2 * margin
|
|
|
|
height = @label.getMeasuredHeight() + 2 * margin
|
2014-07-11 22:10:13 -04:00
|
|
|
@label.regX = @background.regX = width / 2
|
|
|
|
@label.regY = @background.regY = height / 2
|
|
|
|
@label.regX -= margin
|
|
|
|
@label.regY -= margin
|
2014-03-28 14:55:24 -04:00
|
|
|
@background.graphics
|
|
|
|
.clear()
|
2014-06-30 22:16:26 -04:00
|
|
|
.beginFill('rgba(0,0,0,0.4)')
|
|
|
|
.beginStroke('rgba(0,0,0,0.6)')
|
2014-03-28 14:55:24 -04:00
|
|
|
.setStrokeStyle(1)
|
|
|
|
.drawRoundRect(0, 0, width, height, radius)
|
|
|
|
.endFill()
|
|
|
|
.endStroke()
|
|
|
|
[width, height]
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
show: =>
|
2014-02-22 19:58:54 -05:00
|
|
|
return unless @mouseInBounds and @lastPos and not @destroyed
|
2014-05-14 18:29:55 -04:00
|
|
|
@label.text = "{x: #{@lastPos.x}, y: #{@lastPos.y}}"
|
2014-03-28 14:55:24 -04:00
|
|
|
[width, height] = @updateSize()
|
2014-01-03 13:32:13 -05:00
|
|
|
sup = @camera.worldToSurface @lastPos
|
|
|
|
@x = sup.x
|
2014-05-12 16:28:46 -04:00
|
|
|
@y = sup.y - 2.5
|
2014-03-28 14:55:24 -04:00
|
|
|
@addChild @background
|
2014-01-03 13:32:13 -05:00
|
|
|
@addChild @label
|
|
|
|
@cache -width / 2, -height / 2, width, height
|
|
|
|
Backbone.Mediator.publish 'surface:coordinates-shown', {}
|