PolyUITextInput.cpp 26 KB

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