From 9a7880ff7f206f86fb86a7a8569a9488a3f8a9a8 Mon Sep 17 00:00:00 2001
From: Marc Zinnschlag <marc@zpages.de>
Date: Thu, 29 Jul 2010 00:48:01 +0800
Subject: [PATCH] added GUI update for health, magicka and fatigue

---
 apps/openmw/mwgui/mw_layouts.hpp             | 71 ++++++++++++++++----
 apps/openmw/mwgui/window_manager.cpp         |  5 ++
 apps/openmw/mwgui/window_manager.hpp         |  9 ++-
 apps/openmw/mwmechanics/mechanicsmanager.cpp | 42 ++++++++----
 apps/openmw/mwmechanics/stat.hpp             |  2 +
 5 files changed, 99 insertions(+), 30 deletions(-)

diff --git a/apps/openmw/mwgui/mw_layouts.hpp b/apps/openmw/mwgui/mw_layouts.hpp
index fba2b411d..ce4a1ae98 100644
--- a/apps/openmw/mwgui/mw_layouts.hpp
+++ b/apps/openmw/mwgui/mw_layouts.hpp
@@ -89,6 +89,40 @@ namespace MWGui
     void setEffect(const char *img)
     { effect1->setImageTexture(img); }
 
+    void setValue (const std::string& id, const MWMechanics::DynamicStat<int>& value)
+    {
+        static const char *ids[] =
+        {
+            "HBar", "MBar", "FBar",
+            0
+        };
+
+        for (int i=0; ids[i]; ++i)
+            if (ids[i]==id)
+            {
+                switch (i)
+                {
+                    case 0:
+
+                      health->setProgressRange (value.getModified());
+                      health->setProgressPosition (value.getCurrent());
+                      break;
+
+                    case 1:
+
+                      magicka->setProgressRange (value.getModified());
+                      magicka->setProgressPosition (value.getCurrent());
+                      break;
+
+                    case 2:
+
+                      stamina->setProgressRange (value.getModified());
+                      stamina->setProgressPosition (value.getCurrent());
+                      break;
+                }
+            }
+    }
+
     MyGUI::ProgressPtr health, magicka, stamina;
 
     MyGUI::StaticImagePtr weapImage, spellImage;
@@ -136,7 +170,7 @@ namespace MWGui
   class StatsWindow : public OEngine::GUI::Layout
   {
   public:
-    void setBar(const char* name, const char* tname, int val, int max)
+    void setBar(const std::string& name, const std::string& tname, int val, int max)
     {
       MyGUI::ProgressPtr pt;
       getWidget(pt, name);
@@ -169,10 +203,6 @@ namespace MWGui
       setText("RaceText", "Wood Elf");
       setText("ClassText", "Pilgrim");
 
-      setBar("HBar", "HBarT", 60, 100);
-      setBar("MBar", "MBarT", 30, 100);
-      setBar("FBar", "FBarT", 80, 100);
-
       setText("AttribVal1", "30");
       setText("AttribVal2", "40");
       setText("AttribVal3", "30");
@@ -187,7 +217,7 @@ namespace MWGui
     {
       mMainWidget->setCaption(playerName);
     }
-    
+
     /// Set label text for the value with the given ID.
     void setLabel (const std::string& id, const std::string& label)
     {
@@ -197,7 +227,7 @@ namespace MWGui
             "Attrib7", "Attrib8",
             0
         };
-        
+
         for (int i=0; ids[i]; ++i)
             if (ids[i]==id)
             {
@@ -205,7 +235,7 @@ namespace MWGui
                 break;
             }
     }
-    
+
     /// Set value for the given ID.
     void setValue (const std::string& id, const MWMechanics::Stat<int>& value)
     {
@@ -214,7 +244,7 @@ namespace MWGui
             "AttribVal1", "AttribVal2", "AttribVal3", "AttribVal4", "AttribVal5",
             "AttribVal6", "AttribVal7", "AttribVal8",
             0
-        };    
+        };
 
         for (int i=0; ids[i]; ++i)
             if (ids[i]==id)
@@ -222,18 +252,33 @@ namespace MWGui
                 std::ostringstream valueString;
                 valueString << value.getModified();
                 setText (id, valueString.str());
-            
+
                 if (value.getModified()>value.getBase())
                     setTextColor (id, 0, 1, 0);
                 else if (value.getModified()<value.getBase())
                     setTextColor (id, 1, 0, 0);
                 else
                     setTextColor (id, 1, 1, 1);
-            
+
                 break;
-            }    
+            }
+    }
+
+    void setValue (const std::string& id, const MWMechanics::DynamicStat<int>& value)
+    {
+        static const char *ids[] =
+        {
+            "HBar", "MBar", "FBar",
+            0
+        };
+
+        for (int i=0; ids[i]; ++i)
+            if (ids[i]==id)
+            {
+                std::string id (ids[i]);
+                setBar (id, id + "T", value.getCurrent(), value.getModified());
+            }
     }
-    
   };
 }
 #endif
diff --git a/apps/openmw/mwgui/window_manager.cpp b/apps/openmw/mwgui/window_manager.cpp
index b6457f156..db4eb6f41 100644
--- a/apps/openmw/mwgui/window_manager.cpp
+++ b/apps/openmw/mwgui/window_manager.cpp
@@ -95,3 +95,8 @@ void WindowManager::setValue (const std::string& id, const MWMechanics::Stat<int
     stats->setValue (id, value);
 }
 
+void WindowManager::setValue (const std::string& id, const MWMechanics::DynamicStat<int>& value)
+{
+    stats->setValue (id, value);
+    hud->setValue (id, value);
+}
diff --git a/apps/openmw/mwgui/window_manager.hpp b/apps/openmw/mwgui/window_manager.hpp
index 007738fd1..aa1a8cbf9 100644
--- a/apps/openmw/mwgui/window_manager.hpp
+++ b/apps/openmw/mwgui/window_manager.hpp
@@ -113,7 +113,7 @@ namespace MWGui
     {
       if (newMode==GM_Inventory && allowed==GW_None)
         return;
-        
+
       mode = newMode;
       updateVisible();
     }
@@ -138,12 +138,15 @@ namespace MWGui
     }
 
     MyGUI::Gui* getGui() const { return gui; }
-  
+
     void setLabel (const std::string& id, const std::string& label);
     ///< Set label text for the value with the given ID.
-    
+
     void setValue (const std::string& id, const MWMechanics::Stat<int>& value);
     ///< Set value for the given ID.
+
+    void setValue (const std::string& id, const MWMechanics::DynamicStat<int>& value);
+    ///< Set value for the given ID.
   };
 }
 #endif
diff --git a/apps/openmw/mwmechanics/mechanicsmanager.cpp b/apps/openmw/mwmechanics/mechanicsmanager.cpp
index 06a04e7f0..0ba3c8f61 100644
--- a/apps/openmw/mwmechanics/mechanicsmanager.cpp
+++ b/apps/openmw/mwmechanics/mechanicsmanager.cpp
@@ -6,12 +6,12 @@
 #include "../mwgui/window_manager.hpp"
 
 namespace MWMechanics
-{           
+{
     MechanicsManager::MechanicsManager (const ESMS::ESMStore& store,
         MWGui::WindowManager& windowManager)
     : mStore (store), mWindowManager (windowManager)
     {
-    
+
     }
 
     void MechanicsManager::configureGUI()
@@ -28,32 +28,32 @@ namespace MWMechanics
             { "Attrib8", "sAttributeLuck" },
             { 0, 0 }
         };
-    
+
         for (int i=0; names[i][0]; ++i)
         {
-// This crashes because of encoding problems:          
+// This crashes because of encoding problems:
 //            std::string label = mStore.gameSettings.find (names[i][1])->str;
 
             std::string label = names[i][1]; // until the problem is fixed, use the GMST ID as label
-            
+
             mWindowManager.setLabel (names[i][0], label);
         }
     }
-    
+
     void MechanicsManager::addActor (const MWWorld::Ptr& ptr)
     {
         mActors.insert (ptr);
     }
-    
+
     void MechanicsManager::removeActor (const MWWorld::Ptr& ptr)
     {
         mActors.erase (ptr);
     }
-    
+
     void MechanicsManager::dropActors (const MWWorld::Ptr::CellStore *cellStore)
     {
         std::set<MWWorld::Ptr>::iterator iter = mActors.begin();
-        
+
         while (iter!=mActors.end())
             if (iter->getCell()==cellStore)
             {
@@ -62,24 +62,29 @@ namespace MWMechanics
             else
                 ++iter;
     }
-    
+
     void MechanicsManager::watchActor (const MWWorld::Ptr& ptr)
     {
         mWatched = ptr;
     }
-    
+
     void MechanicsManager::update()
     {
         if (!mWatched.isEmpty())
         {
             MWMechanics::CreatureStats& stats = mWatched.getCreatureStats();
-            
+
             static const char *attributeNames[8] =
             {
                 "AttribVal1", "AttribVal2", "AttribVal3", "AttribVal4", "AttribVal5",
                 "AttribVal6", "AttribVal7", "AttribVal8"
             };
-            
+
+            static const char *dynamicNames[3] =
+            {
+                "HBar", "MBar", "FBar"
+            };
+
             for (int i=0; i<8; ++i)
             {
                 if (stats.mAttributes[i]!=mWatchedCreature.mAttributes[i])
@@ -89,7 +94,16 @@ namespace MWMechanics
                     mWindowManager.setValue (attributeNames[i], stats.mAttributes[i]);
                 }
             }
+
+            for (int i=0; i<3; ++i)
+            {
+                if (stats.mDynamic[i]!=mWatchedCreature.mDynamic[i])
+                {
+                    mWatchedCreature.mDynamic[i] = stats.mDynamic[i];
+
+                    mWindowManager.setValue (dynamicNames[i], stats.mDynamic[i]);
+                }
+            }
         }
     }
 }
-
diff --git a/apps/openmw/mwmechanics/stat.hpp b/apps/openmw/mwmechanics/stat.hpp
index 7d7c64527..a42b53444 100644
--- a/apps/openmw/mwmechanics/stat.hpp
+++ b/apps/openmw/mwmechanics/stat.hpp
@@ -87,6 +87,8 @@ namespace MWMechanics
 
         public:
 
+            DynamicStat() : mCurrent (0) {}
+
             const T& getBase() const
             {
                 return mStatic.getBase();