PolyUITextInput.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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()+1) * ((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(caretPosition < 0)
  434. caretPosition = 0;
  435. setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
  436. }
  437. int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
  438. for(int i=caretPosition; i > 0; i--) {
  439. String bit = lines[caretLine]->getText().substr(i,1);
  440. char chr = ((char*)bit.c_str())[0];
  441. if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i < caretPosition-1) {
  442. return i+1;
  443. }
  444. }
  445. return 0;
  446. }
  447. int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
  448. int len = lines[caretLine]->getText().length();
  449. for(int i=caretPosition; i < len; i++) {
  450. String bit = lines[caretLine]->getText().substr(i,1);
  451. char chr = ((char*)bit.c_str())[0];
  452. if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i > caretPosition) {
  453. return i;
  454. }
  455. }
  456. return lines[caretLine]->getText().length();
  457. }
  458. void UITextInput::selectWordAtCaret() {
  459. int selectStart = 0;
  460. int len = currentLine->getText().length();
  461. int selectEnd = len;
  462. for(int i=this->caretPosition; i > 0; i--) {
  463. String bit = currentLine->getText().substr(i,1);
  464. wchar_t chr = ((wchar_t*)bit.c_str())[0];
  465. if( (chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) {
  466. selectStart = i+1;
  467. break;
  468. }
  469. }
  470. for(int i=this->caretPosition; i < len; i++) {
  471. String bit = currentLine->getText().substr(i,1);
  472. wchar_t chr = ((wchar_t*)bit.c_str())[0];
  473. if( (chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) {
  474. selectEnd = i;
  475. break;
  476. }
  477. }
  478. setSelection(this->lineOffset, this->lineOffset, selectStart, selectEnd);
  479. }
  480. void UITextInput::setCaretToMouse(Number x, Number y) {
  481. clearSelection();
  482. x -= padding;
  483. y -= padding;
  484. //if(lines.size() > 1) {
  485. lineOffset = y / (lineHeight+lineSpacing);
  486. if(lineOffset > lines.size()-1)
  487. lineOffset = lines.size()-1;
  488. selectLineFromOffset();
  489. //}
  490. int len = currentLine->getText().length();
  491. Number slen;
  492. for(int i=0; i < len; i++) {
  493. slen = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), currentLine->getText().substr(0,i), fontSize);
  494. if(slen > x) {
  495. caretPosition = i;
  496. break;
  497. }
  498. }
  499. if(x > slen)
  500. caretPosition = len;
  501. updateCaretPosition();
  502. }
  503. void UITextInput::removeLine(ScreenLabel *line) {
  504. for(int i=0;i<lines.size();i++) {
  505. if(lines[i] == line) {
  506. lines.erase(lines.begin()+i);
  507. }
  508. }
  509. linesContainer->removeChild(line);
  510. linesToDelete.push_back(line);
  511. restructLines();
  512. changedText();
  513. }
  514. void UITextInput::selectAll() {
  515. setSelection(0, lines.size()-1, 0, lines[lines.size()-1]->getText().length());
  516. }
  517. void UITextInput::insertText(String text) {
  518. vector<String> strings = text.split("\n");
  519. settingText = true;
  520. if(hasSelection)
  521. deleteSelection();
  522. if(strings.size() > 1) {
  523. String ctext = currentLine->getText();
  524. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  525. ctext = ctext.substr(0,caretPosition);
  526. ctext += strings[0];
  527. currentLine->setText(ctext);
  528. caretPosition = ctext.length();
  529. for(int i=1; i < strings.size()-1; i++) {
  530. insertLine(true);
  531. ctext = strings[i];
  532. currentLine->setText(ctext);
  533. caretPosition = ctext.length();
  534. }
  535. insertLine(true);
  536. ctext = strings[strings.size()-1] + text2;
  537. caretPosition = ctext.length();
  538. currentLine->setText(ctext);
  539. } else {
  540. String ctext = currentLine->getText();
  541. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  542. ctext = ctext.substr(0,caretPosition);
  543. ctext += text + text2;
  544. caretPosition += text.length();
  545. currentLine->setText(ctext);
  546. }
  547. settingText = false;
  548. changedText();
  549. updateCaretPosition();
  550. restructLines();
  551. }
  552. String UITextInput::getSelectionText() {
  553. String totalText = L"";
  554. if(selectionTop == selectionBottom) {
  555. totalText = lines[selectionTop]->getText().substr(selectionL, selectionR-selectionL);
  556. return totalText;
  557. } else {
  558. totalText += lines[selectionTop]->getText().substr(selectionL, lines[selectionTop]->getText().length()-selectionL);
  559. totalText += L"\n";
  560. }
  561. if(selectionBottom > selectionTop+1) {
  562. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  563. totalText += lines[i]->getText();
  564. totalText += L"\n";
  565. }
  566. }
  567. totalText += lines[selectionBottom]->getText().substr(0, selectionL);
  568. return totalText;
  569. }
  570. UIScrollContainer *UITextInput::getScrollContainer() {
  571. return scrollContainer;
  572. }
  573. void UITextInput::saveUndoState() {
  574. UITextInputUndoState newState;
  575. newState.content = getText();
  576. newState.caretPosition = caretPosition;
  577. newState.lineOffset = lineOffset;
  578. newState.hasSelection = hasSelection;
  579. if(hasSelection) {
  580. newState.selectionLine = selectionLine;
  581. newState.selectionCaretPosition = selectionCaretPosition;
  582. }
  583. undoStates[undoStateIndex] = newState;
  584. // if we hit undo state capacity, shift the whole stack
  585. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  586. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  587. undoStates[i] = undoStates[i+1];
  588. }
  589. } else {
  590. undoStateIndex++;
  591. }
  592. maxRedoIndex = undoStateIndex;
  593. }
  594. void UITextInput::setUndoState(UITextInputUndoState state) {
  595. clearSelection();
  596. setText(state.content);
  597. currentLine = lines[state.lineOffset];
  598. caretPosition = state.caretPosition;
  599. lineOffset = state.lineOffset;
  600. updateCaretPosition();
  601. if(state.hasSelection) {
  602. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  603. }
  604. }
  605. void UITextInput::Undo() {
  606. if(undoStateIndex > 0) {
  607. undoStateIndex--;
  608. setUndoState(undoStates[undoStateIndex]);
  609. }
  610. }
  611. void UITextInput::Redo() {
  612. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  613. undoStateIndex++;
  614. setUndoState(undoStates[undoStateIndex]);
  615. }
  616. }
  617. void UITextInput::Cut() {
  618. saveUndoState();
  619. Copy();
  620. if(hasSelection) {
  621. deleteSelection();
  622. }
  623. }
  624. void UITextInput::Copy() {
  625. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  626. }
  627. void UITextInput::Paste() {
  628. saveUndoState();
  629. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  630. }
  631. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  632. }
  633. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  634. if(!hasFocus)
  635. return;
  636. // Logger::log("UCHAR: %d\n", charCode);
  637. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  638. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  639. selectAll();
  640. return;
  641. }
  642. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  643. Copy();
  644. return;
  645. }
  646. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  647. Cut();
  648. return;
  649. }
  650. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  651. Undo();
  652. return;
  653. }
  654. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  655. Redo();
  656. return;
  657. }
  658. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  659. Redo();
  660. return;
  661. }
  662. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  663. Paste();
  664. return;
  665. }
  666. if(key == KEY_LEFT) {
  667. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  668. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  669. if(hasSelection) {
  670. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  671. } else {
  672. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  673. }
  674. } else {
  675. caretPosition = 0;
  676. clearSelection();
  677. updateCaretPosition();
  678. }
  679. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  680. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  681. if(hasSelection) {
  682. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  683. } else {
  684. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  685. }
  686. } else {
  687. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  688. clearSelection();
  689. updateCaretPosition();
  690. }
  691. } else {
  692. if(caretPosition > 0) {
  693. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  694. if(hasSelection) {
  695. if(selectionCaretPosition > 0)
  696. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  697. } else {
  698. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  699. }
  700. } else {
  701. caretPosition--;
  702. clearSelection();
  703. updateCaretPosition();
  704. }
  705. }
  706. }
  707. return;
  708. }
  709. if(key == KEY_RIGHT) {
  710. if(caretPosition < currentLine->getText().length()) {
  711. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  712. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  713. if(hasSelection) {
  714. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  715. } else {
  716. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  717. }
  718. } else {
  719. caretPosition = currentLine->getText().length();
  720. clearSelection();
  721. }
  722. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  723. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  724. if(hasSelection) {
  725. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  726. } else {
  727. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  728. }
  729. } else {
  730. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  731. clearSelection();
  732. }
  733. } else {
  734. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  735. if(hasSelection) {
  736. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  737. } else {
  738. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  739. }
  740. } else {
  741. caretPosition++;
  742. clearSelection();
  743. }
  744. }
  745. updateCaretPosition();
  746. }
  747. return;
  748. }
  749. if(key == KEY_PAGEUP) {
  750. if(multiLine) {
  751. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  752. }
  753. return;
  754. }
  755. if(key == KEY_PAGEDOWN) {
  756. if(multiLine) {
  757. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  758. }
  759. return;
  760. }
  761. if(key == KEY_UP) {
  762. if(multiLine) {
  763. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  764. if(hasSelection) {
  765. if(selectionLine > 0)
  766. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  767. } else {
  768. if(this->lineOffset > 0)
  769. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  770. }
  771. } else {
  772. clearSelection();
  773. if(lineOffset > 0) {
  774. lineOffset--;
  775. selectLineFromOffset();
  776. updateCaretPosition();
  777. }
  778. }
  779. }
  780. blinkerRect->visible = true;
  781. return;
  782. }
  783. if(key == KEY_DOWN) {
  784. if(multiLine) {
  785. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  786. if(hasSelection) {
  787. if(selectionLine < lines.size()-1)
  788. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  789. } else {
  790. if(this->lineOffset < lines.size()-1)
  791. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  792. }
  793. } else {
  794. clearSelection();
  795. if(lineOffset < lines.size()-1) {
  796. lineOffset++;
  797. selectLineFromOffset();
  798. updateCaretPosition();
  799. }
  800. }
  801. }
  802. blinkerRect->visible = true;
  803. return;
  804. }
  805. if(key == KEY_RETURN) {
  806. if(multiLine) {
  807. saveUndoState();
  808. if(hasSelection) {
  809. deleteSelection();
  810. }
  811. insertLine(true);
  812. updateCaretPosition();
  813. } else {
  814. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  815. }
  816. return;
  817. }
  818. String ctext = currentLine->getText();
  819. // if(1) {
  820. if((charCode > 31 && charCode < 127) || charCode > 127) {
  821. if(!isNumberOnly || (isNumberOnly && (charCode > 47 && charCode < 58))) {
  822. saveUndoState();
  823. if(hasSelection)
  824. deleteSelection();
  825. ctext = currentLine->getText();
  826. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  827. ctext = ctext.substr(0,caretPosition);
  828. ctext += charCode + text2;
  829. caretPosition++;
  830. }
  831. }
  832. if(key == KEY_TAB && multiLine) {
  833. saveUndoState();
  834. if(hasSelection)
  835. deleteSelection();
  836. ctext = currentLine->getText();
  837. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  838. ctext = ctext.substr(0,caretPosition);
  839. ctext += (wchar_t)'\t' + text2;
  840. caretPosition++;
  841. }
  842. if(key == KEY_BACKSPACE) {
  843. if(hasSelection) {
  844. saveUndoState();
  845. deleteSelection();
  846. return;
  847. } else {
  848. ctext = currentLine->getText();
  849. if(caretPosition > 0) {
  850. saveUndoState();
  851. if(ctext.length() > 0) {
  852. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  853. ctext = ctext.substr(0,caretPosition-1);
  854. ctext += text2;
  855. caretPosition--;
  856. }
  857. } else {
  858. if(lineOffset > 0) {
  859. saveUndoState();
  860. ScreenLabel *lineToRemove = currentLine;
  861. lineOffset--;
  862. selectLineFromOffset();
  863. caretPosition = currentLine->getText().length();
  864. updateCaretPosition();
  865. currentLine->setText(currentLine->getText() + ctext);
  866. removeLine(lineToRemove);
  867. return;
  868. }
  869. }
  870. }
  871. }
  872. currentLine->setText(ctext);
  873. changedText();
  874. updateCaretPosition();
  875. }
  876. void UITextInput::Update() {
  877. if(hasSelection) {
  878. blinkerRect->visible = false;
  879. }
  880. blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y);
  881. if(hasFocus) {
  882. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  883. } else {
  884. blinkerRect->visible = false;
  885. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  886. }
  887. for(int i=0; i < linesToDelete.size(); i++) {
  888. delete linesToDelete[i];
  889. }
  890. linesToDelete.clear();
  891. }
  892. UITextInput::~UITextInput() {
  893. }
  894. void UITextInput::handleEvent(Event *event) {
  895. if(event->getDispatcher() == inputRect) {
  896. switch(event->getEventCode()) {
  897. case InputEvent::EVENT_MOUSEDOWN:
  898. if(parentEntity) {
  899. ((ScreenEntity*)parentEntity)->focusChild(this);
  900. } else {
  901. hasFocus = true;
  902. }
  903. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  904. draggingSelection = true;
  905. break;
  906. case InputEvent::EVENT_MOUSEUP:
  907. draggingSelection = false;
  908. break;
  909. case InputEvent::EVENT_DOUBLECLICK:
  910. selectWordAtCaret();
  911. break;
  912. case InputEvent::EVENT_MOUSEMOVE:
  913. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  914. if(draggingSelection) {
  915. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  916. }
  917. break;
  918. case InputEvent::EVENT_MOUSEOVER:
  919. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  920. break;
  921. case InputEvent::EVENT_MOUSEOUT:
  922. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  923. break;
  924. }
  925. }
  926. if(event->getDispatcher() == blinkTimer) {
  927. if(hasSelection || draggingSelection) {
  928. blinkerRect->visible = false;
  929. } else {
  930. if(hasFocus)
  931. blinkerRect->visible = !blinkerRect->visible;
  932. else
  933. blinkerRect->visible = false;
  934. }
  935. }
  936. }