BsGUIInputCaret.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "BsGUIInputCaret.h"
  2. #include "BsSpriteTexture.h"
  3. #include "BsGUIManager.h"
  4. #include "BsImageSprite.h"
  5. #include "BsGUIElement.h"
  6. #include "CmFont.h"
  7. using namespace CamelotFramework;
  8. namespace BansheeEngine
  9. {
  10. GUIInputCaret::GUIInputCaret()
  11. :mCaretPos(0)
  12. {
  13. mCaretSprite = cm_new<ImageSprite, PoolAlloc>();
  14. }
  15. GUIInputCaret::~GUIInputCaret()
  16. {
  17. cm_delete<PoolAlloc>(mCaretSprite);
  18. }
  19. Vector2I GUIInputCaret::getSpriteOffset() const
  20. {
  21. return getCaretPosition(getTextOffset());
  22. }
  23. RectI GUIInputCaret::getSpriteClipRect(const CM::RectI& parentClipRect) const
  24. {
  25. Vector2I clipOffset = getSpriteOffset() - mElement->_getOffset() -
  26. Vector2I(mElement->_getTextInputRect().x, mElement->_getTextInputRect().y);
  27. RectI clipRect(-clipOffset.x, -clipOffset.y, mTextDesc.width, mTextDesc.height);
  28. RectI 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. mCaretSprite->update(mCaretDesc);
  45. }
  46. void GUIInputCaret::moveCaretToStart()
  47. {
  48. mCaretPos = 0;
  49. }
  50. void GUIInputCaret::moveCaretToEnd()
  51. {
  52. mCaretPos = getMaxCaretPos();
  53. }
  54. void GUIInputCaret::moveCaretLeft()
  55. {
  56. mCaretPos = (UINT32)std::max(0, (INT32)mCaretPos - 1);
  57. }
  58. void GUIInputCaret::moveCaretRight()
  59. {
  60. UINT32 maxCaretPos = getMaxCaretPos();
  61. mCaretPos = std::min(maxCaretPos, mCaretPos + 1);
  62. }
  63. void GUIInputCaret::moveCaretUp()
  64. {
  65. UINT32 charIdx = getCharIdxAtCaretPos();
  66. if(charIdx > 0)
  67. charIdx -= 1;
  68. UINT32 lineIdx = getLineForChar(charIdx);
  69. const GUIInputLineDesc& desc = getLineDesc(lineIdx);
  70. // If char is a newline, I want that to count as being on the next line because that's
  71. // how user sees it
  72. if(desc.isNewline(charIdx))
  73. lineIdx++;
  74. if(lineIdx == 0)
  75. {
  76. moveCaretToStart();
  77. return;
  78. }
  79. Vector2I caretCoords = getCaretPosition(mElement->_getTextInputOffset());
  80. caretCoords.y -= getCaretHeight();
  81. moveCaretToPos(caretCoords);
  82. }
  83. void GUIInputCaret::moveCaretDown()
  84. {
  85. UINT32 charIdx = getCharIdxAtCaretPos();
  86. if(charIdx > 0)
  87. charIdx -= 1;
  88. UINT32 lineIdx = getLineForChar(charIdx);
  89. const GUIInputLineDesc& desc = getLineDesc(lineIdx);
  90. // If char is a newline, I want that to count as being on the next line because that's
  91. // how user sees it
  92. if(desc.isNewline(charIdx))
  93. lineIdx++;
  94. if(lineIdx == (getNumLines() - 1))
  95. {
  96. moveCaretToEnd();
  97. return;
  98. }
  99. Vector2I caretCoords = getCaretPosition(mElement->_getTextInputOffset());
  100. caretCoords.y += getCaretHeight();
  101. moveCaretToPos(caretCoords);
  102. }
  103. void GUIInputCaret::moveCaretToPos(const CM::Vector2I& pos)
  104. {
  105. INT32 charIdx = getCharIdxAtPos(pos);
  106. if(charIdx != -1)
  107. {
  108. RectI charRect = getCharRect(charIdx);
  109. float xCenter = charRect.x + charRect.width * 0.5f;
  110. if(pos.x <= xCenter)
  111. moveCaretToChar(charIdx, CARET_BEFORE);
  112. else
  113. moveCaretToChar(charIdx, CARET_AFTER);
  114. }
  115. else
  116. {
  117. UINT32 numLines = getNumLines();
  118. if(numLines == 0)
  119. {
  120. mCaretPos = 0;
  121. return;
  122. }
  123. UINT32 curPos = 0;
  124. for(UINT32 i = 0; i < numLines; i++)
  125. {
  126. const GUIInputLineDesc& line = getLineDesc(i);
  127. INT32 lineStart = line.getLineYStart() + getTextOffset().y;
  128. if(pos.y >= lineStart && pos.y < (lineStart + (INT32)line.getLineHeight()))
  129. {
  130. mCaretPos = curPos;
  131. return;
  132. }
  133. UINT32 numChars = line.getEndChar(false) - line.getStartChar() + 1; // +1 For extra line start position
  134. curPos += numChars;
  135. }
  136. {
  137. const GUIInputLineDesc& firstLine = getLineDesc(0);
  138. INT32 lineStart = firstLine.getLineYStart() + getTextOffset().y;
  139. if(pos.y < lineStart) // Before first line
  140. mCaretPos = 0;
  141. else // After last line
  142. mCaretPos = curPos - 1;
  143. }
  144. }
  145. }
  146. void GUIInputCaret::moveCaretToChar(UINT32 charIdx, CaretPos caretPos)
  147. {
  148. if(charIdx >= (UINT32)mTextDesc.text.size())
  149. {
  150. mCaretPos = 0;
  151. return;
  152. }
  153. UINT32 numLines = getNumLines();
  154. UINT32 curPos = 0;
  155. UINT32 curCharIdx = 0;
  156. for(UINT32 i = 0; i < numLines; i++)
  157. {
  158. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  159. curPos++; // Move past line start position
  160. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  161. UINT32 numCaretPositions = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  162. if(charIdx >= (curCharIdx + numChars))
  163. {
  164. curCharIdx += numChars;
  165. curPos += numCaretPositions;
  166. continue;
  167. }
  168. UINT32 diff = charIdx - curCharIdx;
  169. if(caretPos == CARET_BEFORE)
  170. curPos += diff - 1;
  171. else
  172. curPos += diff;
  173. break;
  174. }
  175. mCaretPos = curPos;
  176. }
  177. UINT32 GUIInputCaret::getCharIdxAtCaretPos() const
  178. {
  179. return getCharIdxAtInputIdx(mCaretPos);
  180. }
  181. Vector2I GUIInputCaret::getCaretPosition(const CM::Vector2I& offset) const
  182. {
  183. if(mTextDesc.text.size() > 0)
  184. {
  185. UINT32 curPos = 0;
  186. UINT32 numLines = getNumLines();
  187. for(UINT32 i = 0; i < numLines; i++)
  188. {
  189. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  190. if(mCaretPos == curPos)
  191. {
  192. // Caret is on line start
  193. return Vector2I(offset.x, lineDesc.getLineYStart() + getTextOffset().y);
  194. }
  195. curPos += lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  196. }
  197. UINT32 charIdx = getCharIdxAtCaretPos();
  198. if(charIdx > 0)
  199. charIdx -= 1;
  200. charIdx = std::min((UINT32)(mTextDesc.text.size() - 1), charIdx);
  201. RectI charRect = getCharRect(charIdx);
  202. UINT32 lineIdx = getLineForChar(charIdx);
  203. UINT32 yOffset = getLineDesc(lineIdx).getLineYStart() + getTextOffset().y;
  204. return Vector2I(charRect.x + charRect.width, yOffset);
  205. }
  206. else
  207. {
  208. return offset;
  209. }
  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())
  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->getClosestAvailableSize(mTextDesc.fontSize);
  226. const FontData* fontData = mTextDesc.font->getFontDataForSize(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. }