PolyUITextInput.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  474. if(!hasFocus)
  475. return;
  476. // Logger::log("UCHAR: %d\n", charCode);
  477. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  478. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  479. selectAll();
  480. return;
  481. }
  482. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  483. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  484. return;
  485. }
  486. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  487. return;
  488. }
  489. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  490. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  491. return;
  492. }
  493. if(key == KEY_LEFT) {
  494. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  495. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  496. if(hasSelection) {
  497. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  498. } else {
  499. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  500. }
  501. } else {
  502. caretPosition = 0;
  503. clearSelection();
  504. updateCaretPosition();
  505. }
  506. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  507. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  508. if(hasSelection) {
  509. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  510. } else {
  511. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  512. }
  513. } else {
  514. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  515. clearSelection();
  516. updateCaretPosition();
  517. }
  518. } else {
  519. if(caretPosition > 0) {
  520. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  521. if(hasSelection) {
  522. if(selectionCaretPosition > 0)
  523. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  524. } else {
  525. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  526. }
  527. } else {
  528. caretPosition--;
  529. clearSelection();
  530. updateCaretPosition();
  531. }
  532. }
  533. }
  534. return;
  535. }
  536. if(key == KEY_RIGHT) {
  537. if(caretPosition < currentLine->getText().length()) {
  538. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  539. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  540. if(hasSelection) {
  541. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
  542. } else {
  543. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
  544. }
  545. } else {
  546. caretPosition = currentLine->getText().length();
  547. clearSelection();
  548. }
  549. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  550. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  551. if(hasSelection) {
  552. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  553. } else {
  554. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  555. }
  556. } else {
  557. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  558. clearSelection();
  559. }
  560. } else {
  561. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  562. if(hasSelection) {
  563. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  564. } else {
  565. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  566. }
  567. } else {
  568. caretPosition++;
  569. clearSelection();
  570. }
  571. }
  572. updateCaretPosition();
  573. }
  574. return;
  575. }
  576. if(key == KEY_UP) {
  577. if(multiLine) {
  578. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  579. if(hasSelection) {
  580. if(selectionLine > 0)
  581. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  582. } else {
  583. if(this->lineOffset > 0)
  584. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  585. }
  586. } else {
  587. clearSelection();
  588. if(lineOffset > 0) {
  589. lineOffset--;
  590. selectLineFromOffset();
  591. updateCaretPosition();
  592. }
  593. }
  594. }
  595. blinkerRect->visible = true;
  596. return;
  597. }
  598. if(key == KEY_DOWN) {
  599. if(multiLine) {
  600. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  601. if(hasSelection) {
  602. if(selectionLine < lines.size()-1)
  603. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  604. } else {
  605. if(this->lineOffset < lines.size()-1)
  606. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  607. }
  608. } else {
  609. clearSelection();
  610. if(lineOffset < lines.size()-1) {
  611. lineOffset++;
  612. selectLineFromOffset();
  613. updateCaretPosition();
  614. }
  615. }
  616. }
  617. blinkerRect->visible = true;
  618. return;
  619. }
  620. if(key == KEY_RETURN) {
  621. if(multiLine) {
  622. if(hasSelection) {
  623. deleteSelection();
  624. }
  625. insertLine(true);
  626. updateCaretPosition();
  627. } else {
  628. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  629. }
  630. return;
  631. }
  632. String ctext = currentLine->getText();
  633. // if(1) {
  634. if((charCode > 31 && charCode < 127) || charCode > 127) {
  635. if(!isNumberOnly || (isNumberOnly && (charCode > 47 && charCode < 58))) {
  636. if(hasSelection)
  637. deleteSelection();
  638. ctext = currentLine->getText();
  639. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  640. ctext = ctext.substr(0,caretPosition);
  641. ctext += charCode + text2;
  642. caretPosition++;
  643. }
  644. }
  645. if(key == KEY_TAB && multiLine) {
  646. if(hasSelection)
  647. deleteSelection();
  648. ctext = currentLine->getText();
  649. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  650. ctext = ctext.substr(0,caretPosition);
  651. ctext += (wchar_t)'\t' + text2;
  652. caretPosition++;
  653. }
  654. if(key == KEY_BACKSPACE) {
  655. if(hasSelection) {
  656. deleteSelection();
  657. return;
  658. } else {
  659. ctext = currentLine->getText();
  660. if(caretPosition > 0) {
  661. if(ctext.length() > 0) {
  662. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  663. ctext = ctext.substr(0,caretPosition-1);
  664. ctext += text2;
  665. caretPosition--;
  666. }
  667. } else {
  668. if(lineOffset > 0) {
  669. ScreenLabel *lineToRemove = currentLine;
  670. lineOffset--;
  671. selectLineFromOffset();
  672. caretPosition = currentLine->getText().length();
  673. updateCaretPosition();
  674. currentLine->setText(currentLine->getText() + ctext);
  675. removeLine(lineToRemove);
  676. return;
  677. }
  678. }
  679. }
  680. }
  681. currentLine->setText(ctext);
  682. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  683. updateCaretPosition();
  684. }
  685. void UITextInput::Update() {
  686. if(hasSelection) {
  687. blinkerRect->visible = false;
  688. }
  689. blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y);
  690. if(hasFocus) {
  691. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  692. } else {
  693. blinkerRect->visible = false;
  694. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  695. }
  696. }
  697. UITextInput::~UITextInput() {
  698. }
  699. void UITextInput::handleEvent(Event *event) {
  700. if(event->getDispatcher() == inputRect) {
  701. switch(event->getEventCode()) {
  702. case InputEvent::EVENT_MOUSEDOWN:
  703. if(parentEntity) {
  704. ((ScreenEntity*)parentEntity)->focusChild(this);
  705. } else {
  706. hasFocus = true;
  707. }
  708. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y);
  709. draggingSelection = true;
  710. break;
  711. case InputEvent::EVENT_MOUSEUP:
  712. draggingSelection = false;
  713. break;
  714. case InputEvent::EVENT_DOUBLECLICK:
  715. selectWordAtCaret();
  716. break;
  717. case InputEvent::EVENT_MOUSEMOVE:
  718. if(draggingSelection) {
  719. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y);
  720. }
  721. break;
  722. case InputEvent::EVENT_MOUSEOVER:
  723. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  724. break;
  725. case InputEvent::EVENT_MOUSEOUT:
  726. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  727. break;
  728. }
  729. }
  730. if(event->getDispatcher() == blinkTimer) {
  731. if(hasSelection || draggingSelection) {
  732. blinkerRect->visible = false;
  733. } else {
  734. if(hasFocus)
  735. blinkerRect->visible = !blinkerRect->visible;
  736. else
  737. blinkerRect->visible = false;
  738. }
  739. }
  740. }