From 74abba5e80c82ffc9bf3a8e2a5317639213f5919 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Tue, 25 Jun 2024 07:04:08 +0200 Subject: [PATCH] add labels --- .../clientboot/CBCallbackContainer.java | 4 +- .../de/com/baseband/clientboot/CBWindow.java | 59 +++++++++++++++---- .../com/baseband/clientboot/ClientBoot.java | 17 ++++++ 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/com/baseband/clientboot/CBCallbackContainer.java b/src/main/java/de/com/baseband/clientboot/CBCallbackContainer.java index 6ed3de3..bbd9446 100644 --- a/src/main/java/de/com/baseband/clientboot/CBCallbackContainer.java +++ b/src/main/java/de/com/baseband/clientboot/CBCallbackContainer.java @@ -1,7 +1,7 @@ package de.com.baseband.clientboot; -class CBCallbackContainer { - final String name; +public class CBCallbackContainer { + public String name; final CBCallback callback; CBCallbackContainer(String name, CBCallback callback) { diff --git a/src/main/java/de/com/baseband/clientboot/CBWindow.java b/src/main/java/de/com/baseband/clientboot/CBWindow.java index 57d439d..d4f566f 100644 --- a/src/main/java/de/com/baseband/clientboot/CBWindow.java +++ b/src/main/java/de/com/baseband/clientboot/CBWindow.java @@ -47,9 +47,34 @@ public class CBWindow extends JFrame implements WindowListener { return false; } - @Override - public void setVisible(boolean b) { - super.setVisible(b); + public void checkSelected(boolean wasMovingUp) { + List list = options.peek(); + if(list.isEmpty()) { + selected = 0; + return; + } + int min = Integer.MAX_VALUE; + int max = 0; + while (list.get(selected).callback == null) { + if(wasMovingUp) { + if(selected-- == 0) { + selected++; + wasMovingUp = false; + } + } + else { + if(selected++ == list.size()) { + selected--; + wasMovingUp = true; + } + } + min = Math.min(selected, min); + max = Math.max(selected, max); + if(min == 0 && max == list.size()) { + selected = 0; + break; + } + } } @Override @@ -112,13 +137,18 @@ public class CBWindow extends JFrame implements WindowListener { List list = options.peek(); for (int i = 0; i < list.size(); i++) { - if(justRanSomething.isLocked() && i == selected) { - g.setColor(new Color(0xee00ee)); - g.drawString("> "+ list.get(i).name, 7, y += 2 + 15); - g.setColor(new Color(0xcc00cc)); - continue; + CBCallbackContainer item = list.get(i); + if(item.callback != null) { + if (justRanSomething.isLocked() && i == selected) { + g.setColor(new Color(0xee00ee)); + g.drawString("> " + item.name, 7, y += 2 + 15); + g.setColor(new Color(0xcc00cc)); + continue; + } + g.drawString((i == selected ? "> " : "") + item.name, 5, y += 2 + 15); + } else { + g.drawString(item.name, 3, y += 2 + 15); } - g.drawString((i == selected ? "> " : "") + list.get(i).name, 5, y += 2 + 15); } } @@ -151,13 +181,20 @@ public class CBWindow extends JFrame implements WindowListener { justRanSomething.unlock(); if(e.getKeyCode() == KeyEvent.VK_DOWN && options.peek().size() > selected + 1) { selected++; + checkSelected(false); } - if(e.getKeyCode() == KeyEvent.VK_UP && selected != 0) { + if(e.getKeyCode() == KeyEvent.VK_UP && selected > 0) { selected--; + checkSelected(true); } if(e.getKeyCode() == KeyEvent.VK_ENTER) { justRanSomething.lock(200); - options.peek().get(selected).callback.run(parent); + CBCallback cb = options.peek().get(selected).callback; + if(cb != null) + cb.run(parent); + } + if(e.getKeyCode() == KeyEvent.VK_ESCAPE) { + parent.back(); } } diff --git a/src/main/java/de/com/baseband/clientboot/ClientBoot.java b/src/main/java/de/com/baseband/clientboot/ClientBoot.java index 2ad6bfd..fad07b6 100644 --- a/src/main/java/de/com/baseband/clientboot/ClientBoot.java +++ b/src/main/java/de/com/baseband/clientboot/ClientBoot.java @@ -3,14 +3,17 @@ package de.com.baseband.clientboot; import de.tudbut.parsing.TCN; import java.util.ArrayList; +import java.util.List; public class ClientBoot { public static void main(String[] args) { new ClientBoot("ClientBoot Test") .option("Print something", x -> x.newScreen() + .label("Here, you can print stuff!") .option("Hello!", x1 -> System.out.println("Hello!")) .option("Hellorld!", x1 -> System.out.println("Hellorld!")) + .label("Here, you can exit!") .option("Back", ClientBoot::back)) .option("Exit", ClientBoot::finish) .show(); @@ -31,6 +34,14 @@ public class ClientBoot { return this; } + public ClientBoot label(String name) { + List list = window.options.peek(); + if(window.selected == list.size()) + window.selected++; + list.add(new CBCallbackContainer(name, null)); + return this; + } + public ClientBoot option(String name, CBCallback callback) { window.options.peek().add(new CBCallbackContainer(name, callback)); return this; @@ -38,6 +49,8 @@ public class ClientBoot { public void back() { window.options.pop(); + if(window.options.isEmpty()) + finish(); window.selected = 0; window.justRanSomething.unlock(); } @@ -46,6 +59,10 @@ public class ClientBoot { window.windowClosing(null); } + public CBCallbackContainer currentOption() { + return window.options.peek().get(window.selected); + } + public TCN show() { window.waitForClose(); return data;