PolyUITextInput.cpp 25 KB

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