PolyUITextInput.cpp 31 KB

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