Skip to content

Commit d23a93f

Browse files
(v2) Scene editor: tile sprite size tooling.
1 parent 0151764 commit d23a93f

File tree

3 files changed

+260
-8
lines changed

3 files changed

+260
-8
lines changed

source/v2/phasereditor/phasereditor.scene.ui/src/phasereditor/scene/ui/editor/interactive/TileScaleElement.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,20 +245,20 @@ public void mouseUp(MouseEvent e) {
245245

246246
getModels().forEach(model -> {
247247

248-
model.put("final-tileScaleX", TileSpriteComponent.get_tilePositionX(model));
249-
model.put("final-tileScaleY", TileSpriteComponent.get_tilePositionY(model));
248+
model.put("final-tileScaleX", TileSpriteComponent.get_tileScaleX(model));
249+
model.put("final-tileScaleY", TileSpriteComponent.get_tileScaleY(model));
250250

251-
TileSpriteComponent.set_tilePositionX(model, (float) model.get("initial-tileScaleX"));
252-
TileSpriteComponent.set_tilePositionY(model, (float) model.get("initial-tileScaleY"));
251+
TileSpriteComponent.set_tileScaleX(model, (float) model.get("initial-tileScaleX"));
252+
TileSpriteComponent.set_tileScaleY(model, (float) model.get("initial-tileScaleY"));
253253

254254
});
255255

256256
var before = SingleObjectSnapshotOperation.takeSnapshot(getModels());
257257

258258
getModels().forEach(model -> {
259259

260-
TileSpriteComponent.set_tilePositionX(model, (float) model.get("final-tileScaleX"));
261-
TileSpriteComponent.set_tilePositionY(model, (float) model.get("final-tileScaleY"));
260+
TileSpriteComponent.set_tileScaleX(model, (float) model.get("final-tileScaleX"));
261+
TileSpriteComponent.set_tileScaleY(model, (float) model.get("final-tileScaleY"));
262262

263263
});
264264

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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.SWT;
27+
import org.eclipse.swt.events.MouseEvent;
28+
import org.eclipse.swt.graphics.Color;
29+
import org.eclipse.swt.graphics.GC;
30+
import org.eclipse.swt.graphics.Transform;
31+
import org.eclipse.wb.swt.SWTResourceManager;
32+
33+
import phasereditor.scene.core.ObjectModel;
34+
import phasereditor.scene.core.TileSpriteComponent;
35+
import phasereditor.scene.ui.editor.SceneEditor;
36+
import phasereditor.scene.ui.editor.undo.SingleObjectSnapshotOperation;
37+
import phasereditor.ui.PhaserEditorUI;
38+
39+
/**
40+
* @author arian
41+
*
42+
*/
43+
@SuppressWarnings("boxing")
44+
public class TileSizeElement extends RenderInteractiveElement {
45+
46+
private static final int BOX = 14;
47+
private int _globalX;
48+
private int _globalY;
49+
private boolean _dragging;
50+
private int _initialGlobalY;
51+
private int _initialGlobalX;
52+
private boolean _changeX;
53+
private boolean _changeY;
54+
private boolean _hightlights;
55+
56+
public TileSizeElement(SceneEditor editor, List<ObjectModel> models, boolean changeX, boolean changeY) {
57+
super(editor, models);
58+
59+
_changeX = changeX;
60+
_changeY = changeY;
61+
}
62+
63+
@Override
64+
public void render(GC gc) {
65+
var renderer = getRenderer();
66+
67+
var globalX = 0;
68+
var globalY = 0;
69+
var globalAngle = 0f;
70+
71+
for (var model : getModels()) {
72+
var modelX = 0;
73+
var modelY = 0;
74+
75+
var width = TileSpriteComponent.get_width(model);
76+
var height = TileSpriteComponent.get_height(model);
77+
78+
globalAngle += renderer.globalAngle(model);
79+
80+
float[] xy;
81+
82+
if (_changeX && _changeY) {
83+
xy = renderer.localToScene(model, modelX + width, modelY + height);
84+
} else if (_changeX) {
85+
xy = renderer.localToScene(model, modelX + width, modelY + height / 2);
86+
} else {
87+
xy = renderer.localToScene(model, modelX + width / 2, modelY + height);
88+
}
89+
90+
globalX += (int) xy[0];
91+
globalY += (int) xy[1];
92+
93+
}
94+
95+
var size = getModels().size();
96+
97+
globalX = globalX / size;
98+
globalY = globalY / size;
99+
100+
globalAngle = globalAngle / size;
101+
102+
gc.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
103+
gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
104+
105+
var color = SWTResourceManager.getColor(_hightlights ? SWT.COLOR_WHITE
106+
: (_changeX && _changeY ? SWT.COLOR_YELLOW : (_changeX ? SWT.COLOR_RED : SWT.COLOR_GREEN)));
107+
108+
gc.setBackground(color);
109+
gc.setForeground(color);
110+
111+
fillRect(gc, globalX, globalY, globalAngle + (_changeY ? 90 : 0), BOX, color);
112+
113+
_globalX = globalX;
114+
_globalY = globalY;
115+
116+
}
117+
118+
private static void fillRect(GC gc, int globalX, int globalY, float globalAngle, int size, Color color) {
119+
var tx = new Transform(gc.getDevice());
120+
121+
tx.translate(globalX, globalY);
122+
tx.rotate(globalAngle);
123+
gc.setTransform(tx);
124+
125+
gc.setBackground(color);
126+
gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
127+
128+
gc.fillRectangle(-size / 2, -size / 2, size, size);
129+
gc.drawRectangle(-size / 2, -size / 2, size, size);
130+
131+
gc.setTransform(null);
132+
133+
tx.dispose();
134+
}
135+
136+
@Override
137+
public boolean contains(int sceneX, int sceneY) {
138+
139+
if (_dragging) {
140+
return true;
141+
}
142+
143+
var contains = PhaserEditorUI.distance(sceneX, sceneY, _globalX, _globalY) <= BOX;
144+
145+
_hightlights = contains;
146+
147+
return contains;
148+
}
149+
150+
@Override
151+
public void mouseMove(MouseEvent e) {
152+
if (_dragging && contains(e.x, e.y)) {
153+
154+
for (var model : getModels()) {
155+
156+
var initialLocalXY = (float[]) model.get("initial-local-xy");
157+
var localXY = getRenderer().sceneToLocal(model, e.x, e.y);
158+
159+
var dx = localXY[0] - initialLocalXY[0];
160+
var dy = localXY[1] - initialLocalXY[1];
161+
162+
var initialWidth = (float) model.get("initial-width");
163+
var initialHeight = (float) model.get("initial-height");
164+
165+
var width = initialWidth + dx;
166+
var height = initialHeight + dy;
167+
168+
if (width <= 0 || height <= 0) {
169+
continue;
170+
}
171+
172+
if (_changeX) {
173+
TileSpriteComponent.set_width(model, width);
174+
}
175+
176+
if (_changeY) {
177+
TileSpriteComponent.set_height(model, height);
178+
}
179+
180+
model.setDirty(true);
181+
182+
getEditor().updatePropertyPagesContentWithSelection();
183+
}
184+
}
185+
}
186+
187+
@Override
188+
public void mouseDown(MouseEvent e) {
189+
if (contains(e.x, e.y)) {
190+
191+
_dragging = true;
192+
193+
_initialGlobalX = _globalX;
194+
_initialGlobalY = _globalY;
195+
196+
for (var model : getModels()) {
197+
model.put("initial-width", TileSpriteComponent.get_width(model));
198+
model.put("initial-height", TileSpriteComponent.get_height(model));
199+
200+
var xy = getRenderer().sceneToLocal(model, _initialGlobalX, _initialGlobalY);
201+
model.put("initial-local-xy", xy);
202+
203+
}
204+
}
205+
}
206+
207+
@Override
208+
public void mouseUp(MouseEvent e) {
209+
if (_dragging) {
210+
211+
var editor = getEditor();
212+
213+
getModels().forEach(model -> {
214+
215+
model.put("final-width", TileSpriteComponent.get_width(model));
216+
model.put("final-height", TileSpriteComponent.get_height(model));
217+
218+
TileSpriteComponent.set_width(model, (float) model.get("initial-width"));
219+
TileSpriteComponent.set_height(model, (float) model.get("initial-height"));
220+
221+
});
222+
223+
var before = SingleObjectSnapshotOperation.takeSnapshot(getModels());
224+
225+
getModels().forEach(model -> {
226+
227+
TileSpriteComponent.set_width(model, (float) model.get("final-width"));
228+
TileSpriteComponent.set_height(model, (float) model.get("final-height"));
229+
230+
});
231+
232+
var after = SingleObjectSnapshotOperation.takeSnapshot(getModels());
233+
234+
editor.executeOperation(new SingleObjectSnapshotOperation(before, after, "Set tile position.", true));
235+
236+
editor.setDirty(true);
237+
238+
}
239+
_dragging = false;
240+
}
241+
242+
}

source/v2/phasereditor/phasereditor.scene.ui/src/phasereditor/scene/ui/editor/properties/TileSpriteSection.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import phasereditor.scene.core.TileSpriteModel;
3737
import phasereditor.scene.ui.editor.interactive.TilePositionElement;
3838
import phasereditor.scene.ui.editor.interactive.TileScaleElement;
39+
import phasereditor.scene.ui.editor.interactive.TileSizeElement;
3940
import phasereditor.scene.ui.editor.undo.SingleObjectSnapshotOperation;
4041
import phasereditor.ui.EditorSharedImages;
4142
import phasereditor.ui.properties.FormPropertyPage;
@@ -154,7 +155,16 @@ public void run() {
154155
{
155156
var manager = new ToolBarManager();
156157
manager.add(new Action("Tile Size", EditorSharedImages.getImageDescriptor(IMG_EDIT_OBJ_PROPERTY)) {
157-
//
158+
@Override
159+
public void run() {
160+
getEditor().getScene().setInteractiveElements(
161+
162+
new TileSizeElement(getEditor(), getModels(), true, false),
163+
new TileSizeElement(getEditor(), getModels(), false, true),
164+
new TileSizeElement(getEditor(), getModels(), true, true)
165+
166+
);
167+
}
158168
});
159169
manager.createControl(comp);
160170
}
@@ -240,7 +250,7 @@ protected void resetSizeToTexture(boolean width, boolean height) {
240250
if (height) {
241251
TileSpriteComponent.set_height(model, size.y);
242252
}
243-
253+
244254
model.setDirty(true);
245255
}
246256
}

0 commit comments

Comments
 (0)