AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzUIListBox.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CCZ_UI_LISTBOX_H_)
00015 #define _CCZ_UI_LISTBOX_H_
00016 
00017 #include "CzActor.h"
00018 #include "CzScene.h"
00019 #include "CzUIBase.h"
00020 #include "CzTemplates.h"
00021 
00022 //
00023 //
00024 //
00025 //
00026 // CzUIListBox - An image based game actor hthat acts as a list box, allowing the selection from a list of items
00027 //
00028 //
00029 //
00030 //
00031 struct CzUIListBoxItem
00032 {
00033     CzUIBase*       Actor;          // Actor that represents this list box item
00034     bool            Visible;        // Visible state
00035 
00036     CzUIListBoxItem()
00037     {
00038         Actor = NULL;
00039         Visible = true;
00040     }
00041 };
00042 
00043 class CzUIListBox : public CzUIBase
00044 {
00045 public:
00046     // Public access to items iteration
00047     typedef CzVector<CzUIListBoxItem*>::iterator _Iterator;
00048     _Iterator                   begin() { return Children.begin(); }
00049     _Iterator                   end() { return Children.end(); }
00050 
00051     // Properties
00052 protected:
00053     eCzOrientation          Orientation;            // Stacking orientation
00054     eCzAlignH               AlignH;                 // Horizontal alignment of contained actors (vertical only)
00055     eCzAlignV               AlignV;                 // Horizontal alignment of contained actors (horizontal only)
00056 #if defined(_DEBUG)
00057     CzString                ItemsTargetType;        // Target type for items (Text, Brush etc..)
00058 #endif  // _DEBUG
00059     CzTemplate*             ItemsTemplate;          // Items template used to instantiate grid actor items
00060     CzXomlVariableArray*    ItemsData;              // Bound data array
00061     unsigned int            ItemsTargetTypeHash;    // Target item type hash
00062     bool                    MultiSelect;            // If true then multiple selections are allowed
00063     CzUIBase*               Selection;              // Last selections actor
00064     int                     SelectionIndex;         // Last selection index
00065     CzXomlVariable*         SelectionVar;           // Variable that is updated with the current selection index
00066     int                     CaretIndex;             // Caret index
00067     CzIRect                 CaretColourOffset;      // Amount the caret offsets the selected control
00068 public:
00069     void                    setOrientation(eCzOrientation o)        { Orientation = o; setLayoutDirty(true); }
00070     eCzOrientation          getOrientation() const                  { return Orientation; }
00071     void                    setAlignH(eCzAlignH align)              { AlignH = align; setLayoutDirty(true); }
00072     eCzAlignH               getAlignH() const                       { return AlignH; }
00073     void                    setAlignV(eCzAlignV align)              { AlignV = align; setLayoutDirty(true); }
00074     eCzAlignV               getAlignV() const                       { return AlignV; }
00075     int                     getItemCount() const                    { return Children.size(); }
00076     void                    addItem(CzUIListBoxItem* item);
00077     void                    removeItem(CzUIListBoxItem* item);
00078     CzUIListBoxItem*        getItem(int index)                      { return Children[index]; }
00079     CzUIListBoxItem*        findItem(CzUIBase* actor);
00080     void                    setAllVisible(bool visible);
00081     bool                    setItemsTemplate(const char* temp_name);
00082     bool                    setItemsTemplate(CzTemplate* temp);
00083     CzTemplate*             getItemsTemplate()                      { return ItemsTemplate; }
00084     bool                    setItemsData(const char* variable_name, CzXmlNode* node);
00085     void                    setItemsData(CzXomlVariableArray* data, unsigned int  type_hash);
00086     bool                    setItemsData(CzXomlVariable* data);
00087     CzXomlVariableArray*    getItemsData()                          { return ItemsData; }
00088     void                    setItemsTargetType(const char* type);
00089     unsigned int            getItemsTargetType() const              { return ItemsTargetTypeHash; }
00090     void                    setMultiSelect(bool enable)             { MultiSelect = enable; }
00091     bool                    isMultiSelect() const                   { return MultiSelect; }
00092     void                    setSelectionIndex(int index, bool call_events = false);
00093     int                     getSelectionIndex() const               { return SelectionIndex; }
00094     CzUIBase*               getSelection()                          { return Selection; }
00095     void                    setSelectionVar(CzXomlVariable* var)    { SelectionVar = var; }
00096     void                    setCaretIndex(int index);
00097     int                     getCaretIndex() const                   { return CaretIndex; }
00098     CzIRect                 getCaretColourOffset() const            { return CaretColourOffset; }
00099     void                    setCaretColourOffset(const CzIRect& offset) { CaretColourOffset = offset; }
00100     bool                    setProperty(unsigned int property_name, const CzXomlProperty& data, bool delta);
00101     bool                    setProperty(unsigned int property_name, const CzString& data, bool delta);
00102     bool                    getProperty(unsigned int property_name, CzXomlProperty& prop);
00103     // Properties end
00104 protected:
00105     int                     ItemsCount;                             ///< Total items
00106     CzVector<CzUIListBoxItem*> Children;                            // Collection of items that are linked to this list box
00107     void                    LinkChanged(CzActor* child, bool remove);
00108     void                    RemoveActor(CzActor* actor);
00109     void                    RemoveAllActors();                      // Removes and destroys all actors in the list box
00110     void                    CreateAllActors();                      // Re-creates all actors within the list box from a template
00111     void                    UpdateAllActors(bool force_update);     // Moves data from the bound items data array to the list box actors
00112     bool                    UpdateLayout();                         // Updates the layout of the child items
00113     CzVec2                  CalculateItemPosition(int item_index);
00114     bool                    UpdateBinding(unsigned int property_name, CzXomlVariable* var);
00115 public:
00116     CzUIListBox() : CzUIBase(), AlignH(AlignH_Centre), AlignV(AlignV_Middle), ItemsData(NULL), ItemsTargetTypeHash(0), MultiSelect(false), SelectionIndex(-1), Selection(NULL), 
00117                                 SelectionVar(NULL), Orientation(Orientation_Vertical), ItemsCount(-1), CaretIndex(-1), CaretColourOffset(0, 0, 0, 0)
00118                                 { setActualClassType("listbox"); ReceiveEventFromChild = true; }
00119     virtual ~CzUIListBox();
00120 
00121     virtual void                InitListBox();
00122     bool                        Update(float dt);
00123 
00124     // Event handlers
00125     virtual void                NotifySelectionChanged(int old_index, int new_index);
00126 
00127     // Implementation of IzXomlClass interface
00128     int                         LoadFromXoml(IzXomlResource* parent, bool load_children, CzXmlNode* node);
00129 
00130     // Implementation of IzAnimTarget interface
00131     bool                        UpdateFromAnimation(CzAnimInstance *animation);
00132 
00133     // Utility
00134     void                        ClearAllSelections(CzUIBase* ignore);
00135     void                        ClearAllToggles(CzUIBase* ignore);
00136     void                        ScrollToIndex(int index);
00137 
00138     // Internal (used by XOML system to setup and cleanup the XOML class properties system
00139 protected:
00140     static CzXomlClassDef*  UIListBoxClassDef;                              // XOML class definition
00141 public:
00142     static void             InitClass();
00143     static void             ReleaseClass();
00144 
00145     static CzXomlProperty   _getItemCount(IzXomlResource* target);
00146     static bool             _setOrientation(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00147     static CzXomlProperty   _getOrientation(IzXomlResource* target);
00148     static bool             _setAlignH(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00149     static bool             _setAlignV(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00150     static bool             _setItemsTemplate(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00151     static CzXomlProperty   _getItemsTemplate(IzXomlResource* target);
00152     static bool             _setItemsData(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00153     static CzXomlProperty   _getItemsData(IzXomlResource* target);
00154     static bool             _setItemsTargetType(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00155     static bool             _setOnSelectionChanged(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00156     static bool             _setMultiSelect(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00157     static CzXomlProperty   _getMultiSelect(IzXomlResource* target);
00158     static CzXomlProperty   _getSelection(IzXomlResource* target);
00159     static bool             _setSelectionIndex(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00160     static CzXomlProperty   _getSelectionIndex(IzXomlResource* target);
00161     static bool             _setCaretColourOffset(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00162     static CzXomlProperty   _getCaretColourOffset(IzXomlResource* target);
00163     static bool             _setCaretIndex(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00164     static CzXomlProperty   _getCaretIndex(IzXomlResource* target);
00165 };
00166 
00167 //
00168 //  CzUIListBoxCreator - Creates an instance of a list box actor object
00169 //
00170 class CzUIListBoxCreator : public IzXomlClassCreator
00171 {
00172 public:
00173     CzUIListBoxCreator()
00174     {
00175         setClassName("listbox");
00176     }
00177     IzXomlResource* CreateInstance(IzXomlResource* parent)  { return new CzUIListBox(); }
00178 };
00179 
00180 //
00181 //
00182 //
00183 //
00184 //  Helper macros
00185 //
00186 //
00187 //
00188 //
00189 #define CZ_NEW_LISTBOX(scene, listbox, name, background, width, height)             \
00190     CzUIListBox* listbox = new CzUIListBox();                                       \
00191     listbox->setName(name);                                                         \
00192     scene->addActor(listbox);                                                       \
00193     listbox->Init(background, width, height);                                       \
00194     listbox->InitListBox();
00195 
00196 
00197 
00198 
00199 #endif  // _CCZ_UI_LISTBOX_H_