Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/main/java/mindurka/MVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ private MVars() {}

public static boolean patchEditorLoaded = false;

// FIXME: This is bullshit.
private static final BitMap[] mapbitsPool = new BitMap[2];

private static BitMap mapbits = null;
public static BitMap mapbits() {
if (mapbits == null || mapbits.width != Vars.world.width() || mapbits.height != Vars.world.height()) {
mapbits = BitMap.of(Vars.world.tiles);
private static BitMap mapbits(int index) {
BitMap b = mapbitsPool[index];
if (b == null || b.width != Vars.world.width() || b.height != Vars.world.height()) {
mapbitsPool[index] = b = BitMap.of(Vars.world.tiles);
}
return mapbits;
}
private static BitMap mapbits2 = null;
public static BitMap mapbits2() {
if (mapbits2 == null || mapbits2.width != Vars.world.width() || mapbits2.height != Vars.world.height()) {
mapbits2 = BitMap.of(Vars.world.tiles);
}
return mapbits2;
return b;
}

public static BitMap mapbits() { return mapbits(0); }
public static BitMap mapbits2() { return mapbits(1); }
}
6 changes: 3 additions & 3 deletions src/main/java/mindurka/rules/FortsPlotKindSquare.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public void writeRules(RulesWrite write) {
public void writeTeamRules(Team team, RulesWrite write) {
write.button("rules.mindurka.forts.plot.schematic", () -> {
MVars.mapView.editorAction = new SchematicEditorAction(
wallsSize() * 2 + size, wallsSize() * 2 + size, scheme -> {
plotSchematic(team, scheme);
});
wallsSize() * 2 + size, wallsSize() * 2 + size,
scheme -> plotSchematic(team, scheme)
);
MVars.customRulesDialog.hide();
});
}
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/mindurka/rules/SchematicEditorAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,39 @@
import arc.math.geom.Point2;
import arc.math.geom.Vec2;
import arc.scene.ui.layout.Scl;
import lombok.RequiredArgsConstructor;
import mindurka.ui.OMapView;
import mindurka.ui.SpecialEditorAction;
import mindurka.util.FormatException;
import mindurka.util.Schematic;
import mindustry.Vars;
import mindustry.graphics.Pal;

@RequiredArgsConstructor
public class SchematicEditorAction implements SpecialEditorAction {
// TODO: Make it configurable.
private static final Schematic.Options OPTIONS = new Schematic.Options().skipEmpty().skipAir().skipBuildings();

private final int width, height;
private final Cons<Schematic> accepted;
private final Schematic.Options options;

public SchematicEditorAction(int width, int height, Cons<Schematic> accepted) {
Schematic.Options opts = new Schematic.Options();
opts.skipEmpty = true;
opts.skipAir = true;
opts.skipBuildings = true;
this.width = width;
this.height = height;
this.accepted = accepted;
this.options = opts;
}

public SchematicEditorAction(int width, int height, Cons<Schematic> accepted, Schematic.Options options) {
this.width = width;
this.height = height;
this.accepted = accepted;
this.options = options;
}
Comment thread
RE-AST marked this conversation as resolved.
Resolved
Comment thread
RE-AST marked this conversation as resolved.
Resolved

public Schematic.Options getOptions() {
return options;
}

@Override
public boolean clicked(OMapView view, float mouseX, float mouseY) {
Expand All @@ -30,7 +48,7 @@ public boolean clicked(OMapView view, float mouseX, float mouseY) {
|| p.y >= Vars.world.height() - height) return false;
Schematic scheme;
try {
scheme = Schematic.of(Vars.world.tiles, p.x, p.y, width, height, OPTIONS);
scheme = Schematic.of(Vars.world.tiles, p.x, p.y, width, height, options);
} catch (FormatException e) {
throw new RuntimeException("Unreachable!", e);
}
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/mindurka/ui/EditorTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ public void touched(ToolContext ctx, int x1, int y1, int x2, int y2) {

EditorTool.line(x1, y1, x2, y2, (x, y) -> {
if (x < 0 || y < 0 || x >= ctx.width() || y >= ctx.height()) return;
Block target = MVars.toolOptions.selectedBlock().isOverlay()
Block selected = MVars.toolOptions.selectedBlock();
boolean isBlock = !selected.isOverlay() && !selected.isFloor();
Block target = selected.isOverlay()
? ctx.overlay(x, y)
: MVars.toolOptions.selectedBlock().isFloor()
: selected.isFloor()
? ctx.floor(x, y)
: ctx.block(x, y);
if (ctx.block(x, y) == MVars.toolOptions.selectedBlock()) return;
mindustry.game.Team targetTeam = isBlock ? ctx.team(x, y) : null;
if (ctx.block(x, y) == selected && (!isBlock || ctx.team(x, y) == MVars.toolOptions.team())) return;

points.add(Util.packxy(x2, y2));

Expand All @@ -155,12 +158,14 @@ public void touched(ToolContext ctx, int x1, int y1, int x2, int y2) {
int py = Util.unpacky(point);
if (bits.toggled(px, py)) continue;
if (px < 0 || py < 0 || px >= ctx.width() || py >= ctx.height()) continue;
if ((MVars.toolOptions.selectedBlock().isOverlay()
Block at = selected.isOverlay()
? ctx.overlay(px, py)
: MVars.toolOptions.selectedBlock().isFloor()
: selected.isFloor()
? ctx.floor(px, py)
: ctx.block(px, py)) != target) continue;
ctx.setAny(px, py, MVars.toolOptions.selectedBlock());
: ctx.block(px, py);
if (at != target) continue;
if (isBlock && ctx.team(px, py) != targetTeam) continue;
ctx.setAny(px, py, selected);
bits.enable(px, py);

points.add(Util.packxy(px + 1, py));
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mindurka/ui/EraseToolContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public Block overlay(int x, int y) {
return tile.overlay();
}

@Override
public Team team(int x, int y) {
Tile tile = Vars.world.tiles.get(x, y);
if (tile == null) return Team.derelict;
return tile.team();
}

@Override
public int width() {
return Vars.world.width();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mindurka/ui/LayerToolContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ public Block overlay(int x, int y) {
return tile.overlay();
}

@Override
public Team team(int x, int y) {
Tile tile = Vars.world.tiles.get(x, y);
if (tile == null) return Team.derelict;
return tile.team();
}

@Override
public int width() {
return Vars.world.width();
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/mindurka/ui/OEditorDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ public void build() {
grid.update(() -> grid.setChecked(view.isGrid()));

addMode.get(EditorMode.zoom);
addMode.get(EditorMode.eraser);
Comment thread
RE-AST marked this conversation as resolved.
Resolved
tools.row();
addTool.get(EditorTool.pencil);
addTool.get(EditorTool.line);
Expand Down Expand Up @@ -429,7 +428,26 @@ public void build() {
button.setChecked(true);
});
button.setChecked(true);
t.add(button).size(size, size).left().top();
t.add(button).size(size, size).left().top().row();

Table optionsTable = new Table();
t.add(optionsTable).left().top().row();
t.update(() -> {
if (view.editorAction instanceof mindurka.rules.SchematicEditorAction) {
mindurka.util.Schematic.Options opts =
((mindurka.rules.SchematicEditorAction) view.editorAction).getOptions();
if (optionsTable.getChildren().isEmpty()) {
optionsTable.defaults().left().pad(2f);
optionsTable.check(Core.bundle.get("rules.schematic.skipEmpty", "Skip empty floors"), opts.skipEmpty, v -> opts.skipEmpty = v).row();
optionsTable.check(Core.bundle.get("rules.schematic.skipAir", "Skip air blocks"), opts.skipAir, v -> opts.skipAir = v).row();
optionsTable.check(Core.bundle.get("rules.schematic.skipBuildings", "Skip buildings"), opts.skipBuildings, v -> opts.skipBuildings = v).row();
optionsTable.check(Core.bundle.get("rules.schematic.skipNoOverlay", "Skip air overlays"), opts.skipNoOverlay, v -> opts.skipNoOverlay = v).row();
Comment thread
RE-AST marked this conversation as resolved.
Resolved
// TODO: Add team option.
}
} else {
optionsTable.clear();
}
});
}).grow();
Comment thread
RE-AST marked this conversation as resolved.
Resolved
midOverlay.background(Tex.whiteui);
midOverlay.setColor(Color.black);
Expand Down
66 changes: 51 additions & 15 deletions src/main/java/mindurka/ui/OMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,11 @@ public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCod
// Krita/Voidsprite/iBIS/AnyImageEdtiorInExistence-like binds cuz they are infinitely better than
// whatever the fuck Anuke cooked, and you're obligated to agree.
if (Vars.mobile || button == KeyCode.mouseLeft) {
// To be entirely fair, if your tool is zoom this is the same as zoom action.
if (editorAction != null) {
mousea = MouseAction.Draw.Cancelled.begin();
SpecialEditorAction prev = editorAction;
editorAction = null;
dragging = false;
prev.clicked(OMapView.this, x, y);
redrawPreview = true;
mousex = x;
mousey = y;
dragging = true;
return true;
} else mousea = MVars.toolOptions.mode.drawAction(x, y);
} else if (button == KeyCode.mouseRight) {
Expand Down Expand Up @@ -228,6 +225,16 @@ public void touchUp(InputEvent event, float x, float y, int pointer, KeyCode but
boolean isMain = activeTouches[0] != null && activeTouches[0].pointer == pointer;
removeTouch(pointer, button);

if (editorAction != null && dragging && isMain) {
SpecialEditorAction prev = editorAction;
editorAction = null;
mousea = null;
dragging = false;
prev.clicked(OMapView.this, x, y);
redrawPreview = true;
return;
}

if (mousea != null) {
if (mousea instanceof MouseAction.TouchDrag) {
if (activeTouches() == 0) mousea = null;
Expand Down Expand Up @@ -418,25 +425,54 @@ public void draw() {
} else MVars.toolOptions.tool.touched(previewMap, s.x, s.y, s.x, s.y);
}

// FIXME: Potential performance penalty for drawing a ton of one-length lines
if (previewMap.hasRegion) {
Draw.color(Pal.accent);
Lines.stroke(Scl.scl(2f));
for (int x = previewMap.startx; x <= previewMap.endx + 1; x++) for (int y = previewMap.starty; y <= previewMap.endy + 1; y++) {
boolean enabled = previewMap.bitmap.toggled(x, y);
if (enabled != previewMap.bitmap.toggled(x - 1, y)) {
Vec2 s = unproject(x, y);

for (int x = previewMap.startx; x <= previewMap.endx + 1; x++) {
int runStart = -1;
for (int y = previewMap.starty; y <= previewMap.endy + 1; y++) {
boolean border = previewMap.bitmap.toggled(x, y) != previewMap.bitmap.toggled(x - 1, y);
if (border && runStart == -1) {
runStart = y;
} else if (!border && runStart != -1) {
Vec2 s = unproject(x, runStart);
float sx = s.x, sy = s.y;
s = unproject(x, y);
Lines.line(sx, sy, s.x, s.y);
runStart = -1;
}
}
if (runStart != -1) {
Vec2 s = unproject(x, runStart);
float sx = s.x, sy = s.y;
s = unproject(x, y + 1);
s = unproject(x, previewMap.endy + 2);
Lines.line(sx, sy, s.x, s.y);
}
if (enabled != previewMap.bitmap.toggled(x, y - 1)) {
Vec2 s = unproject(x + 1, y);
}

for (int y = previewMap.starty; y <= previewMap.endy + 1; y++) {
int runStart = -1;
for (int x = previewMap.startx; x <= previewMap.endx + 1; x++) {
boolean border = previewMap.bitmap.toggled(x, y) != previewMap.bitmap.toggled(x, y - 1);
if (border && runStart == -1) {
runStart = x;
} else if (!border && runStart != -1) {
Vec2 s = unproject(runStart, y);
float sx = s.x, sy = s.y;
s = unproject(x, y);
Lines.line(sx, sy, s.x, s.y);
runStart = -1;
}
}
if (runStart != -1) {
Vec2 s = unproject(runStart, y);
float sx = s.x, sy = s.y;
s = unproject(x, y);
s = unproject(previewMap.endx + 2, y);
Lines.line(sx, sy, s.x, s.y);
}
}

Draw.reset();
}

Expand Down
34 changes: 13 additions & 21 deletions src/main/java/mindurka/ui/PlanetBackgroundDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public class PlanetBackgroundDialog extends BaseDialog {
private String savedTexture;
private Texture previewTexture;

private static final float DRAG_SPEED = 0.3f;
private static final float SCROLL_SPEED = 0.1f;
private static final float DRAG_SPEED = 0.1f;
private static final float SCROLL_SPEED = 0.25f;

public PlanetBackgroundDialog() {
super("@rules.planetbackground", new DialogStyle() {{
Expand Down Expand Up @@ -98,7 +98,7 @@ public PlanetBackgroundDialog() {

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode button) {
if (!hasBackground() || button != KeyCode.mouseRight) return false;
if (!hasBackground() || button != KeyCode.mouseRight && button != KeyCode.mouseLeft) return false;
lastX = x;
lastY = y;
dragging = true;
Expand All @@ -119,7 +119,7 @@ public void touchDragged(InputEvent event, float x, float y, int pointer) {
lastY = y;
if (!textureMode) {
rotX = Mathf.mod(rotX + dx * DRAG_SPEED, 360f);
rotY = Mathf.clamp(rotY + dy * DRAG_SPEED, 1f, 179f);
rotY = Mathf.clamp(rotY + dy * DRAG_SPEED*zoom/2, 1f, 179f);
}
updateParams();
}
Expand Down Expand Up @@ -488,13 +488,8 @@ private boolean hasBackground() {
}

private void setup() {
params = state.rules.planetBackground;

if (state.rules.backgroundTexture != null && !state.rules.backgroundTexture.isEmpty()) {
textureMode = true;
} else if (params != null) {
textureMode = false;
}
if(!textureMode) params = state.rules.planetBackground;
state.rules.planetBackground = textureMode ? null : params;

if (params != null) {
zoom = params.zoom > 0f ? params.zoom : 1f;
Expand Down Expand Up @@ -529,16 +524,13 @@ private void setup() {
}

private void updateParams() {
PlanetParams p = state.rules.planetBackground;
if (p != null) {
if (!textureMode) {
p.camPos = new Vec3(
Mathf.cosDeg(rotX) * Mathf.sinDeg(rotY),
Mathf.cosDeg(rotY),
Mathf.sinDeg(rotX) * Mathf.sinDeg(rotY)
);
}
p.zoom = zoom;
if (!textureMode) {
PlanetParams p = state.rules.planetBackground;
p.camPos = new Vec3(
Mathf.cosDeg(rotX) * Mathf.sinDeg(rotY),
Mathf.cosDeg(rotY),
Mathf.sinDeg(rotX) * Mathf.sinDeg(rotY)
);
}
PlanetBackgroundDrawer.update();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mindurka/ui/PlanetBackgroundDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void update() {

if (changed || resized || backgroundBuffer.resizeCheck(size, size)) {
changed = false;

backgroundBuffer.begin(Color.clear);

PlanetParams params = state.rules.planetBackground;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mindurka/ui/PreviewToolContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public Block overlay(int x, int y) {
return tile.overlay();
}

@Override
public Team team(int x, int y) {
Tile tile = Vars.world.tile(x, y);
if (tile == null) return Team.derelict;
return tile.team();
}

Comment thread
RE-AST marked this conversation as resolved.
Resolved
@Override
public int width() {
return Vars.world.width();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/mindurka/ui/ToolContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface ToolContext {
Block block(int x, int y);
Floor floor(int x, int y);
Block overlay(int x, int y);
Team team(int x, int y);

int width();
int height();
Expand Down
Loading
Loading