Less terrible logic UI / Unary operations

This commit is contained in:
Anuken
2020-08-08 20:36:45 -04:00
parent 2042dafb73
commit 4dc90d4074
10 changed files with 156 additions and 31 deletions

View File

@@ -18,7 +18,8 @@ public enum BinaryOp{
and("and", (a, b) -> (int)a & (int)b), and("and", (a, b) -> (int)a & (int)b),
xor("xor", (a, b) -> (int)a ^ (int)b), xor("xor", (a, b) -> (int)a ^ (int)b),
max("max", Math::max), max("max", Math::max),
min("min", Math::min); min("min", Math::min),
atan2("atan2", Math::atan2);
public static final BinaryOp[] all = values(); public static final BinaryOp[] all = values();
@@ -30,6 +31,11 @@ public enum BinaryOp{
this.function = function; this.function = function;
} }
@Override
public String toString(){
return symbol;
}
interface OpLambda{ interface OpLambda{
double get(double a, double b); double get(double a, double b);
} }

View File

@@ -58,7 +58,7 @@ public class LCanvas extends Table{
add(new SetStatement()); add(new SetStatement());
add(new JumpStatement()); add(new JumpStatement());
add(new EnableStatement()); add(new EnableStatement());
add(new OpStatement()); add(new BinaryOpStatement());
} }
private void drawGrid(){ private void drawGrid(){

View File

@@ -257,6 +257,24 @@ public class LExecutor{
} }
} }
public static class UnaryOpI implements LInstruction{
public UnaryOp op;
public int value, dest;
public UnaryOpI(UnaryOp op, int value, int dest){
this.op = op;
this.value = value;
this.dest = dest;
}
UnaryOpI(){}
@Override
public void run(LExecutor exec){
exec.setnum(dest, op.function.get(exec.num(value)));
}
}
public static class EndI implements LInstruction{ public static class EndI implements LInstruction{
@Override @Override
@@ -296,8 +314,8 @@ public class LExecutor{
}else{ }else{
out.setLength(0); out.setLength(0);
//display integer version when possible //display integer version when possible
if(Math.abs(v.numval - (int)v.numval) < 0.000001){ if(Math.abs(v.numval - (long)v.numval) < 0.000001){
out.append((int)v.numval); out.append((long)v.numval);
}else{ }else{
out.append(v.numval); out.append(v.numval);
} }

View File

@@ -1,7 +1,13 @@
package mindustry.logic; package mindustry.logic;
import arc.*;
import arc.func.*; import arc.func.*;
import arc.math.*;
import arc.scene.*;
import arc.scene.actions.*;
import arc.scene.ui.*;
import arc.scene.ui.layout.*; import arc.scene.ui.layout.*;
import arc.util.*;
import arc.util.ArcAnnotate.*; import arc.util.ArcAnnotate.*;
import mindustry.gen.*; import mindustry.gen.*;
import mindustry.logic.LCanvas.*; import mindustry.logic.LCanvas.*;
@@ -23,6 +29,46 @@ public abstract class LStatement{
.size(130f, 40f).pad(2f).color(table.color); .size(130f, 40f).pad(2f).color(table.color);
} }
protected <T> void showSelect(Button b, T[] values, T current, Cons<T> getter){
Table t = new Table(Tex.button);
//triggers events behind the element to simulate deselection
Element hitter = new Element();
Runnable hide = () -> {
Core.app.post(hitter::remove);
t.actions(Actions.fadeOut(0.3f, Interp.fade), Actions.remove());
};
hitter.fillParent = true;
hitter.tapped(hide);
Core.scene.add(hitter);
Core.scene.add(t);
t.update(() -> {
b.localToStageCoordinates(Tmp.v1.set(b.getWidth()/2f, b.getHeight()/2f));
Tmp.v1.clamp(0, 0, Core.graphics.getWidth() - b.getWidth(), Core.graphics.getHeight() - b.getHeight());
t.setPosition(Tmp.v1.x, Tmp.v1.y, Align.center);
});
t.actions(Actions.alpha(0), Actions.fadeIn(0.3f, Interp.fade));
ButtonGroup<Button> group = new ButtonGroup<>();
int i = 0;
t.defaults().size(56f, 40f);
for(T p : values){
t.button(p.toString(), Styles.clearTogglet, () -> {
getter.get(p);
hide.run();
}).checked(current == p).group(group);
if(++i % 4 == 0) t.row();
}
t.pack();
}
public void write(StringBuilder builder){ public void write(StringBuilder builder){
LogicIO.write(this,builder); LogicIO.write(this,builder);
} }

View File

@@ -1,7 +1,6 @@
package mindustry.logic; package mindustry.logic;
import arc.graphics.*; import arc.graphics.*;
import arc.scene.ui.*;
import arc.scene.ui.layout.*; import arc.scene.ui.layout.*;
import mindustry.annotations.Annotations.*; import mindustry.annotations.Annotations.*;
import mindustry.logic.LCanvas.*; import mindustry.logic.LCanvas.*;
@@ -141,8 +140,8 @@ public class LStatements{
} }
} }
@RegisterStatement("op") @RegisterStatement("bop")
public static class OpStatement extends LStatement{ public static class BinaryOpStatement extends LStatement{
public BinaryOp op = BinaryOp.add; public BinaryOp op = BinaryOp.add;
public String a = "a", b = "b", dest = "result"; public String a = "a", b = "b", dest = "result";
@@ -156,11 +155,10 @@ public class LStatements{
field(table, a, str -> a = str); field(table, a, str -> a = str);
TextButton[] button = {null}; table.button(b -> {
button[0] = table.button(op.symbol, Styles.cleart, () -> { b.label(() -> op.symbol);
op = BinaryOp.all[(op.ordinal() + 1) % BinaryOp.all.length]; b.clicked(() -> showSelect(b, BinaryOp.all, op, o -> op = o));
button[0].setText(op.symbol); }, Styles.cleart, () -> {}).size(50f, 40f).pad(4f);
}).size(50f, 30f).pad(4f).get();
field(table, b, str -> b = str); field(table, b, str -> b = str);
} }
@@ -176,6 +174,36 @@ public class LStatements{
} }
} }
@RegisterStatement("uop")
public static class UnaryOpStatement extends LStatement{
public UnaryOp op = UnaryOp.negate;
public String value = "b", dest = "result";
@Override
public void build(Table table){
field(table, dest, str -> dest = str);
table.add(" = ");
table.button(b -> {
b.label(() -> op.symbol);
b.clicked(() -> showSelect(b, UnaryOp.all, op, o -> op = o));
}, Styles.cleart, () -> {}).size(50f, 40f).pad(3f);
field(table, value, str -> value = str);
}
@Override
public LInstruction build(LAssembler builder){
return new UnaryOpI(op, builder.var(value), builder.var(dest));
}
@Override
public LCategory category(){
return LCategory.operations;
}
}
@RegisterStatement("end") @RegisterStatement("end")
public static class EndStatement extends LStatement{ public static class EndStatement extends LStatement{
@Override @Override

View File

@@ -0,0 +1,35 @@
package mindustry.logic;
public enum UnaryOp{
negate("-", a -> -a),
not("not", a -> ~(int)(a)),
abs("abs", Math::abs),
log("log", Math::log),
log10("log10", Math::log10),
sin("sin", Math::sin),
cos("cos", Math::cos),
tan("tan", Math::tan),
floor("floor", Math::floor),
ceil("ceil", Math::ceil),
sqrt("sqrt", Math::sqrt),
;
public static final UnaryOp[] all = values();
public final UnaryOpLambda function;
public final String symbol;
UnaryOp(String symbol, UnaryOpLambda function){
this.symbol = symbol;
this.function = function;
}
@Override
public String toString(){
return symbol;
}
interface UnaryOpLambda{
double get(double a);
}
}

View File

@@ -54,9 +54,8 @@ public class MirrorFilter extends GenerateFilter{
float size = Math.max(image.getWidth() *2, image.getHeight()*2); float size = Math.max(image.getWidth() *2, image.getHeight()*2);
Cons<Vec2> clamper = v -> v.clamp( Cons<Vec2> clamper = v -> v.clamp(
image.x + image.getWidth()/2f - imageWidth/2f, image.x + image.getWidth()/2f - imageWidth/2f,
image.x + image.getWidth()/2f + imageWidth/2f, image.y + image.getHeight()/2f - imageHeight/2f, image.y + image.getHeight()/2f + imageHeight/2f, image.x + image.getWidth()/2f + imageWidth/2f
image.y + image.getHeight()/2f - imageHeight/2f, );
image.y + image.getHeight()/2f + imageHeight/2f);
clamper.get(Tmp.v1.trns(angle - 90, size).add(image.getWidth()/2f + image.x, image.getHeight()/2f + image.y)); clamper.get(Tmp.v1.trns(angle - 90, size).add(image.getWidth()/2f + image.x, image.getHeight()/2f + image.y));
clamper.get(Tmp.v2.set(Tmp.v1).sub(image.getWidth()/2f + image.x, image.getHeight()/2f + image.y).rotate(180f).add(image.getWidth()/2f + image.x, image.getHeight()/2f + image.y)); clamper.get(Tmp.v2.set(Tmp.v1).sub(image.getWidth()/2f + image.x, image.getHeight()/2f + image.y).rotate(180f).add(image.getWidth()/2f + image.x, image.getHeight()/2f + image.y));

View File

@@ -59,7 +59,7 @@ public class PayloadAcceptor extends Block{
@Override @Override
public void handlePayload(Building source, Payload payload){ public void handlePayload(Building source, Payload payload){
this.payload = (T)payload; this.payload = (T)payload;
this.payVector.set(source).sub(this).clamp(-size * tilesize / 2f, size * tilesize / 2f, -size * tilesize / 2f, size * tilesize / 2f); this.payVector.set(source).sub(this).clamp(-size * tilesize / 2f, -size * tilesize / 2f, size * tilesize / 2f, size * tilesize / 2f);
this.payRotation = source.angleTo(this); this.payRotation = source.angleTo(this);
updatePayload(); updatePayload();
@@ -108,7 +108,7 @@ public class PayloadAcceptor extends Block{
payRotation = rotdeg(); payRotation = rotdeg();
if(payVector.len() >= size * tilesize/2f){ if(payVector.len() >= size * tilesize/2f){
payVector.clamp(-size * tilesize / 2f, size * tilesize / 2f, -size * tilesize / 2f, size * tilesize / 2f); payVector.clamp(-size * tilesize / 2f, -size * tilesize / 2f, size * tilesize / 2f, size * tilesize / 2f);
Building front = front(); Building front = front();
if(front != null && front.block().outputsPayload){ if(front != null && front.block().outputsPayload){

View File

@@ -63,10 +63,8 @@ public class Pump extends LiquidBlock{
if(isMultiblock()){ if(isMultiblock()){
Liquid last = null; Liquid last = null;
for(Tile other : tile.getLinkedTilesAs(this, tempTiles)){ for(Tile other : tile.getLinkedTilesAs(this, tempTiles)){
if(other.floor().liquidDrop == null) if(other.floor().liquidDrop == null) continue;
continue; if(other.floor().liquidDrop != last && last != null) return false;
if(other.floor().liquidDrop != last && last != null)
return false;
last = other.floor().liquidDrop; last = other.floor().liquidDrop;
} }
return last != null; return last != null;
@@ -100,16 +98,11 @@ public class Pump extends LiquidBlock{
amount = 0f; amount = 0f;
liquidDrop = null; liquidDrop = null;
if(isMultiblock()){ for(Tile other : tile.getLinkedTiles(tempTiles)){
for(Tile other : tile.getLinkedTiles(tempTiles)){ if(canPump(other)){
if(canPump(other)){ liquidDrop = other.floor().liquidDrop;
liquidDrop = other.floor().liquidDrop; amount += other.floor().liquidMultiplier;
amount += other.floor().liquidMultiplier;
}
} }
}else{
amount = tile.floor().liquidMultiplier;
liquidDrop = tile.floor().liquidDrop;
} }
} }

View File

@@ -1,3 +1,3 @@
org.gradle.daemon=true org.gradle.daemon=true
org.gradle.jvmargs=-Xms256m -Xmx1024m org.gradle.jvmargs=-Xms256m -Xmx1024m
archash=ba33933b2a1e2acc180429b9db3fedf39f2f95f4 archash=93182a28a728438ae456919ec0265afdb77d5090