Skip to content

Commit 4e90c21

Browse files
[*] Changed UI elements:
*Dropdown for layer selectino *Content from TextField gets selected on click *Last Textfields also confirms action when pressing [Enter] [*] GameMap <=> TileLayer communication removed because of bugs
1 parent f7c5593 commit 4e90c21

8 files changed

Lines changed: 107 additions & 92 deletions

File tree

src/data/GameMap.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import data.layer.layerobjects.GO;
55
import data.layer.layerobjects.Tag;
66
import data.layer.layerobjects.TagObject;
7+
import window.Window;
78

89
import java.util.ArrayList;
910
import java.util.HashMap;
@@ -14,13 +15,13 @@
1415
* the map contains all layers
1516
*/
1617
public class GameMap extends TagObject {
18+
private Window w;
19+
1720
private int width, height, tileSize; //width, height and tileSize of the map
1821
private Map<String, Layer> layers; //Layers saved to their name
1922

20-
private int autoTile = 4;
21-
22-
public GameMap(int width, int height, int tileSize) {
23-
this(width, height, tileSize, true);
23+
public GameMap(Window w, int width, int height, int tileSize) {
24+
this(w, width, height, tileSize, true);
2425
}
2526

2627
/**
@@ -30,7 +31,9 @@ public GameMap(int width, int height, int tileSize) {
3031
* @param tileSize tilesize of the map
3132
* @param b true if the default layers should be added
3233
*/
33-
public GameMap(int width, int height, int tileSize, boolean b) {
34+
public GameMap(Window w, int width, int height, int tileSize, boolean b) {
35+
this.w = w;
36+
3437
layers = new HashMap<>();
3538
this.width = width;
3639
this.height = height;
@@ -40,7 +43,7 @@ public GameMap(int width, int height, int tileSize, boolean b) {
4043

4144
if(b) {
4245
addLayer("Background", new FreeLayer(1.0f, width, height, tileSize));
43-
addLayer("Tile", new TileLayer(this, 0.5f, width, height, tileSize));
46+
addLayer("Tile", new TileLayer(w, 0.5f, width, height, tileSize));
4447
addLayer("Object", new FreeLayer(0.0f, width, height, tileSize));
4548
addLayer("Camera", new AreaLayer(-1.0f, width, height, tileSize));
4649
}
@@ -217,20 +220,14 @@ public String getText() {
217220
return "MAP";
218221
}
219222

220-
public void setAutoTile(int at) {
221-
this.autoTile = at;
222-
}
223-
224223
@Override
225224
public GameMap clone() {
226-
GameMap out = new GameMap(width, height, tileSize);
227-
out.setAutoTile(autoTile);
225+
GameMap out = new GameMap(w, width, height, tileSize);
228226
ArrayList<java.util.Map.Entry<String, Layer>> listOfEntry = new ArrayList<>(layers.entrySet());
229227
for(java.util.Map.Entry<String, Layer> e: listOfEntry) {
230228
Layer l = e.getValue();
231229
if(l instanceof TileLayer) {
232230
TileLayer tl = (TileLayer) l;
233-
tl.setMap(out);
234231
out.addLayer(e.getKey(), tl.clone());
235232
continue;
236233
}
@@ -247,19 +244,4 @@ public GameMap clone() {
247244
}
248245
return out;
249246
}
250-
251-
public void updateMap() {
252-
ArrayList<java.util.Map.Entry<String, Layer>> listOfEntry = new ArrayList<>(layers.entrySet());
253-
for(java.util.Map.Entry<String, Layer> e: listOfEntry) {
254-
Layer l = e.getValue();
255-
if (l instanceof TileLayer) {
256-
TileLayer tl = (TileLayer) l;
257-
tl.setMap(this);
258-
}
259-
}
260-
}
261-
262-
public int getAutoTile() {
263-
return autoTile;
264-
}
265247
}

src/data/layer/TileLayer.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import data.Location;
55
import data.layer.layerobjects.GO;
66
import data.TextureHandler;
7+
import window.Window;
78

89
import java.awt.Graphics;
910

@@ -36,20 +37,20 @@ public class TileLayer implements Layer {
3637
private int width, height, tileSize; //width, height and tilesize of the map
3738
private Random r;
3839

39-
private GameMap map;
40+
private Window w;
4041

41-
public TileLayer(GameMap map, float depth, String[][] tiles, int tileSize) {
42+
public TileLayer(Window w, float depth, String[][] tiles, int tileSize) {
4243
this.depth = depth;
4344
this.tileNames = tiles;
4445
this.tileSize = tileSize;
4546
this.width = tiles[0].length;
4647
this.height = tiles.length;
4748
r = new Random();
48-
this.map = map;
49+
this.w = w;
4950
}
5051

51-
public TileLayer(GameMap map, float depth, int width, int height, int tileSize) {
52-
this(map, depth, new String[height][width], tileSize);
52+
public TileLayer(Window w, float depth, int width, int height, int tileSize) {
53+
this(w, depth, new String[height][width], tileSize);
5354
}
5455

5556
@Override
@@ -64,7 +65,7 @@ public void set(String name, float x2, float y2, boolean drag) {
6465
}
6566

6667
private void update(int x, int y, boolean center) {
67-
if(map != null && 2==map.getAutoTile()) return;
68+
if(0==w.getAutoTile()) return;
6869
if (x >= 0 && y >= 0 && x < width && y < height) {
6970
String name = tileNames[y][x];
7071
if (name == null) {
@@ -73,7 +74,7 @@ private void update(int x, int y, boolean center) {
7374
update(x-1, y, false);
7475
update(x, y-1, false);
7576
update(x, y+1, false);
76-
if(map.getAutoTile() == 8) {
77+
if(w.getAutoTile() == 2) {
7778
update(x + 1, y - 1, false);
7879
update(x - 1, y + 1, false);
7980
update(x - 1, y - 1, false);
@@ -89,7 +90,7 @@ private void update(int x, int y, boolean center) {
8990
String blockPart = parts[3];
9091

9192
int out = 0;
92-
if(map.getAutoTile() == 4) {
93+
if(w.getAutoTile() == 1) {
9394
if (y != 0 && tileNames[y - 1][x] != null && tileNames[y - 1][x].startsWith(spriteSheet + "_block_" + blockName + "_"))
9495
out ^= 12;
9596
if (y != height - 1 && tileNames[y + 1][x] != null && tileNames[y + 1][x].startsWith(spriteSheet + "_block_" + blockName + "_"))
@@ -98,7 +99,7 @@ private void update(int x, int y, boolean center) {
9899
out ^= 3;
99100
if (x != width - 1 && tileNames[y][x + 1] != null && tileNames[y][x + 1].startsWith(spriteSheet + "_block_" + blockName + "_"))
100101
out ^= 1;
101-
} else if(map.getAutoTile() == 8) {
102+
} else if(w.getAutoTile() == 2) {
102103

103104
if (y != 0 && tileNames[y - 1][x] != null && tileNames[y - 1][x].startsWith(spriteSheet + "_block_" + blockName + "_"))
104105
out ^= 2;
@@ -132,7 +133,7 @@ private void update(int x, int y, boolean center) {
132133
update(x-1, y, false);
133134
update(x, y-1, false);
134135
update(x, y+1, false);
135-
if(map.getAutoTile() == 8) {
136+
if(w.getAutoTile() == 2) {
136137
update(x + 1, y - 1, false);
137138
update(x - 1, y + 1, false);
138139
update(x - 1, y - 1, false);
@@ -155,7 +156,7 @@ private void update(int x, int y, boolean center) {
155156
update(x-1, y, false);
156157
update(x, y-1, false);
157158
update(x, y+1, false);
158-
if(map.getAutoTile() == 8) {
159+
if(w.getAutoTile() == 2) {
159160
update(x + 1, y - 1, false);
160161
update(x - 1, y + 1, false);
161162
update(x - 1, y - 1, false);
@@ -180,6 +181,12 @@ public boolean remove(float x2, float y2) {
180181
update(x-1, y, false);
181182
update(x, y-1, false);
182183
update(x, y+1, false);
184+
if(w.getAutoTile() == 2) {
185+
update(x + 1, y - 1, false);
186+
update(x - 1, y + 1, false);
187+
update(x - 1, y - 1, false);
188+
update(x + 1, y + 1, false);
189+
}
183190
return true;
184191
}
185192
return false;
@@ -200,7 +207,7 @@ public void fill(Area sel, String name, float x2, float y2) {
200207
while (!stack.isEmpty()) {
201208
Location i = stack.pop();
202209

203-
if (check(oldName, i.x, i.y) && (sel == null || (sel != null && sel.contains(i.x*map.getTileSize(), i.y*map.getTileSize())))) {
210+
if (check(oldName, i.x, i.y) && (sel == null || (sel != null && sel.contains(i.x*w.getMap().getTileSize(), i.y*w.getMap().getTileSize())))) {
204211
set(name, i.x, i.y, false);
205212

206213
if (i.x > 0) stack.push(new Location(i.x - 1, i.y));
@@ -223,7 +230,7 @@ public void fill(Area sel, String name) {
223230
while (!stack.isEmpty()) {
224231
Location i = stack.pop();
225232

226-
if ((sel == null || (sel != null && sel.contains(i.x*map.getTileSize(), i.y*map.getTileSize())))) {
233+
if ((sel == null || (sel != null && sel.contains(i.x*w.getMap().getTileSize(), i.y*w.getMap().getTileSize())))) {
227234
set(name, i.x, i.y, false);
228235

229236
if (i.x > 0) stack.push(new Location(i.x - 1, i.y));
@@ -239,7 +246,7 @@ private boolean check(String oldName, float y, float x) {
239246
String name = tileNames[(int)x][(int)y];
240247
boolean bool1 = (oldName == null && name == null);
241248
boolean bool2 = ((name != null && oldName != null) && name.equals(oldName));
242-
boolean bool3 = (map.getAutoTile() > 2 && oldName != null && name != null && name.contains("block") && oldName.contains("block") && name.split("_")[2].equalsIgnoreCase(oldName.split("_")[2]));
249+
boolean bool3 = (w.getAutoTile() > 0 && oldName != null && name != null && name.contains("block") && oldName.contains("block") && name.split("_")[2].equalsIgnoreCase(oldName.split("_")[2]));
243250
return bool1 || bool2 || bool3;
244251
}
245252

@@ -248,10 +255,6 @@ public GO select(float x, float y) {
248255
return null;
249256
}
250257

251-
public void setMap(GameMap map) {
252-
this.map = map;
253-
}
254-
255258
/**
256259
* @return the tile grid
257260
*/

src/window/UserInputs.java

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
import javax.swing.*;
88
import java.awt.*;
9-
import java.awt.event.ActionEvent;
10-
import java.awt.event.ActionListener;
11-
import java.awt.event.WindowAdapter;
12-
import java.awt.event.WindowEvent;
9+
import java.awt.event.*;
1310

1411
/**
1512
* used to allow the user to make better inputs
@@ -25,10 +22,17 @@ public static void createLayer(Window window, LayerPane layerPane) {
2522

2623
JFrame frame = new JFrame("Adding layer");
2724
JTextField nameInput = new JTextField("name");
28-
JSlider layerType = new JSlider(JSlider.HORIZONTAL, 0, 2, 2);
29-
JLabel layerText = new JLabel("Tile - Area - Pixel");
25+
JComboBox<String> layerType = new JComboBox<>();
26+
layerType.addItem("Tile");
27+
layerType.addItem("Area");
28+
layerType.addItem("Pixel");
29+
3030
JButton create = new JButton("Create");
3131
JTextField depthInput = new JTextField("depth");
32+
33+
addSelectOnClick(nameInput);
34+
addSelectOnClick(depthInput);
35+
3236
//allow float only
3337
depthInput.setInputVerifier(new InputVerifier() {
3438
@Override
@@ -68,7 +72,7 @@ public void windowClosed(WindowEvent e) {
6872
frame.setLayout(null);
6973
frame.setVisible(true);
7074
Insets i = frame.getInsets();
71-
frame.setSize(250 + i.left + i.right, 150 + i.top + i.bottom);
75+
frame.setSize(250 + i.left + i.right, 135 + i.top + i.bottom);
7276
frame.setAlwaysOnTop(true);
7377
frame.setLocationRelativeTo(window);
7478

@@ -77,25 +81,23 @@ public void windowClosed(WindowEvent e) {
7781

7882
frame.add(layerType);
7983
layerType.setBounds(5, 35, 100, 25);
80-
layerType.setMinimum(0);
81-
layerType.setMaximum(2);
82-
layerType.setMajorTickSpacing(1);
83-
84-
frame.add(layerText);
85-
layerText.setBounds(5, 60, 100, 25);
8684

8785
frame.add(depthInput);
88-
depthInput.setBounds(5, 90, 100, 25);
86+
depthInput.setBounds(5, 65, 100, 25);
8987

9088
frame.add(create);
91-
create.setBounds(5, 120, 100, 25);
92-
create.addActionListener(e -> {
89+
create.setBounds(5, 105, 100, 25);
90+
91+
ActionListener a = e->{
9392
String name = nameInput.getText();
94-
layerPane.addLayer(name, layerType.getValue(), Float.parseFloat(depthInput.getText()));
93+
layerPane.addLayer(name, layerType.getSelectedIndex(), Float.parseFloat(depthInput.getText()));
9594
window.setEnabled(true);
9695
window.toFront();
9796
frame.dispose();
98-
});
97+
};
98+
99+
create.addActionListener(a);
100+
depthInput.addActionListener(a);
99101
}
100102

101103
/** Creates window so the user can set the settings of the new tag
@@ -109,6 +111,7 @@ public static void tagName(Window window, Modifier mod) {
109111
JTextField nameIN = new JTextField("Name");
110112
JButton create = new JButton("Create");
111113

114+
addSelectOnClick(nameIN);
112115

113116
frame.addWindowListener(new WindowAdapter() {
114117
@Override
@@ -140,12 +143,16 @@ public void windowClosed(WindowEvent e) {
140143

141144
frame.add(create);
142145
create.setBounds(5, 35, 100, 25);
143-
create.addActionListener(e -> {
146+
147+
ActionListener a = e->{
144148
mod.add(nameIN.getText());
145149
window.setEnabled(true);
146150
window.toFront();
147151
frame.dispose();
148-
});
152+
};
153+
154+
create.addActionListener(a);
155+
nameIN.addActionListener(a);
149156
}
150157

151158
/** Creates window so the user can set the settings of the new map
@@ -160,6 +167,10 @@ public static void newMap(Window window) {
160167
JTextField mapWidthIn = new JTextField("Map width");
161168
JTextField mapHeightIn = new JTextField("Map height");
162169

170+
addSelectOnClick(tileSizeIn);
171+
addSelectOnClick(mapHeightIn);
172+
addSelectOnClick(mapWidthIn);
173+
163174
InputVerifier inputVerifier = new InputVerifier() {
164175

165176
@Override
@@ -216,13 +227,17 @@ public void windowClosed(WindowEvent e) {
216227

217228
frame.add(create);
218229
create.setBounds(5, 95, 100, 25);
219-
create.addActionListener(e -> {
220-
window.setMap(new GameMap(Integer.parseInt(mapWidthIn.getText()), Integer.valueOf(mapHeightIn.getText()), Integer.parseInt(tileSizeIn.getText())), true);
221230

222-
window.setEnabled(true);
223-
window.toFront();
224-
frame.dispose();
225-
});
231+
232+
ActionListener a = e-> {
233+
window.setMap(new GameMap(window, Integer.parseInt(mapWidthIn.getText()), Integer.valueOf(mapHeightIn.getText()), Integer.parseInt(tileSizeIn.getText())), true);
234+
235+
window.setEnabled(true);
236+
window.toFront();
237+
frame.dispose();
238+
};
239+
create.addActionListener(a);
240+
mapHeightIn.addActionListener(a);
226241
}
227242

228243
/** Creates window so the user can confirm his action
@@ -284,4 +299,25 @@ public void windowClosed(WindowEvent e) {
284299
actionListener.actionPerformed(new ActionEvent(window, 0, null));
285300
});
286301
}
302+
303+
304+
305+
306+
307+
308+
309+
310+
private static void addSelectOnClick(JTextField textField) {
311+
textField.addFocusListener(new FocusListener() {
312+
@Override
313+
public void focusGained(FocusEvent e) {
314+
textField.selectAll();
315+
}
316+
317+
@Override
318+
public void focusLost(FocusEvent e) {
319+
320+
}
321+
});
322+
}
287323
}

0 commit comments

Comments
 (0)