Commit f6db8696 authored by bitplane's avatar bitplane

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
parent ebf3a273
...@@ -38,6 +38,7 @@ Changes in version 1.5 (... 2008) ...@@ -38,6 +38,7 @@ Changes in version 1.5 (... 2008)
Nodes are now solid or transparent. ( but still more states are needed ) Nodes are now solid or transparent. ( but still more states are needed )
- GUI: - 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. - 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. - 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. - 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.
......
...@@ -44,6 +44,13 @@ namespace gui ...@@ -44,6 +44,13 @@ namespace gui
//! Sets the selected item. Set this to -1 if no item should be selected //! Sets the selected item. Set this to -1 if no item should be selected
virtual void setSelected(s32 idx) = 0; 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;
}; };
......
...@@ -23,10 +23,11 @@ namespace gui ...@@ -23,10 +23,11 @@ namespace gui
CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent, CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent,
s32 id, core::rect<s32> rectangle) s32 id, core::rect<s32> rectangle)
: IGUIComboBox(environment, parent, id, 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 #ifdef _DEBUG
setDebugName("CGUICheckBox"); setDebugName("CGUIComboBox");
#endif #endif
IGUISkin* skin = Environment->getSkin(); IGUISkin* skin = Environment->getSkin();
...@@ -49,9 +50,21 @@ CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent, ...@@ -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_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->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->setSubElement(true);
ListButton->setTabStop(false); 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); setNotClipped(true);
// this element can be tabbed to // this element can be tabbed to
...@@ -59,6 +72,12 @@ CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent, ...@@ -59,6 +72,12 @@ CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent,
setTabOrder(-1); 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 //! Returns amount of items in box
u32 CGUIComboBox::getItemCount() const u32 CGUIComboBox::getItemCount() const
...@@ -84,7 +103,7 @@ void CGUIComboBox::removeItem(u32 idx) ...@@ -84,7 +103,7 @@ void CGUIComboBox::removeItem(u32 idx)
return; return;
if (Selected == (s32)idx) if (Selected == (s32)idx)
Selected = -1; setSelected(-1);
Items.erase(idx); Items.erase(idx);
} }
...@@ -102,7 +121,7 @@ u32 CGUIComboBox::addItem(const wchar_t* text) ...@@ -102,7 +121,7 @@ u32 CGUIComboBox::addItem(const wchar_t* text)
Items.push_back(core::stringw(text)); Items.push_back(core::stringw(text));
if (Selected == -1) if (Selected == -1)
Selected = 0; setSelected(0);
return Items.size() - 1; return Items.size() - 1;
} }
...@@ -113,7 +132,7 @@ u32 CGUIComboBox::addItem(const wchar_t* text) ...@@ -113,7 +132,7 @@ u32 CGUIComboBox::addItem(const wchar_t* text)
void CGUIComboBox::clear() void CGUIComboBox::clear()
{ {
Items.clear(); Items.clear();
Selected = -1; setSelected(-1);
} }
...@@ -133,19 +152,12 @@ void CGUIComboBox::setSelected(s32 idx) ...@@ -133,19 +152,12 @@ void CGUIComboBox::setSelected(s32 idx)
return; return;
Selected = idx; 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. //! called if an event happened.
bool CGUIComboBox::OnEvent(const SEvent& event) bool CGUIComboBox::OnEvent(const SEvent& event)
{ {
...@@ -177,28 +189,28 @@ bool CGUIComboBox::OnEvent(const SEvent& event) ...@@ -177,28 +189,28 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
switch (event.KeyInput.Key) switch (event.KeyInput.Key)
{ {
case KEY_DOWN: case KEY_DOWN:
Selected += 1; setSelected(Selected+1);
break; break;
case KEY_UP: case KEY_UP:
Selected -= 1; setSelected(Selected-1);
break; break;
case KEY_HOME: case KEY_HOME:
case KEY_PRIOR: case KEY_PRIOR:
Selected = 0; setSelected(0);
break; break;
case KEY_END: case KEY_END:
case KEY_NEXT: case KEY_NEXT:
Selected = (s32)Items.size()-1; setSelected((s32)Items.size()-1);
break; break;
default: default:
absorb = false; absorb = false;
} }
if (Selected <0) if (Selected <0)
Selected = 0; setSelected(0);
if (Selected >= (s32)Items.size()) if (Selected >= (s32)Items.size())
Selected = (s32)Items.size() -1; setSelected((s32)Items.size() -1);
if (Selected != oldSelected) if (Selected != oldSelected)
sendSelectionChangedEvent(); sendSelectionChangedEvent();
...@@ -234,9 +246,9 @@ bool CGUIComboBox::OnEvent(const SEvent& event) ...@@ -234,9 +246,9 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
case EGET_LISTBOX_CHANGED: case EGET_LISTBOX_CHANGED:
if (event.GUIEvent.Caller == ListBox) if (event.GUIEvent.Caller == ListBox)
{ {
Selected = ListBox->getSelected(); setSelected(ListBox->getSelected());
if (Selected <0 || Selected >= (s32)Items.size()) if (Selected <0 || Selected >= (s32)Items.size())
Selected = -1; setSelected(-1);
openCloseMenu(); openCloseMenu();
sendSelectionChangedEvent(); sendSelectionChangedEvent();
...@@ -278,13 +290,13 @@ bool CGUIComboBox::OnEvent(const SEvent& event) ...@@ -278,13 +290,13 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
case EMIE_MOUSE_WHEEL: case EMIE_MOUSE_WHEEL:
{ {
s32 oldSelected = Selected; s32 oldSelected = Selected;
Selected += (event.MouseInput.Wheel < 0) ? 1 : -1; setSelected( Selected +(event.MouseInput.Wheel < 0) ? 1 : -1);
if (Selected <0) if (Selected <0)
Selected = 0; setSelected(0);
if (Selected >= (s32)Items.size()) if (Selected >= (s32)Items.size())
Selected = (s32)Items.size() -1; setSelected((s32)Items.size() -1);
if (Selected != oldSelected) if (Selected != oldSelected)
sendSelectionChangedEvent(); sendSelectionChangedEvent();
...@@ -330,6 +342,10 @@ void CGUIComboBox::draw() ...@@ -330,6 +342,10 @@ void CGUIComboBox::draw()
{ {
HasFocus = currentFocus == this || isMyChild(currentFocus); HasFocus = currentFocus == this || isMyChild(currentFocus);
LastFocus = 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); core::rect<s32> frameRect(AbsoluteRect);
...@@ -339,26 +355,7 @@ void CGUIComboBox::draw() ...@@ -339,26 +355,7 @@ void CGUIComboBox::draw()
skin->draw3DSunkenPane(this, skin->getColor(EGDC_3D_HIGH_LIGHT), skin->draw3DSunkenPane(this, skin->getColor(EGDC_3D_HIGH_LIGHT),
true, true, frameRect, &AbsoluteClippingRect); true, true, frameRect, &AbsoluteClippingRect);
// Draw text // draw children
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
IGUIElement::draw(); IGUIElement::draw();
} }
...@@ -412,16 +409,41 @@ void CGUIComboBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadW ...@@ -412,16 +409,41 @@ void CGUIComboBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadW
{ {
IGUIComboBox::serializeAttributes(out,options); IGUIComboBox::serializeAttributes(out,options);
out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
out->addInt ("Selected", Selected ); 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 //! Reads attributes of the element
void CGUIComboBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) void CGUIComboBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{ {
Items = in->getAttributeAsArray("Items");
IGUIComboBox::deserializeAttributes(in,options); 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")); setSelected(in->getAttributeAsInt("Selected"));
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#ifdef _IRR_COMPILE_WITH_GUI_ #ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUIComboBox.h" #include "IGUIComboBox.h"
#include "IGUIStaticText.h"
#include "irrString.h" #include "irrString.h"
#include "irrArray.h" #include "irrArray.h"
...@@ -52,8 +53,8 @@ namespace gui ...@@ -52,8 +53,8 @@ namespace gui
//! sets the selected item. Set this to -1 if no item should be selected //! sets the selected item. Set this to -1 if no item should be selected
virtual void setSelected(s32 idx); virtual void setSelected(s32 idx);
//! update the position //! sets the text alignment of the text part
virtual void updateAbsolutePosition(); virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
//! called if an event happened. //! called if an event happened.
virtual bool OnEvent(const SEvent& event); virtual bool OnEvent(const SEvent& event);
...@@ -73,11 +74,13 @@ namespace gui ...@@ -73,11 +74,13 @@ namespace gui
void sendSelectionChangedEvent(); void sendSelectionChangedEvent();
IGUIButton* ListButton; IGUIButton* ListButton;
IGUIStaticText* SelectedText;
IGUIListBox* ListBox; IGUIListBox* ListBox;
core::array< core::stringw > Items; core::array< core::stringw > Items;
s32 Selected; s32 Selected;
bool HasFocus; bool HasFocus;
IGUIElement *LastFocus; IGUIElement *LastFocus;
EGUI_ALIGNMENT HAlign, VAlign;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment