PolyUITextInput.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. if(multiLine && currentLine) {
  330. if(linesContainer->getPosition().y + currentLine->getPosition2D().y < 0.0) {
  331. scrollContainer->scrollVertical(-(lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  332. } else if(linesContainer->getPosition().y + currentLine->getPosition2D().y > scrollContainer->getHeight()) {
  333. scrollContainer->scrollVertical((lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  334. }
  335. }
  336. }
  337. void UITextInput::selectLineFromOffset() {
  338. for(int i=0; i < lines.size(); i++) {
  339. if(i == lineOffset) {
  340. currentLine = lines[i];
  341. return;
  342. }
  343. }
  344. }
  345. void UITextInput::dragSelectionTo(Number x, Number y) {
  346. x -= padding;
  347. y -= padding;
  348. int lineOffset = y / (lineHeight+lineSpacing);
  349. if(lineOffset > lines.size()-1)
  350. lineOffset = lines.size()-1;
  351. ScreenLabel *selectToLine = lines[lineOffset];
  352. int len = selectToLine->getText().length();
  353. Number slen;
  354. int caretPosition = selectToLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), selectToLine->getText().substr(0,len), fontSize);
  355. for(int i=0; i < len; i++) {
  356. slen = selectToLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), selectToLine->getText().substr(0,i), fontSize);
  357. if(slen > x) {
  358. caretPosition = i;
  359. break;
  360. }
  361. }
  362. if(x > slen)
  363. caretPosition = len;
  364. if(caretPosition < 0)
  365. caretPosition = 0;
  366. setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
  367. }
  368. int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
  369. for(int i=caretPosition; i > 0; i--) {
  370. String bit = lines[caretLine]->getText().substr(i,1);
  371. char chr = ((char*)bit.c_str())[0];
  372. if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i < caretPosition-1) {
  373. return i+1;
  374. }
  375. }
  376. return 0;
  377. }
  378. int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
  379. int len = lines[caretLine]->getText().length();
  380. for(int i=caretPosition; i < len; i++) {
  381. String bit = lines[caretLine]->getText().substr(i,1);
  382. char chr = ((char*)bit.c_str())[0];
  383. if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i > caretPosition) {
  384. return i;
  385. }
  386. }
  387. return lines[caretLine]->getText().length();
  388. }
  389. void UITextInput::selectWordAtCaret() {
  390. int selectStart = 0;
  391. int len = currentLine->getText().length();
  392. int selectEnd = len;
  393. for(int i=this->caretPosition; i > 0; i--) {
  394. String bit = currentLine->getText().substr(i,1);
  395. wchar_t chr = ((wchar_t*)bit.c_str())[0];
  396. if( (chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) {
  397. selectStart = i+1;
  398. break;
  399. }
  400. }
  401. for(int i=this->caretPosition; i < len; i++) {
  402. String bit = currentLine->getText().substr(i,1);
  403. wchar_t chr = ((wchar_t*)bit.c_str())[0];
  404. if( (chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) {
  405. selectEnd = i;
  406. break;
  407. }
  408. }
  409. setSelection(this->lineOffset, this->lineOffset, selectStart, selectEnd);
  410. }
  411. void UITextInput::setCaretToMouse(Number x, Number y) {
  412. clearSelection();
  413. x -= padding;
  414. y -= padding;
  415. //if(lines.size() > 1) {
  416. lineOffset = y / (lineHeight+lineSpacing);
  417. if(lineOffset > lines.size()-1)
  418. lineOffset = lines.size()-1;
  419. selectLineFromOffset();
  420. //}
  421. int len = currentLine->getText().length();
  422. Number slen;
  423. for(int i=0; i < len; i++) {
  424. slen = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), currentLine->getText().substr(0,i), fontSize);
  425. if(slen > x) {
  426. caretPosition = i;
  427. break;
  428. }
  429. }
  430. if(x > slen)
  431. caretPosition = len;
  432. updateCaretPosition();
  433. }
  434. void UITextInput::removeLine(ScreenLabel *line) {
  435. for(int i=0;i<lines.size();i++) {
  436. if(lines[i] == line) {
  437. lines.erase(lines.begin()+i);
  438. }
  439. }
  440. linesContainer->removeChild(line);
  441. linesToDelete.push_back(line);
  442. restructLines();
  443. }
  444. void UITextInput::selectAll() {
  445. setSelection(0, lines.size()-1, 0, lines[lines.size()-1]->getText().length());
  446. }
  447. void UITextInput::insertText(String text) {
  448. vector<String> strings = text.split("\n");
  449. if(hasSelection)
  450. deleteSelection();
  451. if(strings.size() > 1) {
  452. String ctext = currentLine->getText();
  453. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  454. ctext = ctext.substr(0,caretPosition);
  455. ctext += strings[0];
  456. currentLine->setText(ctext);
  457. caretPosition = ctext.length();
  458. for(int i=1; i < strings.size()-1; i++) {
  459. insertLine(true);
  460. ctext = strings[i];
  461. currentLine->setText(ctext);
  462. caretPosition = ctext.length();
  463. }
  464. insertLine(true);
  465. ctext = strings[strings.size()-1] + text2;
  466. caretPosition = ctext.length();
  467. currentLine->setText(ctext);
  468. } else {
  469. String ctext = currentLine->getText();
  470. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  471. ctext = ctext.substr(0,caretPosition);
  472. ctext += text + text2;
  473. caretPosition += text.length();
  474. currentLine->setText(ctext);
  475. }
  476. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  477. updateCaretPosition();
  478. restructLines();
  479. }
  480. String UITextInput::getSelectionText() {
  481. String totalText = L"";
  482. if(selectionTop == selectionBottom) {
  483. totalText = lines[selectionTop]->getText().substr(selectionL, selectionR-selectionL);
  484. return totalText;
  485. } else {
  486. totalText += lines[selectionTop]->getText().substr(selectionL, lines[selectionTop]->getText().length()-selectionL);
  487. totalText += L"\n";
  488. }
  489. if(selectionBottom > selectionTop+1) {
  490. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  491. totalText += lines[i]->getText();
  492. totalText += L"\n";
  493. }
  494. }
  495. totalText += lines[selectionBottom]->getText().substr(0, selectionL);
  496. return totalText;
  497. }
  498. UIScrollContainer *UITextInput::getScrollContainer() {
  499. return scrollContainer;
  500. }
  501. void UITextInput::saveUndoState() {
  502. UITextInputUndoState newState;
  503. newState.content = getText();
  504. newState.caretPosition = caretPosition;
  505. newState.lineOffset = lineOffset;
  506. newState.hasSelection = hasSelection;
  507. if(hasSelection) {
  508. newState.selectionLine = selectionLine;
  509. newState.selectionCaretPosition = selectionCaretPosition;
  510. }
  511. undoStates[undoStateIndex] = newState;
  512. // if we hit undo state capacity, shift the whole stack
  513. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  514. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  515. undoStates[i] = undoStates[i+1];
  516. }
  517. } else {
  518. undoStateIndex++;
  519. }
  520. maxRedoIndex = undoStateIndex;
  521. }
  522. void UITextInput::setUndoState(UITextInputUndoState state) {
  523. clearSelection();
  524. setText(state.content);
  525. currentLine = lines[state.lineOffset];
  526. caretPosition = state.caretPosition;
  527. lineOffset = state.lineOffset;
  528. updateCaretPosition();
  529. if(state.hasSelection) {
  530. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  531. }
  532. }
  533. void UITextInput::Undo() {
  534. if(undoStateIndex > 0) {
  535. undoStateIndex--;
  536. setUndoState(undoStates[undoStateIndex]);
  537. }
  538. }
  539. void UITextInput::Redo() {
  540. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  541. undoStateIndex++;
  542. setUndoState(undoStates[undoStateIndex]);
  543. }
  544. }
  545. void UITextInput::Cut() {
  546. saveUndoState();
  547. Copy();
  548. if(hasSelection) {
  549. deleteSelection();
  550. }
  551. }
  552. void UITextInput::Copy() {
  553. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  554. }
  555. void UITextInput::Paste() {
  556. saveUndoState();
  557. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  558. }
  559. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  560. }
  561. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  562. if(!hasFocus)
  563. return;
  564. // Logger::log("UCHAR: %d\n", charCode);
  565. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  566. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  567. selectAll();
  568. return;
  569. }
  570. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  571. Copy();
  572. return;
  573. }
  574. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  575. Cut();
  576. return;
  577. }
  578. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  579. Undo();
  580. return;
  581. }
  582. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  583. Redo();
  584. return;
  585. }
  586. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  587. Redo();
  588. return;
  589. }
  590. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  591. Paste();
  592. return;
  593. }
  594. if(key == KEY_LEFT) {
  595. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  596. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  597. if(hasSelection) {
  598. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  599. } else {
  600. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  601. }
  602. } else {
  603. caretPosition = 0;
  604. clearSelection();
  605. updateCaretPosition();
  606. }
  607. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  608. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  609. if(hasSelection) {
  610. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  611. } else {
  612. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  613. }
  614. } else {
  615. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  616. clearSelection();
  617. updateCaretPosition();
  618. }
  619. } else {
  620. if(caretPosition > 0) {
  621. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  622. if(hasSelection) {
  623. if(selectionCaretPosition > 0)
  624. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  625. } else {
  626. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  627. }
  628. } else {
  629. caretPosition--;
  630. clearSelection();
  631. updateCaretPosition();
  632. }
  633. }
  634. }
  635. return;
  636. }
  637. if(key == KEY_RIGHT) {
  638. if(caretPosition < currentLine->getText().length()) {
  639. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  640. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  641. if(hasSelection) {
  642. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  643. } else {
  644. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  645. }
  646. } else {
  647. caretPosition = currentLine->getText().length();
  648. clearSelection();
  649. }
  650. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  651. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  652. if(hasSelection) {
  653. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  654. } else {
  655. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  656. }
  657. } else {
  658. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  659. clearSelection();
  660. }
  661. } else {
  662. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  663. if(hasSelection) {
  664. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  665. } else {
  666. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  667. }
  668. } else {
  669. caretPosition++;
  670. clearSelection();
  671. }
  672. }
  673. updateCaretPosition();
  674. }
  675. return;
  676. }
  677. if(key == KEY_PAGEUP) {
  678. if(multiLine) {
  679. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  680. }
  681. return;
  682. }
  683. if(key == KEY_PAGEDOWN) {
  684. if(multiLine) {
  685. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  686. }
  687. return;
  688. }
  689. if(key == KEY_UP) {
  690. if(multiLine) {
  691. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  692. if(hasSelection) {
  693. if(selectionLine > 0)
  694. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  695. } else {
  696. if(this->lineOffset > 0)
  697. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  698. }
  699. } else {
  700. clearSelection();
  701. if(lineOffset > 0) {
  702. lineOffset--;
  703. selectLineFromOffset();
  704. updateCaretPosition();
  705. }
  706. }
  707. }
  708. blinkerRect->visible = true;
  709. return;
  710. }
  711. if(key == KEY_DOWN) {
  712. if(multiLine) {
  713. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  714. if(hasSelection) {
  715. if(selectionLine < lines.size()-1)
  716. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  717. } else {
  718. if(this->lineOffset < lines.size()-1)
  719. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  720. }
  721. } else {
  722. clearSelection();
  723. if(lineOffset < lines.size()-1) {
  724. lineOffset++;
  725. selectLineFromOffset();
  726. updateCaretPosition();
  727. }
  728. }
  729. }
  730. blinkerRect->visible = true;
  731. return;
  732. }
  733. if(key == KEY_RETURN) {
  734. if(multiLine) {
  735. saveUndoState();
  736. if(hasSelection) {
  737. deleteSelection();
  738. }
  739. insertLine(true);
  740. updateCaretPosition();
  741. } else {
  742. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  743. }
  744. return;
  745. }
  746. String ctext = currentLine->getText();
  747. // if(1) {
  748. if((charCode > 31 && charCode < 127) || charCode > 127) {
  749. if(!isNumberOnly || (isNumberOnly && (charCode > 47 && charCode < 58))) {
  750. saveUndoState();
  751. if(hasSelection)
  752. deleteSelection();
  753. ctext = currentLine->getText();
  754. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  755. ctext = ctext.substr(0,caretPosition);
  756. ctext += charCode + text2;
  757. caretPosition++;
  758. }
  759. }
  760. if(key == KEY_TAB && multiLine) {
  761. saveUndoState();
  762. if(hasSelection)
  763. deleteSelection();
  764. ctext = currentLine->getText();
  765. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  766. ctext = ctext.substr(0,caretPosition);
  767. ctext += (wchar_t)'\t' + text2;
  768. caretPosition++;
  769. }
  770. if(key == KEY_BACKSPACE) {
  771. if(hasSelection) {
  772. saveUndoState();
  773. deleteSelection();
  774. return;
  775. } else {
  776. ctext = currentLine->getText();
  777. if(caretPosition > 0) {
  778. saveUndoState();
  779. if(ctext.length() > 0) {
  780. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  781. ctext = ctext.substr(0,caretPosition-1);
  782. ctext += text2;
  783. caretPosition--;
  784. }
  785. } else {
  786. if(lineOffset > 0) {
  787. saveUndoState();
  788. ScreenLabel *lineToRemove = currentLine;
  789. lineOffset--;
  790. selectLineFromOffset();
  791. caretPosition = currentLine->getText().length();
  792. updateCaretPosition();
  793. currentLine->setText(currentLine->getText() + ctext);
  794. removeLine(lineToRemove);
  795. return;
  796. }
  797. }
  798. }
  799. }
  800. currentLine->setText(ctext);
  801. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  802. updateCaretPosition();
  803. }
  804. void UITextInput::Update() {
  805. if(hasSelection) {
  806. blinkerRect->visible = false;
  807. }
  808. blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y);
  809. if(hasFocus) {
  810. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  811. } else {
  812. blinkerRect->visible = false;
  813. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  814. }
  815. for(int i=0; i < linesToDelete.size(); i++) {
  816. delete linesToDelete[i];
  817. }
  818. linesToDelete.clear();
  819. }
  820. UITextInput::~UITextInput() {
  821. }
  822. void UITextInput::handleEvent(Event *event) {
  823. if(event->getDispatcher() == inputRect) {
  824. switch(event->getEventCode()) {
  825. case InputEvent::EVENT_MOUSEDOWN:
  826. if(parentEntity) {
  827. ((ScreenEntity*)parentEntity)->focusChild(this);
  828. } else {
  829. hasFocus = true;
  830. }
  831. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  832. draggingSelection = true;
  833. break;
  834. case InputEvent::EVENT_MOUSEUP:
  835. draggingSelection = false;
  836. break;
  837. case InputEvent::EVENT_DOUBLECLICK:
  838. selectWordAtCaret();
  839. break;
  840. case InputEvent::EVENT_MOUSEMOVE:
  841. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  842. if(draggingSelection) {
  843. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  844. }
  845. break;
  846. case InputEvent::EVENT_MOUSEOVER:
  847. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  848. break;
  849. case InputEvent::EVENT_MOUSEOUT:
  850. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  851. break;
  852. }
  853. }
  854. if(event->getDispatcher() == blinkTimer) {
  855. if(hasSelection || draggingSelection) {
  856. blinkerRect->visible = false;
  857. } else {
  858. if(hasFocus)
  859. blinkerRect->visible = !blinkerRect->visible;
  860. else
  861. blinkerRect->visible = false;
  862. }
  863. }
  864. }