← Back to team overview

millennium-dev team mailing list archive

[Merge] lp:~sequba/millenniumduel/layered-views into lp:millenniumduel

 

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

Requested reviews:
  Rafał Cieślak (rafalcieslak256)

For more details, see:
https://code.launchpad.net/~sequba/millenniumduel/layered-views/+merge/176733

This branch contains widget LayeredView, which implements displaying multiple Drawables as layers (partially hiding behind one another). - (blueprint: Layered views)

Also, I separated class Box into OrderedContainer (implementing general container features) and new Box class, which is deriving from OrderedContainer.
-- 
https://code.launchpad.net/~sequba/millenniumduel/layered-views/+merge/176733
Your team Millennium Developers is subscribed to branch lp:millenniumduel.
=== added file 'src/Box.cpp'
--- src/Box.cpp	1970-01-01 00:00:00 +0000
+++ src/Box.cpp	2013-07-24 16:18:00 +0000
@@ -0,0 +1,49 @@
+#include "Box.hpp"
+
+Box::Box() : spacing(0), margin(0) {}
+
+void Box::SetSpacing(int s){
+    spacing = s;
+}
+void Box::SetMargin(int m){
+     margin = m;
+}
+
+void Box::_RecalculateSizes(int total, int (Drawable::*size_func)()){
+    if(children.size() == 0) return;
+
+    int shrinked_sum = 0;
+    int expand_count = 0;
+    
+    for(auto &i : children){
+        if(i.mode == BOX_SHRINK){
+            int h = ((i.item).*size_func)(); //use the size_func to get minimal width/height
+            i.size = h;
+            shrinked_sum += h;
+        }else if(i.mode == BOX_EXPAND){
+            expand_count++;
+        }
+        shrinked_sum += spacing;
+    }
+    if (shrinked_sum > 0) shrinked_sum -= spacing;
+    
+    if(expand_count > 0){
+        int rest = total - 2*margin - shrinked_sum;
+        int expand_size = rest / expand_count;
+        int last_expand_size = rest - (expand_size * expand_count) + expand_size;
+        
+        if(expand_size < 0) expand_size = 0;
+        if(last_expand_size < 0) last_expand_size = 0;
+        
+        for(auto &i : children){
+            if(i.mode == BOX_EXPAND){
+                if(expand_count != 1){ //this is not the last expanding item
+                    i.size = expand_size;
+                }else{
+                    i.size = last_expand_size;
+                }
+                expand_count--;
+            }
+        }
+    }
+}

=== added file 'src/Box.hpp'
--- src/Box.hpp	1970-01-01 00:00:00 +0000
+++ src/Box.hpp	2013-07-24 16:18:00 +0000
@@ -0,0 +1,29 @@
+#ifndef BOX_HPP
+#define BOX_HPP
+
+#include "OrderedContainer.hpp"
+    
+class Box : public OrderedContainer
+{
+protected:
+    Box();
+
+public:
+    void SetSpacing(int spacing);
+    void SetMargin(int margin);
+
+    //Drawable Methods:
+    virtual void draw(SDrawArea a) = 0;
+    virtual int GetMinimalHeight() = 0;
+    virtual int GetMinimalWidth () = 0;
+    virtual void on_clicked(SCoordinates p) = 0;
+
+protected:
+    int spacing;
+    int margin;
+    int current_width, current_height;
+
+    void _RecalculateSizes(int total, int (Drawable::*size_func)());
+};
+
+#endif //#ifndef BOX_HPP

=== added file 'src/LayeredView.cpp'
--- src/LayeredView.cpp	1970-01-01 00:00:00 +0000
+++ src/LayeredView.cpp	2013-07-24 16:18:00 +0000
@@ -0,0 +1,84 @@
+#include "LayeredView.hpp"
+
+LayeredView::LayeredView() {}
+
+void LayeredView::ActivateLayer(Drawable& layer)
+{
+    //move layer to the front and make it visible
+    for(auto i : children)
+        if(&i.item == &layer)
+        {
+            children.erase(i);
+            i.order = children.begin()->order - 1;
+            i.is_visible = true;            
+            children.insert(i);
+            break;
+        }
+}
+
+void LayeredView::HideLayer(Drawable& layer)
+{
+    SetVisibility(layer,false);
+}
+
+void LayeredView::SetOnlyVisibleLayer(Drawable& layer)
+{
+    for(auto &i : children)
+        if(&i.item == &layer)
+            i.is_visible = true;            
+        else
+            i.is_visible = false;
+}
+
+void LayeredView::draw(SDrawArea area)
+{
+    for(auto i = children.rbegin() ; i != children.rend() ; i++)
+        if(i->is_visible)
+            i->item.draw(area);
+}
+
+void LayeredView::on_clicked(SCoordinates p)
+{
+    //pass "click signal" to currently active layer
+    for(auto i = children.begin() ; i != children.end() ; i++)
+        if(i->is_visible)
+        {
+            i->item.on_clicked(p);
+            break;
+        }
+}
+
+void LayeredView::on_clicked()
+{
+    //pass "click signal" to currently active layer
+    for(auto i = children.begin() ; i != children.end() ; i++)
+        if(i->is_visible)
+        {
+            i->item.on_clicked();
+            break;        
+        }
+}
+
+int LayeredView::GetMinimalWidth()
+{
+    //find maximum of layers' widths
+    int max = 0;
+    for(auto i : children)
+    {
+        int x = i.item.GetMinimalWidth();
+        if(x > max) max = x;
+    }
+    return max;
+}
+
+int LayeredView::GetMinimalHeight()
+{
+    //find maximum of layers' heights
+    int max = 0;
+    for(auto i : children)
+    {
+        int x = i.item.GetMinimalHeight();
+        if(x > max) max = x;
+    }
+    return max;
+}

=== added file 'src/LayeredView.hpp'
--- src/LayeredView.hpp	1970-01-01 00:00:00 +0000
+++ src/LayeredView.hpp	2013-07-24 16:18:00 +0000
@@ -0,0 +1,23 @@
+#ifndef LAYEREDVIEW_HPP
+#define LAYEREDVIEW_HPP
+
+#include "OrderedContainer.hpp"
+
+class LayeredView : public OrderedContainer
+{
+public:
+    LayeredView();
+
+    void ActivateLayer(Drawable& layer);
+    void HideLayer(Drawable& layer);
+    void SetOnlyVisibleLayer(Drawable& layer);
+
+    //Drawable Methods:
+    virtual void draw(SDrawArea area);
+    virtual void on_clicked(SCoordinates p);
+    virtual void on_clicked();
+    virtual int GetMinimalWidth ();
+    virtual int GetMinimalHeight();
+};
+
+#endif //#ifndef LAYEREDVIEW_HPP

=== renamed file 'src/Box.cpp' => 'src/OrderedContainer.cpp'
--- src/Box.cpp	2013-07-20 20:59:12 +0000
+++ src/OrderedContainer.cpp	2013-07-24 16:18:00 +0000
@@ -1,59 +1,21 @@
-#include "Box.hpp"
-
-Box::Box(){
-    spacing = 0;
-    margin = 0;
-}
-
-void Box::_RecalculateSizes(int total, int (Drawable::*size_func)()){
-    if(children.size() == 0) return;
-
-    int shrinked_sum = 0;
-    int expand_count = 0;
-    
-    for(auto &i : children){
-        if(i.mode == BOX_SHRINK){
-            int h = ((i.item).*size_func)(); //use the size_func to get minimal width/height
-            i.size = h;
-            shrinked_sum += h;
-        }else if(i.mode == BOX_EXPAND){
-            expand_count++;
-        }
-        shrinked_sum += spacing;
-    }
-    if (shrinked_sum > 0) shrinked_sum -= spacing;
-    
-    if(expand_count > 0){
-        int rest = total - 2*margin - shrinked_sum;
-        int expand_size = rest / expand_count;
-        int last_expand_size = rest - (expand_size * expand_count) + expand_size;
-        
-        if(expand_size < 0) expand_size = 0;
-        if(last_expand_size < 0) last_expand_size = 0;
-        
-        for(auto &i : children){
-            if(i.mode == BOX_EXPAND){
-                if(expand_count != 1){ //this is not the last expanding item
-                    i.size = expand_size;
-                }else{
-                    i.size = last_expand_size;
-                }
-                expand_count--;
-            }
-        }
-    }
-}
-
-void Box::Add(Drawable& item, int position, BoxPackingMode mode){
-    BoxEntry be(item);
-    be.order = position;
-    be.mode = mode;
-    children.insert(be);
-}
-
-void Box::SetSpacing(int s){
-    spacing = s;
-}
-void Box::SetMargin(int m){
-     margin = m;
+#include "OrderedContainer.hpp"
+
+OrderedContainer::OrderedContainer() {};
+
+void OrderedContainer::Add(Drawable& item, int position, BoxPackingMode mode, bool is_visible){
+    ContainerEntry ce(item);
+    ce.order = position;
+    ce.mode = mode;
+    ce.is_visible = is_visible;
+    children.insert(ce);
+}
+
+void OrderedContainer::SetVisibility(Drawable& item, bool is_visible)
+{
+    for(auto &i : children)
+        if(&i.item == &item)
+        {
+            i.is_visible = is_visible;
+            break;
+        }
 }

=== renamed file 'src/Box.hpp' => 'src/OrderedContainer.hpp'
--- src/Box.hpp	2013-07-20 20:59:12 +0000
+++ src/OrderedContainer.hpp	2013-07-24 16:18:00 +0000
@@ -1,47 +1,43 @@
+#ifndef ORDEREDCONTAINER_HPP
+#define ORDEREDCONTAINER_HPP
+
 #include "drawable.hpp"
-#include <string>
 #include <set>
 
 enum BoxPackingMode{
     BOX_SHRINK = 0,
     BOX_EXPAND = 1
 };
-    
-class Box : public Drawable{
+
+class OrderedContainer : public Drawable{
 protected:
-    Box();
+    OrderedContainer();
     
 public:
-    void Add(Drawable& item, int position, BoxPackingMode mode = BOX_SHRINK);
-    
-    void SetSpacing(int spacing);
-    void SetMargin(int margin);
-    
+    void Add(Drawable& item, int position, BoxPackingMode mode = BOX_SHRINK, bool is_visible=true);
+    void SetVisibility(Drawable& item, bool is_visible);
+    
+    //Drawable Methods:
     virtual void draw(SDrawArea a) = 0;
-    
     virtual int GetMinimalHeight() = 0;
     virtual int GetMinimalWidth () = 0;
-    
     virtual void on_clicked(SCoordinates p) = 0;
+
 protected:
-
-    struct BoxEntry{
-        BoxEntry(Drawable& i) : item(i) {};
+    struct ContainerEntry{
+        ContainerEntry(Drawable& i) : item(i) {};
         int order; // Used to determine item's position within the container
         BoxPackingMode mode;
+        mutable bool is_visible; //when false, item shouldn't be drawn
         mutable int size; // size in px for this item, used temporarily for calculations
         Drawable& item;
     };
-    struct BoxEntryComparator{
-        bool operator()(const BoxEntry& x,const BoxEntry& y) const {return x.order < y.order;}
+    struct ContainerEntryComparator{
+        bool operator()(const ContainerEntry& x,const ContainerEntry& y) const {return x.order < y.order;}
     };
     
-    /* This set contains all widgets contained within this container. */
-    std::set< BoxEntry, BoxEntryComparator> children;
-    
-    int spacing;
-    int margin;
-    int current_width, current_height;
-    
-    void _RecalculateSizes(int total, int (Drawable::*size_func)());
+    /* This set contains all widgets contained within this container. */ //lol, this line CONTAINS such a cool sentence
+    std::set< ContainerEntry, ContainerEntryComparator> children;
 };
+
+#endif //#ifndef ORDEREDCONTAINER_HPP

=== modified file 'src/main.cpp'
--- src/main.cpp	2013-07-24 14:07:57 +0000
+++ src/main.cpp	2013-07-24 16:18:00 +0000
@@ -3,13 +3,19 @@
 #include "label.hpp"
 #include "Fixed.hpp"
 #include "VBox.hpp"
+<<<<<<< TREE
 #include "Image.hpp"
+=======
+#include "LayeredView.hpp"
+>>>>>>> MERGE-SOURCE
 #include <iostream>
 #include <cstdlib>
 
 Fixed additional_menu;
 
+LayeredView main_menu;
 VBox main_menu_vbox;
+VBox dialog_box;
 
 
 void click_reaction1(){
@@ -29,13 +35,23 @@
 }
 
 void switch_to_main_menu(){
-    Display::SetMainWidget(main_menu_vbox);
+    Display::SetMainWidget(main_menu);
 }
 void switch_to_additional_menu(){
     Display::SetMainWidget(additional_menu);
 }
 
+void open_dialog_box()
+{
+    std::cout << "Opening dialog box..." << std::endl;
+    main_menu.ActivateLayer(dialog_box);
+}
 
+void close_dialog_box()
+{
+    std::cout << "Closing dialog box..." << std::endl;
+    main_menu.HideLayer(dialog_box);
+}
 
 int main(){
     Display::init();
@@ -50,9 +66,8 @@
     Button button_to_2("To menu 2");
     button_to_2.set_click_reaction(switch_to_additional_menu);
     Button button_quit("QUIT");
-    button_quit.set_click_reaction(quit);
-    
-    
+    button_quit.set_click_reaction(open_dialog_box);
+
     Label label_additional_menu("Menu 2");
     
     Button button3("Button3");
@@ -61,7 +76,13 @@
     button_to_main.set_click_reaction(switch_to_main_menu);
     Image image_smile("../data/smile.png");
     
-    
+    Label label_dialog_box("Are you sure?");    
+
+    Button button_yes("Yes");
+    button_yes.set_click_reaction(quit);
+    Button button_no("No");
+    button_no.set_click_reaction(close_dialog_box);
+
     main_menu_vbox.SetSpacing(3); // gaps between buttons
     main_menu_vbox.SetMargin(5); // empty frame around the box
     main_menu_vbox.Add(label_main_menu, 0);
@@ -72,9 +93,23 @@
     additional_menu.add_item(label_additional_menu,SCoordinates(42,27));
     additional_menu.add_item(button3,              SCoordinates(42,52));
     additional_menu.add_item(button_to_main,       SCoordinates(42,77));
+<<<<<<< TREE
     additional_menu.add_item(image_smile,          SCoordinates(42,100));
     
     Display::SetMainWidget(main_menu_vbox);
+=======
+
+    dialog_box.SetSpacing(3);
+    dialog_box.SetMargin(20);    
+    dialog_box.Add(label_dialog_box,0);
+    dialog_box.Add(button_yes,1);
+    dialog_box.Add(button_no,2);
+
+    main_menu.Add(main_menu_vbox,0);
+    main_menu.Add(dialog_box,1,BOX_SHRINK,false);
+
+    Display::SetMainWidget(main_menu);
+>>>>>>> MERGE-SOURCE
     
     Display::main_loop();
     


Follow ups