|
| 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 | +} |
0 commit comments