PolyUITextInput.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. selectAll();
  272. insertText(text);
  273. clearSelection();
  274. }
  275. // this->text = text;
  276. // currentLine->setText(text);
  277. }
  278. void UITextInput::onLoseFocus() {
  279. blinkerRect->visible = false;
  280. }
  281. String UITextInput::getText() {
  282. if(!multiLine) {
  283. return currentLine->getText();
  284. } else {
  285. String totalText = L"";
  286. for(int i=0; i < lines.size(); i++) {
  287. totalText += lines[i]->getText();
  288. if(i < lines.size()-1)
  289. totalText += L"\n";
  290. }
  291. return totalText;
  292. }
  293. }
  294. void UITextInput::updateCaretPosition() {
  295. caretImagePosition = padding;
  296. if(caretPosition == 0) {
  297. caretImagePosition = padding;
  298. } else if(caretPosition > currentLine->getText().length()) {
  299. caretPosition = currentLine->getText().length();
  300. String caretSubString = currentLine->getText().substr(0,caretPosition);
  301. caretImagePosition = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), caretSubString, fontSize);
  302. caretImagePosition = caretImagePosition - 4.0f + padding;
  303. } else {
  304. String caretSubString = currentLine->getText().substr(0,caretPosition);
  305. caretImagePosition = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), caretSubString, fontSize);
  306. caretImagePosition = caretImagePosition - 4.0f + padding;
  307. }
  308. blinkerRect->visible = true;
  309. blinkTimer->Reset();
  310. if(doSelectToCaret) {
  311. doSelectToCaret = false;
  312. }
  313. }
  314. void UITextInput::selectLineFromOffset() {
  315. for(int i=0; i < lines.size(); i++) {
  316. if(i == lineOffset) {
  317. currentLine = lines[i];
  318. return;
  319. }
  320. }
  321. }
  322. void UITextInput::dragSelectionTo(Number x, Number y) {
  323. x -= padding;
  324. y -= padding;
  325. int lineOffset = y / (lineHeight+lineSpacing);
  326. if(lineOffset > lines.size()-1)
  327. lineOffset = lines.size()-1;
  328. ScreenLabel *selectToLine = lines[lineOffset];
  329. int len = selectToLine->getText().length();
  330. Number slen;
  331. int caretPosition = selectToLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), selectToLine->getText().substr(0,len), fontSize);
  332. for(int i=0; i < len; i++) {
  333. slen = selectToLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), selectToLine->getText().substr(0,i), fontSize);
  334. if(slen > x) {
  335. caretPosition = i;
  336. break;
  337. }
  338. }
  339. if(x > slen)
  340. caretPosition = len;
  341. if(caretPosition < 0)
  342. caretPosition = 0;
  343. setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
  344. }
  345. int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
  346. for(int i=caretPosition; i > 0; i--) {
  347. String bit = lines[caretLine]->getText().substr(i,1);
  348. char chr = ((char*)bit.c_str())[0];
  349. if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i < caretPosition-1) {
  350. return i+1;
  351. }
  352. }
  353. return 0;
  354. }
  355. int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
  356. int len = lines[caretLine]->getText().length();
  357. for(int i=caretPosition; i < len; i++) {
  358. String bit = lines[caretLine]->getText().substr(i,1);
  359. char chr = ((char*)bit.c_str())[0];
  360. if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i > caretPosition) {
  361. return i;
  362. }
  363. }
  364. return lines[caretLine]->getText().length();
  365. }
  366. void UITextInput::selectWordAtCaret() {
  367. int selectStart = 0;
  368. int len = currentLine->getText().length();
  369. int selectEnd = len;
  370. for(int i=this->caretPosition; i > 0; i--) {
  371. String bit = currentLine->getText().substr(i,1);
  372. wchar_t chr = ((wchar_t*)bit.c_str())[0];
  373. if( (chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) {
  374. selectStart = i+1;
  375. break;
  376. }
  377. }
  378. for(int i=this->caretPosition; i < len; i++) {
  379. String bit = currentLine->getText().substr(i,1);
  380. wchar_t chr = ((wchar_t*)bit.c_str())[0];
  381. if( (chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) {
  382. selectEnd = i;
  383. break;
  384. }
  385. }
  386. setSelection(this->lineOffset, this->lineOffset, selectStart, selectEnd);
  387. }
  388. void UITextInput::setCaretToMouse(Number x, Number y) {
  389. clearSelection();
  390. x -= padding;
  391. y -= padding;
  392. //if(lines.size() > 1) {
  393. lineOffset = y / (lineHeight+lineSpacing);
  394. if(lineOffset > lines.size()-1)
  395. lineOffset = lines.size()-1;
  396. selectLineFromOffset();
  397. //}
  398. int len = currentLine->getText().length();
  399. Number slen;
  400. for(int i=0; i < len; i++) {
  401. slen = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), currentLine->getText().substr(0,i), fontSize);
  402. if(slen > x) {
  403. caretPosition = i;
  404. break;
  405. }
  406. }
  407. if(x > slen)
  408. caretPosition = len;
  409. updateCaretPosition();
  410. }
  411. void UITextInput::removeLine(ScreenLabel *line) {
  412. for(int i=0;i<lines.size();i++) {
  413. if(lines[i] == line) {
  414. lines.erase(lines.begin()+i);
  415. }
  416. }
  417. removeChild(line);
  418. delete line;
  419. restructLines();
  420. }
  421. void UITextInput::selectAll() {
  422. setSelection(0, lines.size()-1, 0, lines[lines.size()-1]->getText().length());
  423. }
  424. void UITextInput::insertText(String text) {
  425. vector<String> strings = text.split("\n");
  426. int numLines = lines.size();
  427. for(int i=0; i < strings.size(); i++) {
  428. if(i < numLines) {
  429. lines[i]->setText(strings[i]);
  430. } else {
  431. numLines++;
  432. ScreenLabel *newLine = new ScreenLabel(L"", fontSize, fontName, Label::ANTIALIAS_FULL);
  433. newLine->setColor(0,0,0,1);
  434. addChild(newLine);
  435. lines.push_back(newLine);
  436. newLine->setText(strings[i]);
  437. }
  438. }
  439. restructLines();
  440. }
  441. String UITextInput::getSelectionText() {
  442. String totalText = L"";
  443. if(selectionTop == selectionBottom) {
  444. totalText = lines[selectionTop]->getText().substr(selectionL, selectionR-selectionL);
  445. return totalText;
  446. } else {
  447. totalText += lines[selectionTop]->getText().substr(selectionL, lines[selectionTop]->getText().length()-selectionL);
  448. totalText += L"\n";
  449. }
  450. if(selectionBottom > selectionTop+1) {
  451. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  452. totalText += lines[i]->getText();
  453. totalText += L"\n";
  454. }
  455. }
  456. totalText += lines[selectionBottom]->getText().substr(0, selectionL);
  457. return totalText;
  458. }
  459. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  460. if(!hasFocus)
  461. return;
  462. // Logger::log("UCHAR: %d\n", charCode);
  463. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  464. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  465. selectAll();
  466. return;
  467. }
  468. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  469. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  470. return;
  471. }
  472. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  473. return;
  474. }
  475. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  476. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  477. return;
  478. }
  479. if(key == KEY_LEFT) {
  480. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  481. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  482. if(hasSelection) {
  483. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  484. } else {
  485. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  486. }
  487. } else {
  488. caretPosition = 0;
  489. clearSelection();
  490. updateCaretPosition();
  491. }
  492. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  493. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  494. if(hasSelection) {
  495. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  496. } else {
  497. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  498. }
  499. } else {
  500. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  501. clearSelection();
  502. updateCaretPosition();
  503. }
  504. } else {
  505. if(caretPosition > 0) {
  506. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  507. if(hasSelection) {
  508. if(selectionCaretPosition > 0)
  509. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  510. } else {
  511. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  512. }
  513. } else {
  514. caretPosition--;
  515. clearSelection();
  516. updateCaretPosition();
  517. }
  518. }
  519. }
  520. return;
  521. }
  522. if(key == KEY_RIGHT) {
  523. if(caretPosition < currentLine->getText().length()) {
  524. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  525. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  526. if(hasSelection) {
  527. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  528. } else {
  529. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  530. }
  531. } else {
  532. caretPosition = currentLine->getText().length();
  533. clearSelection();
  534. }
  535. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  536. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  537. if(hasSelection) {
  538. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  539. } else {
  540. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  541. }
  542. } else {
  543. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  544. clearSelection();
  545. }
  546. } else {
  547. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  548. if(hasSelection) {
  549. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  550. } else {
  551. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  552. }
  553. } else {
  554. caretPosition++;
  555. clearSelection();
  556. }
  557. }
  558. updateCaretPosition();
  559. }
  560. return;
  561. }
  562. if(key == KEY_UP) {
  563. if(multiLine) {
  564. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  565. if(hasSelection) {
  566. if(selectionLine > 0)
  567. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  568. } else {
  569. if(this->lineOffset > 0)
  570. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  571. }
  572. } else {
  573. clearSelection();
  574. if(lineOffset > 0) {
  575. lineOffset--;
  576. selectLineFromOffset();
  577. updateCaretPosition();
  578. }
  579. }
  580. }
  581. blinkerRect->visible = true;
  582. return;
  583. }
  584. if(key == KEY_DOWN) {
  585. if(multiLine) {
  586. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  587. if(hasSelection) {
  588. if(selectionLine < lines.size()-1)
  589. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  590. } else {
  591. if(this->lineOffset < lines.size()-1)
  592. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  593. }
  594. } else {
  595. clearSelection();
  596. if(lineOffset < lines.size()-1) {
  597. lineOffset++;
  598. selectLineFromOffset();
  599. updateCaretPosition();
  600. }
  601. }
  602. }
  603. blinkerRect->visible = true;
  604. return;
  605. }
  606. if(key == KEY_RETURN) {
  607. if(multiLine) {
  608. if(hasSelection) {
  609. deleteSelection();
  610. }
  611. insertLine(true);
  612. updateCaretPosition();
  613. } else {
  614. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  615. }
  616. return;
  617. }
  618. String ctext = currentLine->getText();
  619. // if(1) {
  620. if((charCode > 31 && charCode < 127) || charCode > 127) {
  621. if(!isNumberOnly || (isNumberOnly && (charCode > 47 && charCode < 58))) {
  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 += charCode + text2;
  628. caretPosition++;
  629. }
  630. }
  631. if(key == KEY_TAB && multiLine) {
  632. if(hasSelection)
  633. deleteSelection();
  634. ctext = currentLine->getText();
  635. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  636. ctext = ctext.substr(0,caretPosition);
  637. ctext += (wchar_t)'\t' + text2;
  638. caretPosition++;
  639. }
  640. if(key == KEY_BACKSPACE) {
  641. if(hasSelection) {
  642. deleteSelection();
  643. return;
  644. } else {
  645. ctext = currentLine->getText();
  646. if(caretPosition > 0) {
  647. if(ctext.length() > 0) {
  648. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  649. ctext = ctext.substr(0,caretPosition-1);
  650. ctext += text2;
  651. caretPosition--;
  652. }
  653. } else {
  654. if(lineOffset > 0) {
  655. ScreenLabel *lineToRemove = currentLine;
  656. lineOffset--;
  657. selectLineFromOffset();
  658. caretPosition = currentLine->getText().length();
  659. updateCaretPosition();
  660. currentLine->setText(currentLine->getText() + ctext);
  661. removeLine(lineToRemove);
  662. return;
  663. }
  664. }
  665. }
  666. }
  667. currentLine->setText(ctext);
  668. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  669. updateCaretPosition();
  670. }
  671. void UITextInput::Update() {
  672. if(hasSelection) {
  673. blinkerRect->visible = false;
  674. }
  675. blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y);
  676. if(hasFocus) {
  677. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  678. } else {
  679. blinkerRect->visible = false;
  680. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  681. }
  682. }
  683. UITextInput::~UITextInput() {
  684. }
  685. void UITextInput::handleEvent(Event *event) {
  686. if(event->getDispatcher() == inputRect) {
  687. switch(event->getEventCode()) {
  688. case InputEvent::EVENT_MOUSEDOWN:
  689. if(parentEntity) {
  690. ((ScreenEntity*)parentEntity)->focusChild(this);
  691. } else {
  692. hasFocus = true;
  693. }
  694. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y);
  695. draggingSelection = true;
  696. break;
  697. case InputEvent::EVENT_MOUSEUP:
  698. draggingSelection = false;
  699. break;
  700. case InputEvent::EVENT_DOUBLECLICK:
  701. selectWordAtCaret();
  702. break;
  703. case InputEvent::EVENT_MOUSEMOVE:
  704. if(draggingSelection) {
  705. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y);
  706. }
  707. break;
  708. case InputEvent::EVENT_MOUSEOVER:
  709. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  710. break;
  711. case InputEvent::EVENT_MOUSEOUT:
  712. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  713. break;
  714. }
  715. }
  716. if(event->getDispatcher() == blinkTimer) {
  717. if(hasSelection || draggingSelection) {
  718. blinkerRect->visible = false;
  719. } else {
  720. if(hasFocus)
  721. blinkerRect->visible = !blinkerRect->visible;
  722. else
  723. blinkerRect->visible = false;
  724. }
  725. }
  726. }