PolyUITextInput.cpp 32 KB

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