Skip to content

Commit

Permalink
Resizing algorithm improvements (CleanroomMC#30)
Browse files Browse the repository at this point in the history
* improve resizing algorithm

* clean up

* move resize method to WidgetTree

* more clean up

* fix

* add some stuff to todo

* fix row/column alignment issues

* fix cyclebutton widget theme

* only call onSlotChanged when the slot actually changed

* fix panel sync handler
  • Loading branch information
brachy84 authored Sep 14, 2023
1 parent b8f141f commit b1fe2da
Show file tree
Hide file tree
Showing 35 changed files with 634 additions and 253 deletions.
11 changes: 10 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@
- [ ] properly add modularui elements to any gui screen
- [ ] a gui to create guis
- [ ] sounds ?
- [ ] change color of themes to IDrawable
- [x] change color of themes to IDrawable

## Internal changes
- [ ] Clean up IInterpolation
- [ ] Fix shift clicking to slots which can not be seen
- [ ] Put axis alignments as inner class into Alignment

## HoloUI
- [ ] Fix/Remove current rotations
- [ ] Implement interactions
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ dependencies {
annotationProcessor('com.google.guava:guava:24.1.1-jre')
annotationProcessor('com.google.code.gson:gson:2.8.6')
annotationProcessor(mixin) { transitive = false }

// Example deobf dependency
// compileOnly rfg.deobf("curse.maven:endercore-231868:2972849:")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
public interface ILayoutWidget {

/**
* Called when this should re-layout it's children.
* Called after the children tried to calculate their size.
* Might be called multiple times.
*/
void layoutWidgets();

/**
* Can be used for some extra calculations
* TODO: Really needed?
* Called after post calculation of this widget.
* Might be called multiple times.
* The last call guarantees, that this widget is fully calculated.
*/
default void postLayoutWidgets() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.widget.sizer.Area;
import com.cleanroommc.modularui.widget.sizer.IResizeable;
import org.jetbrains.annotations.ApiStatus;

/**
* Base interface for gui elements. For example widgets.
Expand All @@ -21,6 +23,8 @@ public interface IGuiElement {
*/
IGuiElement getParent();

IResizeable resizer();

/**
* @return the area this element occupies
*/
Expand Down Expand Up @@ -97,11 +101,6 @@ default boolean isBelowMouse() {
*/
boolean isEnabled();

/**
* Called when the screen resizes. Handles the positioning and sizing of this element.
*/
void resize();

/**
* @return default width if it can't be calculated
*/
Expand Down
45 changes: 19 additions & 26 deletions src/main/java/com/cleanroommc/modularui/api/widget/IWidget.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cleanroommc.modularui.api.widget;

import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.layout.ILayoutWidget;
import com.cleanroommc.modularui.api.layout.IViewportStack;
import com.cleanroommc.modularui.drawable.Stencil;
import com.cleanroommc.modularui.screen.ModularPanel;
Expand All @@ -11,7 +10,6 @@
import com.cleanroommc.modularui.widget.sizer.Flex;
import com.cleanroommc.modularui.widget.sizer.IResizeable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -130,6 +128,15 @@ default boolean hasChildren() {

void setEnabled(boolean enabled);

default boolean areAncestorsEnabled() {
IWidget parent = this;
while (parent != null) {
if (!parent.isEnabled()) return false;
parent = parent.getParent();
}
return true;
}

default boolean canBeSeen(IViewportStack stack) {
return Stencil.isInsideScissorArea(getArea(), stack);
}
Expand Down Expand Up @@ -162,7 +169,6 @@ default boolean canHover() {
/**
* @return resizer of this widget
*/
@Nullable
IResizeable resizer();

/**
Expand All @@ -173,33 +179,20 @@ default boolean canHover() {
void resizer(IResizeable resizer);

/**
* Called when the screen resizes. Handles the positioning and sizing of this element.
* Called before a widget is resized.
*/
@Override
default void resize() {
IResizeable resizer = resizer();
if (resizer != null) {
if (resizer.isSkip()) return;
resizer.apply(this);
}

if (hasChildren()) {
getChildren().forEach(IWidget::resize);
}

if (this instanceof ILayoutWidget) {
((ILayoutWidget) this).layoutWidgets();
}

if (resizer != null) {
resizer.postApply(this);
}
default void beforeResize() {
}

if (this instanceof ILayoutWidget) {
((ILayoutWidget) this).postLayoutWidgets();
}
/**
* Called after a widget is fully resized.
*/
default void onResized() {
}

/**
* Called after the full widget tree is resized and the absolute positions are calculated.
*/
default void postResize() {
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/cleanroommc/modularui/holoui/HoloUI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.holoui;

import com.cleanroommc.modularui.screen.JeiSettings;
import com.cleanroommc.modularui.screen.ModularScreen;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -78,6 +79,9 @@ public Builder plane(Plane3D plane) {
}

public void open(ModularScreen screen) {
JeiSettings jeiSettings = new JeiSettings();
jeiSettings.disableJei();
screen.getContext().setJeiSettings(jeiSettings);
HoloScreenEntity holoScreenEntity = new HoloScreenEntity(Minecraft.getMinecraft().world, this.plane3D);
holoScreenEntity.setPosition(this.x, this.y, this.z);
holoScreenEntity.setScreen(screen);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.manager;

import com.cleanroommc.modularui.screen.JeiSettings;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
Expand All @@ -15,15 +16,17 @@ public class GuiCreationContext {
private final World world;
private final int x, y, z;
private final EnumHand hand;
private final JeiSettings jeiSettings;

@ApiStatus.Internal
public GuiCreationContext(EntityPlayer player, World world, int x, int y, int z, EnumHand hand) {
public GuiCreationContext(EntityPlayer player, World world, int x, int y, int z, EnumHand hand, JeiSettings jeiSettings) {
this.player = player;
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.hand = hand;
this.jeiSettings = jeiSettings;
}

public EntityPlayer getPlayer() {
Expand Down Expand Up @@ -78,6 +81,10 @@ public BlockPos getBlockPos() {
return new BlockPos(this.x, this.y, this.z);
}

public JeiSettings getJeiSettings() {
return this.jeiSettings;
}

public IBlockState getBlockState() {
BlockPos.PooledMutableBlockPos pos = BlockPos.PooledMutableBlockPos.retain(this.x, this.y, this.z);
IBlockState blockState = this.world.getBlockState(pos);
Expand All @@ -94,7 +101,7 @@ public TileEntity getTileEntity() {

public GuiCreationContext with(EnumHand hand) {
if (this.hand != hand) {
return new GuiCreationContext(this.player, this.world, this.x, this.y, this.z, hand);
return new GuiCreationContext(this.player, this.world, this.x, this.y, this.z, hand, this.jeiSettings);
}
return this;
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/cleanroommc/modularui/manager/GuiInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.Objects;
import java.util.function.BiFunction;

public class GuiInfo {
Expand Down Expand Up @@ -49,17 +50,15 @@ public void open(EntityPlayer player, World world, int x, int y, int z) {
}

public ModularPanel createCommonGui(GuiCreationContext context, GuiSyncManager guiSyncManager) {
ModularPanel panel = this.mainPanelCreator.apply(context, guiSyncManager);
ModularPanel panel = Objects.requireNonNull(this.mainPanelCreator.apply(context, guiSyncManager), "The main panel must not be null!");
WidgetTree.collectSyncValues(guiSyncManager, panel);
return panel;
}

@SideOnly(Side.CLIENT)
public ModularScreen createClientGui(GuiCreationContext context, ModularPanel panel) {
Object screen = this.clientGuiCreator.apply(context, panel);
if (!(screen instanceof ModularScreen)) {
throw new IllegalStateException("Client screen must be an instance of ModularScreen");
}
Object screen = Objects.requireNonNull(this.clientGuiCreator.apply(context, panel), "The modular screen must not be null!");
if (!(screen instanceof ModularScreen)) throw new IllegalStateException("Client screen must be an instance of ModularScreen");
return (ModularScreen) screen;
}

Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/cleanroommc/modularui/manager/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.screen.GuiScreenWrapper;
import com.cleanroommc.modularui.screen.ModularContainer;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.screen.*;
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -46,7 +43,7 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int
GuiInfo info = this.guiInfos.get(ID);
if (info == null) return null;
GuiSyncManager guiSyncManager = new GuiSyncManager(player);
info.createCommonGui(new GuiCreationContext(player, world, x, y, z, EnumHand.MAIN_HAND), guiSyncManager);
info.createCommonGui(new GuiCreationContext(player, world, x, y, z, EnumHand.MAIN_HAND, new JeiSettings()), guiSyncManager);
return new ModularContainer(guiSyncManager);
}

Expand All @@ -56,8 +53,10 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int
GuiInfo info = this.guiInfos.get(ID);
if (info == null) return null;
GuiSyncManager guiSyncManager = new GuiSyncManager(player);
GuiCreationContext context = new GuiCreationContext(player, world, x, y, z, EnumHand.MAIN_HAND);
GuiCreationContext context = new GuiCreationContext(player, world, x, y, z, EnumHand.MAIN_HAND, new JeiSettings());
ModularPanel panel = info.createCommonGui(context, guiSyncManager);
return new GuiScreenWrapper(new ModularContainer(guiSyncManager), info.createClientGui(context, panel));
ModularScreen modularScreen = info.createClientGui(context, panel);
modularScreen.getContext().setJeiSettings(context.getJeiSettings());
return new GuiScreenWrapper(new ModularContainer(guiSyncManager), modularScreen);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ public ResourceLocation getResourceLocation() {
return new ResourceLocation(this.owner, this.name);
}

public GuiContext getContext() {
return this.context;
}

public WindowManager getWindowManager() {
return this.windowManager;
}
Expand Down Expand Up @@ -467,11 +471,6 @@ public ModularScreen useTheme(String theme) {
return this;
}

public ModularScreen useJeiSettings(JeiSettings jeiSettings) {
this.context.setJeiSettings(jeiSettings);
return this;
}

public enum UpOrDown {
UP(1), DOWN(-1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class GuiContext extends GuiViewportStack {

public List<Consumer<GuiContext>> postRenderCallbacks = new ArrayList<>();

private JeiSettings jeiSettings = new JeiSettings();
private JeiSettings jeiSettings;

public GuiContext(ModularScreen screen) {
this.screen = screen;
Expand Down Expand Up @@ -432,10 +432,17 @@ public ITheme getTheme() {
}

public JeiSettings getJeiSettings() {
if (this.jeiSettings == null) {
throw new IllegalStateException("The screen is not yet initialised!");
}
return this.jeiSettings;
}

@ApiStatus.Internal
public void setJeiSettings(JeiSettings jeiSettings) {
if (this.jeiSettings != null) {
throw new IllegalStateException("Tried to set jei settings twice");
}
this.jeiSettings = jeiSettings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static void onItemUse(PlayerInteractEvent.RightClickItem event) {
//GuiManager.openClientUI(Minecraft.getMinecraft().player, new TestGui());
HoloUI.builder()
.inFrontOf(Minecraft.getMinecraft().player, 5, false)
.screenScale(0.5f)
.open(new TestGui());
}
}
Expand Down
Loading

0 comments on commit b1fe2da

Please sign in to comment.