added listbox automatic scrolling and scrollbar automatic visibility depending on height

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@788 dfc29bdd-3216-0410-991c-e03cc46cb475
master
bitplane 2007-07-14 22:17:53 +00:00
parent b43e46727a
commit 9b5c411745
4 changed files with 52 additions and 10 deletions

View File

@ -124,6 +124,10 @@ GUI:
- IGUIStaticText new methods setDrawBorder and setTextAlignment.
- Added IGUIListBox::setAutoScrollEnabled and isAutoScrollEnabled, for automatic
scrolling when selecting or adding an item. Scrollbars are now only visible
when the list doesn't fit in the visible area.
Changes in version 1.3.1 (20 Jun 2007)

View File

@ -62,6 +62,14 @@ namespace gui
//! sets the selected item. Set this to -1 if no item should be selected
virtual void setSelected(s32 id) = 0;
//! set whether the listbox should scroll to show a newly selected item
//! or a new item as it is added to the list.
virtual void setAutoScrollEnabled(bool scroll) = 0;
//! returns true if automatic scrolling is enabled, false if not.
virtual bool isAutoScrollEnabled() = 0;
};

View File

@ -23,7 +23,7 @@ CGUIListBox::CGUIListBox(IGUIEnvironment* environment, IGUIElement* parent,
: IGUIListBox(environment, parent, id, rectangle), Selected(-1), ItemHeight(0),
TotalItemHeight(0), ItemsIconWidth(0), Font(0), IconBank(0),
ScrollBar(0), Selecting(false), DrawBack(drawBack),
MoveOverSelect(moveOverSelect), selectTime(0)
MoveOverSelect(moveOverSelect), selectTime(0), AutoScroll(true)
{
#ifdef _DEBUG
setDebugName("CGUIListBox");
@ -38,6 +38,7 @@ CGUIListBox::CGUIListBox(IGUIEnvironment* environment, IGUIElement* parent,
ScrollBar->setSubElement(true);
ScrollBar->setTabStop(false);
ScrollBar->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
ScrollBar->setVisible(false);
ScrollBar->drop();
ScrollBar->setPos(0);
@ -103,6 +104,8 @@ s32 CGUIListBox::addItem(const wchar_t* text)
Items.push_back(i);
recalculateItemHeight();
recalculateScrollPos();
return Items.size() - 1;
}
@ -166,6 +169,12 @@ void CGUIListBox::recalculateItemHeight()
TotalItemHeight = ItemHeight * Items.size();
ScrollBar->setMax(TotalItemHeight - AbsoluteRect.getHeight());
if( TotalItemHeight < AbsoluteRect.getHeight() )
ScrollBar->setVisible(false);
else
ScrollBar->setVisible(true);
}
@ -321,8 +330,7 @@ bool CGUIListBox::OnEvent(SEvent event)
if (!isPointInside(p))
{
Selecting = false;
//Environment->removeFocus(this);
break;
return true;
}
Selecting = false;
@ -380,9 +388,9 @@ void CGUIListBox::selectNew(s32 ypos, bool onlyHover)
//! Update the position and size of the listbox, and update the scrollbar
void CGUIListBox::updateAbsolutePosition()
{
recalculateItemHeight();
IGUIElement::updateAbsolutePosition();
recalculateItemHeight();
}
//! draws the element and its children
@ -501,7 +509,10 @@ void CGUIListBox::setSpriteBank(IGUISpriteBank* bank)
}
void CGUIListBox::recalculateScrollPos()
{
s32 selPos = Selected * ItemHeight - ScrollBar->getPos();
if (!AutoScroll)
return;
s32 selPos = (Selected == -1 ? TotalItemHeight : Selected * ItemHeight) - ScrollBar->getPos();
if (selPos < 0)
{
@ -514,6 +525,16 @@ void CGUIListBox::recalculateScrollPos()
}
}
void CGUIListBox::setAutoScrollEnabled(bool scroll)
{
AutoScroll = scroll;
}
bool CGUIListBox::isAutoScrollEnabled()
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return AutoScroll;
}
//! Writes attributes of the element.
void CGUIListBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0)
@ -521,8 +542,9 @@ void CGUIListBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWr
IGUIListBox::serializeAttributes(out,options);
// todo: out->addString ("IconBank", IconBank->getName?);
out->addBool ("DrawBack", DrawBack);
out->addBool ("MoveOverSelect", MoveOverSelect);
out->addBool ("DrawBack", DrawBack);
out->addBool ("MoveOverSelect", MoveOverSelect);
out->addBool ("AutoScroll", AutoScroll);
// todo: save list of items and icons.
/*core::array<core::stringw> tmpText;
@ -545,8 +567,9 @@ void CGUIListBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWr
//! Reads attributes of the element
void CGUIListBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{
DrawBack = in->getAttributeAsBool("DrawBack");
MoveOverSelect = in->getAttributeAsBool("MoveOverSelect");
DrawBack = in->getAttributeAsBool("DrawBack");
MoveOverSelect = in->getAttributeAsBool("MoveOverSelect");
AutoScroll = in->getAttributeAsBool("AutoScroll");
IGUIListBox::deserializeAttributes(in,options);

View File

@ -71,6 +71,12 @@ namespace gui
//! skin through getIcon
virtual void setSpriteBank(IGUISpriteBank* bank);
//! sets if automatic scrolling is enabled or not. Default is true.
virtual void setAutoScrollEnabled(bool scroll);
//! returns true if automatic scrolling is enabled, false if not.
virtual bool isAutoScrollEnabled();
//! Update the position and size of the listbox, and update the scrollbar
virtual void updateAbsolutePosition();
@ -106,6 +112,7 @@ namespace gui
bool DrawBack;
bool MoveOverSelect;
u32 selectTime;
bool AutoScroll;
};