Something went wrong. Try again.
Massive multiplayer game aiming to create a collaborative musical environment.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673/*********************************************************************** filename: CEGUIItemListBase.cpp created: 31/3/2005 author: Tomas Lindquist Olsen (based on original Listbox code by Paul D Turner)
purpose: Implementation of ItemListBase widget base class*************************************************************************//*************************************************************************** * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team * * 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 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 "CEGUIExceptions.h"#include "CEGUIWindowManager.h"#include "elements/CEGUIItemListBase.h"#include "elements/CEGUIItemEntry.h"
#include <algorithm>
// Start of CEGUI namespace sectionnamespace CEGUI{const String ItemListBase::EventNamespace("ItemListBase");
/************************************************************************* ItemListBaseWindowRenderer*************************************************************************/ItemListBaseWindowRenderer::ItemListBaseWindowRenderer(const String& name) : WindowRenderer(name, ItemListBase::EventNamespace){}
/************************************************************************/
/************************************************************************* used for < comparisons between ItemEntry pointers*************************************************************************/static bool ItemEntry_less(const ItemEntry* a, const ItemEntry* b){ return a->getText() < b->getText();}
/************************************************************************* used for > comparisons between ItemEntry pointers*************************************************************************/static bool ItemEntry_greater(const ItemEntry* a, const ItemEntry* b){ return (a->getText() > b->getText());}
/************************************************************************/
/************************************************************************* Definition of Properties for this class*************************************************************************/ItemListBaseProperties::AutoResizeEnabled ItemListBase::d_autoResizeEnabledProperty;ItemListBaseProperties::SortEnabled ItemListBase::d_sortEnabledProperty;ItemListBaseProperties::SortMode ItemListBase::d_sortModeProperty;
/************************************************************************* Constants*************************************************************************/// event namesconst String ItemListBase::EventListContentsChanged("ListItemsChanged");const String ItemListBase::EventSortEnabledChanged("SortEnabledChanged");const String ItemListBase::EventSortModeChanged("SortModeChanged");
/************************************************************************* Constructor for ItemListBase base class.*************************************************************************/ItemListBase::ItemListBase(const String& type, const String& name) : Window(type, name), d_autoResize(false), d_sortEnabled(false), d_sortMode(Ascending), d_sortCallback(0), d_resort(false){ // by default we dont have a content pane, but to make sure things still work // we "emulate" it by setting it to this d_pane = this;
// add properties for ItemListBase class addItemListBaseProperties();}
/************************************************************************* Destructor for ItemListBase base class.*************************************************************************/ItemListBase::~ItemListBase(void){ //resetList_impl();}
/************************************************************************* Initialise components*************************************************************************/void ItemListBase::initialiseComponents(void){ // this pane may be ourselves, and in fact is by default... d_pane->subscribeEvent(Window::EventChildRemoved, Event::Subscriber(&ItemListBase::handle_PaneChildRemoved, this));}
/************************************************************************* Return the item at index position 'index'.*************************************************************************/ItemEntry* ItemListBase::getItemFromIndex(size_t index) const{ if (index < d_listItems.size()) { return d_listItems[index]; } else { throw InvalidRequestException("ItemListBase::getItemFromIndex - the specified index is out of range for this ItemListBase."); }}
/************************************************************************* Return the index of ItemEntry \a item*************************************************************************/size_t ItemListBase::getItemIndex(const ItemEntry* item) const{ ItemEntryList::const_iterator pos = std::find(d_listItems.begin(), d_listItems.end(), item);
if (pos != d_listItems.end()) { return std::distance(d_listItems.begin(), pos); } else { throw InvalidRequestException("ItemListBase::getItemIndex - the specified ItemEntry is not attached to this ItemListBase."); }}
/************************************************************************* Search the list for an item with the specified text*************************************************************************/ItemEntry* ItemListBase::findItemWithText(const String& text, const ItemEntry* start_item){ // if start_item is NULL begin search at begining, else start at item after start_item size_t index = (!start_item) ? 0 : (getItemIndex(start_item) + 1);
while (index < d_listItems.size()) { // return pointer to this item if it's text matches if (d_listItems[index]->getText() == text) { return d_listItems[index]; } // no matching text, advance to next item else { index++; } }
// no items matched. return 0;}
/************************************************************************* Return whether the specified ItemEntry is in the List*************************************************************************/bool ItemListBase::isItemInList(const ItemEntry* item) const{ //return std::find(d_listItems.begin(), d_listItems.end(), item) != d_listItems.end(); return (item->d_ownerList == this);}
/************************************************************************* Remove all items from the list.*************************************************************************/void ItemListBase::resetList(void){ if (resetList_impl()) { handleUpdatedItemData(); }}
/************************************************************************* Add the given ItemEntry to the list.*************************************************************************/void ItemListBase::addItem(ItemEntry* item){ // make sure the item is valid and that we dont already have it in our list if (item && item->d_ownerList != this) { // if sorting is enabled, re-sort the list if (d_sortEnabled) { d_listItems.insert( std::upper_bound(d_listItems.begin(), d_listItems.end(), item, getRealSortCallback()), item); } // just stick it on the end. else { d_listItems.push_back(item); } // make sure it gets added properly item->d_ownerList = this; addChildWindow(item); handleUpdatedItemData(); }}
/************************************************************************* Insert an item into the list box after a specified item already in the list.*************************************************************************/void ItemListBase::insertItem(ItemEntry* item, const ItemEntry* position){ if (d_sortEnabled) { addItem(item); } else if (item && item->d_ownerList != this) { // if position is NULL begin insert at begining, else insert after item 'position' ItemEntryList::iterator ins_pos;
if (!position) { ins_pos = d_listItems.begin(); } else { ins_pos = std::find(d_listItems.begin(), d_listItems.end(), position);
// throw if item 'position' is not in the list if (ins_pos == d_listItems.end()) { throw InvalidRequestException("ItemListBase::insertItem - the specified ItemEntry for parameter 'position' is not attached to this ItemListBase."); }
}
d_listItems.insert(ins_pos, item); item->d_ownerList = this; addChildWindow(item);
handleUpdatedItemData(); }}
/************************************************************************* Removes the given item from the list box.*************************************************************************/void ItemListBase::removeItem(ItemEntry* item){ if (item && item->d_ownerList == this) { d_pane->removeChildWindow(item); if (item->isDestroyedByParent()) { WindowManager::getSingleton().destroyWindow(item); } }}
/************************************************************************* Set wheter or not this ItemListBase widget should automatically resize to fit its content.*************************************************************************/void ItemListBase::setAutoResizeEnabled(bool setting){ bool old = d_autoResize; d_autoResize = setting;
// if not already enabled, trigger a resize - only if not currently initialising if ( d_autoResize && !old && !d_initialising) { sizeToContent(); }}
/************************************************************************* Causes the list box to update it's internal state after changes have been made to one or more attached ItemEntry objects.*************************************************************************/void ItemListBase::handleUpdatedItemData(bool resort){ if (!d_destructionStarted) { d_resort |= resort; WindowEventArgs args(this); onListContentsChanged(args); }}
/************************************************************************* Handler called internally when the list contents are changed*************************************************************************/void ItemListBase::onListContentsChanged(WindowEventArgs& e){ // if we are not currently initialising we might have things todo if (!d_initialising) { requestRedraw();
// if auto resize is enabled - do it if (d_autoResize) sizeToContent();
// resort list if requested and enabled if (d_resort && d_sortEnabled) sortList(false); d_resort = false;
// redo the item layout and fire our event layoutItemWidgets(); fireEvent(EventListContentsChanged, e, EventNamespace); }}
/************************************************************************ Handler for when a child is removed*************************************************************************//*void ItemListBase::onChildRemoved(WindowEventArgs& e){ // if destruction has already begun, we don't need to do anything. // everything has to go anyway // make sure it is removed from the itemlist if we have an ItemEntry if (!d_destructionStarted && e.window->testClassName("ItemEntry")) { ItemEntryList::iterator pos = std::find(d_listItems.begin(), d_listItems.end(), e.window);
// if item is in the list if (pos != d_listItems.end()) { // remove item (*pos)->d_ownerList = 0; d_listItems.erase(pos); // trigger list update handleUpdatedItemData(); } }
// base class handling Window::onChildRemoved(e);}*/
/************************************************************************ Handler for when the window initiates destruction*************************************************************************//*void ItemListBase::onDestructionStarted(WindowEventArgs& e){ // base class handling Window::onDestructionStarted(e);
// remove everything from the list properly resetList_impl();}*/
/************************************************************************* Remove all items from the list.*************************************************************************/bool ItemListBase::resetList_impl(void){ // just return false if the list is already empty if (d_listItems.empty()) { return false; } // we have items to be removed and possible deleted else { // delete any items we are supposed to while (!d_listItems.empty()) { ItemEntry* item = d_listItems[0]; d_pane->removeChildWindow(item); if (item->isDestroyedByParent()) { WindowManager::getSingleton().destroyWindow(item); } }
// list is cleared by the removeChild calls return true; }}
/************************************************************************* Add ItemListBase specific properties*************************************************************************/void ItemListBase::addItemListBaseProperties(void){ addProperty(&d_autoResizeEnabledProperty); addProperty(&d_sortEnabledProperty); addProperty(&d_sortModeProperty);}
/************************************************************************* Internal version of adding a child window.*************************************************************************/void ItemListBase::addChild_impl(Window* wnd){ // if this is an ItemEntry we add it like one, but only if it is not already in the list! if (wnd->testClassName("ItemEntry")) { // add to the pane if we have one if (d_pane != this) { d_pane->addChildWindow(wnd); } // add item directly to us else { Window::addChild_impl(wnd); }
ItemEntry* item = static_cast<ItemEntry*>(wnd); if (item->d_ownerList != this) { // perform normal addItem // if sorting is enabled, re-sort the list if (d_sortEnabled) { d_listItems.insert( std::upper_bound(d_listItems.begin(), d_listItems.end(), item, getRealSortCallback()), item); } // just stick it on the end. else { d_listItems.push_back(item); } item->d_ownerList = this; handleUpdatedItemData(); } } // otherwise it's base class processing else { Window::addChild_impl(wnd); }}
/************************************************************************ Initialisation done*************************************************************************/void ItemListBase::endInitialisation(void){ Window::endInitialisation(); handleUpdatedItemData(true);}
/************************************************************************ Perform child window layout************************************************************************/void ItemListBase::performChildWindowLayout(void){ Window::performChildWindowLayout(); // if we are not currently initialising if (!d_initialising) { // Redo the item layout. // We don't just call handleUpdateItemData, as that could trigger a resize, // which is not what is being requested. // It would also cause infinite recursion... so lets just avoid that :) layoutItemWidgets(); }}
/************************************************************************ Resize to fit content************************************************************************/void ItemListBase::sizeToContent_impl(void){ Rect renderArea(getItemRenderArea()); Rect wndArea(getArea().asAbsolute(getParentPixelSize()));
// get size of content Size sz(getContentSize());
// calculate the full size with the frame accounted for and resize the window to this sz.d_width += wndArea.getWidth() - renderArea.getWidth(); sz.d_height += wndArea.getHeight() - renderArea.getHeight(); setSize(UVector2(cegui_absdim(sz.d_width), cegui_absdim(sz.d_height)));}
/************************************************************************ Get item render area************************************************************************/Rect ItemListBase::getItemRenderArea(void) const{ if (d_windowRenderer != 0) { ItemListBaseWindowRenderer* wr = (ItemListBaseWindowRenderer*)d_windowRenderer; return wr->getItemRenderArea(); } else { //return getItemRenderArea_impl(); throw InvalidRequestException("ItemListBase::getItemRenderArea - This function must be implemented by the window renderer module"); }}
/************************************************************************ Handler to manage items being removed from the content pane************************************************************************/bool ItemListBase::handle_PaneChildRemoved(const EventArgs& e){ Window* w = static_cast<const WindowEventArgs&>(e).window;
// make sure it is removed from the itemlist if we have an ItemEntry if (w->testClassName("ItemEntry")) { ItemEntryList::iterator pos = std::find(d_listItems.begin(), d_listItems.end(), w);
// if item is in the list if (pos != d_listItems.end()) { // make sure the item is no longer related to us (*pos)->d_ownerList = 0; // remove item d_listItems.erase(pos); // trigger list update handleUpdatedItemData(); } }
return false;}
/************************************************************************ Set sorting enabled state************************************************************************/void ItemListBase::setSortEnabled(bool setting){ if (d_sortEnabled != setting) { d_sortEnabled = setting;
if (d_sortEnabled && !d_initialising) { sortList(); }
WindowEventArgs e(this); onSortEnabledChanged(e); }}
/************************************************************************ Set the user sorting callback************************************************************************/void ItemListBase::setSortCallback(SortCallback cb){ if (d_sortCallback != cb) { d_sortCallback = cb; if (d_sortEnabled && !d_initialising) { sortList(); } handleUpdatedItemData(true); }}
/************************************************************************ Handle sort enabled changed************************************************************************/void ItemListBase::onSortEnabledChanged(WindowEventArgs& e){ fireEvent(EventSortEnabledChanged, e);}
/************************************************************************ Handle sort mode changed************************************************************************/void ItemListBase::onSortModeChanged(WindowEventArgs& e){ fireEvent(EventSortModeChanged, e);}
/************************************************************************ Sort list************************************************************************/void ItemListBase::sortList(bool relayout){ std::sort(d_listItems.begin(), d_listItems.end(), getRealSortCallback()); if (relayout) { layoutItemWidgets(); }}
/************************************************************************ Get the real function pointer to use for the sorting operation************************************************************************/ItemListBase::SortCallback ItemListBase::getRealSortCallback() const{ switch (d_sortMode) { case Ascending: return &ItemEntry_less;
case Descending: return &ItemEntry_greater;
case UserSort: return (d_sortCallback!=0) ? d_sortCallback : &ItemEntry_less;
// we default to ascending sorting default: return &ItemEntry_less; }}
/************************************************************************ Set sort mode************************************************************************/void ItemListBase::setSortMode(SortMode mode){ if (d_sortMode != mode) { d_sortMode = mode; if (d_sortEnabled && !d_initialising) sortList();
WindowEventArgs e(this); onSortModeChanged(e); }}
} // End of CEGUI namespace section