PolyUITextInput.cpp 25 KB

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