PolyUITextInput.cpp 34 KB

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