Moved more generic code to helper.

This commit is contained in:
Mark de Wever 2008-04-12 10:58:17 +00:00
parent cb504f873d
commit 1a6fc6aa18
6 changed files with 89 additions and 85 deletions

View file

@ -52,25 +52,6 @@
#define WRN_G_P LOG_STREAM_INDENT(warn, gui_parse)
#define ERR_G_P LOG_STREAM_INDENT(err, gui_parse)
static Uint32 decode_colour(const std::string& colour);
static Uint32 decode_colour(const std::string& colour)
{
std::vector<std::string> fields = utils::split(colour);
// make sure we have four fields
while(fields.size() < 4) fields.push_back("0");
Uint32 result = 0;
for(int i = 0; i < 4; ++i) {
// shift the previous value before adding, since it's a nop on the
// first run there's no need for an if.
result = result << 8;
result |= lexical_cast_default<int>(fields[i]);
}
return result;
}
namespace gui2{
@ -568,7 +549,7 @@ timage::timage(const config& cfg) :
*/
image_.assign(image::get_image(image::locator(cfg["name"])));
src_clip_ = create_rect(0, 0, image_->w, image_->h);
src_clip_ = ::create_rect(0, 0, image_->w, image_->h);
const std::string& debug = (cfg["debug"]);
if(!debug.empty()) {

View file

@ -13,6 +13,8 @@
*/
#include "gui/widgets/helper.hpp"
#include "gui/widgets/settings.hpp"
#include "serialization/string_utils.hpp"
#include "log.hpp"
@ -40,6 +42,34 @@
namespace gui2 {
namespace {
static bool initialized_ = false;
}
bool init() {
if(initialized_) {
return true;
}
load_settings();
initialized_ = true;
return initialized_;
}
SDL_Rect create_rect(const tpoint& origin, const tpoint& size)
{
return ::create_rect(origin.x, origin.y, size.x, size.y);
}
std::ostream &operator<<(std::ostream &stream, const tpoint& point)
{
stream << point.x << ',' << point.y;
return stream;
}
int decode_font_style(const std::string& style)
{
if(style == "bold") {
@ -57,5 +87,23 @@ int decode_font_style(const std::string& style)
return TTF_STYLE_NORMAL;
}
Uint32 decode_colour(const std::string& colour)
{
std::vector<std::string> fields = utils::split(colour);
// make sure we have four fields
while(fields.size() < 4) fields.push_back("0");
Uint32 result = 0;
for(int i = 0; i < 4; ++i) {
// shift the previous value before adding, since it's a nop on the
// first run there's no need for an if.
result = result << 8;
result |= lexical_cast_default<int>(fields[i]);
}
return result;
}
} // namespace gui2

View file

@ -15,10 +15,48 @@
#ifndef __GUI_WIDGETS_HELPER_HPP_INCLUDED__
#define __GUI_WIDGETS_HELPER_HPP_INCLUDED__
#include "SDL.h"
#include <string>
namespace gui2 {
// init needs a cfg object later to init the subsystem
bool init();
struct tpoint
{
tpoint(const int x_, const int y_) :
x(x_),
y(y_)
{}
int x;
int y;
bool operator==(const tpoint& point) const { return x == point.x && y == point.y; }
bool operator<(const tpoint& point) const
{ return x < point.x || (x == point.x && y < point.y); }
bool operator<=(const tpoint& point) const
{ return x < point.x || (x == point.x && y <= point.y); }
};
std::ostream &operator<<(std::ostream &stream, const tpoint& point);
SDL_Rect create_rect(const tpoint& origin, const tpoint& size);
struct terror
{
terror(const std::string& msg) : message(msg) {}
const std::string message;
};
Uint32 decode_colour(const std::string& colour);
int decode_font_style(const std::string& style);
} // namespace gui2

View file

@ -45,35 +45,6 @@
namespace gui2 {
namespace {
static bool initialized_ = false;
}
bool init() {
if(initialized_) {
return true;
}
load_settings();
initialized_ = true;
return initialized_;
}
SDL_Rect create_rect(const tpoint& origin, const tpoint& size)
{
return ::create_rect(origin.x, origin.y, size.x, size.y);
}
std::ostream &operator<<(std::ostream &stream, const tpoint& point)
{
stream << point.x << ',' << point.y;
return stream;
}
twindow* twidget::get_window()
{
// Go up into the parent tree until we find the top level

View file

@ -16,6 +16,7 @@
#define __GUI_WIDGETS_WIDGET_HPP_INCLUDED__
#include "gui/widgets/event_handler.hpp"
#include "gui/widgets/helper.hpp"
#include "sdl_utils.hpp"
#include "SDL.h"
@ -25,40 +26,6 @@
namespace gui2 {
// init needs a cfg object later to init the subsystem
bool init();
struct tpoint
{
tpoint(const int x_, const int y_) :
x(x_),
y(y_)
{}
int x;
int y;
bool operator==(const tpoint& point) const { return x == point.x && y == point.y; }
bool operator<(const tpoint& point) const
{ return x < point.x || (x == point.x && y < point.y); }
bool operator<=(const tpoint& point) const
{ return x < point.x || (x == point.x && y <= point.y); }
};
std::ostream &operator<<(std::ostream &stream, const tpoint& point);
SDL_Rect create_rect(const tpoint& origin, const tpoint& size);
struct terror
{
terror(const std::string& msg) : message(msg) {}
const std::string message;
};
//! Base class with all possible events, most widgets can ignore most of
//! these, but they are available.
class tevent_executor

View file

@ -22,9 +22,8 @@
#include "gui/widgets/canvas.hpp"
#include "gui/widgets/event_handler.hpp"
#include "gui/widgets/grid.hpp"
#include "gui/widgets/helper.hpp"
#include "gui/widgets/settings.hpp"
// The following due to tpoint.
#include "gui/widgets/widget.hpp"
#include "sdl_utils.hpp"
#include "video.hpp"