PolyUITextInput.cpp 34 KB

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