PolyUITextInput.cpp 27 KB

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