Skip to content

Commit 31190dc

Browse files
(v2) Scene editor: drafting with TileSprite position tooling.
1 parent 73d162e commit 31190dc

File tree

6 files changed

+472
-10
lines changed

6 files changed

+472
-10
lines changed

source/v2/phasereditor/phasereditor.scene.ui/src/phasereditor/scene/ui/editor/SceneCanvas.java

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static java.util.stream.Collectors.toList;
2525

2626
import java.util.ArrayList;
27+
import java.util.Arrays;
2728
import java.util.List;
2829
import java.util.UUID;
2930

@@ -66,6 +67,7 @@
6667
import phasereditor.scene.core.TextualComponent;
6768
import phasereditor.scene.core.TextureComponent;
6869
import phasereditor.scene.core.TransformComponent;
70+
import phasereditor.scene.ui.editor.interactive.RenderInteractiveElement;
6971
import phasereditor.scene.ui.editor.undo.WorldSnapshotOperation;
7072
import phasereditor.ui.ColorUtil;
7173
import phasereditor.ui.ZoomCanvas;
@@ -87,6 +89,7 @@ public class SceneCanvas extends ZoomCanvas implements MouseListener, MouseMoveL
8789
private SceneModel _sceneModel;
8890
private DragObjectsEvents _dragObjectsEvents;
8991
private SelectionEvents _selectionEvents;
92+
private List<RenderInteractiveElement> _interactiveElements;
9093

9194
public SceneCanvas(Composite parent, int style) {
9295
super(parent, style);
@@ -105,6 +108,24 @@ public SceneCanvas(Composite parent, int style) {
105108
init_DND();
106109

107110
setZoomWhenShiftPressed(false);
111+
112+
_interactiveElements = new ArrayList<>();
113+
}
114+
115+
public List<RenderInteractiveElement> getInteractiveElements() {
116+
return _interactiveElements;
117+
}
118+
119+
public void clearInteractiveElements() {
120+
_interactiveElements.clear();
121+
}
122+
123+
public void setInteractiveElements(RenderInteractiveElement... elems) {
124+
clearInteractiveElements();
125+
126+
_interactiveElements.addAll(Arrays.asList(elems));
127+
128+
redraw();
108129
}
109130

110131
private void init_DND() {
@@ -259,9 +280,19 @@ protected void customPaintControl(PaintEvent e) {
259280

260281
renderSelection(e.gc);
261282

283+
renderInteractiveElements(e.gc);
284+
262285
renderLabels(e, calc);
263286
}
264287

288+
private void renderInteractiveElements(GC gc) {
289+
290+
for (var elem : _interactiveElements) {
291+
elem.render(gc);
292+
}
293+
294+
}
295+
265296
private void renderBorders(GC gc, ZoomCalculator calc) {
266297
var view = calc.modelToView(_sceneModel.getBorderX(), _sceneModel.getBorderY(), _sceneModel.getBorderWidth(),
267298
_sceneModel.getBorderHeight());
@@ -321,16 +352,16 @@ private void renderSelection(GC gc) {
321352
gc.setForeground(selectionColor);
322353
gc.drawPolygon(new int[] { (int) bounds[0], (int) bounds[1], (int) bounds[2], (int) bounds[3],
323354
(int) bounds[4], (int) bounds[5], (int) bounds[6], (int) bounds[7] });
324-
355+
325356
var name = EditorComponent.get_editorName(model);
326-
357+
327358
var x = bounds[0];
328359
var y = bounds[1];
329-
360+
330361
gc.setAlpha(150);
331362
gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
332363
gc.drawText(name, (int) x - 1, (int) y - 21, true);
333-
364+
334365
gc.setAlpha(255);
335366
gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
336367
gc.drawText(name, (int) x, (int) y - 20, true);
@@ -1029,11 +1060,28 @@ public void mouseDoubleClick(MouseEvent e) {
10291060

10301061
@Override
10311062
public void mouseDown(MouseEvent e) {
1032-
//
1063+
for (var elem : _interactiveElements) {
1064+
elem.mouseDown(e);
1065+
}
10331066
}
10341067

10351068
@Override
10361069
public void mouseUp(MouseEvent e) {
1070+
boolean contains = false;
1071+
1072+
for (var elem : _interactiveElements) {
1073+
if (elem.contains(e.x, e.y)) {
1074+
contains = true;
1075+
break;
1076+
}
1077+
}
1078+
1079+
if (contains) {
1080+
for (var elem : _interactiveElements) {
1081+
elem.mouseUp(e);
1082+
}
1083+
}
1084+
10371085
if (_dragDetected) {
10381086

10391087
_dragDetected = false;
@@ -1045,13 +1093,42 @@ public void mouseUp(MouseEvent e) {
10451093
return;
10461094
}
10471095

1048-
_selectionEvents.updateSelection(e);
1096+
if (!contains) {
1097+
var prevSelection = _selection;
1098+
1099+
_selectionEvents.updateSelection(e);
1100+
1101+
if (prevSelection.size() != _selection.size()) {
1102+
clearInteractiveElements();
1103+
}
1104+
}
1105+
10491106
}
10501107

10511108
@Override
10521109
public void mouseMove(MouseEvent e) {
1053-
if (_dragObjectsEvents.isDragging()) {
1054-
_dragObjectsEvents.update(e);
1110+
boolean contains = false;
1111+
1112+
for (var elem : _interactiveElements) {
1113+
if (elem.contains(e.x, e.y)) {
1114+
contains = true;
1115+
break;
1116+
}
1117+
}
1118+
1119+
if (contains) {
1120+
1121+
for (var elem : _interactiveElements) {
1122+
elem.mouseMove(e);
1123+
}
1124+
1125+
redraw();
1126+
} else {
1127+
1128+
if (_dragObjectsEvents.isDragging()) {
1129+
_dragObjectsEvents.update(e);
1130+
}
1131+
10551132
}
10561133
}
10571134

source/v2/phasereditor/phasereditor.scene.ui/src/phasereditor/scene/ui/editor/SceneObjectRenderer.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,43 @@ public float[] localToScene(ObjectModel model, float localX, float localY) {
740740

741741
return point;
742742
}
743+
744+
public float globalScaleX(ObjectModel model) {
745+
var scale = TransformComponent.get_scaleX(model);
746+
747+
var parent = ParentComponent.get_parent(model);
748+
749+
if (parent == null) {
750+
return _canvas.getScale() * scale;
751+
}
752+
753+
return scale * globalScaleX(parent);
754+
}
755+
756+
public float globalScaleY(ObjectModel model) {
757+
var scale = TransformComponent.get_scaleY(model);
758+
759+
var parent = ParentComponent.get_parent(model);
760+
761+
if (parent == null) {
762+
return _canvas.getScale() * scale;
763+
}
764+
765+
return scale * globalScaleY(parent);
766+
}
767+
768+
public float globalAngle(ObjectModel model) {
769+
var angle = TransformComponent.get_angle(model);
770+
771+
var parent = ParentComponent.get_parent(model);
772+
773+
if (parent == null) {
774+
return angle;
775+
}
776+
777+
return angle + globalAngle(parent);
778+
}
779+
743780

744781
public float[] sceneToLocal(ObjectModel model, float sceneX, float sceneY) {
745782
var matrix = _modelMatrixMap.get(model);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2015, 2018 Arian Fornaris
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a
6+
// copy of this software and associated documentation files (the
7+
// "Software"), to deal in the Software without restriction, including
8+
// without limitation the rights to use, copy, modify, merge, publish,
9+
// distribute, sublicense, and/or sell copies of the Software, and to permit
10+
// persons to whom the Software is furnished to do so, subject to the
11+
// following conditions: The above copyright notice and this permission
12+
// notice shall be included in all copies or substantial portions of the
13+
// Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
package phasereditor.scene.ui.editor.interactive;
23+
24+
import java.util.List;
25+
26+
import org.eclipse.swt.events.MouseEvent;
27+
import org.eclipse.swt.graphics.GC;
28+
29+
import phasereditor.scene.core.ObjectModel;
30+
import phasereditor.scene.ui.editor.SceneCanvas;
31+
import phasereditor.scene.ui.editor.SceneEditor;
32+
import phasereditor.scene.ui.editor.SceneObjectRenderer;
33+
34+
/**
35+
* @author arian
36+
*
37+
*/
38+
public abstract class RenderInteractiveElement {
39+
40+
private List<ObjectModel> _models;
41+
private SceneEditor _editor;
42+
43+
public RenderInteractiveElement(SceneEditor editor, List<ObjectModel> models) {
44+
super();
45+
_editor = editor;
46+
_models = models;
47+
}
48+
49+
public SceneEditor getEditor() {
50+
return _editor;
51+
}
52+
53+
public SceneCanvas getScene() {
54+
return _editor.getScene();
55+
}
56+
57+
public SceneObjectRenderer getRenderer() {
58+
return _editor.getScene().getSceneRenderer();
59+
}
60+
61+
public List<ObjectModel> getModels() {
62+
return _models;
63+
}
64+
65+
public abstract void render(GC gc);
66+
67+
@SuppressWarnings("unused")
68+
public void mouseUp(MouseEvent e) {
69+
//
70+
}
71+
72+
@SuppressWarnings("unused")
73+
public void mouseMove(MouseEvent e) {
74+
//
75+
76+
}
77+
78+
@SuppressWarnings("unused")
79+
public void mouseDown(MouseEvent e) {
80+
//
81+
}
82+
83+
@SuppressWarnings({ "static-method", "unused" })
84+
public boolean contains(int sceneX, int sceneY) {
85+
return false;
86+
}
87+
88+
}

0 commit comments

Comments
 (0)