BsGUIInputBox.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. #include "BsGUIInputBox.h"
  2. #include "BsGUIManager.h"
  3. #include "BsImageSprite.h"
  4. #include "BsGUIWidget.h"
  5. #include "BsGUISkin.h"
  6. #include "BsSpriteTexture.h"
  7. #include "BsTextSprite.h"
  8. #include "BsGUILayoutOptions.h"
  9. #include "BsGUIButtonEvent.h"
  10. #include "BsGUIMouseEvent.h"
  11. #include "BsGUICommandEvent.h"
  12. #include "CmFont.h"
  13. #include "CmTextUtility.h"
  14. #include "CmTexture.h"
  15. #include "CmCursor.h"
  16. using namespace CamelotFramework;
  17. namespace BansheeEngine
  18. {
  19. const String& GUIInputBox::getGUITypeName()
  20. {
  21. static String name = "InputBox";
  22. return name;
  23. }
  24. GUIInputBox::GUIInputBox(GUIWidget& parent, const GUIElementStyle* style, const GUILayoutOptions& layoutOptions, bool multiline)
  25. :GUIElement(parent, style, layoutOptions), mInputCursorSet(false), mDragInProgress(false),
  26. mSelectionStart(0), mSelectionEnd(0), mSelectionAnchor(0), mInputCaret(nullptr), mCaretShown(false),
  27. mSelectionShown(false), mIsMultiline(multiline)
  28. {
  29. mImageSprite = cm_new<ImageSprite, PoolAlloc>();
  30. mTextSprite = cm_new<TextSprite, PoolAlloc>();
  31. mInputCaret = cm_new<GUIInputCaret, PoolAlloc>(getTextDesc());
  32. mImageDesc.texture = mStyle->normal.texture;
  33. if(mImageDesc.texture != nullptr)
  34. {
  35. mImageDesc.width = mImageDesc.texture->getTexture()->getWidth();
  36. mImageDesc.height = mImageDesc.texture->getTexture()->getHeight();
  37. }
  38. mImageDesc.borderLeft = mStyle->border.left;
  39. mImageDesc.borderRight = mStyle->border.right;
  40. mImageDesc.borderTop = mStyle->border.top;
  41. mImageDesc.borderBottom = mStyle->border.bottom;
  42. }
  43. GUIInputBox::~GUIInputBox()
  44. {
  45. cm_delete<PoolAlloc>(mTextSprite);
  46. cm_delete<PoolAlloc>(mImageSprite);
  47. cm_delete<PoolAlloc>(mInputCaret);
  48. for(auto& sprite : mSelectionSprites)
  49. cm_delete(sprite);
  50. }
  51. GUIInputBox* GUIInputBox::create(GUIWidget& parent, bool multiline, const GUIElementStyle* style)
  52. {
  53. if(style == nullptr)
  54. {
  55. const GUISkin* skin = parent.getSkin();
  56. style = skin->getStyle(getGUITypeName());
  57. }
  58. return new (cm_alloc<GUIInputBox, PoolAlloc>()) GUIInputBox(parent, style, getDefaultLayoutOptions(style), multiline);
  59. }
  60. GUIInputBox* GUIInputBox::create(GUIWidget& parent, const GUILayoutOptions& layoutOptions, bool multiline, const GUIElementStyle* style)
  61. {
  62. if(style == nullptr)
  63. {
  64. const GUISkin* skin = parent.getSkin();
  65. style = skin->getStyle(getGUITypeName());
  66. }
  67. return new (cm_alloc<GUIInputBox, PoolAlloc>()) GUIInputBox(parent, style, layoutOptions, multiline);
  68. }
  69. UINT32 GUIInputBox::getNumRenderElements() const
  70. {
  71. UINT32 numElements = mImageSprite->getNumRenderElements();
  72. numElements += mTextSprite->getNumRenderElements();
  73. if(mCaretShown && GUIManager::instance().getCaretBlinkState())
  74. numElements += mInputCaret->getSprite()->getNumRenderElements();
  75. if(mSelectionShown)
  76. {
  77. for(auto& selectionSprite : mSelectionSprites)
  78. {
  79. numElements += selectionSprite->getNumRenderElements();
  80. }
  81. }
  82. return numElements;
  83. }
  84. const HMaterial& GUIInputBox::getMaterial(UINT32 renderElementIdx) const
  85. {
  86. UINT32 localRenderElementIdx;
  87. Sprite* sprite = renderElemToSprite(renderElementIdx, localRenderElementIdx);
  88. return sprite->getMaterial(localRenderElementIdx);
  89. }
  90. UINT32 GUIInputBox::getNumQuads(UINT32 renderElementIdx) const
  91. {
  92. UINT32 localRenderElementIdx;
  93. Sprite* sprite = renderElemToSprite(renderElementIdx, localRenderElementIdx);
  94. return sprite->getNumQuads(localRenderElementIdx);
  95. }
  96. void GUIInputBox::updateRenderElementsInternal()
  97. {
  98. mImageDesc.offset = mOffset;
  99. mImageDesc.width = mWidth;
  100. mImageDesc.height = mHeight;
  101. mImageDesc.clipRect = mClipRect;
  102. mImageSprite->update(mImageDesc);
  103. mBounds = mImageSprite->getBounds();
  104. TEXT_SPRITE_DESC textDesc = getTextDesc();
  105. mTextSprite->update(textDesc);
  106. if(mCaretShown && GUIManager::instance().getCaretBlinkState())
  107. {
  108. mInputCaret->updateText(textDesc);
  109. mInputCaret->updateSprite(mTextOffset);
  110. }
  111. if(mSelectionShown)
  112. {
  113. Vector<Rect>::type selectionRects = getSelectionRects();
  114. INT32 diff = (INT32)(mSelectionSprites.size() - selectionRects.size());
  115. if(diff > 0)
  116. {
  117. for(UINT32 i = (UINT32)selectionRects.size(); i < (UINT32)mSelectionSprites.size(); i++)
  118. cm_delete(mSelectionSprites[i]);
  119. mSelectionSprites.erase(mSelectionSprites.begin() + selectionRects.size(), mSelectionSprites.end());
  120. }
  121. else if(diff < 0)
  122. {
  123. for(INT32 i = diff; i < 0; i++)
  124. {
  125. ImageSprite* newSprite = cm_new<ImageSprite>();
  126. mSelectionSprites.push_back(newSprite);
  127. }
  128. }
  129. UINT32 idx = 0;
  130. for(auto& sprite : mSelectionSprites)
  131. {
  132. IMAGE_SPRITE_DESC desc;
  133. desc.offset = Int2(selectionRects[idx].x, selectionRects[idx].y);
  134. desc.width = selectionRects[idx].width;
  135. desc.height = selectionRects[idx].height;
  136. desc.clipRect = Rect(0, 0, textDesc.width, textDesc.height);
  137. desc.texture = GUIManager::instance().getTextSelectionTexture();
  138. sprite->update(desc);
  139. idx++;
  140. }
  141. }
  142. }
  143. Sprite* GUIInputBox::renderElemToSprite(UINT32 renderElemIdx, UINT32& localRenderElemIdx) const
  144. {
  145. UINT32 oldNumElements = 0;
  146. UINT32 newNumElements = oldNumElements + mTextSprite->getNumRenderElements();
  147. if(renderElemIdx < newNumElements)
  148. {
  149. localRenderElemIdx = renderElemIdx - oldNumElements;
  150. return mTextSprite;
  151. }
  152. oldNumElements = newNumElements;
  153. newNumElements += mImageSprite->getNumRenderElements();
  154. if(renderElemIdx < newNumElements)
  155. {
  156. localRenderElemIdx = renderElemIdx - oldNumElements;
  157. return mImageSprite;
  158. }
  159. if(mCaretShown && GUIManager::instance().getCaretBlinkState())
  160. {
  161. oldNumElements = newNumElements;
  162. newNumElements += mInputCaret->getSprite()->getNumRenderElements();
  163. if(renderElemIdx < newNumElements)
  164. {
  165. localRenderElemIdx = renderElemIdx - oldNumElements;
  166. return mInputCaret->getSprite();
  167. }
  168. }
  169. if(mSelectionShown)
  170. {
  171. for(auto& selectionSprite : mSelectionSprites)
  172. {
  173. oldNumElements = newNumElements;
  174. newNumElements += selectionSprite->getNumRenderElements();
  175. if(renderElemIdx < newNumElements)
  176. {
  177. localRenderElemIdx = renderElemIdx - oldNumElements;
  178. return selectionSprite;
  179. }
  180. }
  181. }
  182. localRenderElemIdx = renderElemIdx;
  183. return nullptr;
  184. }
  185. UINT32 GUIInputBox::_getOptimalWidth() const
  186. {
  187. if(mImageDesc.texture != nullptr)
  188. {
  189. return mImageDesc.texture->getTexture()->getWidth();
  190. }
  191. return 0;
  192. }
  193. UINT32 GUIInputBox::_getOptimalHeight() const
  194. {
  195. if(mImageDesc.texture != nullptr)
  196. {
  197. return mImageDesc.texture->getTexture()->getHeight();
  198. }
  199. return 0;
  200. }
  201. UINT32 GUIInputBox::_getRenderElementDepth(UINT32 renderElementIdx) const
  202. {
  203. UINT32 localRenderElementIdx;
  204. Sprite* sprite = renderElemToSprite(renderElementIdx, localRenderElementIdx);
  205. if(sprite == mImageSprite)
  206. return _getDepth();
  207. else if(sprite == mTextSprite)
  208. return _getDepth() - 2;
  209. else if(sprite == mInputCaret->getSprite())
  210. return _getDepth() - 3;
  211. else // Selection sprites
  212. return _getDepth() - 1;
  213. }
  214. void GUIInputBox::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  215. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  216. {
  217. UINT32 localRenderElementIdx;
  218. Sprite* sprite = renderElemToSprite(renderElementIdx, localRenderElementIdx);
  219. sprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads, vertexStride, indexStride, localRenderElementIdx);
  220. }
  221. bool GUIInputBox::mouseEvent(const GUIMouseEvent& ev)
  222. {
  223. if(ev.getType() == GUIMouseEventType::MouseOver)
  224. {
  225. mImageDesc.texture = mStyle->hover.texture;
  226. markAsDirty();
  227. if(!mInputCursorSet)
  228. {
  229. Cursor::setCursor(CursorType::IBeam);
  230. mInputCursorSet = true;
  231. }
  232. return true;
  233. }
  234. else if(ev.getType() == GUIMouseEventType::MouseOut)
  235. {
  236. mImageDesc.texture = mStyle->normal.texture;
  237. markAsDirty();
  238. if(!mDragInProgress && mInputCursorSet)
  239. {
  240. Cursor::setCursor(CursorType::Arrow);
  241. mInputCursorSet = false;
  242. }
  243. return true;
  244. }
  245. else if(ev.getType() == GUIMouseEventType::MouseDown)
  246. {
  247. mImageDesc.texture = mStyle->active.texture;
  248. showCaret();
  249. if(mText.size() > 0)
  250. mInputCaret->moveCaretToPos(ev.getPosition());
  251. else
  252. mInputCaret->moveCaretToStart();
  253. scrollTextToCaret();
  254. clearSelection();
  255. markAsDirty();
  256. return true;
  257. }
  258. else if(ev.getType() == GUIMouseEventType::MouseUp)
  259. {
  260. mImageDesc.texture = mStyle->hover.texture;
  261. markAsDirty();
  262. return true;
  263. }
  264. else if(ev.getType() == GUIMouseEventType::MouseDragStart)
  265. {
  266. mDragInProgress = true;
  267. return true;
  268. }
  269. else if(ev.getType() == GUIMouseEventType::MouseDragEnd)
  270. {
  271. mDragInProgress = false;
  272. if(ev.getMouseOverElement() != this && mInputCursorSet)
  273. {
  274. Cursor::setCursor(CursorType::Arrow);
  275. mInputCursorSet = false;
  276. }
  277. return true;
  278. }
  279. else if(ev.getType() == GUIMouseEventType::MouseDrag)
  280. {
  281. // TODO - Update selection
  282. // - If mouse is over control, place selection marker there (make sure start < end)
  283. // - Else move the selection by a certain amount of pixels depending on drag amount
  284. markAsDirty();
  285. }
  286. return false;
  287. }
  288. bool GUIInputBox::keyEvent(const GUIKeyEvent& ev)
  289. {
  290. if(ev.getType() == GUIKeyEventType::KeyDown)
  291. {
  292. if(ev.getKey() == BC_BACK)
  293. {
  294. if(mText.size() > 0)
  295. {
  296. if(mSelectionShown)
  297. {
  298. mText.erase(mText.begin() + mSelectionStart, mText.begin() + mSelectionEnd);
  299. mInputCaret->updateText(getTextDesc());
  300. if(mSelectionStart > 0)
  301. {
  302. UINT32 newCaretPos = mSelectionStart - 1;
  303. mInputCaret->moveCaretToChar(newCaretPos, CARET_AFTER);
  304. }
  305. else
  306. {
  307. mInputCaret->moveCaretToChar(0, CARET_BEFORE);
  308. }
  309. scrollTextToCaret();
  310. clearSelection();
  311. }
  312. else
  313. {
  314. UINT32 charIdx = mInputCaret->getCharIdxAtCaretPos() - 1;
  315. if(charIdx < (UINT32)mText.size())
  316. {
  317. mText.erase(charIdx, 1);
  318. mInputCaret->updateText(getTextDesc());
  319. mInputCaret->moveCaretLeft();
  320. scrollTextToCaret();
  321. }
  322. }
  323. markAsDirty();
  324. }
  325. return true;
  326. }
  327. if(ev.getKey() == BC_DELETE)
  328. {
  329. if(mText.size() > 0)
  330. {
  331. if(mSelectionShown)
  332. {
  333. mText.erase(mText.begin() + mSelectionStart, mText.begin() + mSelectionEnd);
  334. mInputCaret->updateText(getTextDesc());
  335. if(mSelectionStart > 0)
  336. {
  337. UINT32 newCaretPos = mSelectionStart - 1;
  338. mInputCaret->moveCaretToChar(newCaretPos, CARET_AFTER);
  339. }
  340. else
  341. {
  342. mInputCaret->moveCaretToChar(0, CARET_BEFORE);
  343. }
  344. scrollTextToCaret();
  345. clearSelection();
  346. }
  347. else
  348. {
  349. UINT32 charIdx = mInputCaret->getCharIdxAtCaretPos();
  350. if(charIdx < (UINT32)mText.size())
  351. {
  352. mText.erase(charIdx, 1);
  353. mInputCaret->updateText(getTextDesc());
  354. }
  355. }
  356. markAsDirty();
  357. }
  358. return true;
  359. }
  360. if(ev.getKey() == BC_LEFT)
  361. {
  362. if(ev.isShiftDown())
  363. {
  364. bool caretMovedDueToNewline = false;
  365. if(!mSelectionShown)
  366. {
  367. if(isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)))
  368. {
  369. mInputCaret->moveCaretLeft();
  370. caretMovedDueToNewline = true;
  371. }
  372. showSelection(getCaretSelectionCharIdx(SelectionDir::Left));
  373. }
  374. if(mSelectionAnchor == mSelectionEnd)
  375. {
  376. mInputCaret->moveCaretLeft();
  377. UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  378. if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
  379. {
  380. if (isNewlineChar(charIdx) && mInputCaret->getCaretPos() > 0)
  381. {
  382. mInputCaret->moveCaretLeft();
  383. charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  384. // Reverse caret movement if previous char was a newline, and this one is as well
  385. // so we don't skip a line
  386. if (isNewlineChar(charIdx))
  387. {
  388. mInputCaret->moveCaretRight();
  389. }
  390. }
  391. }
  392. else
  393. {
  394. // Reverse caret movement if previous char was a newline, and this one is as well
  395. // so we don't skip a line
  396. if (isNewlineChar(charIdx))
  397. {
  398. mInputCaret->moveCaretRight();
  399. }
  400. }
  401. charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  402. mSelectionStart = std::min(mSelectionEnd, charIdx);
  403. }
  404. else
  405. {
  406. mInputCaret->moveCaretLeft();
  407. UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  408. if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
  409. {
  410. if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right))
  411. && mInputCaret->getCaretPos() > 0)
  412. {
  413. mInputCaret->moveCaretLeft();
  414. charIdx = getCaretSelectionCharIdx(SelectionDir::Right);
  415. // Reverse caret movement if previous char was a newline, and this one is as well
  416. // so we don't skip a line
  417. if (isNewlineChar(charIdx))
  418. {
  419. mInputCaret->moveCaretRight();
  420. }
  421. }
  422. }
  423. else
  424. {
  425. // Reverse caret movement if previous char was a newline, and this one is as well
  426. // so we don't skip a line
  427. if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)))
  428. {
  429. mInputCaret->moveCaretRight();
  430. }
  431. }
  432. charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  433. mSelectionEnd = std::max(mSelectionStart, charIdx);
  434. }
  435. if(mSelectionStart == mSelectionEnd)
  436. clearSelection();
  437. scrollTextToCaret();
  438. markAsDirty();
  439. return true;
  440. }
  441. else
  442. {
  443. clearSelection();
  444. mInputCaret->moveCaretLeft();
  445. scrollTextToCaret();
  446. markAsDirty();
  447. return true;
  448. }
  449. }
  450. if(ev.getKey() == BC_RIGHT)
  451. {
  452. if(ev.isShiftDown())
  453. {
  454. bool caretMovedDueToNewline = false;
  455. if(!mSelectionShown)
  456. {
  457. if(isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Left)))
  458. {
  459. mInputCaret->moveCaretRight();
  460. caretMovedDueToNewline = true;
  461. }
  462. showSelection(getCaretSelectionCharIdx(SelectionDir::Left));
  463. }
  464. if(mSelectionAnchor == mSelectionStart)
  465. {
  466. mInputCaret->moveCaretRight();
  467. UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  468. if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
  469. {
  470. UINT32 maxCaretPos = mInputCaret->getMaxCaretPos();
  471. if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)) && mInputCaret->getCaretPos() <= maxCaretPos)
  472. {
  473. mInputCaret->moveCaretRight();
  474. charIdx = getCaretSelectionCharIdx(SelectionDir::Right);
  475. // Reverse caret movement if previous char was a newline, and this one is as well
  476. // so we don't skip a line
  477. if (isNewlineChar(charIdx))
  478. {
  479. mInputCaret->moveCaretLeft();
  480. }
  481. }
  482. }
  483. else
  484. {
  485. // Reverse caret movement if previous char was a newline, and this one is as well
  486. // so we don't skip a line
  487. if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)))
  488. {
  489. mInputCaret->moveCaretLeft();
  490. }
  491. }
  492. charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  493. mSelectionEnd = std::max(mSelectionStart, charIdx);
  494. }
  495. else
  496. {
  497. mInputCaret->moveCaretRight();
  498. UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  499. if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
  500. {
  501. UINT32 maxCaretPos = mInputCaret->getMaxCaretPos();
  502. if (isNewlineChar(charIdx) && mInputCaret->getCaretPos() <= maxCaretPos)
  503. {
  504. mInputCaret->moveCaretRight();
  505. charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  506. // Reverse caret movement if previous char was a newline, and this one is as well
  507. // so we don't skip a line
  508. if (isNewlineChar(charIdx))
  509. {
  510. mInputCaret->moveCaretLeft();
  511. }
  512. }
  513. }
  514. else
  515. {
  516. // Reverse caret movement if previous char was a newline, and this one is as well
  517. // so we don't skip a line
  518. if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)))
  519. {
  520. mInputCaret->moveCaretLeft();
  521. }
  522. }
  523. charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
  524. mSelectionStart = std::min(mSelectionEnd, charIdx);
  525. }
  526. if(mSelectionStart == mSelectionEnd)
  527. clearSelection();
  528. scrollTextToCaret();
  529. markAsDirty();
  530. return true;
  531. }
  532. else
  533. {
  534. clearSelection();
  535. mInputCaret->moveCaretRight();
  536. scrollTextToCaret();
  537. markAsDirty();
  538. return true;
  539. }
  540. }
  541. if(ev.getKey() == BC_UP)
  542. {
  543. if(ev.isShiftDown())
  544. {
  545. // TODO
  546. markAsDirty();
  547. return true;
  548. }
  549. else
  550. {
  551. clearSelection();
  552. mInputCaret->moveCaretUp();
  553. scrollTextToCaret();
  554. markAsDirty();
  555. return true;
  556. }
  557. }
  558. if(ev.getKey() == BC_DOWN)
  559. {
  560. if(ev.isShiftDown())
  561. {
  562. // TODO
  563. markAsDirty();
  564. return true;
  565. }
  566. else
  567. {
  568. clearSelection();
  569. mInputCaret->moveCaretDown();
  570. scrollTextToCaret();
  571. markAsDirty();
  572. return true;
  573. }
  574. }
  575. if(ev.getKey() == BC_RETURN)
  576. {
  577. if(mIsMultiline)
  578. {
  579. if(mSelectionShown)
  580. {
  581. mText.erase(mText.begin() + mSelectionStart, mText.begin() + mSelectionEnd);
  582. mInputCaret->updateText(getTextDesc());
  583. mInputCaret->moveCaretToChar(mSelectionStart, CARET_BEFORE);
  584. scrollTextToCaret();
  585. clearSelection();
  586. }
  587. mText.insert(mText.begin() + mInputCaret->getCharIdxAtCaretPos(), '\n');
  588. mInputCaret->updateText(getTextDesc());
  589. mInputCaret->moveCaretRight();
  590. scrollTextToCaret();
  591. markAsDirty();
  592. return true;
  593. }
  594. }
  595. if(ev.getKey() == BC_A && ev.isCtrlDown())
  596. {
  597. showSelection(0);
  598. mSelectionStart = 0;
  599. mSelectionEnd = (UINT32)mText.size();
  600. markAsDirty();
  601. return true;
  602. }
  603. }
  604. else if(ev.getType() == GUIKeyEventType::TextInput)
  605. {
  606. if(mSelectionShown)
  607. {
  608. mText.erase(mText.begin() + mSelectionStart, mText.begin() + mSelectionEnd);
  609. mInputCaret->updateText(getTextDesc());
  610. mInputCaret->moveCaretToChar(mSelectionStart, CARET_BEFORE);
  611. clearSelection();
  612. }
  613. mText.insert(mText.begin() + mInputCaret->getCharIdxAtCaretPos(), ev.getInputChar());
  614. mInputCaret->updateText(getTextDesc());
  615. mInputCaret->moveCaretRight();
  616. scrollTextToCaret();
  617. markAsDirty();
  618. return true;
  619. }
  620. return false;
  621. }
  622. bool GUIInputBox::commandEvent(const GUICommandEvent& ev)
  623. {
  624. if(ev.getType() == GUICommandEventType::Redraw)
  625. {
  626. markAsDirty();
  627. return true;
  628. }
  629. return false;
  630. }
  631. void GUIInputBox::showCaret()
  632. {
  633. mCaretShown = true;
  634. markAsDirty();
  635. }
  636. void GUIInputBox::hideCaret()
  637. {
  638. mCaretShown = false;
  639. markAsDirty();
  640. }
  641. void GUIInputBox::scrollTextToCaret()
  642. {
  643. TEXT_SPRITE_DESC textDesc = getTextDesc();
  644. Int2 caretPos = mInputCaret->getCaretPosition(textDesc.offset);
  645. UINT32 caretHeight = mInputCaret->getCaretHeight();
  646. UINT32 caretWidth = 1;
  647. INT32 caretRight = caretPos.x + (INT32)caretWidth;
  648. INT32 caretBottom = caretPos.y + (INT32)caretHeight;
  649. INT32 left = textDesc.offset.x - mTextOffset.x;
  650. INT32 right = left + (INT32)textDesc.width;
  651. INT32 top = textDesc.offset.y - mTextOffset.y;
  652. INT32 bottom = top + (INT32)textDesc.height;
  653. Int2 offset;
  654. if(caretPos.x < left)
  655. {
  656. offset.x = left - caretPos.x;
  657. }
  658. else if(caretRight > right)
  659. {
  660. offset.x = -(caretRight - right);
  661. }
  662. if(caretPos.y < top)
  663. {
  664. offset.y = top - caretPos.y;
  665. }
  666. else if(caretBottom > bottom)
  667. {
  668. offset.y = -(caretBottom - bottom);
  669. }
  670. mTextOffset += offset;
  671. mInputCaret->updateText(getTextDesc());
  672. markAsDirty();
  673. }
  674. void GUIInputBox::showSelection(UINT32 startChar)
  675. {
  676. mSelectionStart = startChar;
  677. mSelectionEnd = startChar;
  678. mSelectionAnchor = startChar;
  679. mSelectionShown = true;
  680. markAsDirty();
  681. }
  682. void GUIInputBox::clearSelection()
  683. {
  684. for(auto& sprite : mSelectionSprites)
  685. cm_delete(sprite);
  686. mSelectionSprites.clear();
  687. mSelectionShown = false;
  688. markAsDirty();
  689. }
  690. UINT32 GUIInputBox::getCaretSelectionCharIdx(SelectionDir dir) const
  691. {
  692. UINT32 charIdx = mInputCaret->getCharIdxAtCaretPos();
  693. if(dir == SelectionDir::Right)
  694. charIdx = (UINT32)std::max(0, (INT32)(charIdx - 1));
  695. return charIdx;
  696. }
  697. bool GUIInputBox::isNewlineChar(CM::UINT32 charIdx) const
  698. {
  699. if(mText[charIdx] == '\n')
  700. return true;
  701. return false;
  702. }
  703. Vector<Rect>::type GUIInputBox::getSelectionRects() const
  704. {
  705. Vector<Rect>::type selectionRects;
  706. if(mSelectionStart == mSelectionEnd)
  707. return selectionRects;
  708. UINT32 startLine = mTextSprite->getLineForChar(mSelectionStart);
  709. UINT32 endLine = startLine;
  710. if(mSelectionEnd > 0)
  711. {
  712. endLine = mTextSprite->getLineForChar(mSelectionEnd - 1);
  713. // Newline chars should count on the second line, but that not how the sprite reports them, so fix that
  714. if(endLine < (mTextSprite->getNumLines() - 1))
  715. {
  716. if((mSelectionEnd - 1) == (mTextSprite->getLineDesc(endLine).endChar - 1))
  717. endLine++;
  718. }
  719. }
  720. {
  721. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(startLine);
  722. UINT32 startCharIdx = mSelectionStart;
  723. UINT32 endCharIdx = mSelectionEnd - 1;
  724. if(startLine != endLine)
  725. {
  726. endCharIdx = lineDesc.endChar - 1;
  727. if(startLine != (mTextSprite->getNumLines() - 1) && endCharIdx > 0) // Ignore newline char
  728. endCharIdx -= 1;
  729. }
  730. if(!isNewlineChar(startCharIdx) && !isNewlineChar(endCharIdx))
  731. {
  732. Rect startChar = mTextSprite->getCharRect(startCharIdx);
  733. Rect endChar = mTextSprite->getCharRect(endCharIdx);
  734. Rect selectionRect;
  735. selectionRect.x = startChar.x;
  736. selectionRect.y = lineDesc.lineYStart;
  737. selectionRect.height = lineDesc.lineHeight;
  738. selectionRect.width = (endChar.x + endChar.width) - startChar.x;
  739. selectionRects.push_back(selectionRect);
  740. }
  741. }
  742. for(UINT32 i = startLine + 1; i < endLine; i++)
  743. {
  744. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(i);
  745. if(lineDesc.startChar == lineDesc.endChar || isNewlineChar(lineDesc.startChar))
  746. continue;
  747. UINT32 endCharIdx = lineDesc.endChar - 1;
  748. if(endCharIdx > 0) // Ignore newline char
  749. endCharIdx -= 1;
  750. Rect startChar = mTextSprite->getCharRect(lineDesc.startChar);
  751. Rect endChar = mTextSprite->getCharRect(endCharIdx);
  752. Rect selectionRect;
  753. selectionRect.x = startChar.x;
  754. selectionRect.y = lineDesc.lineYStart;
  755. selectionRect.height = lineDesc.lineHeight;
  756. selectionRect.width = (endChar.x + endChar.width) - startChar.x;
  757. selectionRects.push_back(selectionRect);
  758. }
  759. if(startLine != endLine)
  760. {
  761. const SpriteLineDesc& lineDesc = mTextSprite->getLineDesc(endLine);
  762. if(lineDesc.startChar != lineDesc.endChar && !isNewlineChar(lineDesc.startChar))
  763. {
  764. UINT32 endCharIdx = mSelectionEnd - 1;
  765. if(!isNewlineChar(endCharIdx))
  766. {
  767. Rect startChar = mTextSprite->getCharRect(lineDesc.startChar);
  768. Rect endChar = mTextSprite->getCharRect(endCharIdx);
  769. Rect selectionRect;
  770. selectionRect.x = startChar.x;
  771. selectionRect.y = lineDesc.lineYStart;
  772. selectionRect.height = lineDesc.lineHeight;
  773. selectionRect.width = (endChar.x + endChar.width) - startChar.x;
  774. selectionRects.push_back(selectionRect);
  775. }
  776. }
  777. }
  778. return selectionRects;
  779. }
  780. CM::Rect GUIInputBox::getTextBounds() const
  781. {
  782. Rect textBounds = mBounds;
  783. textBounds.x += mStyle->margins.left + mStyle->contentOffset.left;
  784. textBounds.y += mStyle->margins.top + mStyle->contentOffset.top;
  785. textBounds.width = (UINT32)std::max(0, (INT32)textBounds.width -
  786. (INT32)(mStyle->margins.left + mStyle->margins.right + mStyle->contentOffset.left + mStyle->contentOffset.right));
  787. textBounds.height = (UINT32)std::max(0, (INT32)textBounds.height -
  788. (INT32)(mStyle->margins.top + mStyle->margins.bottom + mStyle->contentOffset.top + mStyle->contentOffset.bottom));
  789. return textBounds;
  790. }
  791. TEXT_SPRITE_DESC GUIInputBox::getTextDesc() const
  792. {
  793. TEXT_SPRITE_DESC textDesc;
  794. textDesc.text = mText;
  795. textDesc.font = mStyle->font;
  796. textDesc.fontSize = mStyle->fontSize;
  797. Rect textBounds = getTextBounds();
  798. textDesc.offset = Int2(textBounds.x, textBounds.y) + mTextOffset;
  799. textDesc.width = textBounds.width;
  800. textDesc.height = textBounds.height;
  801. textDesc.clipRect = Rect(-mTextOffset.x, -mTextOffset.y, textDesc.width, textDesc.height);
  802. textDesc.horzAlign = mStyle->textHorzAlign;
  803. textDesc.vertAlign = mStyle->textVertAlign;
  804. return textDesc;
  805. }
  806. void GUIInputBox::_setFocus(bool focus)
  807. {
  808. if(focus)
  809. {
  810. mImageDesc.texture = mStyle->focused.texture;
  811. markAsDirty();
  812. }
  813. else
  814. {
  815. mImageDesc.texture = mStyle->normal.texture;
  816. hideCaret();
  817. clearSelection();
  818. markAsDirty();
  819. }
  820. }
  821. }