Skip to content

Commit

Permalink
merge 1.20.3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed Apr 28, 2024
2 parents 426f545 + a06c477 commit 1adfd6f
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
loader_version=0.15.10
# Mod Properties
mod_version=0.12.5
mod_version=0.12.6
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/wispforest/owo/serialization/Endec.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public T decodeStruct(SerializationContext ctx, Deserializer.Struct struct) {
* whichever variant is represented. In the general for non-self-described formats, the
* which variant is represented must also be stored
*/
private static <F, S> Endec<Either<F, S>> either(Endec<F> first, Endec<S> second) {
static <F, S> Endec<Either<F, S>> either(Endec<F> first, Endec<S> second) {
return new EitherEndec<>(first, second, false);
}

Expand Down Expand Up @@ -433,6 +433,14 @@ default Endec<T> catchErrors(BiFunction<Deserializer<?>, Exception, T> decodeOnE
});
}

/**
* Create a new endec which serializes a set of elements
* serialized using this endec as an xmapped list
*/
default Endec<Set<T>> setOf() {
return this.listOf().xmap(HashSet::new, ArrayList::new);
}

/**
* Create a new endec by wrapping {@link #optionalOf()} and mapping between
* present optional &lt;-&gt; value and empty optional &lt;-&gt; null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.*;

public class SerializationContext {
public final class SerializationContext {

private static final SerializationContext EMPTY = new SerializationContext(Map.of(), Set.of());

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/wispforest/owo/serialization/StructEndec.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.*;
import io.wispforest.owo.mixin.ForwardingDynamicOpsAccessor;
import io.wispforest.owo.mixin.RegistryOpsAccessor;
import io.wispforest.owo.serialization.endec.StructField;
import io.wispforest.owo.serialization.format.edm.*;
import net.minecraft.registry.RegistryOps;
import net.minecraft.util.dynamic.ForwardingDynamicOps;
Expand Down Expand Up @@ -36,6 +37,24 @@ default T decode(SerializationContext ctx, Deserializer<?> deserializer) {
return this.decodeStruct(ctx, deserializer.struct());
}

default <S> StructField<S, T> flatFieldOf(Function<S, T> getter) {
return new StructField.Flat<>(this, getter);
}

@Override
default <R> StructEndec<R> xmap(Function<T, R> to, Function<R, T> from) {
return new StructEndec<>() {
@Override
public void encodeStruct(SerializationContext ctx, Serializer.Struct struct, R value) {
StructEndec.this.encodeStruct(ctx, struct, from.apply(value));
}
@Override
public R decodeStruct(SerializationContext ctx, Deserializer.Struct struct) {
return to.apply(StructEndec.this.decodeStruct(ctx, struct));
}
};
}

default MapCodec<T> mapCodec(SerializationContext assumedContext) {
return new MapCodec<>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.SerializationContext;
import io.wispforest.owo.serialization.Serializer;
import io.wispforest.owo.serialization.StructEndec;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;
import java.util.function.Supplier;

public final class StructField<S, F> {
public sealed class StructField<S, F> permits StructField.Flat {

private final String name;
private final Endec<F> endec;
private final Function<S, F> getter;
private final @Nullable Supplier<F> defaultValueFactory;
protected final String name;
protected final Endec<F> endec;
protected final Function<S, F> getter;
protected final @Nullable Supplier<F> defaultValueFactory;

public StructField(String name, Endec<F> endec, Function<S, F> getter, @Nullable Supplier<F> defaultValueFactory) {
this.name = name;
Expand All @@ -40,4 +41,25 @@ public F decodeField(SerializationContext ctx, Deserializer.Struct struct) {
? struct.field(this.name, ctx, this.endec, this.defaultValueFactory.get())
: struct.field(this.name, ctx, this.endec);
}

public static final class Flat<S, F> extends StructField<S, F> {

public Flat(StructEndec<F> endec, Function<S, F> getter) {
super("", endec, getter, (Supplier<F>) null);
}

private StructEndec<F> endec() {
return (StructEndec<F>) this.endec;
}

@Override
public void encodeField(SerializationContext ctx, Serializer.Struct struct, S instance) {
this.endec().encodeStruct(ctx, struct, this.getter.apply(instance));
}

@Override
public F decodeField(SerializationContext ctx, Deserializer.Struct struct) {
return this.endec().decodeStruct(ctx, struct);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.wispforest.owo.serialization.format.edm;

import com.google.common.collect.ImmutableSet;
import io.wispforest.owo.serialization.*;
import io.wispforest.owo.serialization.util.RecursiveSerializer;
import org.apache.commons.lang3.mutable.MutableObject;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -83,10 +86,13 @@ public void writeBytes(SerializationContext ctx, byte[] bytes) {

@Override
public <V> void writeOptional(SerializationContext ctx, Endec<V> endec, Optional<V> optional) {
var result = new MutableObject<@Nullable EdmElement<?>>();
this.frame(encoded -> {
optional.ifPresent(v -> endec.encode(ctx, this, v));
this.consume(EdmElement.wrapOptional(Optional.ofNullable(encoded.get())));
result.setValue(encoded.get());
}, false);

this.consume(EdmElement.wrapOptional(Optional.ofNullable(result.getValue())));
}

// ---
Expand Down
4 changes: 2 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 @@ -182,7 +182,7 @@ public interface Algorithm {
final var padding = container.padding.get();
final var childSpace = container.calculateChildSpace(container.space);

MountingHelper.inflateWithExpand(container.children, childSpace, false);
MountingHelper.inflateWithExpand(container.children, childSpace, false, container.gap());
var mountState = MountingHelper.mountEarly(container::mountChild, container.children, child -> {
layout.add(child);

Expand Down Expand Up @@ -229,7 +229,7 @@ public interface Algorithm {
final var padding = container.padding.get();
final var childSpace = container.calculateChildSpace(container.space);

MountingHelper.inflateWithExpand(container.children, childSpace, true);
MountingHelper.inflateWithExpand(container.children, childSpace, true, container.gap());
var mountState = MountingHelper.mountEarly(container::mountChild, container.children, child -> {
layout.add(child);

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/io/wispforest/owo/ui/util/MountingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ protected MountingHelper(ComponentSink sink, List<Component> children) {
this.lateChildren = children;
}

/**
* @deprecated This method does not account for a possible gap inserted
* between components by the layout algorithm and always assumes all components being
* placed right next to each other. Use {@link #inflateWithExpand(List, Size, boolean, int)} instead
*/
@Deprecated(forRemoval = true)
public static void inflateWithExpand(List<Component> children, Size childSpace, boolean vertical) {
inflateWithExpand(children, childSpace, vertical, 0);
}

public static void inflateWithExpand(List<Component> children, Size childSpace, boolean vertical, int gap) {
var nonExpandChildren = new ArrayList<Component>();

children.forEach(child -> {
Expand All @@ -32,21 +42,22 @@ public static void inflateWithExpand(List<Component> children, Size childSpace,
}
});

//noinspection ExtractMethodRecommender
Size remainingSpace;
if (vertical) {
int height = childSpace.height();
for (var nonExpandChild : nonExpandChildren) {
height -= nonExpandChild.fullSize().height();
}

height -= gap * Math.max(children.size() - 1, 0);
remainingSpace = Size.of(childSpace.width(), Math.max(0, height));
} else {
int width = childSpace.width();
for (var nonExpandChild : nonExpandChildren) {
width -= nonExpandChild.fullSize().width();
}

width -= gap * Math.max(children.size() - 1, 0);
remainingSpace = Size.of(Math.max(0, width), childSpace.height());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ protected void build(FlowLayout rootComponent) {
@Override
protected void build(FlowLayout rootComponent) {}
})));
panel.child(Components.button(Text.literal("expand gap test"), button -> this.client.setScreen(new BaseUIModelScreen<>(FlowLayout.class, new Identifier("uwu", "expand_gap_test")) {
@Override
protected void build(FlowLayout rootComponent) {}
})));
panel.child(Components.button(Text.literal("smolnite"), button -> this.client.setScreen(new SmolComponentTestScreen())));
panel.child(Components.button(Text.literal("sizenite"), button -> this.client.setScreen(new SizingTestScreen())));

Expand Down
86 changes: 86 additions & 0 deletions src/testmod/resources/assets/uwu/owo_ui/expand_gap_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<owo-ui xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../owo-ui.xsd">
<components>
<flow-layout direction="vertical">
<children>
<!-- expand + gap, bug -->
<flow-layout direction="horizontal">
<children>
<flow-layout direction="vertical">
<children>
<!-- ... -->
</children>

<sizing>
<vertical method="fixed">20</vertical>
<horizontal method="fixed">20</horizontal>
</sizing>

<surface>
<panel/>
</surface>
</flow-layout>

<flow-layout direction="vertical">
<children>
<!-- ... -->
</children>

<sizing>
<vertical method="fixed">20</vertical>
<horizontal method="expand">100</horizontal>
</sizing>

<surface>
<panel/>
</surface>
</flow-layout>
</children>

<gap>10</gap>
</flow-layout>

<!-- margins -->
<flow-layout direction="horizontal">
<children>
<flow-layout direction="vertical">
<children>
<!-- ... -->
</children>

<sizing>
<vertical method="fixed">20</vertical>
<horizontal method="fixed">20</horizontal>
</sizing>

<surface>
<panel/>
</surface>

<margins>
<right>10</right>
</margins>
</flow-layout>

<flow-layout direction="vertical">
<children>
<!-- ... -->
</children>

<sizing>
<vertical method="fixed">20</vertical>
<horizontal method="expand">100</horizontal>
</sizing>

<surface>
<panel/>
</surface>
</flow-layout>
</children>
</flow-layout>
</children>

<gap>5</gap>
</flow-layout>
</components>
</owo-ui>

0 comments on commit 1adfd6f

Please sign in to comment.