The code I add to irrlicht is as follows:
IGUIEditBox.h:
Code: Select all
virtual void SetPassword(bool use, wchar_t chr = L'*') = 0;
Code: Select all
public:
//...
virtual void SetPassword(bool use, wchar_t chr = '*');
private:
//...
bool password;
wchar_t PWChar;
Code: Select all
CGUIEditBox::CGUIEditBox(const wchar_t* text, bool border, IGUIEnvironment* environment,
IGUIElement* parent, s32 id,
const core::rect<s32>& rectangle, IOSOperator* op)
: IGUIEditBox(environment, parent, id, rectangle), MouseMarking(false),
Border(border), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
OverrideColor(video::SColor(101,255,255,255)),
OverrideFont(0), Operator(op), CursorPos(0), ScrollPos(0), Max(0), password(false), PWChar(L'')
void CGUIEditBox::SetPassword(bool use, wchar_t chr)
{
password = use;
PWChar = chr;
}
void CGUIEditBox::draw()
{
if (!IsVisible)
return;
bool focus = Environment->hasFocus(this);
IGUISkin* skin = Environment->getSkin();
if (!skin)
return;
irr::video::IVideoDriver* driver = Environment->getVideoDriver();
core::rect<s32> frameRect(AbsoluteRect);
// draw the border
if (Border)
{
skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
false, true, frameRect, &AbsoluteClippingRect);
frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
}
//-----------------------------------this is added
// draw the text
core::stringw tmpText("");
// If password is enabled then construct a string of PWchar the same length as the Text
if(password)
{
u32 i;
for(i = 0;i < Text.size();i++)tmpText += PWChar;
}
else tmpText = Text;
//-----------------------------------
IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont();
if (font)
{
// calculate cursor pos
core::stringw s = tmpText.subString(0,CursorPos);
s32 charcursorpos = font->getDimension(s.c_str()).Width;
s = tmpText.subString(0, ScrollPos); //now use the tmpText everywhere
s32 charscrollpos = font->getDimension(s.c_str()).Width;
core::rect<s32> rct;
// draw mark
if (focus && MarkBegin != MarkEnd)
{
rct = frameRect;
rct.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y);
rct.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y);
s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
s = tmpText.subString(0, realmbgn);
s32 mbegin = font->getDimension(s.c_str()).Width;
s = tmpText.subString(realmbgn, realmend - realmbgn);
s32 mend = font->getDimension(s.c_str()).Width;
rct.UpperLeftCorner.X += mbegin - charscrollpos;
rct.LowerRightCorner.X = rct.UpperLeftCorner.X + mend;
driver->draw2DRectangle(skin->getColor(EGDC_HIGH_LIGHT), rct, &AbsoluteClippingRect);
}
// draw cursor
if (focus && (os::Timer::getTime() - BlinkStartTime) % 700 < 350)
{
rct = frameRect;
rct.UpperLeftCorner.X += charcursorpos;
rct.UpperLeftCorner.X -= charscrollpos;
font->draw(L"_", rct,
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
false, true, &AbsoluteClippingRect);
}
// draw Text
if (tmpText.size())
{
rct = frameRect;
rct.UpperLeftCorner.X -= charscrollpos;
// Save the override color information.
// Then, alter it if the edit box is disabled.
bool prevOver = OverrideColorEnabled;
video::SColor prevColor = OverrideColor;
if ( !this->IsEnabled && !OverrideColorEnabled )
{
OverrideColorEnabled = true;
OverrideColor = skin->getColor( EGDC_GRAY_TEXT );
}
if (focus && MarkBegin != MarkEnd)
{
// marked Text
font->draw(tmpText.c_str(), rct,
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
false, true, &AbsoluteClippingRect);
s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
s = tmpText.subString(0, realmbgn);
s32 mbegin = font->getDimension(s.c_str()).Width;
s = tmpText.subString(realmbgn, realmend - realmbgn);
rct.UpperLeftCorner.X += mbegin;
font->draw(s.c_str(), rct,
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
false, true, &AbsoluteClippingRect);
}
else
{
// normal Text
font->draw(tmpText.c_str(), rct,
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
false, true, &AbsoluteClippingRect);
}
// Return the override color information to its previous settings.
OverrideColorEnabled = prevOver;
OverrideColor = prevColor;
}
}
}