PolyUITextInput.cpp 38 KB

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