From ea8d24cce0c9aef3db52dfbaddb520207c76bc3d Mon Sep 17 00:00:00 2001 From: Mnemotechnician Date: Sun, 14 Aug 2022 16:17:12 +0300 Subject: [PATCH] significant ui improvements --- mod.hjson | 17 +++++-- src/newconsole/ui/CStyles.java | 2 +- src/newconsole/ui/CodeSpinner.java | 6 +-- src/newconsole/ui/Spinner.java | 29 +++++++----- src/newconsole/ui/dialogs/AutorunDialog.java | 5 +- src/newconsole/ui/dialogs/SavesDialog.java | 48 +++++++++++++++++++- 6 files changed, 85 insertions(+), 22 deletions(-) diff --git a/mod.hjson b/mod.hjson index 92707bb..04363de 100644 --- a/mod.hjson +++ b/mod.hjson @@ -7,9 +7,20 @@ displayName: "New Console" name: "newconsole" -author: "Мнемотехник" -description: "Adds a much better console ui.\n\nAlso includes a file browser, a scripts menu, an events menu and much much more!\n\nSee README.md for more info." -version: 1.5 +author: "Mnemotechnician" + +description: ''' +Adds a console ui superiour to the built-in one and the ones provided by any other mods. +User-friendly interface, a good log panel, execution history, monospace font - all that is included in this mod. + +In addiction to the console, this mod offers: +- An ability to save as many scripts as you want, execute them or copy to the console +- An ability to execute scripts when certain events get fired +- A full-blown file browser (be careful, you can cause real harm with that!) + +See README.md for more info. +''' +version: 1.6 subtitle: "Adds a much better console UI." minGameVersion: 136 hidden: true diff --git a/src/newconsole/ui/CStyles.java b/src/newconsole/ui/CStyles.java index adb9e92..52d8cd6 100644 --- a/src/newconsole/ui/CStyles.java +++ b/src/newconsole/ui/CStyles.java @@ -41,7 +41,7 @@ public static void loadSync() { }}); mono.getData().markupEnabled = true; - scriptbg = Tex.buttonOver; + scriptbg = Tex.button; playIcon = Icon.play.tint(Color.green); editIcon = Icon.edit.tint(Color.yellow); diff --git a/src/newconsole/ui/CodeSpinner.java b/src/newconsole/ui/CodeSpinner.java index 0946258..7993186 100644 --- a/src/newconsole/ui/CodeSpinner.java +++ b/src/newconsole/ui/CodeSpinner.java @@ -7,7 +7,7 @@ import mindustry.ui.*; import newconsole.ui.*; -/** Displays arbitrary code in a code block. Provides some optimisations. */ +/** Displays arbitrary code in a code block. */ public class CodeSpinner extends Spinner { public String code; @@ -18,7 +18,7 @@ public CodeSpinner(String code) { table.setBackground(CStyles.scriptbg); - codeLabel = table.labelWrap("").grow().get(); + codeLabel = table.add("", CStyles.monoLabel).with(it -> it.setWrap(true)).grow().get(); this.code = Strings.stripColors(code); } @@ -29,4 +29,4 @@ public void show(boolean animate) { super.show(animate); } -} \ No newline at end of file +} diff --git a/src/newconsole/ui/Spinner.java b/src/newconsole/ui/Spinner.java index 26e07cc..3b38752 100644 --- a/src/newconsole/ui/Spinner.java +++ b/src/newconsole/ui/Spinner.java @@ -79,20 +79,25 @@ public Spinner(String header, TextButton.TextButtonStyle style, boolean unique, if (col.getScene() != null) { col.visible = true; - //col.toFront(); TODO: causes more harm than sudden disappearing of spinners + col.toFront(); col.color.a = parentAlpha * color.a; - col.setSize(width, col.getPrefHeight()); - Vec2 point = localToStageCoordinates(Tmp.v1.set(0, 0)); - float height = Math.min(point.y, table.getPrefHeight()); - point = localToStageCoordinates(Tmp.v1.set(0, -height)); - - /*if (point.y < Core.scene.getHeight() / 2) { - point = localToStageCoordinates(Tmp.v1.set(0, getPrefHeight())); - height = Core.scene.getHeight() - point.y; - }*/ - pane.setHeight(height); - col.setPosition(point.x, point.y); + var reverse = false; + var pos = localToStageCoordinates(Tmp.v1.set(0, -table.getPrefHeight())); + if (pos.y <= 0 && pos.y + table.getPrefHeight() * 2 + height < Core.scene.getHeight()) { + pos.y += table.getPrefHeight() + height; + reverse = true; + } + + var freeHeight = reverse + ? Core.scene.getHeight() - localToStageCoordinates(Tmp.v2.set(0, height)).y + : localToStageCoordinates(Tmp.v2.setZero()).y; + + col.setSize(width, Math.min(freeHeight, table.getPrefHeight())); + pane.setSize(width, Math.min(freeHeight, table.getPrefHeight())); + table.setSize(width, Math.min(freeHeight, table.getPrefHeight())); + col.setPosition(pos.x, pos.y); + col.validate(); } if (autoHide && col.getScene() != null) { diff --git a/src/newconsole/ui/dialogs/AutorunDialog.java b/src/newconsole/ui/dialogs/AutorunDialog.java index 4dcf370..7deecbe 100644 --- a/src/newconsole/ui/dialogs/AutorunDialog.java +++ b/src/newconsole/ui/dialogs/AutorunDialog.java @@ -80,7 +80,10 @@ public AutorunDialog() { Vars.ui.showInfo("@newconsole.empty-script"); } }).growX(); - })).margin(4f).width(350f).with(it -> ((Spinner) it).setStyle(Styles.defaultt)).row(); + })).margin(4f).width(350f).with(it -> { + ((Spinner) it).setStyle(Styles.defaultt); + it.unique = false; + }).row(); }) ).grow(); } diff --git a/src/newconsole/ui/dialogs/SavesDialog.java b/src/newconsole/ui/dialogs/SavesDialog.java index c818fc3..1dea056 100644 --- a/src/newconsole/ui/dialogs/SavesDialog.java +++ b/src/newconsole/ui/dialogs/SavesDialog.java @@ -1,13 +1,16 @@ package newconsole.ui.dialogs; +import arc.struct.*; import arc.graphics.*; import arc.scene.*; import arc.scene.ui.*; import arc.scene.ui.layout.*; +import arc.util.*; import mindustry.*; import mindustry.gen.*; import mindustry.ui.*; import mindustry.ui.dialogs.*; +import java.util.Comparator; import newconsole.*; import newconsole.io.*; @@ -61,7 +64,16 @@ public SavesDialog() { public void rebuild() { scriptsTable.clearChildren(); - ScriptsManager.eachScript((name, script) -> add(name, script)); + + // copy to a seq, then sort by name - fuck java. + Seq> seq = new Seq<>(ScriptsManager.scripts.size); + ScriptsManager.eachScript((name, script) -> { + seq.add(new Pair(name, script)); + }); + + seq.sort(new EntryComparator()); + + seq.each(it -> add(it.first, it.second)); } @Override @@ -98,5 +110,37 @@ public void add(String name, String script) { }); }).growX().pad(2f).marginBottom(20).row(); } - + + /** Same as kotlin.Pair. */ + public static class Pair { + final A first; + final B second; + + public Pair(A first, B second) { + this.first = first; + this.second = second; + } + } + + public class EntryComparator implements Comparator> { + @Override + public int compare(Pair o1, Pair o2) { + if (o1 == o2) return 0; + + var left = Strings.stripColors(o1.first); + var right = Strings.stripColors(o2.first); + + for (int i = 0; i < Math.min(left.length(), right.length()); i++) { + var diff = Character.toLowerCase(left.charAt(i)) - Character.toLowerCase(right.charAt(i)); + if (diff != 0) return diff; + } + + if (left.length() > right.length()) return -1; + return 1; + } + + @Override + public boolean equals(Object o) { return o == this; } + } + }