BsGUIInputCaret.cpp 8.5 KB

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