aboutsummaryrefslogtreecommitdiff
path: root/js/tools/LineTool.js
blob: 4ec327b510a58e75714da8ad2d6a4fe22b51dfe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Motorola Mobility LLC nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */

var Montage =   require("montage/core/core").Montage,
    ShapeTool = require("js/tools/ShapeTool").ShapeTool,
    DrawingToolBase = require("js/tools/drawing-tool-base").DrawingToolBase,
    NJUtils = require("js/lib/NJUtils").NJUtils,
    TagTool = require("js/tools/TagTool").TagTool,
    ShapesController =  require("js/controllers/elements/shapes-controller").ShapesController,
    ShapeModel = require("js/models/shape-model").ShapeModel;

var Line = require("js/lib/geom/line").Line;
var MaterialsModel = require("js/models/materials-model").MaterialsModel;

exports.LineTool = Montage.create(ShapeTool, {
    _toolID: { value: "lineTool" },
    _imageID: { value: "lineToolImg" },
    _toolImageClass: { value: "lineToolUp" },
    _selectedToolImageClass: { value: "lineToolDown" },
    _toolTipText: { value: "Line Tool (L)" },

    _tmpDrawIndex : { value : 1, writable:true},

    _mode: {value: null, writable:true},

    // Need to keep track of current mouse position for KEY modifiers event which do not have mouse coordinates
    _currentX: {value: 0, writable: true},
    _currentY: {value: 0, writable: true},
    _lineView: {value: null, writable:true},
    _ovalView: {value: null, writable:true},

    _strokeSize: { value: 1 },
    _strokeColor: { value: null },

    HandleLeftButtonDown:
    {
        value: function (event)
        {
            if(this._canDraw) {
                this._isDrawing = true;
            }

            this._strokeSize = ShapesController.GetValueInPixels(this.options.strokeSize.value, this.options.strokeSize.units, null);
            if (this.options.stroke.color) {
                if( (this.options.stroke.colorMode === "gradient") || (this.options.stroke.colorMode === "nocolor") ) {
                    this._strokeColor = [0,0,0,1];
                } else {
                    this._strokeColor = this.options.stroke.color.css;
                }
            } else {
                this._strokeColor = [0,0,0,1];
            }

            this.startDraw(event);
        }
    },

    HandleLeftButtonUp: {
        value: function (event) {
            var slope = this._getSlope(),
                canvas,
                xAdj = 0,
                yAdj = 0,
                w,
                h;

            if(slope) {
                this.drawData = this.getDrawingData();
                if(this.drawData) {
                    w = Math.floor(this.drawData.width);
                    h = Math.floor(this.drawData.height);
                    // set the dimensions
                    if(slope === "horizontal") {
                        h = Math.max(this._strokeSize, 1);
                        w = Math.max(w, 1);
                    } else if(slope === "vertical") {
                        w = Math.max(this._strokeSize, 1);
                        h = Math.max(h, 1);
                    } else {
                        // else make the line's stroke fit inside the canvas by growing the canvas
                        var theta = Math.atan(slope);
                        xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta));
                        yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta));

                        w += ~~(xAdj*2);
                        h += ~~(yAdj*2);
                    }

                    if(!this._useExistingCanvas()) {

                        canvas = document.application.njUtils.make("canvas", {"data-RDGE-id": NJUtils.generateRandom()}, this.application.ninja.currentDocument);

                        var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, this.drawData);
                        this.application.ninja.elementMediator.addElements(canvas, styles);
                    } else {
                        canvas = this._targetedElement;
                        if (!canvas.getAttribute( "data-RDGE-id" ))
                            canvas.setAttribute( "data-RDGE-id", NJUtils.generateRandom() );
                        canvas.elementModel.controller = ShapesController;
                        if(!canvas.elementModel.shapeModel) {
                            canvas.elementModel.shapeModel = Montage.create(ShapeModel);
                        }
                        this.RenderShape(w, h, this.drawData.planeMat, this.drawData.midPt, canvas, slope, xAdj, yAdj);
                    }
                }
            }

            this.endDraw(event);

            this._isDrawing = false;
            this._hasDraw=false;

            this.DrawHandles();
        }
    },

    onAddElements: {
        value: function(el) {
            var xAdj = 0, yAdj = 0, w, h, slope = this._getSlope();

            if(this.drawData) {
                // set the dimensions
                w = Math.floor(this.drawData.width);
                h = Math.floor(this.drawData.height);
                if(slope === "horizontal") {
                    h = Math.max(this._strokeSize, 1);
                    w = Math.max(w, 1);
                } else if(slope === "vertical") {
                    w = Math.max(this._strokeSize, 1);
                    h = Math.max(h, 1);
                } else {
                    // else make the line's stroke fit inside the canvas by growing the canvas
                    var theta = Math.atan(slope);
                    xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta));
                    yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta));

                    w += ~~(xAdj*2);
                    h += ~~(yAdj*2);
                }

                this.RenderShape(w, h, this.drawData.planeMat, this.drawData.midPt, el, slope, xAdj, yAdj);
            }
        }
    },

    _getSlope: {
        value: function() {
            var hitRec0 = this._mouseDownHitRec,
                hitRec1 = this._mouseUpHitRec,
                slope,
                dx,
                dy;

            if (hitRec0 && hitRec1) {
                var p0 = hitRec0.getLocalPoint(),
                    p1 = hitRec1.getLocalPoint();

                dx = Math.floor(p0[0] - p1[0]);
                dy = Math.floor(p0[1] - p1[1]);

                if( (dx === 0) && (dy === 0) ) {
                    return null;
                }

                // check for divide by 0 for vertical line:
                if(dx === 0) {
                    // vertical line
                    slope = "vertical";
                } else if (dy === 0) {
                    // horizontal line
                    slope = "horizontal";
                } else {
                    // if slope is positive, draw a line from top-left to bottom-right
                    slope = dy/dx;
                }
            }

            return slope;
        }
    },

    _doDraw: {
        value: function () {
            if (this.mouseDownHitRec !== null) {
                DrawingToolBase.stageComponent = this.application.ninja.stage;
                DrawingToolBase.drawLine(this.mouseDownHitRec, this.mouseUpHitRec, this._strokeSize, this._strokeColor);
            }
        }
    },

    HandleShiftKeyDown: {
        value: function (event) {
            if (this._isDrawing) {
                var slope = Math.abs((this.downPoint.y - this.currentY)/(this.downPoint.x - this.currentX));
                // If slope is less than 0.5, make it a horizontal line
                if(slope < 0.5)
                {
                    this._mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(this.currentX, this.downPoint.y, false, this._mouseDownHitRec);
                }
                // If slope is greater than 2, make it a vertical line
                else if(slope > 2)
                {
                    this._mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(this.downPoint.x, this.currentY, false, this._mouseDownHitRec);
                }
                // make it a 45 degree line
                else
                {
                    var square = this.toSquare(this.downPoint.x, this.currentX, this.downPoint.y, this.currentY);
                    this._mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(square[0] + square[2], square[1] + square[3], false, this._mouseDownHitRec);
                }
                this._doDraw();
            }
        }
    },

    HandleShiftKeyUp: {
        value: function () {
            if (this._isDrawing) {
                this.mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(this.currentX, this.currentY, false, this.mouseDownHitRec);
                this._doDraw();
            }
        }
    },

    RenderShape: {
        value: function (w, h, planeMat, midPt, canvas, slope, xAdj, yAdj)
        {

            var strokeStyleIndex = this.options.strokeStyleIndex;
            var strokeStyle = this.options.strokeStyle;
            var strokeSize = this._strokeSize;

            var left = Math.round(midPt[0] - 0.5*w);
            var top = Math.round(midPt[1] - 0.5*h);

            var strokeColor = this.options.stroke.webGlColor;
            // for default stroke and fill/no materials
            var strokeMaterial = null;
            var strokeM = null;

            if(this.options.use3D)
            {
                strokeM = this.options.strokeMaterial;
                if(strokeM)
                {
                    strokeMaterial = Object.create(MaterialsModel.getMaterial(strokeM));
                }
                if (strokeMaterial && this.options.stroke.color && (strokeMaterial.gradientType === this.options.stroke.color.gradientMode)) {
                    strokeColor = {gradientMode:strokeMaterial.gradientType, color:this.options.stroke.color.stops};
                } else {
                strokeColor = ShapesController.getMaterialColor(strokeM) || strokeColor;
            }
            }

            var world = this.getGLWorld(canvas, this.options.use3D);

            var xOffset = ((left - canvas.offsetLeft + w/2) - canvas.width/2);
            var yOffset = (canvas.height/2 - (top - canvas.offsetTop + h/2));

            var line = Object.create(Line, {});
            line.init(world, xOffset, yOffset, w, h, slope, strokeSize, strokeColor, strokeMaterial, strokeStyle, xAdj, yAdj);

            world.addObject(line);
            world.render();

            canvas.elementModel.shapeModel.shapeCount++;
            if(canvas.elementModel.shapeModel.shapeCount === 1)
            {
//                canvas.elementModel.selection = "Line";
                canvas.elementModel.pi = "LinePi";
                canvas.elementModel.shapeModel.strokeSize = this.options.strokeSize.value + " " + this.options.strokeSize.units;

                canvas.elementModel.shapeModel.strokeStyleIndex = strokeStyleIndex;
                canvas.elementModel.shapeModel.strokeStyle = strokeStyle;

                canvas.elementModel.shapeModel.GLGeomObj = line;
                canvas.elementModel.shapeModel.useWebGl = this.options.use3D;
                canvas.elementModel.shapeModel.slope = slope;
            }
            else
            {
                // TODO - update the shape's info only.  shapeModel will likely need an array of shapes.
            }

            // TODO - This needs to be moved into geom obj's init routine instead of here
            if(!strokeM) {
                this.setColor(canvas, this.options.stroke, false, "lineTool");
            }

            if(canvas.elementModel.isShape)
            {
                this.application.ninja.selectionController.selectElement(canvas);
            }

        }
    }
});