BsGUIInputCaret.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 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. {
  100. //UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  101. //const SpriteLineDesc& line = mTextSprite->getLineDesc(lineIdx);
  102. //if(charIdx == (line.getEndChar(false) - 1)) // If last char on the line don't move beyond it
  103. // moveCaretToChar(charIdx, CARET_BEFORE);
  104. //else
  105. moveCaretToChar(charIdx, CARET_AFTER);
  106. }
  107. }
  108. else
  109. {
  110. UINT32 numLines = mTextSprite->getNumLines();
  111. UINT32 curPos = 0;
  112. for(UINT32 i = 0; i < numLines; i++)
  113. {
  114. const SpriteLineDesc& line = mTextSprite->getLineDesc(i);
  115. if(pos.y >= line.getLineYStart() && pos.y < (line.getLineYStart() + (INT32)line.getLineHeight()))
  116. {
  117. mCaretPos = curPos;
  118. return;
  119. }
  120. UINT32 numChars = line.getEndChar() - line.getStartChar();
  121. curPos += numChars;
  122. }
  123. mCaretPos = curPos;
  124. }
  125. }
  126. void GUIInputCaret::moveCaretToChar(UINT32 charIdx, CaretPos caretPos)
  127. {
  128. if(charIdx >= (UINT32)mTextDesc.text.size())
  129. {
  130. mCaretPos = 0;
  131. return;
  132. }
  133. UINT32 numLines = mTextSprite->getNumLines();
  134. UINT32 curPos = 0;
  135. UINT32 curCharIdx = 0;
  136. for(UINT32 i = 0; i < numLines; i++)
  137. {
  138. if(i == 0)
  139. curPos++; // Beginning of the line has a special caret position, primarily so we can
  140. // still place a caret on an empty line
  141. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  142. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  143. if(charIdx > (curCharIdx + numChars))
  144. {
  145. curCharIdx += numChars;
  146. curPos += numChars;
  147. continue;
  148. }
  149. UINT32 diff = charIdx - curCharIdx;
  150. if(caretPos == CARET_BEFORE)
  151. curPos += diff - 1;
  152. else
  153. curPos += diff;
  154. break;
  155. }
  156. mCaretPos = curPos;
  157. }
  158. UINT32 GUIInputCaret::getCharIdxAtCaretPos() const
  159. {
  160. if(mTextDesc.text.size() == 0)
  161. return 0;
  162. UINT32 numLines = mTextSprite->getNumLines();
  163. UINT32 curPos = 0;
  164. UINT32 curCharIdx = 0;
  165. for(UINT32 i = 0; i < numLines; i++)
  166. {
  167. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  168. if(curPos == mCaretPos)
  169. {
  170. return lineDesc.getStartChar();
  171. }
  172. if(i == 0)
  173. curPos++; // Beginning of the line has a special caret position, primarily so we can
  174. // still place a caret on an empty line
  175. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  176. if(mCaretPos > (curPos + numChars))
  177. {
  178. curCharIdx += numChars;
  179. curPos += numChars;
  180. continue;
  181. }
  182. UINT32 diff = mCaretPos - curPos;
  183. curCharIdx += diff + 1; // +1 because we want the caret to reference the char in front of it on most cases
  184. return curCharIdx;
  185. }
  186. return 0;
  187. }
  188. Int2 GUIInputCaret::getCaretPosition(const CM::Int2& offset) const
  189. {
  190. if(mTextDesc.text.size() > 0)
  191. {
  192. UINT32 curPos = 0;
  193. UINT32 numLines = mTextSprite->getNumLines();
  194. for(UINT32 i = 0; i < numLines; i++)
  195. {
  196. if(mCaretPos == curPos)
  197. {
  198. // Caret is on line start
  199. return Int2(offset.x, mTextSprite->getLineDesc(i).getLineYStart());
  200. }
  201. if(i == 0)
  202. curPos++; // Beginning of the line has a special caret position, primarily so we can
  203. // still place a caret on an empty line
  204. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  205. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  206. curPos += numChars;
  207. }
  208. UINT32 charIdx = getCharIdxAtCaretPos();
  209. if(charIdx > 0)
  210. charIdx -= 1;
  211. charIdx = std::min((UINT32)(mTextDesc.text.size() - 1), charIdx);
  212. Rect charRect = mTextSprite->getCharRect(charIdx);
  213. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  214. UINT32 yOffset = mTextSprite->getLineDesc(lineIdx).getLineYStart();
  215. return Int2(charRect.x + charRect.width + 1, yOffset);
  216. }
  217. else
  218. {
  219. return offset;
  220. }
  221. }
  222. UINT32 GUIInputCaret::getCaretHeight() const
  223. {
  224. UINT32 charIdx = getCharIdxAtCaretPos();
  225. if(charIdx > 0)
  226. charIdx -= 1;
  227. if(charIdx < (UINT32)mTextDesc.text.size())
  228. {
  229. UINT32 lineIdx = mTextSprite->getLineForChar(charIdx);
  230. return mTextSprite->getLineDesc(lineIdx).getLineHeight();
  231. }
  232. else
  233. {
  234. if(mTextDesc.font != nullptr)
  235. {
  236. UINT32 nearestSize = mTextDesc.font->getClosestAvailableSize(mTextDesc.fontSize);
  237. const FontData* fontData = mTextDesc.font->getFontDataForSize(nearestSize);
  238. if(fontData != nullptr)
  239. return fontData->fontDesc.lineHeight;
  240. }
  241. }
  242. return 0;
  243. }
  244. bool GUIInputCaret::isCaretAtNewline() const
  245. {
  246. if(mTextDesc.text.size() == 0)
  247. return true;
  248. UINT32 numLines = mTextSprite->getNumLines();
  249. UINT32 curPos = 0;
  250. for(UINT32 i = 0; i < numLines; i++)
  251. {
  252. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  253. if(curPos == mCaretPos)
  254. return true;
  255. if(i == 0)
  256. curPos++; // Beginning of the line has a special caret position, primarily so we can
  257. // still place a caret on an empty line
  258. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar() - 1;
  259. curPos += numChars;
  260. }
  261. return false;
  262. }
  263. UINT32 GUIInputCaret::getMaxCaretPos() const
  264. {
  265. if(mTextDesc.text.size() == 0)
  266. return 0;
  267. UINT32 numLines = mTextSprite->getNumLines();
  268. UINT32 maxPos = 0;
  269. for(UINT32 i = 0; i < numLines; i++)
  270. {
  271. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  272. if(i == 0)
  273. maxPos++; // Beginning of the line has a special caret position, primarily so we can
  274. // still place a caret on an empty line
  275. UINT32 numChars = lineDesc.getEndChar() - lineDesc.getStartChar();
  276. maxPos += numChars;
  277. }
  278. return maxPos - 1;
  279. }
  280. }