2014-01-03 10:32:13 -08: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-13 01:50:59 +01:00
' surface:stage-mouse-down ' : ' onMouseDown '
2014-01-03 10:32:13 -08:00
' camera:zoom-updated ' : ' onZoomUpdated '
2014-11-19 13:23:55 -08:00
' level:flag-color-selected ' : ' onFlagColorSelected '
2014-01-03 10:32:13 -08:00
constructor: (options) ->
super ( )
@ initialize ( )
@camera = options . camera
2014-08-26 13:56:57 -07:00
@layer = options . layer
console . error @ toString ( ) , ' needs a camera. ' unless @ camera
console . error @ toString ( ) , ' needs a layer. ' unless @ layer
2014-01-03 10:32:13 -08:00
@ build ( )
2014-02-23 09:55:40 -08:00
@show = _ . debounce @ show , 125
2014-01-03 10:32:13 -08: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 16:58:54 -08:00
@show = null
@destroyed = true
2014-01-03 10:32:13 -08:00
2014-08-26 13:56:57 -07:00
toString: -> ' <CoordinateDisplay> '
2014-01-03 10:32:13 -08:00
build: ->
@mouseEnabled = @mouseChildren = false
2014-03-28 11:55:24 -07:00
@ addChild @background = new createjs . Shape ( )
2014-07-01 10:16:26 +08:00
@ addChild @label = new createjs . Text ( ' ' , ' bold 16px Arial ' , ' # FFFFFF ' )
2014-07-13 19:31:34 -04:00
@ addChild @pointMarker = new createjs . Shape ( )
2014-03-28 11:55:24 -07:00
@label.name = ' Coordinate Display Text '
2014-07-01 10:16:26 +08:00
@label.shadow = new createjs . Shadow ( ' # 000000 ' , 1 , 1 , 0 )
@background.name = ' Coordinate Display Background '
2014-07-13 19:31:34 -04:00
@pointMarker.name = ' Point Marker '
2014-08-26 13:56:57 -07:00
@ layer . addChild @
2014-01-03 10:32:13 -08:00
onMouseOver: (e) -> @mouseInBounds = true
onMouseOut: (e) -> @mouseInBounds = false
onMouseMove: (e) ->
2014-05-12 13:28:46 -07:00
wop = @ camera . screenToWorld x: e . x , y: e . y
2014-01-03 10:32:13 -08: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-13 01:50:59 +01:00
onMouseDown: (e) ->
return unless key . shift
2014-05-12 13:28:46 -07:00
wop = @ camera . screenToWorld x: e . x , y: e . y
2014-03-13 01:50:59 +01:00
wop.x = Math . round wop . x
wop.y = Math . round wop . y
2014-08-28 14:00:54 -07:00
Backbone . Mediator . publish ' tome:focus-editor ' , { }
2014-03-13 01:50:59 +01:00
Backbone . Mediator . publish ' surface:coordinate-selected ' , wop
2014-01-03 10:32:13 -08:00
onZoomUpdated: (e) ->
@ hide ( )
@ show ( )
2014-11-19 13:23:55 -08:00
onFlagColorSelected: (e) ->
@placingFlag = Boolean e . color
2014-01-03 10:32:13 -08:00
hide: ->
return unless @ label . parent
@ removeChild @ label
2014-03-28 11:55:24 -07:00
@ removeChild @ background
2014-07-13 19:31:34 -04:00
@ removeChild @ pointMarker
2014-01-03 10:32:13 -08:00
@ uncache ( )
2014-03-28 11:55:24 -07:00
updateSize: ->
2014-05-12 13:28:46 -07:00
margin = 3
2014-07-13 19:31:34 -04:00
contentWidth = @ label . getMeasuredWidth ( ) + ( 2 * margin )
contentHeight = @ label . getMeasuredHeight ( ) + ( 2 * margin )
2014-08-01 00:14:54 -04:00
# Shift pointmarker up so it centers at pointer (affects container cache position)
@pointMarker.regY = contentHeight
2014-07-13 19:31:34 -04:00
pointMarkerStroke = 2
2014-07-19 20:12:56 -04:00
pointMarkerLength = 8
2014-08-01 00:14:54 -04:00
fullPointMarkerLength = pointMarkerLength + ( pointMarkerStroke / 2 )
2014-07-13 19:31:34 -04:00
contributionsToTotalSize = [ ]
2014-08-01 00:14:54 -04:00
contributionsToTotalSize = contributionsToTotalSize . concat @ updateCoordinates contentWidth , contentHeight , fullPointMarkerLength
2014-07-19 20:12:56 -04:00
contributionsToTotalSize = contributionsToTotalSize . concat @ updatePointMarker 0 , contentHeight , pointMarkerLength , pointMarkerStroke
2014-07-13 19:31:34 -04:00
totalWidth = contentWidth + contributionsToTotalSize . reduce (a, b) -> a + b
totalHeight = contentHeight + contributionsToTotalSize . reduce (a, b) -> a + b
2014-07-19 20:12:56 -04:00
2014-08-13 00:01:35 -04:00
if @ isNearTopEdge ( )
2014-08-18 14:08:54 -07:00
verticalEdge =
2014-08-13 00:01:35 -04:00
startPos: - fullPointMarkerLength
posShift: - contentHeight + 4
else
verticalEdge =
startPos: - totalHeight + fullPointMarkerLength
posShift: contentHeight
if @ isNearRightEdge ( )
horizontalEdge =
startPos: - totalWidth + fullPointMarkerLength
posShift: totalWidth
else
horizontalEdge =
startPos: - fullPointMarkerLength
posShift: 0
@ orient verticalEdge , horizontalEdge , totalHeight , totalWidth
isNearTopEdge: ->
2014-08-18 14:08:54 -07:00
yRatio = 1 - ( @ camera . worldViewport . y - @ lastPos . y ) / @ camera . worldViewport . height
yRatio > 0.9
2014-08-13 00:01:35 -04:00
isNearRightEdge: ->
2014-08-18 14:08:54 -07:00
xRatio = ( @ lastPos . x - @ camera . worldViewport . x ) / @ camera . worldViewport . width
xRatio > 0.85
2014-08-13 00:01:35 -04:00
orient: (verticalEdge, horizontalEdge, totalHeight, totalWidth) ->
@label.regY = @background.regY = verticalEdge . posShift
@label.regX = @background.regX = horizontalEdge . posShift
2014-08-16 18:29:31 -04:00
@ cache horizontalEdge . startPos , verticalEdge . startPos , totalWidth , totalHeight
2014-07-13 19:31:34 -04:00
2014-08-04 00:14:27 -04:00
updateCoordinates: (contentWidth, contentHeight, offset) ->
2014-07-13 19:31:34 -04:00
# Center label horizontally and vertically
2014-08-04 00:14:27 -04:00
@label.x = contentWidth / 2 - ( @ label . getMeasuredWidth ( ) / 2 ) + offset
@label.y = contentHeight / 2 - ( @ label . getMeasuredHeight ( ) / 2 ) - offset
2014-07-13 19:31:34 -04:00
2014-03-28 11:55:24 -07:00
@ background . graphics
. clear ( )
2014-07-01 10:16:26 +08:00
. beginFill ( ' rgba(0,0,0,0.4) ' )
. beginStroke ( ' rgba(0,0,0,0.6) ' )
2014-07-13 19:31:34 -04:00
. setStrokeStyle ( backgroundStroke = 1 )
2014-08-04 00:14:27 -04:00
. drawRoundRect ( offset , - offset , contentWidth , contentHeight , radius = 2.5 )
2014-03-28 11:55:24 -07:00
. endFill ( )
. endStroke ( )
2014-08-04 00:14:27 -04:00
contributionsToTotalSize = [ offset , backgroundStroke ]
2014-07-13 19:31:34 -04:00
2014-07-19 20:12:56 -04:00
updatePointMarker: (centerX, centerY, length, strokeSize) ->
strokeStyle = ' square '
2014-07-13 19:31:34 -04:00
@ pointMarker . graphics
2014-07-19 20:12:56 -04:00
. beginStroke ( ' rgb(255, 255, 255) ' )
. setStrokeStyle ( strokeSize , strokeStyle )
. moveTo ( centerX , centerY - length )
. lineTo ( centerX , centerY + length )
. moveTo ( centerX - length , centerY )
. lineTo ( centerX + length , centerY )
2014-07-13 19:31:34 -04:00
. endStroke ( )
2014-07-19 20:12:56 -04:00
contributionsToTotalSize = [ strokeSize , length ]
2014-03-28 11:55:24 -07:00
2014-01-03 10:32:13 -08:00
show: =>
2014-02-22 16:58:54 -08:00
return unless @ mouseInBounds and @ lastPos and not @ destroyed
2014-05-14 15:29:55 -07:00
@label.text = " {x: #{ @ lastPos . x } , y: #{ @ lastPos . y } } "
2014-07-19 20:12:56 -04:00
@ updateSize ( )
2014-01-03 10:32:13 -08:00
sup = @ camera . worldToSurface @ lastPos
@x = sup . x
2014-07-13 19:31:34 -04:00
@y = sup . y
2014-03-28 11:55:24 -07:00
@ addChild @ background
2014-01-03 10:32:13 -08:00
@ addChild @ label
2014-11-19 13:23:55 -08:00
@ addChild @ pointMarker unless @ placingFlag
2014-08-16 18:29:31 -04:00
@ updateCache ( )
2014-01-03 10:32:13 -08:00
Backbone . Mediator . publish ' surface:coordinates-shown ' , { }