Skip to content

Commit

Permalink
1.20.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
Wybxc committed Jul 17, 2023
1 parent e8fe96b commit 9a25eb0
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 84 deletions.
1 change: 1 addition & 0 deletions .architectury-transformer/debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Architectury Transformer DEBUG] Closed File Systems for E:\java\IngameIME-Forge\common\build\libs\IngameIME-Forge-mc1.20.x-1.0.4.jar
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import city.windmill.ingameime.client.gui.widget.CandidateListWidget
import city.windmill.ingameime.client.gui.widget.CompositionWidget
import city.windmill.ingameime.client.gui.widget.Widget
import city.windmill.ingameime.client.jni.ExternalBaseIME
import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiGraphics

object OverlayScreen : net.minecraft.client.gui.components.Widget {
object OverlayScreen : net.minecraft.client.gui.components.Renderable {
private val alphaModeWidget = AlphaModeWidget(Minecraft.getInstance().font)
private val compositionWidget = CompositionWidget(Minecraft.getInstance().font)
private val candidateListWidget = CandidateListWidget(Minecraft.getInstance().font)
Expand Down Expand Up @@ -78,14 +78,15 @@ object OverlayScreen : net.minecraft.client.gui.components.Widget {
/**
* Render the widget when input method is active
*/
override fun render(poseStack: PoseStack, mouseX: Int, mouseY: Int, delta: Float) {
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, delta: Float) {
if (ExternalBaseIME.State) {
poseStack.pushPose()
poseStack.translate(0.0, 0.0, 500.0)
compositionWidget.render(poseStack, mouseX, mouseY, delta)
alphaModeWidget.render(poseStack, mouseX, mouseY, delta)
candidateListWidget.render(poseStack, mouseX, mouseY, delta)
poseStack.popPose()
val pose = guiGraphics.pose()
pose.pushPose()
pose.translate(0.0, 0.0, 500.0)
compositionWidget.render(guiGraphics, mouseX, mouseY, delta)
alphaModeWidget.render(guiGraphics, mouseX, mouseY, delta)
candidateListWidget.render(guiGraphics, mouseX, mouseY, delta)
pose.popPose()
}
}

Expand Down Expand Up @@ -118,4 +119,4 @@ object OverlayScreen : net.minecraft.client.gui.components.Widget {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package city.windmill.ingameime.client.gui.widget

import city.windmill.ingameime.client.jni.ExternalBaseIME
import com.mojang.blaze3d.vertex.PoseStack
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import net.minecraft.client.gui.Font
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.resources.language.I18n
import java.lang.ref.WeakReference

class AlphaModeWidget(font: Font) : Widget(font) {
private val text get() = I18n.get(if (ExternalBaseIME.AlphaMode) "alpha.ingameime.mode" else "native.ingameime.mode")
private var hideDelay: WeakReference<Job>? = null

@OptIn(DelicateCoroutinesApi::class)
override var active = false
set(value) {
hideDelay?.get()?.cancel()
Expand All @@ -35,10 +33,10 @@ class AlphaModeWidget(font: Font) : Widget(font) {
get() = 2 to 3

@Suppress("NAME_SHADOWING")
override fun draw(poseStack: PoseStack, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
super.draw(poseStack, offsetX, offsetY, mouseX, mouseY, delta)
override fun draw(guiGraphics: GuiGraphics, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
super.draw(guiGraphics, offsetX, offsetY, mouseX, mouseY, delta)
val offsetX = offsetX + width / 2 - font.width(text) / 2
val offsetY = offsetY + padding.second
font.draw(poseStack, text, offsetX.toFloat(), offsetY.toFloat(), textColor)
guiGraphics.drawString(font, text, offsetX, offsetY, textColor)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,76 +1,84 @@
package city.windmill.ingameime.client.gui.widget

import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.gui.Font
import net.minecraft.client.gui.GuiGraphics


class CandidateListWidget(font: Font) : Widget(font) {
var candidates: Array<String>? = null

private val drawItem = CandidateEntry(font)

override val active get() = !candidates.isNullOrEmpty()
override val width
get() = super.width + candidates!!.sumOf { s -> drawItem.apply { this.text = s }.width }
override val height
get() = super.height + font.lineHeight
override val padding: Pair<Int, Int>
get() = 1 to 3

@Suppress("NAME_SHADOWING")
override fun draw(poseStack: PoseStack, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
override fun draw(guiGraphics: GuiGraphics, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
candidates?.let {
super.draw(poseStack, offsetX, offsetY, mouseX, mouseY, delta)
super.draw(guiGraphics, offsetX, offsetY, mouseX, mouseY, delta)

var offsetX = offsetX + padding.first
val offsetY = offsetY + padding.second
var index = 1
for (str in it) {
drawItem.index = index
drawItem.text = str
drawItem.draw(poseStack, offsetX, offsetY, mouseX, mouseY, delta)
drawItem.draw(guiGraphics, offsetX, offsetY, mouseX, mouseY, delta)
offsetX += drawItem.width
index++
}
}
}

class CandidateEntry(font: Font) : Widget(font) {
var text: String? = null
var index = 0

private val indexWidth = font.width("00") + 5

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
override val width
get() = super.width + font.width(text) + indexWidth
override val height
get() = super.height + font.lineHeight
override val padding: Pair<Int, Int>
get() = 2 to 3

@Suppress("NAME_SHADOWING", "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
override fun draw(poseStack: PoseStack, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
override fun draw(
guiGraphics: GuiGraphics,
offsetX: Int,
offsetY: Int,
mouseX: Int,
mouseY: Int,
delta: Float
) {
var offsetX = offsetX + padding.first
drawCenteredString(poseStack, font, index.toString(), offsetX + indexWidth / 2, offsetY, textColor)
drawCenteredString(guiGraphics, font, index.toString(), offsetX + indexWidth / 2, offsetY, textColor)
offsetX += indexWidth
font.draw(poseStack, text, offsetX.toFloat(), offsetY.toFloat(), textColor)
guiGraphics.drawString(font, text, offsetX, offsetY, textColor)
}

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
private fun drawCenteredString(
poseStack: PoseStack,
guiGraphics: GuiGraphics,
font: Font,
text: String?,
centerX: Int,
y: Int,
color: Int
) {
font.draw(
poseStack, text,
(centerX - font.width(text) / 2).toFloat(), y.toFloat(), color
guiGraphics.drawString(
font,
text,
(centerX - font.width(text) / 2), y, color
)
}

}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package city.windmill.ingameime.client.gui.widget

import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.gui.Font
import net.minecraft.client.gui.GuiComponent
import net.minecraft.client.gui.GuiGraphics

class CompositionWidget(font: Font) : Widget(font) {
/**
Expand All @@ -25,23 +24,22 @@ class CompositionWidget(font: Font) : Widget(font) {
get() = 1 to 1

@Suppress("NAME_SHADOWING")
override fun draw(poseStack: PoseStack, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
override fun draw(guiGraphics: GuiGraphics, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
compositionData?.let {
val text = it.first
val caret = it.second

super.draw(poseStack, offsetX, offsetY, mouseX, mouseY, delta)
super.draw(guiGraphics, offsetX, offsetY, mouseX, mouseY, delta)

val part1 = text.substring(0, caret)
val part2 = text.substring(caret)

var offsetX = offsetX + padding.first
val offsetY = offsetY + padding.second
offsetX = font.draw(poseStack, part1, offsetX.toFloat(), offsetY.toFloat(), textColor)
guiGraphics.drawString(font, part1, offsetX, offsetY, textColor)
//Caret-blink 0.5s
if ((System.currentTimeMillis() % 1000) > 500) {
GuiComponent.fill(
poseStack,
guiGraphics.fill(
offsetX + 1, //1 pixel width
offsetY,
offsetX + 2, //with 2 pixel margin
Expand All @@ -50,7 +48,7 @@ class CompositionWidget(font: Font) : Widget(font) {
)
}
offsetX += caretWidth
font.draw(poseStack, part2, offsetX.toFloat(), offsetY.toFloat(), textColor)
guiGraphics.drawString(font, part2, offsetX, offsetY, textColor)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package city.windmill.ingameime.client.gui.widget

import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.gui.Font
import net.minecraft.client.gui.GuiComponent
import net.minecraft.client.gui.GuiGraphics


abstract class Widget(val font: Font) : net.minecraft.client.gui.components.Widget {
abstract class Widget(val font: Font) : net.minecraft.client.gui.components.Renderable {
var offsetX = 0
var offsetY = 0
var textColor = 0xFF_00_00_00.toInt()
Expand All @@ -17,15 +16,14 @@ abstract class Widget(val font: Font) : net.minecraft.client.gui.components.Widg
open val height get() = padding.second * 2
open val padding = 0 to 0

override fun render(poseStack: PoseStack, mouseX: Int, mouseY: Int, delta: Float) {
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, delta: Float) {
if (active)
draw(poseStack, offsetX, offsetY, mouseX, mouseY, delta)
draw(guiGraphics, offsetX, offsetY, mouseX, mouseY, delta)
}

open fun draw(poseStack: PoseStack, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
open fun draw(guiGraphics: GuiGraphics, offsetX: Int, offsetY: Int, mouseX: Int, mouseY: Int, delta: Float) {
//Background
GuiComponent.fill(
poseStack,
guiGraphics.fill(
offsetX,
offsetY,
offsetX + width,
Expand All @@ -38,4 +36,4 @@ abstract class Widget(val font: Font) : net.minecraft.client.gui.components.Widg
offsetX = x
offsetY = y
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import city.windmill.ingameime.forge.ScreenEvents;
import com.mojang.blaze3d.vertex.PoseStack;
import kotlin.Pair;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.network.chat.Component;
Expand All @@ -24,30 +25,34 @@ private MixinEditBox(int i, int j, int k, int l, Component component) {
super(i, j, k, l, component);
}

@Inject(method = {"setFocus", "onFocusedChanged"}, at = @At("HEAD"))
@Inject(method = {"setFocused"}, at = @At("HEAD"))
private void onSelected(boolean selected, CallbackInfo info) {
int caretX = bordered ? x + 4 : x;
int x = getX();
int y = getY();
int caretX = bordered ? getX() + 4 : x;
int caretY = bordered ? y + (height - 8) / 2 : y;
if (selected)
IngameIMEForge.INSTANCE.getINGAMEIME_BUS().post(new ScreenEvents.EditOpen(this, new Pair<>(caretX, caretY)));
else
IngameIMEForge.INSTANCE.getINGAMEIME_BUS().post(new ScreenEvents.EditClose(this));
}

@Inject(method = "mouseClicked", at = @At(value = "INVOKE",
@Inject(method = "onClick", at = @At(value = "INVOKE",
target = "net/minecraft/util/Mth.floor(D)I",
shift = At.Shift.BEFORE,
ordinal = 0))
private void onFocused(double double_1, double double_2, int int_1, CallbackInfoReturnable<Boolean> cir) {
int x = getX();
int y = getY();
int caretX = bordered ? x + 4 : x;
int caretY = bordered ? y + (height - 8) / 2 : y;
IngameIMEForge.INSTANCE.getINGAMEIME_BUS().post(new ScreenEvents.EditOpen(this, new Pair<>(caretX, caretY)));
}

@Inject(method = "renderButton",
@Inject(method = "renderWidget",
at = @At(value = "INVOKE", target = "java/lang/String.isEmpty()Z", ordinal = 1),
locals = LocalCapture.CAPTURE_FAILSOFT)
private void onCaret(PoseStack poseStack, int arg1, int arg2, float arg3, CallbackInfo ci, int l, int m, int n, String string, boolean bl, boolean bl2, int o, int p, int q, boolean bl3, int r) {
private void onCaret(GuiGraphics arg, int arg1, int arg2, float arg3, CallbackInfo ci, int l, int m, int n, String string, boolean bl, boolean bl2, int o, int p, int q, boolean bl3, int r) {
IngameIMEForge.INSTANCE.getINGAMEIME_BUS().post(new ScreenEvents.EditCaret(this, new Pair<>(r, p)));
}
}
Loading

0 comments on commit 9a25eb0

Please sign in to comment.