PolyUITextInput.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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. processInputEvents = true;
  29. isNumberOnly = false;
  30. decoratorOffset = 0;
  31. lineOffset = -1;
  32. useStrongHinting = false;
  33. draggingSelection = false;
  34. hasSelection = false;
  35. doSelectToCaret = false;
  36. lineNumbersEnabled = false;
  37. caretPosition = 0;
  38. caretImagePosition = 0;
  39. settingText = false;
  40. needFullRedraw = false;
  41. numLines = 0;
  42. this->positionMode = ScreenEntity::POSITION_TOPLEFT;
  43. Config *conf = CoreServices::getInstance()->getConfig();
  44. if(multiLine)
  45. fontName = conf->getStringValue("Polycode", "uiTextInputFontNameMultiLine");
  46. else
  47. fontName = conf->getStringValue("Polycode", "uiTextInputFontName");
  48. if(multiLine)
  49. fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSizeMultiline");
  50. else
  51. fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSize");
  52. Number rectHeight = height;
  53. if(!multiLine) {
  54. rectHeight = fontSize+12;
  55. }
  56. linesContainer = new ScreenEntity();
  57. linesContainer->processInputEvents = true;
  58. lineSpacing = conf->getNumericValue("Polycode", "textEditLineSpacing");
  59. Number st = conf->getNumericValue("Polycode", "textBgSkinT");
  60. Number sr = conf->getNumericValue("Polycode", "textBgSkinR");
  61. Number sb = conf->getNumericValue("Polycode", "textBgSkinB");
  62. Number sl = conf->getNumericValue("Polycode", "textBgSkinL");
  63. padding = conf->getNumericValue("Polycode", "textBgSkinPadding");
  64. inputRect = new UIBox(conf->getStringValue("Polycode", "textBgSkin"),
  65. st,sr,sb,sl,
  66. width+(padding*2), height+(padding*2));
  67. addChild(inputRect);
  68. if(multiLine) {
  69. lineNumberBg = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  70. lineNumberBg->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  71. lineNumberBg->setColor(0.0, 0.0, 0.0, 0.3);
  72. addChild(lineNumberBg);
  73. lineNumberBg->visible = false;
  74. lineNumberAnchor = new ScreenEntity();
  75. linesContainer->addChild(lineNumberAnchor);
  76. } else {
  77. lineNumberBg = NULL;
  78. lineNumberAnchor = NULL;
  79. }
  80. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  81. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  82. inputRect->addEventListener(this, InputEvent::EVENT_DOUBLECLICK);
  83. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  84. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
  85. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  86. inputRect->processInputEvents = true;
  87. inputRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  88. selectorRectTop = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  89. selectorRectTop->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  90. selectorRectTop->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  91. selectorRectTop->visible = false;
  92. linesContainer->addChild(selectorRectTop);
  93. selectorRectMiddle = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  94. selectorRectMiddle->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  95. selectorRectMiddle->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  96. selectorRectMiddle->visible = false;
  97. linesContainer->addChild(selectorRectMiddle);
  98. selectorRectBottom = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  99. selectorRectBottom->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  100. selectorRectBottom->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  101. selectorRectBottom->visible = false;
  102. linesContainer->addChild(selectorRectBottom);
  103. blinkerRect = new ScreenShape(ScreenShape::SHAPE_RECT, 1, fontSize+2,0,0);
  104. blinkerRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  105. blinkerRect->setColor(0,0,0,1);
  106. linesContainer->addChild(blinkerRect);
  107. blinkerRect->visible = false;
  108. blinkerRect->setPosition(0,3);
  109. blinkTimer = new Timer(true, 500);
  110. blinkTimer->addEventListener(this, Timer::EVENT_TRIGGER);
  111. focusable = true;
  112. this->width = width;
  113. this->height = rectHeight;
  114. setHitbox(width, rectHeight);
  115. updateCaretPosition();
  116. scrollContainer = NULL;
  117. if(multiLine) {
  118. scrollContainer = new UIScrollContainer(linesContainer, false, true, 200, 200);
  119. scrollContainer->addEventListener(this, Event::CHANGE_EVENT);
  120. addChild(scrollContainer);
  121. } else {
  122. addChild(linesContainer);
  123. enableScissor = true;
  124. }
  125. undoStateIndex = 0;
  126. maxRedoIndex = 0;
  127. syntaxHighliter = NULL;
  128. textColor = Color(0.0,0.0,0.0,1.0);
  129. currentBufferLines = 0;
  130. neededBufferLines = 1;
  131. checkBufferLines();
  132. insertLine(true);
  133. }
  134. void UITextInput::checkBufferLines() {
  135. if(neededBufferLines < currentBufferLines)
  136. return;
  137. int aaMode = Label::ANTIALIAS_FULL;
  138. if(useStrongHinting) {
  139. aaMode = Label::ANTIALIAS_STRONG;
  140. }
  141. for(int i=0; i < neededBufferLines - currentBufferLines; i++) {
  142. if(multiLine) {
  143. ScreenLabel *newNumberLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
  144. newNumberLine->color = lineNumberColor;
  145. lineNumberAnchor->addChild(newNumberLine);
  146. numberLines.push_back(newNumberLine);
  147. if(!lineNumbersEnabled) {
  148. newNumberLine->visible = false;
  149. }
  150. }
  151. ScreenLabel *newLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
  152. newLine->color = textColor;
  153. lineHeight = newLine->getHeight();
  154. linesContainer->addChild(newLine);
  155. bufferLines.push_back(newLine);
  156. }
  157. currentBufferLines = neededBufferLines;
  158. }
  159. void UITextInput::setNumberOnly(bool val) {
  160. isNumberOnly = val;
  161. }
  162. void UITextInput::clearSelection() {
  163. hasSelection = false;
  164. selectorRectTop->visible = false;
  165. selectorRectMiddle->visible = false;
  166. selectorRectBottom->visible = false;
  167. }
  168. void UITextInput::setSelection(int lineStart, int lineEnd, int colStart, int colEnd) {
  169. if(lineStart == lineEnd && colStart == colEnd) {
  170. clearSelection();
  171. return;
  172. }
  173. if(lineStart == lineOffset) {
  174. selectionLine = lineEnd;
  175. } else {
  176. selectionLine = lineStart;
  177. }
  178. if(colStart == caretPosition) {
  179. selectionCaretPosition = colEnd;
  180. } else {
  181. selectionCaretPosition = colStart;
  182. }
  183. // printf("SET lineStart:%d lineEnd:%d colStart:%d colEnd:%d\n", lineStart, lineEnd, colStart, colEnd);
  184. if(lineStart > lineEnd) {
  185. int tmp = lineStart;
  186. lineStart = lineEnd;
  187. lineEnd = tmp;
  188. tmp = colStart;
  189. colStart = colEnd;
  190. colEnd = tmp;
  191. }
  192. if(colStart > colEnd && lineStart == lineEnd) {
  193. int tmp = colStart;
  194. colStart = colEnd;
  195. colEnd = tmp;
  196. }
  197. clearSelection();
  198. if(lineStart > lines.size()-1)
  199. return;
  200. String topLine = lines[lineStart];
  201. if(colStart+1 > topLine.length()) {
  202. colStart = topLine.length();
  203. }
  204. Number fColEnd = colEnd;
  205. if(colEnd > topLine.length() || lineStart != lineEnd)
  206. fColEnd = topLine.length();
  207. Number topSize, topHeight, topX;
  208. selectorRectTop->visible = true;
  209. topSize = bufferLines[0]->getLabel()->getTextWidthForString(topLine.substr(colStart,fColEnd-colStart)) ;
  210. topHeight = lineHeight+lineSpacing;
  211. if(colStart >= 0) {
  212. topX = bufferLines[0]->getLabel()->getTextWidthForString(topLine.substr(0,colStart)) + 2;
  213. } else {
  214. topX = 0;
  215. }
  216. selectorRectTop->setScale(topSize, topHeight);
  217. selectorRectTop->setPosition(decoratorOffset + topX + padding + (topSize/2.0), padding + (lineStart * (lineHeight+lineSpacing)) + (topHeight/2.0));
  218. if(lineEnd > lineStart && lineEnd < lines.size()) {
  219. String bottomLine = lines[lineEnd];
  220. selectorRectBottom->visible = true;
  221. Number bottomSize = bufferLines[0]->getLabel()->getTextWidthForString(bottomLine.substr(0,colEnd)) ;
  222. if(bottomSize < 0)
  223. bottomSize = this->width-padding;
  224. Number bottomHeight = lineHeight+lineSpacing;
  225. selectorRectBottom->setScale(bottomSize, bottomHeight);
  226. selectorRectBottom->setPosition(decoratorOffset + padding + (bottomSize/2.0), padding + (lineEnd * (lineHeight+lineSpacing)) + (bottomHeight/2.0));
  227. if(lineEnd != lineStart+1) {
  228. // need filler
  229. selectorRectMiddle->visible = true;
  230. Number midSize = this->width-padding;
  231. Number midHeight = 0;
  232. for(int i=lineStart+1; i < lineEnd;i++) {
  233. midHeight += lineHeight+lineSpacing;
  234. }
  235. selectorRectMiddle->setScale(midSize, midHeight);
  236. selectorRectMiddle->setPosition(decoratorOffset + padding + (midSize/2.0), padding + ((lineStart+1) * (lineHeight+lineSpacing)) + (midHeight/2.0));
  237. }
  238. }
  239. hasSelection = true;
  240. selectionTop = lineStart;
  241. selectionBottom = lineEnd;
  242. selectionL = colStart;
  243. selectionR = colEnd;
  244. }
  245. void UITextInput::deleteSelection() {
  246. if(selectionTop == selectionBottom) {
  247. String ctext = lines[selectionTop];
  248. String newText = ctext.substr(0, selectionL);
  249. int rside = selectionR;
  250. if(rside > ctext.length()-1)
  251. rside = ctext.length() - 1;
  252. newText += ctext.substr(rside,ctext.length() - selectionR);
  253. lines[selectionTop] = newText;
  254. } else {
  255. String ctext = lines[selectionTop];
  256. String newText = ctext.substr(0, selectionL);
  257. lines[selectionTop] = newText;
  258. // if whole lines to remove, do it
  259. vector<int> linesToRemove;
  260. if(selectionBottom > selectionTop + 1) {
  261. for(int i=selectionTop+1; i < selectionBottom; i++) {
  262. linesToRemove.push_back(i);
  263. }
  264. for(int i=0; i < linesToRemove.size(); i++) {
  265. removeLine(linesToRemove[i]);
  266. }
  267. }
  268. ctext = lines[selectionBottom];
  269. int rside = selectionR;
  270. if(rside > ctext.length()-1)
  271. rside = ctext.length() - 1;
  272. newText = ctext.substr(rside,ctext.length() - selectionR);
  273. lineOffset = selectionTop;
  274. selectLineFromOffset();
  275. caretPosition = lines[lineOffset].length();
  276. updateCaretPosition();
  277. lines[lineOffset] = lines[lineOffset] + newText;
  278. removeLine(selectionBottom);
  279. }
  280. clearSelection();
  281. caretPosition = selectionL;
  282. updateCaretPosition();
  283. changedText();
  284. }
  285. void UITextInput::changedText() {
  286. if(settingText)
  287. return;
  288. if(syntaxHighliter && multiLine) {
  289. String totalText = L"";
  290. for(int i=0; i < lines.size(); i++) {
  291. totalText += lines[i];
  292. if(i < lines.size()-1)
  293. totalText += L"\n";
  294. }
  295. std::vector<SyntaxHighlightToken> tokens = syntaxHighliter->parseText(totalText);
  296. // DO SYNTAX HIGHLIGHTING
  297. lineColors.clear();
  298. for(int i=0; i < lines.size(); i++) {
  299. lineColors.push_back(LineColorInfo());
  300. }
  301. int lineIndex = 0;
  302. int rangeStart = 0;
  303. int rangeEnd = 0;
  304. for(int i=0; i < tokens.size(); i++) {
  305. if(tokens[i].text == "\n") {
  306. lineIndex++;
  307. rangeStart = 0;
  308. rangeEnd = 0;
  309. } else {
  310. if(lineIndex < lines.size()) {
  311. int textLength = tokens[i].text.length();
  312. if(tokens[i].text.length() > 1) {
  313. rangeEnd = rangeStart + textLength-1;
  314. lineColors[lineIndex].colors.push_back(LineColorData(tokens[i].color, rangeStart, rangeEnd));
  315. rangeStart = rangeStart + textLength;
  316. } else {
  317. rangeEnd = rangeStart;
  318. lineColors[lineIndex].colors.push_back(LineColorData(tokens[i].color, rangeStart, rangeEnd));
  319. rangeStart++;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. /*
  326. unsigned int startLine = (-linesContainer->getPosition().y) / (lineHeight+lineSpacing);
  327. unsigned int endLine = startLine + ((int)((height / (lineHeight+lineSpacing)))) + 1;
  328. if(endLine > lines.size())
  329. endLine = lines.size();
  330. if(needFullRedraw) {
  331. startLine = 0;
  332. endLine = lines.size();
  333. needFullRedraw = false;
  334. }
  335. String totalText = L"";
  336. for(int i=startLine; i < endLine; i++) {
  337. totalText += lines[i]->getText();
  338. if(i < lines.size()-1)
  339. totalText += L"\n";
  340. }
  341. std::vector<SyntaxHighlightToken> tokens = syntaxHighliter->parseText(totalText);
  342. for(int i=startLine; i < endLine; i++) {
  343. lines[i]->getLabel()->clearColors();
  344. }
  345. int lineIndex = startLine;
  346. int rangeStart = 0;
  347. int rangeEnd = 0;
  348. for(int i=0; i < tokens.size(); i++) {
  349. if(tokens[i].text == "\n") {
  350. lineIndex++;
  351. rangeStart = 0;
  352. rangeEnd = 0;
  353. } else {
  354. if(lineIndex < lines.size()) {
  355. int textLength = tokens[i].text.length();
  356. if(tokens[i].text.length() > 1) {
  357. rangeEnd = rangeStart + textLength-1;
  358. lines[lineIndex]->getLabel()->setColorForRange(tokens[i].color, rangeStart, rangeEnd);
  359. rangeStart = rangeStart + textLength;
  360. } else {
  361. rangeEnd = rangeStart;
  362. lines[lineIndex]->getLabel()->setColorForRange(tokens[i].color, rangeStart, rangeEnd);
  363. rangeStart++;
  364. }
  365. }
  366. }
  367. }
  368. for(int i=startLine; i < endLine; i++) {
  369. lines[i]->setText(lines[i]->getText());
  370. lines[i]->setColor(1.0, 1.0, 1.0, 1.0);
  371. }
  372. }
  373. */
  374. readjustBuffer();
  375. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  376. }
  377. void UITextInput::setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighliter) {
  378. this->syntaxHighliter = syntaxHighliter;
  379. }
  380. void UITextInput::Resize(Number width, Number height) {
  381. inputRect->resizeBox(width, height);
  382. this->width = width;
  383. this->height = height;
  384. matrixDirty = true;
  385. setHitbox(width,height);
  386. if(multiLine) {
  387. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  388. neededBufferLines = (height / ( lineHeight+lineSpacing)) + 1;
  389. checkBufferLines();
  390. renumberLines();
  391. readjustBuffer();
  392. }
  393. if(multiLine && lineNumbersEnabled) {
  394. lineNumberBg->setShapeSize(decoratorOffset, height);
  395. }
  396. if(scrollContainer) {
  397. scrollContainer->Resize(width, height);
  398. }
  399. }
  400. int UITextInput::insertLine(bool after) {
  401. numLines++;
  402. if(after) {
  403. String newText = "";
  404. if(lines.size() > 0) {
  405. String ctext = lines[lineOffset];
  406. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  407. ctext = ctext.substr(0,caretPosition);
  408. lines[lineOffset] = ctext;
  409. newText = text2;
  410. caretPosition=0;
  411. }
  412. vector<String>::iterator it;
  413. it = lines.begin();
  414. for(int i=0; i < lineOffset+1; i++) {
  415. it++;
  416. }
  417. lineOffset = lineOffset + 1;
  418. lines.insert(it,newText);
  419. renumberLines();
  420. restructLines();
  421. } else {
  422. // do we even need that? I don't think so.
  423. }
  424. changedText();
  425. return 1;
  426. }
  427. void UITextInput::enableLineNumbers(bool val) {
  428. lineNumbersEnabled = val;
  429. lineNumberBg->visible = lineNumbersEnabled;
  430. restructLines();
  431. }
  432. void UITextInput::renumberLines() {
  433. if(!multiLine)
  434. return;
  435. int totalLineNumber = lines.size();
  436. if(currentBufferLines > lines.size()) {
  437. totalLineNumber = currentBufferLines;
  438. }
  439. decoratorOffset = 0;
  440. if(multiLine) {
  441. if(lineNumbersEnabled) {
  442. decoratorOffset = 15;
  443. if(totalLineNumber > 9) {
  444. decoratorOffset = 25;
  445. }
  446. if(totalLineNumber > 99) {
  447. decoratorOffset = 35;
  448. }
  449. if(totalLineNumber > 999) {
  450. decoratorOffset = 45;
  451. }
  452. if(totalLineNumber > 9999) {
  453. decoratorOffset = 55;
  454. }
  455. }
  456. }
  457. lineNumberAnchor->setPositionX(padding+decoratorOffset - 10);
  458. }
  459. void UITextInput::restructLines() {
  460. for(int i=0; i < bufferLines.size(); i++) {
  461. bufferLines[i]->setPosition(decoratorOffset + padding,padding + (i*(lineHeight+lineSpacing)),0.0f);
  462. }
  463. if(multiLine && lineNumbersEnabled) {
  464. lineNumberBg->setShapeSize(decoratorOffset, height);
  465. }
  466. if(scrollContainer) {
  467. scrollContainer->setContentSize(width, (((lines.size()) * ((lineHeight+lineSpacing)))) + padding);
  468. }
  469. if(multiLine) {
  470. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  471. }
  472. }
  473. void UITextInput::setText(String text) {
  474. if(!multiLine) {
  475. lines[lineOffset] = text;
  476. caretPosition = text.length();
  477. clearSelection();
  478. updateCaretPosition();
  479. } else {
  480. needFullRedraw = true;
  481. selectAll();
  482. insertText(text);
  483. clearSelection();
  484. }
  485. changedText();
  486. }
  487. void UITextInput::onLoseFocus() {
  488. blinkerRect->visible = false;
  489. clearSelection();
  490. }
  491. String UITextInput::getText() {
  492. if(!multiLine) {
  493. return lines[0];
  494. } else {
  495. String totalText = L"";
  496. for(int i=0; i < lines.size(); i++) {
  497. totalText += lines[i];
  498. if(i < lines.size()-1)
  499. totalText += L"\n";
  500. }
  501. return totalText;
  502. }
  503. }
  504. void UITextInput::updateCaretPosition() {
  505. caretImagePosition = padding;
  506. if(caretPosition == 0) {
  507. caretImagePosition = padding;
  508. } else if(caretPosition > lines[lineOffset].length()) {
  509. caretPosition = lines[lineOffset].length();
  510. String caretSubString = lines[lineOffset].substr(0,caretPosition);
  511. caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
  512. caretImagePosition = caretImagePosition + padding;
  513. } else {
  514. String caretSubString = lines[lineOffset].substr(0,caretPosition);
  515. caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
  516. caretImagePosition = caretImagePosition + padding;
  517. }
  518. blinkerRect->visible = true;
  519. blinkTimer->Reset();
  520. if(doSelectToCaret) {
  521. doSelectToCaret = false;
  522. }
  523. /*
  524. if(multiLine) {
  525. if(linesContainer->getPosition().y + currentLine->getPosition2D().y < 0.0) {
  526. scrollContainer->scrollVertical(-(lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  527. } else if(linesContainer->getPosition().y + currentLine->getPosition2D().y > scrollContainer->getHeight()) {
  528. scrollContainer->scrollVertical((lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  529. }
  530. }
  531. */
  532. }
  533. void UITextInput::selectLineFromOffset() {
  534. lineOffset = lineOffset;
  535. }
  536. void UITextInput::dragSelectionTo(Number x, Number y) {
  537. x -= padding * 2.0;
  538. y -= padding;
  539. int lineOffset = y / (lineHeight+lineSpacing);
  540. if(lineOffset > lines.size()-1)
  541. lineOffset = lines.size()-1;
  542. String selectToLine = lines[lineOffset];
  543. int len = selectToLine.length();
  544. Number slen;
  545. int caretPosition = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,len));
  546. for(int i=0; i < len; i++) {
  547. slen = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,i));
  548. if(slen > x) {
  549. caretPosition = i;
  550. break;
  551. }
  552. }
  553. if(x > slen)
  554. caretPosition = len;
  555. // if(multiLine)
  556. // caretPosition++;
  557. if(caretPosition < 0)
  558. caretPosition = 0;
  559. setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
  560. }
  561. int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
  562. for(int i=caretPosition; i > 0; i--) {
  563. String bit = lines[caretLine].substr(i,1);
  564. char chr = ((char*)bit.c_str())[0];
  565. if(!isNumberOrCharacter(chr) && i < caretPosition-1) {
  566. return i+1;
  567. }
  568. }
  569. return 0;
  570. }
  571. int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
  572. int len = lines[caretLine].length();
  573. for(int i=caretPosition; i < len; i++) {
  574. String bit = lines[caretLine].substr(i,1);
  575. char chr = ((char*)bit.c_str())[0];
  576. if(!isNumberOrCharacter(chr) && i > caretPosition) {
  577. return i;
  578. }
  579. }
  580. return lines[caretLine].length();
  581. }
  582. void UITextInput::selectWordAtCaret() {
  583. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  584. clearSelection();
  585. updateCaretPosition();
  586. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  587. updateCaretPosition();
  588. }
  589. void UITextInput::replaceAll(String what, String withWhat) {
  590. for(int i=0; i < lines.size(); i++) {
  591. lines[i] = lines[i].replace(what, withWhat);
  592. }
  593. needFullRedraw = true;
  594. changedText();
  595. }
  596. void UITextInput::findString(String stringToFind, bool replace, String replaceString) {
  597. clearSelection();
  598. findMatches.clear();
  599. for(int i=0; i < lines.size(); i++) {
  600. String lineText = lines[i];
  601. int offset = 0;
  602. int retVal = -1;
  603. do {
  604. retVal = lineText.find(stringToFind, offset);
  605. if(retVal != -1) {
  606. FindMatch match;
  607. match.lineNumber = i;
  608. match.caretStart = retVal;
  609. match.caretEnd = retVal + stringToFind.length();
  610. findMatches.push_back(match);
  611. offset = retVal + stringToFind.length();
  612. }
  613. } while(retVal != -1);
  614. }
  615. if(findMatches.size() > 0) {
  616. if(replace) {
  617. FindMatch match = findMatches[findIndex];
  618. String oldText = lines[match.lineNumber];
  619. String newText = oldText.substr(0,match.caretStart) + replaceString + oldText.substr(match.caretEnd);
  620. lines[match.lineNumber] = newText;
  621. findMatches[findIndex].caretEnd = findMatches[findIndex].caretStart + replaceString.length();
  622. changedText();
  623. }
  624. findIndex = 0;
  625. findCurrent();
  626. }
  627. }
  628. void UITextInput::findNext() {
  629. if(findMatches.size() == 0)
  630. return;
  631. findIndex++;
  632. if(findIndex == findMatches.size()) {
  633. findIndex = 0;
  634. }
  635. findCurrent();
  636. }
  637. void UITextInput::findPrevious() {
  638. if(findMatches.size() == 0)
  639. return;
  640. findIndex--;
  641. if(findIndex < 0) {
  642. findIndex = findMatches.size()-1;
  643. }
  644. findCurrent();
  645. }
  646. void UITextInput::findCurrent() {
  647. if(findMatches.size() == 0)
  648. return;
  649. FindMatch match = findMatches[findIndex];
  650. lineOffset = match.lineNumber;
  651. caretPosition = match.caretStart;
  652. lineOffset = match.lineNumber;
  653. updateCaretPosition();
  654. showLine(findMatches[findIndex].lineNumber, false);
  655. setSelection(match.lineNumber, match.lineNumber, match.caretStart, match.caretEnd);
  656. }
  657. void UITextInput::setCaretToMouse(Number x, Number y) {
  658. clearSelection();
  659. x -= (padding) + decoratorOffset;
  660. y -= padding;
  661. //if(lines.size() > 1) {
  662. lineOffset = y / (lineHeight+lineSpacing);
  663. if(lineOffset > lines.size()-1)
  664. lineOffset = lines.size()-1;
  665. selectLineFromOffset();
  666. //}
  667. int len = lines[lineOffset].length();
  668. Number slen;
  669. int newCaretPosition = -1;
  670. for(int i=1; i < len; i++) {
  671. slen = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i));
  672. Number slen_prev = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i-1));
  673. if(x >= slen_prev && x <= slen) {
  674. if(x < slen_prev + ((slen - slen_prev) /2.0)) {
  675. newCaretPosition = i-1;
  676. break;
  677. } else {
  678. newCaretPosition = i;
  679. break;
  680. }
  681. }
  682. }
  683. if(newCaretPosition == -1)
  684. newCaretPosition = 0;
  685. if(x > slen)
  686. newCaretPosition = len;
  687. caretPosition = newCaretPosition;
  688. updateCaretPosition();
  689. }
  690. void UITextInput::removeLine(unsigned int lineIndex) {
  691. lines.erase(lines.begin()+lineIndex);
  692. renumberLines();
  693. restructLines();
  694. changedText();
  695. }
  696. void UITextInput::selectAll() {
  697. setSelection(0, lines.size()-1, 0, lines[lines.size()-1].length());
  698. }
  699. void UITextInput::insertText(String text) {
  700. vector<String> strings = text.split("\n");
  701. settingText = true;
  702. if(hasSelection)
  703. deleteSelection();
  704. if(strings.size() > 1) {
  705. String ctext = lines[lineOffset];
  706. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  707. ctext = ctext.substr(0,caretPosition);
  708. ctext += strings[0];
  709. lines[lineOffset] = ctext;
  710. caretPosition = ctext.length();
  711. for(int i=1; i < strings.size()-1; i++) {
  712. insertLine(true);
  713. ctext = strings[i];
  714. lines[lineOffset] = ctext;
  715. caretPosition = ctext.length();
  716. }
  717. insertLine(true);
  718. ctext = strings[strings.size()-1] + text2;
  719. caretPosition = ctext.length();
  720. lines[lineOffset] = ctext;
  721. } else {
  722. String ctext = lines[lineOffset];
  723. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  724. ctext = ctext.substr(0,caretPosition);
  725. ctext += text + text2;
  726. caretPosition += text.length();
  727. lines[lineOffset] = ctext;
  728. }
  729. settingText = false;
  730. restructLines();
  731. renumberLines();
  732. changedText();
  733. updateCaretPosition();
  734. }
  735. String UITextInput::getLineText(unsigned int index) {
  736. if(index < lines.size()) {
  737. return lines[index];
  738. } else {
  739. return "";
  740. }
  741. }
  742. String UITextInput::getSelectionText() {
  743. if(!hasSelection)
  744. return L"";
  745. String totalText = L"";
  746. if(selectionTop == selectionBottom) {
  747. totalText = lines[selectionTop].substr(selectionL, selectionR-selectionL);
  748. return totalText;
  749. } else {
  750. totalText += lines[selectionTop].substr(selectionL, lines[selectionTop].length()-selectionL);
  751. totalText += L"\n";
  752. }
  753. if(selectionBottom > selectionTop+1) {
  754. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  755. totalText += lines[i];
  756. totalText += L"\n";
  757. }
  758. }
  759. totalText += lines[selectionBottom].substr(0, selectionL);
  760. return totalText;
  761. }
  762. void UITextInput::setSelectionColor(Color color) {
  763. selectorRectTop->color = color;
  764. selectorRectMiddle->color = color;
  765. selectorRectBottom->color = color;
  766. }
  767. void UITextInput::setCursorColor(Color color) {
  768. blinkerRect->color = color;
  769. }
  770. void UITextInput::setBackgroundColor(Color color) {
  771. inputRect->color = color;
  772. }
  773. void UITextInput::setLineNumberColor(Color color) {
  774. lineNumberColor = color;
  775. for(int i=0; i < numberLines.size(); i++) {
  776. numberLines[i]->color = lineNumberColor;
  777. }
  778. }
  779. void UITextInput::setTextColor(Color color) {
  780. textColor = color;
  781. for(int i=0; i < bufferLines.size(); i++) {
  782. bufferLines[i]->color = textColor;
  783. }
  784. }
  785. UIScrollContainer *UITextInput::getScrollContainer() {
  786. return scrollContainer;
  787. }
  788. void UITextInput::saveUndoState() {
  789. UITextInputUndoState newState;
  790. newState.content = getText();
  791. newState.caretPosition = caretPosition;
  792. newState.lineOffset = lineOffset;
  793. newState.hasSelection = hasSelection;
  794. if(hasSelection) {
  795. newState.selectionLine = selectionLine;
  796. newState.selectionCaretPosition = selectionCaretPosition;
  797. }
  798. undoStates[undoStateIndex] = newState;
  799. // if we hit undo state capacity, shift the whole stack
  800. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  801. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  802. undoStates[i] = undoStates[i+1];
  803. }
  804. } else {
  805. undoStateIndex++;
  806. }
  807. maxRedoIndex = undoStateIndex;
  808. }
  809. void UITextInput::setUndoState(UITextInputUndoState state) {
  810. clearSelection();
  811. setText(state.content);
  812. lineOffset = state.lineOffset;
  813. caretPosition = state.caretPosition;
  814. lineOffset = state.lineOffset;
  815. updateCaretPosition();
  816. if(state.hasSelection) {
  817. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  818. }
  819. }
  820. void UITextInput::Undo() {
  821. if(undoStateIndex > 0) {
  822. undoStateIndex--;
  823. setUndoState(undoStates[undoStateIndex]);
  824. }
  825. }
  826. void UITextInput::Redo() {
  827. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  828. undoStateIndex++;
  829. setUndoState(undoStates[undoStateIndex]);
  830. }
  831. }
  832. void UITextInput::Cut() {
  833. saveUndoState();
  834. Copy();
  835. if(hasSelection) {
  836. deleteSelection();
  837. }
  838. }
  839. void UITextInput::Copy() {
  840. if(hasSelection) {
  841. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  842. }
  843. }
  844. void UITextInput::Paste() {
  845. saveUndoState();
  846. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  847. }
  848. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  849. if(top) {
  850. scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
  851. } else {
  852. scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
  853. }
  854. }
  855. bool UITextInput::isNumberOrCharacter(wchar_t charCode) {
  856. if(charCode > 47 && charCode < 58)
  857. return true;
  858. if(charCode > 64 && charCode < 91)
  859. return true;
  860. if(charCode > 96 && charCode < 123)
  861. return true;
  862. return false;
  863. }
  864. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  865. if(!hasFocus)
  866. return;
  867. // Logger::log("UCHAR: %d\n", charCode);
  868. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  869. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  870. selectAll();
  871. return;
  872. }
  873. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  874. Copy();
  875. return;
  876. }
  877. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  878. Cut();
  879. return;
  880. }
  881. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  882. Undo();
  883. return;
  884. }
  885. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  886. Redo();
  887. return;
  888. }
  889. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  890. Redo();
  891. return;
  892. }
  893. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  894. Paste();
  895. return;
  896. }
  897. if(key == KEY_LEFT) {
  898. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  899. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  900. if(hasSelection) {
  901. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  902. } else {
  903. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  904. }
  905. } else {
  906. caretPosition = 0;
  907. clearSelection();
  908. updateCaretPosition();
  909. }
  910. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  911. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  912. if(hasSelection) {
  913. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  914. } else {
  915. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  916. }
  917. } else {
  918. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  919. clearSelection();
  920. updateCaretPosition();
  921. }
  922. } else {
  923. if(caretPosition > 0) {
  924. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  925. if(hasSelection) {
  926. if(selectionCaretPosition > 0)
  927. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  928. } else {
  929. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  930. }
  931. } else {
  932. caretPosition--;
  933. clearSelection();
  934. updateCaretPosition();
  935. }
  936. }
  937. }
  938. return;
  939. }
  940. if(key == KEY_RIGHT) {
  941. if(caretPosition < lines[lineOffset].length()) {
  942. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  943. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  944. if(hasSelection) {
  945. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine].length());
  946. } else {
  947. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, lines[lineOffset].length());
  948. }
  949. } else {
  950. caretPosition = lines[lineOffset].length();
  951. clearSelection();
  952. }
  953. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  954. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  955. if(hasSelection) {
  956. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  957. } else {
  958. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  959. }
  960. } else {
  961. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  962. clearSelection();
  963. }
  964. } else {
  965. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  966. if(hasSelection) {
  967. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  968. } else {
  969. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  970. }
  971. } else {
  972. caretPosition++;
  973. clearSelection();
  974. }
  975. }
  976. updateCaretPosition();
  977. }
  978. return;
  979. }
  980. if(key == KEY_PAGEUP) {
  981. if(multiLine) {
  982. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  983. }
  984. return;
  985. }
  986. if(key == KEY_PAGEDOWN) {
  987. if(multiLine) {
  988. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  989. }
  990. return;
  991. }
  992. if(key == KEY_UP) {
  993. if(multiLine) {
  994. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  995. if(hasSelection) {
  996. if(selectionLine > 0)
  997. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  998. } else {
  999. if(this->lineOffset > 0)
  1000. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  1001. }
  1002. } else {
  1003. clearSelection();
  1004. if(lineOffset > 0) {
  1005. lineOffset--;
  1006. selectLineFromOffset();
  1007. updateCaretPosition();
  1008. }
  1009. }
  1010. }
  1011. blinkerRect->visible = true;
  1012. return;
  1013. }
  1014. if(key == KEY_DOWN) {
  1015. if(multiLine) {
  1016. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  1017. if(hasSelection) {
  1018. if(selectionLine < lines.size()-1)
  1019. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  1020. } else {
  1021. if(this->lineOffset < lines.size()-1)
  1022. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  1023. }
  1024. } else {
  1025. clearSelection();
  1026. if(lineOffset < lines.size()-1) {
  1027. lineOffset++;
  1028. selectLineFromOffset();
  1029. updateCaretPosition();
  1030. }
  1031. }
  1032. }
  1033. blinkerRect->visible = true;
  1034. return;
  1035. }
  1036. if(key == KEY_ESCAPE) {
  1037. if(!multiLine) {
  1038. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  1039. }
  1040. }
  1041. if(key == KEY_RETURN) {
  1042. if(multiLine) {
  1043. saveUndoState();
  1044. if(hasSelection) {
  1045. deleteSelection();
  1046. }
  1047. insertLine(true);
  1048. updateCaretPosition();
  1049. } else {
  1050. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  1051. }
  1052. return;
  1053. }
  1054. String ctext = lines[lineOffset];
  1055. if((charCode > 31 && charCode < 127) || charCode > 127) {
  1056. if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
  1057. if(!isNumberOrCharacter(charCode)) {
  1058. saveUndoState();
  1059. }
  1060. if(hasSelection)
  1061. deleteSelection();
  1062. ctext = lines[lineOffset];
  1063. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1064. ctext = ctext.substr(0,caretPosition);
  1065. ctext += charCode + text2;
  1066. caretPosition++;
  1067. }
  1068. }
  1069. if(key == KEY_TAB && multiLine) {
  1070. saveUndoState();
  1071. if(hasSelection)
  1072. deleteSelection();
  1073. ctext = lines[lineOffset];
  1074. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1075. ctext = ctext.substr(0,caretPosition);
  1076. ctext += (wchar_t)'\t' + text2;
  1077. caretPosition++;
  1078. }
  1079. if(key == KEY_BACKSPACE) {
  1080. if(hasSelection) {
  1081. saveUndoState();
  1082. deleteSelection();
  1083. return;
  1084. } else {
  1085. ctext = lines[lineOffset];
  1086. if(caretPosition > 0) {
  1087. saveUndoState();
  1088. if(ctext.length() > 0) {
  1089. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1090. ctext = ctext.substr(0,caretPosition-1);
  1091. ctext += text2;
  1092. caretPosition--;
  1093. }
  1094. } else {
  1095. if(lineOffset > 0) {
  1096. saveUndoState();
  1097. lineOffset--;
  1098. selectLineFromOffset();
  1099. caretPosition = lines[lineOffset].length();
  1100. updateCaretPosition();
  1101. lines[lineOffset] = lines[lineOffset] + ctext;
  1102. removeLine(lineOffset+1);
  1103. return;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. lines[lineOffset] = ctext;
  1109. changedText();
  1110. updateCaretPosition();
  1111. }
  1112. void UITextInput::Update() {
  1113. if(!multiLine) {
  1114. Vector2 pos = getScreenPosition();
  1115. scissorBox.setRect(pos.x,pos.y, width, height);
  1116. }
  1117. if(hasSelection) {
  1118. blinkerRect->visible = false;
  1119. }
  1120. blinkerRect->setPosition(decoratorOffset + caretImagePosition + 1, padding + (lineOffset * ( lineHeight+lineSpacing)));
  1121. if(hasFocus) {
  1122. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  1123. } else {
  1124. blinkerRect->visible = false;
  1125. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  1126. }
  1127. for(int i=0; i < linesToDelete.size(); i++) {
  1128. delete linesToDelete[i];
  1129. }
  1130. linesToDelete.clear();
  1131. }
  1132. UITextInput::~UITextInput() {
  1133. }
  1134. void UITextInput::readjustBuffer() {
  1135. int bufferOffset = -linesContainer->position.y/ ( lineHeight+lineSpacing);
  1136. Number bufferLineOffset = bufferOffset * ( lineHeight+lineSpacing);
  1137. for(int i=0; i < bufferLines.size(); i++) {
  1138. bufferLines[i]->getLabel()->clearColors();
  1139. if(bufferOffset + i < lines.size()) {
  1140. if(bufferOffset+i < lineColors.size()) {
  1141. for(int j=0; j < lineColors[bufferOffset+i].colors.size(); j++) {
  1142. bufferLines[i]->getLabel()->setColorForRange(lineColors[bufferOffset+i].colors[j].color, lineColors[bufferOffset+i].colors[j].rangeStart, lineColors[bufferOffset+i].colors[j].rangeEnd);
  1143. bufferLines[i]->setColor(1.0, 1.0, 1.0, 1.0);
  1144. }
  1145. }
  1146. bufferLines[i]->setText(lines[bufferOffset+i]);
  1147. } else {
  1148. bufferLines[i]->setText("");
  1149. }
  1150. bufferLines[i]->setPosition(decoratorOffset + padding,padding + bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
  1151. }
  1152. for(int i=0; i < numberLines.size(); i++) {
  1153. if(lineNumbersEnabled) {
  1154. numberLines[i]->setText(String::IntToString(bufferOffset+i+1));
  1155. int textWidth = ceil(numberLines[i]->getLabel()->getTextWidth());
  1156. numberLines[i]->setPosition(-textWidth,padding + bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
  1157. numberLines[i]->visible = true;
  1158. }
  1159. }
  1160. }
  1161. void UITextInput::handleEvent(Event *event) {
  1162. if(event->getDispatcher() == scrollContainer) {
  1163. if(event->getEventCode() == Event::CHANGE_EVENT) {
  1164. readjustBuffer();
  1165. }
  1166. }
  1167. if(event->getDispatcher() == inputRect) {
  1168. switch(event->getEventCode()) {
  1169. case InputEvent::EVENT_MOUSEDOWN:
  1170. if(parentEntity) {
  1171. ((ScreenEntity*)parentEntity)->focusChild(this);
  1172. } else {
  1173. hasFocus = true;
  1174. }
  1175. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1176. draggingSelection = true;
  1177. break;
  1178. case InputEvent::EVENT_MOUSEUP:
  1179. draggingSelection = false;
  1180. break;
  1181. case InputEvent::EVENT_DOUBLECLICK:
  1182. selectWordAtCaret();
  1183. break;
  1184. case InputEvent::EVENT_MOUSEMOVE:
  1185. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1186. if(draggingSelection) {
  1187. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1188. }
  1189. break;
  1190. case InputEvent::EVENT_MOUSEOVER:
  1191. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1192. break;
  1193. case InputEvent::EVENT_MOUSEOUT:
  1194. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  1195. break;
  1196. }
  1197. }
  1198. if(event->getDispatcher() == blinkTimer) {
  1199. if(hasSelection || draggingSelection) {
  1200. blinkerRect->visible = false;
  1201. } else {
  1202. if(hasFocus)
  1203. blinkerRect->visible = !blinkerRect->visible;
  1204. else
  1205. blinkerRect->visible = false;
  1206. }
  1207. }
  1208. }