Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP UI builder api. #1544

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/AdjusterInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

import java.util.function.Consumer;

public interface AdjusterInput<T extends Number> extends UiInput<T, AdjusterInput<T>> {
AdjusterInput<T> setRange(double min, double max);
AdjusterInput<T> setRange(double min, double max, double logScaleStart);
double getMin();
double getMax();
AdjusterInput<T> setClamp(boolean min, boolean max);

AdjusterInput<T> setLogarithmic(boolean logarithmic);
AdjusterInput<T> setMaxInfinity(boolean maxInfinity);

AdjusterInput<T> addAfterChangeHandler(Consumer<T> handler);
AdjusterInput<T> removeAfterChangeHandler(Consumer<T> handler);
}
26 changes: 26 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/CheckboxInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

import java.util.function.BooleanSupplier;

public interface CheckboxInput extends UiInput<Boolean, CheckboxInput> {
CheckboxInput setDisable(boolean value);
CheckboxInput setDisable(BooleanSupplier valueSupplier);
}
42 changes: 42 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/ChoiceBoxInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;


import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* An item that implements {@link se.llbit.fxutil.ListSeparator} will be rendered as a separator if supported,
* otherwise it will be ignored.
*/
public interface ChoiceBoxInput<T> extends UiInput<T, ChoiceBoxInput<T>> {
ChoiceBoxInput<T> addItems(Collection<? extends T> items);
default ChoiceBoxInput<T> addItems(T... items) {
return addItems(Arrays.asList(items));
}
ChoiceBoxInput<T> select(Predicate<T> predicate);
ChoiceBoxInput<T> setStringConverter(Function<T, String> toString);
/**
* Set the tooltip converter. Return {@code null} for no tooltip.
*/
ChoiceBoxInput<T> setTooltipConverter(Function<T, String> toTooltip);
}
26 changes: 26 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/Configurable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

public interface Configurable {
/**
* Build the interface for this {@code Configurable}.
*/
void build(UiBuilder builder);
}
54 changes: 54 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/UiBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

import se.llbit.util.Registerable;

import java.util.function.Consumer;

public interface UiBuilder {
/**
* Add a node to this builder. Return {@code false} if the node could not be added. This is implementation
* specific and should be type checked.
*/
default boolean addNode(Object node) {
return false;
}

default void addNodeOrElse(Object node, Consumer<UiBuilder> orElse) {
if (!addNode(node)) {
orElse.accept(this);
}
}

void separator();
AdjusterInput<Integer> integerAdjuster();
AdjusterInput<Double> doubleAdjuster();
CheckboxInput checkbox();
UiButton button();
<T> ChoiceBoxInput<T> choiceBoxInput();
default <T extends Registerable> ChoiceBoxInput<T> registerableChoiceBoxInput() {
ChoiceBoxInput<T> input = choiceBoxInput();
input.setStringConverter(Registerable::getName);
input.setTooltipConverter(Registerable::getDescription);
return input;
}
UiLabel label();
UiText text();
}
33 changes: 33 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/UiButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

import se.llbit.chunky.world.Icon;

import java.util.function.Consumer;

public interface UiButton {
UiButton setText(String text);
UiButton setTooltip(String tooltip);
UiButton setGraphic(Icon icon);

UiButton callCallbacks();
UiButton addCallback(Consumer<UiButton> callback);
UiButton removeCallback(Consumer<UiButton> callback);
}
40 changes: 40 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/UiInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

import java.util.function.Consumer;
import java.util.function.Supplier;

public interface UiInput<T, Self extends UiInput<T, Self>> {
/**
* Set the value of the adjuster without calling any callbacks.
*/
Self set(T value);
Self set(Supplier<T> valueSupplier);
T get();

Self setName(String name);
Self setTooltip(String tooltip);

Self callCallbacks();
Self addCallback(Consumer<T> callback);
Self removeCallback(Consumer<T> callback);

void refresh();
}
25 changes: 25 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/UiLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

public interface UiLabel {
UiLabel setText(String text);
UiLabel setTooltip(String tooltip);
UiLabel setFont(String font, double size);
}
25 changes: 25 additions & 0 deletions chunky/src/java/se/llbit/chunky/ui/builder/UiText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Chunky contributors
*
* This file is part of Chunky.
*
* Chunky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chunky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Chunky. If not, see <http://www.gnu.org/licenses/>.
*/

package se.llbit.chunky.ui.builder;

public interface UiText {
UiText setText(String text);
UiText setTooltip(String tooltip);
UiText setFont(String font, double size);
}
Loading