PolyUITextInput.cpp 30 KB

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