2013-04-28 02:37:42 +08:00
|
|
|
define [
|
|
|
|
'jquery',
|
|
|
|
'underscore',
|
|
|
|
'backbone',
|
|
|
|
'raphael',
|
|
|
|
'globals',
|
2013-04-28 04:54:22 +08:00
|
|
|
'cs!utils',
|
|
|
|
'cs!models/whiteboard_tool'
|
|
|
|
], ($, _, Backbone, Raphael, globals, Utils, WhiteboardToolModel) ->
|
2013-04-28 02:37:42 +08:00
|
|
|
|
|
|
|
# A rectangle in the whiteboard
|
2013-04-28 04:54:22 +08:00
|
|
|
class WhiteboardRectModel extends WhiteboardToolModel
|
2013-04-28 02:37:42 +08:00
|
|
|
|
|
|
|
initialize: (@paper) ->
|
2013-04-28 04:54:22 +08:00
|
|
|
super @paper
|
2013-04-28 04:06:38 +08:00
|
|
|
|
|
|
|
# the defintion of this shape, kept so we can redraw the shape whenever needed
|
2013-04-28 02:37:42 +08:00
|
|
|
# format: x1, y1, x2, y2, stroke color, thickness
|
|
|
|
@definition = [0, 0, 0, 0, "#000", "0px"]
|
|
|
|
|
|
|
|
# Creates a rectangle in the paper
|
|
|
|
# @param {number} x the x value of the top left corner
|
|
|
|
# @param {number} y the y value of the top left corner
|
|
|
|
# @param {string} colour the colour of the object
|
|
|
|
# @param {number} thickness the thickness of the object's line(s)
|
2014-02-20 00:00:17 +08:00
|
|
|
make: (startingData) ->
|
2014-02-22 05:01:20 +08:00
|
|
|
console.log "make startingData"+ startingData
|
2014-02-20 00:00:17 +08:00
|
|
|
x = startingData.payload.data.coordinate.first_x
|
|
|
|
y = startingData.payload.data.coordinate.first_y
|
|
|
|
color = startingData.payload.data.line.color
|
|
|
|
thickness = startingData.payload.data.line.weight
|
2013-11-20 03:04:25 +08:00
|
|
|
|
2013-04-28 04:54:22 +08:00
|
|
|
@obj = @paper.rect(x * @gw + @xOffset, y * @gh + @yOffset, 0, 0, 1)
|
2013-11-20 00:34:34 +08:00
|
|
|
@obj.attr Utils.strokeAndThickness(color, thickness)
|
2013-04-28 02:37:42 +08:00
|
|
|
@definition =
|
|
|
|
shape: "rect"
|
2013-04-28 04:06:38 +08:00
|
|
|
data: [x, y, 0, 0, @obj.attrs["stroke"], @obj.attrs["stroke-width"]]
|
2013-04-28 02:37:42 +08:00
|
|
|
@obj
|
|
|
|
|
|
|
|
# Update the rectangle dimensions
|
|
|
|
# @param {number} x1 the x value of the top left corner
|
|
|
|
# @param {number} y1 the y value of the top left corner
|
|
|
|
# @param {number} x2 the x value of the bottom right corner
|
|
|
|
# @param {number} y2 the y value of the bottom right corner
|
2013-08-01 23:10:59 +08:00
|
|
|
# @param {boolean} square (draw a square or not)
|
2014-02-20 00:00:17 +08:00
|
|
|
update: (startingData) ->
|
|
|
|
x1 = startingData.payload.data.coordinate.first_x
|
|
|
|
y1 = startingData.payload.data.coordinate.first_y
|
|
|
|
x2 = startingData.payload.data.coordinate.last_x
|
|
|
|
y2 = startingData.payload.data.coordinate.last_y
|
|
|
|
square = startingData.payload.data.square
|
2013-04-28 02:37:42 +08:00
|
|
|
if @obj?
|
2013-04-28 10:19:25 +08:00
|
|
|
[x1, x2] = [x2, x1] if x2 < x1
|
2013-10-01 02:23:29 +08:00
|
|
|
[x1, x2] = [x2, x1] if x2 < x1
|
|
|
|
|
|
|
|
if y2 < y1
|
|
|
|
[y1, y2] = [y2, y1]
|
|
|
|
reversed = true
|
|
|
|
|
|
|
|
if square
|
|
|
|
if reversed #if reveresed, the y1 coordinate gets updated, not the y2 coordinate
|
|
|
|
y1 = y2 - (x2 - x1) * @gw / @gh
|
|
|
|
else
|
|
|
|
y2 = y1 + (x2 - x1) * @gw / @gh
|
|
|
|
|
2013-04-28 02:37:42 +08:00
|
|
|
x = x1 * @gw + @xOffset
|
|
|
|
y = y1 * @gh + @yOffset
|
|
|
|
width = (x2 * @gw + @xOffset) - x
|
|
|
|
height = (y2 * @gh + @yOffset) - y
|
2013-10-01 02:23:29 +08:00
|
|
|
#if !square
|
|
|
|
@obj.attr
|
|
|
|
x: x
|
|
|
|
y: y
|
|
|
|
width: width
|
|
|
|
height: height
|
|
|
|
###else
|
2013-08-01 23:10:59 +08:00
|
|
|
@obj.attr
|
|
|
|
x: x
|
|
|
|
y: y
|
|
|
|
width: width
|
2013-10-01 02:23:29 +08:00
|
|
|
height: width###
|
2013-04-29 04:58:37 +08:00
|
|
|
|
|
|
|
# we need to update all these values, specially for when shapes are drawn backwards
|
|
|
|
@definition.data[0] = x1
|
|
|
|
@definition.data[1] = y1
|
2013-04-28 02:37:42 +08:00
|
|
|
@definition.data[2] = x2
|
|
|
|
@definition.data[3] = y2
|
|
|
|
|
|
|
|
# Draw a rectangle on the paper
|
|
|
|
# @param {number} x1 the x value of the top left corner
|
|
|
|
# @param {number} y1 the y value of the top left corner
|
|
|
|
# @param {number} x2 the x value of the bottom right corner
|
|
|
|
# @param {number} y2 the y value of the bottom right corner
|
|
|
|
# @param {string} colour the colour of the object
|
|
|
|
# @param {number} thickness the thickness of the object's line(s)
|
|
|
|
draw: (x1, y1, x2, y2, colour, thickness) ->
|
2013-04-28 10:19:25 +08:00
|
|
|
[x1, x2] = [x2, x1] if x2 < x1
|
|
|
|
[y1, y2] = [y2, y1] if y2 < y1
|
|
|
|
|
2013-04-28 02:37:42 +08:00
|
|
|
x = x1 * @gw
|
|
|
|
y = y1 * @gh
|
2013-04-28 02:58:20 +08:00
|
|
|
r = @paper.rect(x + @xOffset, y + @yOffset, (x2 * @gw) - x, (y2 * @gh) - y, 1)
|
2013-04-28 02:37:42 +08:00
|
|
|
r.attr Utils.strokeAndThickness(colour, thickness)
|
|
|
|
r
|
|
|
|
|
2013-04-28 04:54:22 +08:00
|
|
|
# Creating a rectangle has started
|
|
|
|
# @param {number} x the x value of cursor at the time in relation to the left side of the browser
|
|
|
|
# @param {number} y the y value of cursor at the time in relation to the top of the browser
|
|
|
|
# TODO: moved here but not finished
|
|
|
|
dragOnStart: (x, y) ->
|
|
|
|
# sx = (@paperWidth - @gw) / 2
|
|
|
|
# sy = (@paperHeight - @gh) / 2
|
|
|
|
# # find the x and y values in relation to the whiteboard
|
|
|
|
# @cx2 = (x - @containerOffsetLeft - sx + @xOffset) / @paperWidth
|
|
|
|
# @cy2 = (y - @containerOffsetTop - sy + @yOffset) / @paperHeight
|
|
|
|
# globals.connection.emitMakeShape "rect",
|
|
|
|
# [ @cx2, @cy2, @currentColour, @currentThickness ]
|
|
|
|
|
|
|
|
# Adjusting rectangle continues
|
|
|
|
# @param {number} dx the difference in the x value at the start as opposed to the x value now
|
|
|
|
# @param {number} dy the difference in the y value at the start as opposed to the y value now
|
|
|
|
# @param {number} x the x value of cursor at the time in relation to the left side of the browser
|
|
|
|
# @param {number} y the y value of cursor at the time in relation to the top of the browser
|
|
|
|
# @param {Event} e the mouse event
|
|
|
|
# TODO: moved here but not finished
|
|
|
|
dragOnMove: (dx, dy, x, y, e) ->
|
|
|
|
# # if shift is pressed, make it a square
|
|
|
|
# dy = dx if @shiftPressed
|
|
|
|
# dx = dx / @paperWidth
|
|
|
|
# dy = dy / @paperHeight
|
|
|
|
# # adjust for negative values as well
|
|
|
|
# if dx >= 0
|
|
|
|
# x1 = @cx2
|
|
|
|
# else
|
|
|
|
# x1 = @cx2 + dx
|
|
|
|
# dx = -dx
|
|
|
|
# if dy >= 0
|
|
|
|
# y1 = @cy2
|
|
|
|
# else
|
|
|
|
# y1 = @cy2 + dy
|
|
|
|
# dy = -dy
|
|
|
|
# globals.connection.emitUpdateShape "rect", [ x1, y1, dx, dy ]
|
|
|
|
|
|
|
|
# When rectangle finished being drawn
|
|
|
|
# @param {Event} e the mouse event
|
|
|
|
# TODO: moved here but not finished
|
|
|
|
dragOnEnd: (e) ->
|
|
|
|
# if @obj?
|
|
|
|
# attrs = @obj.attrs
|
|
|
|
# if attrs?
|
|
|
|
# globals.connection.emitPublishShape "rect",
|
|
|
|
# [ attrs.x / @gw, attrs.y / @gh, attrs.width / @gw, attrs.height / @gh,
|
|
|
|
# @currentColour, @currentThickness ]
|
|
|
|
# @obj = null
|
2013-04-28 02:37:42 +08:00
|
|
|
|
|
|
|
WhiteboardRectModel
|