PolyUITextInput.cpp 30 KB

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