PolyUITextInput.cpp 23 KB

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