Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.mineformers.timetravel.client.gui.widget;
- import org.lwjgl.input.Keyboard;
- import net.minecraft.client.gui.FontRenderer;
- import net.minecraft.util.ChatAllowedCharacters;
- import de.mineformers.timetravel.client.gui.widget.listener.ListenerClickable;
- import de.mineformers.timetravel.client.gui.widget.listener.ListenerKeyboard;
- import de.mineformers.timetravel.lib.Textures;
- /**
- * TimeTravel
- *
- * WidgetTextBox
- *
- * @author PaleoCrafter
- * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
- *
- */
- public class WidgetTextBox extends Widget implements ListenerClickable,
- ListenerKeyboard {
- private int width, height;
- private String text;
- private WidgetSlot slotWidget;
- private boolean useSlotBg;
- private boolean focused;
- private int cursorPos;
- public WidgetTextBox(int x, int y, int width, int height, String startText,
- boolean useSlotBg) {
- super(Textures.GUI_WIDGETS, x, y);
- this.width = width;
- this.height = height;
- this.text = startText;
- this.slotWidget = new WidgetSlot(x, y, width, height);
- this.useSlotBg = useSlotBg;
- this.addListener(this);
- this.focused = false;
- this.cursorPos = startText.length() - 1;
- }
- @Override
- public void draw(int mouseX, int mouseY) {
- if (!useSlotBg) {
- // Corners clockwise
- this.drawRectangle(x, y, 31, 16, 5, 5);
- this.drawRectangle(x + width - 5, y, 39, 16, 5, 5);
- this.drawRectangle(x + width - 5, y + height - 5, 39, 24, 5, 5);
- this.drawRectangle(x, y + height - 5, 31, 24, 5, 5);
- // Sides clockwise
- this.drawRectangleStretched(x + 5, y, 37, 16, width - 10, 5, 1, 5);
- this.drawRectangleStretched(x + width - 5, y + 5, 39, 22, 5,
- height - 10, 5, 1);
- this.drawRectangleStretched(x + 5, y + height - 5, 37, 24,
- width - 10, 5, 1, 5);
- this.drawRectangleStretched(x, y + 5, 31, 22, 5, height - 10, 5, 1);
- // Canvas
- this.drawRectangleStretched(x + 5, y + 5, 37, 22, width - 10,
- height - 10, 1, 1);
- } else {
- slotWidget.setPos(x, y);
- slotWidget.draw(mouseX, mouseY);
- }
- FontRenderer fontRenderer = this.mc.fontRenderer;
- String toDraw = text;
- int sWidth = fontRenderer.getStringWidth(toDraw);
- sWidth += focused ? fontRenderer.getStringWidth("_") : 0;
- for (int i = 0; i <= cursorPos && sWidth > width - 4; i++) {
- toDraw = text.substring(i, cursorPos);
- sWidth = fontRenderer.getStringWidth(toDraw);
- sWidth += focused ? fontRenderer.getStringWidth("_") : 0;
- }
- fontRenderer.drawStringWithShadow(toDraw, x + 2, y + (height - 8) / 2,
- 0xe0e0e0);
- if (focused) {
- fontRenderer.drawStringWithShadow("_", x + 2 + sWidth
- - fontRenderer.getStringWidth("_"), y + (height - 8) / 2
- + 2, 0xe0e0e0);
- }
- }
- @Override
- public boolean isHovered(int mouseX, int mouseY) {
- return true;
- }
- @Override
- public void onClick(int mouseX, int mouseY) {
- if (mouseX > screenX && mouseY > screenY && mouseX < (screenX + width)
- && mouseY < (screenY + height)) {
- this.focused = true;
- } else {
- this.focused = false;
- }
- }
- @Override
- public void onKeyTyped(char keyChar, int keyCode) {
- if (focused) {
- switch (keyCode) {
- case Keyboard.KEY_LEFT:
- this.cursorPos -= 1;
- if (cursorPos < 0)
- cursorPos = 0;
- break;
- case Keyboard.KEY_RIGHT:
- this.cursorPos += 1;
- if (cursorPos > text.length() - 1)
- cursorPos = text.length() - 1;
- break;
- case Keyboard.KEY_BACK:
- if (this.text.length() > 0) {
- this.text = this.text.substring(0, text.length() - 1);
- cursorPos -= 1;
- if (cursorPos < 0)
- cursorPos = 0;
- }
- break;
- default:
- if (ChatAllowedCharacters.isAllowedCharacter(keyChar)) {
- this.text = text.substring(0, cursorPos)
- + Character.toString(keyChar)
- + text.substring(cursorPos);
- this.cursorPos += 1;
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement