Author: mordante
Date: Thu May  1 08:32:54 2008
New Revision: 26269

URL: http://svn.gna.org/viewcvs/wesnoth?rev=26269&view=rev
Log:
Now calculate the best sizes for grids and panels.

Modified:
    trunk/src/gui/widgets/grid.cpp
    trunk/src/gui/widgets/grid.hpp
    trunk/src/gui/widgets/helper.cpp
    trunk/src/gui/widgets/helper.hpp
    trunk/src/gui/widgets/panel.cpp
    trunk/src/gui/widgets/panel.hpp
    trunk/src/gui/widgets/window.hpp

Modified: trunk/src/gui/widgets/grid.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/grid.cpp?rev=26269&r1=26268&r2=26269&view=diff
==============================================================================
--- trunk/src/gui/widgets/grid.cpp (original)
+++ trunk/src/gui/widgets/grid.cpp Thu May  1 08:32:54 2008
@@ -52,8 +52,8 @@
        minimum_col_width_(),
        row_height_(),
        col_width_(),
-       row_grow_factor_(),
-       col_grow_factor_(),
+       row_grow_factor_(rows),
+       col_grow_factor_(cols),
        children_(rows * cols)
 {
 }
@@ -119,7 +119,6 @@
 
 void tgrid::set_rows_cols(const unsigned rows, const unsigned cols)
 {
-
        if(rows == rows_ && cols == cols_) {
                return;
        }
@@ -164,49 +163,78 @@
        }
 }
 
+tpoint tgrid::get_minimum_size() const
+{
+       return get_size("minimum", minimum_col_width_, 
+               minimum_row_height_, &tchild::get_minimum_size);
+}
+
+tpoint tgrid::get_maximum_size() const
+{
+       // A grid doesn't have a maximum size.
+       return tpoint(0,0);
+}
+
 tpoint tgrid::get_best_size() const
 {
-
-       if(best_row_height_.empty() || best_col_width_.empty()) {
-
-               DBG_G << "Grid: calculate best size.\n";
-
-               best_row_height_.resize(rows_, 0);
-               best_col_width_.resize(cols_, 0);
+       return get_size("best", best_col_width_, 
+               best_row_height_, &tchild::get_best_size);
+}
+
+//! Helper function to get the best or minimum size.
+//!
+//! @param id                     Name to use in debug output.
+//! @param width                  Reference to the vector width cache for the 
+//!                               size function of the caller.
+//! @param height                 Reference to the vector height cache for the 
+//!                               size function of the caller.
+//! @param size_proc              The function to call on the cells in order to
+//!                               get their sizes.
+//!
+//! @return                       The wanted size.
+tpoint tgrid::get_size(const std::string& id, std::vector<unsigned>& width, 
+               std::vector<unsigned>& height, tpoint (tchild::*size_proc)() 
const) const
+{
+       if(height.empty() || width.empty()) {
+
+               DBG_G << "Grid: calculate " << id << " size.\n";
+
+               height.resize(rows_, 0);
+               width.resize(cols_, 0);
                
-               // First get the best sizes for all items.
+               // First get the sizes for all items.
                for(unsigned row = 0; row < rows_; ++row) {
                        for(unsigned col = 0; col < cols_; ++col) {
 
-                               tpoint size = child(row, col).get_best_size();
-
-                               if(size.x > best_col_width_[col]) {
-                                       best_col_width_[col] = size.x;
+                               const tpoint size = (child(row, 
col).*size_proc)();
+
+                               if(size.x > width[col]) {
+                                       width[col] = size.x;
                                }
 
-                               if(size.y > best_row_height_[row]) {
-                                       best_row_height_[row] = size.y;
+                               if(size.y > height[row]) {
+                                       height[row] = size.y;
                                }
 
                        }
                }
        } else {
-               DBG_G << "Grid: used cached best size.\n";
+               DBG_G << "Grid: used cached " << id << " size.\n";
        }
 
        for(unsigned row = 0; row < rows_; ++row) {
-               DBG_G << "Grid: the best height for row " << row 
-                       << " will be " << best_row_height_[row] << ".\n";
+               DBG_G << "Grid: the " << id << " height for row " << row 
+                       << " will be " << height[row] << ".\n";
        }
 
        for(unsigned col = 0; col < cols_; ++col) {
-               DBG_G << "Grid: the best width for col " << col 
-                       << " will be " << best_col_width_[col]  << ".\n";
+               DBG_G << "Grid: the " << id << " width for col " << col 
+                       << " will be " << width[col]  << ".\n";
        }
 
        return tpoint(
-               std::accumulate(best_col_width_.begin(), best_col_width_.end(), 
0),
-               std::accumulate(best_row_height_.begin(), 
best_row_height_.end(), 0));
+               std::accumulate(width.begin(), width.end(), 0),
+               std::accumulate(height.begin(), height.end(), 0));
 }
 
 void tgrid::set_size(const SDL_Rect& rect)
@@ -230,6 +258,8 @@
        assert(col_width_.size() == cols_);
        assert(row_grow_factor_.size() == rows_);
        assert(col_grow_factor_.size() == cols_);
+       DBG_G << "Grid: best size " << best_size << " available size " << size 
<< ".\n";
+
        if(best_size == size) {
                row_height_ = best_row_height_;
                col_width_ = best_col_width_;
@@ -245,8 +275,10 @@
                // expand it.
                if(size.x > best_size.x) {
                        const unsigned w = size.x - best_size.x;
-                       unsigned w_size = 
std::accumulate(col_grow_factor_.begin(), col_grow_factor_.end(), 0);
-                       DBG_G << "Grid: extra width " << w << " will be divided 
amount " << w_size << " units in " << cols_ << " columns.\n";
+                       unsigned w_size = 
+                               std::accumulate(col_grow_factor_.begin(), 
col_grow_factor_.end(), 0);
+                       DBG_G << "Grid: extra width " << w << " will be divided 
amount " 
+                               << w_size << " units in " << cols_ << " 
columns.\n";
 
                        if(w_size == 0) {
                                // If all sizes are 0 reset them to 1
@@ -268,8 +300,10 @@
 
                if(size.y > best_size.y) {
                        const unsigned h = size.y - best_size.y;
-                       unsigned h_size = 
std::accumulate(row_grow_factor_.begin(), row_grow_factor_.end(), 0);
-                       DBG_G << "Grid: extra height " << h << " will be 
divided amount " << h_size << " units in " << rows_ << " rows.\n";
+                       unsigned h_size = 
+                               std::accumulate(row_grow_factor_.begin(), 
row_grow_factor_.end(), 0);
+                       DBG_G << "Grid: extra height " << h << " will be 
divided amount " 
+                               << h_size << " units in " << rows_ << " 
rows.\n";
 
                        if(h_size == 0) {
                                // If all sizes are 0 reset them to 1
@@ -291,7 +325,6 @@
                layout(orig);
                return;
 
-
        }
 
 
@@ -397,24 +430,62 @@
 tpoint tgrid::tchild::get_best_size() const
 {
        if(!widget_) {
+               return border_space();
+       }
+
+       if(widget_->dirty() || best_size_ == tpoint(0, 0)) {
+               best_size_ = widget_->get_best_size() + border_space();
+       }
+
+       return best_size_;
+}
+
+tpoint tgrid::tchild::get_minimum_size() const
+{
+       if(!widget_) {
+               return border_space();
+       }
+
+       if(widget_->dirty() || minimum_size_ == tpoint(0, 0)) {
+               minimum_size_ = widget_->get_minimum_size() + border_space();
+       }
+
+       return minimum_size_;
+}
+
+tpoint tgrid::tchild::get_maximum_size() const
+{
+       if(!widget_) {
                return tpoint(0, 0);
        }
 
-       if(widget_->dirty() || best_size_ == tpoint(0, 0)) {
-
-               best_size_ = widget_->get_best_size();
-
-               if(border_size_) {
-
-                       if(flags_ & BORDER_TOP) best_size_.y += border_size_;
-                       if(flags_ & BORDER_BOTTOM) best_size_.y += border_size_;
-                                       
-                       if(flags_ & BORDER_LEFT) best_size_.x += border_size_;
-                       if(flags_ & BORDER_RIGHT) best_size_.x += border_size_;
-               }
-       }
-
-       return best_size_;
+       if(widget_->dirty() || maximum_size_ == tpoint(0, 0)) {
+               maximum_size_ = widget_->get_maximum_size();
+
+               // If the widget has no maximum return that 
+               // else we need to add the border.
+               if(maximum_size_ != tpoint(0, 0)) {
+                       maximum_size_ += border_space();
+               }
+       }
+
+       return maximum_size_;
+}
+
+tpoint tgrid::tchild::border_space() const
+{
+       tpoint result(0, 0);
+
+       if(border_size_) {
+
+               if(flags_ & BORDER_TOP) result.y += border_size_;
+               if(flags_ & BORDER_BOTTOM) result.y += border_size_;
+                               
+               if(flags_ & BORDER_LEFT) result.x += border_size_;
+               if(flags_ & BORDER_RIGHT) result.x += border_size_;
+       }
+
+       return result;
 }
 
 void tgrid::tchild::set_size(tpoint orig, tpoint size)
@@ -472,7 +543,8 @@
                } else {
                        widget_size.x = size.x;
                }
-               DBG_G << "Grid cell: horizontal growing from " << best_size.x 
<< " to " << widget_size.x << ".\n";
+               DBG_G << "Grid cell: horizontal growing from " 
+                       << best_size.x << " to " << widget_size.x << ".\n";
        }
 
        if(flags_ & VERTICAL_GROW_SEND_TO_CLIENT) {
@@ -481,7 +553,8 @@
                } else {
                        widget_size.y = size.y;
                }
-               DBG_G << "Grid cell: vertical growing from " << best_size.y << 
" to " << widget_size.y << ".\n";
+               DBG_G << "Grid cell: vertical growing from " 
+                       << best_size.y << " to " << widget_size.y << ".\n";
        }
 
        if((flags_ & VERTICAL_ALIGN_TOP) == VERTICAL_ALIGN_TOP) {

Modified: trunk/src/gui/widgets/grid.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/grid.hpp?rev=26269&r1=26268&r2=26269&view=diff
==============================================================================
--- trunk/src/gui/widgets/grid.hpp (original)
+++ trunk/src/gui/widgets/grid.hpp Thu May  1 08:32:54 2008
@@ -79,9 +79,9 @@
        void remove_child(const std::string& id, const bool find_all = false);
 
        //! Inherited from twidget.
-       tpoint get_minimum_size() const { /*FIXME IMPLEMENT*/ return 
tpoint(0,0); } 
+       tpoint get_minimum_size() const;
        tpoint get_best_size() const;
-       tpoint get_maximum_size() const { /*FIXME IMPLEMENT*/ return 
tpoint(0,0); }
+       tpoint get_maximum_size() const;
 
        //! Inherited from twidget.
        void set_size(const SDL_Rect& rect);
@@ -108,6 +108,7 @@
                        widget_(0),
                        best_size_(0, 0),
                        minimum_size_(0, 0),
+                       maximum_size_(0, 0),
                        clip_()
 
                        // Fixme make a class wo we can store some properties 
in the cache 
@@ -127,9 +128,14 @@
                twidget* widget() { return widget_; }
                void set_widget(twidget* widget) { widget_ = widget; 
set_dirty(); }
 
-               //! Gets the best size for the cell, not const since we modify 
the internal
-               //! state, might use mutable later (if really needed).
+               //! Returns the best size for the cell.
                tpoint get_best_size() const;
+
+               //! Returns the minimum size for the cell.
+               tpoint get_minimum_size() const;
+
+               //! Returns the maximum size for the cell.
+               tpoint get_maximum_size() const;
 
                void set_size(tpoint orig, tpoint size);
 
@@ -153,6 +159,12 @@
 
                //! The minimum size for this cell, like best_size_.
                mutable tpoint minimum_size_;
+
+               //! The maximum size for this cell, like best_size_.
+               mutable tpoint maximum_size_;
+
+               //! Returns the space needed for the border.
+               tpoint border_space() const;
 
                //! The clipping area for the widget. This is also the size of 
                //! the container.
@@ -164,6 +176,7 @@
                { 
                        best_size_ = tpoint(0, 0); 
                        minimum_size_ = tpoint(0, 0); 
+                       maximum_size_ = tpoint(0, 0);
                }
 
        }; // class tchild
@@ -200,12 +213,12 @@
        unsigned cols_;
 
        //! The optimal row heights / col widths.
-       mutable std::vector<unsigned> best_row_height_; //FIXME implement
-       mutable std::vector<unsigned> best_col_width_; //FIXME implement
+       mutable std::vector<unsigned> best_row_height_;
+       mutable std::vector<unsigned> best_col_width_;
 
        //! The minimal row heights / col widths.
-       mutable std::vector<unsigned> minimum_row_height_; //FIXME implement
-       mutable std::vector<unsigned> minimum_col_width_; //FIXME implement
+       mutable std::vector<unsigned> minimum_row_height_;
+       mutable std::vector<unsigned> minimum_col_width_;
 
        //! The row heights / col widths currently used.
        std::vector<unsigned> row_height_;
@@ -225,6 +238,10 @@
        void clear_cache();
 
        void layout(const tpoint& origin);
+
+       //! Helper function to get the best or minimum size.
+       tpoint get_size(const std::string& id, std::vector<unsigned>& width, 
+               std::vector<unsigned>& height, tpoint (tchild::*size_proc)() 
const) const;
 };
 
 } // namespace gui2

Modified: trunk/src/gui/widgets/helper.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/helper.cpp?rev=26269&r1=26268&r2=26269&view=diff
==============================================================================
--- trunk/src/gui/widgets/helper.cpp (original)
+++ trunk/src/gui/widgets/helper.cpp Thu May  1 08:32:54 2008
@@ -62,6 +62,12 @@
 { 
        return ::create_rect(origin.x, origin.y, size.x, size.y); 
 }
+tpoint& tpoint::operator+=(const tpoint& point)
+{ 
+       x += point.x; 
+       y += point.y;
+       return *this;
+}
 
 std::ostream &operator<<(std::ostream &stream, const tpoint& point)
 {

Modified: trunk/src/gui/widgets/helper.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/helper.hpp?rev=26269&r1=26268&r2=26269&view=diff
==============================================================================
--- trunk/src/gui/widgets/helper.hpp (original)
+++ trunk/src/gui/widgets/helper.hpp Thu May  1 08:32:54 2008
@@ -35,12 +35,18 @@
        int y;
 
        bool operator==(const tpoint& point) const { return x == point.x && y 
== point.y; }
+       bool operator!=(const tpoint& point) const { return x != point.x || y 
!= point.y; }
        bool operator<(const tpoint& point) const 
                { return x < point.x || (x == point.x && y < point.y); }
 
        bool operator<=(const tpoint& point) const 
                { return x < point.x || (x == point.x && y <= point.y); }
                
+       tpoint operator+(const tpoint& point) const 
+               { return tpoint(x + point.x, y + point.y); }
+
+       tpoint& operator+=(const tpoint& point);
+
 };
 
 

Modified: trunk/src/gui/widgets/panel.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/panel.cpp?rev=26269&r1=26268&r2=26269&view=diff
==============================================================================
--- trunk/src/gui/widgets/panel.cpp (original)
+++ trunk/src/gui/widgets/panel.cpp Thu May  1 08:32:54 2008
@@ -38,6 +38,38 @@
 
 namespace gui2 {
 
+tpoint tpanel::get_minimum_size() const
+{
+       const tpoint max_size = get_maximum_size();
+       tpoint size = grid_.get_minimum_size() + border_space();
+
+       if(max_size.x) {
+               size.x = minimum(size.x, max_size.x);
+       }
+
+       if(max_size.y) {
+               size.y = minimum(size.y, max_size.y);
+       }
+
+       return size;
+}
+
+tpoint tpanel::get_best_size() const
+{
+       const tpoint max_size = get_maximum_size();
+       tpoint size = grid_.get_best_size() + border_space();
+
+       if(max_size.x) {
+               size.x = minimum(size.x, max_size.x);
+       }
+
+       if(max_size.y) {
+               size.y = minimum(size.y, max_size.y);
+       }
+
+       return size;
+}
+
 void tpanel::draw(surface& surface)
 {
        // Need to preserve the state and inherited draw clear the flag.
@@ -60,7 +92,8 @@
 
 SDL_Rect tpanel::get_client_rect() const
 {
-       const tpanel_definition::tresolution* conf = dynamic_cast<const 
tpanel_definition::tresolution*>(config());
+       const tpanel_definition::tresolution* conf = 
+               dynamic_cast<const tpanel_definition::tresolution*>(config());
        assert(conf);
 
        SDL_Rect result = get_rect();
@@ -79,5 +112,15 @@
        grid_.set_size(get_client_rect());
 }
 
+tpoint tpanel::border_space() const
+{
+       const tpanel_definition::tresolution* conf = 
+               dynamic_cast<const tpanel_definition::tresolution*>(config());
+       assert(conf);
+
+       return tpoint(conf->left_border + conf->right_border,
+               conf->top_border + conf->bottom_border);
+}
+
 } // namespace gui2
 

Modified: trunk/src/gui/widgets/panel.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/panel.hpp?rev=26269&r1=26268&r2=26269&view=diff
==============================================================================
--- trunk/src/gui/widgets/panel.hpp (original)
+++ trunk/src/gui/widgets/panel.hpp Thu May  1 08:32:54 2008
@@ -52,6 +52,12 @@
        unsigned get_state() const { return 0; }
 
        //! Inherited from tcontrol.
+       tpoint get_minimum_size() const;
+       tpoint get_best_size() const;
+
+       // get_maximum_size is inherited from tcontrol.
+
+       //! Inherited from tcontrol.
        void draw(surface& surface);
 
        //! Inherited from tcontrol.
@@ -90,6 +96,9 @@
        //! Inherited from tcontrol.
        const std::string& get_control_type() const 
                { static const std::string type = "panel"; return type; }
+
+       //! Returns the space used by the border.
+       tpoint border_space() const;
 }; 
 
 } // namespace gui2

Modified: trunk/src/gui/widgets/window.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window.hpp?rev=26269&r1=26268&r2=26269&view=diff
==============================================================================
--- trunk/src/gui/widgets/window.hpp (original)
+++ trunk/src/gui/widgets/window.hpp Thu May  1 08:32:54 2008
@@ -94,11 +94,6 @@
        //! Inherited from tpanel.
        void draw(surface& surface);
 
-       //! Inherited from twidget.
-       tpoint get_minimum_size() const { /*FIXME IMPLEMENT*/ return 
tpoint(0,0); } 
-       tpoint get_best_size() const { /*FIXME IMPLEMENT*/ return tpoint(0,0); 
} 
-       tpoint get_maximum_size() const { /*FIXME IMPLEMENT*/ return 
tpoint(0,0); }
-
        //! Gets the coordinates of the client area, for external use the height
        //! and the width are the most interesting things.
        //FIXME this can be removed it the panel defintion inherites from panel 
defintion


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to