aboutsummaryrefslogtreecommitdiff
path: root/js/tools/SelectionTool.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/tools/SelectionTool.js')
-rw-r--r--js/tools/SelectionTool.js829
1 files changed, 829 insertions, 0 deletions
diff --git a/js/tools/SelectionTool.js b/js/tools/SelectionTool.js
new file mode 100644
index 00000000..5f2a959f
--- /dev/null
+++ b/js/tools/SelectionTool.js
@@ -0,0 +1,829 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7var Montage = require("montage/core/core").Montage,
8 viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils,
9 vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils,
10 toolHandleModule = require("js/stage/tool-handle"),
11 ElementsMediator = require("js/mediators/element-mediator").ElementMediator,
12 DrawingToolBase = require("js/tools/drawing-tool-base").DrawingToolBase,
13 Keyboard = require("js/mediators/keyboard-mediator").Keyboard,
14 ModifierToolBase = require("js/tools/modifier-tool-base").ModifierToolBase;
15
16var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, {
17 drawingFeedback: { value: { mode: "Draw2D", type: "" } },
18
19 _canOperateOnStage: { value: true},
20 _isSelecting: {value: false, writable:true},
21 _shiftMove: { value: 10},
22
23 _showTransformHandles: { value: false, enumerable: true },
24
25 _handleToolOptionsChange : {
26 value: function (event) {
27 this._showTransformHandles = event.detail.inTransformMode;
28 this.DrawHandles();
29 }
30 },
31
32 startDraw: {
33 value: function(event) {
34 if(!this.application.ninja.selectedElements.length)
35 {
36 this._isSelecting = true;
37 this._canSnap = false;
38 }
39 else
40 {
41 this._canSnap = true;
42 }
43
44 this.isDrawing = true;
45 this.application.ninja.stage.showSelectionBounds = false;
46
47 if(this._canSnap)
48 {
49 this.initializeSnapping(event);
50 }
51 else
52 {
53 this.drawWithoutSnapping(event);
54 }
55 }
56 },
57
58 drawSelectionMarquee: {
59 value: function(event) {
60 var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas,
61 new WebKitPoint(event.pageX, event.pageY));
62
63 if(this._isSpace) {
64 this._currentDX = point.x - this._currentX;
65 this._currentDY = point.y - this._currentY;
66
67 this.downPoint.x += this._currentDX;
68 this.downPoint.y += this._currentDY;
69 this.currentX += this._currentDX;
70 this.currentY += this._currentDY;
71
72 DrawingToolBase.draw2DRectangle(this.downPoint.x,this.downPoint.y,this.currentX - this.downPoint.x,this.currentY - this.downPoint.y);
73 } else {
74 this._currentX = point.x;
75 this._currentY = point.y;
76
77 DrawingToolBase.draw2DRectangle(this.downPoint.x,this.downPoint.y,point.x - this.downPoint.x,point.y - this.downPoint.y);
78 }
79 }
80 },
81
82 HandleMouseMove: {
83 value: function(event) {
84 if(this._escape) {
85 this._escape = false;
86 this.isDrawing = true;
87 }
88
89 if(this._isSelecting) {
90 // Draw the Selection Marquee
91 this.drawSelectionMarquee(event);
92 }
93 else
94 {
95 if(this.isDrawing) {
96 this._hasDraw = true; // Flag for position of element
97 this.doDraw(event);
98 } else {
99 this._showFeedbackOnMouseMove(event);
100 // if(this._canSnap)
101 // {
102 // this.doSnap(event);
103 // }
104 }
105
106 this.DrawHandles(this._delta);
107 if(this._canSnap && this._isDrawing)
108 {
109 this.application.ninja.stage.stageDeps.snapManager.drawLastHit();
110 }
111 }
112 }
113 },
114
115 HandleLeftButtonUp: {
116 value: function(event) {
117 var selectedItems,
118 point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas,
119 new WebKitPoint(event.pageX, event.pageY));
120
121 this.isDrawing = false;
122 this.application.ninja.stage.showSelectionBounds = true;
123 if(this._escape) {
124 this._escape = false;
125 this._isSelecting = false;
126 return;
127 }
128
129
130 if(this._isSelecting) {
131 this._isSelecting = false;
132
133 // Don't do the marque selection if the mouse has not moved
134 if(this.downPoint.x != point.x && this.downPoint.y != point.y) {
135 var box = [];
136 selectedItems = [];
137
138 box[0] = this.downPoint.x - this.application.ninja.stage.documentOffsetLeft + this.application.ninja.stage.scrollLeft;
139 box[1] = this.downPoint.y - this.application.ninja.stage.documentOffsetTop + this.application.ninja.stage.scrollTop;
140 box[2] = box[0] + (point.x - this.downPoint.x);
141 box[3] = box[1] + (point.y - this.downPoint.y);
142 box = this.absoluteRectangle(box[0], box[1],box[2],box[3]);
143
144
145 //selectionManagerModule.selectionManager.marqueeSelection(box);
146 var childNodes = this.application.ninja.currentDocument.documentRoot.childNodes;
147 childNodes = Array.prototype.slice.call(childNodes, 0);
148 childNodes.forEach(function(item) {
149 if(item.nodeType == 1 && SelectionTool._simpleCollisionDetection(item, box)) {
150 selectedItems.push(item);
151 }
152 });
153
154 this.application.ninja.selectionController.selectElements(selectedItems);
155
156 }
157
158 this.endDraw(event);
159 return;
160 }
161
162
163
164 if(this._hasDraw)
165 {
166 if(this._activateOriginHandle)
167 {
168 this._setTransformOrigin(true);
169 }
170 else if ( ((this.downPoint.x - point.x) !== 0) ||
171 ((this.downPoint.y - point.y) !== 0) )
172 {
173 this._updateTargets(true);
174 }
175
176 this._hasDraw = false;
177 }
178 if(this._handleMode !== null)
179 {
180 this._handleMode = null;
181 this._delta = null;
182 this.DrawHandles();
183 console.log( "move: (" + dx + ", " + dy + ")" );
184 }
185
186 this.endDraw(event);
187 }
188 },
189
190 HandleDoubleClick: {
191 value: function(event) {
192 /*
193 var selectedObject = stageManagerModule.stageManager.GetObjectFromPoint(event.layerX, event.layerY, this._canOperateOnStage);
194
195 if(selectedObject) {
196 if(selectionManagerModule.selectionManager.findSelectedElement(selectedObject) === -1) {
197 selectionManagerModule.selectionManager.setSingleSelection(selectedObject);
198 }
199 }
200 */
201
202 // Temporary Code for Breadcrumb
203 if(this.application.ninja.selectedElements.length > 0) {
204 this.application.ninja.currentSelectedContainer = this.application.ninja.selectedElements[0]._element;
205 } else {
206 console.log(this.application.ninja.currentDocument.documentRoot.uuid);
207 this.application.ninja.currentSelectedContainer = this.application.ninja.currentDocument.documentRoot;
208 }
209
210 }
211 },
212
213 HandleKeyPress: {
214 value: function(event){
215 var inc;
216
217 if (!(event.target instanceof HTMLInputElement)) {
218 if(this.application.ninja.selectedElements.length !== 0) {
219 inc = (event.shiftKey) ? this._shiftMove : 1;
220
221 switch(event.keyCode) {
222 case Keyboard.LEFT:
223 var newLeft = [];
224 var leftArr = this.application.ninja.selectedElements.map(function(item) {
225 newLeft.push( (parseFloat(ElementsMediator.getProperty(item._element, "left")) - inc) + "px" );
226 return ElementsMediator.getProperty(item._element, "left");
227 });
228
229 ElementsMediator.setProperty(this.application.ninja.selectedElements, "left", newLeft , "Change", "selectionTool", leftArr);
230 break;
231 case Keyboard.UP:
232 var newTop = [];
233 var topArr = this.application.ninja.selectedElements.map(function(item) {
234 newTop.push( (parseFloat(ElementsMediator.getProperty(item._element, "top")) - inc) + "px" );
235 return ElementsMediator.getProperty(item._element, "top");
236 });
237
238 ElementsMediator.setProperty(this.application.ninja.selectedElements, "top", newTop , "Change", "selectionTool", topArr);