Skip to content

Commit bac09e4

Browse files
Merge pull request #4 from PhoenixofForce/selections
Selections
2 parents de17c94 + dd1b497 commit bac09e4

19 files changed

+754
-123
lines changed

src/Main.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ public class Main {
77
public static void main(String[] args) {
88
/* TODO:
99
More Important:
10-
-STRG+Z
10+
-Strg LC => copy Texture (color picker)
11+
-selection => delete....
12+
1113
-Improved Save-Directory
1214
-presets
1315
-change layer settings
1416
-open last/ view last edited files
15-
16-
Less Important
1717
-view used ressources (?)
18-
-copy paste
19-
-selection => move copy, delete....
2018
-settings (?)
2119
*/
2220

@@ -30,5 +28,4 @@ public static void main(String[] args) {
3028
}
3129
}
3230
}
33-
3431
}

src/data/GameMap.java

+57-6
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,17 @@ else if(l instanceof AreaLayer) {
135135
//finding biggest and smalles coordinates
136136
float sx = -1, sy = -1, bx = -1, by = -1;
137137
if(expo) {
138-
sx = Integer.MAX_VALUE;
139-
sy = Integer.MAX_VALUE;
140-
bx = Integer.MIN_VALUE;
141-
by = Integer.MIN_VALUE;
142-
143-
for(TileLayer l: tiles) {
138+
//sx = Integer.MAX_VALUE;
139+
//sy = Integer.MAX_VALUE;
140+
//bx = Integer.MIN_VALUE;
141+
//by = Integer.MIN_VALUE;
142+
143+
int[] bounds = getBounds();
144+
sx = bounds[0];
145+
sy = bounds[1];
146+
bx = bounds[2];
147+
by = bounds[3];
148+
/*for(TileLayer l: tiles) {
144149
float csx = l.smallestX(), csy = l.smallestY(), cbx = l.biggestX(), cby = l.biggestY();
145150
if(csx < sx) sx = csx;
146151
if(csy < sy) sy = csy;
@@ -188,6 +193,25 @@ else if(l instanceof AreaLayer) {
188193
return repl + out;
189194
}
190195

196+
public int[] getBounds() {
197+
int sx = Integer.MAX_VALUE,
198+
sy = Integer.MAX_VALUE,
199+
bx = Integer.MIN_VALUE,
200+
by = Integer.MIN_VALUE;
201+
202+
for(String s: layers.keySet()) {
203+
Layer l = layers.get(s);
204+
float csx = l.smallestX(), csy = l.smallestY(), cbx = l.biggestX(), cby = l.biggestY();
205+
if(csx < sx && csx != -1) sx = (int)Math.floor(csx);
206+
if(csy < sy && csy != -1) sy = (int)Math.floor(csy);
207+
if(cbx > bx) bx = (int)Math.ceil(cbx);
208+
if(cby > by) by = (int)Math.ceil(cby);
209+
210+
}
211+
212+
return new int[]{sx, sy, bx, by};
213+
}
214+
191215
@Override
192216
public String getText() {
193217
return "MAP";
@@ -197,6 +221,33 @@ public void setAutoTile(boolean at) {
197221
this.autoTile = at;
198222
}
199223

224+
@Override
225+
public GameMap clone() {
226+
GameMap out = new GameMap(width, height, tileSize);
227+
ArrayList<java.util.Map.Entry<String, Layer>> listOfEntry = new ArrayList<>(layers.entrySet());
228+
for(java.util.Map.Entry<String, Layer> e: listOfEntry) {
229+
Layer l = e.getValue();
230+
if(l instanceof TileLayer) {
231+
TileLayer tl = (TileLayer) l;
232+
tl.setMap(out);
233+
out.addLayer(e.getKey(), tl.clone());
234+
continue;
235+
}
236+
if(l instanceof FreeLayer) {
237+
FreeLayer tl = (FreeLayer) l;
238+
out.addLayer(e.getKey(), tl.clone());
239+
continue;
240+
}
241+
if(l instanceof AreaLayer) {
242+
AreaLayer tl = (AreaLayer) l;
243+
out.addLayer(e.getKey(), tl.clone());
244+
continue;
245+
}
246+
}
247+
out.setAutoTile(autoTile);
248+
return out;
249+
}
250+
200251
public boolean getAutoTile() {
201252
return autoTile;
202253
}

src/data/layer/AreaLayer.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ public boolean remove(float x, float y) {
119119
return false;
120120
}
121121

122-
@Override
123-
public void fill(String name, float x, float y) { }
124-
125122
@Override
126123
public void draw(Graphics g) {
127124
for (Area a: areas) {
@@ -160,6 +157,13 @@ public float biggestY() {
160157
return smallestY == Integer.MIN_VALUE? -1: smallestY;
161158
}
162159

160+
@Override
161+
public AreaLayer clone() {
162+
AreaLayer out = new AreaLayer(depth, width, height, tileSize);
163+
for(int i = 0; i < areas.size(); i++) out.areas.add(areas.get(i).clone());
164+
return out;
165+
}
166+
163167
@Override
164168
public String toMapFormat(List<String> names, float sx, float sy, float bx, float by) {
165169
String out = "";

src/data/layer/FreeLayer.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ public boolean remove(float x, float y) {
7676
return true;
7777
}
7878

79-
@Override
80-
public void fill(String name, float x, float y) { }
81-
8279
@Override
8380
public GO select(float x, float y) {
8481
GO go = find(x, y);
@@ -91,6 +88,29 @@ public GO select(float x, float y) {
9188
return go;
9289
}
9390

91+
public void moveAll(float dx, float dy) {
92+
for(int i = 0; i < images.size(); i++) {
93+
GO go = images.get(i);
94+
95+
go.move(dx, dy);
96+
if (go.x < 0 || go.x + go.width > this.width || go.y < 0 || go.y + go.height > this.height)
97+
go.move(-dx, -dy);
98+
}
99+
}
100+
101+
public void roundAll(int tileSize) {
102+
float smallestX = Integer.MAX_VALUE,
103+
smallestY = Integer.MAX_VALUE;
104+
105+
for(int i = 0; i < images.size(); i++) {
106+
GO r = images.get(i);
107+
if(smallestX > r.x) smallestX = r.x;
108+
if(smallestY > r.y) smallestY = r.y;
109+
}
110+
111+
moveAll(-(smallestX%1), -(smallestY%1));
112+
}
113+
94114
/**
95115
* @param x given x coordinate
96116
* @param y given y coordinate
@@ -143,6 +163,13 @@ public float biggestY() {
143163
return smallestY == Integer.MIN_VALUE? -1: smallestY;
144164
}
145165

166+
@Override
167+
public FreeLayer clone() {
168+
FreeLayer out = new FreeLayer(depth, width, height, tileSize);
169+
for(int i = 0; i < images.size(); i++) out.images.add(images.get(i).clone());
170+
return out;
171+
}
172+
146173
@Override
147174
public String toMapFormat(List<String> names, float sx, float sy, float bx, float by) {
148175
String out = "";

src/data/layer/Layer.java

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public interface Layer {
4848
*/
4949
boolean remove(float x, float y);
5050

51-
void fill(String name, float x, float y);
52-
5351
/**
5452
* draws the layer
5553
* @param g the graphics object which should draw the layer

src/data/layer/TileLayer.java

+43-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.awt.Graphics;
99

10-
import java.util.ArrayList;
10+
import java.awt.geom.Area;
1111
import java.util.List;
1212
import java.util.Random;
1313
import java.util.Stack;
@@ -51,7 +51,7 @@ public void set(String name, float x2, float y2, boolean drag) {
5151
}
5252

5353
private void update(int x, int y, boolean center) {
54-
if(!map.getAutoTile()) return;
54+
if(map != null && !map.getAutoTile()) return;
5555
if (x >= 0 && y >= 0 && x < width && y < height) {
5656
String name = tileNames[y][x];
5757
if (name == null) {
@@ -78,7 +78,7 @@ private void update(int x, int y, boolean center) {
7878
out += 1;
7979
if (x != height-1&&tileNames[y][x + 1] != null && tileNames[y][x + 1].startsWith(spriteSheet+"_block_"+blockName+"_"))
8080
out += 8;
81-
81+
8282
switch (out) {
8383
case 0:
8484
name = spriteSheet+"_block_"+blockName+"_" + "0";
@@ -177,8 +177,7 @@ public boolean remove(float x2, float y2) {
177177
return false;
178178
}
179179

180-
@Override
181-
public void fill(String name, float x2, float y2) {
180+
public void fill(Area sel, String name, float x2, float y2) {
182181
int x = (int) x2;
183182
int y = (int) y2;
184183
if (x >= 0 && y >= 0 && x < width && y < height) {
@@ -193,7 +192,30 @@ public void fill(String name, float x2, float y2) {
193192
while (!stack.isEmpty()) {
194193
Location i = stack.pop();
195194

196-
if (check(oldName, i.x, i.y)) {
195+
if (check(oldName, i.x, i.y) && (sel == null || (sel != null && sel.contains(i.x*map.getTileSize(), i.y*map.getTileSize())))) {
196+
set(name, i.x, i.y, false);
197+
198+
if (i.x > 0) stack.push(new Location(i.x - 1, i.y));
199+
if (i.y > 0) stack.push(new Location(i.x, i.y - 1));
200+
if (i.x < width - 1) stack.push(new Location(i.x + 1, i.y));
201+
if (i.y < height - 1) stack.push(new Location(i.x, i.y + 1));
202+
}
203+
}
204+
}
205+
}
206+
207+
public void fill(Area sel, String name) {
208+
int x = sel.getBounds().x+1;
209+
int y = sel.getBounds().y+1;
210+
if (x >= 0 && y >= 0 && x < width && y < height) {
211+
212+
Stack<Location> stack = new Stack<>();
213+
stack.push(new Location(x, y));
214+
215+
while (!stack.isEmpty()) {
216+
Location i = stack.pop();
217+
218+
if ((sel == null || (sel != null && sel.contains(i.x*map.getTileSize(), i.y*map.getTileSize())))) {
197219
set(name, i.x, i.y, false);
198220

199221
if (i.x > 0) stack.push(new Location(i.x - 1, i.y));
@@ -218,6 +240,10 @@ public GO select(float x, float y) {
218240
return null;
219241
}
220242

243+
public void setMap(GameMap map) {
244+
this.map = map;
245+
}
246+
221247
/**
222248
* @return the tile grid
223249
*/
@@ -288,6 +314,17 @@ public float biggestY() {
288314
return smallestY == Integer.MIN_VALUE? -1: smallestY;
289315
}
290316

317+
@Override
318+
public TileLayer clone() {
319+
TileLayer out = new TileLayer(null, depth, width, height, tileSize);
320+
for(int y = 0; y < height; y++) {
321+
for(int x = 0; x < width; x++) {
322+
out.tileNames[y][x] = tileNames[y][x];
323+
}
324+
}
325+
return out;
326+
}
327+
291328
@Override
292329
public String toMapFormat(List<String> names, float sx, float sy, float bx, float by) {
293330
int startX = Math.max(0, (int) Math.floor(sx));

src/data/layer/layerobjects/Area.java

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public void setColor(Color color) {
111111
this.color = color;
112112
}
113113

114+
@Override
115+
public Area clone() {
116+
Area a = new Area(x1, y1, x2, y2, color);
117+
for(int i = 0; i < getTags().size(); i++) a.addTag(getTags().get(i).clone());
118+
return a;
119+
}
120+
114121
/**
115122
* converts class to saveable text-format
116123
* @return

src/data/layer/layerobjects/GO.java

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public void move(float dx, float dy) {
2626
y += dy;
2727
}
2828

29+
@Override
30+
public GO clone() {
31+
GO g = new GO(name, x, y, width, height);
32+
for(int i = 0; i < getTags().size(); i++) g.addTag(getTags().get(i).clone());
33+
return g;
34+
}
35+
2936
/**
3037
* converts class to saveable text-format
3138
* @return

src/data/layer/layerobjects/Tag.java

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public String getName() {
4848
return name;
4949
}
5050

51+
@Override
52+
public Tag clone() {
53+
return new Tag(name, action);
54+
}
55+
5156
@Override
5257
public boolean equals(Object b) {
5358
if(b instanceof Tag) {

src/window/ClipBoardUtil.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package window;
2+
3+
import java.awt.*;
4+
import java.awt.datatransfer.Clipboard;
5+
import java.awt.datatransfer.DataFlavor;
6+
import java.awt.datatransfer.StringSelection;
7+
8+
public class ClipBoardUtil {
9+
10+
public static void StringToClip(String in) {
11+
StringSelection stringSelection = new StringSelection(in);
12+
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
13+
clipboard.setContents(stringSelection, null);
14+
}
15+
16+
public static String ClipToString() {
17+
try {
18+
return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
19+
} catch (Exception e) {
20+
return "";
21+
}
22+
}
23+
}

src/window/Tools.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.awt.*;
44

55
public enum Tools {
6-
BRUSH(0, Color.GREEN), ERASER(1, Color.RED), BUCKET(2, Color.BLUE);
6+
BRUSH(0, Color.GREEN), ERASER(1, Color.RED), BUCKET(2, Color.BLUE), SELECT(3, Color.YELLOW), MOVE(4, Color.YELLOW);
77

88
private int pos;
99
private Color c;
@@ -15,7 +15,23 @@ public enum Tools {
1515
public Tools next() {
1616
return Tools.values()[(pos+1)%Tools.values().length];
1717
}
18+
19+
public Tools pre() {
20+
int npos = (pos-1);
21+
while(npos < 0) npos += Tools.values().length;
22+
return Tools.values()[npos];
23+
}
24+
1825
public Color getColor() {
1926
return c;
2027
}
28+
public int getIndex() {
29+
return pos;
30+
}
31+
32+
public static Tools get(int i) {
33+
int j = Tools.values().length;
34+
while(i < 0) i+= j;
35+
return Tools.values()[i%j];
36+
}
2137
}

0 commit comments

Comments
 (0)