Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/main/java/com/cleanroommc/modularui/widgets/PagedWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@
import com.cleanroommc.modularui.widget.Widget;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.IntConsumer;

public class PagedWidget<W extends PagedWidget<W>> extends Widget<W> {

private final List<IWidget> pages = new ArrayList<>();
private IWidget currentPage;
private int currentPageIndex = 0;
@Nullable
private IntConsumer onPageChange;

@Override
public void afterInit() {
setPage(0);
}

/**
* Set a consumer that is accepted <b>right after</b> the page is actually changed and the next page widget is enabled. <br/>
* Will also be called with {@code 0} when after this widget is initialized.
*/
public W onPageChange(@Nullable IntConsumer onPageChange) {
this.onPageChange = onPageChange;
return getThis();
}

public void setPage(int page) {
if (page < 0 || page >= this.pages.size()) {
throw new IndexOutOfBoundsException();
Expand All @@ -30,6 +43,10 @@ public void setPage(int page) {
}
this.currentPage = this.pages.get(this.currentPageIndex);
this.currentPage.setEnabled(true);

if (this.onPageChange != null) {
this.onPageChange.accept(page);
}
}

public void nextPage() {
Expand Down