← Back to team overview

millennium-dev team mailing list archive

[Merge] lp:~sequba/millenniumduel/widgets-margin into lp:millenniumduel

 

Jakub Sękowski has proposed merging lp:~sequba/millenniumduel/widgets-margin into lp:millenniumduel.

Requested reviews:
  Millennium Developers (millennium-dev)
Related bugs:
  Bug #1204286 in Millennium Duel: "Margin should be all Drawables' property."
  https://bugs.launchpad.net/millenniumduel/+bug/1204286

For more details, see:
https://code.launchpad.net/~sequba/millenniumduel/widgets-margin/+merge/177284

Bug #1204286 fix. I've achieve this in exactly the way specified here: https://bugs.launchpad.net/millenniumduel/+bug/1204286/comments/3

Note: Clicking on widget's right or down margin doesn't cause OnMarginClicked to be called. (Bug #1205849)
-- 
https://code.launchpad.net/~sequba/millenniumduel/widgets-margin/+merge/177284
Your team Millennium Developers is requested to review the proposed merge of lp:~sequba/millenniumduel/widgets-margin into lp:millenniumduel.
=== modified file 'src/IBox.cpp'
--- src/IBox.cpp	2013-07-28 08:39:44 +0000
+++ src/IBox.cpp	2013-07-28 14:03:26 +0000
@@ -1,13 +1,13 @@
 #include "IBox.hpp"
 
-IBox::IBox() : _spacing(0), _margin(0) {}
+IBox::IBox() :
+    _spacing(0),
+    _current_width(-1),
+    _current_height(-1) {}
 
 void IBox::SetSpacing(int s){
     _spacing = s;
 }
-void IBox::SetMargin(int m){
-     _margin = m;
-}
 
 void IBox::_RecalculateSizes(int total, int (IDrawable::*size_func)()){
     if(children.size() == 0) return;
@@ -30,7 +30,7 @@
     if (shrinked_sum > 0) shrinked_sum -= _spacing;
     
     if(expand_count > 0){
-        int rest = total - 2*_margin - shrinked_sum;
+        int rest = total - shrinked_sum;
         int expand_size = rest / expand_count;
         int last_expand_size = rest - (expand_size * expand_count) + expand_size;
         

=== modified file 'src/IBox.hpp'
--- src/IBox.hpp	2013-07-28 08:39:44 +0000
+++ src/IBox.hpp	2013-07-28 14:03:26 +0000
@@ -10,18 +10,12 @@
 
 public:
     void SetSpacing(int spacing);
-    void SetMargin(int margin);
 
-    //Drawable Methods:
     virtual void Redraw(int w, int h) = 0;
-    virtual int GetMinimalHeight() = 0;
-    virtual int GetMinimalWidth () = 0;
-    virtual void OnClicked(SCoordinates p) = 0;
 
 protected:
     int _spacing;
-    int _margin;
-    int _current_width, _current_height;
+    int _current_width, _current_height; //just "inner", excluding the margin
 
     void _RecalculateSizes(int total, int (IDrawable::*size_func)());
 };

=== modified file 'src/IDrawable.cpp'
--- src/IDrawable.cpp	2013-07-28 08:39:44 +0000
+++ src/IDrawable.cpp	2013-07-28 14:03:26 +0000
@@ -18,6 +18,7 @@
     _cache_bitmap = al_create_bitmap(0,0);
     
     _background_color = al_map_rgba(0,0,0,0); //fully transparent background by default
+    _margin = 0; // by default: no margin
 }
 
 IDrawable::~IDrawable(){
@@ -37,6 +38,11 @@
     _background_color = al_map_rgba(0,0,0,0);
 }
 
+void IDrawable::SetMargin(int margin)
+{
+     _margin = margin;
+}
+
 void IDrawable::Draw(SDrawArea area){
     if(_needs_redrawing || area.w != _previous_width || area.h != _previous_height){
         // Somethings changed, this drawable's buffer needs to be redrawn
@@ -48,7 +54,10 @@
         UseNewTransformTo(0,0);
         al_clear_to_color(_background_color);
         
-        Redraw(area.w, area.h);
+        UseNewTransformTo(_margin,_margin);
+        Redraw(area.w-2*_margin, area.h-2*_margin);
+        
+        UseNewTransformTo(0,0); // not sure, if needed (?)
         
         al_set_target_bitmap(last_bitmap);
         
@@ -67,9 +76,26 @@
     if(parent != nullptr) parent->SetNeedsRedrawing(); 
 }
 
+int IDrawable::GetMinimalWidth ()
+{
+    return _GetInnerMinimalWidth() + 2*_margin;
+}
+int IDrawable::GetMinimalHeight()
+{
+    return _GetInnerMinimalHeight() + 2*_margin;
+}
+
 void IDrawable::OnClicked(SCoordinates p){
-    OnClicked(); //by default, use the ignoring position version
+    //it wouldn't work properly until IDrawable could determine widget's current size
+    if(p.x <= _margin || p.y <= _margin)
+        _OnMarginClicked();
+    else
+        _OnInnerClicked(SCoordinates(p.x-_margin,p.y-_margin));
 }
 
-void IDrawable::OnClicked(){
+void IDrawable::_OnInnerClicked(SCoordinates p)
+{
+    _OnInnerClicked();
 }
+void IDrawable::_OnInnerClicked() {}
+void IDrawable::_OnMarginClicked() {}

=== modified file 'src/IDrawable.hpp'
--- src/IDrawable.hpp	2013-07-27 14:48:54 +0000
+++ src/IDrawable.hpp	2013-07-28 14:03:26 +0000
@@ -32,32 +32,36 @@
     /* Used to restore the default, transparent background */
     void RemoveBackgroundColor();
 
+    void SetMargin(int margin); // - set widget's margin size
 
     /* This method can be called to draw this widget's graphical contents
      * onto the current surface, in the place specified by the argument
      * (coordinates + size). If possible, this method will avoid rendering
      * the widget and will copy a cached bitmap instead, to maximize efficency.
+     * Also, it takes care of widget's margin.
      * This method should be never overwritten by any widgets. */
     void Draw(SDrawArea area);
     
     /* The redraw(...) method is used to re-render this widget's contents.
      * It is used by draw(...) in case the cached bitmap requires updating.
      * This is the method you should override when creating a custom widget.
-     * The caller takes care of proper target bitmap, transformation and the
-     * background, so you do not need to worry about these - just draw your
-     * widget from (0,0) to (w,h).
+     * The caller takes care of proper target bitmap, transformation, the
+     * background and margin size, so you do not need to worry about these
+     * - just draw your widget from (0,0) to (w,h) (ignoring background and
+     * margin).
      * Looking at other widgets' redraw(...) may be helpful when writing a
      * new widget */ 
     virtual void Redraw(int w, int h) = 0;
     
-    
-    /* Click coordinates are relative to widget position */
-    virtual void OnClicked(SCoordinates p);
-    /* This version ignores position */
-    virtual void OnClicked();
+    /* This method should be called on widget when clicked.
+     * It should be never overwritten by any widgets. */
+    void OnClicked(SCoordinates p); //click coordinates are relative to widget position
 
-    virtual int GetMinimalWidth () = 0;
-    virtual int GetMinimalHeight() = 0;
+    /* These two methods can be used to calculate widget's total size. They
+     * make use of GetInnerMinimalWidth and GetInnerMinimalHeight
+     * They should be never overwritten by any widgets. */
+    int GetMinimalWidth ();
+    int GetMinimalHeight();
     
     /* A helpful macro, used by probably every drawable in its draw() func */
     static void UseNewTransformTo(int x, int y);
@@ -77,6 +81,20 @@
     bool _needs_redrawing;
 private:
     ALLEGRO_COLOR _background_color; 
+    int _margin;
+    
+    /* These methods should specify widget's reaction for click respectively on
+     * it's interior or margin.
+     * These are the methods you should override when creating a custom widget. */
+    virtual void _OnInnerClicked(SCoordinates p);
+    virtual void _OnInnerClicked();
+    virtual void _OnMarginClicked();
+        
+    /* These methods should calculate widget's interior's size (excluding the
+     * margin).
+     * These are the methods you should override when creating a custom widget. */
+    virtual int _GetInnerMinimalWidth () = 0;
+    virtual int _GetInnerMinimalHeight() = 0;
     
     /* The cached appearance of this widget. */
     ALLEGRO_BITMAP* _cache_bitmap;

=== modified file 'src/IOrderedContainer.cpp'
--- src/IOrderedContainer.cpp	2013-07-28 08:39:44 +0000
+++ src/IOrderedContainer.cpp	2013-07-28 14:03:26 +0000
@@ -1,8 +1,6 @@
 #include "IOrderedContainer.hpp"
 #include <iostream>
 
-IOrderedContainer::IOrderedContainer() {};
-
 void IOrderedContainer::Add(IDrawable& item, int position, BoxPackingMode mode, bool is_visible){
     if(item.parent != nullptr){
         std::cerr << "Unable to add item to OrderedContainer: item already has a parent" << std::endl;

=== modified file 'src/IOrderedContainer.hpp'
--- src/IOrderedContainer.hpp	2013-07-28 08:39:44 +0000
+++ src/IOrderedContainer.hpp	2013-07-28 14:03:26 +0000
@@ -10,18 +10,11 @@
 };
 
 class IOrderedContainer : public IDrawable{
-protected:
-    IOrderedContainer();
-    
 public:
     void Add(IDrawable& item, int position, BoxPackingMode mode = BOX_SHRINK, bool is_visible = true);
     void SetVisibility(IDrawable& item, bool isVisible);
     
-    //Drawable Methods:
     virtual void Redraw(int w, int h) = 0;
-    virtual int GetMinimalHeight() = 0;
-    virtual int GetMinimalWidth () = 0;
-    virtual void OnClicked(SCoordinates p) = 0;
 
 protected:
     struct ContainerEntry{

=== modified file 'src/WButton.cpp'
--- src/WButton.cpp	2013-07-28 08:39:44 +0000
+++ src/WButton.cpp	2013-07-28 14:03:26 +0000
@@ -19,12 +19,12 @@
     al_draw_text(CDisplay::main_font, al_map_rgb(255,255,255), w/2, h/2 - 8, ALLEGRO_ALIGN_CENTRE, text.c_str());
 }
 
-void WButton::OnClicked(){
+void WButton::_OnInnerClicked(){
     if(_click_reaction == nullptr) return;
     _click_reaction();
 }
 
-int WButton::GetMinimalHeight() {return 20;}
-int WButton::GetMinimalWidth () {
+int WButton::_GetInnerMinimalHeight() {return 20;}
+int WButton::_GetInnerMinimalWidth () {
     return al_get_text_width(CDisplay::main_font, text.c_str()) + 6; // 3px margin from both sides
 }

=== modified file 'src/WButton.hpp'
--- src/WButton.hpp	2013-07-27 14:48:54 +0000
+++ src/WButton.hpp	2013-07-28 14:03:26 +0000
@@ -7,17 +7,20 @@
 class WButton : public IDrawable{
 public:
     WButton(std::string);
+    
+    std::string text;
+    
     void SetClickReaction(void (*f)());
     
-    std::string text;
-    
     virtual void Redraw(int w, int h) override;
-    virtual void OnClicked() override;
     
-    virtual int GetMinimalHeight() override;
-    virtual int GetMinimalWidth () override;
 private:
     void (*_click_reaction)() = nullptr;
+    
+    virtual void _OnInnerClicked() override;
+    
+    virtual int _GetInnerMinimalHeight() override;
+    virtual int _GetInnerMinimalWidth () override;
 };
 
 #endif //__BUTTON_HPP__

=== modified file 'src/WFixed.cpp'
--- src/WFixed.cpp	2013-07-28 08:39:44 +0000
+++ src/WFixed.cpp	2013-07-28 14:03:26 +0000
@@ -30,7 +30,7 @@
     SetNeedsRedrawing();
 }
 
-void WFixed::OnClicked(SCoordinates p){
+void WFixed::_OnInnerClicked(SCoordinates p){
     for(auto q : items){
         IDrawable* d = q.first;
         SCoordinates c = q.second;
@@ -40,5 +40,5 @@
     }
 }
 
-int WFixed::GetMinimalHeight(){ return _height; }
-int WFixed::GetMinimalWidth() { return _width;  }
+int WFixed::_GetInnerMinimalHeight(){ return _height; }
+int WFixed::_GetInnerMinimalWidth() { return _width;  }

=== modified file 'src/WFixed.hpp'
--- src/WFixed.hpp	2013-07-28 08:39:44 +0000
+++ src/WFixed.hpp	2013-07-28 14:03:26 +0000
@@ -7,18 +7,22 @@
 class WFixed : public IDrawable{
 public:
     WFixed(int w = 0, int h = 0);
-    void Redraw(int w, int h);
+    
     void AddItem(IDrawable& d, SCoordinates c);
     void RemoveItem(IDrawable& d);
-    void OnClicked(SCoordinates p);
-    
-    virtual int GetMinimalWidth () override;
-    virtual int GetMinimalHeight() override;
+    
+    void Redraw(int w, int h);
+    
 private:
     std::map<IDrawable*,SCoordinates> items;
     
     int _width;
     int _height;
+
+    virtual void _OnInnerClicked(SCoordinates p);
+
+    virtual int _GetInnerMinimalWidth () override;
+    virtual int _GetInnerMinimalHeight() override;
 };
 
 #endif //__WFIXED_HPP__

=== modified file 'src/WHBox.cpp'
--- src/WHBox.cpp	2013-07-28 09:06:58 +0000
+++ src/WHBox.cpp	2013-07-28 14:03:26 +0000
@@ -5,14 +5,13 @@
     
     _RecalculateSizes(w, &IDrawable::GetMinimalWidth);
 
-    int x = _margin, y = _margin;
+    int x =0, y =0;
     
     for(auto i : children){
         if(!i.is_visible) continue; //an unvisible widget. skip it...
         
         int width  = i.size;
-        int height = h - 2*_margin;
-        
+        int height = h;
         
         i.item.Draw(SDrawArea(x,y,width,height));
         
@@ -25,17 +24,17 @@
     _current_width  = w;
 }
 
-int WHBox::GetMinimalHeight() {
+int WHBox::_GetInnerMinimalHeight() {
     // Maximum of all items' heights
     int max = 0;
     for(auto i : children){
         int h = i.item.GetMinimalHeight();
         if (h > max) max = h;
     }
-    return max + 2*_margin;
+    return max;
     
 }
-int WHBox::GetMinimalWidth () {
+int WHBox::_GetInnerMinimalWidth () {
     // Sum of all items' widths and gaps
     int sum = 0;
     for(auto i : children){
@@ -43,12 +42,12 @@
         sum += _spacing;
     }
     if(sum >= 0) sum -= _spacing;
-    return sum + 2*_margin;
+    return sum;
 }
 
-void WHBox::OnClicked(SCoordinates p){
-    if (p.x < _margin || p.y < _margin || _current_height - _margin <= p.y) return;
-    int sum = _margin;
+void WHBox::_OnInnerClicked(SCoordinates p){
+    if(_current_height < p.y || _current_width < p.x) return; //call _OnMarginClicked() ?
+    int sum=0;
     for(auto i : children){
         if(!i.is_visible) continue;
         if(p.x < sum + i.size) { // This item was clicked!
@@ -58,7 +57,7 @@
         sum += i.size;
         sum += _spacing;
         if(p.x < sum){ // Empty space between items was clicked
-            return;
+            return; //call _OnMarginClicked() ?
         }
     }
 }

=== modified file 'src/WHBox.hpp'
--- src/WHBox.hpp	2013-07-27 17:01:04 +0000
+++ src/WHBox.hpp	2013-07-28 14:03:26 +0000
@@ -7,10 +7,11 @@
 public:
     virtual void Redraw(int w, int h) override;
     
-    virtual int GetMinimalHeight() override;
-    virtual int GetMinimalWidth () override;
-    
-    virtual void OnClicked(SCoordinates p) override;
+private:
+    virtual void _OnInnerClicked(SCoordinates p) override;
+
+    virtual int _GetInnerMinimalHeight() override;
+    virtual int _GetInnerMinimalWidth () override;
 };
 
 #endif //__WHBOX_HPP__

=== modified file 'src/WImage.cpp'
--- src/WImage.cpp	2013-07-28 08:39:44 +0000
+++ src/WImage.cpp	2013-07-28 14:03:26 +0000
@@ -16,8 +16,8 @@
     al_destroy_bitmap(_bitmap);
 }
 
-int WImage::GetMinimalHeight(){return _height;}
-int WImage::GetMinimalWidth (){return _width; }
+int WImage::_GetInnerMinimalHeight(){return _height;}
+int WImage::_GetInnerMinimalWidth (){return _width; }
 void WImage::SetMinimalHeight(int h) {_height = h; SetNeedsRedrawing();}
 void WImage::SetMinimalWidth (int w) {_width  = w; SetNeedsRedrawing();}
 

=== modified file 'src/WImage.hpp'
--- src/WImage.hpp	2013-07-28 08:39:44 +0000
+++ src/WImage.hpp	2013-07-28 14:03:26 +0000
@@ -14,9 +14,6 @@
     
     virtual void Redraw(int w, int h) override;
     
-    virtual int GetMinimalHeight() override;
-    virtual int GetMinimalWidth () override;
-    
     void SetMinimalHeight(int);
     void SetMinimalWidth (int);
 private:
@@ -28,6 +25,8 @@
     int _bitmap_height;
     int _bitmap_width;
     
+    virtual int _GetInnerMinimalHeight() override;
+    virtual int _GetInnerMinimalWidth () override;
 };
 
 #endif //__WIMAGE_HPP__

=== modified file 'src/WLabel.cpp'
--- src/WLabel.cpp	2013-07-28 08:39:44 +0000
+++ src/WLabel.cpp	2013-07-28 14:03:26 +0000
@@ -8,17 +8,15 @@
 //extern ALLEGRO_FONT* main_font;
 
 
-WLabel::WLabel(std::string _text) : text(_text){
-
-}
+WLabel::WLabel(std::string _text) : text(_text) {}
 
 void WLabel::Redraw(int w, int h){
     ALLEGRO_COLOR white = al_map_rgb(255, 255, 255);
     al_draw_text(CDisplay::main_font, white, w/2, h/2 - 8 ,ALLEGRO_ALIGN_CENTRE, text.c_str());
 }
 
-int WLabel::GetMinimalHeight() {return 20;}
-int WLabel::GetMinimalWidth () {
+int WLabel::_GetInnerMinimalHeight() {return 20;}
+int WLabel::_GetInnerMinimalWidth () {
     return al_get_text_width(CDisplay::main_font, text.c_str()) + 6; // 3px margin from both sides
 }
 

=== modified file 'src/WLabel.hpp'
--- src/WLabel.hpp	2013-07-27 14:48:54 +0000
+++ src/WLabel.hpp	2013-07-28 14:03:26 +0000
@@ -10,9 +10,10 @@
     std::string text;
     
     virtual void Redraw(int w, int h) override;
-    
-    virtual int GetMinimalHeight() override;
-    virtual int GetMinimalWidth () override;
+
+private:
+    virtual int _GetInnerMinimalHeight() override;
+    virtual int _GetInnerMinimalWidth () override;
 };
 
 #endif //__WLABEL_HPP__

=== modified file 'src/WLayeredView.cpp'
--- src/WLayeredView.cpp	2013-07-28 08:39:44 +0000
+++ src/WLayeredView.cpp	2013-07-28 14:03:26 +0000
@@ -1,7 +1,5 @@
 #include "WLayeredView.hpp"
 
-WLayeredView::WLayeredView() {}
-
 void WLayeredView::ActivateLayer(IDrawable& layer)
 {
     //move layer to the front and make it visible
@@ -41,7 +39,7 @@
             i->item.Draw(SDrawArea(0,0,w,h));
 }
 
-void WLayeredView::OnClicked(SCoordinates p)
+void WLayeredView::_OnInnerClicked(SCoordinates p)
 {
     //pass "click signal" to currently active layer
     for(auto i = children.begin() ; i != children.end() ; i++)
@@ -52,18 +50,7 @@
         }
 }
 
-void WLayeredView::OnClicked()
-{
-    //pass "click signal" to currently active layer
-    for(auto i = children.begin() ; i != children.end() ; i++)
-        if(i->is_visible)
-        {
-            i->item.OnClicked();
-            break;        
-        }
-}
-
-int WLayeredView::GetMinimalWidth()
+int WLayeredView::_GetInnerMinimalWidth()
 {
     //find maximum of layers' widths
     int max = 0;
@@ -75,7 +62,7 @@
     return max;
 }
 
-int WLayeredView::GetMinimalHeight()
+int WLayeredView::_GetInnerMinimalHeight()
 {
     //find maximum of layers' heights
     int max = 0;

=== modified file 'src/WLayeredView.hpp'
--- src/WLayeredView.hpp	2013-07-27 17:01:04 +0000
+++ src/WLayeredView.hpp	2013-07-28 14:03:26 +0000
@@ -6,19 +6,16 @@
 class WLayeredView : public IOrderedContainer
 {
 public:
-    WLayeredView();
-
     void ActivateLayer(IDrawable& layer);
     void HideLayer(IDrawable& layer);
     void SetOnlyVisibleLayer(IDrawable& layer);
 
-    //Drawable Methods:
     virtual void Redraw(int w, int h);
-    virtual void OnClicked(SCoordinates p);
-    virtual void OnClicked();
-    
-    virtual int GetMinimalWidth ();
-    virtual int GetMinimalHeight();
+private:
+    virtual void _OnInnerClicked(SCoordinates p);
+
+    virtual int _GetInnerMinimalWidth ();
+    virtual int _GetInnerMinimalHeight();
 };
 
 #endif // __WLAYEREDVIEW_HPP__

=== modified file 'src/WVBox.cpp'
--- src/WVBox.cpp	2013-07-28 09:06:58 +0000
+++ src/WVBox.cpp	2013-07-28 14:03:26 +0000
@@ -5,15 +5,14 @@
     
     _RecalculateSizes(h, &IDrawable::GetMinimalHeight);
     
-    int x =  _margin, y = _margin;
+    int x =0, y=0;
     
     for(auto i : children){
         if(!i.is_visible) continue; //an unvisible widget. skip it...
         
-        int width  = w - 2*_margin;
+        int width  = w;
         int height = i.size;
         
-        
         i.item.Draw(SDrawArea(x,y,width,height));
         
         y += i.size;
@@ -26,7 +25,7 @@
     
 }
 
-int WVBox::GetMinimalHeight() {
+int WVBox::_GetInnerMinimalHeight() {
     // Sum of all items' heights and gaps
     int sum = 0;
     for(auto i : children){
@@ -34,21 +33,21 @@
         sum += _spacing;
     }
     if(sum >= 0) sum -= _spacing;
-    return sum + 2*_margin;
+    return sum;
 }
-int WVBox::GetMinimalWidth () {
+int WVBox::_GetInnerMinimalWidth () {
     // Maximum of all items' widths
     int max = 0;
     for(auto i : children){
         int w = i.item.GetMinimalWidth();
         if (w > max) max = w;
     }
-    return max + 2*_margin;
+    return max;
 }
 
-void WVBox::OnClicked(SCoordinates p){
-    if (p.y < _margin || p.x < _margin || _current_width - _margin <= p.x) return;
-    int sum = _margin;
+void WVBox::_OnInnerClicked(SCoordinates p){
+    if(_current_height < p.y || _current_width < p.x) return; //call _OnMarginClicked() ?
+    int sum=0;
     for(auto i : children){
         if(!i.is_visible) continue;
         if(p.y < sum + i.size) { // This item was clicked!
@@ -58,7 +57,7 @@
         sum += i.size;
         sum += _spacing;
         if(p.y < sum){ // Empty space between items was clicked
-            return;
+            return; //call _OnMarginClicked() ?
         }
     }
 }

=== modified file 'src/WVBox.hpp'
--- src/WVBox.hpp	2013-07-27 17:01:04 +0000
+++ src/WVBox.hpp	2013-07-28 14:03:26 +0000
@@ -7,10 +7,11 @@
 public:
     virtual void Redraw(int w, int h) override;
     
-    virtual int GetMinimalHeight() override;
-    virtual int GetMinimalWidth () override;
-    
-    virtual void OnClicked(SCoordinates p) override;
+private:
+    virtual void _OnInnerClicked(SCoordinates p) override;
+
+    virtual int _GetInnerMinimalHeight() override;
+    virtual int _GetInnerMinimalWidth () override;
 };
 
 #endif //__WVBOX_HPP__


Follow ups