Skip to content

Commit

Permalink
feat: Updatable
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Sep 13, 2023
1 parent f705f91 commit 51e0a88
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
25 changes: 21 additions & 4 deletions src/main/java/io/wispforest/owo/ui/container/Containers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.util.ElementConfigurer;
import net.minecraft.text.Text;

public final class Containers {
Expand All @@ -13,19 +14,35 @@ private Containers() {}
// ------

public static GridLayout grid(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns) {
return new GridLayout(horizontalSizing, verticalSizing, rows, columns);
return new GridLayout(horizontalSizing, verticalSizing, rows, columns, null);
}

public static GridLayout grid(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns, ElementConfigurer<GridLayout> configurer) {
return new GridLayout(horizontalSizing, verticalSizing, rows, columns, configurer);
}

public static FlowLayout verticalFlow(Sizing horizontalSizing, Sizing verticalSizing) {
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.VERTICAL);
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.VERTICAL, null);
}

public static FlowLayout horizontalFlow(Sizing horizontalSizing, Sizing verticalSizing) {
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.HORIZONTAL);
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.HORIZONTAL, null);
}

public static FlowLayout ltrTextFlow(Sizing horizontalSizing, Sizing verticalSizing) {
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.LTR_TEXT);
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.LTR_TEXT, null);
}

public static FlowLayout verticalFlow(Sizing horizontalSizing, Sizing verticalSizing, ElementConfigurer<FlowLayout> configurer) {
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.VERTICAL, configurer);
}

public static FlowLayout horizontalFlow(Sizing horizontalSizing, Sizing verticalSizing, ElementConfigurer<FlowLayout> configurer) {
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.HORIZONTAL, configurer);
}

public static FlowLayout ltrTextFlow(Sizing horizontalSizing, Sizing verticalSizing, ElementConfigurer<FlowLayout> configurer) {
return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.LTR_TEXT, configurer);
}

public static StackLayout stack(Sizing horizontalSizing, Sizing verticalSizing) {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/io/wispforest/owo/ui/container/FlowLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,45 @@
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.parsing.UIModel;
import io.wispforest.owo.ui.parsing.UIParsing;
import io.wispforest.owo.ui.util.ElementConfigurer;
import io.wispforest.owo.ui.util.MountingHelper;
import io.wispforest.owo.ui.util.Updatable;
import io.wispforest.owo.util.Observable;
import org.apache.commons.lang3.mutable.MutableInt;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.*;

public class FlowLayout extends BaseParentComponent {
public class FlowLayout extends BaseParentComponent implements Updatable {

protected final List<Component> children = new ArrayList<>();
protected final List<Component> childrenView = Collections.unmodifiableList(this.children);
protected final Algorithm algorithm;

protected Size contentSize = Size.zero();
protected Observable<Integer> gap = Observable.of(0);
protected ElementConfigurer<FlowLayout> configurer;

protected FlowLayout(Sizing horizontalSizing, Sizing verticalSizing, Algorithm algorithm) {
protected FlowLayout(Sizing horizontalSizing, Sizing verticalSizing, Algorithm algorithm, ElementConfigurer<FlowLayout> configurer) {
super(horizontalSizing, verticalSizing);
this.algorithm = algorithm;

this.gap.observe(integer -> this.updateLayout());
this.configurer = configurer;
update();
}

@Override
public void update() {
if (configurer != null) {
children.clear();
configurer.configure(this);
updateLayout();
}
}


@Override
protected int determineHorizontalContentSize(Sizing sizing) {
return this.contentSize.width() + this.padding.get().horizontal();
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/io/wispforest/owo/ui/container/GridLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,43 @@
import io.wispforest.owo.ui.parsing.UIModel;
import io.wispforest.owo.ui.parsing.UIModelParsingException;
import io.wispforest.owo.ui.parsing.UIParsing;
import io.wispforest.owo.ui.util.ElementConfigurer;
import io.wispforest.owo.ui.util.Updatable;
import org.apache.commons.lang3.mutable.MutableInt;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.*;

public class GridLayout extends BaseParentComponent {
public class GridLayout extends BaseParentComponent implements Updatable {

protected final int rows, columns;

protected final Component[] children;
protected Component[] children;
protected final List<Component> nonNullChildren = new ArrayList<>();
protected final List<Component> nonNullChildrenView = Collections.unmodifiableList(this.nonNullChildren);

protected ElementConfigurer<GridLayout> configurer;
protected Size contentSize = Size.zero();

protected GridLayout(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns) {
protected GridLayout(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns, ElementConfigurer<GridLayout> configurer) {
super(horizontalSizing, verticalSizing);

this.rows = rows;
this.columns = columns;

this.configurer = configurer;
this.children = new Component[rows * columns];
update();
}

@Override
public void update() {
if (configurer != null) {
children = new Component[rows * columns];
configurer.configure(this);
}
}

@Override
Expand Down Expand Up @@ -193,6 +206,6 @@ public static GridLayout parse(Element element) {
int rows = UIParsing.parseUnsignedInt(element.getAttributeNode("rows"));
int columns = UIParsing.parseUnsignedInt(element.getAttributeNode("columns"));

return new GridLayout(Sizing.content(), Sizing.content(), rows, columns);
return new GridLayout(Sizing.content(), Sizing.content(), rows, columns, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.wispforest.owo.ui.util;

public interface ElementConfigurer<T> {
void configure(T element);
}
5 changes: 5 additions & 0 deletions src/main/java/io/wispforest/owo/ui/util/Updatable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.wispforest.owo.ui.util;

public interface Updatable {
void update();
}

0 comments on commit 51e0a88

Please sign in to comment.