gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
src/gdxstudio/SceneEditor.java
| a9b2a64 | 1 | package gdxstudio; |
| a9b2a64 | 2 | |
| a9b2a64 | 3 | import java.awt.Cursor; |
| a9b2a64 | 4 | |
| a9b2a64 | 5 | import scene2d.Asset; |
| a9b2a64 | 6 | import scene2d.Scene; |
| a9b2a64 | 7 | |
| a9b2a64 | 8 | import com.badlogic.gdx.scenes.scene2d.Actor; |
| a9b2a64 | 9 | import com.badlogic.gdx.scenes.scene2d.InputEvent; |
| a9b2a64 | 10 | import com.badlogic.gdx.scenes.scene2d.ui.List; |
| a9b2a64 | 11 | import com.badlogic.gdx.scenes.scene2d.ui.SelectBox; |
| a9b2a64 | 12 | import com.badlogic.gdx.scenes.scene2d.ui.Table; |
| a9b2a64 | 13 | import com.badlogic.gdx.scenes.scene2d.ui.TextButton; |
| a9b2a64 | 14 | import com.badlogic.gdx.scenes.scene2d.ui.TextField; |
| a9b2a64 | 15 | import com.badlogic.gdx.scenes.scene2d.utils.Align; |
| a9b2a64 | 16 | import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; |
| a9b2a64 | 17 | import com.badlogic.gdx.utils.Array; |
| a9b2a64 | 18 | import com.badlogic.gdx.utils.Timer.Task; |
| a9b2a64 | 19 | |
| a9b2a64 | 20 | public class SceneEditor extends Scene {
|
| a9b2a64 | 21 | public static Actor selectedActor = null; |
| a9b2a64 | 22 | public static boolean reloadAssets = false; |
| a9b2a64 | 23 | static AddField addField; |
| a9b2a64 | 24 | |
| a9b2a64 | 25 | public SceneEditor(){
|
| a9b2a64 | 26 | super(); |
| a9b2a64 | 27 | if(reloadAssets){
|
| a9b2a64 | 28 | Asset.loadBlocking(); // this is the first time this Scene is created by the Stage |
| a9b2a64 | 29 | Content.assetPanel.updateAsset(); |
| a9b2a64 | 30 | com.badlogic.gdx.utils.Timer.schedule(new Task(){
|
| a9b2a64 | 31 | @Override |
| a9b2a64 | 32 | public void run() {
|
| a9b2a64 | 33 | Frame.scenePanel.showStudio(); |
| a9b2a64 | 34 | Scene.scenesMap.removeKey("gdxstudio.SceneEditor");
|
| a9b2a64 | 35 | Frame.scenePanel.update(); |
| a9b2a64 | 36 | } |
| a9b2a64 | 37 | }, 1f); |
| a9b2a64 | 38 | reloadAssets = false; |
| a9b2a64 | 39 | addField = new AddField(); |
| a9b2a64 | 40 | } |
| a9b2a64 | 41 | else{
|
| a9b2a64 | 42 | Frame.scenePanel.showStudio(); |
| a9b2a64 | 43 | Scene.scenesMap.removeKey("gdxstudio.SceneEditor");
|
| a9b2a64 | 44 | } |
| a9b2a64 | 45 | Content.studioPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| a9b2a64 | 46 | } |
| a9b2a64 | 47 | |
| a9b2a64 | 48 | public static void doClick(Actor actor){
|
| a9b2a64 | 49 | addField.remove(); |
| a9b2a64 | 50 | Frame.actorPanel.lock(); |
| a9b2a64 | 51 | Frame.propertyPanel.clear(); |
| a9b2a64 | 52 | Frame.effectPanel.clear(); |
| a9b2a64 | 53 | Frame.eventPanel.clear(); |
| a9b2a64 | 54 | selectedActor = actor; |
| a9b2a64 | 55 | Scene.getCurrentScene().outline(actor); |
| a9b2a64 | 56 | Frame.actorPanel.list.setSelectedIndex(Frame.actorPanel.indexOf(actor.getName())); |
| a9b2a64 | 57 | StatusBar.updateSelected(actor.getName()); |
| a9b2a64 | 58 | Frame.dashPanel.update(); |
| a9b2a64 | 59 | Frame.propertyPanel.update(); |
| a9b2a64 | 60 | Frame.effectPanel.update(); |
| a9b2a64 | 61 | Frame.eventPanel.update(); |
| a9b2a64 | 62 | Frame.actorPanel.unlock(); |
| a9b2a64 | 63 | if(selectedActor instanceof List || selectedActor instanceof SelectBox){
|
| a9b2a64 | 64 | addField.setPosition(actor.getX(), actor.getY() - addField.getHeight()); |
| a9b2a64 | 65 | Scene.getRoot().addActor(addField); |
| a9b2a64 | 66 | } |
| a9b2a64 | 67 | } |
| a9b2a64 | 68 | |
| a9b2a64 | 69 | |
| a9b2a64 | 70 | @Override |
| a9b2a64 | 71 | public void onClick(Actor actor) {
|
| a9b2a64 | 72 | doClick(actor); |
| a9b2a64 | 73 | } |
| a9b2a64 | 74 | |
| a9b2a64 | 75 | boolean dragging; |
| a9b2a64 | 76 | int edge; |
| a9b2a64 | 77 | float startX, startY, lastX, lastY; |
| a9b2a64 | 78 | @Override |
| a9b2a64 | 79 | public void onTouchDown(Actor actor) {
|
| a9b2a64 | 80 | if (Scene.mouseButton == 0) {
|
| a9b2a64 | 81 | edge = 0; |
| a9b2a64 | 82 | float x = Scene.mouse.x; |
| a9b2a64 | 83 | float y = Scene.mouse.y; |
| a9b2a64 | 84 | if (x > actor.getX() + actor.getWidth() - 10) edge |= Align.right; |
| a9b2a64 | 85 | if (y > actor.getY() + actor.getHeight() - 10) edge |= Align.top; |
| a9b2a64 | 86 | //if (x < actor.getX() + 20 && y < actor.getY() + 20) edge = Align.left; no rotation |
| a9b2a64 | 87 | dragging = edge != 0; |
| a9b2a64 | 88 | startX = x; |
| a9b2a64 | 89 | startY = y; |
| a9b2a64 | 90 | lastX = x; |
| a9b2a64 | 91 | lastY = y; |
| a9b2a64 | 92 | } |
| a9b2a64 | 93 | } |
| a9b2a64 | 94 | |
| a9b2a64 | 95 | @Override |
| a9b2a64 | 96 | public void onTouchUp() {
|
| a9b2a64 | 97 | dragging = false; |
| a9b2a64 | 98 | Content.studioPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| a9b2a64 | 99 | } |
| a9b2a64 | 100 | |
| a9b2a64 | 101 | @Override |
| a9b2a64 | 102 | public void onDragged() {
|
| a9b2a64 | 103 | if(selectedActor != null){
|
| a9b2a64 | 104 | if(!dragging){
|
| a9b2a64 | 105 | selectedActor.setPosition(Scene.mouse.x, Scene.mouse.y); |
| a9b2a64 | 106 | Frame.propertyPanel.updateProperty("X", ""+selectedActor.getX(), 0);
|
| a9b2a64 | 107 | Frame.propertyPanel.updateProperty("Y", ""+selectedActor.getY(), 0);
|
| a9b2a64 | 108 | StatusBar.updateXY(Scene.mouse.x, Scene.mouse.y); |
| a9b2a64 | 109 | |
| a9b2a64 | 110 | } |
| a9b2a64 | 111 | else |
| a9b2a64 | 112 | {
|
| a9b2a64 | 113 | float x = Scene.mouse.x; |
| a9b2a64 | 114 | float y = Scene.mouse.y; |
| a9b2a64 | 115 | float width = selectedActor.getWidth(), height = selectedActor.getHeight(); |
| a9b2a64 | 116 | float windowX = selectedActor.getX(), windowY = selectedActor.getY(); |
| a9b2a64 | 117 | if ((edge & Align.right) != 0) {
|
| a9b2a64 | 118 | width += x - lastX; |
| a9b2a64 | 119 | } |
| a9b2a64 | 120 | if ((edge & Align.top) != 0) {
|
| a9b2a64 | 121 | height += y - lastY; |
| a9b2a64 | 122 | } |
| a9b2a64 | 123 | if ((edge & Align.top) != 0 && (edge & Align.right) != 0) {
|
| a9b2a64 | 124 | width += x - lastX; |
| a9b2a64 | 125 | height += y - lastY; |
| a9b2a64 | 126 | } |
| a9b2a64 | 127 | if (edge == Align.left){
|
| a9b2a64 | 128 | float rot = getAngle(selectedActor.getX()+selectedActor.getOriginX(), |
| a9b2a64 | 129 | selectedActor.getY()+selectedActor.getOriginY(), x, y) |
| a9b2a64 | 130 | - selectedActor.getRotation(); |
| a9b2a64 | 131 | selectedActor.rotate(rot); |
| a9b2a64 | 132 | } |
| a9b2a64 | 133 | lastX = x; |
| a9b2a64 | 134 | lastY = y; |
| a9b2a64 | 135 | selectedActor.setBounds(Math.round(windowX), Math.round(windowY), Math.round(width), Math.round(height)); |
| a9b2a64 | 136 | Frame.propertyPanel.updateProperty("Width", ""+width, 0);
|
| a9b2a64 | 137 | Frame.propertyPanel.updateProperty("Height", ""+height, 0);
|
| a9b2a64 | 138 | } |
| a9b2a64 | 139 | Scene.isDirty = true; |
| a9b2a64 | 140 | } |
| a9b2a64 | 141 | } |
| a9b2a64 | 142 | |
| a9b2a64 | 143 | @Override |
| a9b2a64 | 144 | public void onGesture(GestureType type) {
|
| a9b2a64 | 145 | } |
| a9b2a64 | 146 | |
| a9b2a64 | 147 | private boolean isHand = true; |
| a9b2a64 | 148 | private boolean isRight = false; |
| a9b2a64 | 149 | private boolean isTop = false; |
| a9b2a64 | 150 | |
| a9b2a64 | 151 | @Override |
| a9b2a64 | 152 | public void act(float delta){
|
| a9b2a64 | 153 | if(selectedActor != null){
|
| a9b2a64 | 154 | isRight = false; |
| a9b2a64 | 155 | isTop = false; |
| a9b2a64 | 156 | //Stage.log("my"+Stage.mouse.y+"top"+selectedActor.getTop());
|
| a9b2a64 | 157 | if (Scene.mouse.x > selectedActor.getRight() - 10 && Scene.mouse.x < selectedActor.getRight()) {
|
| a9b2a64 | 158 | Content.studioPanel.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)); |
| a9b2a64 | 159 | isHand = false; |
| a9b2a64 | 160 | isRight = true; |
| a9b2a64 | 161 | } |
| a9b2a64 | 162 | if(Scene.mouse.y > selectedActor.getTop() - 10 && Scene.mouse.y < selectedActor.getTop()){
|
| a9b2a64 | 163 | Content.studioPanel.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)); |
| a9b2a64 | 164 | isHand = false; |
| a9b2a64 | 165 | isTop = true; |
| a9b2a64 | 166 | } |
| a9b2a64 | 167 | if (isRight && isTop) {
|
| a9b2a64 | 168 | Content.studioPanel.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)); |
| a9b2a64 | 169 | isHand = false; |
| a9b2a64 | 170 | } |
| a9b2a64 | 171 | if (!isRight && !isTop){
|
| a9b2a64 | 172 | if(!isHand){
|
| a9b2a64 | 173 | Content.studioPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| a9b2a64 | 174 | isHand = true; |
| a9b2a64 | 175 | } |
| a9b2a64 | 176 | } |
| a9b2a64 | 177 | } |
| a9b2a64 | 178 | } |
| a9b2a64 | 179 | |
| a9b2a64 | 180 | @Override |
| a9b2a64 | 181 | public void onKeyTyped(char key) {};
|
| a9b2a64 | 182 | @Override |
| a9b2a64 | 183 | public void onKeyUp(int keycode){};
|
| a9b2a64 | 184 | @Override |
| a9b2a64 | 185 | public void onKeyDown(int keycode){};
|
| a9b2a64 | 186 | @Override |
| a9b2a64 | 187 | public void onPause(){};
|
| a9b2a64 | 188 | @Override |
| a9b2a64 | 189 | public void onResume(){};
|
| a9b2a64 | 190 | @Override |
| a9b2a64 | 191 | public void onDispose(){};
|
| a9b2a64 | 192 | } |
| a9b2a64 | 193 | |
| a9b2a64 | 194 | class AddField extends Table {
|
| a9b2a64 | 195 | TextField tf; |
| a9b2a64 | 196 | TextButton addBtn; |
| a9b2a64 | 197 | TextButton removeBtn; |
| a9b2a64 | 198 | |
| a9b2a64 | 199 | public AddField(){
|
| a9b2a64 | 200 | super(Asset.skin); |
| a9b2a64 | 201 | setBackground(Asset.skin.getDrawable("dialogDim"));
|
| a9b2a64 | 202 | tf = new TextField("", Asset.skin);
|
| a9b2a64 | 203 | addBtn = new TextButton("Add", Asset.skin);
|
| a9b2a64 | 204 | removeBtn = new TextButton("Remove", Asset.skin);
|
| a9b2a64 | 205 | add(tf); |
| a9b2a64 | 206 | add(addBtn); |
| a9b2a64 | 207 | add(removeBtn); |
| a9b2a64 | 208 | pack(); |
| a9b2a64 | 209 | addBtn.addListener(new ClickListener(){
|
| a9b2a64 | 210 | @Override |
| a9b2a64 | 211 | public void clicked(InputEvent event, float x, float y){
|
| a9b2a64 | 212 | super.clicked(event, x, y); |
| a9b2a64 | 213 | if(!tf.getText().isEmpty()){
|
| a9b2a64 | 214 | if(SceneEditor.selectedActor instanceof List){
|
| a9b2a64 | 215 | List list = (List) SceneEditor.selectedActor; |
| a9b2a64 | 216 | Array<String> arr = new Array<String>(list.getItems()); |
| a9b2a64 | 217 | arr.add(tf.getText()); |
| a9b2a64 | 218 | list.setItems(arr.toArray()); |
| a9b2a64 | 219 | list.pack(); |
| a9b2a64 | 220 | AddField.this.setPosition(list.getX(), list.getY()-AddField.this.getHeight()); |
| a9b2a64 | 221 | } |
| a9b2a64 | 222 | if(SceneEditor.selectedActor instanceof SelectBox){
|
| a9b2a64 | 223 | SelectBox list = (SelectBox) SceneEditor.selectedActor; |
| a9b2a64 | 224 | Array<String> arr = new Array<String>(list.getItems()); |
| a9b2a64 | 225 | arr.add(tf.getText()); |
| a9b2a64 | 226 | list.setItems(arr.toArray()); |
| a9b2a64 | 227 | list.pack(); |
| a9b2a64 | 228 | AddField.this.setPosition(list.getX(), list.getY()-AddField.this.getHeight()); |
| a9b2a64 | 229 | } |
| a9b2a64 | 230 | |
| a9b2a64 | 231 | } |
| a9b2a64 | 232 | } |
| a9b2a64 | 233 | }); |
| a9b2a64 | 234 | removeBtn.addListener(new ClickListener(){
|
| a9b2a64 | 235 | @Override |
| a9b2a64 | 236 | public void clicked(InputEvent event, float x, float y){
|
| a9b2a64 | 237 | super.clicked(event, x, y); |
| a9b2a64 | 238 | if(SceneEditor.selectedActor instanceof List){
|
| a9b2a64 | 239 | List list = (List) SceneEditor.selectedActor; |
| a9b2a64 | 240 | if(list.getItems().length == 0) |
| a9b2a64 | 241 | return; |
| a9b2a64 | 242 | Array<String> arr = new Array<String>(list.getItems()); |
| a9b2a64 | 243 | list.setSelectedIndex(list.getItems().length-1); |
| a9b2a64 | 244 | arr.removeIndex(list.getSelectedIndex()); |
| a9b2a64 | 245 | list.setItems(arr.toArray()); |
| a9b2a64 | 246 | list.pack(); |
| a9b2a64 | 247 | AddField.this.setPosition(list.getX(), list.getY()-AddField.this.getHeight()); |
| a9b2a64 | 248 | } |
| a9b2a64 | 249 | if(SceneEditor.selectedActor instanceof SelectBox){
|
| a9b2a64 | 250 | SelectBox list = (SelectBox) SceneEditor.selectedActor; |
| a9b2a64 | 251 | if(list.getItems().length == 0) |
| a9b2a64 | 252 | return; |
| a9b2a64 | 253 | Array<String> arr = new Array<String>(list.getItems()); |
| a9b2a64 | 254 | list.setSelection(list.getItems().length-1); |
| a9b2a64 | 255 | arr.removeIndex(list.getSelectionIndex()); |
| a9b2a64 | 256 | list.setItems(arr.toArray()); |
| a9b2a64 | 257 | list.pack(); |
| a9b2a64 | 258 | AddField.this.setPosition(list.getX(), list.getY()-AddField.this.getHeight()); |
| a9b2a64 | 259 | } |
| a9b2a64 | 260 | } |
| a9b2a64 | 261 | }); |
| a9b2a64 | 262 | } |
| a9b2a64 | 263 | } |