This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
259 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/assets/javascripts/components/mark/tools/rectangle-tool/delete-button.cjsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
React = require 'react' | ||
|
||
RADIUS = 8 | ||
STROKE_COLOR = 'white' | ||
FILL_COLOR = 'black' | ||
STROKE_WIDTH = 1.5 | ||
|
||
CROSS_PATH = " | ||
M #{-1 * RADIUS * 0.7 } 0 | ||
L #{RADIUS * 0.7 } 0 | ||
M 0 #{-1 * RADIUS * 0.7 } | ||
L 0 #{RADIUS * 0.7 } | ||
" | ||
|
||
DESTROY_TRANSITION_DURATION = 0 | ||
|
||
module.exports = React.createClass | ||
displayName: 'DeleteButton' | ||
|
||
getDefaultProps: -> | ||
x: 0 | ||
y: 0 | ||
rotate: 0 | ||
|
||
render: -> | ||
transform = " | ||
translate(#{@props.x+40}, #{@props.y-40}) | ||
rotate(#{@props.rotate}) | ||
scale(#{1 / @props.tool.props.xScale}, #{1 / @props.tool.props.yScale}) | ||
" | ||
|
||
<g className="clickable drawing-tool-delete-button" transform={transform} stroke={STROKE_COLOR} strokeWidth={STROKE_WIDTH} onClick={@destroyTool}> | ||
<circle r={RADIUS} fill={FILL_COLOR} /> | ||
<path d={CROSS_PATH} transform="rotate(45)" /> | ||
</g> | ||
|
||
destroyTool: -> | ||
@props.tool.setState destroying: true, => | ||
setTimeout @props.tool.props.onDestroy, DESTROY_TRANSITION_DURATION |
14 changes: 14 additions & 0 deletions
14
app/assets/javascripts/components/mark/tools/rectangle-tool/drag-handle.cjsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
React = require 'react' | ||
Draggable = require 'lib/draggable' | ||
|
||
RADIUS = 10 | ||
OVERSHOOT = 4 | ||
|
||
module.exports = React.createClass | ||
displayName: 'DragHandle' | ||
|
||
render: -> | ||
|
||
<Draggable onDrag = {@props.onDrag} > | ||
<circle className="drag-handle" r={RADIUS} fill="red" cx="#{@props.x}", cy="#{@props.y}"} stroke="transparent" /> | ||
</Draggable> |
188 changes: 188 additions & 0 deletions
188
app/assets/javascripts/components/mark/tools/rectangle-tool/index.cjsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# @cjsx React.DOM | ||
React = require 'react' | ||
Draggable = require 'lib/draggable' | ||
Classification = require 'models/classification' | ||
DragHandle = require './drag-handle' | ||
DeleteButton = require './delete-button' | ||
|
||
SELECTED_RADIUS = 20 | ||
DELETE_BUTTON_ANGLE = 45 | ||
DELETE_BUTTON_DISTANCE = 9 / 10 | ||
DEBUG = false | ||
|
||
|
||
module.exports = React.createClass | ||
displayName: 'RectangleTool' | ||
|
||
propTypes: | ||
key: React.PropTypes.number.isRequired | ||
mark: React.PropTypes.object.isRequired | ||
|
||
initCoords: null | ||
|
||
statics: | ||
defaultValues: ({x, y}) -> | ||
x: x | ||
y: y | ||
width: 0 | ||
height: 0 | ||
|
||
initStart: ({x,y}, mark) -> | ||
@initCoords = {x,y} | ||
{x,y} | ||
|
||
initMove: (cursor, mark) -> | ||
if cursor.x > @initCoords.x | ||
width = cursor.x - mark.x | ||
x = mark.x | ||
else | ||
width = @initCoords.x - cursor.x | ||
x = cursor.x | ||
|
||
if cursor.y > @initCoords.y | ||
height = cursor.y - mark.y | ||
y = mark.y | ||
else | ||
height = @initCoords.y - cursor.y | ||
y = cursor.y | ||
|
||
{x, y, width, height} | ||
|
||
initCoords: null | ||
|
||
|
||
getInitialState: -> | ||
console.log 'Rectangle GET STATE' | ||
console.log "PROPS", @props | ||
mark = @props.mark | ||
unless mark.status? | ||
mark.status = 'mark' | ||
mark: mark | ||
# set up the state in order to caluclate the polyline as rectangle | ||
x: @props.mark.x | ||
y: @props.mark.y | ||
width: @props.width | ||
height: @props.height | ||
|
||
buttonDisabled: false | ||
lockTool: false | ||
|
||
componentWillReceiveProps: -> | ||
@setState x: @props.mark.x | ||
@setState y: @props.mark.y | ||
@setState width: @props.width | ||
@setState height: @props.height | ||
|
||
|
||
handleMainDrag: (e) -> | ||
# console.log "handleDrag (HD)" | ||
return if @state.lockTool | ||
{ x,y } = @props.getEventOffset e | ||
mark = @state.mark | ||
markHeight = mark.yLower - mark.yUpper | ||
# why? in handleMarkClick clickOffset.x is mark.x - ex. Is not the same event? | ||
# does @props.clickOffset.x represent the mark station before the current drag event? | ||
mark.x = x + @props.clickOffset.x # add initial click offset | ||
mark.y = y + @props.clickOffset.y | ||
|
||
# prevent dragging mark beyond image bounds | ||
return if ( y - markHeight/2 ) < 0 | ||
return if ( y + markHeight/2 ) > @props.imageHeight | ||
|
||
@setState x: mark.x | ||
@setState y: mark.y | ||
@setState mark: mark | ||
# , => @forceUpdate() | ||
|
||
# handleMainDrag: (e, d) -> | ||
# @props.mark.x += d.x / @props.xScale | ||
# @props.mark.y += d.y / @props.yScale | ||
# @props.onChange e | ||
|
||
handleX1Y1Drag: (e, d) -> | ||
@props.mark.x += d.x / @props.xScale | ||
@props.mark.y += d.y / @props.yScale | ||
@props.mark.width -= d.x / @props.xScale | ||
@props.mark.height -= d.y / @props.yScale | ||
@props.onChange e | ||
|
||
handleX1Y2Drag: (e, d) -> | ||
@props.mark.x += d.x / @props.xScale | ||
@props.mark.width -= d.x / @props.xScale | ||
@props.mark.height += d.y / @props.yScale | ||
@props.onChange e | ||
|
||
handleX2Y1Drag: (e, d) -> | ||
@props.mark.y += d.y / @props.yScale | ||
@props.mark.width += d.x / @props.xScale | ||
@props.mark.height -= d.y / @props.yScale | ||
@props.onChange e | ||
|
||
handleX2Y2Drag: (e, d) -> | ||
@props.mark.width += d.x / @props.xScale | ||
@props.mark.height += d.y / @props.yScale | ||
@props.onChange e | ||
|
||
getDeleteButtonPosition: -> | ||
theta = (DELETE_BUTTON_ANGLE) * (Math.PI / 180) | ||
x: (SELECTED_RADIUS / @props.xScale) * Math.cos theta | ||
y: -1 * (SELECTED_RADIUS / @props.yScale) * Math.sin theta | ||
|
||
handleMouseDown: -> | ||
console.log 'handleMouseDown()' | ||
@props.onSelect @props.mark | ||
|
||
render: -> | ||
# console.log "Render: Rectangle STATE @ Render", @state | ||
classString = "rectangleTool" | ||
x1 = @props.mark.x | ||
width = @props.mark.width | ||
x2 = x1 + width | ||
y1 = @props.mark.y | ||
height = @props.mark.height | ||
y2 = y1 + height | ||
|
||
points = [ | ||
[x1, y1].join ',' | ||
[x2, y1].join ',' | ||
[x2, y2].join ',' | ||
[x1, y2].join ',' | ||
[x1, y1].join ',' | ||
].join '\n' | ||
|
||
# console.log "render props.clickOffset", @props.clickOffset | ||
# console.log "render props", @props | ||
|
||
<g | ||
className = {classString} | ||
tool={this} | ||
onMouseDown={@props.onSelect unless @props.disabled} | ||
> | ||
|
||
<Draggable | ||
onStart = {@props.handleMarkClick.bind null, @props.mark} | ||
onDrag = {@handleMainDrag} > | ||
<polyline points={points} strokeWidth="5" stroke="orange" fill="none"/> | ||
</Draggable> | ||
|
||
{ if @props.selected | ||
<g> | ||
<DeleteButton tool={this} x={@state.x + (width * DELETE_BUTTON_DISTANCE)} y={@state.y} /> | ||
<DragHandle x={@props.mark.x} y={@props.mark.y} onDrag={@handleX1Y1Drag} /> | ||
<DragHandle x={x2} y={y1} onDrag={@handleX2Y1Drag} /> | ||
<DragHandle x={x2} y={y2} onDrag={@handleX2Y2Drag} /> | ||
<DragHandle x={x1} y={y2} onDrag={@handleX1Y2Drag} /> | ||
</g> | ||
} | ||
</g> | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters