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
9 changes: 9 additions & 0 deletions src/main/java/me/eigenraven/lwjgl3ify/client/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.ProgressManager;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import me.eigenraven.lwjgl3ify.CommonProxy;
import me.eigenraven.lwjgl3ify.api.InputEvents;
import me.eigenraven.lwjgl3ify.core.Config;
Expand Down Expand Up @@ -117,6 +118,14 @@ public void onGuiChange(final GuiOpenEvent event) {
}
}

@SubscribeEvent
@SuppressWarnings("unused")
public void onRenderTick(final TickEvent.RenderTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
TextFieldHandler.updateTextInputArea();
}
}

static float lastProgress = -1.0f;

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.eigenraven.lwjgl3ify.client;

/** @see me.eigenraven.lwjgl3ify.mixins.early.game.MixinGuiTextField */
public interface GuiTextFieldExt {

int lwjgl3ify$getCaretOffset();
}
68 changes: 68 additions & 0 deletions src/main/java/me/eigenraven/lwjgl3ify/client/TextFieldHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.util.ChatAllowedCharacters;

import org.jetbrains.annotations.Nullable;
import org.lwjgl.sdl.SDLClipboard;
import org.lwjgl.sdl.SDL_Rect;
import org.lwjgl.system.MemoryStack;
import org.lwjglx.opengl.Display;

import me.eigenraven.lwjgl3ify.Lwjgl3ify;
Expand All @@ -25,6 +29,9 @@ public final class TextFieldHandler {
private static final WeakReference<GuiTextField> NULL_TEXT_FIELD = new WeakReference<>(null);
private static WeakReference<GuiTextField> focusedTextInput = NULL_TEXT_FIELD;
public static final StringBuilder textBuffer = new StringBuilder(8);
private static final int[] lastTextInputArea = new int[5];
private static boolean textInputAreaSent = false;
private static boolean textInputAreaSet = false;

public static boolean isTextInputActive() {
return textInputDepth > 0;
Expand All @@ -39,6 +46,7 @@ public static void beginTextInput() {
Lwjgl3ify.LOG.info("[DEBUG] Beginning text input");
}
MainThreadExec.runOnMainThread(() -> { SDL_StartTextInput(Display.getWindow()); });
textInputAreaSent = false;
}
}

Expand Down Expand Up @@ -73,6 +81,66 @@ public static void resetTextInput() {
public static void setFocusedTextField(GuiTextField textField) {
if (focusedTextInput.get() != textField) {
focusedTextInput = new WeakReference<>(textField);
textInputAreaSent = false;
}
}

public static void updateTextInputArea() {
if (textInputDepth <= 0) {
return;
}
final GuiTextField textField = focusedTextInput.get();
if (textField == null || !textField.isFocused() || !textField.getVisible()) {
if (textInputAreaSet) {
if (!MainThreadExec.runOnMainThread(() -> SDL_SetTextInputArea(Display.getWindow(), null, 0))) {
return;
}
textInputAreaSet = false;
textInputAreaSent = false;
if (Config.DEBUG_PRINT_KEY_EVENTS) {
Lwjgl3ify.LOG.info("[DEBUG] Text input area cleared");
}
}
return;
}
final Minecraft mc = Minecraft.getMinecraft();
final int scale = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight).getScaleFactor();
final boolean background = textField.getEnableBackgroundDrawing();
final int textX = background ? textField.xPosition + 4 : textField.xPosition;
final int textY = background ? textField.yPosition + (textField.height - 8) / 2 : textField.yPosition;
final float windowScale = scale / Display.getPixelScaleFactor();
final int x = Math.round(textX * windowScale);
final int y = Math.round((textY - 1) * windowScale);
final int w = Math.round(textField.getWidth() * windowScale);
final int h = Math.round((mc.fontRenderer.FONT_HEIGHT + 2) * windowScale);
final int cursor = Math.round(((GuiTextFieldExt) textField).lwjgl3ify$getCaretOffset() * windowScale);
final int[] area = lastTextInputArea;
if (textInputAreaSent && area[0] == x && area[1] == y && area[2] == w && area[3] == h && area[4] == cursor) {
return;
}
area[0] = x;
area[1] = y;
area[2] = w;
area[3] = h;
area[4] = cursor;
textInputAreaSet = true;
textInputAreaSent = MainThreadExec.runOnMainThread(() -> {
try (MemoryStack stack = MemoryStack.stackPush()) {
final SDL_Rect.Buffer rect = SDL_Rect.malloc(1, stack);
rect.get(0)
.set(x, y, w, h);
return SDL_SetTextInputArea(Display.getWindow(), rect, cursor);
}
});
Comment thread
sivaDog marked this conversation as resolved.
Resolved
if (Config.DEBUG_PRINT_KEY_EVENTS) {
Lwjgl3ify.LOG.info(
"[DEBUG] Text input area x={} y={} w={} h={} cursor={} sent={}",
x,
y,
w,
h,
cursor,
textInputAreaSent);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package me.eigenraven.lwjgl3ify.mixins.early.game;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiTextField;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -10,14 +13,41 @@
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import me.eigenraven.lwjgl3ify.client.GuiTextFieldExt;
import me.eigenraven.lwjgl3ify.client.TextFieldHandler;

@Mixin(GuiTextField.class)
public class MixinGuiTextField {
public class MixinGuiTextField implements GuiTextFieldExt {

@Shadow
private boolean isFocused;

@Shadow
@Final
private FontRenderer field_146211_a;

@Shadow
private String text;

@Shadow
private int lineScrollOffset;

@Shadow
private int cursorPosition;

@Override
public int lwjgl3ify$getCaretOffset() {
final GuiTextField self = (GuiTextField) (Object) this;
final FontRenderer font = field_146211_a != null ? field_146211_a : Minecraft.getMinecraft().fontRenderer;
if (font == null || text == null) {
return 0;
}
final int scroll = Math.max(0, Math.min(lineScrollOffset, text.length()));
final String visible = font.trimStringToWidth(text.substring(scroll), self.getWidth());
final int caret = Math.max(0, Math.min(cursorPosition - scroll, visible.length()));
return font.getStringWidth(visible.substring(0, caret));
}

@Inject(method = "setFocused", at = @At("HEAD"))
private void lwjgl3ify$onFocusChange(boolean nowFocused, CallbackInfo ci) {
final GuiTextField self = (GuiTextField) (Object) this;
Expand Down
Loading