1 | package org.apollo;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.LinkedHashSet;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.apollo.gio.gesture.data.TrackAdjustmentGestureData;
|
---|
11 | import org.apollo.util.Mutable;
|
---|
12 | import org.apollo.widgets.LinkedTrack;
|
---|
13 | import org.apollo.widgets.SampledTrack;
|
---|
14 | import org.apollo.widgets.TrackWidgetCommons;
|
---|
15 | import org.expeditee.core.Dimension;
|
---|
16 | import org.expeditee.core.Point;
|
---|
17 | import org.expeditee.gio.EcosystemManager;
|
---|
18 | import org.expeditee.gio.gesture.Gesture;
|
---|
19 | import org.expeditee.gio.gesture.Gesture.GestureType;
|
---|
20 | import org.expeditee.gio.gesture.GestureAction;
|
---|
21 | import org.expeditee.gio.gesture.GestureListener;
|
---|
22 | import org.expeditee.gio.gesture.StandardGestureActions;
|
---|
23 | import org.expeditee.gio.gesture.StandardGestureActions.StandardGestureType;
|
---|
24 | import org.expeditee.gio.gesture.data.ItemSpecificGestureData;
|
---|
25 | import org.expeditee.gio.gesture.data.PickUpGestureData;
|
---|
26 | import org.expeditee.gio.input.KBMInputEvent.Key;
|
---|
27 | import org.expeditee.gio.input.StandardInputEventListeners;
|
---|
28 | import org.expeditee.gui.DisplayController;
|
---|
29 | import org.expeditee.gui.Frame;
|
---|
30 | import org.expeditee.gui.FrameIO;
|
---|
31 | import org.expeditee.gui.FreeItems;
|
---|
32 | import org.expeditee.items.Dot;
|
---|
33 | import org.expeditee.items.Item;
|
---|
34 | import org.expeditee.items.Item.HighlightMode;
|
---|
35 | import org.expeditee.items.ItemUtils;
|
---|
36 | import org.expeditee.items.Line;
|
---|
37 | import org.expeditee.items.Text;
|
---|
38 | import org.expeditee.items.UserAppliedPermission;
|
---|
39 | import org.expeditee.items.widgets.Widget;
|
---|
40 | import org.expeditee.items.widgets.WidgetCorner;
|
---|
41 | import org.expeditee.items.widgets.WidgetEdge;
|
---|
42 |
|
---|
43 | public class ApolloGestureActions implements GestureListener {
|
---|
44 | public enum ApolloGestureType {
|
---|
45 | ADJUST_INITIATION_TIME, ADJUST_VERTICAL_POSITION, CREATE_LINKED_TRACKS, MODULUS_POSITION, // TODO: Think of a
|
---|
46 | // better name.
|
---|
47 | // cts16
|
---|
48 | TOGGLE_QUALITY_GRAPHICS
|
---|
49 | }
|
---|
50 |
|
---|
51 | private static ApolloGestureActions _instance = null;
|
---|
52 |
|
---|
53 | public static ApolloGestureActions getInstance() {
|
---|
54 | if (_instance == null) {
|
---|
55 | _instance = new ApolloGestureActions();
|
---|
56 | }
|
---|
57 |
|
---|
58 | return _instance;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private HashMap<ApolloGestureType, GestureType> _gestureTypes;
|
---|
62 |
|
---|
63 | private HashMap<GestureType, GestureAction> _actions;
|
---|
64 |
|
---|
65 | private ApolloGestureActions() {
|
---|
66 | _gestureTypes = new HashMap<ApolloGestureType, GestureType>();
|
---|
67 | initialiseGestureTypes();
|
---|
68 | _actions = new HashMap<GestureType, GestureAction>();
|
---|
69 | initialiseActions();
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public void preGesture(final Gesture gesture) {
|
---|
74 | final GestureAction action = getGestureAction(gesture.getType());
|
---|
75 | if (action == null) {
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | action.prepare(gesture);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public void onGesture(final Gesture gesture) {
|
---|
83 | final GestureAction action = getGestureAction(gesture.getType());
|
---|
84 | if (action == null) {
|
---|
85 | return;
|
---|
86 | }
|
---|
87 | action.exec(gesture);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public void postGesture(final Gesture gesture) {
|
---|
92 | final GestureAction action = getGestureAction(gesture.getType());
|
---|
93 | if (action == null) {
|
---|
94 | return;
|
---|
95 | }
|
---|
96 | action.finalise(gesture);
|
---|
97 | }
|
---|
98 |
|
---|
99 | private void setGestureAction(GestureType type, GestureAction action) {
|
---|
100 | if (type == null)
|
---|
101 | return;
|
---|
102 |
|
---|
103 | _actions.put(type, action);
|
---|
104 | }
|
---|
105 |
|
---|
106 | private GestureAction getGestureAction(GestureType type) {
|
---|
107 | if (type == null)
|
---|
108 | return null;
|
---|
109 |
|
---|
110 | return _actions.get(type);
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void initialiseActions() {
|
---|
114 | // Set the ADJUST_INITIATION_TIME action
|
---|
115 | setGestureAction(gestureType(ApolloGestureType.ADJUST_INITIATION_TIME), new GestureAction() {
|
---|
116 | @Override
|
---|
117 | public void exec(Gesture gesture) {
|
---|
118 | TrackAdjustmentGestureData data = (TrackAdjustmentGestureData) gesture.getData();
|
---|
119 |
|
---|
120 | Widget selectedIW = null;
|
---|
121 |
|
---|
122 | if (data.getCurrentItem() != null) {
|
---|
123 | if (data.getCurrentItem() instanceof WidgetEdge) {
|
---|
124 | selectedIW = ((WidgetEdge) data.getCurrentItem()).getWidgetSource();
|
---|
125 | } else if (data.getCurrentItem() instanceof WidgetCorner) {
|
---|
126 | selectedIW = ((WidgetCorner) data.getCurrentItem()).getWidgetSource();
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (selectedIW != null)
|
---|
131 | adjustInitiationTime(selectedIW, data.getAdjustment());
|
---|
132 | }
|
---|
133 | });
|
---|
134 |
|
---|
135 | // Set the ADJUST_VERTICAL_POSITION action
|
---|
136 | setGestureAction(gestureType(ApolloGestureType.ADJUST_VERTICAL_POSITION), new GestureAction() {
|
---|
137 | @Override
|
---|
138 | public void exec(Gesture gesture) {
|
---|
139 | TrackAdjustmentGestureData data = (TrackAdjustmentGestureData) gesture.getData();
|
---|
140 |
|
---|
141 | Widget selectedIW = null;
|
---|
142 |
|
---|
143 | if (data.getCurrentItem() != null) {
|
---|
144 | if (data.getCurrentItem() instanceof WidgetEdge) {
|
---|
145 | selectedIW = ((WidgetEdge) data.getCurrentItem()).getWidgetSource();
|
---|
146 | } else if (data.getCurrentItem() instanceof WidgetCorner) {
|
---|
147 | selectedIW = ((WidgetCorner) data.getCurrentItem()).getWidgetSource();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (selectedIW != null)
|
---|
152 | adjustVerticalPosition(selectedIW, data.getAdjustment());
|
---|
153 | }
|
---|
154 | });
|
---|
155 |
|
---|
156 | // Set the CREATE_LINKED_TRACKS action
|
---|
157 | setGestureAction(gestureType(ApolloGestureType.CREATE_LINKED_TRACKS), new GestureAction() {
|
---|
158 | @Override
|
---|
159 | public void exec(Gesture gesture) {
|
---|
160 | ItemSpecificGestureData data = (ItemSpecificGestureData) gesture.getData();
|
---|
161 |
|
---|
162 | createLinkedTracks(data.getCurrentItem(), data.getCurrentItems());
|
---|
163 | }
|
---|
164 | });
|
---|
165 |
|
---|
166 | // Set the MOD_POSITION action
|
---|
167 | setGestureAction(gestureType(ApolloGestureType.MODULUS_POSITION), new GestureAction() {
|
---|
168 | @Override
|
---|
169 | public void exec(Gesture gesture) {
|
---|
170 | Frame current = DisplayController.getCurrentFrame();
|
---|
171 | Dimension windowSize = EcosystemManager.getGraphicsManager().getWindowSize();
|
---|
172 |
|
---|
173 | for (Item i : current.getItems()) {
|
---|
174 | Point pos = i.getPosition();
|
---|
175 | pos.setX(pos.getX() % windowSize.width);
|
---|
176 | pos.setY(pos.getY() % windowSize.height);
|
---|
177 | i.setPosition(pos);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | });
|
---|
181 |
|
---|
182 | // Set the TOGGLE_QUALITY_GRAPHICS action
|
---|
183 | setGestureAction(gestureType(ApolloGestureType.TOGGLE_QUALITY_GRAPHICS), new GestureAction() {
|
---|
184 | @Override
|
---|
185 | public void exec(Gesture gesture) {
|
---|
186 | ApolloSystem.useQualityGraphics = !ApolloSystem.useQualityGraphics;
|
---|
187 | }
|
---|
188 | });
|
---|
189 |
|
---|
190 | // Set the MOVE_CURSOR action
|
---|
191 | setGestureAction(StandardGestureActions.getInstance().gestureType(StandardGestureType.MOVE_CURSOR),
|
---|
192 | new GestureAction() {
|
---|
193 | @Override
|
---|
194 | public void exec(Gesture gesture) {
|
---|
195 | mouseMoved(gesture.getData().getPosition());
|
---|
196 | }
|
---|
197 | });
|
---|
198 |
|
---|
199 | // Set the PLACE action
|
---|
200 | setGestureAction(StandardGestureActions.getInstance().gestureType(StandardGestureType.PLACE),
|
---|
201 | new GestureAction() {
|
---|
202 |
|
---|
203 | final List<SampledTrack> sampledTracks = new LinkedList<SampledTrack>();
|
---|
204 |
|
---|
205 |
|
---|
206 | @Override
|
---|
207 | public void prepare(final Gesture gesture) {
|
---|
208 | final FreeItems freeItems = FreeItems.getInstance();
|
---|
209 | List<Widget> widgets = null;
|
---|
210 | if (freeItems != null && !freeItems.isEmpty()) {
|
---|
211 | widgets = ItemUtils.extractWidgets(freeItems);
|
---|
212 | }
|
---|
213 | widgets.forEach(w -> {
|
---|
214 | if (w instanceof SampledTrack) {
|
---|
215 | final SampledTrack st = (SampledTrack) w;
|
---|
216 | st.setIgnoreInjection(true);
|
---|
217 | sampledTracks.add(st);
|
---|
218 | }
|
---|
219 | });
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Override
|
---|
223 | public void exec(final Gesture gesture) {
|
---|
224 | final PickUpGestureData data = (PickUpGestureData) gesture.getData();
|
---|
225 | if (data.getCopy()) {
|
---|
226 | StandardGestureActions.getInstance().copyPlace(sampledTracks.get(0).getItems().get(0));
|
---|
227 | }
|
---|
228 | anchorTracks();
|
---|
229 | }
|
---|
230 |
|
---|
231 | @Override
|
---|
232 | public void finalise(final Gesture gesture) {
|
---|
233 | sampledTracks.forEach(st -> st.setIgnoreInjection(false));
|
---|
234 | sampledTracks.clear();
|
---|
235 | }
|
---|
236 | });
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Initialises the set of gesture types. */
|
---|
240 | private void initialiseGestureTypes() {
|
---|
241 | for (ApolloGestureType type : ApolloGestureType.values()) {
|
---|
242 | GestureType gestureType;
|
---|
243 | if ((gestureType = GestureType.register(type.toString())) == null) {
|
---|
244 | gestureType = GestureType.get(type.toString());
|
---|
245 | }
|
---|
246 | _gestureTypes.put(type, gestureType);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | /** Gets the gesture type associated with the given standard type. */
|
---|
251 | public GestureType gestureType(ApolloGestureType type) {
|
---|
252 | if (type == null)
|
---|
253 | return null;
|
---|
254 |
|
---|
255 | return _gestureTypes.get(type);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * TODO: EVERYTHING BELOW HERE IS ONLY TEMPORARILY SITUATED IN THIS CLASS
|
---|
260 | *
|
---|
261 | * The following methods were mostly copy-pasted here from the old
|
---|
262 | * ApolloFrameKeyboardActions and ApolloFrameMouseActions classes (to enable
|
---|
263 | * quick change-over to the new input system). Really they should be relocated
|
---|
264 | * to more suitable homes based on their function.
|
---|
265 | */
|
---|
266 |
|
---|
267 | public static void adjustInitiationTime(Widget track, long adjustment) {
|
---|
268 | if (track instanceof SampledTrack) {
|
---|
269 | adjustInitiationTime((SampledTrack) track, adjustment);
|
---|
270 | } else if (track instanceof LinkedTrack) {
|
---|
271 | adjustInitiationTime((LinkedTrack) track, adjustment);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | public static void adjustInitiationTime(SampledTrack track, long adjustment) {
|
---|
276 | Mutable.Long oldIt = track.getInitiationTimeFromMeta();
|
---|
277 | if (oldIt == null)
|
---|
278 | oldIt = Mutable.createMutableLong(0);
|
---|
279 | track.setInitiationTime(oldIt.value + adjustment);
|
---|
280 | }
|
---|
281 |
|
---|
282 | public static void adjustInitiationTime(LinkedTrack track, long adjustment) {
|
---|
283 | Mutable.Long oldIt = track.getInitiationTimeFromMeta();
|
---|
284 | if (oldIt == null)
|
---|
285 | oldIt = Mutable.createMutableLong(0);
|
---|
286 | track.setInitiationTime(oldIt.value + adjustment);
|
---|
287 | }
|
---|
288 |
|
---|
289 | public static void adjustVerticalPosition(Widget track, long adjustment) {
|
---|
290 | if (track instanceof SampledTrack) {
|
---|
291 | adjustVerticalPosition((SampledTrack) track, adjustment);
|
---|
292 | } else if (track instanceof LinkedTrack) {
|
---|
293 | adjustVerticalPosition((LinkedTrack) track, adjustment);
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | public static void adjustVerticalPosition(SampledTrack track, long adjustment) {
|
---|
298 | track.setYPosition(track.getY() + (int) adjustment);
|
---|
299 | }
|
---|
300 |
|
---|
301 | public static void adjustVerticalPosition(LinkedTrack track, long adjustment) {
|
---|
302 | track.setYPosition(track.getY() + (int) adjustment);
|
---|
303 | }
|
---|
304 |
|
---|
305 | public static void createLinkedTracks(Item currentItem, Collection<Item> enclosedItems) {
|
---|
306 | if (currentItem == null) {
|
---|
307 |
|
---|
308 | if (enclosedItems != null) {
|
---|
309 |
|
---|
310 | Frame current = DisplayController.getCurrentFrame();
|
---|
311 |
|
---|
312 | Collection<Item> toRemove = new LinkedHashSet<Item>(enclosedItems.size());
|
---|
313 | LinkedList<LinkedTrack> enclosedLinkedTracks = new LinkedList<LinkedTrack>();
|
---|
314 | LinkedList<SampledTrack> enclosedTracks = new LinkedList<SampledTrack>();
|
---|
315 |
|
---|
316 | // Get enclosing shape and enclosed tracks
|
---|
317 | for (Item ip : enclosedItems) {
|
---|
318 | if (ip.getParent() != current)
|
---|
319 | continue;
|
---|
320 |
|
---|
321 | if (ip.hasPermission(UserAppliedPermission.full)) {
|
---|
322 | // Only include lines if one of their enpoints are also being removed
|
---|
323 | if (ip instanceof Line) {
|
---|
324 | Line l = (Line) ip;
|
---|
325 | Item end = l.getEndItem();
|
---|
326 | Item start = l.getStartItem();
|
---|
327 |
|
---|
328 | // If one end of a line is being delted, remove the
|
---|
329 | // other end if all its connecting lines are being
|
---|
330 | // delted
|
---|
331 | if (enclosedItems.contains(end)) {
|
---|
332 | if (!enclosedItems.contains(start) && enclosedItems.containsAll(start.getLines())) {
|
---|
333 | toRemove.add(start);
|
---|
334 | }
|
---|
335 | } else if (enclosedItems.contains(start)) {
|
---|
336 | if (enclosedItems.containsAll(end.getLines())) {
|
---|
337 | toRemove.add(end);
|
---|
338 | }
|
---|
339 | } else {
|
---|
340 | continue;
|
---|
341 | }
|
---|
342 | } else if (ip instanceof WidgetCorner) {
|
---|
343 | Widget iw = ((WidgetCorner) ip).getWidgetSource();
|
---|
344 | if (!enclosedTracks.contains(iw) && iw instanceof SampledTrack) {
|
---|
345 | enclosedTracks.add((SampledTrack) iw);
|
---|
346 | } else if (!enclosedLinkedTracks.contains(iw) && iw instanceof LinkedTrack) {
|
---|
347 | enclosedLinkedTracks.add((LinkedTrack) iw);
|
---|
348 | }
|
---|
349 | } else {
|
---|
350 | if ((ip instanceof Line || ip instanceof Dot)
|
---|
351 | && ip.getHighlightMode() == HighlightMode.Enclosed) {
|
---|
352 | toRemove.add(ip);
|
---|
353 | toRemove.addAll(ip.getConnected());
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | }
|
---|
358 | } // End searching enclosed items
|
---|
359 |
|
---|
360 | // If there was some enclosed tracks
|
---|
361 | if (current != null && !toRemove.isEmpty()
|
---|
362 | && (!enclosedLinkedTracks.isEmpty() || !enclosedTracks.isEmpty())) {
|
---|
363 |
|
---|
364 | // Remove enclosure
|
---|
365 | current.removeAllItems(toRemove);
|
---|
366 |
|
---|
367 | // Determine initiation time of group
|
---|
368 | Mutable.Long initTime = null;
|
---|
369 | Point initiatePosition = null;
|
---|
370 |
|
---|
371 | for (SampledTrack st : enclosedTracks) {
|
---|
372 |
|
---|
373 | if (initTime == null) {
|
---|
374 | initTime = st.getInitiationTimeFromMeta();
|
---|
375 | initiatePosition = st.getPosition();
|
---|
376 | } else if (st.getInitiationTimeFromMeta() != null
|
---|
377 | && st.getInitiationTimeFromMeta().value < initTime.value) {
|
---|
378 | initTime = st.getInitiationTimeFromMeta();
|
---|
379 | initiatePosition = st.getPosition();
|
---|
380 | }
|
---|
381 | }
|
---|
382 | for (LinkedTrack lt : enclosedLinkedTracks) {
|
---|
383 |
|
---|
384 | if (initTime == null) {
|
---|
385 | initTime = lt.getInitiationTimeFromMeta();
|
---|
386 | initiatePosition = lt.getPosition();
|
---|
387 | } else if (lt.getInitiationTimeFromMeta() != null
|
---|
388 | && lt.getInitiationTimeFromMeta().value < initTime.value) {
|
---|
389 | initTime = lt.getInitiationTimeFromMeta();
|
---|
390 | initiatePosition = lt.getPosition();
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | assert (initTime != null);
|
---|
395 | assert (initiatePosition != null);
|
---|
396 |
|
---|
397 | // Creat the link to contain all tracks
|
---|
398 | String name = current.getTitle();
|
---|
399 | if (name == null)
|
---|
400 | name = "Unnamed";
|
---|
401 | name += " group";
|
---|
402 |
|
---|
403 | Text linkSource = new Text(current.getNextItemID());
|
---|
404 | linkSource.addToData(TrackWidgetCommons.META_NAME_TAG + name);
|
---|
405 | linkSource.addToData(TrackWidgetCommons.META_INITIATIONTIME_TAG + initTime);
|
---|
406 | linkSource.setPosition(initiatePosition);
|
---|
407 | linkSource.setParent(current);
|
---|
408 | LinkedTrack linkedVersion = new LinkedTrack(linkSource, null);
|
---|
409 |
|
---|
410 | // Create a new frame to hold the track group
|
---|
411 | Frame newFrame = FrameIO.CreateNewFrame(linkedVersion.getItems().get(0));
|
---|
412 |
|
---|
413 | for (SampledTrack st : enclosedTracks) {
|
---|
414 | st.saveAudio(); // Blocking
|
---|
415 | current.removeAllItems(st.getItems());
|
---|
416 | newFrame.addAllItems(st.getItems());
|
---|
417 | }
|
---|
418 |
|
---|
419 | for (LinkedTrack lt : enclosedLinkedTracks) {
|
---|
420 | current.removeAllItems(lt.getItems());
|
---|
421 | newFrame.addAllItems(lt.getItems());
|
---|
422 | }
|
---|
423 |
|
---|
424 | FrameIO.SaveFrame(newFrame);
|
---|
425 |
|
---|
426 | // Link the linked tracks
|
---|
427 | linkedVersion.setLink(newFrame.getName(), null);
|
---|
428 |
|
---|
429 | // Add the new link
|
---|
430 | current.addAllItems(linkedVersion.getItems());
|
---|
431 |
|
---|
432 | // Ensure initiation time is retained to the exact frame... avoiding loss due to
|
---|
433 | // resolution
|
---|
434 | linkedVersion.setInitiationTime(initTime.value);
|
---|
435 | }
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * The minimum distance from a track widget to auto-align free space items to.
|
---|
442 | */
|
---|
443 | private static final int COARSE_X_PLACEMENT_TOLERANCE = 80;
|
---|
444 |
|
---|
445 | private static final int COARSE_Y_PLACEMENT_TOLERANCE = 20;
|
---|
446 |
|
---|
447 | public void mouseMoved(Point to) {
|
---|
448 | boolean forwareToExpeditee = true;
|
---|
449 |
|
---|
450 | if (isYAxisRestictionOn() && !FreeItems.getInstance().isEmpty()) {
|
---|
451 |
|
---|
452 | // Restrict movement of free items to Y-Axis only
|
---|
453 | forwareToExpeditee = false;
|
---|
454 |
|
---|
455 | int smallestY = FreeItems.getInstance().get(0).getY();
|
---|
456 |
|
---|
457 | for (Item i : FreeItems.getInstance()) {
|
---|
458 | if (i.getY() < smallestY)
|
---|
459 | smallestY = i.getY();
|
---|
460 | }
|
---|
461 |
|
---|
462 | for (Item i : FreeItems.getInstance()) {
|
---|
463 | i.setY(to.getY() + (i.getY() - smallestY));
|
---|
464 | }
|
---|
465 |
|
---|
466 | } else if (isSnapOn() && !FreeItems.getInstance().isEmpty()) {
|
---|
467 |
|
---|
468 | // Couse movement of free items: Restraining left-most pixel in items
|
---|
469 | // to the closest anchored track widgets x position.
|
---|
470 |
|
---|
471 | Frame currentFrame = DisplayController.getCurrentFrame();
|
---|
472 | if (currentFrame != null) {
|
---|
473 |
|
---|
474 | // Search all anchored track x positions for the current frame
|
---|
475 | List<Widget> widgets = currentFrame.getInteractiveWidgets();
|
---|
476 |
|
---|
477 | int closestXPosition = -1;
|
---|
478 | int closestYPosition = -1;
|
---|
479 | int closestXDistance = -1;
|
---|
480 | int closestYDistance = -1;
|
---|
481 |
|
---|
482 | for (Widget iw : widgets) {
|
---|
483 |
|
---|
484 | if (iw instanceof SampledTrack || iw instanceof LinkedTrack) {
|
---|
485 |
|
---|
486 | // Determine TOP-LEFT Snapping
|
---|
487 | int xDistance = Math.abs(to.getX() - iw.getX());
|
---|
488 | if (closestXDistance < 0 || xDistance < closestXDistance) {
|
---|
489 | closestXDistance = xDistance;
|
---|
490 | closestXPosition = iw.getX();
|
---|
491 | }
|
---|
492 |
|
---|
493 | int yDistance = Math.abs(to.getY() - iw.getY());
|
---|
494 | if (closestYDistance < 0 || yDistance < closestYDistance) {
|
---|
495 | closestYDistance = yDistance;
|
---|
496 | closestYPosition = iw.getY();
|
---|
497 | }
|
---|
498 |
|
---|
499 | // Determine BOTTOM-RIGHT Snapping
|
---|
500 | xDistance = Math.abs(to.getX() - (iw.getX() + iw.getWidth()));
|
---|
501 | if (closestXDistance < 0 || xDistance < closestXDistance) {
|
---|
502 | closestXDistance = xDistance;
|
---|
503 | closestXPosition = iw.getX() + iw.getWidth();
|
---|
504 | }
|
---|
505 |
|
---|
506 | yDistance = Math.abs(to.getY() - (iw.getY() + iw.getHeight()));
|
---|
507 | if (closestYDistance < 0 || yDistance < closestYDistance) {
|
---|
508 | closestYDistance = yDistance;
|
---|
509 | closestYPosition = iw.getY() + iw.getHeight();
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | }
|
---|
514 |
|
---|
515 | // Determine top-left position of free items
|
---|
516 | int smallestX = FreeItems.getInstance().get(0).getX();
|
---|
517 | int smallestY = FreeItems.getInstance().get(0).getY();
|
---|
518 | for (Item i : FreeItems.getInstance()) {
|
---|
519 | if (i.getX() < smallestX)
|
---|
520 | smallestX = i.getX();
|
---|
521 | if (i.getY() < smallestY)
|
---|
522 | smallestY = i.getY();
|
---|
523 | }
|
---|
524 |
|
---|
525 | for (Item i : FreeItems.getInstance()) {
|
---|
526 |
|
---|
527 | int x;
|
---|
528 | int y;
|
---|
529 |
|
---|
530 | if (closestXDistance > 0 && closestXDistance < COARSE_X_PLACEMENT_TOLERANCE)
|
---|
531 | x = closestXPosition + (i.getX() - smallestX);
|
---|
532 | else
|
---|
533 | x = to.getX() + (i.getX() - smallestX);
|
---|
534 |
|
---|
535 | if (closestYDistance > 0 && closestYDistance < COARSE_Y_PLACEMENT_TOLERANCE)
|
---|
536 | y = closestYPosition + (i.getY() - smallestY);
|
---|
537 | else
|
---|
538 | y = to.getY() + (i.getY() - smallestY);
|
---|
539 |
|
---|
540 | i.setPosition(x, y);
|
---|
541 |
|
---|
542 | }
|
---|
543 |
|
---|
544 | forwareToExpeditee = false;
|
---|
545 |
|
---|
546 | }
|
---|
547 |
|
---|
548 | }
|
---|
549 |
|
---|
550 | // Expeditees frame mouse actions uses an offset and fights over free-item
|
---|
551 | // movement if it listens to the mouse event router... therefore add an extra
|
---|
552 | // layer to avoid this... otherwise auto-aligned items jitter like crazy while
|
---|
553 | // moving the cursus
|
---|
554 | // if (forwareToExpeditee) {
|
---|
555 | // FrameMouseActions.getInstance().mouseMoved(e);
|
---|
556 | // } else {
|
---|
557 | // FrameGraphics.refresh(true);
|
---|
558 | // }
|
---|
559 | }
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * @return True if the user is restricting movement on the y-axis only
|
---|
563 | */
|
---|
564 | public static boolean isYAxisRestictionOn() {
|
---|
565 | return StandardInputEventListeners.kbmStateListener.isKeyDown(Key.CTRL)
|
---|
566 | && !StandardInputEventListeners.kbmStateListener.isKeyDown(Key.SHIFT);
|
---|
567 | }
|
---|
568 |
|
---|
569 | public static boolean isSnapOn() {
|
---|
570 | return StandardInputEventListeners.kbmStateListener.isKeyDown(Key.SHIFT)
|
---|
571 | && !StandardInputEventListeners.kbmStateListener.isKeyDown(Key.CTRL);
|
---|
572 | }
|
---|
573 |
|
---|
574 | public void anchorTracks() {
|
---|
575 |
|
---|
576 | // Widget -> delta IT time
|
---|
577 | Map<LinkedTrack, Mutable.Long> anchoredLinkedTracks = null;
|
---|
578 | Map<SampledTrack, Mutable.Long> anchoredTracks = null;
|
---|
579 |
|
---|
580 | if (FreeItems.getInstance().size() > 1) {
|
---|
581 | List<Widget> anchoringWidgets = ItemUtils.extractWidgets(FreeItems.getInstance());
|
---|
582 |
|
---|
583 | Mutable.Long firstInitTime = null;
|
---|
584 | anchoredLinkedTracks = new HashMap<LinkedTrack, Mutable.Long>();
|
---|
585 | anchoredTracks = new HashMap<SampledTrack, Mutable.Long>();
|
---|
586 |
|
---|
587 | for (Widget iw : anchoringWidgets) {
|
---|
588 |
|
---|
589 | Mutable.Long it = null;
|
---|
590 |
|
---|
591 | if (iw instanceof LinkedTrack) {
|
---|
592 |
|
---|
593 | LinkedTrack lt = (LinkedTrack) iw;
|
---|
594 | it = lt.getInitiationTimeFromMeta();
|
---|
595 | if (it == null)
|
---|
596 | it = Mutable.createMutableLong(0);
|
---|
597 |
|
---|
598 | anchoredLinkedTracks.put(lt, it);
|
---|
599 |
|
---|
600 | } else if (iw instanceof SampledTrack) {
|
---|
601 |
|
---|
602 | SampledTrack st = (SampledTrack) iw;
|
---|
603 | it = st.getInitiationTimeFromMeta();
|
---|
604 | if (it == null)
|
---|
605 | it = Mutable.createMutableLong(0);
|
---|
606 |
|
---|
607 | anchoredTracks.put(st, it);
|
---|
608 |
|
---|
609 | } else
|
---|
610 | continue;
|
---|
611 |
|
---|
612 | if (firstInitTime == null) {
|
---|
613 | firstInitTime = Mutable.createMutableLong(it.value); // Important to create new instance
|
---|
614 | } else if (it.value < firstInitTime.value) {
|
---|
615 | firstInitTime = Mutable.createMutableLong(it.value); // Important to create new instance
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | // Should do a accurate anchor with init times properly aligned?
|
---|
620 | if ((anchoredLinkedTracks.size() + anchoredTracks.size()) > 1) {
|
---|
621 |
|
---|
622 | assert (firstInitTime != null);
|
---|
623 |
|
---|
624 | // Then calc all the deltas
|
---|
625 | for (LinkedTrack lt : anchoredLinkedTracks.keySet()) {
|
---|
626 | Mutable.Long it = anchoredLinkedTracks.get(lt);
|
---|
627 | it.value -= firstInitTime.value;
|
---|
628 | }
|
---|
629 |
|
---|
630 | for (SampledTrack st : anchoredTracks.keySet()) {
|
---|
631 | Mutable.Long it = anchoredTracks.get(st);
|
---|
632 | it.value -= firstInitTime.value;
|
---|
633 | }
|
---|
634 |
|
---|
635 | } else {
|
---|
636 | anchoredLinkedTracks = null;
|
---|
637 | anchoredTracks = null;
|
---|
638 | }
|
---|
639 |
|
---|
640 | }
|
---|
641 |
|
---|
642 | // If anchored a group of tracks .. adjust initiation times to
|
---|
643 | if (anchoredLinkedTracks != null) {
|
---|
644 |
|
---|
645 | Mutable.Long firstInitTime = null;
|
---|
646 |
|
---|
647 | for (LinkedTrack lt : anchoredLinkedTracks.keySet()) {
|
---|
648 | Mutable.Long it = anchoredLinkedTracks.get(lt);
|
---|
649 | if (it.value == 0) {
|
---|
650 | firstInitTime = lt.getInitiationTimeFromMeta();
|
---|
651 | break;
|
---|
652 | }
|
---|
653 | }
|
---|
654 | if (firstInitTime == null) {
|
---|
655 | for (SampledTrack st : anchoredTracks.keySet()) {
|
---|
656 | Mutable.Long it = anchoredTracks.get(st);
|
---|
657 | if (it.value == 0) {
|
---|
658 | firstInitTime = st.getInitiationTimeFromMeta();
|
---|
659 | break;
|
---|
660 | }
|
---|
661 | }
|
---|
662 | }
|
---|
663 | assert (firstInitTime != null);
|
---|
664 |
|
---|
665 | for (LinkedTrack lt : anchoredLinkedTracks.keySet()) {
|
---|
666 | Mutable.Long it = anchoredLinkedTracks.get(lt);
|
---|
667 | if (it.value == 0)
|
---|
668 | continue;
|
---|
669 | lt.setInitiationTime(firstInitTime.value + it.value);
|
---|
670 | }
|
---|
671 |
|
---|
672 | for (SampledTrack st : anchoredTracks.keySet()) {
|
---|
673 | Mutable.Long it = anchoredTracks.get(st);
|
---|
674 | if (it.value == 0)
|
---|
675 | continue;
|
---|
676 | st.setInitiationTime(firstInitTime.value + it.value);
|
---|
677 | }
|
---|
678 |
|
---|
679 | }
|
---|
680 | }
|
---|
681 | }
|
---|