|
| 1 | +package games.rednblack.talos.editor.addons.vectorfield; |
| 2 | + |
| 3 | +import com.badlogic.gdx.Gdx; |
| 4 | +import com.badlogic.gdx.files.FileHandle; |
| 5 | +import com.badlogic.gdx.scenes.scene2d.InputEvent; |
| 6 | +import com.badlogic.gdx.scenes.scene2d.ui.Image; |
| 7 | +import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; |
| 8 | +import com.badlogic.gdx.utils.Array; |
| 9 | +import com.kotcrab.vis.ui.widget.Menu; |
| 10 | +import com.kotcrab.vis.ui.widget.MenuBar; |
| 11 | +import com.kotcrab.vis.ui.widget.MenuItem; |
| 12 | +import com.kotcrab.vis.ui.widget.file.FileChooserAdapter; |
| 13 | +import games.rednblack.talos.TalosMain; |
| 14 | +import games.rednblack.talos.editor.addons.IAddon; |
| 15 | +import games.rednblack.talos.editor.dialogs.SettingsDialog; |
| 16 | +import games.rednblack.talos.editor.project.IProject; |
| 17 | + |
| 18 | +public class VectorFieldAddon implements IAddon { |
| 19 | + |
| 20 | + public static VectorFieldProject VF_PROJECT; |
| 21 | + |
| 22 | + private VectorFieldWorkspace workspace; |
| 23 | + private VectorFieldPropertiesPanel propertiesPanel; |
| 24 | + |
| 25 | + @Override |
| 26 | + public void init () { |
| 27 | + VF_PROJECT = new VectorFieldProject(this); |
| 28 | + workspace = new VectorFieldWorkspace(); |
| 29 | + propertiesPanel = new VectorFieldPropertiesPanel(TalosMain.Instance().UIStage().getSkin(), workspace); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void initUIContent () { |
| 34 | + TalosMain.Instance().UIStage().swapToAddonContent(propertiesPanel, workspace, null); |
| 35 | + TalosMain.Instance().disableNodeStage(); |
| 36 | + TalosMain.Instance().UIStage().Menu().disableTalosSpecific(); |
| 37 | + TalosMain.Instance().UIStage().getStage().setKeyboardFocus(workspace); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean projectFileDrop (FileHandle handle) { |
| 42 | + if (handle.extension().equals("tvf")) { |
| 43 | + TalosMain.Instance().ProjectController().setProject(getProjectType()); |
| 44 | + TalosMain.Instance().ProjectController().loadProject(handle); |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + IProject currProjectType = TalosMain.Instance().ProjectController().getProject(); |
| 49 | + |
| 50 | + if (currProjectType == VF_PROJECT) { |
| 51 | + if (handle.extension().equals("fga")) { |
| 52 | + String data = handle.readString(); |
| 53 | + workspace.importFGA(data); |
| 54 | + propertiesPanel.syncFromWorkspace(); |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public IProject getProjectType () { |
| 64 | + return VF_PROJECT; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void announceLocalSettings (SettingsDialog settingsDialog) { |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void buildMenu (MenuBar menuBar) { |
| 73 | + Menu menu = new Menu("Vector Field"); |
| 74 | + |
| 75 | + MenuItem newFile = new MenuItem("New Vector Field", icon("ic-file-new")); |
| 76 | + menu.addItem(newFile); |
| 77 | + MenuItem openFile = new MenuItem("Open Vector Field", icon("ic-folder")); |
| 78 | + menu.addItem(openFile); |
| 79 | + MenuItem importFga = new MenuItem("Import .fga"); |
| 80 | + menu.addItem(importFga); |
| 81 | + MenuItem exportFga = new MenuItem("Export .fga"); |
| 82 | + menu.addItem(exportFga); |
| 83 | + |
| 84 | + newFile.addListener(new ClickListener() { |
| 85 | + @Override |
| 86 | + public void clicked (InputEvent event, float x, float y) { |
| 87 | + TalosMain.Instance().ProjectController().newProject(VF_PROJECT); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + openFile.addListener(new ClickListener() { |
| 92 | + @Override |
| 93 | + public void clicked (InputEvent event, float x, float y) { |
| 94 | + TalosMain.Instance().UIStage().openProjectAction(VF_PROJECT); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + importFga.addListener(new ClickListener() { |
| 99 | + @Override |
| 100 | + public void clicked (InputEvent event, float x, float y) { |
| 101 | + TalosMain.Instance().UIStage().showFileChooser(".fga", new FileChooserAdapter() { |
| 102 | + @Override |
| 103 | + public void selected (Array<FileHandle> files) { |
| 104 | + if (files.size > 0) { |
| 105 | + // ensure we're in VF project mode |
| 106 | + IProject currProject = TalosMain.Instance().ProjectController().getProject(); |
| 107 | + if (currProject != VF_PROJECT) { |
| 108 | + TalosMain.Instance().ProjectController().newProject(VF_PROJECT); |
| 109 | + } |
| 110 | + String data = files.get(0).readString(); |
| 111 | + workspace.importFGA(data); |
| 112 | + propertiesPanel.syncFromWorkspace(); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + exportFga.addListener(new ClickListener() { |
| 120 | + @Override |
| 121 | + public void clicked (InputEvent event, float x, float y) { |
| 122 | + TalosMain.Instance().UIStage().showSaveFileChooser(".fga", new FileChooserAdapter() { |
| 123 | + @Override |
| 124 | + public void selected (Array<FileHandle> files) { |
| 125 | + if (files.size > 0) { |
| 126 | + String fgaData = workspace.exportFGA(); |
| 127 | + files.get(0).writeString(fgaData, false); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + menuBar.addMenu(menu); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void dispose () { |
| 139 | + } |
| 140 | + |
| 141 | + public VectorFieldWorkspace getWorkspace () { |
| 142 | + return workspace; |
| 143 | + } |
| 144 | + |
| 145 | + public VectorFieldPropertiesPanel getPropertiesPanel () { |
| 146 | + return propertiesPanel; |
| 147 | + } |
| 148 | + |
| 149 | + private Image icon (String name) { |
| 150 | + return new Image(TalosMain.Instance().UIStage().getSkin().getDrawable(name)); |
| 151 | + } |
| 152 | +} |
0 commit comments