PolyUITextInput.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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::showLine(unsigned int lineNumber, bool top) {
  553. }
  554. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  555. if(!hasFocus)
  556. return;
  557. // Logger::log("UCHAR: %d\n", charCode);
  558. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  559. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  560. selectAll();
  561. return;
  562. }
  563. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  564. Copy();
  565. return;
  566. }
  567. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  568. Cut();
  569. return;
  570. }
  571. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  572. Undo();
  573. return;
  574. }
  575. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  576. Redo();
  577. return;
  578. }
  579. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  580. Redo();
  581. return;
  582. }
  583. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  584. Paste();
  585. return;
  586. }
  587. if(key == KEY_LEFT) {
  588. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  589. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  590. if(hasSelection) {
  591. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  592. } else {
  593. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  594. }
  595. } else {
  596. caretPosition = 0;
  597. clearSelection();
  598. updateCaretPosition();
  599. }
  600. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  601. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  602. if(hasSelection) {
  603. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  604. } else {
  605. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  606. }
  607. } else {
  608. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  609. clearSelection();
  610. updateCaretPosition();
  611. }
  612. } else {
  613. if(caretPosition > 0) {
  614. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  615. if(hasSelection) {
  616. if(selectionCaretPosition > 0)
  617. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  618. } else {
  619. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  620. }
  621. } else {
  622. caretPosition--;
  623. clearSelection();
  624. updateCaretPosition();
  625. }
  626. }
  627. }
  628. return;
  629. }
  630. if(key == KEY_RIGHT) {
  631. if(caretPosition < currentLine->getText().length()) {
  632. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  633. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  634. if(hasSelection) {
  635. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  636. } else {
  637. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  638. }
  639. } else {
  640. caretPosition = currentLine->getText().length();
  641. clearSelection();
  642. }
  643. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  644. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  645. if(hasSelection) {
  646. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  647. } else {
  648. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  649. }
  650. } else {
  651. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  652. clearSelection();
  653. }
  654. } else {
  655. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  656. if(hasSelection) {
  657. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  658. } else {
  659. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  660. }
  661. } else {
  662. caretPosition++;
  663. clearSelection();
  664. }
  665. }
  666. updateCaretPosition();
  667. }
  668. return;
  669. }
  670. if(key == KEY_UP) {
  671. if(multiLine) {
  672. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  673. if(hasSelection) {
  674. if(selectionLine > 0)
  675. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  676. } else {
  677. if(this->lineOffset > 0)
  678. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  679. }
  680. } else {
  681. clearSelection();
  682. if(lineOffset > 0) {
  683. lineOffset--;
  684. selectLineFromOffset();
  685. updateCaretPosition();
  686. }
  687. }
  688. }
  689. blinkerRect->visible = true;
  690. return;
  691. }
  692. if(key == KEY_DOWN) {
  693. if(multiLine) {
  694. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  695. if(hasSelection) {
  696. if(selectionLine < lines.size()-1)
  697. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  698. } else {
  699. if(this->lineOffset < lines.size()-1)
  700. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  701. }
  702. } else {
  703. clearSelection();
  704. if(lineOffset < lines.size()-1) {
  705. lineOffset++;
  706. selectLineFromOffset();
  707. updateCaretPosition();
  708. }
  709. }
  710. }
  711. blinkerRect->visible = true;
  712. return;
  713. }
  714. if(key == KEY_RETURN) {
  715. if(multiLine) {
  716. saveUndoState();
  717. if(hasSelection) {
  718. deleteSelection();
  719. }
  720. insertLine(true);
  721. updateCaretPosition();
  722. } else {
  723. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  724. }
  725. return;
  726. }
  727. String ctext = currentLine->getText();
  728. // if(1) {
  729. if((charCode > 31 && charCode < 127) || charCode > 127) {
  730. if(!isNumberOnly || (isNumberOnly && (charCode > 47 && charCode < 58))) {
  731. saveUndoState();
  732. if(hasSelection)
  733. deleteSelection();
  734. ctext = currentLine->getText();
  735. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  736. ctext = ctext.substr(0,caretPosition);
  737. ctext += charCode + text2;
  738. caretPosition++;
  739. }
  740. }
  741. if(key == KEY_TAB && multiLine) {
  742. saveUndoState();
  743. if(hasSelection)
  744. deleteSelection();
  745. ctext = currentLine->getText();
  746. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  747. ctext = ctext.substr(0,caretPosition);
  748. ctext += (wchar_t)'\t' + text2;
  749. caretPosition++;
  750. }
  751. if(key == KEY_BACKSPACE) {
  752. if(hasSelection) {
  753. saveUndoState();
  754. deleteSelection();
  755. return;
  756. } else {
  757. ctext = currentLine->getText();
  758. if(caretPosition > 0) {
  759. saveUndoState();
  760. if(ctext.length() > 0) {
  761. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  762. ctext = ctext.substr(0,caretPosition-1);
  763. ctext += text2;
  764. caretPosition--;
  765. }
  766. } else {
  767. if(lineOffset > 0) {
  768. saveUndoState();
  769. ScreenLabel *lineToRemove = currentLine;
  770. lineOffset--;
  771. selectLineFromOffset();
  772. caretPosition = currentLine->getText().length();
  773. updateCaretPosition();
  774. currentLine->setText(currentLine->getText() + ctext);
  775. removeLine(lineToRemove);
  776. return;
  777. }
  778. }
  779. }
  780. }
  781. currentLine->setText(ctext);
  782. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  783. updateCaretPosition();
  784. }
  785. void UITextInput::Update() {
  786. if(hasSelection) {
  787. blinkerRect->visible = false;
  788. }
  789. blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y);
  790. if(hasFocus) {
  791. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  792. } else {
  793. blinkerRect->visible = false;
  794. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  795. }
  796. for(int i=0; i < linesToDelete.size(); i++) {
  797. delete linesToDelete[i];
  798. }
  799. linesToDelete.clear();
  800. }
  801. UITextInput::~UITextInput() {
  802. }
  803. void UITextInput::handleEvent(Event *event) {
  804. if(event->getDispatcher() == inputRect) {
  805. switch(event->getEventCode()) {
  806. case InputEvent::EVENT_MOUSEDOWN:
  807. if(parentEntity) {
  808. ((ScreenEntity*)parentEntity)->focusChild(this);
  809. } else {
  810. hasFocus = true;
  811. }
  812. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  813. draggingSelection = true;
  814. break;
  815. case InputEvent::EVENT_MOUSEUP:
  816. draggingSelection = false;
  817. break;
  818. case InputEvent::EVENT_DOUBLECLICK:
  819. selectWordAtCaret();
  820. break;
  821. case InputEvent::EVENT_MOUSEMOVE:
  822. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  823. if(draggingSelection) {
  824. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  825. }
  826. break;
  827. case InputEvent::EVENT_MOUSEOVER:
  828. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  829. break;
  830. case InputEvent::EVENT_MOUSEOUT:
  831. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  832. break;
  833. }
  834. }
  835. if(event->getDispatcher() == blinkTimer) {
  836. if(hasSelection || draggingSelection) {
  837. blinkerRect->visible = false;
  838. } else {
  839. if(hasFocus)
  840. blinkerRect->visible = !blinkerRect->visible;
  841. else
  842. blinkerRect->visible = false;
  843. }
  844. }
  845. }