| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244 |
- /*
- Copyright (C) 2012 by Ivan Safrin
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #include "PolyUITextInput.h"
- #include "PolyConfig.h"
- #include "PolyInputEvent.h"
- #include "PolyLabel.h"
- #include "PolyCoreServices.h"
- #include "PolyEventHandler.h"
- using namespace Polycode;
- UITextInput::UITextInput(bool multiLine, Number width, Number height) : UIElement() {
- this->multiLine = multiLine;
- processInputEvents = true;
- isNumberOnly = false;
-
- draggingSelection = false;
- hasSelection = false;
- doSelectToCaret = false;
-
- caretPosition = 0;
- caretImagePosition = 0;
-
- settingText = false;
-
- currentLine = NULL;
-
- needFullRedraw = false;
-
- lineOffset = -1;
- numLines = 0;
-
- this->positionMode = ScreenEntity::POSITION_TOPLEFT;
- Config *conf = CoreServices::getInstance()->getConfig();
-
- if(multiLine)
- fontName = conf->getStringValue("Polycode", "uiTextInputFontNameMultiLine");
- else
- fontName = conf->getStringValue("Polycode", "uiTextInputFontName");
-
- if(multiLine)
- fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSizeMultiline");
- else
- fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSize");
-
- Number rectHeight = height;
- if(!multiLine) {
- rectHeight = fontSize+12;
- }
-
- linesContainer = new ScreenEntity();
- linesContainer->processInputEvents = true;
-
- lineSpacing = conf->getNumericValue("Polycode", "textEditLineSpacing");
-
- Number st = conf->getNumericValue("Polycode", "textBgSkinT");
- Number sr = conf->getNumericValue("Polycode", "textBgSkinR");
- Number sb = conf->getNumericValue("Polycode", "textBgSkinB");
- Number sl = conf->getNumericValue("Polycode", "textBgSkinL");
-
- padding = conf->getNumericValue("Polycode", "textBgSkinPadding");
-
- inputRect = new UIBox(conf->getStringValue("Polycode", "textBgSkin"),
- st,sr,sb,sl,
- width+(padding*2), height+(padding*2));
-
- addChild(inputRect);
-
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
- inputRect->addEventListener(this, InputEvent::EVENT_DOUBLECLICK);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
- inputRect->processInputEvents = true;
- inputRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
-
- selectorRectTop = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
- selectorRectTop->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- selectorRectTop->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
- selectorRectTop->visible = false;
- linesContainer->addChild(selectorRectTop);
- selectorRectMiddle = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
- selectorRectMiddle->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- selectorRectMiddle->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
- selectorRectMiddle->visible = false;
- linesContainer->addChild(selectorRectMiddle);
- selectorRectBottom = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
- selectorRectBottom->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- selectorRectBottom->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
- selectorRectBottom->visible = false;
- linesContainer->addChild(selectorRectBottom);
-
-
- blinkerRect = new ScreenShape(ScreenShape::SHAPE_RECT, 1, fontSize+4,0,0);
- blinkerRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- blinkerRect->setColor(0,0,0,1);
- linesContainer->addChild(blinkerRect);
- blinkerRect->visible = false;
- blinkerRect->setPosition(0,3);
-
- blinkTimer = new Timer(true, 500);
- blinkTimer->addEventListener(this, Timer::EVENT_TRIGGER);
- focusable = true;
- this->width = width;
- this->height = rectHeight;
- setHitbox(width, rectHeight);
-
- updateCaretPosition();
-
-
- scrollContainer = NULL;
-
- if(multiLine) {
- scrollContainer = new UIScrollContainer(linesContainer, false, true, 200, 200);
- addChild(scrollContainer);
- } else {
- addChild(linesContainer);
- enableScissor = true;
- }
-
- undoStateIndex = 0;
- maxRedoIndex = 0;
-
- syntaxHighliter = NULL;
-
- insertLine(true);
- }
- void UITextInput::setNumberOnly(bool val) {
- isNumberOnly = val;
- }
- void UITextInput::clearSelection() {
- hasSelection = false;
- selectorRectTop->visible = false;
- selectorRectMiddle->visible = false;
- selectorRectBottom->visible = false;
- }
- void UITextInput::setSelection(int lineStart, int lineEnd, int colStart, int colEnd) {
- if(lineStart == lineEnd && colStart == colEnd) {
- clearSelection();
- return;
- }
- if(lineStart == lineOffset) {
- selectionLine = lineEnd;
- } else {
- selectionLine = lineStart;
- }
- if(colStart == caretPosition) {
- selectionCaretPosition = colEnd;
- } else {
- selectionCaretPosition = colStart;
- }
-
- //printf("SET lineStart:%d lineEnd:%d colStart:%d colEnd:%d\n", lineStart, lineEnd, colStart, colEnd);
-
- if(lineStart > lineEnd) {
- int tmp = lineStart;
- lineStart = lineEnd;
- lineEnd = tmp;
-
- tmp = colStart;
- colStart = colEnd;
- colEnd = tmp;
- }
-
- if(colStart > colEnd && lineStart == lineEnd) {
- int tmp = colStart;
- colStart = colEnd;
- colEnd = tmp;
- }
-
- clearSelection();
-
-
- if(lineStart > lines.size()-1)
- return;
- ScreenLabel *topLine = lines[lineStart];
-
- if(colStart+1 > topLine->getText().length()) {
- colStart = topLine->getText().length();
- }
-
- Number fColEnd = colEnd;
-
- if(colEnd > topLine->getText().length() || lineStart != lineEnd)
- fColEnd = topLine->getText().length();
- Number topSize, topHeight, topX;
-
- selectorRectTop->visible = true;
- topSize = topLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), topLine->getText().substr(colStart,fColEnd-colStart), fontSize) - 4;
- topHeight = lineHeight+lineSpacing;
- if(colStart > 0) {
- topX = topLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), topLine->getText().substr(0,colStart-1), fontSize); ;
- } else {
- topX = 0;
- }
- selectorRectTop->setScale(topSize, topHeight);
- selectorRectTop->setPosition(topX + padding + (topSize/2.0)+ 1, padding + (lineStart * (lineHeight+lineSpacing)) + (topHeight/2.0));
-
- if(lineEnd > lineStart && lineEnd < lines.size()) {
- ScreenLabel *bottomLine = lines[lineEnd];
- selectorRectBottom->visible = true;
- Number bottomSize = bottomLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), bottomLine->getText().substr(0,colEnd), fontSize) - 4;
- if(bottomSize < 0)
- bottomSize = this->width-padding;
- Number bottomHeight = lineHeight+lineSpacing;
- selectorRectBottom->setScale(bottomSize, bottomHeight);
- selectorRectBottom->setPosition(padding + (bottomSize/2.0) + 1, padding + (lineEnd * (lineHeight+lineSpacing)) + (bottomHeight/2.0));
-
- if(lineEnd != lineStart+1) {
- // need filler
- selectorRectMiddle->visible = true;
- Number midSize = this->width-padding;
- Number midHeight = 0;
- for(int i=lineStart+1; i < lineEnd;i++) {
- midHeight += lineHeight+lineSpacing;
- }
- selectorRectMiddle->setScale(midSize, midHeight);
- selectorRectMiddle->setPosition(padding + (midSize/2.0), padding + ((lineStart+1) * (lineHeight+lineSpacing)) + (midHeight/2.0));
-
- }
-
- }
- hasSelection = true;
-
- selectionTop = lineStart;
- selectionBottom = lineEnd;
- selectionL = colStart;
- selectionR = colEnd;
- }
- void UITextInput::deleteSelection() {
- if(selectionTop == selectionBottom) {
- String ctext = lines[selectionTop]->getText();
- String newText = ctext.substr(0, selectionL);
- int rside = selectionR;
- if(rside > ctext.length()-1)
- rside = ctext.length() - 1;
- newText += ctext.substr(rside,ctext.length() - selectionR);
- lines[selectionTop]->setText(newText);
- } else {
-
- String ctext = lines[selectionTop]->getText();
- String newText = ctext.substr(0, selectionL);
- lines[selectionTop]->setText(newText);
- ScreenLabel *bottomLine = lines[selectionBottom];
-
- // if whole lines to remove, do it
- vector<ScreenLabel*> linesToRemove;
- if(selectionBottom > selectionTop + 1) {
- for(int i=selectionTop+1; i < selectionBottom; i++) {
- linesToRemove.push_back(lines[i]);
- }
- for(int i=0; i < linesToRemove.size(); i++) {
- removeLine(linesToRemove[i]);
- }
- }
-
- ctext = bottomLine->getText();
-
- int rside = selectionR;
- if(rside > ctext.length()-1)
- rside = ctext.length() - 1;
- newText = ctext.substr(rside,ctext.length() - selectionR);
-
-
- lineOffset = selectionTop;
- selectLineFromOffset();
- caretPosition = currentLine->getText().length();
- updateCaretPosition();
- currentLine->setText(currentLine->getText() + newText);
- removeLine(bottomLine);
-
-
- }
- clearSelection();
- caretPosition = selectionL;
- updateCaretPosition();
- changedText();
- }
- void UITextInput::changedText() {
- if(settingText)
- return;
-
- if(syntaxHighliter && multiLine) {
-
- unsigned int startLine = (-linesContainer->getPosition().y) / (lineHeight+lineSpacing);
- unsigned int endLine = startLine + ((int)((height / (lineHeight+lineSpacing)))) + 1;
-
- if(endLine > lines.size())
- endLine = lines.size();
-
- if(needFullRedraw) {
- startLine = 0;
- endLine = lines.size();
- needFullRedraw = false;
- }
-
- String totalText = L"";
- for(int i=startLine; i < endLine; i++) {
- totalText += lines[i]->getText();
- if(i < lines.size()-1)
- totalText += L"\n";
- }
-
- std::vector<SyntaxHighlightToken> tokens = syntaxHighliter->parseText(totalText);
-
- for(int i=startLine; i < endLine; i++) {
- lines[i]->getLabel()->clearColors();
- }
-
- int lineIndex = startLine;
- int rangeStart = 0;
- int rangeEnd = 0;
-
- for(int i=0; i < tokens.size(); i++) {
- if(tokens[i].text == "\n") {
- lineIndex++;
- rangeStart = 0;
- rangeEnd = 0;
- } else {
-
- if(lineIndex < lines.size()) {
- int textLength = tokens[i].text.length();
- if(tokens[i].text.length() > 1) {
- rangeEnd = rangeStart + textLength-1;
- lines[lineIndex]->getLabel()->setColorForRange(tokens[i].color, rangeStart, rangeEnd);
- rangeStart = rangeStart + textLength;
- } else {
- rangeEnd = rangeStart;
- lines[lineIndex]->getLabel()->setColorForRange(tokens[i].color, rangeStart, rangeEnd);
- rangeStart++;
- }
- }
- }
- }
-
- for(int i=startLine; i < endLine; i++) {
- lines[i]->setText(lines[i]->getText());
- lines[i]->setColor(1.0, 1.0, 1.0, 1.0);
- }
-
- }
- dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
- }
- void UITextInput::setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighliter) {
- this->syntaxHighliter = syntaxHighliter;
- }
- void UITextInput::Resize(Number width, Number height) {
- inputRect->resizeBox(width, height);
- this->width = width;
- this->height = height;
- matrixDirty = true;
- setHitbox(width,height);
-
- if(multiLine) {
- inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
- }
- if(scrollContainer) {
- scrollContainer->Resize(width, height);
- }
- }
- int UITextInput::insertLine(bool after) {
-
- numLines++;
-
- ScreenLabel *newLine = new ScreenLabel(L"", fontSize, fontName, Label::ANTIALIAS_FULL);
- newLine->setColor(0,0,0,1);
- lineHeight = newLine->getHeight();
- linesContainer->addChild(newLine);
-
- if(after) {
-
- if(currentLine) {
- String ctext = currentLine->getText();
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- currentLine->setText(ctext);
- newLine->setText(text2);
- caretPosition=0;
- }
-
- currentLine = newLine;
-
- vector<ScreenLabel*>::iterator it;
- it = lines.begin();
-
- for(int i=0; i < lineOffset+1; i++) {
- it++;
- }
-
- lineOffset = lineOffset + 1;
- lines.insert(it,newLine);
-
- restructLines();
- } else {
- // do we even need that? I don't think so.
- }
-
- changedText();
- return 1;
- }
- void UITextInput::restructLines() {
- for(int i=0; i < lines.size(); i++) {
- lines[i]->setPosition(padding,padding + (i*(lineHeight+lineSpacing)) ,0.0f);
- }
-
- if(scrollContainer) {
- scrollContainer->setContentSize(width, (((lines.size()) * ((lineHeight+lineSpacing)))) + padding);
- }
-
- if(multiLine) {
- inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
- }
-
- }
- void UITextInput::setText(String text) {
- if(!multiLine) {
- currentLine->setText(text);
- caretPosition = text.length();
- clearSelection();
- updateCaretPosition();
- } else {
- needFullRedraw = true;
- selectAll();
- insertText(text);
- clearSelection();
- }
-
- // this->text = text;
- // currentLine->setText(text);
- }
- void UITextInput::onLoseFocus() {
- blinkerRect->visible = false;
- clearSelection();
- }
- String UITextInput::getText() {
-
- if(!multiLine) {
- return currentLine->getText();
- } else {
- String totalText = L"";
- for(int i=0; i < lines.size(); i++) {
- totalText += lines[i]->getText();
- if(i < lines.size()-1)
- totalText += L"\n";
- }
- return totalText;
- }
- }
- void UITextInput::updateCaretPosition() {
- caretImagePosition = padding;
- if(caretPosition == 0) {
- caretImagePosition = padding;
- } else if(caretPosition > currentLine->getText().length()) {
- caretPosition = currentLine->getText().length();
- String caretSubString = currentLine->getText().substr(0,caretPosition);
- caretImagePosition = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), caretSubString, fontSize);
- caretImagePosition = caretImagePosition - 4.0f + padding;
- } else {
- String caretSubString = currentLine->getText().substr(0,caretPosition);
- caretImagePosition = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), caretSubString, fontSize);
- caretImagePosition = caretImagePosition - 4.0f + padding;
- }
- blinkerRect->visible = true;
- blinkTimer->Reset();
-
- if(doSelectToCaret) {
- doSelectToCaret = false;
-
- }
-
- if(multiLine && currentLine) {
-
-
- if(linesContainer->getPosition().y + currentLine->getPosition2D().y < 0.0) {
- scrollContainer->scrollVertical(-(lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
- } else if(linesContainer->getPosition().y + currentLine->getPosition2D().y > scrollContainer->getHeight()) {
- scrollContainer->scrollVertical((lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
- }
- }
- }
- void UITextInput::selectLineFromOffset() {
- for(int i=0; i < lines.size(); i++) {
- if(i == lineOffset) {
- currentLine = lines[i];
- return;
- }
- }
- }
- void UITextInput::dragSelectionTo(Number x, Number y) {
- x -= padding;
- y -= padding;
- int lineOffset = y / (lineHeight+lineSpacing);
- if(lineOffset > lines.size()-1)
- lineOffset = lines.size()-1;
-
- ScreenLabel *selectToLine = lines[lineOffset];
-
- int len = selectToLine->getText().length();
- Number slen;
- int caretPosition = selectToLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), selectToLine->getText().substr(0,len), fontSize);
- for(int i=0; i < len; i++) {
- slen = selectToLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), selectToLine->getText().substr(0,i), fontSize);
- if(slen > x) {
- caretPosition = i;
- break;
- }
- }
- if(x > slen)
- caretPosition = len;
-
- if(multiLine)
- caretPosition++;
-
- if(caretPosition < 0)
- caretPosition = 0;
- setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
- }
- int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
- for(int i=caretPosition; i > 0; i--) {
- String bit = lines[caretLine]->getText().substr(i,1);
- char chr = ((char*)bit.c_str())[0];
- if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i < caretPosition-1) {
- return i+1;
- }
- }
- return 0;
- }
- int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
- int len = lines[caretLine]->getText().length();
- for(int i=caretPosition; i < len; i++) {
- String bit = lines[caretLine]->getText().substr(i,1);
- char chr = ((char*)bit.c_str())[0];
- if(((chr > 0 && chr < 48) || (chr > 57 && chr < 65) || (chr > 90 && chr < 97) || (chr > 122 && chr < 127)) && i > caretPosition) {
- return i;
- }
- }
- return lines[caretLine]->getText().length();
- }
- void UITextInput::selectWordAtCaret() {
- caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
- clearSelection();
- updateCaretPosition();
-
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
- updateCaretPosition();
- }
- void UITextInput::replaceAll(String what, String withWhat) {
- for(int i=0; i < lines.size(); i++) {
- lines[i]->setText(lines[i]->getText().replace(what, withWhat));
- }
- needFullRedraw = true;
- changedText();
- }
- void UITextInput::findString(String stringToFind, bool replace, String replaceString) {
- clearSelection();
- findMatches.clear();
-
- for(int i=0; i < lines.size(); i++) {
- String lineText = lines[i]->getText();
-
- int offset = 0;
- int retVal = -1;
- do {
- retVal = lineText.find(stringToFind, offset);
- if(retVal != -1) {
- FindMatch match;
- match.lineNumber = i;
- match.caretStart = retVal;
- match.caretEnd = retVal + stringToFind.length();
- findMatches.push_back(match);
- offset = retVal + stringToFind.length();
- }
- } while(retVal != -1);
-
- }
-
-
- if(findMatches.size() > 0) {
- if(replace) {
- FindMatch match = findMatches[findIndex];
- String oldText = lines[match.lineNumber]->getText();
- String newText = oldText.substr(0,match.caretStart) + replaceString + oldText.substr(match.caretEnd);
-
- lines[match.lineNumber]->setText(newText);
- findMatches[findIndex].caretEnd = findMatches[findIndex].caretStart + replaceString.length();
- changedText();
- }
-
- findIndex = 0;
- findCurrent();
- }
- }
- void UITextInput::findNext() {
- if(findMatches.size() == 0)
- return;
- findIndex++;
- if(findIndex == findMatches.size()) {
- findIndex = 0;
- }
- findCurrent();
- }
- void UITextInput::findPrevious() {
- if(findMatches.size() == 0)
- return;
- findIndex--;
- if(findIndex < 0) {
- findIndex = findMatches.size()-1;
- }
- findCurrent();
- }
- void UITextInput::findCurrent() {
- if(findMatches.size() == 0)
- return;
- FindMatch match = findMatches[findIndex];
- currentLine = lines[match.lineNumber];
- caretPosition = match.caretStart;
- lineOffset = match.lineNumber;
- updateCaretPosition();
- showLine(findMatches[findIndex].lineNumber, false);
-
- setSelection(match.lineNumber, match.lineNumber, match.caretStart, match.caretEnd);
- }
- void UITextInput::setCaretToMouse(Number x, Number y) {
- clearSelection();
- x -= padding;
- y -= padding;
- //if(lines.size() > 1) {
- lineOffset = y / (lineHeight+lineSpacing);
- if(lineOffset > lines.size()-1)
- lineOffset = lines.size()-1;
- selectLineFromOffset();
- //}
-
- int len = currentLine->getText().length();
- Number slen;
- for(int i=0; i < len; i++) {
- slen = currentLine->getLabel()->getTextWidth(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), currentLine->getText().substr(0,i), fontSize);
- if(slen > x) {
- caretPosition = i;
- break;
- }
- }
- if(x > slen)
- caretPosition = len;
-
- if(multiLine)
- caretPosition++;
-
- updateCaretPosition();
- }
- void UITextInput::removeLine(ScreenLabel *line) {
- for(int i=0;i<lines.size();i++) {
- if(lines[i] == line) {
- lines.erase(lines.begin()+i);
- }
- }
- linesContainer->removeChild(line);
- linesToDelete.push_back(line);
- restructLines();
- changedText();
- }
- void UITextInput::selectAll() {
- setSelection(0, lines.size()-1, 0, lines[lines.size()-1]->getText().length());
- }
- void UITextInput::insertText(String text) {
- vector<String> strings = text.split("\n");
- settingText = true;
- if(hasSelection)
- deleteSelection();
- if(strings.size() > 1) {
- String ctext = currentLine->getText();
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += strings[0];
- currentLine->setText(ctext);
- caretPosition = ctext.length();
-
- for(int i=1; i < strings.size()-1; i++) {
- insertLine(true);
- ctext = strings[i];
- currentLine->setText(ctext);
- caretPosition = ctext.length();
- }
-
- insertLine(true);
- ctext = strings[strings.size()-1] + text2;
- caretPosition = ctext.length();
- currentLine->setText(ctext);
-
- } else {
- String ctext = currentLine->getText();
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += text + text2;
- caretPosition += text.length();
- currentLine->setText(ctext);
- }
-
- settingText = false;
-
- changedText();
- updateCaretPosition();
- restructLines();
- }
- String UITextInput::getLineText(unsigned int index) {
- if(index < lines.size()) {
- return lines[index]->getText();
- } else {
- return "";
- }
- }
- String UITextInput::getSelectionText() {
- String totalText = L"";
- if(selectionTop == selectionBottom) {
- totalText = lines[selectionTop]->getText().substr(selectionL, selectionR-selectionL);
- return totalText;
- } else {
- totalText += lines[selectionTop]->getText().substr(selectionL, lines[selectionTop]->getText().length()-selectionL);
- totalText += L"\n";
- }
-
- if(selectionBottom > selectionTop+1) {
- for(int i=selectionTop+1; i <= selectionBottom; i++) {
- totalText += lines[i]->getText();
- totalText += L"\n";
- }
- }
-
- totalText += lines[selectionBottom]->getText().substr(0, selectionL);
-
- return totalText;
- }
- UIScrollContainer *UITextInput::getScrollContainer() {
- return scrollContainer;
- }
- void UITextInput::saveUndoState() {
- UITextInputUndoState newState;
- newState.content = getText();
- newState.caretPosition = caretPosition;
- newState.lineOffset = lineOffset;
- newState.hasSelection = hasSelection;
- if(hasSelection) {
- newState.selectionLine = selectionLine;
- newState.selectionCaretPosition = selectionCaretPosition;
- }
- undoStates[undoStateIndex] = newState;
-
- // if we hit undo state capacity, shift the whole stack
- if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
- for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
- undoStates[i] = undoStates[i+1];
- }
- } else {
- undoStateIndex++;
- }
-
- maxRedoIndex = undoStateIndex;
- }
- void UITextInput::setUndoState(UITextInputUndoState state) {
- clearSelection();
- setText(state.content);
- currentLine = lines[state.lineOffset];
- caretPosition = state.caretPosition;
- lineOffset = state.lineOffset;
- updateCaretPosition();
-
- if(state.hasSelection) {
- setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
- }
- }
- void UITextInput::Undo() {
- if(undoStateIndex > 0) {
- undoStateIndex--;
- setUndoState(undoStates[undoStateIndex]);
- }
- }
- void UITextInput::Redo() {
- if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
- undoStateIndex++;
- setUndoState(undoStates[undoStateIndex]);
- }
- }
- void UITextInput::Cut() {
- saveUndoState();
- Copy();
- if(hasSelection) {
- deleteSelection();
- }
- }
- void UITextInput::Copy() {
- CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
- }
- void UITextInput::Paste() {
- saveUndoState();
- insertText(CoreServices::getInstance()->getCore()->getClipboardString());
- }
- void UITextInput::showLine(unsigned int lineNumber, bool top) {
- if(top) {
- scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
- } else {
- scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
- }
- }
- void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
-
- if(!hasFocus)
- return;
-
- // Logger::log("UCHAR: %d\n", charCode);
-
- CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
- if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
- selectAll();
- return;
- }
- if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
- Copy();
- return;
- }
- if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
- Cut();
- return;
- }
-
- if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
- Undo();
- return;
- }
- if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
- Redo();
- return;
- }
- if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
- Redo();
- return;
- }
- if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
- Paste();
- return;
- }
-
-
- if(key == KEY_LEFT) {
- if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
- }
- } else {
- caretPosition = 0;
- clearSelection();
- updateCaretPosition();
- }
- } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
- }
- } else {
- caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
- clearSelection();
- updateCaretPosition();
- }
- } else {
- if(caretPosition > 0) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- if(selectionCaretPosition > 0)
- setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
- }
- } else {
- caretPosition--;
- clearSelection();
- updateCaretPosition();
- }
- }
- }
- return;
- }
-
- if(key == KEY_RIGHT) {
- if(caretPosition < currentLine->getText().length()) {
- if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine]->getText().length());
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, currentLine->getText().length());
- }
- } else {
- caretPosition = currentLine->getText().length();
- clearSelection();
- }
- } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
- }
- } else {
- caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
- clearSelection();
- }
- } else {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
- }
- } else {
- caretPosition++;
- clearSelection();
- }
- }
- updateCaretPosition();
- }
- return;
- }
-
- if(key == KEY_PAGEUP) {
- if(multiLine) {
- scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
-
- }
- return;
- }
- if(key == KEY_PAGEDOWN) {
- if(multiLine) {
- scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
-
- }
- return;
- }
-
- if(key == KEY_UP) {
- if(multiLine) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- if(selectionLine > 0)
- setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
- } else {
- if(this->lineOffset > 0)
- setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
- }
- } else {
- clearSelection();
- if(lineOffset > 0) {
- lineOffset--;
- selectLineFromOffset();
- updateCaretPosition();
- }
- }
- }
- blinkerRect->visible = true;
- return;
- }
-
- if(key == KEY_DOWN) {
- if(multiLine) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- if(selectionLine < lines.size()-1)
- setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
- } else {
- if(this->lineOffset < lines.size()-1)
- setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
- }
- } else {
- clearSelection();
- if(lineOffset < lines.size()-1) {
- lineOffset++;
- selectLineFromOffset();
- updateCaretPosition();
- }
- }
- }
- blinkerRect->visible = true;
- return;
- }
-
- if(key == KEY_ESCAPE) {
- if(!multiLine) {
- dispatchEvent(new Event(), Event::CANCEL_EVENT);
- }
- }
-
- if(key == KEY_RETURN) {
- if(multiLine) {
- saveUndoState();
- if(hasSelection) {
- deleteSelection();
- }
- insertLine(true);
- updateCaretPosition();
- } else {
- dispatchEvent(new Event(), Event::COMPLETE_EVENT);
- }
- return;
- }
-
- String ctext = currentLine->getText();
-
-
- // if(1) {
- if((charCode > 31 && charCode < 127) || charCode > 127) {
- if(!isNumberOnly || (isNumberOnly && (charCode > 47 && charCode < 58))) {
- saveUndoState();
- if(hasSelection)
- deleteSelection();
- ctext = currentLine->getText();
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += charCode + text2;
- caretPosition++;
- }
- }
-
- if(key == KEY_TAB && multiLine) {
- saveUndoState();
- if(hasSelection)
- deleteSelection();
- ctext = currentLine->getText();
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += (wchar_t)'\t' + text2;
- caretPosition++;
- }
-
- if(key == KEY_BACKSPACE) {
- if(hasSelection) {
- saveUndoState();
- deleteSelection();
- return;
- } else {
- ctext = currentLine->getText();
- if(caretPosition > 0) {
- saveUndoState();
- if(ctext.length() > 0) {
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition-1);
- ctext += text2;
- caretPosition--;
- }
- } else {
- if(lineOffset > 0) {
- saveUndoState();
- ScreenLabel *lineToRemove = currentLine;
- lineOffset--;
- selectLineFromOffset();
- caretPosition = currentLine->getText().length();
- updateCaretPosition();
- currentLine->setText(currentLine->getText() + ctext);
- removeLine(lineToRemove);
- return;
- }
- }
- }
- }
-
- currentLine->setText(ctext);
- changedText();
- updateCaretPosition();
- }
- void UITextInput::Update() {
- if(!multiLine) {
- Vector2 pos = getScreenPosition();
- scissorBox.setRect(pos.x,pos.y, width, height);
- }
- if(hasSelection) {
- blinkerRect->visible = false;
- }
- blinkerRect->setPosition(caretImagePosition,currentLine->getPosition2D().y);
- if(hasFocus) {
- // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
- } else {
- blinkerRect->visible = false;
- // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
- }
-
- for(int i=0; i < linesToDelete.size(); i++) {
- delete linesToDelete[i];
- }
- linesToDelete.clear();
- }
- UITextInput::~UITextInput() {
- }
-
- void UITextInput::handleEvent(Event *event) {
- if(event->getDispatcher() == inputRect) {
- switch(event->getEventCode()) {
- case InputEvent::EVENT_MOUSEDOWN:
- if(parentEntity) {
- ((ScreenEntity*)parentEntity)->focusChild(this);
- } else {
- hasFocus = true;
- }
- setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
- draggingSelection = true;
- break;
- case InputEvent::EVENT_MOUSEUP:
- draggingSelection = false;
- break;
- case InputEvent::EVENT_DOUBLECLICK:
- selectWordAtCaret();
- break;
- case InputEvent::EVENT_MOUSEMOVE:
- CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
- if(draggingSelection) {
- dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
- }
- break;
- case InputEvent::EVENT_MOUSEOVER:
- CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
- break;
- case InputEvent::EVENT_MOUSEOUT:
- CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
- break;
- }
- }
-
- if(event->getDispatcher() == blinkTimer) {
- if(hasSelection || draggingSelection) {
- blinkerRect->visible = false;
- } else {
- if(hasFocus)
- blinkerRect->visible = !blinkerRect->visible;
- else
- blinkerRect->visible = false;
- }
- }
-
- }
|