wesnoth/src/gui/core/widget_definition.hpp
Charles Dang dfa09cd9d9 GUI2: refactored styled_widget canvas initialization process
Back in 515f450432 I attempted to optimize this, but I didn't do it right. Since I didn't
define a move ctor for canvas, that std::move call was useless and the copy ctor was being called
anyway. Though, that change wasn't a total waste. It still reduced the number of canvases created
from (I think) 3 to 2, since the code was no longer going default-construction
and copy-assignment from the state struct.

These new changes completely remove the canvas object from the state struct. Nothing particularity
special was being done there, only a call to canvas::set_cfg. Instead, the canvas config is saved
in the state objects and then styled_widget initializes the canvas itself. This does mean there's
config copying going on here, sadly, but it's unavoidable given the current design.

This also removes the definition_load_configuration function from styled_widget. Its contents were
moved to the ctor. This ensures only the exact number of canvas objects needed are created. No
copying from the state objects, no reallocations. This also means we can delete the copy ctor (see
below).

A move ctor was added to canvas (though it admittedly isn't needed now since no canvas moving
occurs), and the copy ctor deleted.
2017-12-07 19:18:42 +11:00

115 lines
2.7 KiB
C++

/*
Copyright (C) 2007 - 2017 by Mark de Wever <koraq@xs4all.nl>
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 as published by
the Free Software Foundation; either version 2 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.
See the COPYING file for more details.
*/
#pragma once
#include "config.hpp"
#include "font/font_options.hpp"
#include "font/text.hpp"
#include "gui/core/linked_group_definition.hpp"
#include <vector>
namespace gui2
{
/**
* Contains the state info for a resolution.
*
* At the moment all states are the same so there is no need to use
* inheritance. If that is needed at some point the containers should contain
* pointers
*/
struct state_definition
{
explicit state_definition(const config& cfg);
config canvas_cfg_;
};
/** Base class of a resolution, contains the common keys for a resolution. */
struct resolution_definition
{
explicit resolution_definition(const config& cfg);
unsigned window_width;
unsigned window_height;
unsigned min_width;
unsigned min_height;
unsigned default_width;
unsigned default_height;
unsigned max_width;
unsigned max_height;
std::vector<linked_group_definition> linked_groups;
unsigned text_extra_width;
unsigned text_extra_height;
unsigned text_font_size;
font::family_class text_font_family;
font::pango_text::FONT_STYLE text_font_style;
std::vector<state_definition> state;
};
typedef std::shared_ptr<resolution_definition>
resolution_definition_ptr;
typedef std::shared_ptr<const resolution_definition>
resolution_definition_const_ptr;
/**
* Casts a resolution_definition_const_ptr to another type.
*
* @tparam T The type to cast to, the non const version.
*
* @param ptr The pointer to cast.
*
* @returns A reference to type casted to.
*/
template <class T>
const T& cast(resolution_definition_const_ptr ptr)
{
std::shared_ptr<const T> conf = std::make_shared<const T>(ptr);
assert(conf);
return *conf;
}
struct styled_widget_definition
{
explicit styled_widget_definition(const config& cfg);
template <class T>
void load_resolutions(const config& cfg)
{
for (const auto & resolution : cfg.child_range("resolution"))
{
resolutions.emplace_back(std::make_shared<T>(resolution));
}
}
std::string id;
t_string description;
std::vector<resolution_definition_ptr> resolutions;
};
typedef std::shared_ptr<styled_widget_definition> styled_widget_definition_ptr;
} // namespace gui2