BsGUIInputCaret.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "BsGUIInputCaret.h"
  2. #include "BsGUIManager.h"
  3. #include "BsImageSprite.h"
  4. #include "CmFont.h"
  5. using namespace CamelotFramework;
  6. namespace BansheeEngine
  7. {
  8. GUIInputCaret::GUIInputCaret(const TEXT_SPRITE_DESC& textDesc)
  9. :mCaretPos(0), mTextDesc(textDesc)
  10. {
  11. mCaretSprite = cm_new<ImageSprite, PoolAlloc>();
  12. mTextSprite = cm_new<TextSprite, PoolAlloc>();
  13. mTextSprite->update(mTextDesc);
  14. }
  15. GUIInputCaret::~GUIInputCaret()
  16. {
  17. cm_delete<PoolAlloc>(mCaretSprite);
  18. cm_delete<PoolAlloc>(mTextSprite);
  19. }
  20. void GUIInputCaret::updateText(const TEXT_SPRITE_DESC& textDesc)
  21. {
  22. mTextDesc = textDesc;
  23. mTextDesc.clipRect = Rect(0, 0, 0, 0); // No clipping otherwise we don't know position of chars
  24. // outside of the element, which is something we need when moving the cursor
  25. mTextSprite->update(mTextDesc);
  26. }
  27. void GUIInputCaret::updateSprite(const CM::Int2& offset)
  28. {
  29. IMAGE_SPRITE_DESC mCaretDesc;
  30. mCaretDesc.offset = getCaretPosition(mTextDesc.offset);
  31. mCaretDesc.width = 1;
  32. mCaretDesc.height = getCaretHeight();
  33. mCaretDesc.clipRect = Rect(-mCaretDesc.offset.x + mTextDesc.offset.x - offset.x, -mCaretDesc.offset.y + mTextDesc.offset.y - offset.y,
  34. mTextDesc.width + 1, mTextDesc.height); // Increase clip size by 1, so we can fit the caret in case it is fully at the end of the text
  35. mCaretDesc.texture = GUIManager::instance().getCaretTexture();
  36. mCaretSprite->update(mCaretDesc);
  37. }
  38. void GUIInputCaret::moveCaretToStart()
  39. {
  40. mCaretPos = 0;
  41. }
  42. void GUIInputCaret::moveCaretToEnd()
  43. {
  44. mCaretPos = getMaxCaretPos();
  45. }
  46. void GUIInputCaret::moveCaretLeft()
  47. {
  48. mCaretPos = (UINT32)std::max(0, (INT32)mCaretPos - 1);
  49. }
  50. void GUIInputCaret::moveCaretRight()
  51. {
  52. UINT32 maxCaretPos = getMaxCaretPos();
  53. mCaretPos = std::min(maxCaretPos, mCaretPos + 1);
  54. }
  55. void GUIInputCaret::moveCaretUp()
  56. {
  57. UINT32 charIdx = getCharIdxAtCaretPos();
  58. if(charIdx > 0)
  59. charIdx -= 1;
  60. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  61. const SpriteLineDesc& desc = mTextSprite->getLineDesc(lineIdx);
  62. // If char is a newline, I want that to count as being on the next line because that's
  63. // how user sees it
  64. if(desc.isNewline(charIdx))
  65. lineIdx++;
  66. if(lineIdx == 0)
  67. return;
  68. Int2 caretCoords = getCaretPosition(mTextDesc.offset);
  69. caretCoords.y -= getCaretHeight();
  70. moveCaretToPos(caretCoords);
  71. }
  72. void GUIInputCaret::moveCaretDown()
  73. {
  74. UINT32 charIdx = getCharIdxAtCaretPos();
  75. if(charIdx > 0)
  76. charIdx -= 1;
  77. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  78. const SpriteLineDesc& desc = mTextSprite->getLineDesc(lineIdx);
  79. // If char is a newline, I want that to count as being on the next line because that's
  80. // how user sees it
  81. if(desc.isNewline(charIdx))
  82. lineIdx++;
  83. if(lineIdx == (mTextSprite->getNumLines() - 1))
  84. return;
  85. Int2 caretCoords = getCaretPosition(mTextDesc.offset);
  86. caretCoords.y += getCaretHeight();
  87. moveCaretToPos(caretCoords);
  88. }
  89. void GUIInputCaret::moveCaretToPos(const CM::Int2& pos)
  90. {
  91. INT32 charIdx = mTextSprite->getCharIdxAtPos(pos);
  92. if(charIdx != -1)
  93. {
  94. Rect charRect = mTextSprite->getCharRect(charIdx);
  95. float xCenter = charRect.x + charRect.width * 0.5f;
  96. if(pos.x <= xCenter)
  97. moveCaretToChar(charIdx, CARET_BEFORE);
  98. else
  99. moveCaretToChar(charIdx, CARET_AFTER);
  100. }
  101. else
  102. {
  103. UINT32 numLines = mTextSprite->getNumLines();
  104. UINT32 curPos = 0;
  105. for(UINT32 i = 0; i < numLines; i++)
  106. {
  107. const SpriteLineDesc& line = mTextSprite->getLineDesc(i);
  108. if(pos.y >= line.getLineYStart() && pos.y < (line.getLineYStart() + (INT32)line.getLineHeight()))
  109. {
  110. mCaretPos = curPos;
  111. return;
  112. }
  113. UINT32 numChars = line.getEndChar(false) - line.getStartChar() + 1; // +1 For extra line start position
  114. curPos += numChars;
  115. }
  116. mCaretPos = curPos;
  117. }
  118. }
  119. void GUIInputCaret::moveCaretToChar(UINT32 charIdx, CaretPos caretPos)
  120. {
  121. if(charIdx >= (UINT32)mTextDesc.text.size())
  122. {
  123. mCaretPos = 0;
  124. return;
  125. }
  126. UINT32 numLines = mTextSprite->getNumLines();
  127. UINT32 curPos = 0;
  128. UINT32 curCharIdx = 0;
  129. for(UINT32 i = 0; i < numLines; i++)
  130. {
  131. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  132. curPos++; // Move past line start position
  133. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  134. UINT32 numCaretPositions = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  135. if(charIdx >= (curCharIdx + numChars))
  136. {
  137. curCharIdx += numChars;
  138. curPos += numCaretPositions;
  139. continue;
  140. }
  141. UINT32 diff = charIdx - curCharIdx;
  142. if(caretPos == CARET_BEFORE)
  143. curPos += diff - 1;
  144. else
  145. curPos += diff;
  146. break;
  147. }
  148. mCaretPos = curPos;
  149. }
  150. UINT32 GUIInputCaret::getCharIdxAtCaretPos() const
  151. {
  152. if(mTextDesc.text.size() == 0)
  153. return 0;
  154. UINT32 numLines = mTextSprite->getNumLines();
  155. UINT32 curPos = 0;
  156. UINT32 curCharIdx = 0;
  157. for(UINT32 i = 0; i < numLines; i++)
  158. {
  159. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  160. if(curPos == mCaretPos)
  161. return lineDesc.getStartChar();
  162. curPos++; // Move past line start position
  163. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  164. UINT32 numCaretPositions = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  165. if(mCaretPos >= (curPos + numCaretPositions))
  166. {
  167. curCharIdx += numChars;
  168. curPos += numCaretPositions;
  169. continue;
  170. }
  171. UINT32 diff = mCaretPos - curPos;
  172. curCharIdx += diff + 1; // Character after the caret
  173. return curCharIdx;
  174. }
  175. return 0;
  176. }
  177. Int2 GUIInputCaret::getCaretPosition(const CM::Int2& offset) const
  178. {
  179. if(mTextDesc.text.size() > 0)
  180. {
  181. UINT32 curPos = 0;
  182. UINT32 numLines = mTextSprite->getNumLines();
  183. for(UINT32 i = 0; i < numLines; i++)
  184. {
  185. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  186. if(mCaretPos == curPos)
  187. {
  188. // Caret is on line start
  189. return Int2(offset.x, lineDesc.getLineYStart());
  190. }
  191. curPos += lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  192. }
  193. UINT32 charIdx = getCharIdxAtCaretPos();
  194. if(charIdx > 0)
  195. charIdx -= 1;
  196. charIdx = std::min((UINT32)(mTextDesc.text.size() - 1), charIdx);
  197. Rect charRect = mTextSprite->getCharRect(charIdx);
  198. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  199. UINT32 yOffset = mTextSprite->getLineDesc(lineIdx).getLineYStart();
  200. return Int2(charRect.x + charRect.width, yOffset);
  201. }
  202. else
  203. {
  204. return offset;
  205. }
  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())
  213. {
  214. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  215. return mTextSprite->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. if(mTextDesc.text.size() == 0)
  232. return true;
  233. UINT32 numLines = mTextSprite->getNumLines();
  234. UINT32 curPos = 0;
  235. for(UINT32 i = 0; i < numLines; i++)
  236. {
  237. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  238. if(curPos == mCaretPos)
  239. return true;
  240. UINT32 numChars = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  241. curPos += numChars;
  242. }
  243. return false;
  244. }
  245. UINT32 GUIInputCaret::getMaxCaretPos() const
  246. {
  247. if(mTextDesc.text.size() == 0)
  248. return 0;
  249. UINT32 numLines = mTextSprite->getNumLines();
  250. UINT32 maxPos = 0;
  251. for(UINT32 i = 0; i < numLines; i++)
  252. {
  253. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  254. UINT32 numChars = lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  255. maxPos += numChars;
  256. }
  257. return maxPos - 1;
  258. }
  259. }