diff --git a/src/MinerTools/MinerVars.java b/src/MinerTools/MinerVars.java index 0971913..8db8e5b 100644 --- a/src/MinerTools/MinerVars.java +++ b/src/MinerTools/MinerVars.java @@ -1,27 +1,28 @@ package MinerTools; +import MinRi2.ModCore.io.*; import MinerTools.input.*; -import MinerTools.io.*; import MinerTools.ui.*; import arc.KeyBinds.*; +import arc.files.*; import arc.util.*; import mindustry.*; import static arc.Core.*; -import static mindustry.Vars.maxSchematicSize; +import static mindustry.Vars.*; public class MinerVars{ public static final String modName = "miner-tools"; public static final String modSymbol = "[yellow][M]"; public static final float worldFontScl = Vars.tilesize / 36f; - public static MSettings settings; + public static MinModSettings settings; public static MUI ui; public static boolean desktop; public static void init(){ - settings = new MSettings(); + settings = MinModSettings.registerSettings(modName); ui = new MUI(); desktop = app.isDesktop(); @@ -35,6 +36,7 @@ public static void init(){ ui.init(); betterSchemeSize(); + migrateOldSettings(); } public static void betterSchemeSize(){ @@ -53,4 +55,15 @@ public static void initBindings(){ Reflect.invoke(keybinds, "load"); Reflect.invoke(Vars.ui.controls, "setup"); } + + private static void migrateOldSettings(){ + Fi old = modDirectory.child("MinerTools").child("settings"); + + if(old.exists()){ + old.copyTo(settings.settingsFi); + settings.load(); + settings.save(); + modDirectory.child("MinerTools").deleteDirectory(); + } + } } diff --git a/src/MinerTools/io/MSettings.java b/src/MinerTools/io/MSettings.java deleted file mode 100644 index 392a080..0000000 --- a/src/MinerTools/io/MSettings.java +++ /dev/null @@ -1,225 +0,0 @@ -package MinerTools.io; - -import arc.files.*; -import arc.struct.*; -import arc.util.*; -import arc.util.io.*; - -import java.io.*; - -import static mindustry.Vars.*; - -public class MSettings{ - protected static final byte typeBool = 0, typeInt = 1, typeLong = 2, typeFloat = 3, typeString = 4; - private static final String settingsName = "settings"; - private static final String backupName = settingsName + ".backup"; - public static boolean debug; - protected Seq mSettings = new Seq<>(); - private Fi root, settings, backup; - private boolean loaded = false; - private boolean modified = false; - - public void init(){ - root = modDirectory.child("MinerTools"); - root.mkdirs(); - - settings = root.child(settingsName); - backup = root.child(backupName); - - try{ - if(!settings.exists()){ - settings.file().createNewFile(); - } - if(!backup.exists()){ - backup.file().createNewFile(); - } - }catch(IOException e){ - Log.err("MinerToolsSettings: Failed to create file: ", e); - } - - load(); - - Timer.schedule(() -> { - if(modified) save(); - }, 0f, 60f * 1); - } - - public MinerSetting findSetting(String name){ - return mSettings.find(s -> s.name.equals(name)); - } - - public void put(String name, Object obj){ - put(name, obj, false, false); - } - - public void put(String name, Object obj, boolean isDef, boolean forceSave){ - MinerSetting ms = findSetting(name); - - if(ms != null){ - if(isDef) return; - ms.value = obj; - }else{ - mSettings.add(new MinerSetting(name, obj)); - } - modified = true; - - if(forceSave) save(); - } - - public T get(String name, T def){ - MinerSetting setting = findSetting(name); - if(setting == null){ - return def; - } - return (T)setting.value; - } - - public boolean getBool(String name){ - return get(name, false); - } - - public boolean getBool(String name, boolean def){ - return get(name, def); - } - - public int getInt(String name){ - return get(name, 0); - } - - public int getInt(String name, int def){ - return get(name, def); - } - - public long getLong(String name){ - return get(name, 0L); - } - - public long getLong(String name, long def){ - return get(name, def); - } - - public float getFloat(String name){ - return get(name, 0f); - } - - public float getFloat(String name, float def){ - return get(name, def); - } - - public String getString(String name){ - return get(name, ""); - } - - public String getString(String name, String def){ - return get(name, def); - } - - private void load(){ - if(settings.file().length() != 0L || backup.file().length() != 0L){ - loadSettings(); - } - loaded = true; - } - - private void loadSettings(){ - try{ - Log.info("&lcMinerToolsSettings: Trying to load settings"); - loadSettings(settings); - Log.info("&lcMinerToolsSettings: Load settings successfully"); - }catch(Throwable e){ - Log.err("MinerToolsSettings: Filed to load settings: ", e); - Log.err("MinerToolsSettings: Trying to load backup: ", e); - try{ - loadSettings(backup); - }catch(IOException ex){ - Log.err("MinerToolsSettings: Filed to load backup: ", ex); - } - } - } - - private void loadSettings(Fi fi) throws IOException{ - mSettings.clear(); - - try(DataInputStream reads = new DataInputStream(fi.read(Streams.defaultBufferSize))){ - int size = reads.readInt(); - for(int i = 0; i < size; i++){ - String name = reads.readUTF(); - - byte type = reads.readByte(); - - Object value; - switch(type){ - case typeBool -> value = reads.readBoolean(); - case typeInt -> value = reads.readInt(); - case typeLong -> value = reads.readLong(); - case typeFloat -> value = reads.readFloat(); - case typeString -> value = reads.readUTF(); - default -> throw new IOException("MinerToolsSettings: Field to load type: " + type); - } - - mSettings.add(new MinerSetting(name, value)); - - if(debug){ - Log.infoTag("MinerToolsSettings", "Read setting " + name + ": " + value); - } - } - }catch(Exception e){ - fi.delete(); - ui.showException(e); - } - } - - private void save(){ - if(!loaded) return; - - if(debug){ - Log.infoTag("MinerToolsSettings", "Saving"); - } - - settings.copyTo(backup); - - var writes = settings.writes(); - - writes.i(mSettings.size); - for(MinerSetting setting : mSettings){ - - Object value = setting.value; - - writes.str(setting.name); - - if(value instanceof Boolean b){ - writes.b(typeBool); - writes.bool(b); - }else if(value instanceof Integer i){ - writes.b(typeInt); - writes.i(i); - }else if(value instanceof Long l){ - writes.b(typeLong); - writes.l(l); - }else if(value instanceof Float f){ - writes.b(typeFloat); - writes.f(f); - }else if(value instanceof String s){ - writes.b(typeString); - writes.str(s); - } - - if(debug){ - Log.infoTag("MinerToolsSettings", "Write setting " + setting.name + ": " + value); - } - } - - writes.close(); - modified = false; - } - - public static class MinerSetting{ - public String name; - public Object value; - - public MinerSetting(String name, Object value){ - this.name = name; - this.value = value; - } - } -} diff --git a/src/MinerTools/modules/SpawnerInfo/SpawnerGroup.java b/src/MinerTools/modules/SpawnerInfo/SpawnerGroup.java index ff41914..5e1e338 100644 --- a/src/MinerTools/modules/SpawnerInfo/SpawnerGroup.java +++ b/src/MinerTools/modules/SpawnerInfo/SpawnerGroup.java @@ -1,6 +1,6 @@ package MinerTools.modules.SpawnerInfo; -import MinerTools.utils.math.*; +import MinRi2.ModCore.math.*; import arc.math.geom.*; import arc.struct.*; diff --git a/src/MinerTools/modules/SpawnerInfo/SpawnerInfo.java b/src/MinerTools/modules/SpawnerInfo/SpawnerInfo.java index 5b00067..117f7d6 100644 --- a/src/MinerTools/modules/SpawnerInfo/SpawnerInfo.java +++ b/src/MinerTools/modules/SpawnerInfo/SpawnerInfo.java @@ -1,7 +1,7 @@ package MinerTools.modules.SpawnerInfo; +import MinRi2.ModCore.utils.*; import MinerTools.modules.*; -import MinerTools.utils.*; import arc.*; import arc.func.*; import arc.math.geom.*; diff --git a/src/MinerTools/ui/MStyles.java b/src/MinerTools/ui/MStyles.java index 3f56907..4fef0ac 100644 --- a/src/MinerTools/ui/MStyles.java +++ b/src/MinerTools/ui/MStyles.java @@ -8,9 +8,9 @@ import arc.scene.ui.ImageButton.*; import arc.scene.ui.TextButton.*; import arc.scene.ui.TextField.*; -import arc.util.*; import mindustry.graphics.*; +import static MinRi2.ModCore.ui.MinTex.*; import static mindustry.gen.Tex.*; import static mindustry.ui.Styles.*; @@ -18,19 +18,12 @@ public class MStyles{ // TODO: 注解处理器生成 public static TextureRegionDrawable accentGrayGran; - public static TextureRegionDrawable whiteuiRegion, transAccent, transRed, clearFlatOver; - public static ImageButtonStyle clearToggleAccentb, logicVarTogglet, chatb, rclearTransi; public static TextButtonStyle clearPartial2t, clearAccentt, toggleTranst, settingt; public static TextFieldStyle noneField; public static void load(){ loadModSprites(); - - whiteuiRegion = (TextureRegionDrawable)whiteui; - transAccent = getColoredRegion(Pal.accent, 0.55f); - transRed = getColoredRegion(Color.red, 0.55f); - clearFlatOver = getColoredRegion(Color.lightGray, 0.45f); clearToggleAccentb = new ImageButtonStyle(){{ over = flatOver; @@ -89,12 +82,4 @@ public static void loadModSprites(){ private static TextureRegion getSprite(String name){ return Core.atlas.find(MinerVars.modName + "-" + name); } - - public static TextureRegionDrawable getColoredRegion(Color color){ - return (TextureRegionDrawable)whiteuiRegion.tint(color); - } - - public static TextureRegionDrawable getColoredRegion(Color color, float alpha){ - return (TextureRegionDrawable)whiteuiRegion.tint(Tmp.c1.set(color).a(alpha)); - } } diff --git a/src/MinerTools/ui/MUI.java b/src/MinerTools/ui/MUI.java index 62de7dc..0f6d9a5 100644 --- a/src/MinerTools/ui/MUI.java +++ b/src/MinerTools/ui/MUI.java @@ -1,26 +1,15 @@ package MinerTools.ui; +import MinRi2.ModCore.ui.*; import MinerTools.interfaces.*; -import MinerTools.ui.override.CoreItemsDisplay; import MinerTools.ui.override.*; import MinerTools.ui.settings.*; -import MinerTools.ui.tables.*; import MinerTools.ui.tables.floats.*; -import arc.func.*; -import arc.math.*; import arc.scene.*; -import arc.scene.actions.*; -import arc.scene.event.*; import arc.scene.ui.*; -import arc.scene.ui.layout.*; import arc.struct.*; -import arc.util.*; -import mindustry.ui.*; import static arc.Core.*; -import static arc.util.Align.center; -import static mindustry.Vars.state; -import static mindustry.ui.Styles.black6; public class MUI{ /* 集中处理鼠标未指向ScrollPane但又占用滑动的情况 */ @@ -49,62 +38,6 @@ public class MUI{ public MUI(){ } - public static void showTableAtMouse(Cons cons){ - showTableAt(input.mouseX(), input.mouseY(), center, cons); - } - - public static void showTableAt(float x, float y, int align, Cons
cons){ - showTableAt(x, y, align, cons, table -> !table.hasMouse()); - } - - public static void showTableAt(float x, float y, int align, Cons
cons, Boolf
hideBoolp){ - Table table = new Table(black6); - scene.add(table); - table.actions(Actions.fadeIn(0.5f, Interp.smooth), Actions.remove()); - - cons.get(table); - table.pack(); - - table.setPosition(x, y, align); - table.keepInStage(); - - table.update(() -> { - if(hideBoolp.get(table)){ - table.actions(Actions.fadeOut(0.5f, Interp.smooth), Actions.remove()); - } - }); - } - - public static void showInfoToast(String info, float duration, int align){ - showInfoToastAt(scene.root.getX(align), scene.root.getY(align), info, duration, center); - } - - public static void showInfoToastAt(float x, float y, String info, float duration, int align){ - Table table = new Table(Styles.black3); - table.touchable = Touchable.disabled; - - table.update(() -> { - if(state.isMenu()) table.remove(); - }); - - table.actions(Actions.fadeIn(0.5f, Interp.smooth), Actions.delay(duration), Actions.fadeOut(0.5f, Interp.smooth), Actions.remove()); - table.add(info).style(Styles.outlineLabel); - - table.pack(); - - table.setPosition(x, y, align); - table.keepInStage(); - scene.add(table); - } - - public static void setClipboardText(String text){ - /* Do not copy the empty text */ - if(!text.equals("")){ - app.setClipboardText(text); - showInfoToast("Copy: " + text, 3f, Align.bottom); - } - } - public void init(){ MStyles.load(); diff --git a/src/MinerTools/ui/override/BetterInfoTable.java b/src/MinerTools/ui/override/BetterInfoTable.java index 7d02016..74c968b 100644 --- a/src/MinerTools/ui/override/BetterInfoTable.java +++ b/src/MinerTools/ui/override/BetterInfoTable.java @@ -1,9 +1,9 @@ package MinerTools.ui.override; +import MinRi2.ModCore.ui.*; +import MinRi2.ModCore.utils.*; import MinerTools.*; import MinerTools.interfaces.*; -import MinerTools.utils.*; -import MinerTools.utils.ui.*; import arc.*; import arc.func.*; import arc.graphics.*; diff --git a/src/MinerTools/ui/settings/BaseSetting.java b/src/MinerTools/ui/settings/BaseSetting.java index 52f8e18..fca8cf7 100644 --- a/src/MinerTools/ui/settings/BaseSetting.java +++ b/src/MinerTools/ui/settings/BaseSetting.java @@ -1,8 +1,8 @@ package MinerTools.ui.settings; +import MinRi2.ModCore.ui.*; import MinerTools.*; import MinerTools.ui.*; -import MinerTools.utils.ui.*; import arc.func.*; import arc.graphics.*; import arc.scene.*; diff --git a/src/MinerTools/ui/settings/MSettingsMenu.java b/src/MinerTools/ui/settings/MSettingsMenu.java index 84553de..c472117 100644 --- a/src/MinerTools/ui/settings/MSettingsMenu.java +++ b/src/MinerTools/ui/settings/MSettingsMenu.java @@ -1,10 +1,10 @@ package MinerTools.ui.settings; +import MinRi2.ModCore.ui.*; import MinerTools.*; import MinerTools.graphics.*; import MinerTools.ui.*; import MinerTools.ui.tables.*; -import MinerTools.utils.ui.*; import arc.flabel.*; import arc.scene.actions.*; import arc.scene.ui.layout.*; diff --git a/src/MinerTools/ui/tables/Addable.java b/src/MinerTools/ui/tables/Addable.java deleted file mode 100644 index 8f54e07..0000000 --- a/src/MinerTools/ui/tables/Addable.java +++ /dev/null @@ -1,5 +0,0 @@ -package MinerTools.ui.tables; - -public interface Addable{ - void addUI(); -} diff --git a/src/MinerTools/ui/tables/floats/ChatTable.java b/src/MinerTools/ui/tables/floats/ChatTable.java index 518dec9..8f8e931 100644 --- a/src/MinerTools/ui/tables/floats/ChatTable.java +++ b/src/MinerTools/ui/tables/floats/ChatTable.java @@ -1,5 +1,6 @@ package MinerTools.ui.tables.floats; +import MinRi2.ModCore.ui.*; import MinerTools.ui.*; import MinerTools.ui.settings.*; import arc.*; @@ -21,8 +22,8 @@ import java.text.*; import java.util.*; +import static MinRi2.ModCore.ui.UIUtils.setClipboardText; import static MinerTools.MinerVars.desktop; -import static MinerTools.ui.MUI.setClipboardText; import static arc.Core.*; import static mindustry.Vars.*; import static mindustry.ui.Styles.*; @@ -150,7 +151,7 @@ private void rebuildDialog(){ table.row(); - table.table(MStyles.clearFlatOver, messages -> { + table.table(MinTex.clearFlatOver, messages -> { for(String msg : messageStack.messages){ messages.button(msg, MStyles.toggleTranst, () -> addSelectMessage(msg)) .checked(b -> selectMessages.contains(msg, true)).growX().left().padTop(2f) @@ -174,7 +175,7 @@ private void rebuildDialog(){ t.row(); - t.table(MStyles.clearFlatOver, s -> { + t.table(MinTex.clearFlatOver, s -> { }).update(selectsTable -> { selectsTable.clearChildren(); @@ -216,7 +217,7 @@ private void resetMessages(){ } private void sendMessage(){ - if(!textField.getText().equals("")){ + if(!textField.getText().isEmpty()){ history.insert(0, textField.getText()); historyIndex = -1; diff --git a/src/MinerTools/ui/tables/floats/FloatTable.java b/src/MinerTools/ui/tables/floats/FloatTable.java index 8f9c600..48a759b 100644 --- a/src/MinerTools/ui/tables/floats/FloatTable.java +++ b/src/MinerTools/ui/tables/floats/FloatTable.java @@ -1,11 +1,10 @@ package MinerTools.ui.tables.floats; +import MinRi2.ModCore.ui.*; +import MinRi2.ModCore.ui.operator.*; import MinerTools.*; import MinerTools.ui.*; import MinerTools.ui.settings.*; -import MinerTools.ui.tables.*; -import MinerTools.utils.ui.*; -import MinerTools.utils.ui.operator.*; import arc.*; import arc.math.*; import arc.math.geom.*; @@ -25,7 +24,6 @@ public class FloatTable extends SavedTable implements Addable{ protected Table title, bodyCont, body; protected boolean isSetup; - public FloatTable(String name){ this(name, true); } @@ -35,7 +33,7 @@ public FloatTable(String name, boolean removable){ } public FloatTable(String name, boolean hasSetting, boolean removable){ - super(name, true, true); + super(MinerVars.settings, name, true, true); if(name == null){ throw new RuntimeException("FloatTable must have a name."); @@ -73,6 +71,8 @@ public final void addUI(){ keepInStage(); Vars.ui.hudGroup.addChild(this); + toFront(); + ResizeAdjuster.add(this); FloatManager.add(this); } @@ -131,7 +131,7 @@ protected void setupTitle(){ buttons.button(Icon.editSmall, MStyles.clearToggleAccentb, () -> { operate(); - MUI.showInfoToastAt(getX(Align.center), getTop(), "@miner-tools.operator.show-hint", 1f, Align.bottom); + UIUtils.showInfoToastAt(getX(Align.center), getTop(), "@miner-tools.operator.show-hint", 1f, Align.bottom); }).checked(b -> operating()).disabled(b -> !operable()); RotatedImage image = new RotatedImage(Icon.downSmall, 180); @@ -164,7 +164,7 @@ protected void rebuildBody(Table body){ protected void removeManually(){ if(hasSetting){ MinerVars.settings.put("floats." + name + ".shown", false); - MUI.showInfoToastAt(getX(Align.center), getTop() + 8, "@miner-tools.floats.reshow-hint", 2, Align.bottom); + UIUtils.showInfoToastAt(getX(Align.center), getTop() + 8, "@miner-tools.floats.reshow-hint", 2, Align.bottom); } remove(); } diff --git a/src/MinerTools/ui/tables/floats/MainTable.java b/src/MinerTools/ui/tables/floats/MainTable.java index 2658097..ab083f3 100644 --- a/src/MinerTools/ui/tables/floats/MainTable.java +++ b/src/MinerTools/ui/tables/floats/MainTable.java @@ -1,5 +1,6 @@ package MinerTools.ui.tables.floats; +import MinRi2.ModCore.ui.*; import MinerTools.*; import MinerTools.ui.*; import arc.scene.ui.layout.*; @@ -10,7 +11,7 @@ public class MainTable extends FloatTable{ public MainTable(){ super("main", false, false); - title.background(MStyles.transAccent); + title.background(MinTex.transAccent); } @Override diff --git a/src/MinerTools/ui/tables/floats/TemporaryFloatTable.java b/src/MinerTools/ui/tables/floats/TemporaryFloatTable.java index 9cce94f..5f97971 100644 --- a/src/MinerTools/ui/tables/floats/TemporaryFloatTable.java +++ b/src/MinerTools/ui/tables/floats/TemporaryFloatTable.java @@ -1,6 +1,6 @@ package MinerTools.ui.tables.floats; -import MinerTools.utils.ui.*; +import MinRi2.ModCore.ui.*; import arc.scene.*; import arc.scene.ui.layout.*; diff --git a/src/MinerTools/ui/tables/members/PlayerList.java b/src/MinerTools/ui/tables/members/PlayerList.java index 078ff67..07554c0 100644 --- a/src/MinerTools/ui/tables/members/PlayerList.java +++ b/src/MinerTools/ui/tables/members/PlayerList.java @@ -1,5 +1,6 @@ package MinerTools.ui.tables.members; +import MinRi2.ModCore.ui.*; import MinerTools.ui.*; import MinerTools.ui.tables.MembersTable.*; import MinerTools.utils.*; @@ -70,7 +71,7 @@ private void rebuild(){ .get().clicked(() -> panToPlayer(player)); info.labelWrap(player.coloredName()).width(200).pad(7) - .get().clicked(() -> MUI.setClipboardText(player.coloredName())); + .get().clicked(() -> UIUtils.setClipboardText(player.coloredName())); info.add().width(-1f).grow(); @@ -80,7 +81,7 @@ private void rebuild(){ return target == player; }); - info.button(Icon.list, clearNonei, () -> MUI.showTableAtMouse(table -> { + info.button(Icon.list, clearNonei, () -> UIUtils.showTableAtMouse(table -> { table.background(Tex.buttonOver); table.button(Icon.eyeSmall, clearNonei, () -> { diff --git a/src/MinerTools/ui/tables/members/TeamsInfo.java b/src/MinerTools/ui/tables/members/TeamsInfo.java index e4e0dc3..8b1d8d4 100644 --- a/src/MinerTools/ui/tables/members/TeamsInfo.java +++ b/src/MinerTools/ui/tables/members/TeamsInfo.java @@ -1,11 +1,11 @@ package MinerTools.ui.tables.members; +import MinRi2.ModCore.ui.*; import MinerTools.game.*; import MinerTools.ui.*; import MinerTools.ui.tables.MembersTable.*; import MinerTools.ui.tables.floats.*; import MinerTools.utils.*; -import MinerTools.utils.ui.*; import arc.*; import arc.graphics.*; import arc.scene.ui.layout.*; @@ -196,7 +196,7 @@ public static PowerInfoTable get(Team team, PowerInfo info){ protected void setupTitle(){ super.setupTitle(); - title.background(MStyles.getColoredRegion(team.color, 0.6f)); + title.background(MinTex.getColoredRegion(team.color, 0.6f)); } @Override diff --git a/src/MinerTools/utils/AlignUtils.java b/src/MinerTools/utils/AlignUtils.java deleted file mode 100644 index ba1b0fa..0000000 --- a/src/MinerTools/utils/AlignUtils.java +++ /dev/null @@ -1,42 +0,0 @@ -package MinerTools.utils; - -import arc.util.*; - -public class AlignUtils{ - - /** - * 反转方向 例如: bottomLeft -> topRight - */ - public static int flip(int align){ - return flipX(flipY(align)); - } - - /** - * 反转横向 例如: bottomLeft -> bottomRight - */ - public static int flipX(int align){ - if(Align.isLeft(align)){ - align &= ~Align.left; - align |= Align.right; - }else if(Align.isRight(align)){ - align &= ~Align.right; - align |= Align.left; - } - return align; - } - - /** - * 反转纵向 例如: bottomLeft -> topLeft - */ - public static int flipY(int align){ - if(Align.isTop(align)){ - align &= ~Align.top; - align |= Align.bottom; - }else if(Align.isBottom(align)){ - align &= ~Align.bottom; - align |= Align.top; - } - - return align; - } -} diff --git a/src/MinerTools/utils/DebounceTask.java b/src/MinerTools/utils/DebounceTask.java deleted file mode 100644 index 460f49f..0000000 --- a/src/MinerTools/utils/DebounceTask.java +++ /dev/null @@ -1,35 +0,0 @@ -package MinerTools.utils; - -import arc.util.*; -import arc.util.Timer.*; - -/** - * 函数防抖 - * 一段时间后执行,若期间再次执行,重置倒计时 - * @author minri2 - */ -public class DebounceTask{ - private final float delay; - private final Runnable runnable; - private final Timer timer; - - public DebounceTask(float delay, Runnable runnable){ - this.timer = new Timer(); - - this.delay = delay; - this.runnable = runnable; - } - - public void run(){ - if(!timer.isEmpty()){ - timer.clear(); - } - - timer.scheduleTask(new Task(){ - @Override - public void run(){ - runnable.run(); - } - }, delay); - } -} diff --git a/src/MinerTools/utils/ReflectUtils.java b/src/MinerTools/utils/ReflectUtils.java deleted file mode 100644 index 23f1160..0000000 --- a/src/MinerTools/utils/ReflectUtils.java +++ /dev/null @@ -1,69 +0,0 @@ -package MinerTools.utils; - -import arc.util.*; - -import java.lang.reflect.*; - -public class ReflectUtils{ - public static Field getField(Class clazz, String name){ - try{ - Field field = clazz.getDeclaredField(name); - field.setAccessible(true); - return field; - }catch(Exception e){ - throw new RuntimeException(e); - } - } - - public static Field getField(Object object, String name){ - try{ - Field field = object.getClass().getDeclaredField(name); - field.setAccessible(true); - return field; - }catch(Exception e){ - throw new RuntimeException(e); - } - } - - public static Method getMethod(Class clazz, String name, Class... parameterTypes){ - try{ - Method method = clazz.getDeclaredMethod(name, parameterTypes); - method.setAccessible(true); - return method; - }catch(Exception e){ - throw new RuntimeException(e); - } - } - - public static T setValue(Field field, Object object, T value){ - try{ - field.set(object, value); - return value; - }catch(Exception e){ - throw new RuntimeException(e); - } - } - - public static T getValue(Field field, Object object){ - try{ - return (T)field.get(object); - }catch(Exception e){ - throw new RuntimeException(e); - } - } - - public static @Nullable T invokeMethod(Object object, Method method, Object... args){ - try{ - Object result = method.invoke(object, args); - - if(method.getReturnType() == void.class){ - return null; - } - - return (T)result; - }catch(Exception e){ - throw new RuntimeException(e); - } - } - -} diff --git a/src/MinerTools/utils/math/GeometryUtils.java b/src/MinerTools/utils/math/GeometryUtils.java deleted file mode 100644 index c302115..0000000 --- a/src/MinerTools/utils/math/GeometryUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -package MinerTools.utils.math; - -import arc.math.*; -import arc.math.geom.*; -import arc.util.*; - -public class GeometryUtils{ - /** - * 返回矩形(rect)在给定方位是否与矩形(other)有公共边 - * @param align 判断方位 - * @return 有的话返回true - */ - public static boolean hasCommonEdge(Rect rect, Rect other, int align){ - if(rect.overlaps(other)){ - return false; - } - - float left = rect.x, right = left + rect.width; - float bottom = rect.y, top = bottom + rect.height; - - float otherLeft = other.x, otherRight = otherLeft + other.width; - float otherBottom = other.y, otherTop = otherBottom + other.height; - - if(Align.isRight(align) || Align.isLeft(align)){ - float ex = right; - float oex = otherLeft; - - if(Align.isLeft(align)){ - ex = left; - oex = otherRight; - } - - return (rect.height > other.height - ? ((otherBottom >= bottom && otherBottom <= top) || (otherTop >= bottom && otherTop <= top)) - : ((bottom >= otherBottom && bottom <= otherTop) || (top >= otherBottom && top <= otherTop))) - && Mathf.equal(ex, oex); - } - - if(Align.isTop(align) || Align.isBottom(align)){ - float ey = top; - float oey = otherBottom; - - if(Align.isBottom(align)){ - ey = bottom; - oey = otherTop; - } - - return (rect.width > other.width - ? ((otherLeft >= left && otherLeft <= right) || (otherRight >= left && otherRight <= right)) - : ((left >= otherLeft && left <= otherRight) || (right >= otherLeft && right <= otherRight))) - && Mathf.equal(ey, oey); - } - - return false; - } -} diff --git a/src/MinerTools/utils/math/Mathu.java b/src/MinerTools/utils/math/Mathu.java deleted file mode 100644 index 95ec6e3..0000000 --- a/src/MinerTools/utils/math/Mathu.java +++ /dev/null @@ -1,26 +0,0 @@ -package MinerTools.utils.math; - -import arc.math.geom.*; -import arc.util.*; - -public class Mathu{ - - public static Vec2 getCentroid(float[] points, Vec2 out){ - int size = (points.length - 1) / 2; - if(size == 0){ - return out.set(points[0], points[1]); - }else if(size == 1){ - Vec2 v1 = Tmp.v1.set(points[0], points[1]); - Vec2 v2 = Tmp.v2.set(points[2], points[3]); - - return out.set(v1).mulAdd(v2.sub(v1), 0.5f); - }else{ - points[0] += 1f; - points[1] += 1.5f; - points[2] += 1.5f; - points[3] += 1f; - return Geometry.polygonCentroid(points, 0, points.length, out); - } - } - -} diff --git a/src/MinerTools/utils/ui/BorderSnapper.java b/src/MinerTools/utils/ui/BorderSnapper.java deleted file mode 100644 index a4d7edc..0000000 --- a/src/MinerTools/utils/ui/BorderSnapper.java +++ /dev/null @@ -1,103 +0,0 @@ -package MinerTools.utils.ui; - -import MinerTools.utils.math.*; -import arc.math.geom.*; -import arc.scene.*; -import arc.scene.actions.*; -import arc.util.*; - -public class BorderSnapper{ - // 目标元素 - private final Element targetElem; - - // 吸附的元素 - public Element snapElem; - - public boolean pause; - - // 目标元素相对于吸附元素的原点坐标 - private float relativeX, relativeY; - // 记录吸附元素的坐标,用于判断坐标是否改变 - private float lastX, lastY; - - public BorderSnapper(Element targetElem){ - this.targetElem = targetElem; - - // 植入Action - targetElem.addAction(Actions.forever(Actions.run(this::updateSnap))); - } - - public void cancelSnapping(){ - snapElem = null; - } - - public boolean setSnap(Element snapElem, int snapAlign){ - if(canSnap(snapElem, snapAlign)){ - this.snapElem = snapElem; - - Vec2 pos = Tmp.v1.setZero(); - ElementUtils.localToTargetCoordinate(targetElem, snapElem.parent, pos); - - relativeX = snapElem.x - pos.x; - relativeY = snapElem.y - pos.y; - - return true; - } - - return false; - } - - public void resume(){ - pause = false; - } - - public void pause(){ - pause = true; - } - - private void updateSnap(){ - if(pause || snapElem == null) return; - - if(!snapElem.hasParent()) return; - - if(updateSnapChanged()){ - float sx = snapElem.x; - float sy = snapElem.y; - - targetElem.setPosition(sx - relativeX, sy - relativeY); - targetElem.keepInStage(); - } - } - - private boolean updateSnapChanged(){ - float sx = snapElem.x; - float sy = snapElem.y; - - if(sx != lastX || sy != lastY){ - lastX = sx; - lastY = sy; - - return true; - } - - lastX = sx; - lastY = sy; - - return false; - } - - private boolean canSnap(Element snapElem, int snapAlign){ - if(snapElem == null || snapAlign == 0){ - return false; - } - - if(!snapElem.hasParent()){ - return false; - } - - Rect targetBounds = ElementUtils.getBoundsOnScene(targetElem, Tmp.r1); - Rect snapBounds = ElementUtils.getBoundsOnScene(snapElem, Tmp.r2); - return GeometryUtils.hasCommonEdge(targetBounds, snapBounds, snapAlign); - } - -} diff --git a/src/MinerTools/utils/ui/ElementUtils.java b/src/MinerTools/utils/ui/ElementUtils.java deleted file mode 100644 index 681e035..0000000 --- a/src/MinerTools/utils/ui/ElementUtils.java +++ /dev/null @@ -1,206 +0,0 @@ -package MinerTools.utils.ui; - -import MinerTools.ui.*; -import arc.func.*; -import arc.graphics.*; -import arc.math.geom.*; -import arc.scene.*; -import arc.scene.event.*; -import arc.scene.ui.*; -import arc.scene.ui.layout.*; -import arc.util.*; -import arc.util.pooling.*; -import mindustry.ui.*; - -import static arc.Core.bundle; - -public class ElementUtils{ - - public static Cell getCell(Element element){ - Group parent = element.parent; - - if(parent instanceof Table table){ - return table.getCell(element); - } - - return null; - } - - public static void addTooltip(Element element, String text, boolean allowMobile){ - addTooltip(element, text, Align.top, allowMobile); - } - - public static void addTooltip(Element element, String text, int align, boolean allowMobile){ - addTooltip(element, t -> { - t.background(Styles.black8).margin(4f); - t.add(text).style(Styles.outlineLabel); - }, align, allowMobile); - } - - public static void addTooltip(Element element, Prov prov, boolean allowMobile){ - addTooltip(element, Align.top, prov, allowMobile); - } - - public static void addTooltip(Element element, int align, Prov prov, boolean allowMobile){ - addTooltip(element, t -> { - t.background(Styles.black8).margin(4f); - t.label(prov).style(Styles.outlineLabel); - }, align, allowMobile); - } - - public static void addTooltip(Element element, Cons
cons, boolean allowMobile){ - addTooltip(element, cons, Align.top, allowMobile); - } - - /** - * 为ui元素添加提示 - * @param element 需要添加提示的元素 - * @param cons 自定义的信息编辑 - * @param align 对齐位置 - * @param allowMobile 是否需要手机提示 - */ - public static void addTooltip(Element element, Cons
cons, int align, boolean allowMobile){ - var tip = new Tooltip(cons){ - { - targetActor = element; - - container.update(() -> { - if(!targetActor.hasMouse()){ - hide(); - } - }); - } - - @Override - public void exit(InputEvent event, float x, float y, int pointer, Element toActor){ - } - - @Override - protected void setContainerPosition(Element element, float x, float y){ - Vec2 pos = element.localToStageCoordinates(Tmp.v1.set(0, 0)); - - container.pack(); - container.setPosition(pos.x, pos.y, align); - container.setOrigin(0, element.getHeight()); - } - }; - tip.allowMobile = allowMobile; - - element.addListener(tip); - } - - public static void addIntroductionFor(Group group, String bundleName, boolean allowMobile){ - for(Element child : group.getChildren()){ - /* add some tooltips */ - if(child.name != null){ - addTooltip(child, bundle.get(bundleName + "." + child.name), allowMobile); - } - } - } - - /** - * hit但是无视是否可点击 - */ - public static Element hit(Group group, float x, float y){ - Vec2 point = Tmp.v1; - Element[] childrenArray = group.getChildren().items; - for(int i = group.getChildren().size - 1; i >= 0; i--){ - Element child = childrenArray[i]; - if(!child.visible) continue; - - child.parentToLocalCoordinates(point.set(x, y)); - - Element hit; - - if(child instanceof Group g){ - hit = hit(g, point.x, point.y); - }else{ - hit = hit(child, point.x, point.y); - } - - if(hit != null) return hit; - } - return null; - } - - - /** - * hit但是无视是否可点击 - */ - public static Element hit(Element e, float x, float y){ - return isOverlays(e, x, y) ? e : null; - } - - /** - * 仅判断点击是否落在元素内部 - */ - public static boolean isOverlays(Element e, float x, float y){ - return x >= e.translation.x && x < e.getWidth() + e.translation.x && y >= e.translation.y && y < e.getHeight() + e.translation.y; - } - - /** - * 添加标题 - * @param table 添加标题的表 - * @param title 添加的标题内容 - * @param color 背景颜色1 - */ - public static void addTitle(Table table, String title, Color color){ - table.table(MStyles.getColoredRegion(color), t -> { - t.add(title).style(Styles.outlineLabel); - }).margin(8f).growX(); - table.row(); - } - - public static Rect getBounds(Element element, Rect out){ - return out.set(element.x, element.y, element.getWidth(), element.getHeight()); - } - - public static Rect getBoundsOnScene(Element element, Rect out){ - Vec2 v = Pools.obtain(Vec2.class, Vec2::new); - element.localToStageCoordinates(v.set(0, 0)); - - out.set(v.x, v.y, element.getWidth(), element.getHeight()); - - v.setZero(); - Pools.free(v); - - return out; - } - - /** - * 获取元素原点(左下角)在Scene坐标系下的坐标 - * @param element 获取的元素 - * @param out 输出坐标 - * @return 返回输出坐标 - */ - public static Vec2 getOriginOnScene(Element element, Vec2 out){ - return out.set(element.localToStageCoordinates(Tmp.v1.set(0, 0))); - } - - /** - * 将元素坐标系转换到目标坐标系 - * @param local 当前坐标系 - * @param target 目标坐标系 - * @param pos 转换点/输出点 - */ - public static void localToTargetCoordinate(Element local, Element target, Vec2 pos){ - local.localToStageCoordinates(pos); - target.stageToLocalCoordinates(pos); - } - - /** - * 将元素的某个方位与目标元素的某个方位对齐 - * @param element 设置位置的元素 - * @param target 目标元素 - * @param align 元素对齐方位 - */ - public static void alignTo(Element element, Element target, int align, int alignTarget){ - Vec2 v = Pools.obtain(Vec2.class, Vec2::new); - v.set(target.getX(alignTarget), target.getY(alignTarget)); // 目标元素在目标方位的坐标 - - localToTargetCoordinate(target.parent, element.parent, v); // 将目标方位转换成元素的父坐标系 - - element.setPosition(v.x, v.y, align); // 对齐 - } - -} diff --git a/src/MinerTools/utils/ui/ResizeAdjuster.java b/src/MinerTools/utils/ui/ResizeAdjuster.java deleted file mode 100644 index d168fa7..0000000 --- a/src/MinerTools/utils/ui/ResizeAdjuster.java +++ /dev/null @@ -1,46 +0,0 @@ -package MinerTools.utils.ui; - -import arc.*; -import arc.scene.*; -import arc.struct.*; -import mindustry.game.EventType.*; - -/** - * 当窗口重设大小时 按比例变动元素位置 - */ -public class ResizeAdjuster{ - private static final ObjectSet elements = new ObjectSet<>(); - private static float lastSceneWidth, lastSceneHeight; - - static{ - lastSceneWidth = Core.scene.getWidth(); - lastSceneHeight = Core.scene.getHeight(); - - Events.on(ResizeEvent.class, e -> { - adjust(); - }); - } - - public static void add(Element element){ - elements.add(element); - } - - private static void adjust(){ - float sceneWidth = Core.scene.getWidth(); - float sceneHeight = Core.scene.getHeight(); - - float resizeScaleX = sceneWidth / lastSceneWidth; - float resizeScaleY = sceneHeight / lastSceneHeight; - - for(Element element : elements){ - float x = element.x; - float y = element.y; - - element.setPosition(x * resizeScaleX, y * resizeScaleY); - element.keepInStage(); - } - - lastSceneWidth = sceneWidth; - lastSceneHeight = sceneHeight; - } -} diff --git a/src/MinerTools/utils/ui/operator/ElementOperator.java b/src/MinerTools/utils/ui/operator/ElementOperator.java deleted file mode 100644 index 6e5c5e9..0000000 --- a/src/MinerTools/utils/ui/operator/ElementOperator.java +++ /dev/null @@ -1,745 +0,0 @@ -package MinerTools.utils.ui.operator; - -import MinerTools.ui.*; -import MinerTools.utils.ui.*; -import arc.*; -import arc.graphics.*; -import arc.graphics.g2d.*; -import arc.input.*; -import arc.math.*; -import arc.math.geom.*; -import arc.scene.*; -import arc.scene.event.*; -import arc.scene.style.*; -import arc.scene.ui.layout.*; -import arc.struct.*; -import arc.util.*; -import arc.util.pooling.*; -import mindustry.*; -import mindustry.game.EventType.*; -import mindustry.gen.*; -import mindustry.graphics.*; -import mindustry.ui.*; - -/** - * 元素操作 - * 可以对元素进行的大小缩放和位置拖拽的缩放 - * @author minri2 - */ -public class ElementOperator{ - // 可操作的表 - static final Seq operableTables = new Seq<>(); - // 原版可对齐的元素 - static final Seq vanillaElements = new Seq<>(); - - // 当前对齐的线,用于绘制 (Scene坐标系) - private static final ObjectSet verticalLines = new ObjectSet<>(); - private static final ObjectSet horizontalLines = new ObjectSet<>(); - - // 大小边框所占比例(中间是拖拽) - private static final float resizeBorderRatio = 2f / 10f; - // 距离小于此值时对齐 - private static final float alignBorder = 4f; - - public static boolean operating = false; - public static boolean dragMode, resizeMode; - // 操作目标元素 - private static Element target; - private static PuppetElement puppet; - private static Element hitter; - private static @Nullable OperateCons consumer; - private static OperatorBackground background; - private static boolean initialized; - private static int touchEdge; - - static{ - Events.on(ResetEvent.class, e -> Core.app.post(ElementOperator::getVanillaElements)); - } - - private ElementOperator(){ - } - - /** - * 获取原版可对齐的元素 - */ - private static void getVanillaElements(){ - vanillaElements.clear(); - - Group hudGroup = Vars.ui.hudGroup; - - Element mainStack = Reflect.get(Vars.ui.hudfrag.blockfrag, "mainStack"), - wavesTable = hudGroup.find("waves"), - minimap = hudGroup.find("minimap"), - position = hudGroup.find("position"), - coreInfo = hudGroup.find("coreinfo"); - - vanillaElements.addAll(mainStack, wavesTable, minimap, position, coreInfo); - } - - private static void clearAlignLines(){ - verticalLines.clear(); - horizontalLines.clear(); - } - - private static void init(){ - background = new OperatorBackground(); - puppet = new OperatorPuppet(); - hitter = new Element(); - - background.addChild(hitter); - background.addChild(puppet); - - puppet.addListener(new InputListener(){ - float startX, startY; // 开始触碰元素的坐标(元素坐标系) - float lastWidth, lastHeight; - - @Override - public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode button){ - startX = x; - startY = y; - - lastWidth = puppet.getWidth(); - lastHeight = puppet.getHeight(); - - updateEdge(x, y); - - clearAlignLines(); - - return true; - } - - @Override - public void touchDragged(InputEvent event, float dragX, float dragY, int pointer){ - float deltaX = dragX - startX; - float deltaY = dragY - startY; - - clearAlignLines(); - - if(dragMode){ - updateDragMode(deltaX, deltaY); - }else if(resizeMode){ - updateResizeMode(deltaX, deltaY, lastWidth, lastHeight); - } - } - - @Override - public void touchUp(InputEvent event, float x, float y, int pointer, KeyCode button){ - clearAlignLines(); - } - }); - - hitter.setFillParent(true); - hitter.addListener(new InputListener(){ - @Override - public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode button){ - Vec2 v = Pools.obtain(Vec2.class, Vec2::new); - Vec2 scenePos = hitter.localToStageCoordinates(v.set(x, y)); - - OperableTable operableTable = operableTables.find(o -> { - if(!o.visible || !o.hasParent()){ - return false; - } - - Vec2 pos = o.stageToLocalCoordinates(Tmp.v2.set(scenePos)); - return ElementUtils.isOverlays(o, pos.x, pos.y); - }); - - if(operableTable != null && operableTable != target){ - operableTable.operate(); - }else if(Core.scene.hit(scenePos.x, scenePos.y, true) == hitter){ - // Scene会将触摸焦点转移到背景上, 下一帧再隐藏 - Core.app.post(ElementOperator::hide); - } - - v.setZero(); - Pools.free(v); - - return true; - } - }); - - initialized = true; - } - - /** - * 操作元素 - * @param element 操作的元素 - */ - public static void operate(Element element){ - operate(element, null); - } - - /** - * 操作元素 - * @param element 操作的元素 - * @param operateCons 操作传感 - */ - public static void operate(Element element, @Nullable OperateCons operateCons){ - - if(!initialized){ - init(); - } - - if(!operable(element)){ - return; // throw an error? - } - - if(target != null){ - consumer.onReleased(); - } - - target = element; - consumer = operateCons; - - puppet.setTarget(target); - - clearAlignLines(); - - show(); - } - - public static boolean operating(Element element){ - return target == element; - } - - public static boolean operable(Element element){ - return element != null && element.visible && element.hasParent(); - } - - private static boolean alizable(Element element){ - return element != null && element.visible - && element.hasParent() && element != target - && (consumer == null || consumer.alizable(element)); - } - - private static void show(){ - if(operating){ - return; - } - - operating = true; - background.show(); - } - - private static void hide(){ - if(!operating){ - return; - } - - target = null; - operating = false; - background.hide(); - } - - // x, y: 元素坐标 - private static void updateEdge(float x, float y){ - float width = puppet.getWidth(), height = puppet.getHeight(); - float borderX = width * resizeBorderRatio, borderY = height * resizeBorderRatio; - - dragMode = resizeMode = false; - touchEdge = 0; - - if(x >= borderX && x <= width - borderX - && y >= borderY && y <= height - borderY){ - dragMode = true; - touchEdge = Align.center; - return; - } - - resizeMode = true; - - if(x < borderX){ - touchEdge |= Align.left; - }else if(x > width - borderX){ - touchEdge |= Align.right; - } - - if(y < borderY){ - touchEdge |= Align.bottom; - }else if(y > height - borderY){ - touchEdge |= Align.top; - } - } - - private static void updateDragMode(float deltaX, float deltaY){ - puppet.moveBy(deltaX, deltaY); - - if(consumer != null){ - if(consumer.keepWithinStage()){ - puppet.keepInStage(); - } - - consumer.onDragged(deltaX, deltaY); - } - - updateDragAlign(); - } - - private static void updateResizeMode(float deltaX, float deltaY, float lastWidth, float lastHeight){ - boolean keepInStage = consumer != null && consumer.keepWithinStage(); - - float width = puppet.getWidth(); - float height = puppet.getHeight(); - - float minWidth = puppet.getMinWidth(); - float minHeight = puppet.getMinHeight(); - - float maxWidth = puppet.getScene().getWidth(); - float maxHeight = puppet.getScene().getHeight(); - - float deltaWidth = width - lastWidth; - float deltaHeight = height - lastHeight; - - // 木偶元素坐标与Scene坐标系下的相同 - float x = puppet.x; - float y = puppet.y; - - if(Align.isLeft(touchEdge)){ - if(width - deltaX < minWidth) deltaX = -(minWidth - width); - if(keepInStage && x + deltaX < 0) deltaX = -x; - width -= deltaX; - x += deltaX; - } - if(Align.isBottom(touchEdge)){ - if(height - deltaY < minHeight) deltaY = -(minHeight - height); - if(keepInStage && y + deltaY < 0) deltaY = -y; - height -= deltaY; - y += deltaY; - } - if(Align.isRight(touchEdge)){ - deltaX -= deltaWidth; // 消除宽增的坐标影响 - if(width + deltaX < minWidth) deltaX = minWidth - width; - if(keepInStage && x + width + deltaX > maxWidth) - deltaX = maxWidth - x - width; - width += deltaX; - } - if(Align.isTop(touchEdge)){ - deltaY -= deltaHeight; // 消除高增的坐标影响 - if(height + deltaY < minHeight) deltaY = minHeight - height; - if(keepInStage && y + height + deltaY > maxHeight) - deltaY = maxHeight - y - height; - height += deltaY; - } - - puppet.setBounds(x, y, width, height); - - target.invalidateHierarchy(); - - if(consumer != null){ - consumer.onResized(deltaX, deltaY); - } - - updateResizeAlign(); - } - - private static void updateDragAlign(){ - for(Element element : vanillaElements){ - if(alizable(element)){ - updateDragAlign(element); - } - } - - for(OperableTable table : operableTables){ - if(alizable(table)){ - updateDragAlign(table); - } - } - } - - private static void updateDragAlign(Element element){ - // 木偶元素坐标与Scene坐标系下的相同 - float x = puppet.x, y = puppet.y; - float w = puppet.getWidth(), h = puppet.getHeight(); - - // 对齐元素坐标不一定等于Scene坐标系下的,先做转换 - Vec2 v = Pools.obtain(Vec2.class, Vec2::new); - ElementUtils.getOriginOnScene(element, v); - float ex = v.x, ey = v.y; - float ew = element.getWidth(), eh = element.getHeight(); - - // 木偶元素边界 - float left = x, right = x + w; - float bottom = y, top = y + h; - - // 对齐元素边界 - float eleft = ex, eright = ex + ew; - float ebottom = ey, etop = ey + eh; - - float alignX = x, alignY = y; - int alignFrom = 0, alignTo = 0; - - boolean aligned = false; - - if(Math.abs(left - eleft) <= alignBorder){ // 左边往左边贴 - alignX = eleft; - - verticalLines.add(eleft); - - alignFrom |= Align.left; - alignTo |= Align.left; - - aligned = true; - }else if(Math.abs(right - eleft) <= alignBorder){ // 右边往左边贴 - alignX = eleft - w; - - verticalLines.add(eleft); - - alignFrom |= Align.right; - alignTo |= Align.left; - - aligned = true; - } - - if(Math.abs(left - eright) <= alignBorder){ // 左边往右边贴 - alignX = eright; - - verticalLines.add(eright); - - alignFrom |= Align.left; - alignTo |= Align.right; - - aligned = true; - }else if(Math.abs(right - eright) <= alignBorder){ // 右边往右边贴 - alignX = eright - w; - - verticalLines.add(eright); - - alignFrom |= Align.right; - alignTo |= Align.right; - - aligned = true; - } - - if(Math.abs(bottom - ebottom) <= alignBorder){ // 下边往下边贴 - alignY = ebottom; - - horizontalLines.add(ebottom); - - alignFrom |= Align.bottom; - alignTo |= Align.bottom; - - aligned = true; - }else if(Math.abs(top - ebottom) <= alignBorder){ // 上边往下边贴 - alignY = ebottom - h; - - horizontalLines.add(ebottom); - - alignFrom |= Align.top; - alignTo |= Align.bottom; - - aligned = true; - } - - if(Math.abs(bottom - etop) <= alignBorder){ // 下边往上边贴 - alignY = etop; - - horizontalLines.add(etop); - - alignFrom |= Align.bottom; - alignTo |= Align.top; - - aligned = true; - }else if(Math.abs(top - etop) <= alignBorder){ // 上边往上边贴 - alignY = etop - h; - - horizontalLines.add(etop); - - alignFrom |= Align.top; - alignTo |= Align.top; - - aligned = true; - } - - puppet.setPosition(alignX, alignY); - - if(consumer != null && aligned){ - consumer.onAligned(element, alignFrom, alignTo); - } - - v.setZero(); - Pools.free(v); - } - - private static void updateResizeAlign(){ - for(Element element : vanillaElements){ - if(alizable(element)){ - updateResizeAlign(element); - } - } - - for(OperableTable table : operableTables){ - if(alizable(table)){ - updateResizeAlign(table); - } - } - } - - private static void updateResizeAlign(Element element){ - // 木偶元素坐标与Scene坐标系下的相同 - float x = puppet.x, y = puppet.y; - float w = puppet.getWidth(), h = puppet.getHeight(); - - // 对齐元素坐标不一定等于Scene坐标系下的,先做转换 - Vec2 v = Pools.obtain(Vec2.class, Vec2::new); - ElementUtils.getOriginOnScene(element, v); - float ex = v.x, ey = v.y; - float ew = element.getWidth(), eh = element.getHeight(); - - // 木偶元素边界 - float left = x, right = x + w; - float bottom = y, top = y + h; - - // 对齐元素边界 - float eleft = ex, eright = ex + ew; - float ebottom = ey, etop = ey + eh; - - float alignX = x, alignY = y; - float alignWidth = w, alignHeight = h; - int alignFrom = 0, alignTo = 0; - - boolean aligned = false; - - if(Align.isRight(touchEdge)){ - alignFrom |= Align.right; - - if(Math.abs(right - eleft) <= alignBorder){ // 右边往左边贴 - alignWidth = eleft - left; - - verticalLines.add(eleft); - - alignTo |= Align.left; - aligned = true; - }else if(Math.abs(right - eright) <= alignBorder){ // 右边往右边贴 - alignWidth = eright - left; - - verticalLines.add(eright); - - alignTo |= Align.right; - aligned = true; - } - }else if(Align.isLeft(touchEdge)){ - alignFrom |= Align.left; - - if(Math.abs(left - eleft) <= alignBorder){ // 左边往左边贴 - alignX = eleft; - alignWidth = right - eleft; - - verticalLines.add(eleft); - - alignTo |= Align.left; - aligned = true; - }else if(Math.abs(left - eright) <= alignBorder){ // 左边往右边贴 - alignX = eright; - alignWidth = right - eright; - - verticalLines.add(eright); - - alignTo |= Align.right; - aligned = true; - } - } - - if(Align.isTop(touchEdge)){ - alignFrom |= Align.top; - - if(Math.abs(top - ebottom) <= alignBorder){ // 上边往下边贴 - alignHeight = ebottom - bottom; - - horizontalLines.add(ebottom); - - alignTo |= Align.bottom; - aligned = true; - }else if(Math.abs(top - etop) <= alignBorder){ // 上边贴上边 - alignHeight = etop - bottom; - - horizontalLines.add(etop); - - alignTo |= Align.top; - aligned = true; - } - }else if(Align.isBottom(touchEdge)){ - alignFrom |= Align.bottom; - - if(Math.abs(bottom - ebottom) <= alignBorder){ // 下边贴下边 - alignY = ebottom; - alignHeight = top - ebottom; - - horizontalLines.add(ebottom); - - alignFrom |= Align.bottom; - aligned = true; - }else if(Math.abs(bottom - etop) <= alignBorder){ // 下边贴上边 - alignY = etop; - alignHeight = top - etop; - - horizontalLines.add(etop); - - alignTo |= Align.top; - aligned = true; - } - } - - puppet.setBounds(alignX, alignY, alignWidth, alignHeight); - - if(consumer != null && aligned){ - consumer.onAligned(element, alignFrom, alignTo); - } - - v.setZero(); - Pools.free(v); - } - - private static class OperatorPuppet extends PuppetElement{ - private static void drawPuppet(float x, float y, float width, float height){ - float halfWidth = width / 2, halfHeight = height / 2; - float halfX = x + halfWidth, halfY = y + halfHeight; - - // 单边宽高 - float borderWidth = width * resizeBorderRatio, borderHeight = height * resizeBorderRatio; - - float halfXAxes = (width - borderWidth) / 2; - float halfYAxes = (height - borderHeight) / 2; - - // 边框 - Drawf.dashRect(Pal.accent, x, y, width, borderHeight); - Drawf.dashRect(Pal.accent, x, y, borderWidth, height); - Drawf.dashRect(Pal.accent, x + halfXAxes * 2, y, borderWidth, height); - Drawf.dashRect(Pal.accent, x, y + halfYAxes * 2, width, borderHeight); - - // 填充 - Draw.color(Pal.accent, 0.3f); - Fill.rect(halfX, halfY, width, height); - - // 四方的小图标提示 - float size = 16; - Draw.color(Color.green, 1f); - - TextureRegion dragRegion = Icon.add.getRegion(); - Draw.rect(dragRegion, halfX, halfY, size, size); - - TextureRegion upRegion = Icon.up.getRegion(); - TextureRegion downRegion = Icon.down.getRegion(); - TextureRegion leftRegion = Icon.left.getRegion(); - TextureRegion rightRegion = Icon.right.getRegion(); - - Draw.rect(upRegion, halfX, halfY + halfYAxes, size, size); - Draw.rect(downRegion, halfX, halfY - halfYAxes, size, size); - Draw.rect(rightRegion, halfX + halfXAxes, halfY, size, size); - Draw.rect(leftRegion, halfX - halfXAxes, halfY, size, size); - - // 角落的小图标提示 - float degree = Mathf.atan2(width, height) * Mathf.radiansToDegrees; - Draw.rect(rightRegion, halfX + halfXAxes, halfY + halfYAxes, size, size, degree); // ↗ - Draw.rect(rightRegion, halfX + halfXAxes, halfY - halfYAxes, size, size, -degree); // ↘ - Draw.rect(rightRegion, halfX - halfXAxes, halfY + halfYAxes, size, size, 180 - degree); // ↖ - Draw.rect(rightRegion, halfX - halfXAxes, halfY - halfYAxes, size, size, degree - 180); // ↙ - - Draw.reset(); - } - - @Override - public void draw(){ - super.draw(); - - drawPuppet(x, y, width, height); - } - - } - - private static class OperatorBackground extends WidgetGroup{ - public Drawable background; - - public OperatorBackground(){ - background = Styles.black6; - setFillParent(true); - } - - private void drawOperableTable(float x, float y, float width, float height){ - float halfWidth = width / 2, halfHeight = height / 2; - float halfX = x + halfWidth, halfY = y + halfHeight; - - // 边框 - Drawf.dashRect(Pal.lightishGray, x, y, width, height); - - // 填充 - Draw.color(Pal.lightishGray, 0.3f); - Fill.rect(halfX, halfY, width, height); - - Draw.reset(); - } - - private void drawVanillaElement(float x, float y, float width, float height){ - // 边框 - Drawf.dashRect(Color.sky, x, y, width, height); - - Draw.reset(); - } - - private void drawAlignLines(){ - Draw.color(Pal.accent, 0.8f); - - for(float x : verticalLines){ - Lines.line(x, 0, x, height); - } - - for(float y : horizontalLines){ - Lines.line(0, y, width, y); - } - - Draw.reset(); - } - - @Override - public void draw(){ - // Draw background - Scene stage = getScene(); - Draw.color(color.r, color.g, color.b, color.a * parentAlpha); - background.draw(x, y, stage.getWidth(), stage.getHeight()); - Draw.reset(); - - super.draw(); - - Vec2 pos = Pools.obtain(Vec2.class, Vec2::new); - - for(OperableTable table : operableTables){ - if(table.operable()){ - ElementUtils.getOriginOnScene(table, pos); - stageToLocalCoordinates(pos); - - drawOperableTable(pos.x, pos.y, table.getWidth(), table.getHeight()); - } - } - - for(Element element : vanillaElements){ - if(operable(element)){ - ElementUtils.getOriginOnScene(element, pos); - stageToLocalCoordinates(pos); - - drawVanillaElement(pos.x, pos.y, element.getWidth(), element.getHeight()); - } - } - - drawAlignLines(); - - pos.setZero(); - Pools.free(pos); - } - - public void show(){ - // Showing - if(hasParent()){ - return; - } - - Core.scene.add(this); - } - - public void hide(){ - if(!hasParent()){ - return; - } - - remove(); - } - } -} diff --git a/src/MinerTools/utils/ui/operator/OperableTable.java b/src/MinerTools/utils/ui/operator/OperableTable.java deleted file mode 100644 index 6807e60..0000000 --- a/src/MinerTools/utils/ui/operator/OperableTable.java +++ /dev/null @@ -1,38 +0,0 @@ -package MinerTools.utils.ui.operator; - -import arc.scene.ui.layout.*; - -public class OperableTable extends Table implements OperateCons{ - public boolean keepWithinStage; - - public OperableTable(boolean keepWithinStage){ - this.keepWithinStage = keepWithinStage; - - ElementOperator.operableTables.add(this); - } - - public void operate(){ - ElementOperator.operate(this, this); - } - - public boolean operating(){ - return ElementOperator.operating(this); - } - - public boolean dragging(){ - return operating() && ElementOperator.dragMode; - } - - public boolean resizing(){ - return operating() && ElementOperator.resizeMode; - } - - public boolean operable(){ - return ElementOperator.operable(this); - } - - @Override - public boolean keepWithinStage(){ - return keepWithinStage; - } -} diff --git a/src/MinerTools/utils/ui/operator/OperateCons.java b/src/MinerTools/utils/ui/operator/OperateCons.java deleted file mode 100644 index 8cbd682..0000000 --- a/src/MinerTools/utils/ui/operator/OperateCons.java +++ /dev/null @@ -1,25 +0,0 @@ -package MinerTools.utils.ui.operator; - -import arc.scene.*; - -public interface OperateCons{ - default boolean keepWithinStage(){ - return true; - } - - default boolean alizable(Element element){ - return true; - } - - default void onDragged(float deltaX, float deltaY){ - } - - default void onResized(float deltaX, float deltaY){ - } - - default void onAligned(Element snap, int alignFrom, int alignTo){ - } - - default void onReleased(){ - } -} \ No newline at end of file diff --git a/src/MinerTools/utils/ui/operator/SavedTable.java b/src/MinerTools/utils/ui/operator/SavedTable.java deleted file mode 100644 index 0e4a2e9..0000000 --- a/src/MinerTools/utils/ui/operator/SavedTable.java +++ /dev/null @@ -1,63 +0,0 @@ -package MinerTools.utils.ui.operator; - -import MinerTools.*; -import MinerTools.utils.*; - -public class SavedTable extends OperableTable{ - private final DebounceTask savePositionTask = new DebounceTask(1f, () -> { - MinerVars.settings.put(name + ".pos.x", x); - MinerVars.settings.put(name + ".pos.y", y); - }), saveSizeTask = new DebounceTask(1f, () -> { - MinerVars.settings.put(name + ".size.width", width); - MinerVars.settings.put(name + ".size.height", height); - }); - protected boolean savePosition, saveSize; - - public SavedTable(String name, boolean savePosition, boolean saveSize){ - super(true); - - if(name == null && (savePosition || saveSize)){ - throw new RuntimeException("To save position and size, SavedTable must have a name." + this); - } - - this.name = name; - - this.savePosition = savePosition; - this.saveSize = saveSize; - } - - protected void readPosition(){ - if(savePosition){ - float x = MinerVars.settings.get(name + ".pos.x", this.x); - float y = MinerVars.settings.get(name + ".pos.y", this.y); - setPosition(x, y); - } - } - - protected void readSize(){ - if(saveSize){ - float width = MinerVars.settings.get(name + ".size.width", this.width); - float height = MinerVars.settings.get(name + ".size.height", this.height); - - // 自定义大小缩放无法保证ui有至少的空间 - width = Math.max(getMinWidth(), width); - height = Math.max(getMinHeight(), height); - - setSize(width, height); - } - } - - @Override - public void onDragged(float deltaX, float deltaY){ - if(savePosition){ - savePositionTask.run(); - } - } - - @Override - public void onResized(float deltaWidth, float deltaHeight){ - if(saveSize){ - saveSizeTask.run(); - } - } -}