Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.cleanroommc.modularui.api.widget;

import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

public interface IParentWidget<I extends IWidget, W extends IParentWidget<I, W>> {

W getThis();

boolean addChild(I child, int index);

default W child(int index, I child) {
if (!addChild(child, index)) {
throw new IllegalStateException("Failed to add child");
}
return getThis();
}

default W child(I child) {
if (!addChild(child, -1)) {
throw new IllegalStateException("Failed to add child");
}
return getThis();
}

default W childIf(boolean condition, I child) {
if (condition) return child(child);
return getThis();
}

default W childIf(BooleanSupplier condition, I child) {
if (condition.getAsBoolean()) return child(child);
return getThis();
}

default W childIf(boolean condition, Supplier<I> child) {
if (condition) return child(child.get());
return getThis();
}

default W childIf(BooleanSupplier condition, Supplier<I> child) {
if (condition.getAsBoolean()) return child(child.get());
return getThis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @param <T>
*/
public interface IValueWidget<T> {
public interface IValueWidget<T> extends IWidget {

/**
* @return stored value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ default boolean canHover() {
return true;
}

default boolean canClickThrough() {
return true;
}

/**
* Marks tooltip for this widget as dirty.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ default Result onKeyTapped(char typedChar, int keyCode) {
}

/**
* Called when this widget is focused or when the mouse is above this widget
* Called when this widget is focused or when the mouse is above this widget.
* This method should return true if it can scroll at all and not if it scrolled right now.
* If this scroll view scrolled to the end and this returns false, the scroll will get passed through another scroll view below this.
*
* @param scrollDirection up or down
* @param amount usually irrelevant
* @return if other widgets should get called too
* @param amount amount scrolled by (usually irrelevant)
* @return true if this widget can be scrolled at all
*/
default boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private static boolean handleMouseInput(int button, @Nullable ModularScreen muiS
}
acc.setEventButton(button);
acc.setLastMouseEvent(Minecraft.getSystemTime());
if (muiScreen != null && muiScreen.handleDraggableInput(button, true)) return true;
return doAction(muiScreen, ms -> ms.onMousePressed(button));
}
if (button != -1) {
Expand All @@ -204,6 +205,7 @@ private static boolean handleMouseInput(int button, @Nullable ModularScreen muiS
}
}
acc.setEventButton(-1);
if (muiScreen != null && muiScreen.handleDraggableInput(button, false)) return true;
return doAction(muiScreen, ms -> ms.onMouseRelease(button));
}
if (acc.getEventButton() != -1 && acc.getLastMouseEvent() > 0L) {
Expand Down Expand Up @@ -499,7 +501,7 @@ public static void drawDebugScreen(@Nullable ModularScreen muiScreen, @Nullable
boolean allowShiftTransfer = slotGroup != null && slotGroup.allowShiftTransfer();
GuiDraw.drawText("Shift-Click Priority: " + (allowShiftTransfer ? slotGroup.getShiftClickPriority() : "DISABLED"), 5, lineY, 1, color, false);
}
} else if(hovered instanceof RichTextWidget richTextWidget) {
} else if (hovered instanceof RichTextWidget richTextWidget) {
drawSegmentLine(lineY -= 4, color);
lineY -= 10;
Object hoveredElement = richTextWidget.getHoveredElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ public boolean onMousePressed(int mouseButton) {
boolean result = false;

if (this.hovering.isEmpty()) {
// no element is hovered -> try close panel
if (closeOnOutOfBoundsClick()) {
animateClose();
result = true;
Expand All @@ -298,6 +299,7 @@ public boolean onMousePressed(int mouseButton) {
loop:
for (LocatedWidget widget : this.hovering) {
widget.applyMatrix(getContext());
// click widget and see how it reacts
if (widget.getElement() instanceof Interactable interactable) {
switch (interactable.onMousePressed(mouseButton)) {
case IGNORE:
Expand Down Expand Up @@ -327,14 +329,16 @@ public boolean onMousePressed(int mouseButton) {
}
}
}
// see if widget can be dragged
if (getContext().onHoveredClick(mouseButton, widget)) {
pressed = LocatedWidget.EMPTY;
result = true;
widget.unapplyMatrix(getContext());
break;
}
widget.unapplyMatrix(getContext());
if (widget.getElement().canHover()) {
// see if widgets below this can be interacted with
if (!widget.getElement().canClickThrough()) {
result = true;
break;
}
Expand Down Expand Up @@ -440,7 +444,7 @@ public boolean onKeyPressed(char typedChar, int keyCode) {
}
widget.unapplyMatrix(getContext());
}
if (widget.getElement().canHover()) break;
if (!widget.getElement().canClickThrough()) break;
}
if (!this.keyboard.held) {
this.keyboard.lastPressed = pressed;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/cleanroommc/modularui/screen/ModularScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ public void drawForeground(float partialTicks) {
GlStateManager.enableAlpha();
}

public boolean handleDraggableInput(int button, boolean pressed) {
if (this.context.hasDraggable()) {
if (pressed) {
this.context.onMousePressed(button);
} else {
this.context.onMouseReleased(button);
}
return true;
}
return false;
}

public boolean onMousePressed(int mouseButton) {
for (IGuiAction.MousePressed action : getGuiActionListeners(IGuiAction.MousePressed.class)) {
action.press(mouseButton);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.cleanroommc.modularui.screen;

import com.cleanroommc.modularui.api.IPanelHandler;
import com.cleanroommc.modularui.api.MCHelper;
import com.cleanroommc.modularui.widget.WidgetTree;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;

import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void openPanel() {
this.screen = this.parent.getScreen();
}
if (this.panel == null) {
this.panel = Objects.requireNonNull(this.provider.build(this.screen.getMainPanel(), Minecraft.getMinecraft().player));
this.panel = Objects.requireNonNull(this.provider.build(this.screen.getMainPanel(), MCHelper.getPlayer()));
if (this.panel == this.screen.getMainPanel()) {
throw new IllegalArgumentException("Must not return main panel!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ModularGuiContext extends GuiContext {
public final ModularScreen screen;
private LocatedWidget focusedWidget = LocatedWidget.EMPTY;
@Nullable
private IGuiElement hovered;
private IWidget hovered;
private int timeHovered = 0;
private final HoveredIterable hoveredWidgets;

Expand Down Expand Up @@ -81,7 +81,7 @@ public boolean isHoveredFor(IGuiElement guiElement, int ticks) {
* @return the hovered widget (widget directly below the mouse)
*/
@Nullable
public IGuiElement getHovered() {
public IWidget getHovered() {
return this.hovered;
}

Expand Down Expand Up @@ -310,7 +310,7 @@ public void drawDraggable() {

@ApiStatus.Internal
public void onFrameUpdate() {
IGuiElement hovered = this.screen.getPanelManager().getTopWidget();
IWidget hovered = this.screen.getPanelManager().getTopWidget();
if (hasDraggable() && (this.lastDragX != getAbsMouseX() || this.lastDragY != getAbsMouseY())) {
this.lastDragX = getAbsMouseX();
this.lastDragY = getAbsMouseY();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static void onItemUse(PlayerInteractEvent.RightClickItem event) {
.inFrontOf(Minecraft.getMinecraft().player, 5, false)
.screenScale(0.5f)
.open(new TestGui());*/
ClientGUI.open(new ResizerTest());
//ClientGUI.open(new ResizerTest());
ClientGUI.open(new TestGui());
}
}
}
109 changes: 73 additions & 36 deletions src/main/java/com/cleanroommc/modularui/test/ResizerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,98 @@
import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.drawable.ItemDrawable;
import com.cleanroommc.modularui.drawable.SpriteDrawable;
import com.cleanroommc.modularui.screen.CustomModularScreen;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.utils.GameObjectHelper;
import com.cleanroommc.modularui.utils.SpriteHelper;
import com.cleanroommc.modularui.utils.fakeworld.ArraySchema;
import com.cleanroommc.modularui.utils.fakeworld.ISchema;
import com.cleanroommc.modularui.widget.DraggableWidget;
import com.cleanroommc.modularui.widgets.RichTextWidget;
import com.cleanroommc.modularui.widgets.SchemaWidget;

import com.cleanroommc.modularui.widgets.SortableListWidget;

import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.text.TextFormatting;

import org.jetbrains.annotations.NotNull;

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

public class ResizerTest extends CustomModularScreen {

@Override
public @NotNull ModularPanel buildUI(ModularGuiContext context) {
/*TextureAtlasSprite sprite = SpriteHelper.getSpriteOfBlockState(GameObjectHelper.getBlockState("minecraft", "command_block"), EnumFacing.UP);
return buildSortableListUI(context);
}

public @NotNull ModularPanel buildSpriteUI(ModularGuiContext context) {
TextureAtlasSprite sprite = SpriteHelper.getSpriteOfBlockState(GameObjectHelper.getBlockState("minecraft", "command_block"), EnumFacing.UP);
//SpriteHelper.getSpriteOfItem(new ItemStack(Items.DIAMOND));
return ModularPanel.defaultPanel("main")
.size(150)
.child(new DraggableWidget<>()
.background(new SpriteDrawable(sprite))
.size(20)
.align(Alignment.Center));*/
.center());
}


public @NotNull ModularPanel buildSortableListUI(ModularGuiContext context) {
List<String> things = new ArrayList<>();
for (int i = 0; i < 15; i++) {
things.add("Thing " + i);
}
return ModularPanel.defaultPanel("main")
.padding(7)
.child(new SortableListWidget<String>()
.children(things, thing -> new SortableListWidget.Item<>(thing)
.overlay(IKey.str(thing))));
}

public @NotNull ModularPanel buildRichTextUI(ModularGuiContext context) {
return new ModularPanel("main")
.size(176, 166)
.child(new RichTextWidget()
.sizeRel(1f).margin(7)
.autoUpdate(true)
.textBuilder(text -> text.add("Hello ")
.add(new ItemDrawable(new ItemStack(Blocks.GRASS))
.asIcon()
.asHoverable()
.tooltip(richTooltip -> richTooltip.addFromItem(new ItemStack(Blocks.GRASS))))
.add(", nice to ")
.add(new ItemDrawable(new ItemStack(Items.PORKCHOP))
.asIcon()
.asInteractable()
.onMousePressed(button -> {
ModularUI.LOGGER.info("Pressed Pork");
return true;
}))
.add(" you. ")
.add(TextFormatting.GREEN + "This is a long ")
.add(IKey.str("string").format(TextFormatting.DARK_PURPLE)
.asTextIcon()
.asHoverable()
.addTooltipLine("Text Tooltip"))
.add(" of characters" + TextFormatting.RESET)
.add(" and not numbers as some might think...")
.newLine()
.add("")
.textShadow(false)
));
}

public @NotNull ModularPanel buildWorldSchemeUI(ModularGuiContext context) {
/*TrackedDummyWorld world = new TrackedDummyWorld();
world.addBlock(new BlockPos(0, 0, 0), new BlockInfo(Blocks.DIAMOND_BLOCK.getDefaultState()));
world.addBlock(new BlockPos(0, 1, 0), new BlockInfo(Blocks.BEDROCK.getDefaultState()));
Expand All @@ -43,7 +111,6 @@ public class ResizerTest extends CustomModularScreen {
.isometric(true)
.asIcon().size(140));*/


/*MapSchema world = new MapSchema.Builder()
.add(new BlockPos(0, 0, 0), Blocks.DIAMOND_BLOCK.getDefaultState())
.add(new BlockPos(0, 1, 0), Blocks.BEDROCK.getDefaultState())
Expand All @@ -52,7 +119,7 @@ public class ResizerTest extends CustomModularScreen {
.add(new BlockPos(0, 3, 0), Blocks.BEACON.getDefaultState())
.build();*/

/*ISchema schema = ArraySchema.builder()
ISchema schema = ArraySchema.builder()
.layer("D D", " ", " ", " ")
.layer(" DDD ", " E E ", " ", " ")
.layer(" DDD ", " E ", " G ", " B ")
Expand All @@ -70,37 +137,7 @@ public class ResizerTest extends CustomModularScreen {
.child(new SchemaWidget.LayerButton(schema, 0, 3)
.bottom(1)
.left(1)
.size(16));*/

return new ModularPanel("main")
.size(176, 166)
.child(new RichTextWidget()
.sizeRel(1f).margin(7)
.autoUpdate(true)
.textBuilder(text -> text.add("Hello ")
.add(new ItemDrawable(new ItemStack(Blocks.GRASS))
.asIcon()
.asHoverable()
.tooltip(richTooltip -> richTooltip.addFromItem(new ItemStack(Blocks.GRASS))))
.add(", nice to ")
.add(new ItemDrawable(new ItemStack(Items.PORKCHOP))
.asIcon()
.asInteractable()
.onMousePressed(button -> {
ModularUI.LOGGER.info("Pressed Pork");
return true;
}))
.add(" you. ")
.add(TextFormatting.GREEN + "This is a long ")
.add(IKey.str("string").format(TextFormatting.DARK_PURPLE)
.asTextIcon()
.asHoverable()
.addTooltipLine("Text Tooltip"))
.add(" of characters" + TextFormatting.RESET)
.add(" and not numbers as some might think...")
.newLine()
.add("")
.textShadow(false)
));
.size(16));
return panel;
}
}
Loading