Jump to content

[1.11.2] Multi-line textbox cursor behaving funny


Silly511

Recommended Posts

I have a gui with a multi-line textbox that works when typing text, deleting text, etc. but the cursor behaves very weirdly. When I press the right arrow key and I'm at the end of a line it appears to work(as in the cursor doesn't move past the end of the line) but if I press the right arrow key a bunch of times, and then press the left arrow key, the cursor doesn't move as if the cursor position is off the end of the line but the cursor isn't rendered off the line. Same thing happens at the beginning of a line, when I press the left arrow key it appears to stay but if I press the right arrow key the cursor doesn't move. If I put print statements before I change the cursorX and after, it seems cursorX is not going higher than or lower than it's supposed to but instead it's setting itself to back one and then forward one. I have no clue what is happening, or even how to explain it in a good way. Heres my keyTyped method in my GUI class:

 

	@Override
	protected void keyTyped(char typedChar, int keyCode) throws IOException {
		if (keyCode == Keyboard.KEY_ESCAPE) {
			this.mc.displayGuiScreen(null);
		} else if (keyCode == Keyboard.KEY_UP && this.cursorY > 0) {
			this.cursorY--;
			this.cursorX = MathHelper.clamp(this.cursorX, 0, this.text.get(cursorY).length());
		} else if (keyCode == Keyboard.KEY_DOWN && this.cursorY < this.text.size() - 1) {
			this.cursorY++;
			this.cursorX = MathHelper.clamp(this.cursorX, 0, this.text.get(cursorY).length());
		} else if (keyCode == Keyboard.KEY_LEFT && this.cursorX > 0) {
			this.cursorX--;
		} else if (keyCode == Keyboard.KEY_RIGHT && this.cursorX < this.text.get(cursorY).length()) {
			this.cursorX++;
		} else if (keyCode == Keyboard.KEY_RETURN) {
			String line = text.get(cursorY);
			
			text.set(cursorY, line.substring(0, cursorX));
			text.add(cursorY + 1, line.substring(cursorX, line.length()));
			
			cursorY++;
			cursorX = 0;
		} else if (keyCode == Keyboard.KEY_BACK && cursorX > 0) {
			cursorX--;
			
			text.set(cursorY, new StringBuilder(text.get(cursorY)).deleteCharAt(cursorX).toString());
		} else if (ChatAllowedCharacters.isAllowedCharacter(typedChar)) {
			String line = text.get(cursorY);
			
			if (line.length() + 1 > 255) return;
			
			text.set(cursorY, new StringBuilder(line).insert(cursorX, typedChar).toString());
			cursorX++;
		}
	}

 

What's really odd is deleting works perfectly fine, it's only the arrow keys that don't work.

 

Heres my entire GUI class if you need it:

 

Spoiler

package silly511.aestheticeffect.gui;

import java.io.IOException;
import java.util.List;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.google.common.collect.Lists;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ChatAllowedCharacters;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import silly511.aestheticeffect.tile.TileHologramProjector;

public class GuiHologramProjector extends GuiScreen {
	
	private List<String> text = Lists.newArrayList("");
	private int cursorX;
	private int cursorY;
	private int scroll;
	
	private BlockPos pos;
	private World world;
	private TileHologramProjector tile;
	
	public GuiHologramProjector(int x, int y, int z, World world) {
		this.pos = new BlockPos(x, y, z);
		this.world = world;
		this.tile = (TileHologramProjector) world.getTileEntity(pos);
	}

	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks) {
		int x = 100;
		int y = 100;
		
		this.drawDefaultBackground();
		
		for (int i = 0; i < 15; i++) {
			if (i + scroll > text.size() - 1) break;
			
			this.mc.fontRendererObj.drawString(text.get(scroll + i), x, y + (mc.fontRendererObj.FONT_HEIGHT + 2) * i, 0xFFFFFF, false);
		}
		
		int lineWidth = this.mc.fontRendererObj.getStringWidth(text.get(cursorY).substring(0, cursorX)) + 1;
		this.mc.fontRendererObj.drawString("_", x + lineWidth, y + (mc.fontRendererObj.FONT_HEIGHT + 2) * (cursorY - scroll) + 2, 0xFFFFFF);
		
		super.drawScreen(mouseX, mouseY, partialTicks);
	}

	@Override
	protected void keyTyped(char typedChar, int keyCode) throws IOException {
		if (keyCode == Keyboard.KEY_ESCAPE) {
			this.mc.displayGuiScreen(null);
		} else if (keyCode == Keyboard.KEY_UP && this.cursorY > 0) {
			this.cursorY--;
			this.cursorX = MathHelper.clamp(this.cursorX, 0, this.text.get(cursorY).length());
		} else if (keyCode == Keyboard.KEY_DOWN && this.cursorY < this.text.size() - 1) {
			this.cursorY++;
			this.cursorX = MathHelper.clamp(this.cursorX, 0, this.text.get(cursorY).length());
		} else if (keyCode == Keyboard.KEY_LEFT && this.cursorX > 0) {
			this.cursorX--;
		} else if (keyCode == Keyboard.KEY_RIGHT && this.cursorX < this.text.get(cursorY).length()) {
			this.cursorX++;
		} else if (keyCode == Keyboard.KEY_RETURN) {
			String line = text.get(cursorY);
			
			text.set(cursorY, line.substring(0, cursorX));
			text.add(cursorY + 1, line.substring(cursorX, line.length()));
			
			cursorY++;
			cursorX = 0;
		} else if (keyCode == Keyboard.KEY_BACK && cursorX > 0) {
			cursorX--;
			
			text.set(cursorY, new StringBuilder(text.get(cursorY)).deleteCharAt(cursorX).toString());
		} else if (ChatAllowedCharacters.isAllowedCharacter(typedChar)) {
			String line = text.get(cursorY);
			
			if (line.length() + 1 > 255) return;
			
			text.set(cursorY, new StringBuilder(line).insert(cursorX, typedChar).toString());
			cursorX++;
		}
	}

	@Override
	public void handleMouseInput() throws IOException {
		int scrollDir = Mouse.getEventDWheel();
		
		if (scrollDir != 0) {
			if (this.scroll > 0 && scrollDir >= 1)
				this.scroll--;
			else if (this.scroll < text.size() - 1 && scrollDir <= -1)
				this.scroll++;
		}
		
		super.handleMouseInput();
	}

	@Override
	protected void actionPerformed(GuiButton button) throws IOException {
		if (button.id == 0) {
			this.mc.displayGuiScreen(null);
			//TODO
		}
	}

	@Override
	public void initGui() {
		Keyboard.enableRepeatEvents(true);
		
		this.addButton(new GuiButton(0, 0, 0, I18n.format("gui.done")));
	}

	@Override
	public void onGuiClosed() {
		Keyboard.enableRepeatEvents(false);
	}

	@Override
	public boolean doesGuiPauseGame() {
		return false;
	}

}

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.