mail[Wesnoth-commits] r24702 - in /trunk/src/gui: dialogs/ widgets/


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by koraq on March 16, 2008 - 13:10:
Author: mordante
Date: Sun Mar 16 13:10:43 2008
New Revision: 24702

URL: http://svn.gna.org/viewcvs/wesnoth?rev=24702&view=rev
Log:
Adding the first proof-of-concept window builder code.
With this code it's possible to define a window from a config file.
The code a lot of extra love and polishing.

Added:
    trunk/src/gui/dialogs/
    trunk/src/gui/dialogs/addon_connect.cpp   (with props)
    trunk/src/gui/dialogs/addon_connect.hpp   (with props)
    trunk/src/gui/widgets/window_builder.cpp   (with props)
    trunk/src/gui/widgets/window_builder.hpp   (with props)
Modified:
    trunk/src/gui/widgets/settings.cpp
    trunk/src/gui/widgets/settings.hpp

Added: trunk/src/gui/dialogs/addon_connect.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/addon_connect.cpp?rev=24702&view=auto
==============================================================================
--- trunk/src/gui/dialogs/addon_connect.cpp (added)
+++ trunk/src/gui/dialogs/addon_connect.cpp Sun Mar 16 13:10:43 2008
@@ -1,0 +1,50 @@
+/* $Id$ */
+/*
+   copyright (c) 2008 by mark de wever <koraq@xxxxxxxxx>
+   part of the battle for wesnoth project http://www.wesnoth.org/
+
+   this program is free software; you can redistribute it and/or modify
+   it under the terms of the gnu general public license version 2
+   or at your option any later version.
+   this program is distributed in the hope that it will be useful,
+   but without any warranty.
+
+   see the copying file for more details.
+*/
+
+#include "gui/dialogs/addon_connect.hpp"
+
+#include "gui/widgets/widget.hpp"
+#include "gui/widgets/window.hpp"
+#include "gui/widgets/window_builder.hpp"
+#include "gui/widgets/button.hpp"
+
+#include "gui/widgets/settings.hpp"
+#include "log.hpp"
+#include "video.hpp"
+
+#define DBG_GUI LOG_STREAM(debug, widget)
+#define LOG_GUI LOG_STREAM(info, widget)
+#define WRN_GUI LOG_STREAM(warn, widget)
+#define ERR_GUI LOG_STREAM(err, widget)
+
+namespace gui2 {
+
+static void hello()
+{
+       std::cerr << "\n\n\nHello world.\n\n\n";
+}
+
+void addon_connect(CVideo& video, const std::string& server)
+{
+
+       gui2::init();
+       gui2::twindow window = build(video, get_id(ADDON_CONNECT));
+
+//     window.connect("connect", &hello);
+
+       window.show(true);
+}
+
+
+} // namespace gui2

Propchange: trunk/src/gui/dialogs/addon_connect.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/src/gui/dialogs/addon_connect.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: trunk/src/gui/dialogs/addon_connect.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/addon_connect.hpp?rev=24702&view=auto
==============================================================================
--- trunk/src/gui/dialogs/addon_connect.hpp (added)
+++ trunk/src/gui/dialogs/addon_connect.hpp Sun Mar 16 13:10:43 2008
@@ -1,0 +1,28 @@
+/* $Id$ */
+/*
+   copyright (c) 2008 by mark de wever <koraq@xxxxxxxxx>
+   part of the battle for wesnoth project http://www.wesnoth.org/
+
+   this program is free software; you can redistribute it and/or modify
+   it under the terms of the gnu general public license version 2
+   or at your option any later version.
+   this program is distributed in the hope that it will be useful,
+   but without any warranty.
+
+   see the copying file for more details.
+*/
+
+#ifndef __GUI_DIALOGS_ADDON_CONNECT_HPP_INCLUDED__
+#define __GUI_DIALOGS_ADDON_CONNECT_HPP_INCLUDED__
+
+#include <string>
+
+class CVideo;
+
+namespace gui2 {
+
+       void addon_connect(CVideo& video, const std::string& server);
+
+} // namespace gui2
+
+#endif

Propchange: trunk/src/gui/dialogs/addon_connect.hpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/src/gui/dialogs/addon_connect.hpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: trunk/src/gui/widgets/settings.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/settings.cpp?rev=24702&r1=24701&r2=24702&view=diff
==============================================================================
--- trunk/src/gui/widgets/settings.cpp (original)
+++ trunk/src/gui/widgets/settings.cpp Sun Mar 16 13:10:43 2008
@@ -22,6 +22,7 @@
 #include "gettext.hpp"
 #include "gui/widgets/button.hpp"
 #include "gui/widgets/widget.hpp"
+#include "gui/widgets/window_builder.hpp"
 #include "log.hpp"
 #include "serialization/parser.hpp"
 #include "serialization/preprocessor.hpp"
@@ -43,19 +44,38 @@
 
 namespace {
 
+       //! Map with all known windows, (the builder class builds a window).
+       std::map<std::string, twindow_builder> windows;
+
        //! Map with all known guis.
        std::map<std::string, tgui_definition> guis;
 
        //! Points to the current gui.
        std::map<std::string, tgui_definition>::const_iterator current_gui = 
guis.end();
 
+       //! Vector with all known windows, these are validated on existance 
on startup.
+       //! The enum twindow_type is the index of the array.
+       std::vector<std::string> window_type_list(DUMMY);
 
 } // namespace 
 
+static void fill_window_types() 
+{
+       window_type_list[ADDON_CONNECT] = "addon_connect";
+}
+
+const std::string& get_id(const twindow_type window_type)
+{
+       assert(window_type >= 0 && window_type < DUMMY);
+
+       return window_type_list[window_type];
+}
+
 void load_settings() 
 {
        LOG_GUI << "Init gui\n";
 
+       fill_window_types();
 
        const SDL_Rect rect = screen_area();
        screen_width = rect.w;
@@ -70,9 +90,9 @@
                ERR_GUI << "Could not read file '" << filename << "'\n";
        }
 
-       const config::child_list& cfgs = cfg.get_children("gui");
-       for(std::vector<config*>::const_iterator itor = cfgs.begin();
-                       itor != cfgs.end(); ++itor) {
+       const config::child_list& gui_cfgs = cfg.get_children("gui");
+       for(std::vector<config*>::const_iterator itor = gui_cfgs.begin();
+                       itor != gui_cfgs.end(); ++itor) {
 
                std::pair<std::string, tgui_definition> child;
                child.first = child.second.read(**itor);
@@ -108,6 +128,7 @@
 
        std::cerr << "Parsing gui " << id << '\n';
 
+       /***** Window definitions *****/
        const config::child_list& window_cfgs = 
cfg.get_children("window_definition");
        for(std::vector<config*>::const_iterator itor = window_cfgs.begin();
                        itor != window_cfgs.end(); ++itor) {
@@ -119,6 +140,7 @@
 
        VALIDATE(windows.find("default") != windows.end(), _ ("No default 
window defined."));
 
+       /***** Button definitions *****/
        const config::child_list& button_cfgs = 
cfg.get_children("button_definition");
        for(std::vector<config*>::const_iterator itor = button_cfgs.begin();
                        itor != button_cfgs.end(); ++itor) {
@@ -130,7 +152,25 @@
 
        VALIDATE(buttons.find("default") != buttons.end(), _ ("No default 
button defined."));
 
-
+       /***** Window types *****/
+       const config::child_list& window_instance_cfgs = 
cfg.get_children("window");
+       for(std::vector<config*>::const_iterator itor = 
window_instance_cfgs.begin();
+                       itor != window_instance_cfgs.end(); ++itor) {
+
+               std::pair<std::string, twindow_builder> child;
+               child.first = child.second.read(**itor);
+               window_types.insert(child);
+       }
+
+       if(id == "default") {
+               // The default gui needs to define all window types since 
we're the 
+               // fallback in case another gui doesn't define the window 
type.
+               for(std::vector<std::string>::const_iterator itor = 
window_type_list.begin();
+                               itor != window_type_list.end(); ++itor) {
+
+                       VALIDATE(window_types.find(*itor) != 
window_types.end(), _("Window not defined.")); 
+               }
+       }
 
        return id;
 }
@@ -381,4 +421,34 @@
        assert(false);
 }
 
+std::vector<twindow_builder::tresolution>::const_iterator 
get_window_builder(const std::string& type)
+{
+       std::map<std::string, twindow_builder>::const_iterator 
+               window = current_gui->second.window_types.find(type);
+
+       if(true) { // FIXME Test for default gui.
+               assert(window != current_gui->second.window_types.end());
+       } else {
+               // FIXME Get the defintion in the default gui and do an 
assertion test.
+       }
+
+       for(std::vector<twindow_builder::tresolution>::const_iterator 
+                       itor = window->second.resolutions.begin(),
+                       end = window->second.resolutions.end();
+                       itor != end;
+                       ++itor) {
+
+               if(screen_width <= itor->window_width &&
+                               screen_height <= itor->window_height) {
+
+                       return itor;
+               } else if (itor == end - 1) {
+                       return itor;
+               }
+       }
+
+       assert(false);
+}
+
+
 } // namespace gui2

Modified: trunk/src/gui/widgets/settings.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/settings.hpp?rev=24702&r1=24701&r2=24702&view=diff
==============================================================================
--- trunk/src/gui/widgets/settings.hpp (original)
+++ trunk/src/gui/widgets/settings.hpp Sun Mar 16 13:10:43 2008
@@ -19,6 +19,7 @@
 #define __GUI_WIDGETS_SETTING_HPP_INCLUDED__
 
 #include "gui/widgets/canvas.hpp"
+#include "gui/widgets/window_builder.hpp"
 #include "tstring.hpp"
 
 #include <map>
@@ -29,6 +30,14 @@
 class config;
 
 namespace gui2 {
+
+enum twindow_type {
+       ADDON_CONNECT,           //<! The addon connection dialog.
+
+       DUMMY                    //<! Dummy always the last one.
+};
+
+const std::string& get_id(const twindow_type window_type);
 
 struct tbutton_definition
 {
@@ -139,10 +148,14 @@
 
        std::map<std::string, tbutton_definition> buttons;
        std::map<std::string, twindow_definition> windows;
+
+       std::map<std::string, twindow_builder> window_types;
 };
 
        std::vector<tbutton_definition::tresolution>::const_iterator 
get_button(const std::string& definition);
        std::vector<twindow_definition::tresolution>::const_iterator 
get_window(const std::string& definition);
+
+       std::vector<twindow_builder::tresolution>::const_iterator 
get_window_builder(const std::string& type);
 
        //! Loads the setting for the theme.
        void load_settings();

Added: trunk/src/gui/widgets/window_builder.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window_builder.cpp?rev=24702&view=auto
==============================================================================
--- trunk/src/gui/widgets/window_builder.cpp (added)
+++ trunk/src/gui/widgets/window_builder.cpp Sun Mar 16 13:10:43 2008
@@ -1,0 +1,194 @@
+/* $Id$ */
+/*
+   Copyright (C) 2008 by Mark de Wever <koraq@xxxxxxxxx>
+   Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License version 2
+   or at your option any later version.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY.
+
+   See the COPYING file for more details.
+*/
+
+#include "gui/widgets/window_builder.hpp"
+
+#include "config.hpp"
+#include "gettext.hpp"
+#include "gui/widgets/button.hpp"
+#include "gui/widgets/settings.hpp"
+#include "gui/widgets/window.hpp"
+#include "log.hpp"
+#include "wml_exception.hpp"
+
+#include <cassert>
+
+#define DBG_GUI LOG_STREAM(debug, widget)
+#define LOG_GUI LOG_STREAM(info, widget)
+#define WRN_GUI LOG_STREAM(warn, widget)
+#define ERR_GUI LOG_STREAM(err, widget)
+
+
+namespace gui2 {
+
+twindow build(CVideo& video, const std::string& type)
+{
+       std::vector<twindow_builder::tresolution>::const_iterator 
+               definition = get_window_builder(type);
+
+
+       twindow window(video, 100, 100, definition->width, 
definition->height);
+
+       const unsigned rows = definition->grid.rows;
+       const unsigned cols = definition->grid.cols;
+
+       window.set_rows(rows);
+       window.set_cols(cols);
+
+       for(unsigned x = 0; x < rows; ++x) {
+               for(unsigned y = 0; y < cols; ++y) {
+
+                       tbutton *button = new tbutton();
+
+                       const std::string id = definition->grid.widgets[x * 
cols + y].id;
+                       const std::string def = definition->grid.widgets[x * 
cols + y].definition;
+                       button->set_definition(id);
+                       button->set_definition(def);
+                       window.add_child(button, x, y);
+
+                       DBG_GUI << "Placed button '" << id << "' with 
defintion '" 
+                               << def << "' at " << x << ", " << y << '\n';
+
+               }
+       }
+
+       return window;
+}
+
+namespace {
+
+} // namespace
+
+
+const std::string& twindow_builder::read(const config& cfg)
+{
+/*WIKI
+ * [window]
+ *
+ * A window defines how a window looks in the game.
+ *
+ *
+ *     id = (string = "")            Unique id for this window.
+ *     description = (t_string = "") Unique translatable name for this 
window.
+ *
+ *
+ *
+ * [/window]
+ */
+
+
+       id_ = cfg["id"];
+       description_ = cfg["description"];
+
+       VALIDATE(!id_.empty(), missing_mandatory_wml_key("window", "id"));
+       VALIDATE(!description_.empty(), missing_mandatory_wml_key("window", 
"description"));
+
+       DBG_GUI << "Reading window " << id_ << '\n';
+
+       const config::child_list& cfgs = cfg.get_children("resolution");
+       VALIDATE(!cfgs.empty(), _("No resolution defined."));
+       for(std::vector<config*>::const_iterator itor = cfgs.begin();
+                       itor != cfgs.end(); ++itor) {
+
+               resolutions.push_back(tresolution(**itor));
+       }
+
+
+       return id_;
+}
+
+twindow_builder::tresolution::tresolution(const config& cfg) :
+       window_width(lexical_cast_default<unsigned>(cfg["window_width"])),
+       window_height(lexical_cast_default<unsigned>(cfg["window_height"])),
+       width(lexical_cast_default<unsigned>(cfg["width"])),
+       height(lexical_cast_default<unsigned>(cfg["height"])),
+       definition(cfg["window_definition"]),
+       grid(cfg.child("grid"))
+{
+/*WIKI
+ * [resolution]
+ *     window_width = (unsigned = 0) Width of the application window.
+ *     window_height = (unsigned = 0) 
+ *                                   Height of the application window.
+ *     width = (unsigned)            Width of the window to show.
+ *     height = (unsigned)           Height of the window to show.
+ *
+ *     window_definition = (string = "default")
+ *                                   Definition of the window which we want 
to show.
+ *
+ *     [grid]                        The grid with the widgets to show.
+ *
+ * [/resolution]
+ */
+
+       DBG_GUI << "Parsing resolution " 
+               << window_width << ", " << window_height << '\n';
+
+       if(definition.empty()) {
+               definition = "default";
+       }
+       
+}
+
+twindow_builder::tresolution::tgrid::tgrid(const config* cfg) : 
+       rows(0),
+       cols(0),
+       widgets()
+{
+       VALIDATE(cfg, _("No grid defined."));
+
+       const config::child_list& row_cfgs = cfg->get_children("row");
+       for(std::vector<config*>::const_iterator row_itor = row_cfgs.begin();
+                       row_itor != row_cfgs.end(); ++row_itor) {
+
+               unsigned col = 0;
+
+               const config::child_list& col_cfgs = 
(**row_itor).get_children("column");
+               for(std::vector<config*>::const_iterator col_itor = 
col_cfgs.begin();
+                               col_itor != col_cfgs.end(); ++col_itor) {
+
+                       assert((**col_itor).child("button"));
+                       
widgets.push_back(twidget(*((**col_itor).child("button"))));
+
+                       ++col;
+               }
+
+               ++rows;
+               if(row_itor == row_cfgs.begin()) {
+                       cols = col;
+               } else {
+                       VALIDATE(col, _("A row must have a column."));
+                       VALIDATE(col == cols, _("Number of columns differ."));
+               }
+
+       }
+
+       DBG_GUI << "Grid has " << rows << " rows and " << cols << " 
columns.\n";
+}
+
+twindow_builder::tresolution::tgrid::twidget::twidget(const config& cfg) :
+       id(cfg["id"]),
+       definition(cfg["button_definition"])
+{
+
+       if(definition.empty()) {
+               definition = "default";
+       }
+
+       DBG_GUI << "Found button with id '" << id << "' and definition '" << 
definition << "'.\n";
+       
+}
+
+} // namespace gui2
+

Propchange: trunk/src/gui/widgets/window_builder.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/src/gui/widgets/window_builder.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: trunk/src/gui/widgets/window_builder.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window_builder.hpp?rev=24702&view=auto
==============================================================================
--- trunk/src/gui/widgets/window_builder.hpp (added)
+++ trunk/src/gui/widgets/window_builder.hpp Sun Mar 16 13:10:43 2008
@@ -1,0 +1,99 @@
+/* $Id$ */
+/*
+   Copyright (C) 2008 by Mark de Wever <koraq@xxxxxxxxx>
+   Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License version 2
+   or at your option any later version.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY.
+
+   See the COPYING file for more details.
+*/
+
+#ifndef __GUI_WIDGETS_WINDOW_BUILDER_HPP_INCLUDED__
+#define __GUI_WIDGETS_WINDOW_BUILDER_HPP_INCLUDED__
+
+
+#include <string>
+#include <vector>
+
+class config;
+class CVideo;
+
+namespace gui2 {
+
+class twindow;
+
+twindow build(CVideo& video, const std::string& type);
+
+class twindow_builder
+{
+public:
+       const std::string& read(const config& cfg);
+
+       struct tresolution
+       {
+       private:
+               tresolution();
+
+       public:
+               tresolution(const config& cfg);
+
+               unsigned window_width;
+               unsigned window_height;
+
+               unsigned width;
+               unsigned height;
+
+               // note x, y hardcoded.
+               
+               std::string definition;
+
+               struct tgrid
+               {
+               private:
+                       tgrid();
+
+               public:
+                       tgrid(const config* cfg);
+
+                       unsigned rows;
+                       unsigned cols;
+
+                       struct twidget
+                       {
+                       // NOTE a widget is always a button atm.
+                       private:
+                               twidget();
+
+                       public:
+                               twidget(const config& cfg);
+
+                               std::string id;
+                               std::string definition;
+                       };
+
+                       std::vector<twidget> widgets;
+               };
+
+       
+               tgrid grid;
+       };
+
+       std::vector<tresolution> resolutions;
+       
+private:
+       std::string id_;
+       std::string description_;
+
+};
+
+} // namespace gui2
+
+
+#endif
+
+
+

Propchange: trunk/src/gui/widgets/window_builder.hpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/src/gui/widgets/window_builder.hpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision




Related Messages


Powered by MHonArc, Updated Sun Mar 16 14:21:06 2008