BsGUIInputCaret.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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(const CM::Int2& offset)
  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 getCharIdxAtCaretPos(mCaretPos);
  166. }
  167. UINT32 GUIInputCaret::getCharIdxAtCaretPos(UINT32 caretPos) const
  168. {
  169. if(mTextDesc.text.size() == 0)
  170. return 0;
  171. UINT32 numLines = getNumLines();
  172. UINT32 curPos = 0;
  173. UINT32 curCharIdx = 0;
  174. for(UINT32 i = 0; i < numLines; i++)
  175. {
  176. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  177. if(curPos == caretPos)
  178. return lineDesc.getStartChar();
  179. curPos++; // Move past line start position
  180. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  181. UINT32 numCaretPositions = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  182. if(caretPos >= (curPos + numCaretPositions))
  183. {
  184. curCharIdx += numChars;
  185. curPos += numCaretPositions;
  186. continue;
  187. }
  188. UINT32 diff = caretPos - curPos;
  189. curCharIdx += diff + 1; // Character after the caret
  190. return curCharIdx;
  191. }
  192. return 0;
  193. }
  194. Int2 GUIInputCaret::getCaretPosition(const CM::Int2& offset) const
  195. {
  196. if(mTextDesc.text.size() > 0)
  197. {
  198. UINT32 curPos = 0;
  199. UINT32 numLines = getNumLines();
  200. for(UINT32 i = 0; i < numLines; i++)
  201. {
  202. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  203. if(mCaretPos == curPos)
  204. {
  205. // Caret is on line start
  206. return Int2(offset.x, lineDesc.getLineYStart());
  207. }
  208. curPos += lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  209. }
  210. UINT32 charIdx = getCharIdxAtCaretPos();
  211. if(charIdx > 0)
  212. charIdx -= 1;
  213. charIdx = std::min((UINT32)(mTextDesc.text.size() - 1), charIdx);
  214. Rect charRect = getCharRect(charIdx);
  215. UINT32 lineIdx = getLineForChar(charIdx);
  216. UINT32 yOffset = getLineDesc(lineIdx).getLineYStart();
  217. return Int2(charRect.x + charRect.width, yOffset);
  218. }
  219. else
  220. {
  221. return offset;
  222. }
  223. }
  224. UINT32 GUIInputCaret::getCaretHeight() const
  225. {
  226. UINT32 charIdx = getCharIdxAtCaretPos();
  227. if(charIdx > 0)
  228. charIdx -= 1;
  229. if(charIdx < (UINT32)mTextDesc.text.size())
  230. {
  231. UINT32 lineIdx = getLineForChar(charIdx);
  232. return getLineDesc(lineIdx).getLineHeight();
  233. }
  234. else
  235. {
  236. if(mTextDesc.font != nullptr)
  237. {
  238. UINT32 nearestSize = mTextDesc.font->getClosestAvailableSize(mTextDesc.fontSize);
  239. const FontData* fontData = mTextDesc.font->getFontDataForSize(nearestSize);
  240. if(fontData != nullptr)
  241. return fontData->fontDesc.lineHeight;
  242. }
  243. }
  244. return 0;
  245. }
  246. bool GUIInputCaret::isCaretAtNewline() const
  247. {
  248. if(mTextDesc.text.size() == 0)
  249. return true;
  250. UINT32 numLines = getNumLines();
  251. UINT32 curPos = 0;
  252. for(UINT32 i = 0; i < numLines; i++)
  253. {
  254. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  255. if(curPos == mCaretPos)
  256. return true;
  257. UINT32 numChars = lineDesc.getEndChar(false) - lineDesc.getStartChar();
  258. curPos += numChars;
  259. }
  260. return false;
  261. }
  262. UINT32 GUIInputCaret::getMaxCaretPos() const
  263. {
  264. if(mTextDesc.text.size() == 0)
  265. return 0;
  266. UINT32 numLines = getNumLines();
  267. UINT32 maxPos = 0;
  268. for(UINT32 i = 0; i < numLines; i++)
  269. {
  270. const GUIInputLineDesc& lineDesc = getLineDesc(i);
  271. UINT32 numChars = lineDesc.getEndChar(false) - lineDesc.getStartChar() + 1; // + 1 for special line start position
  272. maxPos += numChars;
  273. }
  274. return maxPos - 1;
  275. }
  276. }