Skip to content

Commit

Permalink
feat: Some more utility functions for Actions and BiActions
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 8, 2024
1 parent d0cd75a commit 96785da
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.xpdustry.distributor.api.DistributorProvider;
import com.xpdustry.distributor.api.audience.Audience;
import com.xpdustry.distributor.api.key.Key;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import mindustry.Vars;
Expand Down Expand Up @@ -178,6 +179,40 @@ static Action show(final WindowManager manager) {
return window -> manager.create(window).show();
}

/**
* Returns an action using the result of the given delegate to run another action.
*
* @param delegate the delegate
* @return the action
*/
static Action delegate(final Function<Window, Action> delegate) {
return window -> delegate.apply(window).act(window);
}

/**
* Returns an action composed of multiple actions.
*
* @param actions the actions
* @return the action
*/
static Action compose(final Action... actions) {
return compose(List.of(actions));
}

/**
* Returns an action composed of multiple actions.
*
* @param actions the actions
* @return the action
*/
static Action compose(final List<Action> actions) {
return window -> {
for (final var action : actions) {
action.act(window);
}
};
}

/**
* Executes the action.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.xpdustry.distributor.api.gui;

import com.xpdustry.distributor.api.key.Key;
import java.util.List;
import java.util.function.BiFunction;

/**
* A function that is executed when an interaction occurs in a gui. Takes an additional parameter as the input.
Expand Down Expand Up @@ -47,6 +49,41 @@ static <T> BiAction<T> with(final Key<T> key) {
return (window, value) -> window.getState().set(key, value);
}

/**
* Returns a bi-action using the result of the given delegate to run another action.
*
* @param delegate the delegate
* @return the bi-action
*/
static <T> BiAction<T> delegate(final BiFunction<Window, T, Action> delegate) {
return (window, value) -> delegate.apply(window, value).act(window);
}

/**
* Returns a bi-action composed of multiple bi-actions.
*
* @param actions the bi-actions
* @return the bi-action
*/
@SafeVarargs
static <T> BiAction<T> compose(final BiAction<T>... actions) {
return compose(List.of(actions));
}

/**
* Returns a bi-action composed of multiple bi-actions.
*
* @param actions the bi-actions
* @return the bi-action
*/
static <T> BiAction<T> compose(final List<BiAction<T>> actions) {
return (window, value) -> {
for (final var action : actions) {
action.act(window, value);
}
};
}

/**
* Executes the bi-action.
*
Expand Down

0 comments on commit 96785da

Please sign in to comment.