BsGUIInputBox.cpp 26 KB

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