BsGUIInputCaret.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUIInputCaret.h"
  4. #include "2D/BsSpriteTexture.h"
  5. #include "GUI/BsGUIManager.h"
  6. #include "2D/BsImageSprite.h"
  7. #include "GUI/BsGUIElement.h"
  8. #include "Text/BsFont.h"
  9. namespace bs
  10. {
  11. GUIInputCaret::GUIInputCaret()
  12. :mCaretPos(0)
  13. {
  14. mCaretSprite = bs_new<ImageSprite>();
  15. }
  16. GUIInputCaret::~GUIInputCaret()
  17. {
  18. bs_delete(mCaretSprite);
  19. }
  20. Vector2I GUIInputCaret::getSpriteOffset() const
  21. {
  22. return getCaretPosition(getTextOffset());
  23. }
  24. Rect2I GUIInputCaret::getSpriteClipRect(const Rect2I& parentClipRect) const
  25. {
  26. Vector2I offset(mElement->_getLayoutData().area.x, mElement->_getLayoutData().area.y);
  27. Vector2I clipOffset = getSpriteOffset() - offset -
  28. Vector2I(mElement->_getTextInputRect().x, mElement->_getTextInputRect().y);
  29. Rect2I clipRect(-clipOffset.x, -clipOffset.y, mTextDesc.width, mTextDesc.height);
  30. Rect2I localParentCliprect = parentClipRect;
  31. // Move parent rect to our space
  32. localParentCliprect.x += mElement->_getTextInputOffset().x + clipRect.x;
  33. localParentCliprect.y += mElement->_getTextInputOffset().y + clipRect.y;
  34. // Clip our rectangle so its not larger then the parent
  35. clipRect.clip(localParentCliprect);
  36. // Increase clip size by 1, so we can fit the caret in case it is fully at the end of the text
  37. clipRect.width += 1;
  38. return clipRect;
  39. }
  40. void GUIInputCaret::updateSprite()
  41. {
  42. IMAGE_SPRITE_DESC mCaretDesc;
  43. mCaretDesc.width = 1;
  44. mCaretDesc.height = getCaretHeight();
  45. mCaretDesc.texture = GUIManager::instance().getCaretTexture().getInternalPtr();
  46. GUIWidget* widget = nullptr;
  47. if (mElement != nullptr)
  48. widget = mElement->_getParentWidget();
  49. mCaretSprite->update(mCaretDesc, (UINT64)widget);
  50. }
  51. void GUIInputCaret::moveCaretToStart()
  52. {
  53. mCaretPos = 0;
  54. }
  55. void GUIInputCaret::moveCaretToEnd()
  56. {
  57. mCaretPos = getMaxCaretPos();
  58. }
  59. void GUIInputCaret::moveCaretLeft()
  60. {
  61. mCaretPos = (UINT32)std::max(0, (INT32)mCaretPos - 1);
  62. }
  63. void GUIInputCaret::moveCaretRight()
  64. {
  65. UINT32 maxCaretPos = getMaxCaretPos();
  66. mCaretPos = std::min(maxCaretPos, mCaretPos + 1);
  67. }
  68. void GUIInputCaret::moveCaretUp()
  69. {
  70. UINT32 charIdx = getCharIdxAtCaretPos();
  71. if(charIdx > 0)
  72. charIdx -= 1;
  73. UINT32 lineIdx = getLineForChar(charIdx);
  74. const GUIInputLineDesc& desc = getLineDesc(lineIdx);
  75. // If char is a newline, I want that to count as being on the next line because that's
  76. // how user sees it
  77. if(desc.isNewline(charIdx))
  78. lineIdx++;
  79. if(lineIdx == 0)
  80. {
  81. moveCaretToStart();
  82. return;
  83. }
  84. Vector2I caretCoords = getCaretPosition(mElement->_getTextInputOffset());
  85. caretCoords.y -= getCaretHeight();
  86. moveCaretToPos(caretCoords);
  87. }
  88. void GUIInputCaret::moveCaretDown()
  89. {
  90. UINT32 charIdx = getCharIdxAtCaretPos();
  91. if(charIdx > 0)
  92. charIdx -= 1;
  93. UINT32 lineIdx = getLineForChar(charIdx);
  94. const GUIInputLineDesc& desc = getLineDesc(lineIdx);
  95. // If char is a newline, I want that to count as being on the next line because that's
  96. // how user sees it
  97. if(desc.isNewline(charIdx))
  98. lineIdx++;
  99. if(lineIdx == (getNumLines() - 1))
  100. {
  101. moveCaretToEnd();
  102. return;
  103. }
  104. Vector2I caretCoords = getCaretPosition(mElement->_getTextInputOffset());
  105. caretCoords.y += getCaretHeight();
  106. moveCaretToPos(caretCoords);
  107. }
  108. void GUIInputCaret::moveCaretToPos(const Vector2I& pos)
  109. {
  110. INT32 charIdx = getCharIdxAtPos(pos);
  111. if(charIdx != -1)
  112. {
  113. Rect2I charRect = getCharRect(charIdx);
  114. float xCenter = charRect.x + charRect.width * 0.5f;
  115. if(pos.x <= xCenter)
  116. moveCaretToChar(charIdx, CARET_BEFORE);
  117. else
  118. moveCaretToChar(charIdx, CARET_AFTER);
  119. }
  120. else
  121. {
  122. UINT32 numLines = getNumLines();
  123. if(numLines == 0)
  124. {
  125. mCaretPos = 0;
  126. return;
  127. }
  128. UINT32 curPos = 0;
  129. for(UINT32 i = 0; i < numLines; i++)
  130. {
  131. const GUIInputLineDesc& line = getLineDesc(i);
  132. INT32 lineStart = line.getLineYStart() + getTextOffset().y;
  133. if(pos.y >= lineStart && pos.y < (lineStart + (INT32)line.getLineHeight()))
  134. {
  135. mCaretPos = curPos;
  136. return;
  137. }
  138. UINT32 numChars = line.getEndChar(false) - line.getStartChar() + 1; // +1 For extra line start position
  139. curPos += numChars;
  140. }
  141. {
  142. const GUIInputLineDesc& firstLine = getLineDesc(0);
  143. INT32 lineStart = firstLine.getLineYStart() + getTextOffset().y;
  144. if(pos.y < lineStart) // Before first line
  145. mCaretPos = 0;
  146. else // After last line
  147. mCaretPos = curPos - 1;
  148. }
  149. }
  150. }
  151. void GUIInputCaret::moveCaretToChar(UINT32 charIdx, CaretPos caretPos)
  152. {
  153. if(charIdx >= (UINT32)mTextDesc.text.size())
  154. {
  155. mCaretPos = 0;
  156. return;
  157. }
  158. UINT32 numLines = getNumLines();
  159. UINT32 curPos = 0;
  160. UINT32 curCharIdx = 0;
  161. for(UINT32 i = 0; i < numLines; i++)
  162. {
  163. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  164. curPos++; // Move past line start position
  165. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  166. UINT32 numCaretPositions = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  167. if(charIdx >= (curCharIdx + numChars))
  168. {
  169. curCharIdx += numChars;
  170. curPos += numCaretPositions;
  171. continue;
  172. }
  173. UINT32 diff = charIdx - curCharIdx;
  174. if(caretPos == CARET_BEFORE)
  175. curPos += diff - 1;
  176. else
  177. curPos += diff;
  178. break;
  179. }
  180. mCaretPos = curPos;
  181. }
  182. UINT32 GUIInputCaret::getCharIdxAtCaretPos() const
  183. {
  184. return getCharIdxAtInputIdx(mCaretPos);
  185. }
  186. Vector2I GUIInputCaret::getCaretPosition(const Vector2I& offset) const
  187. {
  188. if(mTextDesc.text.size() > 0 && isDescValid())
  189. {
  190. UINT32 curPos = 0;
  191. UINT32 numLines = getNumLines();
  192. for(UINT32 i = 0; i < numLines; i++)
  193. {
  194. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  195. if(mCaretPos == curPos)
  196. {
  197. // Caret is on line start
  198. return Vector2I(offset.x, lineDesc.getLineYStart() + getTextOffset().y);
  199. }
  200. curPos += lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  201. }
  202. UINT32 charIdx = getCharIdxAtCaretPos();
  203. if(charIdx > 0)
  204. charIdx -= 1;
  205. charIdx = std::min((UINT32)(mTextDesc.text.size() - 1), charIdx);
  206. Rect2I charRect = getCharRect(charIdx);
  207. UINT32 lineIdx = getLineForChar(charIdx);
  208. UINT32 yOffset = getLineDesc(lineIdx).getLineYStart() + getTextOffset().y;
  209. return Vector2I(charRect.x + charRect.width, yOffset);
  210. }
  211. return offset;
  212. }
  213. UINT32 GUIInputCaret::getCaretHeight() const
  214. {
  215. UINT32 charIdx = getCharIdxAtCaretPos();
  216. if(charIdx > 0)
  217. charIdx -= 1;
  218. if(charIdx < (UINT32)mTextDesc.text.size() && isDescValid())
  219. {
  220. UINT32 lineIdx = getLineForChar(charIdx);
  221. return getLineDesc(lineIdx).getLineHeight();
  222. }
  223. else
  224. {
  225. if(mTextDesc.font != nullptr)
  226. {
  227. UINT32 nearestSize = mTextDesc.font->getClosestSize(mTextDesc.fontSize);
  228. SPtr<const FontBitmap> fontData = mTextDesc.font->getBitmap(nearestSize);
  229. if(fontData != nullptr)
  230. return fontData->fontDesc.lineHeight;
  231. }
  232. }
  233. return 0;
  234. }
  235. bool GUIInputCaret::isCaretAtNewline() const
  236. {
  237. return isNewline(mCaretPos);
  238. }
  239. UINT32 GUIInputCaret::getMaxCaretPos() const
  240. {
  241. if(mTextDesc.text.size() == 0)
  242. return 0;
  243. UINT32 numLines = getNumLines();
  244. UINT32 maxPos = 0;
  245. for(UINT32 i = 0; i < numLines; i++)
  246. {
  247. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  248. UINT32 numChars = lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  249. maxPos += numChars;
  250. }
  251. return maxPos - 1;
  252. }
  253. }