Skip to content

Commit

Permalink
Provide additional methods for adding entities
Browse files Browse the repository at this point in the history
Closes #95
  • Loading branch information
steffen-wilke committed Jul 21, 2018
1 parent 83bd915 commit 89e1dc5
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 18 deletions.
Binary file added utiliti/resources/cursor-add.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions utiliti/resources/localization/strings.properties
Expand Up @@ -174,3 +174,13 @@ input_prompt_name_title=Enter blueprint name
input_replace_map=Do you really want to replace the existing map {0}?
input_replace_map_title=Replace Map
input_select_project_folder=Select a project folder

add_prop=Add Prop
add_creature=Add Creature
add_light=Add Light
add_trigger=Add Trigger
add_spawnpoint=Add Spawnpoint
add_collisionbox=Add Collisionbox
add_area=Add Area
add_shadow=Add Static Shadow
add_emitter=Add Emitter
2 changes: 1 addition & 1 deletion utiliti/src/de/gurkenlabs/utiliti/Icons.java
Expand Up @@ -15,7 +15,7 @@ public final class Icons {
public static final Icon FOLDER = new ImageIcon(Resources.getImage("object_cube-10x10.png"));
public static final Icon LIGHT = new ImageIcon(Resources.getImage("bulb.png"));
public static final Icon TRIGGER = new ImageIcon(Resources.getImage("trigger.png"));
public static final Icon SPAWMPOINT = new ImageIcon(Resources.getImage("spawnpoint.png"));
public static final Icon SPAWNPOINT = new ImageIcon(Resources.getImage("spawnpoint.png"));
public static final Icon COLLISIONBOX = new ImageIcon(Resources.getImage("collisionbox.png"));
public static final Icon MAPAREA = new ImageIcon(Resources.getImage("maparea.png"));
public static final Icon SHADOWBOX = new ImageIcon(Resources.getImage("shadowbox.png"));
Expand Down
5 changes: 2 additions & 3 deletions utiliti/src/de/gurkenlabs/utiliti/MapSelectionPanel.java
Expand Up @@ -52,7 +52,6 @@
import de.gurkenlabs.litiengine.entities.Spawnpoint;
import de.gurkenlabs.litiengine.entities.StaticShadow;
import de.gurkenlabs.litiengine.entities.Trigger;
import de.gurkenlabs.litiengine.environment.tilemap.IMap;
import de.gurkenlabs.litiengine.environment.tilemap.IMapObject;
import de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer;
import de.gurkenlabs.litiengine.environment.tilemap.MapObjectType;
Expand Down Expand Up @@ -265,7 +264,7 @@ public void focusLost(FocusEvent e) {
this.nodeCreatures = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_creatures"), Icons.CREATURE));
this.nodeLights = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_lights"), Icons.LIGHT));
this.nodeTriggers = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_triggers"), Icons.TRIGGER));
this.nodeSpawnpoints = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_spawnpoints"), Icons.SPAWMPOINT));
this.nodeSpawnpoints = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_spawnpoints"), Icons.SPAWNPOINT));
this.nodeCollisionBoxes = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_collboxes"), Icons.COLLISIONBOX));
this.nodeMapAreas = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_areas"), Icons.MAPAREA));
this.nodeStaticShadows = new DefaultMutableTreeNode(new IconTreeListItem(Resources.get("panel_mapselection_shadow"), Icons.SHADOWBOX));
Expand Down Expand Up @@ -634,7 +633,7 @@ private void updateMapObjectTree() {
this.nodeProps.setUserObject(new IconTreeListItem(Game.getEnvironment().getProps().size() + " " + Resources.get("panel_mapselection_props"), Icons.PROP));
this.nodeCreatures.setUserObject(new IconTreeListItem(Game.getEnvironment().getCreatures().size() + " " + Resources.get("panel_mapselection_creatures"), Icons.CREATURE));
this.nodeTriggers.setUserObject(new IconTreeListItem(Game.getEnvironment().getTriggers().size() + " " + Resources.get("panel_mapselection_triggers"), Icons.TRIGGER));
this.nodeSpawnpoints.setUserObject(new IconTreeListItem(Game.getEnvironment().getSpawnPoints().size() + " " + Resources.get("panel_mapselection_spawnpoints"), Icons.SPAWMPOINT));
this.nodeSpawnpoints.setUserObject(new IconTreeListItem(Game.getEnvironment().getSpawnPoints().size() + " " + Resources.get("panel_mapselection_spawnpoints"), Icons.SPAWNPOINT));
this.nodeCollisionBoxes.setUserObject(new IconTreeListItem(Game.getEnvironment().getCollisionBoxes().size() + " " + Resources.get("panel_mapselection_collboxes"), Icons.COLLISIONBOX));
this.nodeMapAreas.setUserObject(new IconTreeListItem(Game.getEnvironment().getAreas().size() + " " + Resources.get("panel_mapselection_areas"), Icons.MAPAREA));
this.nodeStaticShadows.setUserObject(new IconTreeListItem(Game.getEnvironment().getStaticShadows().size() + " " + Resources.get("panel_mapselection_shadow"), Icons.SHADOWBOX));
Expand Down
69 changes: 66 additions & 3 deletions utiliti/src/de/gurkenlabs/utiliti/Program.java
Expand Up @@ -40,6 +40,7 @@
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
Expand All @@ -62,6 +63,7 @@
import de.gurkenlabs.litiengine.Game;
import de.gurkenlabs.litiengine.GameAdapter;
import de.gurkenlabs.litiengine.Resources;
import de.gurkenlabs.litiengine.environment.tilemap.MapObjectType;
import de.gurkenlabs.litiengine.environment.tilemap.MapProperty;
import de.gurkenlabs.litiengine.environment.tilemap.xml.Map;
import de.gurkenlabs.litiengine.graphics.ImageFormat;
Expand All @@ -79,6 +81,7 @@
public class Program {
public static final Font TEXT_FONT = new JLabel().getFont().deriveFont(10f);
public static final BufferedImage CURSOR = Resources.getImage("cursor.png");
public static final BufferedImage CURSOR_ADD = Resources.getImage("cursor-add.png");
public static final BufferedImage CURSOR_MOVE = Resources.getImage("cursor-move.png");
public static final BufferedImage CURSOR_SELECT = Resources.getImage("cursor-select.png");
public static final BufferedImage CURSOR_LOAD = Resources.getImage("cursor-load.png");
Expand All @@ -98,6 +101,7 @@ public class Program {
private static AssetPanel assetPanel;
private static AssetTree assetTree;
private static JPopupMenu canvasPopup;
private static JPopupMenu addPopupMenu;
private static JLabel statusBar;
private static boolean isChanging;

Expand Down Expand Up @@ -247,7 +251,7 @@ private static void setupInterface() {
// remove canvas because we want to add a wrapping panel
window.remove(canvas);

initCanvasPopupMenu(canvas);
initPopupMenus(canvas);

JPanel renderPanel = new JPanel(new BorderLayout());
renderPanel.add(canvas);
Expand Down Expand Up @@ -693,7 +697,7 @@ private static JToolBar initToolBar() {
basicMenu.add(mv);

place.addActionListener(a -> {

addPopupMenu.show(place, 0, place.getHeight());
place.setSelected(true);
ed.setSelected(false);
mv.setSelected(false);
Expand Down Expand Up @@ -722,19 +726,27 @@ private static JToolBar initToolBar() {
ed.setSelected(false);
mv.setSelected(false);
place.setSelected(true);
place.requestFocus();
Game.getScreenManager().getRenderComponent().setCursor(Program.CURSOR_ADD, 0, 0);
}

if (i == MapComponent.EDITMODE_EDIT) {
place.setSelected(false);
mv.setSelected(false);
ed.setSelected(true);
ed.requestFocus();
Game.getScreenManager().getRenderComponent().setCursor(CURSOR, 0, 0);
}

if (i == MapComponent.EDITMODE_MOVE) {
if(!mv.isEnabled()) {
return;
}

ed.setSelected(false);
place.setSelected(false);
mv.setSelected(true);
mv.requestFocus();
Game.getScreenManager().getRenderComponent().setCursor(CURSOR_MOVE, 0, 0);
}
});
Expand Down Expand Up @@ -871,8 +883,58 @@ private static JToolBar initToolBar() {
return basicMenu;
}

private static void initCanvasPopupMenu(Canvas canvas) {
private static void initAddMenu(JComponent addMenu) {
JMenuItem addProp = new JMenuItem(Resources.get("add_prop"), Icons.PROP);
addProp.addActionListener(a -> setCreateMode(MapObjectType.PROP));

JMenuItem addCreature = new JMenuItem(Resources.get("add_creature"), Icons.CREATURE);
addCreature.addActionListener(a -> setCreateMode(MapObjectType.CREATURE));

JMenuItem addLight = new JMenuItem(Resources.get("add_light"), Icons.LIGHT);
addLight.addActionListener(a -> setCreateMode(MapObjectType.LIGHTSOURCE));

JMenuItem addTrigger = new JMenuItem(Resources.get("add_trigger"), Icons.TRIGGER);
addTrigger.addActionListener(a -> setCreateMode(MapObjectType.TRIGGER));

JMenuItem addSpawnpoint = new JMenuItem(Resources.get("add_spawnpoint"), Icons.SPAWNPOINT);
addSpawnpoint.addActionListener(a -> setCreateMode(MapObjectType.SPAWNPOINT));

JMenuItem addCollisionBox = new JMenuItem(Resources.get("add_collisionbox"), Icons.COLLISIONBOX);
addCollisionBox.addActionListener(a -> setCreateMode(MapObjectType.COLLISIONBOX));

JMenuItem addMapArea = new JMenuItem(Resources.get("add_area"), Icons.MAPAREA);
addMapArea.addActionListener(a -> setCreateMode(MapObjectType.AREA));

JMenuItem addShadow = new JMenuItem(Resources.get("add_shadow"), Icons.SHADOWBOX);
addShadow.addActionListener(a -> setCreateMode(MapObjectType.STATICSHADOW));

JMenuItem addEmitter = new JMenuItem(Resources.get("add_emitter"), Icons.EMITTER);
addEmitter.addActionListener(a -> setCreateMode(MapObjectType.EMITTER));

addMenu.add(addProp);
addMenu.add(addCreature);
addMenu.add(addLight);
addMenu.add(addTrigger);
addMenu.add(addSpawnpoint);
addMenu.add(addCollisionBox);
addMenu.add(addMapArea);
addMenu.add(addShadow);
addMenu.add(addEmitter);
}

private static void setCreateMode(MapObjectType tpye) {
EditorScreen.instance().getMapComponent().setEditMode(MapComponent.EDITMODE_CREATE);
EditorScreen.instance().getMapObjectPanel().setMapObjectType(tpye);
}

private static void initPopupMenus(Canvas canvas) {
canvasPopup = new JPopupMenu();
addPopupMenu = new JPopupMenu();
JMenu addSubMenu = new JMenu("Add ...");
addSubMenu.setIcon(Icons.ADD);
initAddMenu(addPopupMenu);
initAddMenu(addSubMenu);

JMenuItem delete = new JMenuItem("Delete Entity", new ImageIcon(Resources.getImage("button-deletex16.png")));
delete.addActionListener(e -> EditorScreen.instance().getMapComponent().delete());
delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
Expand All @@ -897,6 +959,7 @@ private static void initCanvasPopupMenu(Canvas canvas) {
blueprint.addActionListener(e -> EditorScreen.instance().getMapComponent().defineBlueprint());
blueprint.setEnabled(false);

canvasPopup.add(addSubMenu);
canvasPopup.add(paste);
canvasPopup.addSeparator();
canvasPopup.add(copy);
Expand Down
39 changes: 28 additions & 11 deletions utiliti/src/de/gurkenlabs/utiliti/components/MapComponent.java
Expand Up @@ -512,13 +512,11 @@ public void setEditMode(int editMode) {
case EDITMODE_CREATE:
this.setFocus(null, true);
EditorScreen.instance().getMapObjectPanel().bind(null);
Game.getScreenManager().getRenderComponent().setCursor(Program.CURSOR_ADD, 0, 0);
break;
case EDITMODE_EDIT:
Game.getScreenManager().getRenderComponent().setCursor(Program.CURSOR, 0, 0);
break;
case EDITMODE_MOVE:
Game.getScreenManager().getRenderComponent().setCursor(Program.CURSOR_MOVE, 0, 0);
break;
default:
break;
}
Expand Down Expand Up @@ -827,10 +825,14 @@ private static IMapObjectLayer getCurrentLayer() {
private IMapObject createNewMapObject(MapObjectType type) {
IMapObject mo = new MapObject();
mo.setType(type.toString());
mo.setX((int) this.newObjectArea.getX());
mo.setY((int) this.newObjectArea.getY());
mo.setWidth((int) this.newObjectArea.getWidth());
mo.setHeight((int) this.newObjectArea.getHeight());
mo.setX((float) this.newObjectArea.getX());
mo.setY((float) this.newObjectArea.getY());

// ensure a minimum size for the new object
float width = (float) this.newObjectArea.getWidth();
float height = (float) this.newObjectArea.getHeight();
mo.setWidth(width == 0 ? 16 : width);
mo.setHeight(height == 0 ? 16 : height);
mo.setId(Game.getEnvironment().getNextMapId());
mo.setName("");

Expand Down Expand Up @@ -1120,6 +1122,13 @@ private void setupKeyboardControls() {
this.setEditMode(EDITMODE_MOVE);
}
});

Input.keyboard().onKeyPressed(KeyEvent.VK_ESCAPE, e -> {
if (this.currentEditMode == EDITMODE_CREATE) {
this.setEditMode(EDITMODE_EDIT);
}
});

Input.keyboard().onKeyReleased(KeyEvent.VK_CONTROL, e -> this.setEditMode(EDITMODE_EDIT));

Input.keyboard().onKeyReleased(KeyEvent.VK_Z, e -> {
Expand Down Expand Up @@ -1283,7 +1292,9 @@ private void handleMouseWheelScrolled(ComponentMouseWheelEvent e) {
*/
private void handleMouseMoved(ComponentMouseEvent e) {
if (this.getFocus() == null) {
Game.getScreenManager().getRenderComponent().setCursor(Program.CURSOR, 0, 0);
if (this.currentEditMode != EDITMODE_CREATE) {
Game.getScreenManager().getRenderComponent().setCursor(Program.CURSOR, 0, 0);
}
this.currentTransform = TransformType.NONE;
return;
}
Expand Down Expand Up @@ -1325,7 +1336,9 @@ private void handleMousePressed(ComponentMouseEvent e) {

switch (this.currentEditMode) {
case EDITMODE_CREATE:
this.startPoint = Input.mouse().getMapLocation();
if (SwingUtilities.isLeftMouseButton(e.getEvent())) {
this.startPoint = Input.mouse().getMapLocation();
}
break;
case EDITMODE_MOVE:
break;
Expand Down Expand Up @@ -1353,7 +1366,10 @@ private void handleMouseDragged(ComponentMouseEvent e) {
return;
}

newObjectArea = this.getCurrentMouseSelectionArea(true);
if (SwingUtilities.isLeftMouseButton(e.getEvent())) {
newObjectArea = this.getCurrentMouseSelectionArea(true);
}

break;
case EDITMODE_EDIT:
if (Input.keyboard().isPressed(KeyEvent.VK_CONTROL)) {
Expand Down Expand Up @@ -1390,9 +1406,10 @@ private void handleMouseReleased(ComponentMouseEvent e) {

switch (this.currentEditMode) {
case EDITMODE_CREATE:
if (this.newObjectArea == null) {
if (this.newObjectArea == null || !SwingUtilities.isLeftMouseButton(e.getEvent())) {
break;
}

IMapObject mo = this.createNewMapObject(EditorScreen.instance().getMapObjectPanel().getObjectType());
this.newObjectArea = null;
this.setFocus(mo, !Input.keyboard().isPressed(KeyEvent.VK_SHIFT));
Expand Down
2 changes: 2 additions & 0 deletions utiliti/src/de/gurkenlabs/utiliti/swing/FileDrop.java
Expand Up @@ -23,6 +23,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import de.gurkenlabs.utiliti.swing.FileDrop.TransferableObject;

/**
* This class makes it easy to drag and drop files from the operating system to
* a Java program. Any <tt>java.awt.Component</tt> can be dropped onto, but only
Expand Down
Expand Up @@ -203,6 +203,10 @@ private void switchPanel(MapObjectType type) {
this.componentPanel.repaint();
}

public void setMapObjectType(MapObjectType type) {
this.comboBoxType.setSelectedItem(type);
}

@Override
protected void clearControls() {
// clear controls
Expand Down

0 comments on commit 89e1dc5

Please sign in to comment.