PolyUITextInput.cpp 32 KB

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