From b79af55134ae33b4ee6d6901026e04165738978b Mon Sep 17 00:00:00 2001
From: Nick Gasson <nick@nickg.me.uk>
Date: Sun, 25 Oct 2009 21:52:01 +0000
Subject: [PATCH] Start using the new GUI in the game

---
 include/gui/Colour.hpp   | 32 --------------------------------
 include/gui/IControl.hpp |  2 +-
 include/gui2/Colour.hpp  |  4 ++--
 include/gui2/Label.hpp   |  2 ++
 src/Game.cpp             | 25 ++++++++++++++++++-------
 src/ft/Font.cpp          |  2 +-
 src/gui2/Label.cpp       | 17 +++++++++++++++++
 src/gui2/Layout.cpp      |  8 ++++----
 src/gui2/Theme.cpp       |  6 +++---
 9 files changed, 48 insertions(+), 50 deletions(-)
 delete mode 100644 include/gui/Colour.hpp

diff --git a/include/gui/Colour.hpp b/include/gui/Colour.hpp
deleted file mode 100644
index ec87f2a..0000000
--- a/include/gui/Colour.hpp
+++ /dev/null
@@ -1,32 +0,0 @@
-//
-//  Copyright (C) 2006-2009  Nick Gasson
-//
-//  This program is free software: you can redistribute it and/or modify
-//  it under the terms of the GNU General Public License as published by
-//  the Free Software Foundation, either version 3 of the License, or
-//  (at your option) any later version.
-//
-//  This program is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//  GNU General Public License for more details.
-//
-//  You should have received a copy of the GNU General Public License
-//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-//
-
-#ifndef INC_GUI_COLOUR_HPP
-#define INC_GUI_COLOUR_HPP
-
-#include "Platform.hpp"
-
-namespace gui {
-   typedef tuple<float, float, float> Colour;
-
-   inline Colour makeColour(float r, float g, float b)
-   {
-      return make_tuple(r, g, b);
-   }
-}
-
-#endif
diff --git a/include/gui/IControl.hpp b/include/gui/IControl.hpp
index 471d0ed..446884f 100644
--- a/include/gui/IControl.hpp
+++ b/include/gui/IControl.hpp
@@ -20,7 +20,7 @@
 
 #include "Platform.hpp"
 #include "IFont.hpp"
-#include "Colour.hpp"
+#include "gui2/Colour.hpp"
 
 #include <string>
 
diff --git a/include/gui2/Colour.hpp b/include/gui2/Colour.hpp
index 6d427cf..f0e35f8 100644
--- a/include/gui2/Colour.hpp
+++ b/include/gui2/Colour.hpp
@@ -15,8 +15,8 @@
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 //
 
-#ifndef INC_GUI2_COLOUR_HPP
-#define INC_GUI2_COLOUR_HPP
+#ifndef INC_GUI_COLOUR_HPP
+#define INC_GUI_COLOUR_HPP
 
 #include "Platform.hpp"
 
diff --git a/include/gui2/Label.hpp b/include/gui2/Label.hpp
index c84567c..43cec8a 100644
--- a/include/gui2/Label.hpp
+++ b/include/gui2/Label.hpp
@@ -32,6 +32,8 @@ namespace gui {
       const string& text() const { return text_; }
       void text(const string& t) { text_ = t; }
 
+      void format(const char* fmt, ...);
+
       void render(RenderContext& rc) const;
       void adjust_for_theme(Theme& theme);
    private:
diff --git a/src/Game.cpp b/src/Game.cpp
index bf982a6..086a218 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -28,6 +28,9 @@
 #include "IterateTrack.hpp"
 #include "IConfig.hpp"
 
+#include "gui2/ILayout.hpp"
+#include "gui2/Label.hpp"
+
 #include <GL/gl.h>
 
 using namespace gui;
@@ -78,9 +81,10 @@ private:
    ITextControlPtr myTempLabel, myPressureLabel;
    IMeterControlPtr myThrottleMeter;
    IMeterControlPtr myCoalMeter, myWaterMeter;
-
+   ILayoutPtr layout;
+   
    string myStatusMsg;
-   IFontPtr myStatusFont;
+   gui::IFontPtr myStatusFont;
 };
 
 Game::Game(IMapPtr aMap)
@@ -97,8 +101,8 @@ Game::Game(IMapPtr aMap)
    // Build the GUI
    myStatsPanel = makeFlowBox(FLOW_BOX_VERT);
 
-   IFontPtr stdFont = load_font("data/fonts/Vera.ttf", 14);
-   myStatusFont = load_font("data/fonts/Vera.ttf", 18);
+   gui::IFontPtr stdFont = gui::load_font("data/fonts/Vera.ttf", 14);
+   myStatusFont = gui::load_font("data/fonts/Vera.ttf", 18);
    
    mySpeedLabel = makeLabel(stdFont);
    myStatsPanel->addChild(mySpeedLabel);
@@ -107,11 +111,11 @@ Game::Game(IMapPtr aMap)
    myStatsPanel->addChild(myThrottleMeter);
 
    myCoalMeter = makeFuelMeter(stdFont, "Coal",
-                               make_tuple(0.1f, 0.1f, 0.1f));
+                               make_colour(0.1f, 0.1f, 0.1f));
    //myStatsPanel->addChild(myCoalMeter);
    
    myWaterMeter = makeFuelMeter(stdFont, "Water",
-                                make_tuple(0.1f, 0.1f, 0.8f));
+                                make_colour(0.1f, 0.1f, 0.8f));
    //myStatsPanel->addChild(myWaterMeter);
 
    myTempLabel = makeLabel(stdFont, "Temp");
@@ -125,6 +129,8 @@ Game::Game(IMapPtr aMap)
    myStatsPanel->addChild(myBrakeLabel);
 
    myStatsPanel->setOrigin(5, 10);
+
+   layout = gui::make_layout("layouts/game.xml");
 }
 
 Game::~Game()
@@ -162,7 +168,9 @@ void Game::display(IGraphicsPtr aContext) const
 
 void Game::overlay() const
 {
-   myStatsPanel->render();
+   //myStatsPanel->render();
+
+   layout->render();
 
    const int screenH = getGameWindow()->height();
    const int screenW = getGameWindow()->width();
@@ -181,6 +189,9 @@ void Game::update(IPickBufferPtr aPickBuffer, int aDelta)
    myThrottleMeter->setValue(myTrain->controller()->throttle());
    myBrakeLabel->setVisible(myTrain->controller()->brakeOn());
 
+   layout->get_cast<gui::Label>("/status_wnd/speed_label").format(
+      "Speed: %.1lfmph", myTrain->speed() * msToMPH);
+   
    const double pressure = myTrain->controller()->pressure();
    myPressureLabel->setText("Pressure: %.lfpsi", pressure);
 
diff --git a/src/ft/Font.cpp b/src/ft/Font.cpp
index 0f91639..8fe5d4b 100644
--- a/src/ft/Font.cpp
+++ b/src/ft/Font.cpp
@@ -266,7 +266,7 @@ void Font::print(int x, int y, Colour c, const string& s) const
    glPopMatrix();
    glPopAttrib();
 }
-
+   
 int Font::text_width(const string& s) const
 {
    float w = 0.0f;
diff --git a/src/gui2/Label.cpp b/src/gui2/Label.cpp
index 2926476..c1a98d2 100644
--- a/src/gui2/Label.cpp
+++ b/src/gui2/Label.cpp
@@ -18,6 +18,9 @@
 #include "gui2/Label.hpp"
 #include "ILogger.hpp"
 
+#include <cstdarg>
+#include <cstdio>
+
 using namespace gui;
 
 Label::Label(const AttributeSet& attrs)
@@ -37,3 +40,17 @@ void Label::adjust_for_theme(Theme& theme)
    width(theme.normal_font()->text_width(text_));
    height(theme.normal_font()->height());
 }
+
+void Label::format(const char* fmt, ...)
+{
+   va_list ap;
+   va_start(ap, fmt);
+
+   char* buf;
+   vasprintf(&buf, fmt, ap);
+   text(buf);
+   
+   free(buf);
+   va_end(ap);
+}
+
diff --git a/src/gui2/Layout.cpp b/src/gui2/Layout.cpp
index 92bf833..c078a9e 100644
--- a/src/gui2/Layout.cpp
+++ b/src/gui2/Layout.cpp
@@ -78,10 +78,6 @@ Layout::Layout(const string& file_name)
    IXMLParserPtr parser = makeXMLParser("schemas/layout.xsd");
    parser->parse(file_name, *this);
 
-   assert(root);
-   Theme dummy;
-   root->adjust_for_theme(dummy);
-
    log() << "Loaded UI layout from " << file_name;
 }
 
@@ -135,6 +131,10 @@ void Layout::endElement(const string& local_name)
 
 void Layout::render() const
 {
+   assert(root);
+   Theme dummy;
+   root->adjust_for_theme(dummy);
+   
    RenderContext rc;
    root->render(rc);
 }
diff --git a/src/gui2/Theme.cpp b/src/gui2/Theme.cpp
index c069099..87fc039 100644
--- a/src/gui2/Theme.cpp
+++ b/src/gui2/Theme.cpp
@@ -24,15 +24,15 @@ using namespace gui;
 Theme::Theme()
 {
    normal_font_ = ft::load_font("data/fonts/Vera.ttf",
-      15, ft::FONT_MONO);
+      18, ft::FONT_NORMAL);
 }
       
 Colour Theme::background() const
 {
-   return make_colour(0.3f, 0.0f, 0.0f, 0.5f);
+   return make_colour(0.0f, 0.0f, 0.3f, 0.5f);
 }
 
 Colour Theme::border() const
 {
-   return make_colour(1.0f, 0.0f, 0.0f, 1.0f);
+   return make_colour(0.0f, 0.0f, 1.0f, 1.0f);
 }
-- 
2.39.5