Skip to content

Commit

Permalink
Add initial support for a component based editor
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Dec 26, 2023
1 parent dcdf7d6 commit 7d6da4e
Show file tree
Hide file tree
Showing 30 changed files with 501 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ interface IMinecraft {
fun loadResourceLocation(resourceLocation: MyResourceLocation): InputStream
fun getLogger(label: String): MCLogger

val mouseX: Int
val mouseY: Int
val isDevelopmentEnvironment: Boolean
val defaultFontRenderer: IFontRenderer
val keyboardConstants: IKeyboardConstants
val scaledWidth: Int
val scaledHeight: Int

companion object {
@JvmField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ default void drawDarkRect(int x, int y, int width, int height) {
void refreshScissor();

void disableScissor();

default IMinecraft getMinecraft() {
return IMinecraft.instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,21 @@ public <T> T foldChildren(T initial, @NotNull BiFunction<@NotNull GuiComponent,
* Called by the parent renderer.
*
* @param context the context in which this
* @return {@code true} if this event was handled, {@code false} if this event should be handled by another element
*/
public void mouseEvent(@NotNull MouseEvent mouseEvent,@NotNull GuiImmediateContext context) {
public boolean mouseEvent(@NotNull MouseEvent mouseEvent,@NotNull GuiImmediateContext context) {
return false;
}

/**
* Called by the parent renderer.
* <p>N.B.: this method is called regardless of whether this element is focused or not.</p>
*
* @param context the context in which this
* @return {@code true} if this event was handled, {@code false} if this event should be handled by another element
*/
public void keyboardEvent(@NotNull KeyboardEvent event,@NotNull GuiImmediateContext context) {
public boolean keyboardEvent(@NotNull KeyboardEvent event,@NotNull GuiImmediateContext context) {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ data class GuiImmediateContext(
)
}

/**
* Construct a new context, which has not been translated, but possible smaller if the arguments demand so.
*
* @param maxWidth max width of the new context. this argument will be ignored if it is larger than the current width
* @param maxHeight max height of the new context. this argument will be ignored if it is larger than the current height
*/
fun limitSize(maxWidth: Int, maxHeight: Int): GuiImmediateContext {
return translated(0, 0, minOf(width, maxWidth), minOf(height, maxHeight))
}

fun scaled(scale: Float): GuiImmediateContext {
return GuiImmediateContext(
renderContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,26 @@ public void render(GuiImmediateContext context) {
}

@Override
public void mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
foldWithContext(context, (guiComponent, guiImmediateContext) ->
guiComponent.mouseEvent(mouseEvent, guiImmediateContext));
public boolean mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
// TODO: early return
boolean[] wasHandled = new boolean[1];
foldWithContext(context, (guiComponent, guiImmediateContext) -> {
if (guiComponent.mouseEvent(mouseEvent, guiImmediateContext)) {
wasHandled[0] = true;
}
});
return wasHandled[0];
}

@Override
public void keyboardEvent(KeyboardEvent keyboardEvent, GuiImmediateContext context) {
foldWithContext(context, (guiComponent, guiImmediateContext) ->
guiComponent.keyboardEvent(keyboardEvent, guiImmediateContext));
public boolean keyboardEvent(KeyboardEvent keyboardEvent, GuiImmediateContext context) {
// TODO: early return
boolean[] wasHandled = new boolean[1];
foldWithContext(context, (guiComponent, guiImmediateContext) -> {
if (guiComponent.keyboardEvent(keyboardEvent, guiImmediateContext)) {
wasHandled[0] = true;
}
});
return wasHandled[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ import io.github.moulberry.moulconfig.gui.MouseEvent
import io.github.moulberry.moulconfig.gui.MouseEvent.Click

class ButtonComponent(element: GuiComponent, insets: Int, val onClick: Runnable) : PanelComponent(element, insets) {
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext) {
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
if (context.isHovered && mouseEvent is Click) {
val (mouseButton, mouseState) = mouseEvent
if (mouseState && mouseButton == 0) onClick.run()
if (mouseState && mouseButton == 0) {
onClick.run()
return true
}
}
return false
}

override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext) {
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
if (isFocused && event is KeyboardEvent.KeyPressed &&
event.pressed && event.keycode == KeyboardConstants.enter
) {
onClick.run()
} else super.keyboardEvent(event, context)
return true
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void render(GuiImmediateContext context) {
}

@Override
public void keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
child.keyboardEvent(event, getChildContext(context));
public boolean keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
return child.keyboardEvent(event, getChildContext(context));
}

@Override
public void mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
child.mouseEvent(mouseEvent, getChildContext(context));
public boolean mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
return child.mouseEvent(mouseEvent, getChildContext(context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CollapsibleComponent(
}
}

override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext) {
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
val barHeight = maxOf(title.get().height, fr.height)

if (mouseEvent is MouseEvent.Click && context.translated(
Expand All @@ -84,33 +84,31 @@ class CollapsibleComponent(
) {
if (mouseEvent.mouseState)
collapsedState.set(!collapsedState.get())
return
return true
}

title.get().mouseEvent(
return title.get().mouseEvent(
mouseEvent, context.translated(
iconWidth,
0,
context.width - iconWidth,
barHeight
)
)
body.get().mouseEvent(
) || body.get().mouseEvent(
mouseEvent, context.translated(0, barHeight, context.width, context.height - barHeight)
)
}

override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext) {
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
val barHeight = maxOf(title.get().height, fr.height)
title.get().keyboardEvent(
return title.get().keyboardEvent(
event, context.translated(
iconWidth,
0,
context.width - iconWidth,
barHeight
)
)
body.get().keyboardEvent(
) || body.get().keyboardEvent(
event, context.translated(0, barHeight, context.width, context.height - barHeight)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,26 @@ public void render(GuiImmediateContext context) {
}

@Override
public void mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
foldWithContext(context, (component, context1) -> component.mouseEvent(mouseEvent, context1));
public boolean mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
// TODO: early return
boolean[] wasHandled = new boolean[1];
foldWithContext(context, (guiComponent, guiImmediateContext) -> {
if (guiComponent.mouseEvent(mouseEvent, guiImmediateContext)) {
wasHandled[0] = true;
}
});
return wasHandled[0];
}

@Override
public void keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
foldWithContext(context, (guiComponent, guiImmediateContext) ->
guiComponent.keyboardEvent(event, guiImmediateContext));
public boolean keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
// TODO: early return
boolean[] wasHandled = new boolean[1];
foldWithContext(context, (guiComponent, guiImmediateContext) -> {
if (guiComponent.keyboardEvent(event, guiImmediateContext)) {
wasHandled[0] = true;
}
});
return wasHandled[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class HoverComponent(

}

override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext) {
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
return child.mouseEvent(mouseEvent, context)
}

override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext) {
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
return child.keyboardEvent(event, context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ open class IndirectComponent(
component.get().render(context)
}

override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext) {
component.get().keyboardEvent(event, context)
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
return component.get().keyboardEvent(event, context)
}

override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext) {
component.get().mouseEvent(mouseEvent, context)
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
return component.get().mouseEvent(mouseEvent, context)
}

override fun <T : Any?> foldChildren(initial: T, visitor: BiFunction<GuiComponent, T, T>): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import io.github.moulberry.moulconfig.gui.GuiImmediateContext;
import io.github.moulberry.moulconfig.gui.KeyboardEvent;
import io.github.moulberry.moulconfig.gui.MouseEvent;
import lombok.Getter;

import java.util.function.BiFunction;

/**
* Renders an element with a floating rect.
*/
@Getter
public class PanelComponent extends GuiComponent {
private final GuiComponent element;
private final int insets;
Expand Down Expand Up @@ -57,35 +59,31 @@ public int getWidth() {
return element.getWidth() + insets * 2;
}

public int getInsets() {
return insets;
}

@Override
public int getHeight() {
return element.getHeight() + insets * 2 + 2;
}

GuiImmediateContext getChildContext(GuiImmediateContext context) {
protected GuiImmediateContext getChildContext(GuiImmediateContext context) {
return context.translated(insets, insets, element.getWidth(), element.getHeight());
}

@Override
public void render(GuiImmediateContext context) {
context.getRenderContext().pushMatrix();
context.getRenderContext().drawDarkRect(0, 0, getWidth(), getHeight() - 2);
context.getRenderContext().drawDarkRect(0, 0, context.getWidth(), context.getHeight() - 2);
context.getRenderContext().translate(insets, insets, 0);
element.render(getChildContext(context));
context.getRenderContext().popMatrix();
}

@Override
public void keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
element.keyboardEvent(event, getChildContext(context));
public boolean keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
return element.keyboardEvent(event, getChildContext(context));
}

@Override
public void mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
element.mouseEvent(mouseEvent, getChildContext(context));
public boolean mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
return element.mouseEvent(mouseEvent, getChildContext(context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,27 @@ public void render(GuiImmediateContext context) {
}

@Override
public void mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
foldWithContext(context, (guiComponent, guiImmediateContext) -> guiComponent.mouseEvent(mouseEvent, guiImmediateContext));
public boolean mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
// TODO: early return
boolean[] wasHandled = new boolean[1];
foldWithContext(context, (guiComponent, guiImmediateContext) -> {
if (guiComponent.mouseEvent(mouseEvent, guiImmediateContext)) {
wasHandled[0] = true;
}
});
return wasHandled[0];
}

@Override
public void keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
foldWithContext(context, (guiComponent, guiImmediateContext) -> guiComponent.keyboardEvent(event, guiImmediateContext));
public boolean keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
// TODO: early return
boolean[] wasHandled = new boolean[1];
foldWithContext(context, (guiComponent, guiImmediateContext) -> {
if (guiComponent.keyboardEvent(event, guiImmediateContext)) {
wasHandled[0] = true;
}
});
return wasHandled[0];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class ScaleComponent(
context.renderContext.popMatrix()
}

override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext) {
child.keyboardEvent(event, context.scaled(scaleFactor.get()))
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
return child.keyboardEvent(event, context.scaled(scaleFactor.get()))
}

override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext) {
super.mouseEvent(mouseEvent, context.scaled(scaleFactor.get()))
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
return child.mouseEvent(mouseEvent, context.scaled(scaleFactor.get()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ public void render(GuiImmediateContext context) {
}

@Override
public void keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
child.keyboardEvent(event, context.translatedNonRendering(0, -scrollOffset, width, height));
public boolean keyboardEvent(KeyboardEvent event, GuiImmediateContext context) {
return child.keyboardEvent(event, context.translatedNonRendering(0, -scrollOffset, width, height));
}

@Override
public void mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
public boolean mouseEvent(MouseEvent mouseEvent, GuiImmediateContext context) {
if (child.mouseEvent(mouseEvent, context.translatedNonRendering(0, -scrollOffset, width, height))) {
return true;
}
if (context.isHovered() && mouseEvent instanceof MouseEvent.Scroll) {
scrollOffset = (int) Math.max(0, Math.min(scrollOffset - (((MouseEvent.Scroll) mouseEvent).getDWheel()), child.getHeight() - height));
return true;
}
child.mouseEvent(mouseEvent, context.translatedNonRendering(0, -scrollOffset, width, height));
return false;
}
}
Loading

0 comments on commit 7d6da4e

Please sign in to comment.