PolyUITextInput.cpp 30 KB

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