PolyUITextInput.cpp 31 KB

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