Show/hide building picker
authorNick Gasson <nick@nickg.me.uk>
Sat, 5 Dec 2009 21:28:26 +0000 (21:28 +0000)
committerNick Gasson <nick@nickg.me.uk>
Sat, 5 Dec 2009 21:28:26 +0000 (21:28 +0000)
include/IBuildingPicker.hpp
include/gui/ToggleButton.hpp
include/gui/Widget.hpp
src/BuildingPicker.cpp
src/Editor.cpp
src/gui/ToggleButton.cpp
src/gui/Widget.cpp

index 76ed10d827cf624a7ac64e9436d611735020eb3f..c5453c5ee3961b94fe1c8b7c21f9e95a3d72a3dc 100644 (file)
@@ -26,7 +26,7 @@
 struct IBuildingPicker {
    virtual ~IBuildingPicker() {}
 
-   virtual IBuildingPtr get() const = 0;
+   virtual tuple<IBuildingPtr, float> get() const = 0;
 };
 
 typedef shared_ptr<IBuildingPicker> IBuildingPickerPtr;
index 5f0edb084743627d9018e9fcafe3ac2165b402d8..4a3b450a717c09f32e8ce55aeb6812fa821f7a99 100644 (file)
@@ -32,8 +32,8 @@ namespace gui {
       void render(RenderContext& rc) const;
       bool handleClick(int x, int y);
 
-      void on() { enabled = true; }
-      void off() { enabled = false; }
+      void on();
+      void off();
    private:
       ITexturePtr texture;
       bool enabled;
index 48aa49716c668a8e9c2bd95abf7087d4e7e0eb0e..938c454df9489e8924dd00e2485a8134c837c879 100644 (file)
@@ -44,10 +44,11 @@ namespace gui {
       void y(int y) { y_ = y; }
       void width(int w) { width_ = w; }
       void height(int h) { height_ = h; }
-      void visible(bool v) { visible_ = v; }
+      void visible(bool v);
 
       enum Signal {
-         SIG_CLICK, SIG_RENDER
+         SIG_CLICK, SIG_RENDER, SIG_SHOW, SIG_HIDE,
+         SIG_ENTER, SIG_LEAVE
       };
 
       typedef function<void (Widget&)> SignalHandler;
index 43db812ac5596b52590a339c5f41fb08f04fc570..14c381340e0c63a46fae2605aef4cbcb1922c418 100644 (file)
 
 class BuildingPicker : public IBuildingPicker {
 public:
-   BuildingPicker(gui::ILayoutPtr l)
-      : layout(l)
-   {
-      using namespace placeholders;
-      
-      enumResources("buildings", buildingList);
-      
-      if (buildingList.empty())
-         warn() << "No buildings found";
-      else {
-         buildingIt = buildingList.begin();
-         changeActive(loadBuilding((*buildingIt)->name()));
-      }
-      
-      layout->get("/building_wnd/preview").connect(gui::Widget::SIG_RENDER,
-         bind(&::BuildingPicker::renderBuildingPreview, this, _1));
-      layout->get("/building_wnd/next").connect(gui::Widget::SIG_CLICK,
-         bind(&::BuildingPicker::next, this));
-      layout->get("/building_wnd/prev").connect(gui::Widget::SIG_CLICK,
-         bind(&::BuildingPicker::prev, this));
-   }
-      
-   void next()
-   {
-      if (++buildingIt == buildingList.end())
-         buildingIt = buildingList.begin();
-      
-      changeActive(loadBuilding((*buildingIt)->name()));      
-   }
-   
-   void prev()
-   {
-      if (buildingIt == buildingList.begin())
-         buildingIt = buildingList.end();
-      buildingIt--;
-      
-      changeActive(loadBuilding((*buildingIt)->name()));
-   }
-   
-   IBuildingPtr get() const
-   {
-      return activeBuilding;
-   }
-   
-   void renderBuildingPreview(gui::Widget& canvas)
-   {
-      static ILightPtr sun = makeSunLight();
-      
-      sun->apply();
-
-      glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
-      glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
-      glTranslatef(1.5f, -2.6f, -1.5f);
-      glColor3f(1.0f, 1.0f, 1.0f);
-      glRotatef(35.0f, 0.0f, 1.0f, 0.0f);
-      
-      activeBuilding->model()->render();
-   }
+   BuildingPicker(gui::ILayoutPtr l);
+     
+private: 
+   void next();   
+   void prev();
+   void rotate();
+   tuple<IBuildingPtr, float> get() const;
+   void renderBuildingPreview(gui::Widget& canvas);
+   void show();
+   void hide();
 
-private:
    void changeActive(IBuildingPtr b);
    
    ResourceList buildingList;
    ResourceList::const_iterator buildingIt;
    IBuildingPtr activeBuilding;
    gui::ILayoutPtr layout;
+   float rotation;
 };
 
+BuildingPicker::BuildingPicker(gui::ILayoutPtr l)
+   : layout(l),
+     rotation(0.0f)
+{
+   using namespace placeholders;
+   
+   enumResources("buildings", buildingList);
+   
+   if (buildingList.empty())
+      warn() << "No buildings found";
+   else {
+      buildingIt = buildingList.begin();
+      changeActive(loadBuilding((*buildingIt)->name()));
+   }
+   
+   layout->get("/building_wnd/preview").connect(gui::Widget::SIG_RENDER,
+      bind(&BuildingPicker::renderBuildingPreview, this, _1));
+   layout->get("/building_wnd/next").connect(gui::Widget::SIG_CLICK,
+      bind(&BuildingPicker::next, this));
+   layout->get("/building_wnd/prev").connect(gui::Widget::SIG_CLICK,
+      bind(&BuildingPicker::prev, this));
+   layout->get("/building_wnd/rotate").connect(gui::Widget::SIG_CLICK,
+      bind(&BuildingPicker::rotate, this));
+
+   layout->get("/tool_wnd/tools/building").connect(gui::Widget::SIG_ENTER,
+      bind(&BuildingPicker::show, this));
+   layout->get("/tool_wnd/tools/building").connect(gui::Widget::SIG_LEAVE,
+      bind(&BuildingPicker::hide, this));
+
+   hide();
+}
+    
+void BuildingPicker::next()
+{
+   if (++buildingIt == buildingList.end())
+      buildingIt = buildingList.begin();
+   
+   changeActive(loadBuilding((*buildingIt)->name()));      
+}
+   
+void BuildingPicker::prev()
+{
+   if (buildingIt == buildingList.begin())
+      buildingIt = buildingList.end();
+   buildingIt--;
+   
+   changeActive(loadBuilding((*buildingIt)->name()));
+}
+
+void BuildingPicker::show()
+{
+   layout->get("/building_wnd").visible(true);
+}
+
+void BuildingPicker::hide()
+{
+   layout->get("/building_wnd").visible(false);
+}
+   
+tuple<IBuildingPtr, float> BuildingPicker::get() const
+{
+   return make_tuple(activeBuilding, rotation);
+}
+
+void BuildingPicker::renderBuildingPreview(gui::Widget& canvas)
+{
+   static ILightPtr sun = makeSunLight();
+      
+   glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
+   glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
+   glTranslatef(1.5f, -2.6f, -1.5f);
+   glColor3f(1.0f, 1.0f, 1.0f);
+   glRotatef(rotation, 0.0f, 1.0f, 0.0f);
+   
+   sun->apply();
+   
+   activeBuilding->model()->render();
+}
+
 void BuildingPicker::changeActive(IBuildingPtr b)
 {
    activeBuilding = b;
@@ -98,6 +129,13 @@ void BuildingPicker::changeActive(IBuildingPtr b)
    layout->cast<gui::Label&>("/building_wnd/bld_name").text(b->name());
 }
 
+void BuildingPicker::rotate()
+{
+   rotation += 90.0f;
+   if (rotation >= 350.0f)
+      rotation = 0.0f;
+}
+
 IBuildingPickerPtr makeBuildingPicker(gui::ILayoutPtr layout)
 {
    return IBuildingPickerPtr(new BuildingPicker(layout));
index 41324f1be296a3fc43464da21b591c1671aa04af..cb16cc2fe7b08336c8dc29f892a38140e5cd683b 100644 (file)
@@ -526,8 +526,12 @@ void Editor::onMouseRelease(IPickBufferPtr aPickBuffer, int x, int y,
          map->extendStation(dragBegin, dragEnd);
          break;
       case BUILDING_TOOL:
-         //map->placeBuilding(dragBegin, theBuildingPicker->active(),
-         //   theModelViewer->angle());
+         {
+            float angle;
+            IBuildingPtr building;
+            tie(building, angle) = buildingPicker->get();
+            map->placeBuilding(dragBegin, building, angle);
+         }
          break;
       }
          
index a4d7fcc1d290248c03e7fdf8984a3195841e7bba..cdc6f00d1e0e4a0d3db263d6e8744fe79f57796b 100644 (file)
@@ -40,3 +40,19 @@ bool ToggleButton::handleClick(int x, int y)
    on();
    return Widget::handleClick(x, y);
 }
+
+void ToggleButton::on()
+{
+   if (enabled == false) {
+      enabled = true;
+      raise(SIG_ENTER);
+   }
+}
+
+void ToggleButton::off()
+{
+   if (enabled == true) {
+      enabled = false;
+      raise(SIG_LEAVE);
+   }
+}
index 388fbc2fbfd0a4c53841b811bf0db0d1f0d56b92..4db2a657d8a52aefe4cd9181932a5b3a2348abd8 100644 (file)
@@ -64,3 +64,17 @@ void Widget::dumpLocation() const
            << " y=" << y() << " width=" << width()
            << " height=" << height();
 }
+
+void Widget::visible(bool v)
+{
+   bool event = visible_ != v;
+   
+   visible_ = v;
+
+   if (event) {
+      if (visible_)
+         raise(SIG_SHOW);
+      else
+         raise(SIG_HIDE);
+   }
+}