PolyUITextInput.cpp 33 KB

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