added: toggle

This commit is contained in:
ramollia
2025-10-03 22:34:26 +02:00
parent ac6de04e68
commit fae5b8bcda
3 changed files with 48 additions and 2 deletions

View File

@@ -3,13 +3,18 @@ package org.toop.app.layer;
import org.toop.app.events.AppEvents;
import org.toop.framework.eventbus.GlobalEventBus;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import java.util.function.Consumer;
public final class Container {
public enum Type {
VERTICAL, HORIZONTAL,
@@ -82,5 +87,36 @@ public final class Container {
return addButton("button", x, runnable);
}
public Label addToggle(String cssClass, String unChecked, String checked, Consumer<Boolean> consumer) {
final Label element = new Label(unChecked);
element.getStyleClass().add(cssClass);
final BooleanProperty selected = new SimpleBooleanProperty(false);
element.setOnMouseEntered(_ -> {
GlobalEventBus.post(new AppEvents.OnNodeHover());
});
element.setOnMouseClicked(_ -> {
GlobalEventBus.post(new AppEvents.OnNodeClick());
selected.set(!selected.get());
if (selected.get()) {
element.setText(checked);
} else {
element.setText(unChecked);
}
consumer.accept(selected.get());
});
container.getChildren().addLast(element);
return element;
}
public Label addToggle(String unChecked, String checked, Consumer<Boolean> consumer) {
return addToggle("toggle", unChecked, checked, consumer);
}
public Pane getContainer() { return container; }
}

View File

@@ -11,9 +11,19 @@ public class SelectionLayer extends Layer {
protected SelectionLayer(GameType type) {
super("selection.css");
final Container player1Container = Container.create(Container.Type.HORIZONTAL, 5);
player1Container.addToggle("test","other test", (checked) -> {
if (checked) {
System.out.println("Checked");
} else {
System.out.println("Un checked");
}
});
final Container controlContainer = Container.create(Container.Type.VERTICAL, 5);
controlContainer.addButton("Back", () -> { App.activate(new MainLayer()); });
addContainer(player1Container, Pos.TOP_LEFT, 2, 2);
addContainer(controlContainer, Pos.BOTTOM_LEFT, 2, -2);
}
}

View File

@@ -20,7 +20,7 @@
-fx-font-size: 24px;
}
.button {
.button, .toggle {
-fx-padding: 10;
-fx-background-color: transparent;
@@ -29,7 +29,7 @@
-fx-transition: all 0.3s ease-in-out;
}
.button:hover {
.button:hover, .toggle:hover {
-fx-cursor: hand;
-fx-scale-x: 1.1;