Skip to content

Commit

Permalink
Docs *almost* done
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmeowry committed May 24, 2023
1 parent 92d2ee2 commit f57cb9b
Show file tree
Hide file tree
Showing 11 changed files with 487 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

import org.jetbrains.annotations.NotNull;

/**
* Represents a composable that can be further configured after creation
* @param <S> The type of the object being configured
*/
public interface Configurable<S> {
/**
* Configures this composable element.
* @param configurator Configurator to be applied
* @return The configured object
*/
S configure(Configurator<S> configurator);

/**
* Configures this composable element with a certain subtype of the object.
* @param configurator Configurator to be applied
* @return The configured objecc
* @param <V> The subtype of the object
*/
@SuppressWarnings("unchecked")
default <V extends S> V configureTyped(@NotNull Configurator<V> configurator) {
configurator.configure((V) this);
return (V) this;
}

interface Configurator<S> {
void configure(S self);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@
import space.maxus.flare.ui.Composable;
import space.maxus.flare.ui.ComposableLike;

/**
* ContainerSlot is a component that can have an item placed in it.
* <br />
* See more in Flare docs: <a href="https://flare.maxus.space/ui/composable#containerslot">Container Slot</a>
*/
public interface ContainerSlot extends Configurable<ContainerSlot>, Composable, Disable {
/**
* Returns item builder for empty container slot item
* @param name Extra message in item name
* @param description Extra description
* @return Item builder for empty container slot item
*/
static ItemStackBuilder emptyBuilder(@Nullable String name, @Nullable String description) {
return Items.builder(Material.LIGHT_GRAY_STAINED_GLASS_PANE)
.hideAllFlags()
Expand All @@ -23,44 +34,114 @@ static ItemStackBuilder emptyBuilder(@Nullable String name, @Nullable String des
.addLoreLine("<dark_gray>Click with item to put it here");
}

/**
* Returns item provider for empty container slot item
* @param name Extra message in item name
* @param description Extra description
* @return Item provider for empty container slot item
*/
@Contract("_, _ -> new")
static @NotNull ItemProvider emptyItem(@Nullable String name, @Nullable String description) {
return ItemProvider.still(emptyBuilder(name, description).build());
}

/**
* Constructs a new container slot with handlers
* @param onPut Put handler
* @param onTake Take handler
* @return New container slot with handlers
*/
static @NotNull ContainerSlot of(@NotNull ContainerEvent onPut, @NotNull ContainerEvent onTake) {
return new ContainerSlotImpl(null, emptyItem(null, null), false, (a, b) -> true, (a, b) -> true, onPut, onTake);
}

/**
* Constructs a new container slot builder
* @return New container slot builder
*/
static @NotNull Builder builder() {
return new ContainerSlotImpl.Builder();
}

/**
* Returns the reactive state of an item inside this container
* @return Reactive state of an item inside this container
*/
ReactiveState<ItemStack> itemState();

/**
* Gets the item inside this container
* @return Item inside this container
*/
default @Nullable ItemStack getItem() {
return itemState().get();
}

/**
* Sets the item inside this container
* @param stack Item inside this container
*/
default void setItem(@Nullable ItemStack stack) {
itemState().set(stack);
}

/**
* Builder for ContainerSlot
*/
interface Builder extends ComposableLike {
/**
* Makes the container disabled
* @param disabled Disabled state
* @return This builder
*/
Builder disabled(boolean disabled);

/**
* Sets the empty item provider for this container
* @param provider The empty item provider
* @return This builder
*/
Builder empty(@Nullable ItemProvider provider);

/**
* Sets the item inside this container
* @param item The item to be set
* @return This builder
*/
Builder item(@Nullable ItemStack item);

/**
* Sets the put handler for this container
* @param put Put handler
* @return This builder
*/
Builder onPut(ContainerEvent put);

/**
* Sets the take handler for this container
* @param take Take handler
* @return This builder
*/
Builder onTake(ContainerEvent take);

/**
* Sets the put filter for this container
* @param put Put filter
* @return This builder
*/
Builder filterPut(ContainerPredicate put);

/**
* Sets the take filter for this container
* @param take Take filter
* @return This builder
*/
Builder filterTake(ContainerPredicate take);

/**
* Builds this container
* @return Built container slot
*/
ContainerSlot build();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@

import java.util.Objects;

/**
* A button that takes player to previously opened frame. Only renders if it exists.
* <br />
* See more in Flare docs: <a href="https://flare.maxus.space/ui/composable#gobackbutton">Go Back Button</a>
*/
public interface GoBackButton extends Disable, Composable, Configurable<GoBackButton> {
/**
* Returns the default builder for go back button
* @param frame The previous frame
* @return The default builder for go back button
*/
static ItemStackBuilder goBackItemBuilder(@Nullable Frame frame) {
return Items.builder(Material.ARROW)
.name("<gray>Go Back <dark_gray>[◀]")
Expand All @@ -25,11 +35,20 @@ static ItemStackBuilder goBackItemBuilder(@Nullable Frame frame) {
.hideAllFlags();
}

/**
* Returns a default go back item
* @param player Player for whom to build this button
* @return Default go back button
*/
@Contract(pure = true)
static @NotNull ItemProvider goBackItem(HumanEntity player) {
return () -> goBackItemBuilder(PlayerFrameStateManager.peekPrevious(player)).build();
}

/**
* Returns a default go back item
* @return Default go back item
*/
@Contract(" -> new")
static @NotNull GoBackButton create() {
return new GoBackButtonImpl(null, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@
import org.jetbrains.annotations.NotNull;
import space.maxus.flare.item.ItemProvider;

/**
* A placeholder is a slot that can not be interacted with.
* <br />
* See more in Flare docs: <a href="https://flare.maxus.space/ui/composable#placeholder">Placeholder</a>
*/
public interface Placeholder extends ProviderRendered {
/**
* Constructs a new placeholder for an item provider
* @param provider The item provider
* @return A new placeholder with item provider
*/
@Contract("_ -> new")
static @NotNull Placeholder of(ItemProvider provider) {
return new PlaceholderImpl(provider);
}

/**
* Constructs a new placeholder with a still item provider of this item
* @param still Item to be used
* @return A new placeholder with item
*/
@Contract("_ -> new")
static @NotNull Placeholder of(ItemStack still) {
return new PlaceholderImpl(ItemProvider.still(still));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;
import space.maxus.flare.item.ItemProvider;
import space.maxus.flare.item.ItemStackBuilder;
import space.maxus.flare.item.Items;
Expand All @@ -12,44 +13,106 @@

import java.util.Objects;

/**
* A progress bar is a dynamic component that renders progress inside itself.
* <br />
* See more in Flare docs: <a href="https://flare.maxus.space/ui/composable#progressbar">Progress Bar</a>
*/
public interface ProgressBar extends Composable, Configurable<ProgressBar> {
/**
* Returns an item builder for a full progress bar part
* @param material Base material
* @param progress Bar progress
* @param dotted Whether the display should be dotted.
* @return An item builder for a full progress bar part
* @see FlareUtil#renderBarText(float, int, boolean)
*/
static ItemStackBuilder fullBuilder(Material material, float progress, boolean dotted) {
return Items.builder(material)
.name("<gray>Progress: <green>%s%% <dark_gray>[%%]".formatted(Math.round(progress * 100f)))
.addLoreLine(FlareUtil.renderBarText(progress, dotted ? 24 : 10, dotted))
.hideAllFlags();
}

/**
* Returns an item builder for an empty progress bar part
* @param material Base material
* @param progress Bar progress
* @param dotted Whether the display should be dotted.
* @return An item builder for an empty progress bar part
* @see FlareUtil#renderBarText(float, int, boolean)
*/
static ItemStackBuilder emptyBuilder(Material material, float progress, boolean dotted) {
return Items.builder(material)
.name("<gray>Progress: <red>%s%% <dark_gray>[%%]".formatted(Math.round(progress * 100f)))
.addLoreLine(FlareUtil.renderBarText(progress, dotted ? 24 : 10, dotted))
.hideAllFlags();
}

/**
* Returns an item builder for a full progress bar part
* @param material Base material
* @param state The bar progress reactive state
* @param dotted Whether the display should be dotted.
* @return An item builder for a full progress bar part
* @see FlareUtil#renderBarText(float, int, boolean)
*/
static @NotNull ItemProvider fullProvider(ReactiveState<Float> state, Material material, boolean dotted) {
return Reactive.item(state, progress -> fullBuilder(material, Objects.requireNonNullElse(progress, .0f), dotted).build());
}

/**
* Returns an item builder for an empty progress bar part
* @param material Base material
* @param state The bar progress reactive state
* @param dotted Whether the display should be dotted.
* @return An item builder for an empty progress bar part
* @see FlareUtil#renderBarText(float, int, boolean)
*/
static @NotNull ItemProvider emptyProvider(ReactiveState<Float> state, Material material, boolean dotted) {
return Reactive.item(state, progress -> emptyBuilder(material, Objects.requireNonNullElse(progress, .0f), dotted).build());
}

/**
* Constructs a progress bar with full and empty item providers
* @param full Item provider for filled part
* @param empty Item provider for empty part
* @return A progress bar with full and empty item providers
*/
static @NotNull ProgressBar of(ItemProvider full, ItemProvider empty) {
return of(full, empty, 0f);
}

static @NotNull ProgressBar of(ItemProvider full, ItemProvider empty, float progress) {
/**
* Constructs a progress bar with full and empty item providers and base progress
* @param full Item provider for filled part
* @param empty Item provider for empty part
* @param progress Starting progress of the bar.
* @return A progress bar with full and empty item providers
*/
static @NotNull ProgressBar of(ItemProvider full, ItemProvider empty, @Range(from = 0, to = 1) float progress) {
return new ProgressBarImpl(progress, full, empty);
}

/**
* Returns the current progress state
* @return Current progress state.
*/
ReactiveState<Float> progressState();

default float getProgress() {
/**
* Gets current bar progress
* @return Current bar progress
*/
default @Range(from = 0, to = 1) float getProgress() {
return progressState().get();
}

default float setProgress(float newProgress) {
return progressState().get();
/**
* Sets the bar progress
* @param newProgress New progress
*/
default void setProgress(@Range(from = 0, to = 1) float newProgress) {
progressState().set(newProgress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
import space.maxus.flare.ui.Composable;
import space.maxus.flare.ui.space.Slot;

/**
* An interface for components that are implicitly rendered by an item provider
*/
public interface ProviderRendered extends Composable {
/**
* Gets the provider for rendering
* @return The provider for rendering
*/
@NotNull ItemProvider getProvider();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

import java.util.concurrent.atomic.AtomicReference;

/**
* An abstract class for component implementations that automatically handles root frame injection
*/
public abstract class RootReferencing implements Composable {
/**
* The root frame
*/
protected final AtomicReference<Frame> root = new AtomicReference<>(null);
private @Nullable ComposableSpace attachedSpace;

Expand Down
Loading

0 comments on commit f57cb9b

Please sign in to comment.