BsGUIInputCaret.cpp 6.8 KB

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