Added IGUIComboBox::setTextAlignment and made combo boxes properly serializable

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@1233 dfc29bdd-3216-0410-991c-e03cc46cb475
master
bitplane 2008-02-08 08:23:38 +00:00
parent ebf3a2730b
commit f6db869698
4 changed files with 84 additions and 51 deletions

View File

@ -38,6 +38,7 @@ Changes in version 1.5 (... 2008)
Nodes are now solid or transparent. ( but still more states are needed )
- GUI:
- Added setTextAlignment to IGUIComboBox
- Fixed a bug in CGUISpriteBank which caused a crash when a non-looping animated sprite reached the end of its animation.
- Modal screens no longer flash invisible children when rejecting a focus change.
- Finally added StarSonata patch with table element and TabControl additions. Table is based on MultiColor listbox by Acki, and has loads of changes by CuteAlien.

View File

@ -44,6 +44,13 @@ namespace gui
//! Sets the selected item. Set this to -1 if no item should be selected
virtual void setSelected(s32 idx) = 0;
//! Sets text justification of the text area
/** \param horizontal: EGUIA_UPPERLEFT for left justified (default),
EGUIA_LOWEERRIGHT for right justified, or EGUIA_CENTER for centered text.
\param vertical: EGUIA_UPPERLEFT to align with top edge,
EGUIA_LOWEERRIGHT for bottom edge, or EGUIA_CENTER for centered text (default). */
virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical) = 0;
};

View File

@ -23,10 +23,11 @@ namespace gui
CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent,
s32 id, core::rect<s32> rectangle)
: IGUIComboBox(environment, parent, id, rectangle),
ListButton(0), ListBox(0), Selected(-1), HasFocus(false), LastFocus(0)
ListButton(0), SelectedText(0), ListBox(0), Selected(-1), HasFocus(false), LastFocus(0),
HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER)
{
#ifdef _DEBUG
setDebugName("CGUICheckBox");
setDebugName("CGUIComboBox");
#endif
IGUISkin* skin = Environment->getSkin();
@ -49,9 +50,21 @@ CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent,
ListButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL));
ListButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL));
}
ListButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
ListButton->setSubElement(true);
ListButton->setTabStop(false);
r.UpperLeftCorner.X = 2;
r.UpperLeftCorner.Y = 2;
r.LowerRightCorner.X = RelativeRect.getWidth() - (ListButton->getAbsolutePosition().getWidth() + 2);
r.LowerRightCorner.Y = RelativeRect.getHeight() - 2;
SelectedText = Environment->addStaticText(L"", r, false, false, this, -1, false);
SelectedText->setSubElement(true);
SelectedText->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
SelectedText->setTextAlignment(EGUIA_UPPERLEFT, EGUIA_CENTER);
SelectedText->enableOverrideColor(true);
setNotClipped(true);
// this element can be tabbed to
@ -59,6 +72,12 @@ CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent,
setTabOrder(-1);
}
void CGUIComboBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
{
HAlign = horizontal;
VAlign = vertical;
SelectedText->setTextAlignment(horizontal, vertical);
}
//! Returns amount of items in box
u32 CGUIComboBox::getItemCount() const
@ -84,7 +103,7 @@ void CGUIComboBox::removeItem(u32 idx)
return;
if (Selected == (s32)idx)
Selected = -1;
setSelected(-1);
Items.erase(idx);
}
@ -102,7 +121,7 @@ u32 CGUIComboBox::addItem(const wchar_t* text)
Items.push_back(core::stringw(text));
if (Selected == -1)
Selected = 0;
setSelected(0);
return Items.size() - 1;
}
@ -113,7 +132,7 @@ u32 CGUIComboBox::addItem(const wchar_t* text)
void CGUIComboBox::clear()
{
Items.clear();
Selected = -1;
setSelected(-1);
}
@ -133,19 +152,12 @@ void CGUIComboBox::setSelected(s32 idx)
return;
Selected = idx;
if (Selected == -1)
SelectedText->setText(L"");
else
SelectedText->setText(Items[Selected].c_str());
}
void CGUIComboBox::updateAbsolutePosition()
{
IGUIElement::updateAbsolutePosition();
const s32 width = Environment->getSkin()->getSize(EGDS_WINDOW_BUTTON_WIDTH);
ListButton->setRelativePosition(core::rect<s32>(RelativeRect.getWidth() - width - 2, 2,
RelativeRect.getWidth() - 2, RelativeRect.getHeight() - 2));
}
//! called if an event happened.
bool CGUIComboBox::OnEvent(const SEvent& event)
{
@ -177,28 +189,28 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
switch (event.KeyInput.Key)
{
case KEY_DOWN:
Selected += 1;
setSelected(Selected+1);
break;
case KEY_UP:
Selected -= 1;
setSelected(Selected-1);
break;
case KEY_HOME:
case KEY_PRIOR:
Selected = 0;
setSelected(0);
break;
case KEY_END:
case KEY_NEXT:
Selected = (s32)Items.size()-1;
setSelected((s32)Items.size()-1);
break;
default:
absorb = false;
}
if (Selected <0)
Selected = 0;
setSelected(0);
if (Selected >= (s32)Items.size())
Selected = (s32)Items.size() -1;
setSelected((s32)Items.size() -1);
if (Selected != oldSelected)
sendSelectionChangedEvent();
@ -234,9 +246,9 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
case EGET_LISTBOX_CHANGED:
if (event.GUIEvent.Caller == ListBox)
{
Selected = ListBox->getSelected();
setSelected(ListBox->getSelected());
if (Selected <0 || Selected >= (s32)Items.size())
Selected = -1;
setSelected(-1);
openCloseMenu();
sendSelectionChangedEvent();
@ -278,13 +290,13 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
case EMIE_MOUSE_WHEEL:
{
s32 oldSelected = Selected;
Selected += (event.MouseInput.Wheel < 0) ? 1 : -1;
setSelected( Selected +(event.MouseInput.Wheel < 0) ? 1 : -1);
if (Selected <0)
Selected = 0;
setSelected(0);
if (Selected >= (s32)Items.size())
Selected = (s32)Items.size() -1;
setSelected((s32)Items.size() -1);
if (Selected != oldSelected)
sendSelectionChangedEvent();
@ -330,6 +342,10 @@ void CGUIComboBox::draw()
{
HasFocus = currentFocus == this || isMyChild(currentFocus);
LastFocus = currentFocus;
SelectedText->setBackgroundColor(skin->getColor(EGDC_HIGH_LIGHT));
SelectedText->setDrawBackground(HasFocus);
SelectedText->setOverrideColor(skin->getColor(HasFocus ? EGDC_HIGH_LIGHT_TEXT : EGDC_BUTTON_TEXT));
}
core::rect<s32> frameRect(AbsoluteRect);
@ -339,26 +355,7 @@ void CGUIComboBox::draw()
skin->draw3DSunkenPane(this, skin->getColor(EGDC_3D_HIGH_LIGHT),
true, true, frameRect, &AbsoluteClippingRect);
// Draw text
if (Selected != -1)
{
frameRect = AbsoluteRect;
frameRect.UpperLeftCorner.X += 2;
frameRect.UpperLeftCorner.Y += 2;
frameRect.LowerRightCorner.X -= ListButton->getAbsolutePosition().getWidth() + 2;
frameRect.LowerRightCorner.Y -= 2;
if (HasFocus)
skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), frameRect, &AbsoluteClippingRect);
IGUIFont* font = skin->getFont();
if (font)
font->draw(Items[Selected].c_str(), frameRect,
skin->getColor(HasFocus ? EGDC_HIGH_LIGHT_TEXT : EGDC_BUTTON_TEXT),
false, true, &AbsoluteClippingRect);
}
// draw buttons
// draw children
IGUIElement::draw();
}
@ -412,16 +409,41 @@ void CGUIComboBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadW
{
IGUIComboBox::serializeAttributes(out,options);
out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
out->addInt ("Selected", Selected );
out->addArray ("Items", Items);
out->addInt ("ItemCount", Items.size());
for (u32 i=0; i < Items.size(); ++i)
{
core::stringc s = "Item";
s += i;
s += "Text";
out->addString(s.c_str(), Items[i].c_str());
}
}
//! Reads attributes of the element
void CGUIComboBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{
Items = in->getAttributeAsArray("Items");
IGUIComboBox::deserializeAttributes(in,options);
setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
(EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
// clear the list
clear();
// get item count
u32 c = in->getAttributeAsInt("ItemCount");
// add items
for (u32 i=0; i < c; ++i)
{
core::stringc s = "Item";
s += i;
s += "Text";
addItem(in->getAttributeAsStringW(s.c_str()).c_str());
}
setSelected(in->getAttributeAsInt("Selected"));
}

View File

@ -9,6 +9,7 @@
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUIComboBox.h"
#include "IGUIStaticText.h"
#include "irrString.h"
#include "irrArray.h"
@ -52,8 +53,8 @@ namespace gui
//! sets the selected item. Set this to -1 if no item should be selected
virtual void setSelected(s32 idx);
//! update the position
virtual void updateAbsolutePosition();
//! sets the text alignment of the text part
virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
//! called if an event happened.
virtual bool OnEvent(const SEvent& event);
@ -73,11 +74,13 @@ namespace gui
void sendSelectionChangedEvent();
IGUIButton* ListButton;
IGUIStaticText* SelectedText;
IGUIListBox* ListBox;
core::array< core::stringw > Items;
s32 Selected;
bool HasFocus;
IGUIElement *LastFocus;
EGUI_ALIGNMENT HAlign, VAlign;
};