BsGUIInputCaret.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. {
  68. moveCaretToStart();
  69. return;
  70. }
  71. Int2 caretCoords = getCaretPosition(mTextDesc.offset);
  72. caretCoords.y -= getCaretHeight();
  73. moveCaretToPos(caretCoords);
  74. }
  75. void GUIInputCaret::moveCaretDown()
  76. {
  77. UINT32 charIdx = getCharIdxAtCaretPos();
  78. if(charIdx > 0)
  79. charIdx -= 1;
  80. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  81. const SpriteLineDesc& desc = mTextSprite->getLineDesc(lineIdx);
  82. // If char is a newline, I want that to count as being on the next line because that's
  83. // how user sees it
  84. if(desc.isNewline(charIdx))
  85. lineIdx++;
  86. if(lineIdx == (mTextSprite->getNumLines() - 1))
  87. {
  88. moveCaretToEnd();
  89. return;
  90. }
  91. Int2 caretCoords = getCaretPosition(mTextDesc.offset);
  92. caretCoords.y += getCaretHeight();
  93. moveCaretToPos(caretCoords);
  94. }
  95. void GUIInputCaret::moveCaretToPos(const CM::Int2& pos)
  96. {
  97. INT32 charIdx = mTextSprite->getCharIdxAtPos(pos);
  98. if(charIdx != -1)
  99. {
  100. Rect charRect = mTextSprite->getCharRect(charIdx);
  101. float xCenter = charRect.x + charRect.width * 0.5f;
  102. if(pos.x <= xCenter)
  103. moveCaretToChar(charIdx, CARET_BEFORE);
  104. else
  105. moveCaretToChar(charIdx, CARET_AFTER);
  106. }
  107. else
  108. {
  109. UINT32 numLines = mTextSprite->getNumLines();
  110. if(numLines == 0)
  111. {
  112. mCaretPos = 0;
  113. return;
  114. }
  115. UINT32 curPos = 0;
  116. for(UINT32 i = 0; i < numLines; i++)
  117. {
  118. const SpriteLineDesc& line = mTextSprite->getLineDesc(i);
  119. if(pos.y >= line.getLineYStart() && pos.y < (line.getLineYStart() + (INT32)line.getLineHeight()))
  120. {
  121. mCaretPos = curPos;
  122. return;
  123. }
  124. UINT32 numChars = line.getEndChar(false) - line.getStartChar() + 1; // +1 For extra line start position
  125. curPos += numChars;
  126. }
  127. const SpriteLineDesc& firstLine = mTextSprite->getLineDesc(0);
  128. if(pos.y < firstLine.getLineYStart()) // Before first line
  129. mCaretPos = 0;
  130. else // After last line
  131. mCaretPos = curPos - 1;
  132. }
  133. }
  134. void GUIInputCaret::moveCaretToChar(UINT32 charIdx, CaretPos caretPos)
  135. {
  136. if(charIdx >= (UINT32)mTextDesc.text.size())
  137. {
  138. mCaretPos = 0;
  139. return;
  140. }
  141. UINT32 numLines = mTextSprite->getNumLines();
  142. UINT32 curPos = 0;
  143. UINT32 curCharIdx = 0;
  144. for(UINT32 i = 0; i < numLines; i++)
  145. {
  146. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  147. curPos++; // Move past line start position
  148. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  149. UINT32 numCaretPositions = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  150. if(charIdx >= (curCharIdx + numChars))
  151. {
  152. curCharIdx += numChars;
  153. curPos += numCaretPositions;
  154. continue;
  155. }
  156. UINT32 diff = charIdx - curCharIdx;
  157. if(caretPos == CARET_BEFORE)
  158. curPos += diff - 1;
  159. else
  160. curPos += diff;
  161. break;
  162. }
  163. mCaretPos = curPos;
  164. }
  165. UINT32 GUIInputCaret::getCharIdxAtCaretPos() const
  166. {
  167. return getCharIdxAtCaretPos(mCaretPos);
  168. }
  169. UINT32 GUIInputCaret::getCharIdxAtCaretPos(UINT32 caretPos) const
  170. {
  171. if(mTextDesc.text.size() == 0)
  172. return 0;
  173. UINT32 numLines = mTextSprite->getNumLines();
  174. UINT32 curPos = 0;
  175. UINT32 curCharIdx = 0;
  176. for(UINT32 i = 0; i < numLines; i++)
  177. {
  178. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  179. if(curPos == caretPos)
  180. return lineDesc.getStartChar();
  181. curPos++; // Move past line start position
  182. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  183. UINT32 numCaretPositions = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  184. if(caretPos >= (curPos + numCaretPositions))
  185. {
  186. curCharIdx += numChars;
  187. curPos += numCaretPositions;
  188. continue;
  189. }
  190. UINT32 diff = caretPos - curPos;
  191. curCharIdx += diff + 1; // Character after the caret
  192. return curCharIdx;
  193. }
  194. return 0;
  195. }
  196. Int2 GUIInputCaret::getCaretPosition(const CM::Int2& offset) const
  197. {
  198. if(mTextDesc.text.size() > 0)
  199. {
  200. UINT32 curPos = 0;
  201. UINT32 numLines = mTextSprite->getNumLines();
  202. for(UINT32 i = 0; i < numLines; i++)
  203. {
  204. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  205. if(mCaretPos == curPos)
  206. {
  207. // Caret is on line start
  208. return Int2(offset.x, lineDesc.getLineYStart());
  209. }
  210. curPos += lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  211. }
  212. UINT32 charIdx = getCharIdxAtCaretPos();
  213. if(charIdx > 0)
  214. charIdx -= 1;
  215. charIdx = std::min((UINT32)(mTextDesc.text.size() - 1), charIdx);
  216. Rect charRect = mTextSprite->getCharRect(charIdx);
  217. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  218. UINT32 yOffset = mTextSprite->getLineDesc(lineIdx).getLineYStart();
  219. return Int2(charRect.x + charRect.width, yOffset);
  220. }
  221. else
  222. {
  223. return offset;
  224. }
  225. }
  226. UINT32 GUIInputCaret::getCaretHeight() const
  227. {
  228. UINT32 charIdx = getCharIdxAtCaretPos();
  229. if(charIdx > 0)
  230. charIdx -= 1;
  231. if(charIdx < (UINT32)mTextDesc.text.size())
  232. {
  233. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  234. return mTextSprite->getLineDesc(lineIdx).getLineHeight();
  235. }
  236. else
  237. {
  238. if(mTextDesc.font != nullptr)
  239. {
  240. UINT32 nearestSize = mTextDesc.font->getClosestAvailableSize(mTextDesc.fontSize);
  241. const FontData* fontData = mTextDesc.font->getFontDataForSize(nearestSize);
  242. if(fontData != nullptr)
  243. return fontData->fontDesc.lineHeight;
  244. }
  245. }
  246. return 0;
  247. }
  248. bool GUIInputCaret::isCaretAtNewline() const
  249. {
  250. if(mTextDesc.text.size() == 0)
  251. return true;
  252. UINT32 numLines = mTextSprite->getNumLines();
  253. UINT32 curPos = 0;
  254. for(UINT32 i = 0; i < numLines; i++)
  255. {
  256. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  257. if(curPos == mCaretPos)
  258. return true;
  259. UINT32 numChars = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  260. curPos += numChars;
  261. }
  262. return false;
  263. }
  264. UINT32 GUIInputCaret::getMaxCaretPos() const
  265. {
  266. if(mTextDesc.text.size() == 0)
  267. return 0;
  268. UINT32 numLines = mTextSprite->getNumLines();
  269. UINT32 maxPos = 0;
  270. for(UINT32 i = 0; i < numLines; i++)
  271. {
  272. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  273. UINT32 numChars = lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  274. maxPos += numChars;
  275. }
  276. return maxPos - 1;
  277. }
  278. }