add labels
All checks were successful
/ Build & Publish (push) Successful in 42s

This commit is contained in:
Daniella 2024-06-25 07:04:08 +02:00
parent ebf5c1d39e
commit 74abba5e80
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
3 changed files with 67 additions and 13 deletions

View file

@ -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) {

View file

@ -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<CBCallbackContainer> 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<CBCallbackContainer> 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();
}
}

View file

@ -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<CBCallbackContainer> 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;