PolyUITextInput.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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::getLineText(unsigned int index) {
  553. if(index < lines.size()) {
  554. return lines[index]->getText();
  555. } else {
  556. return "";
  557. }
  558. }
  559. String UITextInput::getSelectionText() {
  560. String totalText = L"";
  561. if(selectionTop == selectionBottom) {
  562. totalText = lines[selectionTop]->getText().substr(selectionL, selectionR-selectionL);
  563. return totalText;
  564. } else {
  565. totalText += lines[selectionTop]->getText().substr(selectionL, lines[selectionTop]->getText().length()-selectionL);
  566. totalText += L"\n";
  567. }
  568. if(selectionBottom > selectionTop+1) {
  569. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  570. totalText += lines[i]->getText();
  571. totalText += L"\n";
  572. }
  573. }
  574. totalText += lines[selectionBottom]->getText().substr(0, selectionL);
  575. return totalText;
  576. }
  577. UIScrollContainer *UITextInput::getScrollContainer() {
  578. return scrollContainer;
  579. }
  580. void UITextInput::saveUndoState() {
  581. UITextInputUndoState newState;
  582. newState.content = getText();
  583. newState.caretPosition = caretPosition;
  584. newState.lineOffset = lineOffset;
  585. newState.hasSelection = hasSelection;
  586. if(hasSelection) {
  587. newState.selectionLine = selectionLine;
  588. newState.selectionCaretPosition = selectionCaretPosition;
  589. }
  590. undoStates[undoStateIndex] = newState;
  591. // if we hit undo state capacity, shift the whole stack
  592. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  593. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  594. undoStates[i] = undoStates[i+1];
  595. }
  596. } else {
  597. undoStateIndex++;
  598. }
  599. maxRedoIndex = undoStateIndex;
  600. }
  601. void UITextInput::setUndoState(UITextInputUndoState state) {
  602. clearSelection();
  603. setText(state.content);
  604. currentLine = lines[state.lineOffset];
  605. caretPosition = state.caretPosition;
  606. lineOffset = state.lineOffset;
  607. updateCaretPosition();
  608. if(state.hasSelection) {
  609. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  610. }
  611. }
  612. void UITextInput::Undo() {
  613. if(undoStateIndex > 0) {
  614. undoStateIndex--;
  615. setUndoState(undoStates[undoStateIndex]);
  616. }
  617. }
  618. void UITextInput::Redo() {
  619. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  620. undoStateIndex++;
  621. setUndoState(undoStates[undoStateIndex]);
  622. }
  623. }
  624. void UITextInput::Cut() {
  625. saveUndoState();
  626. Copy();
  627. if(hasSelection) {
  628. deleteSelection();
  629. }
  630. }
  631. void UITextInput::Copy() {
  632. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  633. }
  634. void UITextInput::Paste() {
  635. saveUndoState();
  636. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  637. }
  638. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  639. }
  640. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  641. if(!hasFocus)
  642. return;
  643. // Logger::log("UCHAR: %d\n", charCode);
  644. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  645. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  646. selectAll();
  647. return;
  648. }
  649. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  650. Copy();
  651. return;
  652. }
  653. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  654. Cut();
  655. return;
  656. }
  657. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  658. Undo();
  659. return;
  660. }
  661. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  662. Redo();
  663. return;
  664. }
  665. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  666. Redo();
  667. return;
  668. }
  669. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  670. Paste();
  671. return;
  672. }
  673. if(key == KEY_LEFT) {
  674. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  675. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  676. if(hasSelection) {
  677. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  678. } else {
  679. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  680. }
  681. } else {
  682. caretPosition = 0;
  683. clearSelection();
  684. updateCaretPosition();
  685. }
  686. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  687. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  688. if(hasSelection) {
  689. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  690. } else {
  691. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  692. }
  693. } else {
  694. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  695. clearSelection();
  696. updateCaretPosition();
  697. }
  698. } else {
  699. if(caretPosition > 0) {
  700. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  701. if(hasSelection) {
  702. if(selectionCaretPosition > 0)
  703. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  704. } else {
  705. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  706. }
  707. } else {
  708. caretPosition--;
  709. clearSelection();
  710. updateCaretPosition();
  711. }
  712. }
  713. }
  714. return;
  715. }
  716. if(key == KEY_RIGHT) {
  717. if(caretPosition < currentLine->getText().length()) {
  718. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  719. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  720. if(hasSelection) {
  721. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  722. } else {
  723. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  724. }
  725. } else {
  726. caretPosition = currentLine->getText().length();
  727. clearSelection();
  728. }
  729. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  730. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  731. if(hasSelection) {
  732. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  733. } else {
  734. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  735. }
  736. } else {
  737. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  738. clearSelection();
  739. }
  740. } else {
  741. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  742. if(hasSelection) {
  743. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  744. } else {
  745. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  746. }
  747. } else {
  748. caretPosition++;
  749. clearSelection();
  750. }
  751. }
  752. updateCaretPosition();
  753. }
  754. return;
  755. }
  756. if(key == KEY_PAGEUP) {
  757. if(multiLine) {
  758. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  759. }
  760. return;
  761. }
  762. if(key == KEY_PAGEDOWN) {
  763. if(multiLine) {
  764. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  765. }
  766. return;
  767. }
  768. if(key == KEY_UP) {
  769. if(multiLine) {
  770. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  771. if(hasSelection) {
  772. if(selectionLine > 0)
  773. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  774. } else {
  775. if(this->lineOffset > 0)
  776. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  777. }
  778. } else {
  779. clearSelection();
  780. if(lineOffset > 0) {
  781. lineOffset--;
  782. selectLineFromOffset();
  783. updateCaretPosition();
  784. }
  785. }
  786. }
  787. blinkerRect->visible = true;
  788. return;
  789. }
  790. if(key == KEY_DOWN) {
  791. if(multiLine) {
  792. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  793. if(hasSelection) {
  794. if(selectionLine < lines.size()-1)
  795. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  796. } else {
  797. if(this->lineOffset < lines.size()-1)
  798. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  799. }
  800. } else {
  801. clearSelection();
  802. if(lineOffset < lines.size()-1) {
  803. lineOffset++;
  804. selectLineFromOffset();
  805. updateCaretPosition();
  806. }
  807. }
  808. }
  809. blinkerRect->visible = true;
  810. return;
  811. }
  812. if(key == KEY_RETURN) {
  813. if(multiLine) {
  814. saveUndoState();
  815. if(hasSelection) {
  816. deleteSelection();
  817. }
  818. insertLine(true);
  819. updateCaretPosition();
  820. } else {
  821. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  822. }
  823. return;
  824. }
  825. String ctext = currentLine->getText();
  826. // if(1) {
  827. if((charCode > 31 && charCode < 127) || charCode > 127) {
  828. if(!isNumberOnly || (isNumberOnly && (charCode > 47 && charCode < 58))) {
  829. saveUndoState();
  830. if(hasSelection)
  831. deleteSelection();
  832. ctext = currentLine->getText();
  833. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  834. ctext = ctext.substr(0,caretPosition);
  835. ctext += charCode + text2;
  836. caretPosition++;
  837. }
  838. }
  839. if(key == KEY_TAB && multiLine) {
  840. saveUndoState();
  841. if(hasSelection)
  842. deleteSelection();
  843. ctext = currentLine->getText();
  844. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  845. ctext = ctext.substr(0,caretPosition);
  846. ctext += (wchar_t)'\t' + text2;
  847. caretPosition++;
  848. }
  849. if(key == KEY_BACKSPACE) {
  850. if(hasSelection) {
  851. saveUndoState();
  852. deleteSelection();
  853. return;
  854. } else {
  855. ctext = currentLine->getText();
  856. if(caretPosition > 0) {
  857. saveUndoState();
  858. if(ctext.length() > 0) {
  859. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  860. ctext = ctext.substr(0,caretPosition-1);
  861. ctext += text2;
  862. caretPosition--;
  863. }
  864. } else {
  865. if(lineOffset > 0) {
  866. saveUndoState();
  867. ScreenLabel *lineToRemove = currentLine;
  868. lineOffset--;
  869. selectLineFromOffset();
  870. caretPosition = currentLine->getText().length();
  871. updateCaretPosition();
  872. currentLine->setText(currentLine->getText() + ctext);
  873. removeLine(lineToRemove);
  874. return;
  875. }
  876. }
  877. }
  878. }
  879. currentLine->setText(ctext);
  880. changedText();
  881. updateCaretPosition();
  882. }
  883. void UITextInput::Update() {
  884. if(hasSelection) {
  885. blinkerRect->visible = false;
  886. }
  887. blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y);
  888. if(hasFocus) {
  889. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  890. } else {
  891. blinkerRect->visible = false;
  892. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  893. }
  894. for(int i=0; i < linesToDelete.size(); i++) {
  895. delete linesToDelete[i];
  896. }
  897. linesToDelete.clear();
  898. }
  899. UITextInput::~UITextInput() {
  900. }
  901. void UITextInput::handleEvent(Event *event) {
  902. if(event->getDispatcher() == inputRect) {
  903. switch(event->getEventCode()) {
  904. case InputEvent::EVENT_MOUSEDOWN:
  905. if(parentEntity) {
  906. ((ScreenEntity*)parentEntity)->focusChild(this);
  907. } else {
  908. hasFocus = true;
  909. }
  910. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  911. draggingSelection = true;
  912. break;
  913. case InputEvent::EVENT_MOUSEUP:
  914. draggingSelection = false;
  915. break;
  916. case InputEvent::EVENT_DOUBLECLICK:
  917. selectWordAtCaret();
  918. break;
  919. case InputEvent::EVENT_MOUSEMOVE:
  920. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  921. if(draggingSelection) {
  922. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  923. }
  924. break;
  925. case InputEvent::EVENT_MOUSEOVER:
  926. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  927. break;
  928. case InputEvent::EVENT_MOUSEOUT:
  929. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  930. break;
  931. }
  932. }
  933. if(event->getDispatcher() == blinkTimer) {
  934. if(hasSelection || draggingSelection) {
  935. blinkerRect->visible = false;
  936. } else {
  937. if(hasFocus)
  938. blinkerRect->visible = !blinkerRect->visible;
  939. else
  940. blinkerRect->visible = false;
  941. }
  942. }
  943. }