PolyUITextInput.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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. if(!hasSelection)
  631. return L"";
  632. String totalText = L"";
  633. if(selectionTop == selectionBottom) {
  634. totalText = lines[selectionTop]->getText().substr(selectionL, selectionR-selectionL);
  635. return totalText;
  636. } else {
  637. totalText += lines[selectionTop]->getText().substr(selectionL, lines[selectionTop]->getText().length()-selectionL);
  638. totalText += L"\n";
  639. }
  640. if(selectionBottom > selectionTop+1) {
  641. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  642. totalText += lines[i]->getText();
  643. totalText += L"\n";
  644. }
  645. }
  646. totalText += lines[selectionBottom]->getText().substr(0, selectionL);
  647. return totalText;
  648. }
  649. UIScrollContainer *UITextInput::getScrollContainer() {
  650. return scrollContainer;
  651. }
  652. void UITextInput::saveUndoState() {
  653. UITextInputUndoState newState;
  654. newState.content = getText();
  655. newState.caretPosition = caretPosition;
  656. newState.lineOffset = lineOffset;
  657. newState.hasSelection = hasSelection;
  658. if(hasSelection) {
  659. newState.selectionLine = selectionLine;
  660. newState.selectionCaretPosition = selectionCaretPosition;
  661. }
  662. undoStates[undoStateIndex] = newState;
  663. // if we hit undo state capacity, shift the whole stack
  664. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  665. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  666. undoStates[i] = undoStates[i+1];
  667. }
  668. } else {
  669. undoStateIndex++;
  670. }
  671. maxRedoIndex = undoStateIndex;
  672. }
  673. void UITextInput::setUndoState(UITextInputUndoState state) {
  674. clearSelection();
  675. setText(state.content);
  676. currentLine = lines[state.lineOffset];
  677. caretPosition = state.caretPosition;
  678. lineOffset = state.lineOffset;
  679. updateCaretPosition();
  680. if(state.hasSelection) {
  681. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  682. }
  683. }
  684. void UITextInput::Undo() {
  685. if(undoStateIndex > 0) {
  686. undoStateIndex--;
  687. setUndoState(undoStates[undoStateIndex]);
  688. }
  689. }
  690. void UITextInput::Redo() {
  691. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  692. undoStateIndex++;
  693. setUndoState(undoStates[undoStateIndex]);
  694. }
  695. }
  696. void UITextInput::Cut() {
  697. saveUndoState();
  698. Copy();
  699. if(hasSelection) {
  700. deleteSelection();
  701. }
  702. }
  703. void UITextInput::Copy() {
  704. if(hasSelection) {
  705. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  706. }
  707. }
  708. void UITextInput::Paste() {
  709. saveUndoState();
  710. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  711. }
  712. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  713. if(top) {
  714. scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
  715. } else {
  716. scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
  717. }
  718. }
  719. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  720. if(!hasFocus)
  721. return;
  722. // Logger::log("UCHAR: %d\n", charCode);
  723. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  724. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  725. selectAll();
  726. return;
  727. }
  728. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  729. Copy();
  730. return;
  731. }
  732. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  733. Cut();
  734. return;
  735. }
  736. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  737. Undo();
  738. return;
  739. }
  740. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  741. Redo();
  742. return;
  743. }
  744. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  745. Redo();
  746. return;
  747. }
  748. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  749. Paste();
  750. return;
  751. }
  752. if(key == KEY_LEFT) {
  753. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  754. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  755. if(hasSelection) {
  756. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  757. } else {
  758. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  759. }
  760. } else {
  761. caretPosition = 0;
  762. clearSelection();
  763. updateCaretPosition();
  764. }
  765. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  766. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  767. if(hasSelection) {
  768. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  769. } else {
  770. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  771. }
  772. } else {
  773. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  774. clearSelection();
  775. updateCaretPosition();
  776. }
  777. } else {
  778. if(caretPosition > 0) {
  779. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  780. if(hasSelection) {
  781. if(selectionCaretPosition > 0)
  782. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  783. } else {
  784. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  785. }
  786. } else {
  787. caretPosition--;
  788. clearSelection();
  789. updateCaretPosition();
  790. }
  791. }
  792. }
  793. return;
  794. }
  795. if(key == KEY_RIGHT) {
  796. if(caretPosition < currentLine->getText().length()) {
  797. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  798. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  799. if(hasSelection) {
  800. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  801. } else {
  802. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  803. }
  804. } else {
  805. caretPosition = currentLine->getText().length();
  806. clearSelection();
  807. }
  808. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  809. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  810. if(hasSelection) {
  811. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  812. } else {
  813. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  814. }
  815. } else {
  816. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  817. clearSelection();
  818. }
  819. } else {
  820. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  821. if(hasSelection) {
  822. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  823. } else {
  824. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  825. }
  826. } else {
  827. caretPosition++;
  828. clearSelection();
  829. }
  830. }
  831. updateCaretPosition();
  832. }
  833. return;
  834. }
  835. if(key == KEY_PAGEUP) {
  836. if(multiLine) {
  837. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  838. }
  839. return;
  840. }
  841. if(key == KEY_PAGEDOWN) {
  842. if(multiLine) {
  843. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  844. }
  845. return;
  846. }
  847. if(key == KEY_UP) {
  848. if(multiLine) {
  849. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  850. if(hasSelection) {
  851. if(selectionLine > 0)
  852. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  853. } else {
  854. if(this->lineOffset > 0)
  855. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  856. }
  857. } else {
  858. clearSelection();
  859. if(lineOffset > 0) {
  860. lineOffset--;
  861. selectLineFromOffset();
  862. updateCaretPosition();
  863. }
  864. }
  865. }
  866. blinkerRect->visible = true;
  867. return;
  868. }
  869. if(key == KEY_DOWN) {
  870. if(multiLine) {
  871. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  872. if(hasSelection) {
  873. if(selectionLine < lines.size()-1)
  874. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  875. } else {
  876. if(this->lineOffset < lines.size()-1)
  877. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  878. }
  879. } else {
  880. clearSelection();
  881. if(lineOffset < lines.size()-1) {
  882. lineOffset++;
  883. selectLineFromOffset();
  884. updateCaretPosition();
  885. }
  886. }
  887. }
  888. blinkerRect->visible = true;
  889. return;
  890. }
  891. if(key == KEY_ESCAPE) {
  892. if(!multiLine) {
  893. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  894. }
  895. }
  896. if(key == KEY_RETURN) {
  897. if(multiLine) {
  898. saveUndoState();
  899. if(hasSelection) {
  900. deleteSelection();
  901. }
  902. insertLine(true);
  903. updateCaretPosition();
  904. } else {
  905. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  906. }
  907. return;
  908. }
  909. String ctext = currentLine->getText();
  910. // if(1) {
  911. if((charCode > 31 && charCode < 127) || charCode > 127) {
  912. if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
  913. saveUndoState();
  914. if(hasSelection)
  915. deleteSelection();
  916. ctext = currentLine->getText();
  917. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  918. ctext = ctext.substr(0,caretPosition);
  919. ctext += charCode + text2;
  920. caretPosition++;
  921. }
  922. }
  923. if(key == KEY_TAB && multiLine) {
  924. saveUndoState();
  925. if(hasSelection)
  926. deleteSelection();
  927. ctext = currentLine->getText();
  928. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  929. ctext = ctext.substr(0,caretPosition);
  930. ctext += (wchar_t)'\t' + text2;
  931. caretPosition++;
  932. }
  933. if(key == KEY_BACKSPACE) {
  934. if(hasSelection) {
  935. saveUndoState();
  936. deleteSelection();
  937. return;
  938. } else {
  939. ctext = currentLine->getText();
  940. if(caretPosition > 0) {
  941. saveUndoState();
  942. if(ctext.length() > 0) {
  943. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  944. ctext = ctext.substr(0,caretPosition-1);
  945. ctext += text2;
  946. caretPosition--;
  947. }
  948. } else {
  949. if(lineOffset > 0) {
  950. saveUndoState();
  951. ScreenLabel *lineToRemove = currentLine;
  952. lineOffset--;
  953. selectLineFromOffset();
  954. caretPosition = currentLine->getText().length();
  955. updateCaretPosition();
  956. currentLine->setText(currentLine->getText() + ctext);
  957. removeLine(lineToRemove);
  958. return;
  959. }
  960. }
  961. }
  962. }
  963. currentLine->setText(ctext);
  964. changedText();
  965. updateCaretPosition();
  966. }
  967. void UITextInput::Update() {
  968. if(!multiLine) {
  969. Vector2 pos = getScreenPosition();
  970. scissorBox.setRect(pos.x,pos.y, width, height);
  971. }
  972. if(hasSelection) {
  973. blinkerRect->visible = false;
  974. }
  975. blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y - 2);
  976. if(hasFocus) {
  977. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  978. } else {
  979. blinkerRect->visible = false;
  980. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  981. }
  982. for(int i=0; i < linesToDelete.size(); i++) {
  983. delete linesToDelete[i];
  984. }
  985. linesToDelete.clear();
  986. }
  987. UITextInput::~UITextInput() {
  988. }
  989. void UITextInput::handleEvent(Event *event) {
  990. if(event->getDispatcher() == inputRect) {
  991. switch(event->getEventCode()) {
  992. case InputEvent::EVENT_MOUSEDOWN:
  993. if(parentEntity) {
  994. ((ScreenEntity*)parentEntity)->focusChild(this);
  995. } else {
  996. hasFocus = true;
  997. }
  998. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  999. draggingSelection = true;
  1000. break;
  1001. case InputEvent::EVENT_MOUSEUP:
  1002. draggingSelection = false;
  1003. break;
  1004. case InputEvent::EVENT_DOUBLECLICK:
  1005. selectWordAtCaret();
  1006. break;
  1007. case InputEvent::EVENT_MOUSEMOVE:
  1008. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1009. if(draggingSelection) {
  1010. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1011. }
  1012. break;
  1013. case InputEvent::EVENT_MOUSEOVER:
  1014. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1015. break;
  1016. case InputEvent::EVENT_MOUSEOUT:
  1017. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  1018. break;
  1019. }
  1020. }
  1021. if(event->getDispatcher() == blinkTimer) {
  1022. if(hasSelection || draggingSelection) {
  1023. blinkerRect->visible = false;
  1024. } else {
  1025. if(hasFocus)
  1026. blinkerRect->visible = !blinkerRect->visible;
  1027. else
  1028. blinkerRect->visible = false;
  1029. }
  1030. }
  1031. }