PolyUITextInput.cpp 25 KB

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