BsGUIInputCaret.cpp 7.4 KB

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