PolyUITextInput.cpp 29 KB

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