PolyUITextInput.cpp 25 KB

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