BsGUIInputCaret.cpp 7.6 KB

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