Processor global variable dialog
This commit is contained in:
@@ -27,45 +27,63 @@ public class GlobalVars{
|
||||
public static final Rand rand = new Rand();
|
||||
|
||||
//non-constants that depend on state
|
||||
private static int varTime, varTick, varSecond, varMinute, varWave, varWaveTime, varServer, varClient, varClientLocale, varClientUnit, varClientName, varClientTeam, varClientMobile;
|
||||
private static int varTime, varTick, varSecond, varMinute, varWave, varWaveTime, varMapW, varMapH, varServer, varClient, varClientLocale, varClientUnit, varClientName, varClientTeam, varClientMobile;
|
||||
|
||||
private ObjectIntMap<String> namesToIds = new ObjectIntMap<>();
|
||||
private Seq<Var> vars = new Seq<>(Var.class);
|
||||
private Seq<VarEntry> varEntries = new Seq<>();
|
||||
private IntSet privilegedIds = new IntSet();
|
||||
private UnlockableContent[][] logicIdToContent;
|
||||
private int[][] contentIdToLogicId;
|
||||
|
||||
public void init(){
|
||||
put("the end", null);
|
||||
putEntryOnly("sectionProcessor");
|
||||
|
||||
putEntryOnly("@this");
|
||||
putEntryOnly("@thisx");
|
||||
putEntryOnly("@thisy");
|
||||
putEntryOnly("@links");
|
||||
putEntryOnly("@ipt");
|
||||
|
||||
putEntryOnly("sectionGeneral");
|
||||
|
||||
put("the end", null, false, true);
|
||||
//add default constants
|
||||
put("false", 0);
|
||||
put("true", 1);
|
||||
put("null", null);
|
||||
putEntry("false", 0);
|
||||
putEntry("true", 1);
|
||||
put("null", null, false, true);
|
||||
|
||||
//math
|
||||
put("@pi", Mathf.PI);
|
||||
put("π", Mathf.PI); //for the "cool" kids
|
||||
put("@e", Mathf.E);
|
||||
put("@degToRad", Mathf.degRad);
|
||||
put("@radToDeg", Mathf.radDeg);
|
||||
putEntry("@pi", Mathf.PI);
|
||||
put("π", Mathf.PI, false, true); //for the "cool" kids
|
||||
putEntry("@e", Mathf.E);
|
||||
putEntry("@degToRad", Mathf.degRad);
|
||||
putEntry("@radToDeg", Mathf.radDeg);
|
||||
|
||||
putEntryOnly("sectionMap");
|
||||
|
||||
//time
|
||||
varTime = put("@time", 0);
|
||||
varTick = put("@tick", 0);
|
||||
varSecond = put("@second", 0);
|
||||
varMinute = put("@minute", 0);
|
||||
varWave = put("@waveNumber", 0);
|
||||
varWaveTime = put("@waveTime", 0);
|
||||
varTime = putEntry("@time", 0);
|
||||
varTick = putEntry("@tick", 0);
|
||||
varSecond = putEntry("@second", 0);
|
||||
varMinute = putEntry("@minute", 0);
|
||||
varWave = putEntry("@waveNumber", 0);
|
||||
varWaveTime = putEntry("@waveTime", 0);
|
||||
|
||||
varServer = put("@server", 0, true);
|
||||
varClient = put("@client", 0, true);
|
||||
varMapW = putEntry("@mapw", 0);
|
||||
varMapH = putEntry("@maph", 0);
|
||||
|
||||
putEntryOnly("sectionNetwork");
|
||||
|
||||
varServer = putEntry("@server", 0, true);
|
||||
varClient = putEntry("@client", 0, true);
|
||||
|
||||
//privileged desynced client variables
|
||||
varClientLocale = put("@clientLocale", null, true);
|
||||
varClientUnit = put("@clientUnit", null, true);
|
||||
varClientName = put("@clientName", null, true);
|
||||
varClientTeam = put("@clientTeam", 0, true);
|
||||
varClientMobile = put("@clientMobile", 0, true);
|
||||
varClientLocale = putEntry("@clientLocale", null, true);
|
||||
varClientUnit = putEntry("@clientUnit", null, true);
|
||||
varClientName = putEntry("@clientName", null, true);
|
||||
varClientTeam = putEntry("@clientTeam", 0, true);
|
||||
varClientMobile = putEntry("@clientMobile", 0, true);
|
||||
|
||||
//special enums
|
||||
put("@ctrlProcessor", ctrlProcessor);
|
||||
@@ -115,6 +133,8 @@ public class GlobalVars{
|
||||
logicIdToContent = new UnlockableContent[ContentType.all.length][];
|
||||
contentIdToLogicId = new int[ContentType.all.length][];
|
||||
|
||||
putEntryOnly("sectionLookup");
|
||||
|
||||
Fi ids = Core.files.internal("logicids.dat");
|
||||
if(ids.exists()){
|
||||
//read logic ID mapping data (generated in ImagePacker)
|
||||
@@ -125,7 +145,7 @@ public class GlobalVars{
|
||||
contentIdToLogicId[ctype.ordinal()] = new int[Vars.content.getBy(ctype).size];
|
||||
|
||||
//store count constants
|
||||
put("@" + ctype.name() + "Count", amount);
|
||||
putEntry("@" + ctype.name() + "Count", amount);
|
||||
|
||||
for(int i = 0; i < amount; i++){
|
||||
String name = in.readUTF();
|
||||
@@ -159,6 +179,9 @@ public class GlobalVars{
|
||||
vars.items[varWave].numval = state.wave;
|
||||
vars.items[varWaveTime].numval = state.wavetime / 60f;
|
||||
|
||||
vars.items[varMapW].numval = world.width();
|
||||
vars.items[varMapH].numval = world.height();
|
||||
|
||||
//network
|
||||
vars.items[varServer].numval = (net.server() || !net.active()) ? 1 : 0;
|
||||
vars.items[varClient].numval = net.client() ? 1 : 0;
|
||||
@@ -173,6 +196,10 @@ public class GlobalVars{
|
||||
}
|
||||
}
|
||||
|
||||
public Seq<VarEntry> getEntries(){
|
||||
return varEntries;
|
||||
}
|
||||
|
||||
/** @return a piece of content based on its logic ID. This is not equivalent to content ID. */
|
||||
public @Nullable Content lookupContent(ContentType type, int id){
|
||||
var arr = logicIdToContent[type.ordinal()];
|
||||
@@ -209,6 +236,11 @@ public class GlobalVars{
|
||||
|
||||
/** Adds a constant value by name. */
|
||||
public int put(String name, Object value, boolean privileged){
|
||||
return put(name, value, privileged, true);
|
||||
}
|
||||
|
||||
/** Adds a constant value by name. */
|
||||
public int put(String name, Object value, boolean privileged, boolean hidden){
|
||||
int existingIdx = namesToIds.get(name, -1);
|
||||
if(existingIdx != -1){ //don't overwrite existing vars (see #6910)
|
||||
Log.debug("Failed to add global logic variable '@', as it already exists.", name);
|
||||
@@ -228,10 +260,44 @@ public class GlobalVars{
|
||||
namesToIds.put(name, index);
|
||||
if(privileged) privilegedIds.add(index);
|
||||
vars.add(var);
|
||||
|
||||
if(!hidden){
|
||||
varEntries.add(new VarEntry(index, name, "", "", privileged));
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public int put(String name, Object value){
|
||||
return put(name, value, false);
|
||||
}
|
||||
|
||||
public int putEntry(String name, Object value){
|
||||
return put(name, value, false, false);
|
||||
}
|
||||
|
||||
public int putEntry(String name, Object value, boolean privileged){
|
||||
return put(name, value, privileged, false);
|
||||
}
|
||||
|
||||
public void putEntryOnly(String name){
|
||||
varEntries.add(new VarEntry(0, name, "", "", false));
|
||||
}
|
||||
|
||||
/** An entry that describes a variable for documentation purposes. This is *only* used inside UI for global variables. */
|
||||
public static class VarEntry{
|
||||
public int id;
|
||||
public String name, description, icon;
|
||||
public boolean privileged;
|
||||
|
||||
public VarEntry(int id, String name, String description, String icon, boolean privileged){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.icon = icon;
|
||||
this.privileged = privileged;
|
||||
}
|
||||
|
||||
public VarEntry(){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
61
core/src/mindustry/logic/GlobalVarsDialog.java
Normal file
61
core/src/mindustry/logic/GlobalVarsDialog.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.*;
|
||||
import arc.graphics.*;
|
||||
import arc.scene.ui.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.util.*;
|
||||
import mindustry.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.graphics.*;
|
||||
import mindustry.ui.*;
|
||||
import mindustry.ui.dialogs.*;
|
||||
|
||||
public class GlobalVarsDialog extends BaseDialog{
|
||||
|
||||
public GlobalVarsDialog(){
|
||||
super("@logic.globals");
|
||||
|
||||
addCloseButton();
|
||||
shown(this::setup);
|
||||
onResize(this::setup);
|
||||
}
|
||||
|
||||
void setup(){
|
||||
float prefWidth = Math.min(Core.graphics.getWidth() * 0.9f / Scl.scl(1f) - 220f, 600f);
|
||||
cont.clearChildren();
|
||||
|
||||
cont.pane(t -> {
|
||||
t.margin(10f).marginRight(16f);
|
||||
t.defaults().fillX().fillY();
|
||||
for(var entry : Vars.logicVars.getEntries()){
|
||||
|
||||
if(entry.name.startsWith("section")){
|
||||
Color color = Pal.accent;
|
||||
t.add("@lglobal." + entry.name).fillX().center().labelAlign(Align.center).colspan(4).color(color).padTop(4f).padBottom(2f).row();
|
||||
t.image(Tex.whiteui).height(4f).color(color).colspan(4).padBottom(8f).row();
|
||||
}else{
|
||||
Color varColor = Pal.gray;
|
||||
float stub = 8f, mul = 0.5f, pad = 4;
|
||||
|
||||
String desc = entry.description;
|
||||
if(desc == null || desc.isEmpty()){
|
||||
desc = Core.bundle.get("lglobal." + entry.name, "");
|
||||
}
|
||||
|
||||
String fdesc = desc;
|
||||
|
||||
t.add(new Image(Tex.whiteui, varColor.cpy().mul(mul))).width(stub);
|
||||
t.stack(new Image(Tex.whiteui, varColor), new Label(" " + entry.name + " ", Styles.outlineLabel)).padRight(pad);
|
||||
|
||||
t.add(new Image(Tex.whiteui, Pal.gray.cpy().mul(mul))).width(stub);
|
||||
t.table(Tex.pane, out -> out.add(fdesc).style(Styles.outlineLabel).width(prefWidth).padLeft(2).padRight(2).wrap()).padRight(pad);
|
||||
|
||||
t.row();
|
||||
|
||||
t.add().fillX().colspan(4).height(4).row();
|
||||
}
|
||||
}
|
||||
}).grow();
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ public class LogicDialog extends BaseDialog{
|
||||
Cons<String> consumer = s -> {};
|
||||
boolean privileged;
|
||||
@Nullable LExecutor executor;
|
||||
GlobalVarsDialog globalsDialog = new GlobalVarsDialog();
|
||||
|
||||
public LogicDialog(){
|
||||
super("logic");
|
||||
@@ -51,7 +52,7 @@ public class LogicDialog extends BaseDialog{
|
||||
add(buttons).growX().name("canvas");
|
||||
}
|
||||
|
||||
private Color typeColor(Var s, Color color){
|
||||
public static Color typeColor(Var s, Color color){
|
||||
return color.set(
|
||||
!s.isobj ? Pal.place :
|
||||
s.objval == null ? Color.darkGray :
|
||||
@@ -65,7 +66,7 @@ public class LogicDialog extends BaseDialog{
|
||||
);
|
||||
}
|
||||
|
||||
private String typeName(Var s){
|
||||
public static String typeName(Var s){
|
||||
return
|
||||
!s.isobj ? "number" :
|
||||
s.objval == null ? "null" :
|
||||
@@ -178,6 +179,8 @@ public class LogicDialog extends BaseDialog{
|
||||
});
|
||||
|
||||
dialog.addCloseButton();
|
||||
dialog.buttons.button("@logic.globals", Icon.list, () -> globalsDialog.show()).size(210f, 64f);
|
||||
|
||||
dialog.show();
|
||||
}).name("variables").disabled(b -> executor == null || executor.vars.length == 0);
|
||||
|
||||
|
||||
@@ -354,8 +354,6 @@ public class LogicBlock extends Block{
|
||||
}
|
||||
}
|
||||
|
||||
asm.putConst("@mapw", world.width());
|
||||
asm.putConst("@maph", world.height());
|
||||
asm.putConst("@links", executor.links.length);
|
||||
asm.putConst("@ipt", instructionsPerTick);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user