mail[Wesnoth-commits] r40294 - in /trunk/src/gui/widgets: generator.cpp generator.hpp generator_private.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by koraq on December 20, 2009 - 16:55:
Author: mordante
Date: Sun Dec 20 16:55:20 2009
New Revision: 40294

URL: http://svn.gna.org/viewcvs/wesnoth?rev=40294&view=rev
Log:
Allow generator items to have a shown flag.

By toggling this flag the contents update their size and placement of
the items.

Modified:
    trunk/src/gui/widgets/generator.cpp
    trunk/src/gui/widgets/generator.hpp
    trunk/src/gui/widgets/generator_private.hpp

Modified: trunk/src/gui/widgets/generator.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/generator.cpp?rev=40294&r1=40293&r2=40294&view=diff
==============================================================================
--- trunk/src/gui/widgets/generator.cpp (original)
+++ trunk/src/gui/widgets/generator.cpp Sun Dec 20 16:55:20 2009
@@ -26,6 +26,22 @@
 
 namespace minimum_selection {
 
+void tone::set_item_shown(const unsigned index, const bool show)
+{
+       if(show && get_selected_item_count() == 0) {
+               do_select_item(index);
+       } else if(!show && is_selected(index)) {
+               do_deselect_item(index);
+
+               for(unsigned i = index + 1; i < get_item_count(); ++i) {
+                       if(get_item_shown(i)) {
+                               do_select_item(i);
+                               break;
+                       }
+               }
+       }
+}
+
 void tone::create_item(const unsigned index)
 {
        if(get_selected_item_count() == 0) {
@@ -44,6 +60,8 @@
 
 void tone::delete_item(const unsigned index)
 {
+       /** @todo do_select_item needs to test for shown flag. */
+
        if(is_selected(index)) {
                do_deselect_item(index);
 
@@ -65,6 +83,13 @@
        }
 }
 
+void tnone::set_item_shown(const unsigned index, const bool show)
+{
+       if(!show && is_selected(index)) {
+               do_deselect_item(index);
+       }
+}
+
 } // namespace minimum_selection
 
 /***** ***** ***** ***** Placement ***** ***** ***** *****/
@@ -76,6 +101,25 @@
 {
 }
 
+void thorizontal_list::set_item_shown(const unsigned index, const bool show)
+{
+       // Adjust the size.
+       tpoint best_size = this->get_best_size();
+       const int item_width = show
+                       ?  get_item(index).get_best_size().x
+                       : -get_item(index).get_best_size().x;
+
+       best_size.x += item_width;
+       this->set_layout_size(best_size);
+
+       // Adjust the placement.
+       for(unsigned i = index + 1; i < get_item_count(); ++i) {
+               tpoint origin = get_item(i).get_origin();
+               origin.x += item_width;
+               get_item(i).set_origin(origin);
+       }
+}
+
 void thorizontal_list::create_item(const unsigned /*index*/)
 {
        if(!placed_) {
@@ -93,7 +137,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                const tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -123,7 +167,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -146,7 +190,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -179,7 +223,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -202,7 +246,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                const tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -250,7 +294,9 @@
 
        for(size_t i = get_selected_item() + 1; i < get_item_count(); ++i) {
 
-               if(get_item(i).get_visible() == twidget::INVISIBLE) {
+               if(get_item(i).get_visible() == twidget::INVISIBLE
+                               || !get_item_shown(i)) {
+
                        continue;
                }
 
@@ -269,6 +315,25 @@
 {
 }
 
+void tvertical_list::set_item_shown(const unsigned index, const bool show)
+{
+       // Adjust the size.
+       tpoint best_size = this->get_best_size();
+       const int item_height = show
+                       ?  get_item(index).get_best_size().y
+                       : -get_item(index).get_best_size().y;
+
+       best_size.y += item_height;
+       this->set_layout_size(best_size);
+
+       // Adjust the placement.
+       for(unsigned i = index + 1; i < get_item_count(); ++i) {
+               tpoint origin = get_item(i).get_origin();
+               origin.y += item_height;
+               get_item(i).set_origin(origin);
+       }
+}
+
 void tvertical_list::create_item(const unsigned /*index*/)
 {
        if(!placed_) {
@@ -286,7 +351,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                const tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -316,7 +381,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -339,7 +404,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -372,7 +437,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -396,7 +461,7 @@
        for(size_t i = 0; i < get_item_count(); ++i) {
 
                const tgrid& grid = get_item(i);
-               if(grid.get_visible() == twidget::INVISIBLE) {
+               if(grid.get_visible() == twidget::INVISIBLE || 
!get_item_shown(i)) {
                        continue;
                }
 
@@ -442,7 +507,9 @@
 
        for(size_t i = get_selected_item() + 1; i < get_item_count(); ++i) {
 
-               if(get_item(i).get_visible() == twidget::INVISIBLE) {
+               if(get_item(i).get_visible() == twidget::INVISIBLE
+                                       || !get_item_shown(i)) {
+
                        continue;
                }
 

Modified: trunk/src/gui/widgets/generator.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/generator.hpp?rev=40294&r1=40293&r2=40294&view=diff
==============================================================================
--- trunk/src/gui/widgets/generator.hpp (original)
+++ trunk/src/gui/widgets/generator.hpp Sun Dec 20 16:55:20 2009
@@ -99,6 +99,19 @@
        /** Returns whether the item is selected. */
        virtual bool is_selected(const unsigned index) const = 0;
 
+       /**
+        * Shows or hides an item.
+        *
+        * The caller is resposible for reformatting the grid.
+        *
+        * @param index               The item to show or hide.
+        * @param show                If true shows the item, else hides it.
+        */
+       virtual void set_item_shown(const unsigned index, const bool show) = 
0;
+
+       /** Returns whether the item is shown. */
+       virtual bool get_item_shown(const unsigned index) const = 0;
+
        /** Returns the number of items. */
        virtual unsigned get_item_count() const = 0;
 

Modified: trunk/src/gui/widgets/generator_private.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/generator_private.hpp?rev=40294&r1=40293&r2=40294&view=diff
==============================================================================
--- trunk/src/gui/widgets/generator_private.hpp (original)
+++ trunk/src/gui/widgets/generator_private.hpp Sun Dec 20 16:55:20 2009
@@ -41,6 +41,14 @@
        : public virtual tgenerator_
 {
        /**
+        * Called when an item is shown or hidden.
+        *
+        * @param index               The item to show or hide.
+        * @param show                If true shows the item, else hides it.
+        */
+       void set_item_shown(const unsigned index, const bool show);
+
+       /**
         * Called when an item is created.
         *
         * @param index               The index of the new item.
@@ -80,6 +88,9 @@
        : public virtual tgenerator_
 {
 
+       /** See tone::set_item_shown(). */
+       void set_item_shown(const unsigned index, const bool show);
+
        /** See minimum_selection::tone::create_item() */
        void create_item(const unsigned /*index*/) {}
 
@@ -134,6 +145,7 @@
 struct tinfinite
        : public virtual tgenerator_
 {
+       /** See tone::select_item(). */
        void select_item(const unsigned index)
        {
                do_select_item(index);
@@ -154,6 +166,14 @@
        thorizontal_list();
 
        /**
+        * Called when an item is shown or hidden.
+        *
+        * @param index               The item to show or hide.
+        * @param show                If true shows the item, else hides it.
+        */
+       void set_item_shown(const unsigned index, const bool show);
+
+       /**
         * Called when an item is created.
         *
         * This function should place the new item.
@@ -254,6 +274,9 @@
        : public virtual tgenerator_
 {
        tvertical_list();
+
+       /** See thorizontal_list::set_item_shown(). */
+       void set_item_shown(const unsigned index, const bool show);
 
        /** See thorizontal_list::create_item(). */
        void create_item(const unsigned index);
@@ -328,6 +351,12 @@
 struct tmatrix
        : public virtual tgenerator_
 {
+       /** See thorizontal_list::set_item_shown(). */
+       void set_item_shown(const unsigned /*index*/, const bool /*show*/)
+       {
+               ERROR_LOG(false);
+       }
+
        /** See thorizontal_list::create_item(). */
        void create_item(const unsigned /*index*/) { ERROR_LOG(false); }
 
@@ -391,6 +420,12 @@
 struct tindependant
        : public virtual tgenerator_
 {
+       /** See thorizontal_list::set_item_shown(). */
+       void set_item_shown(const unsigned /*index*/, const bool /*show*/)
+       {
+               /* DO NOTHING */
+       }
+
        /** See thorizontal_list::create_item(). */
        void create_item(const unsigned /*index*/)
        {
@@ -601,6 +636,39 @@
        }
 
        /** Inherited from tgenerator_. */
+       void set_item_shown(const unsigned index, const bool show)
+       {
+               assert(index < items_.size());
+               if(items_[index]->shown != show) {
+
+                       /*
+                        * Item needs to be visible in get_best_size(), which 
might be
+                        * called in placement::set_item_shown.
+                        */
+                       if(show) {
+                               
items_[index]->grid.set_visible(twidget::VISIBLE);
+                       }
+
+                       placement::set_item_shown(index, show);
+
+                       items_[index]->shown = show;
+                       if(!show) {
+                               
items_[index]->grid.set_visible(twidget::INVISIBLE);
+                       }
+
+                       minimum_selection::set_item_shown(index, show);
+               }
+       }
+
+       /** Inherited from tgenerator_. */
+       virtual bool get_item_shown(const unsigned index) const
+       {
+               assert(index < items_.size());
+               return items_[index]->shown;
+       }
+
+
+       /** Inherited from tgenerator_. */
        unsigned get_item_count() const
        {
                return items_.size();
@@ -696,7 +764,7 @@
        void layout_init(const bool full_initialization)
        {
                foreach(titem* item, items_) {
-                       if(item->grid.get_visible() != twidget::INVISIBLE) {
+                       if(item->grid.get_visible() != twidget::INVISIBLE && 
item->shown) {
                                item->grid.layout_init(full_initialization);
                        }
                }
@@ -750,7 +818,7 @@
                assert(this->get_visible() == twidget::VISIBLE);
 
                foreach(titem* item, items_) {
-                       if(item->grid.get_visible() == twidget::VISIBLE) {
+                       if(item->grid.get_visible() == twidget::VISIBLE && 
item->shown) {
                                item->grid.draw_children(frame_buffer);
                        }
                }
@@ -852,6 +920,7 @@
                titem()
                        : grid()
                        , selected(false)
+                       , shown(true)
                {
                }
 
@@ -860,6 +929,17 @@
 
                /** Is the item selected or not. */
                bool selected;
+
+               /**
+                * Is the row shown or not.
+                *
+                * This flag is used the help to set the visible flag, it's 
prefered to
+                * test this flag for external functions.
+                *
+                * @todo functions now test for visible and shown, that can 
use some
+                * polishing.
+                */
+               bool shown;
        };
 
        /** The number of selected items. */




Related Messages


Powered by MHonArc, Updated Sun Dec 20 17:00:18 2009