PolyUITextInput.cpp 34 KB

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