PolyUITextInput.cpp 25 KB

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