Less terrible logic UI / Unary operations
This commit is contained in:
@@ -18,7 +18,8 @@ public enum BinaryOp{
|
||||
and("and", (a, b) -> (int)a & (int)b),
|
||||
xor("xor", (a, b) -> (int)a ^ (int)b),
|
||||
max("max", Math::max),
|
||||
min("min", Math::min);
|
||||
min("min", Math::min),
|
||||
atan2("atan2", Math::atan2);
|
||||
|
||||
public static final BinaryOp[] all = values();
|
||||
|
||||
@@ -30,6 +31,11 @@ public enum BinaryOp{
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return symbol;
|
||||
}
|
||||
|
||||
interface OpLambda{
|
||||
double get(double a, double b);
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class LCanvas extends Table{
|
||||
add(new SetStatement());
|
||||
add(new JumpStatement());
|
||||
add(new EnableStatement());
|
||||
add(new OpStatement());
|
||||
add(new BinaryOpStatement());
|
||||
}
|
||||
|
||||
private void drawGrid(){
|
||||
|
||||
@@ -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{
|
||||
|
||||
@Override
|
||||
@@ -296,8 +314,8 @@ public class LExecutor{
|
||||
}else{
|
||||
out.setLength(0);
|
||||
//display integer version when possible
|
||||
if(Math.abs(v.numval - (int)v.numval) < 0.000001){
|
||||
out.append((int)v.numval);
|
||||
if(Math.abs(v.numval - (long)v.numval) < 0.000001){
|
||||
out.append((long)v.numval);
|
||||
}else{
|
||||
out.append(v.numval);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.*;
|
||||
import arc.func.*;
|
||||
import arc.math.*;
|
||||
import arc.scene.*;
|
||||
import arc.scene.actions.*;
|
||||
import arc.scene.ui.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.util.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.logic.LCanvas.*;
|
||||
@@ -23,6 +29,46 @@ public abstract class LStatement{
|
||||
.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){
|
||||
LogicIO.write(this,builder);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.graphics.*;
|
||||
import arc.scene.ui.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import mindustry.annotations.Annotations.*;
|
||||
import mindustry.logic.LCanvas.*;
|
||||
@@ -141,8 +140,8 @@ public class LStatements{
|
||||
}
|
||||
}
|
||||
|
||||
@RegisterStatement("op")
|
||||
public static class OpStatement extends LStatement{
|
||||
@RegisterStatement("bop")
|
||||
public static class BinaryOpStatement extends LStatement{
|
||||
public BinaryOp op = BinaryOp.add;
|
||||
public String a = "a", b = "b", dest = "result";
|
||||
|
||||
@@ -156,11 +155,10 @@ public class LStatements{
|
||||
|
||||
field(table, a, str -> a = str);
|
||||
|
||||
TextButton[] button = {null};
|
||||
button[0] = table.button(op.symbol, Styles.cleart, () -> {
|
||||
op = BinaryOp.all[(op.ordinal() + 1) % BinaryOp.all.length];
|
||||
button[0].setText(op.symbol);
|
||||
}).size(50f, 30f).pad(4f).get();
|
||||
table.button(b -> {
|
||||
b.label(() -> op.symbol);
|
||||
b.clicked(() -> showSelect(b, BinaryOp.all, op, o -> op = o));
|
||||
}, Styles.cleart, () -> {}).size(50f, 40f).pad(4f);
|
||||
|
||||
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")
|
||||
public static class EndStatement extends LStatement{
|
||||
@Override
|
||||
|
||||
35
core/src/mindustry/logic/UnaryOp.java
Normal file
35
core/src/mindustry/logic/UnaryOp.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user