Advertisement
PaleoCrafter

WidgetTextBox

Sep 23rd, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. package de.mineformers.timetravel.client.gui.widget;
  2.  
  3. import org.lwjgl.input.Keyboard;
  4.  
  5. import net.minecraft.client.gui.FontRenderer;
  6. import net.minecraft.util.ChatAllowedCharacters;
  7. import de.mineformers.timetravel.client.gui.widget.listener.ListenerClickable;
  8. import de.mineformers.timetravel.client.gui.widget.listener.ListenerKeyboard;
  9. import de.mineformers.timetravel.lib.Textures;
  10.  
  11. /**
  12. * TimeTravel
  13. *
  14. * WidgetTextBox
  15. *
  16. * @author PaleoCrafter
  17. * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
  18. *
  19. */
  20. public class WidgetTextBox extends Widget implements ListenerClickable,
  21. ListenerKeyboard {
  22.  
  23. private int width, height;
  24. private String text;
  25.  
  26. private WidgetSlot slotWidget;
  27. private boolean useSlotBg;
  28. private boolean focused;
  29. private int cursorPos;
  30.  
  31. public WidgetTextBox(int x, int y, int width, int height, String startText,
  32. boolean useSlotBg) {
  33. super(Textures.GUI_WIDGETS, x, y);
  34. this.width = width;
  35. this.height = height;
  36. this.text = startText;
  37. this.slotWidget = new WidgetSlot(x, y, width, height);
  38. this.useSlotBg = useSlotBg;
  39. this.addListener(this);
  40. this.focused = false;
  41. this.cursorPos = startText.length() - 1;
  42. }
  43.  
  44. @Override
  45. public void draw(int mouseX, int mouseY) {
  46.  
  47. if (!useSlotBg) {
  48. // Corners clockwise
  49. this.drawRectangle(x, y, 31, 16, 5, 5);
  50. this.drawRectangle(x + width - 5, y, 39, 16, 5, 5);
  51. this.drawRectangle(x + width - 5, y + height - 5, 39, 24, 5, 5);
  52. this.drawRectangle(x, y + height - 5, 31, 24, 5, 5);
  53.  
  54. // Sides clockwise
  55. this.drawRectangleStretched(x + 5, y, 37, 16, width - 10, 5, 1, 5);
  56. this.drawRectangleStretched(x + width - 5, y + 5, 39, 22, 5,
  57. height - 10, 5, 1);
  58. this.drawRectangleStretched(x + 5, y + height - 5, 37, 24,
  59. width - 10, 5, 1, 5);
  60. this.drawRectangleStretched(x, y + 5, 31, 22, 5, height - 10, 5, 1);
  61.  
  62. // Canvas
  63. this.drawRectangleStretched(x + 5, y + 5, 37, 22, width - 10,
  64. height - 10, 1, 1);
  65. } else {
  66. slotWidget.setPos(x, y);
  67. slotWidget.draw(mouseX, mouseY);
  68. }
  69.  
  70. FontRenderer fontRenderer = this.mc.fontRenderer;
  71. String toDraw = text;
  72. int sWidth = fontRenderer.getStringWidth(toDraw);
  73. sWidth += focused ? fontRenderer.getStringWidth("_") : 0;
  74. for (int i = 0; i <= cursorPos && sWidth > width - 4; i++) {
  75. toDraw = text.substring(i, cursorPos);
  76. sWidth = fontRenderer.getStringWidth(toDraw);
  77. sWidth += focused ? fontRenderer.getStringWidth("_") : 0;
  78. }
  79.  
  80. fontRenderer.drawStringWithShadow(toDraw, x + 2, y + (height - 8) / 2,
  81. 0xe0e0e0);
  82. if (focused) {
  83. fontRenderer.drawStringWithShadow("_", x + 2 + sWidth
  84. - fontRenderer.getStringWidth("_"), y + (height - 8) / 2
  85. + 2, 0xe0e0e0);
  86. }
  87.  
  88. }
  89.  
  90. @Override
  91. public boolean isHovered(int mouseX, int mouseY) {
  92. return true;
  93. }
  94.  
  95. @Override
  96. public void onClick(int mouseX, int mouseY) {
  97. if (mouseX > screenX && mouseY > screenY && mouseX < (screenX + width)
  98. && mouseY < (screenY + height)) {
  99. this.focused = true;
  100. } else {
  101. this.focused = false;
  102. }
  103. }
  104.  
  105. @Override
  106. public void onKeyTyped(char keyChar, int keyCode) {
  107. if (focused) {
  108. switch (keyCode) {
  109. case Keyboard.KEY_LEFT:
  110. this.cursorPos -= 1;
  111. if (cursorPos < 0)
  112. cursorPos = 0;
  113. break;
  114. case Keyboard.KEY_RIGHT:
  115. this.cursorPos += 1;
  116. if (cursorPos > text.length() - 1)
  117. cursorPos = text.length() - 1;
  118. break;
  119. case Keyboard.KEY_BACK:
  120. if (this.text.length() > 0) {
  121. this.text = this.text.substring(0, text.length() - 1);
  122. cursorPos -= 1;
  123. if (cursorPos < 0)
  124. cursorPos = 0;
  125. }
  126. break;
  127. default:
  128. if (ChatAllowedCharacters.isAllowedCharacter(keyChar)) {
  129. this.text = text.substring(0, cursorPos)
  130. + Character.toString(keyChar)
  131. + text.substring(cursorPos);
  132. this.cursorPos += 1;
  133. }
  134. break;
  135. }
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement