| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // 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 "console/consoleTypes.h"
- #include "console/console.h"
- #include "graphics/gColor.h"
- #include "graphics/dgl.h"
- #include "gui/guiCanvas.h"
- #include "gui/guiMLTextCtrl.h"
- #include "gui/guiTextEditCtrl.h"
- #include "gui/guiDefaultControlRender.h"
- #include "memory/frameAllocator.h"
- #include "string/unicode.h"
- #include <vector>
- #include <string>
- #include "guiTextEditCtrl_ScriptBinding.h"
- #pragma region GuiTextEditTextBlock
- GuiTextEditTextBlock::GuiTextEditTextBlock()
- {
- mGlobalBounds.set(0, 0, 0, 0);
- mTextOffsetX = 0;
- mTextScrollX = 0;
- mText = string();
- mLineStartIbeamValue = 0;
- }
- void GuiTextEditTextBlock::render(const RectI& bounds, string line, U32 ibeamStartValue, GuiControlProfile* profile, GuiControlState currentState, GuiTextEditSelection& selector)
- {
- mGlobalBounds.set(bounds.point, bounds.extent);
- mText.assign(line);
- mLineStartIbeamValue = ibeamStartValue;
- RectI clipRect = dglGetClipRect();
- if (mGlobalBounds.overlaps(clipRect))
- {
- const U32 selStart = selector.getSelStart();
- const U32 selEnd = selector.getSelEnd();
- U32 lengthOfPreBlockText = mClamp(selStart - mLineStartIbeamValue, 0, line.length());
- U32 lengthOfPostBlockText = mClamp(line.length() - (selEnd - mLineStartIbeamValue), 0, line.length());
- U32 lengthOfHighlightBlockText = mClamp(line.length() - (lengthOfPreBlockText + lengthOfPostBlockText), 0, line.length());
- processTextAlignment(line, profile);
- Point2I movingStartPoint = getGlobalTextStart();
- movingStartPoint.x += renderTextSection(movingStartPoint, 0, lengthOfPreBlockText, profile, currentState);
- movingStartPoint.x += renderTextSection(movingStartPoint, lengthOfPreBlockText, lengthOfHighlightBlockText, profile, currentState, true);
- movingStartPoint.x += renderTextSection(movingStartPoint, lengthOfPreBlockText + lengthOfHighlightBlockText, lengthOfPostBlockText, profile, currentState);
- }
- Point2I textStartPoint = getGlobalTextStart();
- if (selector.renderIbeam(textStartPoint, mGlobalBounds.extent, line, mLineStartIbeamValue, mLineStartIbeamValue + line.length(), profile))
- {
- Point2I cursorCenter = selector.getCursorCenter();
- performScrollJumpX(cursorCenter.x, clipRect.point.x, clipRect.point.x + clipRect.extent.x);
- }
- }
- U32 GuiTextEditTextBlock::renderTextSection(const Point2I& startPoint, const U32 subStrStart, const U32 subStrLen, GuiControlProfile* profile, const GuiControlState currentState, bool isSelectedText)
- {
- if (subStrLen != 0)
- {
- string sectionText = mText.substr(subStrStart, subStrLen);
- U32 blockStrWidth = profile->mFont->getStrWidth(sectionText.c_str());
- Point2I pointToDraw = Point2I(startPoint.x, startPoint.y);
- if (isSelectedText)
- {
- dglSetBitmapModulation(profile->mFontColorTextSL);
- RectI highlightRect = RectI(pointToDraw.x, pointToDraw.y, blockStrWidth, mGlobalBounds.extent.y);
- dglDrawRectFill(highlightRect, profile->mFillColorTextSL);
- }
- else
- {
- const ColorI& fontColor = profile->getFontColor(currentState);
- dglSetBitmapModulation(fontColor);
- }
- dglDrawText(profile->mFont, pointToDraw, sectionText.c_str(), profile->mFontColors);
- return blockStrWidth;
- }
- return 0;
- }
- void GuiTextEditTextBlock::performScrollJumpX(const S32 targetX, const S32 areaStart, const S32 areaEnd)
- {
- S32 diff = 0;
- if (targetX < areaStart)
- {
- diff = targetX - areaStart;
- }
- else if (targetX > areaEnd)
- {
- diff = targetX - areaEnd;
- }
- mTextScrollX += diff;
- }
- U32 GuiTextEditTextBlock::calculateIbeamPositionInLine(const S32 targetX, GuiControlProfile* profile)
- {
- if (mText.length() == 0)
- return mLineStartIbeamValue;
- S32 curX = getGlobalTextStart().x;
- U32 result = mText.length();
- for (U32 count = 0; count < mText.length(); count++)
- {
- char c = mText[count];
- if (!profile->mFont->isValidChar(c))
- continue;
- S32 backDiff = mAbs(curX - targetX);
- curX += profile->mFont->getCharXIncrement(c);
- if (curX > targetX)
- {
- S32 forwardDiff = mAbs(curX - targetX);
- if (backDiff < forwardDiff)
- result = count;
- else
- result = count + 1;
- break;
- }
- }
- return result;
- }
- void GuiTextEditTextBlock::processScrollVelocity(const S32 delta, const S32 extentX, GuiControlProfile* profile)
- {
- U32 max = profile->mFont->getStrWidth(mText.c_str()) - extentX;
- mTextScrollX = mClamp(mTextScrollX + delta, 0, max);
- }
- void GuiTextEditTextBlock::processTextAlignment(const string line, GuiControlProfile* profile)
- {
- if (profile->mAlignment == GuiControlProfile::AlignmentType::LeftAlign ||
- mGlobalBounds.extent.x < profile->mFont->getStrWidth(line.c_str()))
- {
- mTextOffsetX = 0;
- }
- else if (profile->mAlignment == GuiControlProfile::AlignmentType::RightAlign)
- {
- mTextOffsetX = mGlobalBounds.extent.x - profile->mFont->getStrWidth(line.c_str());
- }
- else if (profile->mAlignment == GuiControlProfile::AlignmentType::CenterAlign)
- {
- mTextOffsetX = (S32)mRound((mGlobalBounds.extent.x - profile->mFont->getStrWidth(line.c_str())) / 2);
- }
- }
- #pragma endregion
- #pragma region GuiTextEditSelection
- GuiTextEditSelection::GuiTextEditSelection()
- {
- mBlockStart = 0;
- mBlockEnd = 0;
- mCursorPos = 0;
- mCursorOn = false;
- mNumFramesElapsed = 0;
- mCursorAtEOL = false;
- mIsFirstResponder = false;
- mGlobalUnadjustedCursorRect.set(0, 0, 0, 0);
- mCursorRendered = false;
- mNumFramesElapsed = 0;
- mTimeLastCursorFlipped = 0;
- mCursorOn = false;
- }
- bool GuiTextEditSelection::renderIbeam(const Point2I& startPoint, const Point2I& extent, const string line, const U32 start, const U32 end, GuiControlProfile* profile)
- {
- if (!mIsFirstResponder || !mCursorOn ||
- (mCursorAtEOL && mCursorPos == start && mCursorPos != 0) ||
- (!mCursorAtEOL && mCursorPos == end && mCursorPos != mTextLength) ||
- (mCursorPos < start || mCursorPos > end))
- {
- return false;
- }
- string blockText = line.substr(0, mCursorPos - start);
- U32 blockStrWidth = profile->mFont->getStrWidth(blockText.c_str());
- RectI ibeamRect = RectI(startPoint.x + blockStrWidth - 1, startPoint.y, 2, extent.y);
- setCursorRect(ibeamRect);
- RectI clipRect = dglGetClipRect();
- if (clipRect.point.x > ibeamRect.point.x)
- {
- ibeamRect.point.x = clipRect.point.x;
- }
- else if ((clipRect.point.x + clipRect.extent.x) < (ibeamRect.point.x + ibeamRect.extent.x))
- {
- ibeamRect.point.x = (clipRect.point.x + clipRect.extent.x) - ibeamRect.extent.x;
- }
- dglDrawRectFill(ibeamRect, profile->mCursorColor);
- return true;
- }
- void GuiTextEditSelection::selectTo(const U32 target)
- {
- S32 safeTarget = mClamp(target, 0, mTextLength);
- if (mBlockStart == mBlockEnd)
- {
- mBlockAnchor = mBlockStart = mBlockEnd = mCursorPos;
- }
- if (safeTarget > mBlockAnchor)
- {
- mBlockStart = mBlockAnchor;
- mBlockEnd = mCursorPos = safeTarget;
- }
- else if (safeTarget < mBlockAnchor)
- {
- mBlockStart = mCursorPos = safeTarget;
- mBlockEnd = mBlockAnchor;
- }
- else
- {
- mCursorPos = mBlockStart = mBlockEnd = safeTarget;
- }
- }
- void GuiTextEditSelection::eraseSelection(string& fullText)
- {
- if (hasSelection())
- {
- fullText.erase(mBlockStart, mBlockEnd - mBlockStart);
- mCursorPos = mBlockStart;
- mTextLength = fullText.length();
- clearSelection();
- }
- }
- void GuiTextEditSelection::onPreRender(const U32 time)
- {
- if (mIsFirstResponder)
- {
- U32 timeElapsed = time - mTimeLastCursorFlipped;
- mNumFramesElapsed++;
- if ((timeElapsed > 400) && (mNumFramesElapsed > 3))
- {
- mCursorOn = !mCursorOn;
- mTimeLastCursorFlipped = time;
- mNumFramesElapsed = 0;
- }
- }
- }
- void GuiTextEditSelection::resetCursorBlink()
- {
- mCursorOn = true;
- mNumFramesElapsed = 0;
- mTimeLastCursorFlipped = Platform::getVirtualMilliseconds();
- }
- void GuiTextEditSelection::selectWholeWord(const string& text)
- {
- bool selectingSpace = (mCursorPos < text.length() && text[mCursorPos] == ' ');
- for (S32 i = mCursorPos; i >= 0; i--)
- {
- if (i == 0)
- {
- mBlockStart = 0;
- break;
- }
- if ((!selectingSpace && text[i - 1] == ' ') ||
- (selectingSpace && text[i - 1] != ' '))
- {
- mBlockStart = i;
- break;
- }
- }
- mBlockAnchor = mBlockStart;
- bool foundSpace = false;
- for (S32 j = mCursorPos; j <= (text.length() + 1); j++)
- {
- if (j == (text.length() + 1))
- {
- mBlockEnd = j;
- break;
- }
- if (!foundSpace && text[j] == ' ')
- {
- foundSpace = true;
- }
- else if (foundSpace && text[j] != ' ')
- {
- mBlockEnd = j;
- break;
- }
- }
- mCursorPos = mBlockEnd;
- mCursorAtEOL = true;
- }
- GuiTextEditSelection::GuiTextEditSelection(const GuiTextEditSelection& selector)
- {
- mBlockAnchor = selector.mBlockAnchor;
- mBlockStart = selector.mBlockStart;
- mBlockEnd = selector.mBlockEnd;
- mCursorPos = selector.mCursorPos;
- mCursorAtEOL = selector.mCursorAtEOL;
- mIsFirstResponder = selector.mIsFirstResponder;
- mGlobalUnadjustedCursorRect = selector.mGlobalUnadjustedCursorRect;
- mCursorRendered = selector.mCursorRendered;
- mTextLength = selector.mTextLength;
- mNumFramesElapsed = 0;
- mTimeLastCursorFlipped = Platform::getVirtualMilliseconds();
- mCursorOn = true;
- }
- void GuiTextEditSelection::stepCursorForward()
- {
- if (hasSelection())
- {
- setCursorPosition(mBlockEnd);
- }
- else
- {
- setCursorPosition(mCursorPos + 1);
- }
- }
- void GuiTextEditSelection::stepCursorBackward()
- {
- if (hasSelection())
- {
- setCursorPosition(mBlockStart);
- }
- else
- {
- setCursorPosition(mCursorPos - 1);
- }
- }
- #pragma endregion
- #pragma region GuiTextEditCtrl
- IMPLEMENT_CONOBJECT(GuiTextEditCtrl);
- U32 GuiTextEditCtrl::smNumAwake = 0;
- GuiTextEditCtrl::GuiTextEditCtrl()
- {
- mInsertOn = true;
- mMouseOver = false;
- mPasswordText = false;
- mReturnCausesTab = false;
- mSinkAllKeyEvents = false;
- mActive = true;
- mSelector = GuiTextEditSelection();
- mUndoSelector = GuiTextEditSelection();
- mUndoText = string();
- mTextOffsetY = 0;
- mEscapeCommand = StringTable->EmptyString;
- mPasswordMask = StringTable->insert( "*" );
- mEditCursor = NULL;
- mMaxStrLen = MAX_STRING_LENGTH;
- mInputMode = AllText;
- mSuspendVerticalScrollJump = false;
- mTextBlockList = TextBlockList();
- }
- static EnumTable::Enums inputModeEnums[] =
- {
- { GuiTextEditCtrl::AllText, "AllText" },
- { GuiTextEditCtrl::Decimal, "Decimal" },
- { GuiTextEditCtrl::Number, "Number" },
- { GuiTextEditCtrl::Alpha, "Alpha" },
- { GuiTextEditCtrl::AlphaNumeric, "AlphaNumeric" }
- };
- static EnumTable gInputModeTable(5, &inputModeEnums[0]);
- void GuiTextEditCtrl::initPersistFields()
- {
- Parent::initPersistFields();
- addDepricatedField("validate");
- addDepricatedField("truncate");
- addDepricatedField("passwordMask");
- addDepricatedField("historySize");
- addDepricatedField("tabComplete");
- addGroup("Text Edit");
- addField("escapeCommand", TypeString, Offset(mEscapeCommand, GuiTextEditCtrl));
- addField("sinkAllKeyEvents", TypeBool, Offset(mSinkAllKeyEvents, GuiTextEditCtrl));
- addField("password", TypeBool, Offset(mPasswordText, GuiTextEditCtrl));
- addField("returnCausesTab", TypeBool, Offset(mReturnCausesTab, GuiTextEditCtrl));
- addProtectedField("maxLength", TypeS32, Offset(mMaxStrLen, GuiTextEditCtrl), &setMaxLengthProperty, &defaultProtectedGetFn, "The max number of characters that can be entered into the text edit box.");
- addProtectedField("inputMode", TypeEnum, Offset(mInputMode, GuiTextEditCtrl), &setInputMode, &getInputMode, &writeInputMode, 1, &gInputModeTable, "InputMode allows different characters to be entered.");
- addField("editCursor", TypeGuiCursor, Offset(mEditCursor, GuiTextEditCtrl));
- endGroup("Text Edit");
- }
- bool GuiTextEditCtrl::onAdd()
- {
- if ( ! Parent::onAdd() )
- return false;
- if( mText[0] )
- {
- setText(mText);
- }
- return true;
- }
- void GuiTextEditCtrl::onStaticModified(const char* slotName)
- {
- if(!dStricmp(slotName, "text"))
- setText(mText);
- }
- void GuiTextEditCtrl::inspectPostApply()
- {
- Parent::inspectPostApply();
- if (mTextID && *mTextID != 0)
- setTextID(mTextID);
- else
- setText(mText);
- }
- bool GuiTextEditCtrl::onWake()
- {
- if (! Parent::onWake())
- return false;
- if (mConsoleVariable[0])
- {
- const char *txt = Con::getVariable(mConsoleVariable);
- if (txt)
- {
- if (dStrlen(txt) > (U32)mMaxStrLen)
- {
- char* buf = new char[mMaxStrLen + 1];
- dStrncpy(buf, txt, mMaxStrLen);
- buf[mMaxStrLen] = 0;
- setScriptValue(buf);
- delete[] buf;
- }
- else
- setScriptValue(txt);
- }
- }
- // If this is the first awake text edit control, enable keyboard translation
- if (smNumAwake == 0)
- Platform::enableKeyboardTranslation();
- ++smNumAwake;
- mSuspendVerticalScrollJump = false;
- return true;
- }
- void GuiTextEditCtrl::onSleep()
- {
- Parent::onSleep();
- // If this is the last awake text edit control, disable keyboard translation
- --smNumAwake;
- if (smNumAwake == 0)
- Platform::disableKeyboardTranslation();
- }
- void GuiTextEditCtrl::execConsoleCallback()
- {
- setVariable(mTextBuffer.c_str());
- if ( mConsoleCommand[0] )
- {
- Con::evaluate( mConsoleCommand, false );
- }
- }
- const char* GuiTextEditCtrl::getText()
- {
- return mTextBuffer.c_str();
- }
-
- void GuiTextEditCtrl::setText( const UTF8 *txt )
- {
- setUpdate();
- enforceMaxLength();
- if (txt && txt[0] != 0)
- {
- Parent::setText(txt);
- mTextBuffer.assign(txt);
- }
- else
- mTextBuffer.clear();
- setVariable(mTextBuffer.c_str());
- }
- void GuiTextEditCtrl::setText( const UTF16* txt)
- {
- if(txt && txt[0] != 0)
- {
- UTF8* txt8 = convertUTF16toUTF8( txt );
- setText(txt8);
- delete[] txt8;
- }
- else
- {
- setText("");
- }
- }
- void GuiTextEditCtrl::enforceMaxLength()
- {
- int diff = mTextBuffer.length() - mMaxStrLen;
- if (diff > 0) {
- mTextBuffer.resize(mMaxStrLen);
- }
- }
- void GuiTextEditCtrl::setTextID(const char *id)
- {
- S32 n = Con::getIntVariable(id, -1);
- if (n != -1)
- {
- setTextID(n);
- }
- }
- void GuiTextEditCtrl::setTextID(S32 id)
- {
- const UTF8 *str = getGUIString(id);
- if (str)
- setText((const char*)str);
- }
- bool GuiTextEditCtrl::validate()
- {
- bool valid = true;
- if (isMethod("onValidate"))
- {
- valid = dAtob(Con::executef(this, 2, "onValidate"));
- }
- return valid;
- }
- const RectI GuiTextEditCtrl::getGlobalInnerRect()
- {
- Point2I offset = Point2I(mBounds.point.Zero);
- Point2I extent = Point2I(getExtent());
- RectI innerRect = getInnerRect(offset, extent, SelectedState, mProfile);
- Point2I globalCtrlOffset = localToGlobalCoord(innerRect.point);
- RectI globalInnerRect(globalCtrlOffset, innerRect.extent);
- return globalInnerRect;
- }
- S32 GuiTextEditCtrl::calculateIbeamPosition(const Point2I& globalMousePoint)
- {
- if (mTextBuffer.length() == 0)
- return 0;
- RectI globalInnerRect = getGlobalInnerRect();
- return calculateIbeamPosition(globalMousePoint, globalInnerRect);
- }
- S32 GuiTextEditCtrl::calculateIbeamPosition(const Point2I& globalMousePoint, const RectI& globalInnerRect)
- {
- if (mTextBuffer.length() == 0 || mTextBlockList.size() == 0)
- return 0;
- string textBuffer = applyPasswordMasking();
- if (!mTextWrap)
- {
- return mTextBlockList.front().calculateIbeamPositionInLine(globalMousePoint.x, mProfile);
- }
- else
- {
- RectI firstLineBounds = mTextBlockList.front().getGlobalBounds();
- S32 textStartY = firstLineBounds.point.y;
- if (textStartY > globalMousePoint.y)
- return 0;
- U32 height = firstLineBounds.extent.y;
- if ((textStartY + (mTextBlockList.size() * height)) < globalMousePoint.y)
- return mTextBuffer.length();
- S32 curY = textStartY;
- for (auto block : mTextBlockList)
- {
- curY += height;
- if (curY > globalMousePoint.y)
- {
- U32 pos = block.calculateIbeamPositionInLine(globalMousePoint.x, mProfile);
- mSelector.setCursorAtEOL(block.calculateCursorAtEOL(pos));
- return pos + block.getStartValue();
- }
- }
- mSelector.setCursorAtEOL(true);
- return mTextBlockList.back().calculateIbeamPositionInLine(globalMousePoint.x, mProfile);
- }
- }
- void GuiTextEditCtrl::onTouchDown( const GuiEvent &event )
- {
- mScrollVelocity = 0;
- mSuspendVerticalScrollJump = false;
- if (!mVisible || !mAwake)
- return;
- mSelector.setTextLength(mTextBuffer.length());
- if(event.mouseClickCount > 2)
- {
- selectAllText();
- }
- else if(event.mouseClickCount > 1)
- {
- mSelector.selectWholeWord(mTextBuffer);
- }
- else
- {
- S32 newCursorPos = calculateIbeamPosition(event.mousePoint);
- if (event.modifier & SI_SHIFT)
- {
- modifySelectBlock(newCursorPos);
- }
- else
- {
- mSelector.setCursorPosition(newCursorPos);
- }
- }
- mouseLock();
- setFirstResponder();
- mSelector.resetCursorBlink();
- if( isMethod("onTouchDown") )
- {
- char buf[3][32];
- dSprintf(buf[0], 32, "%d", event.modifier);
- dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
- dSprintf(buf[2], 32, "%d", event.mouseClickCount);
- Con::executef(this, 4, "onTouchDown", buf[0], buf[1], buf[2]);
- }
- }
- void GuiTextEditCtrl::onTouchDragged( const GuiEvent &event )
- {
- if (!mVisible || !mAwake)
- return;
- mSuspendVerticalScrollJump = false;
- RectI globalInnerRect = getGlobalInnerRect();
- adjustScrollVelocity(event.mousePoint, globalInnerRect);
-
- if ((mTextWrap && event.mousePoint.y < globalInnerRect.point.y) ||
- (!mTextWrap && event.mousePoint.x < globalInnerRect.point.x))
- {
- modifySelectBlock(0);
- }
- else if ((mTextWrap && event.mousePoint.y > (globalInnerRect.point.y + globalInnerRect.extent.y)) ||
- (!mTextWrap && event.mousePoint.x > (globalInnerRect.point.x + globalInnerRect.extent.x)))
- {
- modifySelectBlock(mTextBuffer.length());
- }
- else
- {
- modifySelectBlock(calculateIbeamPosition(event.mousePoint, globalInnerRect));
- }
- // Notify Script.
- if( isMethod("onTouchDragged") )
- {
- char buf[3][32];
- dSprintf(buf[0], 32, "%d", event.modifier);
- dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
- dSprintf(buf[2], 32, "%d", event.mouseClickCount);
- Con::executef(this, 4, "onTouchDragged", buf[0], buf[1], buf[2]);
- }
- }
- void GuiTextEditCtrl::onTouchUp(const GuiEvent &event)
- {
- mScrollVelocity = 0;
- mSuspendVerticalScrollJump = false;
- mouseUnlock();
- if (!mVisible || !mAwake)
- return;
- // Notify Script.
- if( isMethod("onTouchUp") )
- {
- char buf[3][32];
- dSprintf(buf[0], 32, "%d", event.modifier);
- dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
- dSprintf(buf[2], 32, "%d", event.mouseClickCount);
- Con::executef(this, 4, "onTouchUp", buf[0], buf[1], buf[2]);
- }
- }
- bool GuiTextEditCtrl::onMouseWheelUp(const GuiEvent& event)
- {
- if (!mVisible || !mAwake)
- return true;
- if(mTextWrap && mTextOffsetY > 0)
- {
- mScrollVelocity = 0;
- mSuspendVerticalScrollJump = true;
- mTextOffsetY = getMax(mTextOffsetY - static_cast<S32>(mProfile->mFont->getHeight()), 0);
- return true;
- }
- GuiControl* parent = getParent();
- if (parent)
- return parent->onMouseWheelUp(event);
- else
- return false;
- }
- bool GuiTextEditCtrl::onMouseWheelDown(const GuiEvent& event)
- {
- if (!mVisible || !mAwake)
- return true;
- U32 blockHeight = mTextBlockList.size() * mProfile->mFont->getHeight();
- RectI innerRect = getGlobalInnerRect();
- S32 max = blockHeight - innerRect.extent.y;
- if (mTextWrap && innerRect.extent.y < blockHeight && mTextOffsetY < max)
- {
- mScrollVelocity = 0;
- mSuspendVerticalScrollJump = true;
- mTextOffsetY = getMin(mTextOffsetY + static_cast<S32>(mProfile->mFont->getHeight()), max);
- return true;
- }
- GuiControl* parent = getParent();
- if (parent)
- return parent->onMouseWheelDown(event);
- else
- return false;
- }
- void GuiTextEditCtrl::onTouchEnter(const GuiEvent& event)
- {
- if (!mActive)
- return;
- mMouseOver = true;
- Con::executef(this, 1, "onTouchEnter");
- //update
- setUpdate();
- }
- void GuiTextEditCtrl::onTouchLeave(const GuiEvent& event)
- {
- if (!mActive)
- return;
- mMouseOver = false;
- Con::executef(this, 1, "onTouchLeave");
- //update
- setUpdate();
- }
- void GuiTextEditCtrl::saveUndoState()
- {
- //save the current state
- mUndoText = mTextBuffer;
- mUndoSelector = mSelector;
- }
- void GuiTextEditCtrl::onCopy(bool andCut)
- {
- // Don't copy/cut password field!
- if(mPasswordText)
- return;
- if (mSelector.hasSelection())
- {
- //save the current state
- saveUndoState();
- //copy the text to the clipboard
- string subString = mSelector.getSelection(mTextBuffer);
- Platform::setClipboard(subString.c_str());
- //if we pressed the cut shortcut, we need to cut the selected text from the control...
- if (andCut)
- {
- mSelector.eraseSelection(mTextBuffer);
- }
- mSelector.clearSelection();
- }
- }
- void GuiTextEditCtrl::onPaste()
- {
- //first, make sure there's something in the clipboard to copy...
- string clipboard = Platform::getClipboard();
- if(clipboard.length() <= 0)
- return;
- //save the current state
- saveUndoState();
- //delete anything hilited
- if (mSelector.hasSelection())
- {
- mSelector.eraseSelection(mTextBuffer);
- mSelector.clearSelection();
- }
- U32 pos = mSelector.getCursorPos();
- mTextBuffer.insert(pos, clipboard);
- setText(mTextBuffer);
- pos += clipboard.length();
- mSelector.setCursorPosition(pos);
- execConsoleCallback();
- }
- void GuiTextEditCtrl::onUndo()
- {
- string tempText = mTextBuffer;
- GuiTextEditSelection tempSelector = mSelector;
- mTextBuffer = mUndoText;
- mSelector = mUndoSelector;
- mUndoText = tempText;
- mUndoSelector = tempSelector;
- }
- bool GuiTextEditCtrl::onKeyDown(const GuiEvent &event)
- {
- if(! isActive())
- return false;
- mSuspendVerticalScrollJump = false;
- S32 stringLen = mTextBuffer.length();
- mSelector.setTextLength(stringLen);
- setUpdate();
- bool result = false;
- if (event.modifier & SI_SHIFT)
- {
- result = handleKeyDownWithShift(event);
- }
- else if (event.modifier & SI_CTRL)
- {
- //When holding the ctrl key, events must be handled here or passed up.
- return handleKeyDownWithCtrl(event);
- }
- else if (event.modifier & SI_ALT)
- {
- result = handleKeyDownWithAlt(event);
- #if (defined(TORQUE_OS_OSX) || defined(TORQUE_OS_IOS))
- //Likewise, the cmd key must be handled here or passed up.
- return result;
- #endif
- }
- if(result || (!result && handleKeyDownWithNoModifier(event)))
- {
- return true;
- }
- if (handleCharacterInput(event) || mSinkAllKeyEvents)
- {
- return true;
- }
- return Parent::onKeyDown( event );
- }
- bool GuiTextEditCtrl::tabNext()
- {
- if (isMethod("onTab"))
- Con::executef(this, 2, "onTab", "0");
- GuiCanvas *root = getRoot();
- if (root)
- {
- root->tabNext();
- return true;
- }
- return false;
- }
- bool GuiTextEditCtrl::tabPrev()
- {
- if (isMethod("onTab"))
- Con::executef(this, 2, "onTab", "1");
- GuiCanvas *root = getRoot();
- if (root)
- {
- root->tabPrev();
- return true;
- }
- return false;
- }
- void GuiTextEditCtrl::setFirstResponder()
- {
- mSelector.setFirstResponder(true);
- Parent::setFirstResponder();
-
- #if !defined(TORQUE_OS_IOS) && !defined(TORQUE_OS_ANDROID)
- Platform::enableKeyboardTranslation();
- #endif
- }
- void GuiTextEditCtrl::onLoseFirstResponder()
- {
- Platform::disableKeyboardTranslation();
- //execute the validate command
- bool valid = validate();
- if (valid)
- {
- execAltConsoleCallback();
- }
- if( isMethod( "onLoseFirstResponder" ) )
- Con::executef( this, 2, "onLoseFirstResponder", valid);
- mSelector.setFirstResponder(false);
- mTextOffsetY = 0;
- mScrollVelocity = 0;
- if (!mTextWrap && mTextBlockList.size() > 0)
- {
- mTextBlockList.front().resetScroll();
- }
- // Redraw the control:
- setUpdate();
- }
- void GuiTextEditCtrl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
- {
- Parent::parentResized( oldParentExtent, newParentExtent );
- mTextOffsetY = 0;
- if (!mTextWrap && mTextBlockList.size() > 0)
- {
- mTextBlockList.front().resetScroll();
- }
- }
- GuiControlState GuiTextEditCtrl::getCurrentState()
- {
- if (!mActive)
- return GuiControlState::DisabledState;
- else if (isFirstResponder())
- return GuiControlState::SelectedState;
- else if (mMouseOver)
- return GuiControlState::HighlightState;
- else
- return GuiControlState::NormalState;
- }
- const ColorI& GuiTextEditCtrl::getCurrentFontColor()
- {
- auto currentState = getCurrentState();
- return mProfile->getFontColor(currentState);
- }
- void GuiTextEditCtrl::onPreRender()
- {
- mSelector.onPreRender(Platform::getVirtualMilliseconds());
- processScrollVelocity();
- }
- void GuiTextEditCtrl::onRender(Point2I offset, const RectI & updateRect)
- {
- GuiControlState currentState = getCurrentState();
- RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
- renderUniversalRect(ctrlRect, mProfile, currentState);
- //Render Text
- RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
- RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
- if (contentRect.isValidRect())
- {
- if (currentState != SelectedState)
- mSelector.clearSelection();
-
- string textBuffer = applyPasswordMasking();
- renderText(contentRect.point, contentRect.extent, textBuffer.c_str(), mProfile);
- //Render the childen
- renderChildControls(offset, contentRect, updateRect);
- }
- }
- void GuiTextEditCtrl::renderLineList(const Point2I& offset, const Point2I& extent, const S32 startOffsetY, const vector<string> lineList, GuiControlProfile* profile, const TextRotationOptions rot)
- {
- const S32 textHeight = profile->mFont->getHeight();
- S32 totalWidth = extent.x;
- if (mTextBlockList.size() > lineList.size())
- {
- mTextBlockList.resize(lineList.size());
- }
- //Now print each line
- U32 ibeamPos = 0;
- S32 offsetY = startOffsetY - mTextOffsetY;
- for (U32 i = 0; i < lineList.size(); i++)
- {
- if(mTextBlockList.size() <= i)
- {
- mTextBlockList.push_back(GuiTextEditTextBlock());
- }
- Point2I start = Point2I(0, offsetY);
- Point2I blockExtent = Point2I(extent.x, textHeight);
- RectI blockBounds = RectI(start + offset + profile->mTextOffset, blockExtent);
- mTextBlockList[i].render(blockBounds, lineList[i], ibeamPos, mProfile, getCurrentState(), mSelector);
- offsetY += textHeight;
- ibeamPos += lineList[i].length();
- }
- performScrollJumpY();
- }
- void GuiTextEditCtrl::processScrollVelocity()
- {
- if (mScrollVelocity == 0)
- {
- return;
- }
- U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastScrollProcess;
- S32 delta = mRound((F32)(timeElapsed * mScrollVelocity) / 1000);
- if (delta != 0)
- {
- RectI innerRect = getGlobalInnerRect();
- if (mTextWrap)
- {
- U32 max = (mTextBlockList.size() * mProfile->mFont->getHeight()) - innerRect.extent.y;
- mTextOffsetY = mClamp(mTextOffsetY + delta, 0, max);
- }
- else if(mTextBlockList.size() > 0)
- {
- mTextBlockList.front().processScrollVelocity(delta, innerRect.extent.x, mProfile);
- }
- mTimeLastScrollProcess = Platform::getVirtualMilliseconds();
- S32 newCursorPos = calculateIbeamPosition(Canvas->getCursorPos());
- modifySelectBlock(newCursorPos);
- }
- }
- void GuiTextEditCtrl::performScrollJumpY()
- {
- if (mTextWrap && !mSuspendVerticalScrollJump && isFirstResponder() && mSelector.isCursorRendered())
- {
- RectI clipRect = dglGetClipRect();
- S32 areaStart = clipRect.point.y;
- S32 areaEnd = clipRect.point.y + clipRect.extent.y;
- RectI cursorRect = mSelector.getCursorRect();
- S32 lineTop = cursorRect.point.y;
- S32 lineBottom = lineTop + cursorRect.extent.y;
- S32 diff = 0;
- if (lineTop < areaStart)
- {
- diff = lineTop - areaStart;
- }
- else if (lineBottom > areaEnd)
- {
- diff = lineBottom - areaEnd;
- }
- mTextOffsetY += diff;
- }
- }
- void GuiTextEditCtrl::adjustScrollVelocity(const Point2I& globalMousePoint, const RectI& globalInnerRect)
- {
- RectI nonScrollArea = RectI(globalInnerRect);
- if (mTextWrap)
- {
- nonScrollArea.point.y += SCROLL_EDGE_SIZE;
- nonScrollArea.extent.y -= (2 * SCROLL_EDGE_SIZE);
- }
- else
- {
- nonScrollArea.point.x += SCROLL_EDGE_SIZE;
- nonScrollArea.extent.x -= (2 * SCROLL_EDGE_SIZE);
- }
- if (nonScrollArea.pointInRect(globalMousePoint))
- {
- mScrollVelocity = 0;
- }
- else if (globalInnerRect.pointInRect(globalMousePoint))
- {
- if (mScrollVelocity == 0)
- {
- mTimeLastScrollProcess = Platform::getVirtualMilliseconds();
- }
- mScrollVelocity = SCROLL_VELOCITY_PER_SEC;
- if ((mTextWrap && globalMousePoint.y < (globalInnerRect.point.y + SCROLL_EDGE_SIZE)) ||
- (!mTextWrap && globalMousePoint.x < (globalInnerRect.point.x + SCROLL_EDGE_SIZE)))
- {
- mScrollVelocity = -SCROLL_VELOCITY_PER_SEC;
- }
- }
- else
- {
- mScrollVelocity = 0;
- }
- }
- bool GuiTextEditCtrl::hasText()
- {
- return (mTextBuffer.length());
- }
- void GuiTextEditCtrl::keyDenied()
- {
- if (isMethod("onDenied"))
- Con::executef(this, 1, "onDenied");
- }
- const char *GuiTextEditCtrl::getScriptValue()
- {
- return StringTable->insert(mTextBuffer.c_str());
- }
- void GuiTextEditCtrl::setScriptValue(const char *value)
- {
- mTextBuffer.assign(value);
- mSelector.setTextLength(mTextBuffer.length());
- }
- GuiTextEditCtrl::InputMode GuiTextEditCtrl::getInputModeEnum(const char* label)
- {
- // Search for Mnemonic.
- for (U32 i = 0; i < (sizeof(inputModeEnums) / sizeof(EnumTable::Enums)); i++)
- {
- if (dStricmp(inputModeEnums[i].label, label) == 0)
- return (InputMode)inputModeEnums[i].index;
- }
- // Warn.
- Con::warnf("GuiTextEditCtrl::getInputModeEnum() - Invalid mode of '%s'", label);
- return (InputMode)-1;
- }
- const char* GuiTextEditCtrl::getInputModeDescription(const InputMode mode)
- {
- // Search for Mnemonic.
- for (U32 i = 0; i < (sizeof(inputModeEnums) / sizeof(EnumTable::Enums)); i++)
- {
- if (inputModeEnums[i].index == mode)
- return inputModeEnums[i].label;
- }
- // Warn.
- Con::warnf("GuiTextEditCtrl::getInputModeDescription() - Invalid input mode.");
- return StringTable->EmptyString;
- }
- //Returns true if valid, false if invalid
- bool GuiTextEditCtrl::inputModeValidate(const U16 key, S32 cursorPos)
- {
- if (key == '-')
- {
- if (mInputMode == Alpha || mInputMode == AlphaNumeric)
- {
- return false;
- }
- else if (mInputMode == Decimal || mInputMode == Number)
- {
- //a minus sign only exists at the beginning, and only a single minus sign
- if (cursorPos != 0 || (mInsertOn && mTextBuffer[0] == '-'))
- {
- return false;
- }
- }
- }
- else if (key >= '0' && key <= '9')
- {
- if (mInputMode == Alpha)
- {
- return false;
- }
- }
- else if (key == '.')
- {
- if (mInputMode == Number || mInputMode == Alpha || mInputMode == AlphaNumeric)
- {
- return false;
- }
- else if (mInputMode == Decimal)
- {
- if (!mInsertOn && mTextBuffer[cursorPos] == '.')
- {
- return true;
- }
- const char* dot = dStrchr(mTextBuffer.c_str(), '.');
- if (dot != NULL)
- {
- return false;
- }
- }
- }
- else if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
- {
- if (mInputMode == Decimal || mInputMode == Number)
- {
- return false;
- }
- }
- else if (key == 32)
- {
- if (mInputMode == Decimal || mInputMode == Number)
- {
- return false;
- }
- }
- else if (mInputMode == Decimal || mInputMode == Number || mInputMode == Alpha || mInputMode == AlphaNumeric)
- {
- //The remaining characters only go with AllText
- return false;
- }
- //Looks like we have a valid character!
- return true;
- }
- void GuiTextEditCtrl::setMaxLength(S32 max)
- {
- mMaxStrLen = getMax(1, getMin(max, MAX_STRING_LENGTH));
- }
- void GuiTextEditCtrl::setInputMode(const InputMode mode)
- {
- if(mInputMode == mode)
- return;
- //Time to set the mode
- mInputMode = mode;
- //now let's parse that buffer and get rid of invalid characters
- if (mode != AllText)
- {
- bool oldInsert = mInsertOn;
- mInsertOn = false;
- for (S32 i = 0; i < MAX_STRING_LENGTH; i++)
- {
- const UTF16 character = mTextBuffer[i];
- if (character == '\0')
- {
- //Done and done.
- break;
- }
-
- if (!inputModeValidate(character, i))
- {
- //Bad Character! Let's remove it.
- mTextBuffer.erase(i, 1);
- //Step it back
- i--;
- }
- }
- mInsertOn = oldInsert;
- }
- }
- string GuiTextEditCtrl::applyPasswordMasking()
- {
- if (mPasswordText)
- {
- string passwordCover = string();
- passwordCover.resize(mTextBuffer.length(), mPasswordMask[0]);
- return passwordCover;
- }
- return mTextBuffer;
- }
- bool GuiTextEditCtrl::handleKeyDownWithShift(const GuiEvent& event)
- {
- switch (event.keyCode)
- {
- case KEY_TAB:
- return tabPrev();
- case KEY_HOME:
- mSelector.selectTo(0);
- return true;
- case KEY_END:
- mSelector.selectTo(mTextBuffer.length());
- return true;
- case KEY_LEFT:
- return handleShiftArrowKey(GuiDirection::Left);
- case KEY_RIGHT:
- return handleShiftArrowKey(GuiDirection::Right);
- case KEY_UP:
- return handleShiftArrowKey(GuiDirection::Up);
- case KEY_DOWN:
- return handleShiftArrowKey(GuiDirection::Down);
- }
- return false;
- }
- bool GuiTextEditCtrl::handleKeyDownWithCtrl(const GuiEvent& event)
- {
- switch (event.keyCode)
- {
- #if !(defined(TORQUE_OS_OSX) || defined(TORQUE_OS_IOS))
- // windows style cut / copy / paste / undo keybinds
- case KEY_C:
- case KEY_X:
- onCopy(event.keyCode == KEY_X);
- return true;
- case KEY_V:
- onPaste();
- return true;
- case KEY_Z:
- onUndo();
- return true;
- #endif
- case KEY_DELETE:
- case KEY_BACKSPACE:
- selectAllText();
- handleBackSpace();
- return true;
- }
- return false;
- }
- bool GuiTextEditCtrl::handleKeyDownWithAlt(const GuiEvent& event)
- {
- #if (defined(TORQUE_OS_OSX) || defined(TORQUE_OS_IOS))
- // Added Mac cut/copy/paste/undo keys
- // Mac command key maps to alt in torque.
- switch(event.keyCode)
- {
- case KEY_C:
- case KEY_X:
- onCopy( event.keyCode==KEY_X );
- return true;
-
- case KEY_V:
- onPaste();
- return true;
- case KEY_Z:
- onUndo();
- return true;
- }
- #endif
- return false;
- }
- bool GuiTextEditCtrl::handleKeyDownWithNoModifier(const GuiEvent& event)
- {
- switch (event.keyCode)
- {
- case KEY_TAB:
- return tabNext();
- case KEY_ESCAPE:
- if (isMethod(mEscapeCommand))
- {
- Con::evaluate(mEscapeCommand);
- return true;
- }
- return false;
- case KEY_RETURN:
- case KEY_NUMPADENTER:
- if (!validate())
- {
- return true;
- }
- return handleEnterKey();
- case KEY_LEFT:
- return handleArrowKey(GuiDirection::Left);
- case KEY_RIGHT:
- return handleArrowKey(GuiDirection::Right);
- case KEY_UP:
- return handleArrowKey(GuiDirection::Up);
- case KEY_DOWN:
- return handleArrowKey(GuiDirection::Down);
- case KEY_DELETE:
- return handleDelete();
- case KEY_BACKSPACE:
- return handleBackSpace();
- case KEY_INSERT:
- mInsertOn = !mInsertOn;
- return true;
- case KEY_HOME:
- mSelector.setCursorPosition(0);
- return true;
- case KEY_END:
- mSelector.setCursorPosition(mTextBuffer.length());
- return true;
- }
- return false;
- }
- bool GuiTextEditCtrl::handleCharacterInput(const GuiEvent& event)
- {
- if (mProfile->mFont.isNull())
- return false;
- if (mProfile->mFont->isValidChar(event.ascii))
- {
- // Get the character ready to add to a UTF8 string.
- string characterToInsert = string(1, event.ascii);
- //Stop characters that aren't allowed based on InputMode
- if (!inputModeValidate(event.ascii, mSelector.getCursorPos()))
- {
- keyDenied();
- return true;
- }
- saveUndoState();
- if (mSelector.hasSelection())
- {
- mSelector.eraseSelection(mTextBuffer);
- }
- if (mTextBuffer.length() < mMaxStrLen || !mInsertOn)
- {
- if (!mInsertOn)
- {
- mTextBuffer.erase(mSelector.getCursorPos(), 1);
- }
- mTextBuffer.insert(mSelector.getCursorPos(), characterToInsert);
- mSelector.setTextLength(mTextBuffer.length());
- mSelector.stepCursorForward();
- setText(mTextBuffer);
- }
- else
- keyDenied();
- execConsoleCallback();
- return true;
- }
- return false;
- }
- bool GuiTextEditCtrl::handleBackSpace()
- {
- if (mTextBuffer.length() == 0 || (mSelector.getCursorPos() == 0 && !mSelector.hasSelection()))
- return true;
- saveUndoState();
- if (mSelector.hasSelection())
- {
- mSelector.eraseSelection(mTextBuffer);
- }
- else
- {
- mSelector.stepCursorBackward();
- mTextBuffer.erase(mSelector.getCursorPos(), 1);
- }
- mSelector.setTextLength(mTextBuffer.length());
- setText(mTextBuffer);
- execConsoleCallback();
- return true;
- }
- bool GuiTextEditCtrl::handleDelete()
- {
- if (mTextBuffer.length() == mSelector.getCursorPos() && !mSelector.hasSelection())
- return true;
- saveUndoState();
- if (mSelector.hasSelection())
- {
- mSelector.eraseSelection(mTextBuffer);
- }
- else
- {
- mTextBuffer.erase(mSelector.getCursorPos(), 1);
- }
- mSelector.setTextLength(mTextBuffer.length());
- setText(mTextBuffer);
- execConsoleCallback();
- return true;
- }
- bool GuiTextEditCtrl::handleEnterKey()
- {
- execAltConsoleCallback();
- if (isMethod("onReturn"))
- Con::executef(this, 1, "onReturn");
- if (mReturnCausesTab)
- {
- tabNext();
- }
- return true;
- }
- bool GuiTextEditCtrl::handleArrowKey(GuiDirection direction)
- {
- if (direction == GuiDirection::Left)
- {
- mSelector.setCursorAtEOL(false);
- mSelector.stepCursorBackward();
- }
- else if (direction == GuiDirection::Right)
- {
- mSelector.setCursorAtEOL(false);
- mSelector.stepCursorForward();
- }
- else if (direction == GuiDirection::Up)
- {
- S32 newCursorPos = getLineAdjustedIbeamPosition(-mProfile->mFont->getHeight());
- if (newCursorPos == mSelector.getCursorPos())
- {
- newCursorPos = 0;
- }
- mSelector.setCursorPosition(newCursorPos);
- }
- else if (direction == GuiDirection::Down)
- {
- S32 newCursorPos = getLineAdjustedIbeamPosition(mProfile->mFont->getHeight());
- if (newCursorPos == mSelector.getCursorPos())
- {
- newCursorPos = mTextBuffer.length();
- }
- mSelector.setCursorPosition(newCursorPos);
- }
- setUpdate();
- mSelector.resetCursorBlink();
- return true;
- }
- bool GuiTextEditCtrl::handleShiftArrowKey(GuiDirection direction)
- {
- if (direction == GuiDirection::Left)
- {
- mSelector.setCursorAtEOL(false);
- modifySelectBlock(mSelector.getCursorPos() - 1);
- }
- else if (direction == GuiDirection::Right)
- {
- mSelector.setCursorAtEOL(false);
- modifySelectBlock(mSelector.getCursorPos() + 1);
- }
- else if (direction == GuiDirection::Up)
- {
- S32 newCursorPos = getLineAdjustedIbeamPosition(-mProfile->mFont->getHeight());
- modifySelectBlock(newCursorPos);
- }
- else if (direction == GuiDirection::Down)
- {
- S32 newCursorPos = getLineAdjustedIbeamPosition(mProfile->mFont->getHeight());
- modifySelectBlock(newCursorPos);
- }
- setUpdate();
- mSelector.resetCursorBlink();
- return true;
- }
- S32 GuiTextEditCtrl::getLineAdjustedIbeamPosition(S32 heightAdjustment)
- {
- Point2I centerPoint = mSelector.getCursorCenter();
- centerPoint.y += heightAdjustment;
- return calculateIbeamPosition(centerPoint);
- }
- void GuiTextEditCtrl::modifySelectBlock(const U32 target)
- {
- mSelector.setTextLength(mTextBuffer.length());
- mSelector.selectTo(target);
- setUpdate();
- }
- void GuiTextEditCtrl::selectAllText()
- {
- mSelector.setCursorPosition(0);
- modifySelectBlock(mTextBuffer.length());
- }
- void GuiTextEditCtrl::getCursor(GuiCursor*& cursor, bool& showCursor, const GuiEvent& lastGuiEvent)
- {
- if (mEditCursor == NULL)
- {
- SimObject* obj;
- obj = Sim::findObject("EditCursor");
- mEditCursor = dynamic_cast<GuiCursor*>(obj);
- }
- if (mEditCursor != NULL)
- {
- cursor = mEditCursor;
- }
- }
- #pragma endregion
|