PolyUITextInput.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyUITextInput.h"
  20. #include "PolyConfig.h"
  21. #include "PolyInputEvent.h"
  22. #include "PolyLabel.h"
  23. #include "PolyCoreServices.h"
  24. #include "PolyEventHandler.h"
  25. using namespace Polycode;
  26. UITextInput::UITextInput(bool multiLine, Number width, Number height) : UIElement() {
  27. this->multiLine = multiLine;
  28. processInputEvents = true;
  29. isNumberOnly = false;
  30. decoratorOffset = 0;
  31. useStrongHinting = false;
  32. draggingSelection = false;
  33. hasSelection = false;
  34. doSelectToCaret = false;
  35. lineNumbersEnabled = false;
  36. caretPosition = 0;
  37. caretImagePosition = 0;
  38. settingText = false;
  39. currentLine = NULL;
  40. needFullRedraw = false;
  41. lineOffset = -1;
  42. numLines = 0;
  43. this->positionMode = ScreenEntity::POSITION_TOPLEFT;
  44. Config *conf = CoreServices::getInstance()->getConfig();
  45. if(multiLine)
  46. fontName = conf->getStringValue("Polycode", "uiTextInputFontNameMultiLine");
  47. else
  48. fontName = conf->getStringValue("Polycode", "uiTextInputFontName");
  49. if(multiLine)
  50. fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSizeMultiline");
  51. else
  52. fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSize");
  53. Number rectHeight = height;
  54. if(!multiLine) {
  55. rectHeight = fontSize+12;
  56. }
  57. linesContainer = new ScreenEntity();
  58. linesContainer->processInputEvents = true;
  59. lineSpacing = conf->getNumericValue("Polycode", "textEditLineSpacing");
  60. Number st = conf->getNumericValue("Polycode", "textBgSkinT");
  61. Number sr = conf->getNumericValue("Polycode", "textBgSkinR");
  62. Number sb = conf->getNumericValue("Polycode", "textBgSkinB");
  63. Number sl = conf->getNumericValue("Polycode", "textBgSkinL");
  64. padding = conf->getNumericValue("Polycode", "textBgSkinPadding");
  65. inputRect = new UIBox(conf->getStringValue("Polycode", "textBgSkin"),
  66. st,sr,sb,sl,
  67. width+(padding*2), height+(padding*2));
  68. addChild(inputRect);
  69. if(multiLine) {
  70. lineNumberBg = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  71. lineNumberBg->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  72. lineNumberBg->setColor(0.0, 0.0, 0.0, 0.3);
  73. addChild(lineNumberBg);
  74. lineNumberBg->visible = false;
  75. lineNumberAnchor = new ScreenEntity();
  76. linesContainer->addChild(lineNumberAnchor);
  77. } else {
  78. lineNumberBg = NULL;
  79. lineNumberAnchor = NULL;
  80. }
  81. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  82. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  83. inputRect->addEventListener(this, InputEvent::EVENT_DOUBLECLICK);
  84. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  85. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
  86. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  87. inputRect->processInputEvents = true;
  88. inputRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  89. selectorRectTop = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  90. selectorRectTop->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  91. selectorRectTop->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  92. selectorRectTop->visible = false;
  93. linesContainer->addChild(selectorRectTop);
  94. selectorRectMiddle = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  95. selectorRectMiddle->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  96. selectorRectMiddle->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  97. selectorRectMiddle->visible = false;
  98. linesContainer->addChild(selectorRectMiddle);
  99. selectorRectBottom = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  100. selectorRectBottom->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  101. selectorRectBottom->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  102. selectorRectBottom->visible = false;
  103. linesContainer->addChild(selectorRectBottom);
  104. blinkerRect = new ScreenShape(ScreenShape::SHAPE_RECT, 1, fontSize+2,0,0);
  105. blinkerRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  106. blinkerRect->setColor(0,0,0,1);
  107. linesContainer->addChild(blinkerRect);
  108. blinkerRect->visible = false;
  109. blinkerRect->setPosition(0,3);
  110. blinkTimer = new Timer(true, 500);
  111. blinkTimer->addEventListener(this, Timer::EVENT_TRIGGER);
  112. focusable = true;
  113. this->width = width;
  114. this->height = rectHeight;
  115. setHitbox(width, rectHeight);
  116. updateCaretPosition();
  117. scrollContainer = NULL;
  118. if(multiLine) {
  119. scrollContainer = new UIScrollContainer(linesContainer, false, true, 200, 200);
  120. addChild(scrollContainer);
  121. } else {
  122. addChild(linesContainer);
  123. enableScissor = true;
  124. }
  125. undoStateIndex = 0;
  126. maxRedoIndex = 0;
  127. syntaxHighliter = NULL;
  128. textColor = Color(0.0,0.0,0.0,1.0);
  129. insertLine(true);
  130. }
  131. void UITextInput::setNumberOnly(bool val) {
  132. isNumberOnly = val;
  133. }
  134. void UITextInput::clearSelection() {
  135. hasSelection = false;
  136. selectorRectTop->visible = false;
  137. selectorRectMiddle->visible = false;
  138. selectorRectBottom->visible = false;
  139. }
  140. void UITextInput::setSelection(int lineStart, int lineEnd, int colStart, int colEnd) {
  141. if(lineStart == lineEnd && colStart == colEnd) {
  142. clearSelection();
  143. return;
  144. }
  145. if(lineStart == lineOffset) {
  146. selectionLine = lineEnd;
  147. } else {
  148. selectionLine = lineStart;
  149. }
  150. if(colStart == caretPosition) {
  151. selectionCaretPosition = colEnd;
  152. } else {
  153. selectionCaretPosition = colStart;
  154. }
  155. // printf("SET lineStart:%d lineEnd:%d colStart:%d colEnd:%d\n", lineStart, lineEnd, colStart, colEnd);
  156. if(lineStart > lineEnd) {
  157. int tmp = lineStart;
  158. lineStart = lineEnd;
  159. lineEnd = tmp;
  160. tmp = colStart;
  161. colStart = colEnd;
  162. colEnd = tmp;
  163. }
  164. if(colStart > colEnd && lineStart == lineEnd) {
  165. int tmp = colStart;
  166. colStart = colEnd;
  167. colEnd = tmp;
  168. }
  169. clearSelection();
  170. if(lineStart > lines.size()-1)
  171. return;
  172. ScreenLabel *topLine = lines[lineStart];
  173. if(colStart+1 > topLine->getText().length()) {
  174. colStart = topLine->getText().length();
  175. }
  176. Number fColEnd = colEnd;
  177. if(colEnd > topLine->getText().length() || lineStart != lineEnd)
  178. fColEnd = topLine->getText().length();
  179. Number topSize, topHeight, topX;
  180. selectorRectTop->visible = true;
  181. topSize = topLine->getLabel()->getTextWidthForString(topLine->getText().substr(colStart,fColEnd-colStart)) ;
  182. topHeight = lineHeight+lineSpacing;
  183. if(colStart >= 0) {
  184. topX = topLine->getLabel()->getTextWidthForString(topLine->getText().substr(0,colStart)) + 2;
  185. } else {
  186. topX = 0;
  187. }
  188. selectorRectTop->setScale(topSize, topHeight);
  189. selectorRectTop->setPosition(decoratorOffset + topX + padding + (topSize/2.0), padding + (lineStart * (lineHeight+lineSpacing)) + (topHeight/2.0));
  190. if(lineEnd > lineStart && lineEnd < lines.size()) {
  191. ScreenLabel *bottomLine = lines[lineEnd];
  192. selectorRectBottom->visible = true;
  193. Number bottomSize = bottomLine->getLabel()->getTextWidthForString(bottomLine->getText().substr(0,colEnd)) ;
  194. if(bottomSize < 0)
  195. bottomSize = this->width-padding;
  196. Number bottomHeight = lineHeight+lineSpacing;
  197. selectorRectBottom->setScale(bottomSize, bottomHeight);
  198. selectorRectBottom->setPosition(decoratorOffset + padding + (bottomSize/2.0), padding + (lineEnd * (lineHeight+lineSpacing)) + (bottomHeight/2.0));
  199. if(lineEnd != lineStart+1) {
  200. // need filler
  201. selectorRectMiddle->visible = true;
  202. Number midSize = this->width-padding;
  203. Number midHeight = 0;
  204. for(int i=lineStart+1; i < lineEnd;i++) {
  205. midHeight += lineHeight+lineSpacing;
  206. }
  207. selectorRectMiddle->setScale(midSize, midHeight);
  208. selectorRectMiddle->setPosition(decoratorOffset + padding + (midSize/2.0), padding + ((lineStart+1) * (lineHeight+lineSpacing)) + (midHeight/2.0));
  209. }
  210. }
  211. hasSelection = true;
  212. selectionTop = lineStart;
  213. selectionBottom = lineEnd;
  214. selectionL = colStart;
  215. selectionR = colEnd;
  216. }
  217. void UITextInput::deleteSelection() {
  218. if(selectionTop == selectionBottom) {
  219. String ctext = lines[selectionTop]->getText();
  220. String newText = ctext.substr(0, selectionL);
  221. int rside = selectionR;
  222. if(rside > ctext.length()-1)
  223. rside = ctext.length() - 1;
  224. newText += ctext.substr(rside,ctext.length() - selectionR);
  225. lines[selectionTop]->setText(newText);
  226. } else {
  227. String ctext = lines[selectionTop]->getText();
  228. String newText = ctext.substr(0, selectionL);
  229. lines[selectionTop]->setText(newText);
  230. ScreenLabel *bottomLine = lines[selectionBottom];
  231. // if whole lines to remove, do it
  232. vector<ScreenLabel*> linesToRemove;
  233. if(selectionBottom > selectionTop + 1) {
  234. for(int i=selectionTop+1; i < selectionBottom; i++) {
  235. linesToRemove.push_back(lines[i]);
  236. }
  237. for(int i=0; i < linesToRemove.size(); i++) {
  238. removeLine(linesToRemove[i]);
  239. }
  240. }
  241. ctext = bottomLine->getText();
  242. int rside = selectionR;
  243. if(rside > ctext.length()-1)
  244. rside = ctext.length() - 1;
  245. newText = ctext.substr(rside,ctext.length() - selectionR);
  246. lineOffset = selectionTop;
  247. selectLineFromOffset();
  248. caretPosition = currentLine->getText().length();
  249. updateCaretPosition();
  250. currentLine->setText(currentLine->getText() + newText);
  251. removeLine(bottomLine);
  252. }
  253. clearSelection();
  254. caretPosition = selectionL;
  255. updateCaretPosition();
  256. changedText();
  257. }
  258. void UITextInput::changedText() {
  259. if(settingText)
  260. return;
  261. if(syntaxHighliter && multiLine) {
  262. unsigned int startLine = (-linesContainer->getPosition().y) / (lineHeight+lineSpacing);
  263. unsigned int endLine = startLine + ((int)((height / (lineHeight+lineSpacing)))) + 1;
  264. if(endLine > lines.size())
  265. endLine = lines.size();
  266. if(needFullRedraw) {
  267. startLine = 0;
  268. endLine = lines.size();
  269. needFullRedraw = false;
  270. }
  271. String totalText = L"";
  272. for(int i=startLine; i < endLine; i++) {
  273. totalText += lines[i]->getText();
  274. if(i < lines.size()-1)
  275. totalText += L"\n";
  276. }
  277. std::vector<SyntaxHighlightToken> tokens = syntaxHighliter->parseText(totalText);
  278. for(int i=startLine; i < endLine; i++) {
  279. lines[i]->getLabel()->clearColors();
  280. }
  281. int lineIndex = startLine;
  282. int rangeStart = 0;
  283. int rangeEnd = 0;
  284. for(int i=0; i < tokens.size(); i++) {
  285. if(tokens[i].text == "\n") {
  286. lineIndex++;
  287. rangeStart = 0;
  288. rangeEnd = 0;
  289. } else {
  290. if(lineIndex < lines.size()) {
  291. int textLength = tokens[i].text.length();
  292. if(tokens[i].text.length() > 1) {
  293. rangeEnd = rangeStart + textLength-1;
  294. lines[lineIndex]->getLabel()->setColorForRange(tokens[i].color, rangeStart, rangeEnd);
  295. rangeStart = rangeStart + textLength;
  296. } else {
  297. rangeEnd = rangeStart;
  298. lines[lineIndex]->getLabel()->setColorForRange(tokens[i].color, rangeStart, rangeEnd);
  299. rangeStart++;
  300. }
  301. }
  302. }
  303. }
  304. for(int i=startLine; i < endLine; i++) {
  305. lines[i]->setText(lines[i]->getText());
  306. lines[i]->setColor(1.0, 1.0, 1.0, 1.0);
  307. }
  308. }
  309. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  310. }
  311. void UITextInput::setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighliter) {
  312. this->syntaxHighliter = syntaxHighliter;
  313. }
  314. void UITextInput::Resize(Number width, Number height) {
  315. inputRect->resizeBox(width, height);
  316. this->width = width;
  317. this->height = height;
  318. matrixDirty = true;
  319. setHitbox(width,height);
  320. if(multiLine) {
  321. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  322. }
  323. if(multiLine && lineNumbersEnabled) {
  324. lineNumberBg->setShapeSize(decoratorOffset, height);
  325. }
  326. if(scrollContainer) {
  327. scrollContainer->Resize(width, height);
  328. }
  329. }
  330. int UITextInput::insertLine(bool after) {
  331. numLines++;
  332. int aaMode = Label::ANTIALIAS_FULL;
  333. if(useStrongHinting) {
  334. aaMode = Label::ANTIALIAS_STRONG;
  335. }
  336. if(multiLine) {
  337. ScreenLabel *newNumberLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
  338. newNumberLine->color = lineNumberColor;
  339. lineNumberAnchor->addChild(newNumberLine);
  340. numberLines.push_back(newNumberLine);
  341. if(!lineNumbersEnabled) {
  342. newNumberLine->visible = false;
  343. }
  344. }
  345. ScreenLabel *newLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
  346. newLine->color = textColor;
  347. lineHeight = newLine->getHeight();
  348. linesContainer->addChild(newLine);
  349. if(after) {
  350. if(currentLine) {
  351. String ctext = currentLine->getText();
  352. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  353. ctext = ctext.substr(0,caretPosition);
  354. currentLine->setText(ctext);
  355. newLine->setText(text2);
  356. caretPosition=0;
  357. }
  358. currentLine = newLine;
  359. vector<ScreenLabel*>::iterator it;
  360. it = lines.begin();
  361. for(int i=0; i < lineOffset+1; i++) {
  362. it++;
  363. }
  364. lineOffset = lineOffset + 1;
  365. lines.insert(it,newLine);
  366. renumberLines();
  367. restructLines();
  368. } else {
  369. // do we even need that? I don't think so.
  370. }
  371. changedText();
  372. return 1;
  373. }
  374. void UITextInput::enableLineNumbers(bool val) {
  375. lineNumbersEnabled = val;
  376. lineNumberBg->visible = lineNumbersEnabled;
  377. restructLines();
  378. }
  379. void UITextInput::renumberLines() {
  380. if(!multiLine)
  381. return;
  382. decoratorOffset = 0;
  383. if(multiLine) {
  384. for(int i=0; i < numberLines.size(); i++) {
  385. if(lineNumbersEnabled) {
  386. numberLines[i]->setText(String::IntToString(i+1));
  387. int textWidth = ceil(numberLines[i]->getLabel()->getTextWidth());
  388. numberLines[i]->setPosition(-textWidth,padding + (i*(lineHeight+lineSpacing)),0.0f);
  389. if(textWidth > decoratorOffset - 10) {
  390. decoratorOffset = textWidth + 10;
  391. }
  392. numberLines[i]->visible = true;
  393. } else {
  394. numberLines[i]->visible = false;
  395. }
  396. }
  397. }
  398. lineNumberAnchor->setPositionX(padding+decoratorOffset - 10);
  399. }
  400. void UITextInput::restructLines() {
  401. for(int i=0; i < lines.size(); i++) {
  402. lines[i]->setPosition(decoratorOffset + padding,padding + (i*(lineHeight+lineSpacing)),0.0f);
  403. }
  404. if(multiLine && lineNumbersEnabled) {
  405. lineNumberBg->setShapeSize(decoratorOffset, height);
  406. }
  407. if(scrollContainer) {
  408. scrollContainer->setContentSize(width, (((lines.size()) * ((lineHeight+lineSpacing)))) + padding);
  409. }
  410. if(multiLine) {
  411. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  412. }
  413. }
  414. void UITextInput::setText(String text) {
  415. if(!multiLine) {
  416. currentLine->setText(text);
  417. caretPosition = text.length();
  418. clearSelection();
  419. updateCaretPosition();
  420. } else {
  421. needFullRedraw = true;
  422. selectAll();
  423. insertText(text);
  424. clearSelection();
  425. }
  426. // this->text = text;
  427. // currentLine->setText(text);
  428. }
  429. void UITextInput::onLoseFocus() {
  430. blinkerRect->visible = false;
  431. clearSelection();
  432. }
  433. String UITextInput::getText() {
  434. if(!multiLine) {
  435. return currentLine->getText();
  436. } else {
  437. String totalText = L"";
  438. for(int i=0; i < lines.size(); i++) {
  439. totalText += lines[i]->getText();
  440. if(i < lines.size()-1)
  441. totalText += L"\n";
  442. }
  443. return totalText;
  444. }
  445. }
  446. void UITextInput::updateCaretPosition() {
  447. caretImagePosition = padding;
  448. if(caretPosition == 0) {
  449. caretImagePosition = padding;
  450. } else if(caretPosition > currentLine->getText().length()) {
  451. caretPosition = currentLine->getText().length();
  452. String caretSubString = currentLine->getText().substr(0,caretPosition);
  453. caretImagePosition = currentLine->getLabel()->getTextWidthForString(caretSubString);
  454. caretImagePosition = caretImagePosition + padding;
  455. } else {
  456. String caretSubString = currentLine->getText().substr(0,caretPosition);
  457. caretImagePosition = currentLine->getLabel()->getTextWidthForString(caretSubString);
  458. caretImagePosition = caretImagePosition + padding;
  459. }
  460. blinkerRect->visible = true;
  461. blinkTimer->Reset();
  462. if(doSelectToCaret) {
  463. doSelectToCaret = false;
  464. }
  465. if(multiLine && currentLine) {
  466. if(linesContainer->getPosition().y + currentLine->getPosition2D().y < 0.0) {
  467. scrollContainer->scrollVertical(-(lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  468. } else if(linesContainer->getPosition().y + currentLine->getPosition2D().y > scrollContainer->getHeight()) {
  469. scrollContainer->scrollVertical((lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  470. }
  471. }
  472. }
  473. void UITextInput::selectLineFromOffset() {
  474. for(int i=0; i < lines.size(); i++) {
  475. if(i == lineOffset) {
  476. currentLine = lines[i];
  477. return;
  478. }
  479. }
  480. }
  481. void UITextInput::dragSelectionTo(Number x, Number y) {
  482. x -= padding * 2.0;
  483. y -= padding;
  484. int lineOffset = y / (lineHeight+lineSpacing);
  485. if(lineOffset > lines.size()-1)
  486. lineOffset = lines.size()-1;
  487. ScreenLabel *selectToLine = lines[lineOffset];
  488. int len = selectToLine->getText().length();
  489. Number slen;
  490. int caretPosition = selectToLine->getLabel()->getTextWidthForString(selectToLine->getText().substr(0,len));
  491. for(int i=0; i < len; i++) {
  492. slen = selectToLine->getLabel()->getTextWidthForString(selectToLine->getText().substr(0,i));
  493. if(slen > x) {
  494. caretPosition = i;
  495. break;
  496. }
  497. }
  498. if(x > slen)
  499. caretPosition = len;
  500. // if(multiLine)
  501. // caretPosition++;
  502. if(caretPosition < 0)
  503. caretPosition = 0;
  504. setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
  505. }
  506. int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
  507. for(int i=caretPosition; i > 0; i--) {
  508. String bit = lines[caretLine]->getText().substr(i,1);
  509. char chr = ((char*)bit.c_str())[0];
  510. if(!isNumberOrCharacter(chr) && i < caretPosition-1) {
  511. return i+1;
  512. }
  513. }
  514. return 0;
  515. }
  516. int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
  517. int len = lines[caretLine]->getText().length();
  518. for(int i=caretPosition; i < len; i++) {
  519. String bit = lines[caretLine]->getText().substr(i,1);
  520. char chr = ((char*)bit.c_str())[0];
  521. if(!isNumberOrCharacter(chr) && i > caretPosition) {
  522. return i;
  523. }
  524. }
  525. return lines[caretLine]->getText().length();
  526. }
  527. void UITextInput::selectWordAtCaret() {
  528. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  529. clearSelection();
  530. updateCaretPosition();
  531. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  532. updateCaretPosition();
  533. }
  534. void UITextInput::replaceAll(String what, String withWhat) {
  535. for(int i=0; i < lines.size(); i++) {
  536. lines[i]->setText(lines[i]->getText().replace(what, withWhat));
  537. }
  538. needFullRedraw = true;
  539. changedText();
  540. }
  541. void UITextInput::findString(String stringToFind, bool replace, String replaceString) {
  542. clearSelection();
  543. findMatches.clear();
  544. for(int i=0; i < lines.size(); i++) {
  545. String lineText = lines[i]->getText();
  546. int offset = 0;
  547. int retVal = -1;
  548. do {
  549. retVal = lineText.find(stringToFind, offset);
  550. if(retVal != -1) {
  551. FindMatch match;
  552. match.lineNumber = i;
  553. match.caretStart = retVal;
  554. match.caretEnd = retVal + stringToFind.length();
  555. findMatches.push_back(match);
  556. offset = retVal + stringToFind.length();
  557. }
  558. } while(retVal != -1);
  559. }
  560. if(findMatches.size() > 0) {
  561. if(replace) {
  562. FindMatch match = findMatches[findIndex];
  563. String oldText = lines[match.lineNumber]->getText();
  564. String newText = oldText.substr(0,match.caretStart) + replaceString + oldText.substr(match.caretEnd);
  565. lines[match.lineNumber]->setText(newText);
  566. findMatches[findIndex].caretEnd = findMatches[findIndex].caretStart + replaceString.length();
  567. changedText();
  568. }
  569. findIndex = 0;
  570. findCurrent();
  571. }
  572. }
  573. void UITextInput::findNext() {
  574. if(findMatches.size() == 0)
  575. return;
  576. findIndex++;
  577. if(findIndex == findMatches.size()) {
  578. findIndex = 0;
  579. }
  580. findCurrent();
  581. }
  582. void UITextInput::findPrevious() {
  583. if(findMatches.size() == 0)
  584. return;
  585. findIndex--;
  586. if(findIndex < 0) {
  587. findIndex = findMatches.size()-1;
  588. }
  589. findCurrent();
  590. }
  591. void UITextInput::findCurrent() {
  592. if(findMatches.size() == 0)
  593. return;
  594. FindMatch match = findMatches[findIndex];
  595. currentLine = lines[match.lineNumber];
  596. caretPosition = match.caretStart;
  597. lineOffset = match.lineNumber;
  598. updateCaretPosition();
  599. showLine(findMatches[findIndex].lineNumber, false);
  600. setSelection(match.lineNumber, match.lineNumber, match.caretStart, match.caretEnd);
  601. }
  602. void UITextInput::setCaretToMouse(Number x, Number y) {
  603. clearSelection();
  604. x -= (padding) + decoratorOffset;
  605. y -= padding;
  606. //if(lines.size() > 1) {
  607. lineOffset = y / (lineHeight+lineSpacing);
  608. if(lineOffset > lines.size()-1)
  609. lineOffset = lines.size()-1;
  610. selectLineFromOffset();
  611. //}
  612. int len = currentLine->getText().length();
  613. Number slen;
  614. int newCaretPosition = -1;
  615. for(int i=1; i < len; i++) {
  616. slen = currentLine->getLabel()->getTextWidthForString(currentLine->getText().substr(0,i));
  617. Number slen_prev = currentLine->getLabel()->getTextWidthForString(currentLine->getText().substr(0,i-1));
  618. if(x >= slen_prev && x <= slen) {
  619. if(x < slen_prev + ((slen - slen_prev) /2.0)) {
  620. newCaretPosition = i-1;
  621. break;
  622. } else {
  623. newCaretPosition = i;
  624. break;
  625. }
  626. }
  627. }
  628. if(newCaretPosition == -1)
  629. newCaretPosition = 0;
  630. if(x > slen)
  631. newCaretPosition = len;
  632. caretPosition = newCaretPosition;
  633. updateCaretPosition();
  634. }
  635. void UITextInput::removeLine(ScreenLabel *line) {
  636. for(int i=0;i<lines.size();i++) {
  637. if(lines[i] == line) {
  638. lines.erase(lines.begin()+i);
  639. }
  640. }
  641. linesContainer->removeChild(line);
  642. linesToDelete.push_back(line);
  643. renumberLines();
  644. restructLines();
  645. changedText();
  646. }
  647. void UITextInput::selectAll() {
  648. setSelection(0, lines.size()-1, 0, lines[lines.size()-1]->getText().length());
  649. }
  650. void UITextInput::insertText(String text) {
  651. vector<String> strings = text.split("\n");
  652. settingText = true;
  653. if(hasSelection)
  654. deleteSelection();
  655. if(strings.size() > 1) {
  656. String ctext = currentLine->getText();
  657. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  658. ctext = ctext.substr(0,caretPosition);
  659. ctext += strings[0];
  660. currentLine->setText(ctext);
  661. caretPosition = ctext.length();
  662. for(int i=1; i < strings.size()-1; i++) {
  663. insertLine(true);
  664. ctext = strings[i];
  665. currentLine->setText(ctext);
  666. caretPosition = ctext.length();
  667. }
  668. insertLine(true);
  669. ctext = strings[strings.size()-1] + text2;
  670. caretPosition = ctext.length();
  671. currentLine->setText(ctext);
  672. } else {
  673. String ctext = currentLine->getText();
  674. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  675. ctext = ctext.substr(0,caretPosition);
  676. ctext += text + text2;
  677. caretPosition += text.length();
  678. currentLine->setText(ctext);
  679. }
  680. settingText = false;
  681. changedText();
  682. updateCaretPosition();
  683. restructLines();
  684. }
  685. String UITextInput::getLineText(unsigned int index) {
  686. if(index < lines.size()) {
  687. return lines[index]->getText();
  688. } else {
  689. return "";
  690. }
  691. }
  692. String UITextInput::getSelectionText() {
  693. if(!hasSelection)
  694. return L"";
  695. String totalText = L"";
  696. if(selectionTop == selectionBottom) {
  697. totalText = lines[selectionTop]->getText().substr(selectionL, selectionR-selectionL);
  698. return totalText;
  699. } else {
  700. totalText += lines[selectionTop]->getText().substr(selectionL, lines[selectionTop]->getText().length()-selectionL);
  701. totalText += L"\n";
  702. }
  703. if(selectionBottom > selectionTop+1) {
  704. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  705. totalText += lines[i]->getText();
  706. totalText += L"\n";
  707. }
  708. }
  709. totalText += lines[selectionBottom]->getText().substr(0, selectionL);
  710. return totalText;
  711. }
  712. void UITextInput::setSelectionColor(Color color) {
  713. selectorRectTop->color = color;
  714. selectorRectMiddle->color = color;
  715. selectorRectBottom->color = color;
  716. }
  717. void UITextInput::setCursorColor(Color color) {
  718. blinkerRect->color = color;
  719. }
  720. void UITextInput::setBackgroundColor(Color color) {
  721. inputRect->color = color;
  722. }
  723. void UITextInput::setLineNumberColor(Color color) {
  724. lineNumberColor = color;
  725. for(int i=0; i < numberLines.size(); i++) {
  726. numberLines[i]->color = lineNumberColor;
  727. }
  728. }
  729. void UITextInput::setTextColor(Color color) {
  730. textColor = color;
  731. for(int i=0; i < lines.size(); i++) {
  732. lines[i]->color = textColor;
  733. }
  734. }
  735. UIScrollContainer *UITextInput::getScrollContainer() {
  736. return scrollContainer;
  737. }
  738. void UITextInput::saveUndoState() {
  739. UITextInputUndoState newState;
  740. newState.content = getText();
  741. newState.caretPosition = caretPosition;
  742. newState.lineOffset = lineOffset;
  743. newState.hasSelection = hasSelection;
  744. if(hasSelection) {
  745. newState.selectionLine = selectionLine;
  746. newState.selectionCaretPosition = selectionCaretPosition;
  747. }
  748. undoStates[undoStateIndex] = newState;
  749. // if we hit undo state capacity, shift the whole stack
  750. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  751. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  752. undoStates[i] = undoStates[i+1];
  753. }
  754. } else {
  755. undoStateIndex++;
  756. }
  757. maxRedoIndex = undoStateIndex;
  758. }
  759. void UITextInput::setUndoState(UITextInputUndoState state) {
  760. clearSelection();
  761. setText(state.content);
  762. currentLine = lines[state.lineOffset];
  763. caretPosition = state.caretPosition;
  764. lineOffset = state.lineOffset;
  765. updateCaretPosition();
  766. if(state.hasSelection) {
  767. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  768. }
  769. }
  770. void UITextInput::Undo() {
  771. if(undoStateIndex > 0) {
  772. undoStateIndex--;
  773. setUndoState(undoStates[undoStateIndex]);
  774. }
  775. }
  776. void UITextInput::Redo() {
  777. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  778. undoStateIndex++;
  779. setUndoState(undoStates[undoStateIndex]);
  780. }
  781. }
  782. void UITextInput::Cut() {
  783. saveUndoState();
  784. Copy();
  785. if(hasSelection) {
  786. deleteSelection();
  787. }
  788. }
  789. void UITextInput::Copy() {
  790. if(hasSelection) {
  791. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  792. }
  793. }
  794. void UITextInput::Paste() {
  795. saveUndoState();
  796. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  797. }
  798. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  799. if(top) {
  800. scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
  801. } else {
  802. scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
  803. }
  804. }
  805. bool UITextInput::isNumberOrCharacter(wchar_t charCode) {
  806. if(charCode > 47 && charCode < 58)
  807. return true;
  808. if(charCode > 64 && charCode < 91)
  809. return true;
  810. if(charCode > 96 && charCode < 123)
  811. return true;
  812. return false;
  813. }
  814. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  815. if(!hasFocus)
  816. return;
  817. // Logger::log("UCHAR: %d\n", charCode);
  818. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  819. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  820. selectAll();
  821. return;
  822. }
  823. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  824. Copy();
  825. return;
  826. }
  827. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  828. Cut();
  829. return;
  830. }
  831. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  832. Undo();
  833. return;
  834. }
  835. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  836. Redo();
  837. return;
  838. }
  839. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  840. Redo();
  841. return;
  842. }
  843. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  844. Paste();
  845. return;
  846. }
  847. if(key == KEY_LEFT) {
  848. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  849. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  850. if(hasSelection) {
  851. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  852. } else {
  853. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  854. }
  855. } else {
  856. caretPosition = 0;
  857. clearSelection();
  858. updateCaretPosition();
  859. }
  860. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  861. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  862. if(hasSelection) {
  863. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  864. } else {
  865. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  866. }
  867. } else {
  868. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  869. clearSelection();
  870. updateCaretPosition();
  871. }
  872. } else {
  873. if(caretPosition > 0) {
  874. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  875. if(hasSelection) {
  876. if(selectionCaretPosition > 0)
  877. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  878. } else {
  879. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  880. }
  881. } else {
  882. caretPosition--;
  883. clearSelection();
  884. updateCaretPosition();
  885. }
  886. }
  887. }
  888. return;
  889. }
  890. if(key == KEY_RIGHT) {
  891. if(caretPosition < currentLine->getText().length()) {
  892. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  893. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  894. if(hasSelection) {
  895. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  896. } else {
  897. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  898. }
  899. } else {
  900. caretPosition = currentLine->getText().length();
  901. clearSelection();
  902. }
  903. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  904. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  905. if(hasSelection) {
  906. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  907. } else {
  908. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  909. }
  910. } else {
  911. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  912. clearSelection();
  913. }
  914. } else {
  915. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  916. if(hasSelection) {
  917. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  918. } else {
  919. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  920. }
  921. } else {
  922. caretPosition++;
  923. clearSelection();
  924. }
  925. }
  926. updateCaretPosition();
  927. }
  928. return;
  929. }
  930. if(key == KEY_PAGEUP) {
  931. if(multiLine) {
  932. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  933. }
  934. return;
  935. }
  936. if(key == KEY_PAGEDOWN) {
  937. if(multiLine) {
  938. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  939. }
  940. return;
  941. }
  942. if(key == KEY_UP) {
  943. if(multiLine) {
  944. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  945. if(hasSelection) {
  946. if(selectionLine > 0)
  947. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  948. } else {
  949. if(this->lineOffset > 0)
  950. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  951. }
  952. } else {
  953. clearSelection();
  954. if(lineOffset > 0) {
  955. lineOffset--;
  956. selectLineFromOffset();
  957. updateCaretPosition();
  958. }
  959. }
  960. }
  961. blinkerRect->visible = true;
  962. return;
  963. }
  964. if(key == KEY_DOWN) {
  965. if(multiLine) {
  966. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  967. if(hasSelection) {
  968. if(selectionLine < lines.size()-1)
  969. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  970. } else {
  971. if(this->lineOffset < lines.size()-1)
  972. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  973. }
  974. } else {
  975. clearSelection();
  976. if(lineOffset < lines.size()-1) {
  977. lineOffset++;
  978. selectLineFromOffset();
  979. updateCaretPosition();
  980. }
  981. }
  982. }
  983. blinkerRect->visible = true;
  984. return;
  985. }
  986. if(key == KEY_ESCAPE) {
  987. if(!multiLine) {
  988. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  989. }
  990. }
  991. if(key == KEY_RETURN) {
  992. if(multiLine) {
  993. saveUndoState();
  994. if(hasSelection) {
  995. deleteSelection();
  996. }
  997. insertLine(true);
  998. updateCaretPosition();
  999. } else {
  1000. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  1001. }
  1002. return;
  1003. }
  1004. String ctext = currentLine->getText();
  1005. if((charCode > 31 && charCode < 127) || charCode > 127) {
  1006. if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
  1007. if(!isNumberOrCharacter(charCode)) {
  1008. saveUndoState();
  1009. }
  1010. if(hasSelection)
  1011. deleteSelection();
  1012. ctext = currentLine->getText();
  1013. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1014. ctext = ctext.substr(0,caretPosition);
  1015. ctext += charCode + text2;
  1016. caretPosition++;
  1017. }
  1018. }
  1019. if(key == KEY_TAB && multiLine) {
  1020. saveUndoState();
  1021. if(hasSelection)
  1022. deleteSelection();
  1023. ctext = currentLine->getText();
  1024. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1025. ctext = ctext.substr(0,caretPosition);
  1026. ctext += (wchar_t)'\t' + text2;
  1027. caretPosition++;
  1028. }
  1029. if(key == KEY_BACKSPACE) {
  1030. if(hasSelection) {
  1031. saveUndoState();
  1032. deleteSelection();
  1033. return;
  1034. } else {
  1035. ctext = currentLine->getText();
  1036. if(caretPosition > 0) {
  1037. saveUndoState();
  1038. if(ctext.length() > 0) {
  1039. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1040. ctext = ctext.substr(0,caretPosition-1);
  1041. ctext += text2;
  1042. caretPosition--;
  1043. }
  1044. } else {
  1045. if(lineOffset > 0) {
  1046. saveUndoState();
  1047. ScreenLabel *lineToRemove = currentLine;
  1048. lineOffset--;
  1049. selectLineFromOffset();
  1050. caretPosition = currentLine->getText().length();
  1051. updateCaretPosition();
  1052. currentLine->setText(currentLine->getText() + ctext);
  1053. removeLine(lineToRemove);
  1054. return;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. currentLine->setText(ctext);
  1060. changedText();
  1061. updateCaretPosition();
  1062. }
  1063. void UITextInput::Update() {
  1064. if(!multiLine) {
  1065. Vector2 pos = getScreenPosition();
  1066. scissorBox.setRect(pos.x,pos.y, width, height);
  1067. }
  1068. if(hasSelection) {
  1069. blinkerRect->visible = false;
  1070. }
  1071. blinkerRect->setPosition(decoratorOffset + caretImagePosition + 1,currentLine->getPosition2D().y+1);
  1072. if(hasFocus) {
  1073. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  1074. } else {
  1075. blinkerRect->visible = false;
  1076. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  1077. }
  1078. for(int i=0; i < linesToDelete.size(); i++) {
  1079. delete linesToDelete[i];
  1080. }
  1081. linesToDelete.clear();
  1082. }
  1083. UITextInput::~UITextInput() {
  1084. }
  1085. void UITextInput::handleEvent(Event *event) {
  1086. if(event->getDispatcher() == inputRect) {
  1087. switch(event->getEventCode()) {
  1088. case InputEvent::EVENT_MOUSEDOWN:
  1089. if(parentEntity) {
  1090. ((ScreenEntity*)parentEntity)->focusChild(this);
  1091. } else {
  1092. hasFocus = true;
  1093. }
  1094. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1095. draggingSelection = true;
  1096. break;
  1097. case InputEvent::EVENT_MOUSEUP:
  1098. draggingSelection = false;
  1099. break;
  1100. case InputEvent::EVENT_DOUBLECLICK:
  1101. selectWordAtCaret();
  1102. break;
  1103. case InputEvent::EVENT_MOUSEMOVE:
  1104. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1105. if(draggingSelection) {
  1106. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1107. }
  1108. break;
  1109. case InputEvent::EVENT_MOUSEOVER:
  1110. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1111. break;
  1112. case InputEvent::EVENT_MOUSEOUT:
  1113. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  1114. break;
  1115. }
  1116. }
  1117. if(event->getDispatcher() == blinkTimer) {
  1118. if(hasSelection || draggingSelection) {
  1119. blinkerRect->visible = false;
  1120. } else {
  1121. if(hasFocus)
  1122. blinkerRect->visible = !blinkerRect->visible;
  1123. else
  1124. blinkerRect->visible = false;
  1125. }
  1126. }
  1127. }