Skip to content

Commit

Permalink
Port GuiElementColour to components
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Apr 7, 2024
1 parent 8487cc9 commit 63821f1
Show file tree
Hide file tree
Showing 24 changed files with 858 additions and 527 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.notenoughupdates.moulconfig.common

import org.jetbrains.annotations.ApiStatus
import java.awt.image.BufferedImage
import java.io.Closeable

/**
* A dynamically loaded texture. Must be destroyed manually.
*/
@ApiStatus.NonExtendable
abstract class DynamicTextureReference : Closeable {
/**
* An opaque reference to this dynamic texture. Can be used with [RenderContext.bindTexture].
*/
abstract val identifier: MyResourceLocation

/**
* Destroy this texture. Using [identifier] after calling this will cause issues.
*/
fun destroy() {
if (!wasDestroyed)
doDestroy()
wasDestroyed = true
}

abstract fun update(bufferedImage: BufferedImage)

protected abstract fun doDestroy()

private var wasDestroyed = false

override fun close() {
destroy()
}

protected fun finalize() {
if (!wasDestroyed)
IMinecraft.instance.getLogger("DynamicTextureReference").warn("Dangling DynamicTextureReference")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ interface IKeyboardConstants {
val keyC: Int
val keyX: Int
val keyV: Int
val keyN: Int
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package io.github.notenoughupdates.moulconfig.common

import io.github.notenoughupdates.moulconfig.internal.MCLogger
import org.jetbrains.annotations.ApiStatus
import java.io.InputStream
import java.util.*

/**
* Not for manual implementation. This should be implemented by the corresponding platform.
* @see IMinecraft.instance
*/
@ApiStatus.NonExtendable
interface IMinecraft {
@Deprecated("Prefer using .bindTexture from RenderContext")
fun bindTexture(resourceLocation: MyResourceLocation)
fun loadResourceLocation(resourceLocation: MyResourceLocation): InputStream
fun getLogger(label: String): MCLogger

val mouseX: Int
val mouseY: Int
val mouseXHF: Double
val mouseYHF: Double
val isDevelopmentEnvironment: Boolean
val defaultFontRenderer: IFontRenderer
val keyboardConstants: IKeyboardConstants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.image.BufferedImage;
import java.util.List;

public interface RenderContext {
Expand Down Expand Up @@ -59,6 +60,14 @@ default void drawStringCenteredScaledMaxWidth(

void enableDepth();


/**
* Dynamically load a buffered image into a minecraft bindable texture. The returned resource location must be destroyed.
*
* @return a resource location that can be used with {@link #bindTexture}
*/
@NotNull DynamicTextureReference generateDynamicTexture(@NotNull BufferedImage image);

default void drawVerticalLine(int x, int startY, int endY, int color) {
if (startY > endY) {
int temp = startY;
Expand Down Expand Up @@ -97,7 +106,7 @@ default void drawOpenCloseTriangle(boolean isOpen, float x, float y, float width
}
}

int drawString(IFontRenderer fontRenderer, String text, int x, int y, int color, boolean shadow);
int drawString(@NotNull IFontRenderer fontRenderer, @NotNull String text, int x, int y, int color, boolean shadow);

void drawColoredRect(float left, float top, float right, float bottom, int color);

Expand All @@ -109,7 +118,25 @@ default void drawTexturedRect(float x, float y, float width, float height) {

void drawTexturedRect(float x, float y, float width, float height, float u1, float v1, float u2, float v2);

default void drawNinePatch(NinePatch<MyResourceLocation> patch, float x, float y, int width, int height) {

enum TextureFilter {
/**
* Smoothly interpolate between pixels
*/
LINEAR,
/**
* Do not interpolate between pixels (pixelated upscaling).
*/
NEAREST;
}

/**
* Set the texture min and mag filter which dictate how an image is upscaled / interpolated.
* Affects the {@link #bindTexture currently bound texture}.
*/
void setTextureMinMagFilter(@NotNull TextureFilter filter);

default void drawNinePatch(@NotNull NinePatch<@NotNull MyResourceLocation> patch, float x, float y, int width, int height) {
pushMatrix();
translate(x, y, 0);
patch.draw(NinePatchRenderer.INSTANCE, this, width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import io.github.notenoughupdates.moulconfig.common.RenderContext
/**
* A context containing the constraints of a gui elements, as well as the state of the user interface, relative to that gui element.
*/
data class GuiImmediateContext(
data class GuiImmediateContext constructor(
val renderContext: RenderContext,

/**
Expand Down Expand Up @@ -66,6 +66,16 @@ data class GuiImmediateContext(
* The position of the mouse, relative to the root element.
*/
val absoluteMouseY: Int,

/**
* The position of the mouse, relative to this gui element in as high of a resolution as possible.
*/
val mouseXHF: Float,

/**
* The position of the mouse, relative to this gui element in as high of a resolution as possible.
*/
val mouseYHF: Float,
) {
val isHovered: Boolean
/**
Expand All @@ -85,7 +95,8 @@ data class GuiImmediateContext(
renderContext,
renderOffsetX - xBleed, renderOffsetY - yBleed, width + 2 * xBleed, height + 2 * yBleed,
mouseX + xBleed, mouseY + yBleed,
absoluteMouseX, absoluteMouseY
absoluteMouseX, absoluteMouseY,
mouseXHF, mouseYHF
)
}

Expand All @@ -107,7 +118,9 @@ data class GuiImmediateContext(
mouseX - xOffset,
mouseY - yOffset,
absoluteMouseX,
absoluteMouseY
absoluteMouseY,
mouseXHF - xOffset,
mouseYHF - yOffset
)
}

Expand All @@ -122,7 +135,9 @@ data class GuiImmediateContext(
fun translatedNonRendering(xOffset: Int, yOffset: Int, width: Int, height: Int): GuiImmediateContext {
return GuiImmediateContext(
renderContext, renderOffsetX, renderOffsetY, width, height, mouseX - xOffset, mouseY - yOffset,
absoluteMouseX, absoluteMouseY
absoluteMouseX, absoluteMouseY,
mouseXHF,
mouseYHF
)
}

Expand All @@ -142,7 +157,9 @@ data class GuiImmediateContext(
renderOffsetX, renderOffsetY,
(width / scale).toInt(), (height / scale).toInt(),
((mouseX - renderOffsetX) * scale).toInt(), ((mouseY - renderOffsetY) * scale).toInt(),
absoluteMouseX, absoluteMouseY
absoluteMouseX, absoluteMouseY,
((mouseXHF - renderOffsetX) * scale),
((mouseYHF - renderOffsetY) * scale),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ import io.github.notenoughupdates.moulconfig.gui.MouseEvent
import io.github.notenoughupdates.moulconfig.gui.MouseEvent.Click
import io.github.notenoughupdates.moulconfig.internal.ClipboardUtils
import io.github.notenoughupdates.moulconfig.observer.GetSetter
import java.util.function.Supplier
import kotlin.math.max
import kotlin.math.min

open class TextFieldComponent(
val text: GetSetter<String>,
private val width: Int,
val editable: GetSetter<Boolean> = GetSetter.constant(true),
val editable: Supplier<Boolean> = GetSetter.constant(true),
val suggestion: String = "",
val font: IFontRenderer = IMinecraft.instance.defaultFontRenderer,
) : GuiComponent() {
var cursor = 0
var selection = -1
var scrollOffset = 0
var visibleText: String? = null
private var cursor = 0
private var selection = -1
private var scrollOffset = 0
private var visibleText: String? = null
override fun getWidth(): Int {
return width
}
Expand Down Expand Up @@ -54,7 +55,7 @@ open class TextFieldComponent(

override fun render(context: GuiImmediateContext) {
validateCursor()
checkScrollOffset()
checkScrollOffset() // TODO: read width from context.
visibleText = font.trimStringToWidth(safeSubString(text.get(), scrollOffset), width - TEXT_PADDING_X * 2)
renderBox(context)
renderText(context, visibleText!!)
Expand Down Expand Up @@ -204,9 +205,12 @@ open class TextFieldComponent(
}
} else if (event is KeyboardEvent.CharTyped) {
val it = event.char
if (it >= ' ' && it != '§' && it.code != 127)
var anyWritten = false
if (it >= ' ' && it != '§' && it.code != 127) {
writeText(it + "")
return true
anyWritten = true
}
return anyWritten
} else {
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.notenoughupdates.moulconfig.internal

import io.github.notenoughupdates.moulconfig.common.IFontRenderer
import io.github.notenoughupdates.moulconfig.common.RenderContext

object DrawContextExt {
@JvmOverloads
@JvmStatic
fun RenderContext.drawStringCenteredScalingDownWithMaxWidth(
text: String,
centerX: Int,
centerY: Int,
maxWidth: Int,
color: Int,
shadow: Boolean = false,
fr: IFontRenderer = minecraft.defaultFontRenderer,
) {
pushMatrix()
val width = fr.getStringWidth(text)
val factor = (maxWidth / width.toFloat()).coerceAtMost(1f)
translate(centerX.toFloat(), centerY.toFloat(), 0F)
scale(factor, factor, 1F)
drawString(fr, text, -width / 2, -fr.height / 2, color, shadow)
popMatrix()
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ forge_loader = "11.15.1.2318-1.8.9"
fabric_loader = "0.15.6"
fabric_api = "0.95.0+1.20.4"
yarn_build = "3"
lombok = "1.18.26"
lombok = "1.18.32"
jbAnnotations = "24.0.1"
kotlin = "1.8.21"
shadow = "8.1.1"
Expand Down
9 changes: 9 additions & 0 deletions legacy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ unimined.minecraft {
config("client") {
this.jvmArgs.add("-Dmoulconfig.testmod=true")
this.jvmArgs.add("-Dmoulconfig.warn.crash=false")
this.jvmArgs.remove("-XX:+UseG1GC")
this.jvmArgs.remove("-XX:G1NewSizePercent=20")
this.jvmArgs.remove("-XX:MaxGCPauseMillis=50")
this.jvmArgs.remove("-XX:G1ReservePercent=20")
this.jvmArgs.remove("-XX:G1HeapRegionSize=32M")
this.env.put(
"LD_LIBRARY_PATH",
":/nix/store/agp6lqznayysqvqkx4k1ggr8n1rsyi8c-gcc-13.2.0-lib/lib:/nix/store/ldi0rb00gmbdg6915lhch3k3b3ib460z-libXcursor-1.2.2/lib:/nix/store/8xbbv82pabjcbj30vrna4gcz4g9q97z4-libXrandr-1.5.4/lib:/nix/store/smrb2g0addhgahkfjjl3k8rfd30gdc29-libXxf86vm-1.1.5/lib:/nix/store/lpqy1z1h8li6h3cp9ax6vifl71dks1ff-libglvnd-1.7.0/lib"
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ protected GuiImmediateContext createContext() {
return new GuiImmediateContext(
new ForgeRenderContext(), 0, 0,
width, height,
x, y, x, y
x, y, x, y,
x, y
);
}

Expand Down Expand Up @@ -87,7 +88,7 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
ForgeRenderContext frc = new ForgeRenderContext();
context.getRoot().render(new GuiImmediateContext(
frc,
0, 0, width, height, mouseX, mouseY, mouseX, mouseY
0, 0, width, height, mouseX, mouseY, mouseX, mouseY , mouseX, mouseY
));
frc.doDrawTooltip();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@ public boolean fulfillsSearch(String word) {
}
return searchDescNameCache.contains(word);
}

public void setGuiContext(GuiContext guiContext) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.github.notenoughupdates.moulconfig.common.IFontRenderer;
import io.github.notenoughupdates.moulconfig.common.IMinecraft;
import io.github.notenoughupdates.moulconfig.common.RenderContext;
import io.github.notenoughupdates.moulconfig.gui.component.MetaComponent;
import io.github.notenoughupdates.moulconfig.gui.editors.GuiOptionEditorAccordion;
import io.github.notenoughupdates.moulconfig.gui.elements.GuiElementTextField;
import io.github.notenoughupdates.moulconfig.internal.*;
Expand Down Expand Up @@ -520,6 +521,7 @@ public void render() {
if (editor == null) {
continue;
}
editor.setGuiContext(guiContext);
if (editor instanceof GuiOptionEditorAccordion) {
GuiOptionEditorAccordion accordion = (GuiOptionEditorAccordion) editor;
if (accordion.getToggled()) {
Expand Down Expand Up @@ -577,6 +579,7 @@ public void render() {
if (editor == null) {
continue;
}
editor.setGuiContext(guiContext);
if (editor instanceof GuiOptionEditorAccordion) {
GuiOptionEditorAccordion accordion = (GuiOptionEditorAccordion) editor;
if (accordion.getToggled()) {
Expand Down Expand Up @@ -656,6 +659,8 @@ public void render() {
context.doDrawTooltip();
}

private GuiContext guiContext = new GuiContext(new MetaComponent());

public boolean mouseInput(int mouseX, int mouseY) {
lastMouseX = mouseX;
val iMinecraft = IMinecraft.instance;
Expand Down Expand Up @@ -783,6 +788,7 @@ public boolean mouseInput(int mouseX, int mouseY) {
if (editor == null) {
continue;
}
editor.setGuiContext(guiContext);
if (editor instanceof GuiOptionEditorAccordion) {
GuiOptionEditorAccordion accordion = (GuiOptionEditorAccordion) editor;
if (accordion.getToggled()) {
Expand Down Expand Up @@ -866,6 +872,7 @@ public boolean mouseInput(int mouseX, int mouseY) {
if (editor == null) {
continue;
}
editor.setGuiContext(guiContext);
if (editor instanceof GuiOptionEditorAccordion) {
GuiOptionEditorAccordion accordion = (GuiOptionEditorAccordion) editor;
if (accordion.getToggled()) {
Expand Down Expand Up @@ -914,6 +921,7 @@ public boolean mouseInput(int mouseX, int mouseY) {
if (editor == null) {
continue;
}
editor.setGuiContext(guiContext);
if (editor instanceof GuiOptionEditorAccordion) {
GuiOptionEditorAccordion accordion = (GuiOptionEditorAccordion) editor;
if (accordion.getToggled()) {
Expand Down Expand Up @@ -973,6 +981,7 @@ public boolean keyboardInput() {
if (editor == null) {
continue;
}
editor.setGuiContext(guiContext);
if (editor instanceof GuiOptionEditorAccordion) {
GuiOptionEditorAccordion accordion = (GuiOptionEditorAccordion) editor;
if (accordion.getToggled()) {
Expand Down
Loading

0 comments on commit 63821f1

Please sign in to comment.