ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DInputField.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package org.openzen.drawablegui;
  7. import org.openzen.drawablegui.draw.DDrawnRectangle;
  8. import org.openzen.drawablegui.draw.DDrawnShape;
  9. import org.openzen.drawablegui.draw.DDrawnText;
  10. import org.openzen.drawablegui.listeners.ListenerHandle;
  11. import org.openzen.drawablegui.live.LiveObject;
  12. import org.openzen.drawablegui.live.LiveString;
  13. import org.openzen.drawablegui.live.MutableLiveObject;
  14. import org.openzen.drawablegui.live.MutableLiveString;
  15. import org.openzen.drawablegui.style.DDimension;
  16. import org.openzen.drawablegui.style.DStyleClass;
  17. /**
  18. *
  19. * @author Hoofdgebruiker
  20. */
  21. public class DInputField implements DComponent {
  22. public final MutableLiveString value;
  23. private final ListenerHandle<LiveString.Listener> valueListener;
  24. private final DStyleClass styleClass;
  25. private final MutableLiveObject<DSizing> sizing = DSizing.create();
  26. private DIRectangle bounds = DIRectangle.EMPTY;
  27. private final DDimension preferredWidth;
  28. private DComponentContext context;
  29. private DInputFieldStyle style;
  30. private DFontMetrics fontMetrics;
  31. private int cursorFrom = -1;
  32. private int cursorTo = -1;
  33. private Runnable onEnter = null;
  34. private Runnable onEscape = null;
  35. private boolean cursorBlink = true;
  36. private DTimerHandle blinkTimer;
  37. private DDrawnShape shape;
  38. private DDrawnText text;
  39. private DDrawnRectangle cursor;
  40. private DDrawnRectangle selection;
  41. public DInputField(DStyleClass styleClass, MutableLiveString value, DDimension preferredWidth) {
  42. this.styleClass = styleClass;
  43. this.value = value;
  44. this.preferredWidth = preferredWidth;
  45. valueListener = value.addListener((oldValue, newValue) -> handleValueUpdated(newValue));
  46. cursorFrom = 0;
  47. cursorTo = value.getValue().length();
  48. }
  49. public void setOnEnter(Runnable onEnter) {
  50. this.onEnter = onEnter;
  51. }
  52. public void setOnEscape(Runnable onEscape) {
  53. this.onEscape = onEscape;
  54. }
  55. @Override
  56. public void close() {
  57. valueListener.close();
  58. unmount();
  59. }
  60. @Override
  61. public void mount(DComponentContext parent) {
  62. context = parent.getChildContext("input", styleClass);
  63. style = context.getStyle(DInputFieldStyle::new);
  64. fontMetrics = context.getFontMetrics(style.font);
  65. sizing.setValue(new DSizing(
  66. preferredWidth.evalInt(context.getUIContext()) + style.margin.getHorizontal() + style.border.getPaddingHorizontal(),
  67. fontMetrics.getAscent() + fontMetrics.getDescent() + style.margin.getVertical() + style.border.getPaddingVertical()));
  68. if (blinkTimer != null)
  69. blinkTimer.close();
  70. blinkTimer = context.getUIContext().setTimer(300, this::blink);
  71. if (text != null)
  72. text.close();
  73. text = parent.drawText(
  74. 2,
  75. style.font,
  76. style.color,
  77. bounds.x + style.margin.left + style.border.getPaddingLeft(),
  78. bounds.y + style.margin.top + style.border.getPaddingTop() + fontMetrics.getAscent(),
  79. value.getValue());
  80. if (cursor != null)
  81. cursor.close();
  82. cursor = parent.fillRect(3, DIRectangle.EMPTY, cursorBlink ? style.cursorColor : 0);
  83. if (selection != null)
  84. selection.close();
  85. selection = parent.fillRect(1, DIRectangle.EMPTY, 0);
  86. setCursor(cursorFrom, cursorTo);
  87. }
  88. @Override
  89. public void unmount() {
  90. blinkTimer.close();
  91. if (style != null)
  92. style.border.close();
  93. if (shape != null)
  94. shape.close();
  95. if (text != null)
  96. text.close();
  97. if (cursor != null)
  98. cursor.close();
  99. if (selection != null)
  100. selection.close();
  101. }
  102. private void blink() {
  103. cursorBlink = !cursorBlink;
  104. cursor.setColor(cursorBlink ? style.cursorColor : 0);
  105. }
  106. @Override
  107. public LiveObject<DSizing> getSizing() {
  108. return sizing;
  109. }
  110. @Override
  111. public DIRectangle getBounds() {
  112. return bounds;
  113. }
  114. @Override
  115. public int getBaselineY() {
  116. return style.margin.top + style.border.getPaddingTop() + fontMetrics.getAscent();
  117. }
  118. @Override
  119. public void setBounds(DIRectangle bounds) {
  120. this.bounds = bounds;
  121. setCursor(cursorFrom, cursorTo);
  122. if (shape != null)
  123. shape.close();
  124. shape = context.fillPath(0, style.shape.instance(style.margin.apply(bounds)), DTransform2D.IDENTITY, style.backgroundColor);
  125. text.setPosition(
  126. bounds.x + style.margin.left + style.border.getPaddingLeft(),
  127. bounds.y + style.margin.top + style.border.getPaddingTop() + fontMetrics.getAscent());
  128. style.border.update(context, bounds);
  129. }
  130. @Override
  131. public void onMouseEnter(DMouseEvent e) {
  132. context.getUIContext().setCursor(DUIContext.Cursor.TEXT);
  133. }
  134. @Override
  135. public void onMouseExit(DMouseEvent e) {
  136. context.getUIContext().setCursor(DUIContext.Cursor.NORMAL);
  137. }
  138. @Override
  139. public void onMouseClick(DMouseEvent e) {
  140. context.getUIContext().getWindow().focus(this);
  141. }
  142. @Override
  143. public void onKeyPressed(DKeyEvent e) {
  144. boolean shift = e.has(DKeyEvent.SHIFT);
  145. switch (e.keyCode) {
  146. case UP:
  147. setCursor(0, 0);
  148. break;
  149. case DOWN:
  150. setCursor(value.getValue().length(), value.getValue().length());
  151. break;
  152. case LEFT: {
  153. int to = Math.max(0, cursorTo - 1);
  154. setCursor(shift ? cursorFrom : to, to);
  155. break;
  156. }
  157. case RIGHT: {
  158. int to = Math.min(value.getValue().length(), cursorTo + 1);
  159. setCursor(shift ? cursorFrom : to, to);
  160. break;
  161. }
  162. case DELETE:
  163. delete();
  164. break;
  165. case BACKSPACE:
  166. backspace();
  167. break;
  168. case ENTER:
  169. enter();
  170. break;
  171. case ESCAPE:
  172. escape();
  173. break;
  174. default:
  175. if (e.character == DKeyEvent.CHAR_UNDEFINED)
  176. return;
  177. insert(Character.toString(e.character));
  178. break;
  179. }
  180. }
  181. private void setCursor(int from, int to) {
  182. cursorFrom = from;
  183. cursorTo = to;
  184. int cursorXFrom = fontMetrics.getWidth(value.getValue(), 0, Math.min(cursorFrom, cursorTo));
  185. int cursorXTo = fontMetrics.getWidth(value.getValue(), 0, Math.max(cursorFrom, cursorTo));
  186. if (cursorFrom != cursorTo) {
  187. selection.setRectangle(new DIRectangle(
  188. bounds.x + style.margin.left + style.border.getPaddingLeft() + cursorXFrom,
  189. bounds.y + style.margin.top + style.border.getPaddingTop(),
  190. cursorXTo - cursorXFrom,
  191. fontMetrics.getAscent() + fontMetrics.getDescent()));
  192. selection.setColor(style.selectionColor);
  193. } else {
  194. selection.setColor(0);
  195. }
  196. cursor.setRectangle(new DIRectangle(
  197. bounds.x + style.margin.left + style.border.getPaddingLeft() + cursorXTo,
  198. bounds.y + style.margin.top + style.border.getPaddingTop(),
  199. style.cursorWidth,
  200. fontMetrics.getAscent() + fontMetrics.getDescent()));
  201. }
  202. private void handleValueUpdated(String newValue) {
  203. if (text != null)
  204. text.close();
  205. text = context.drawText(
  206. 2,
  207. style.font,
  208. style.color,
  209. bounds.x + style.margin.left + style.border.getPaddingLeft(),
  210. bounds.y + style.margin.top + style.border.getPaddingTop() + fontMetrics.getAscent(),
  211. value.getValue());
  212. }
  213. private void backspace() {
  214. if (cursorFrom == 0 && cursorTo == 0)
  215. return;
  216. if (cursorFrom == cursorTo) {
  217. value.setValue(value.getValue().substring(0, cursorFrom - 1) + value.getValue().substring(cursorFrom));
  218. setCursor(cursorFrom - 1, cursorTo - 1);
  219. } else {
  220. int from = Math.min(cursorFrom, cursorTo);
  221. int to = Math.max(cursorFrom, cursorTo);
  222. setCursor(from, from);
  223. value.setValue(value.getValue().substring(0, from) + value.getValue().substring(to));
  224. }
  225. }
  226. private void delete() {
  227. if (cursorFrom == 0 && cursorTo == 0)
  228. return;
  229. if (cursorFrom == cursorTo) {
  230. if (cursorFrom < value.getValue().length()) {
  231. value.setValue(value.getValue().substring(0, cursorFrom) + value.getValue().substring(cursorFrom + 1));
  232. }
  233. } else {
  234. int from = Math.min(cursorFrom, cursorTo);
  235. int to = Math.max(cursorFrom, cursorTo);
  236. setCursor(from, from);
  237. value.setValue(value.getValue().substring(0, from) + value.getValue().substring(to));
  238. }
  239. }
  240. private void insert(String value) {
  241. int from = Math.min(cursorFrom, cursorTo);
  242. int to = Math.max(cursorFrom, cursorTo);
  243. this.value.setValue(this.value.getValue().substring(0, from) + value + this.value.getValue().substring(to));
  244. setCursor(from + value.length(), from + value.length());
  245. }
  246. private void enter() {
  247. if (onEnter != null)
  248. onEnter.run();
  249. }
  250. private void escape() {
  251. if (onEscape != null)
  252. onEscape.run();
  253. }
  254. }