Move the tcontrol into its own file.

Made some code in the widgets more generic and moved it into the
tcontrol class.
This commit is contained in:
Mark de Wever 2008-04-05 21:51:46 +00:00
parent de873197d7
commit a98d2bf535
14 changed files with 269 additions and 275 deletions

View file

@ -554,6 +554,7 @@ libwesnoth_sources = [
"src/gui/dialogs/addon_connect.cpp",
"src/gui/widgets/button.cpp",
"src/gui/widgets/canvas.cpp",
"src/gui/widgets/control.cpp",
"src/gui/widgets/event_handler.cpp",
"src/gui/widgets/grid.cpp",
"src/gui/widgets/label.cpp",

View file

@ -68,6 +68,7 @@ wesnoth_source = \
gui/dialogs/addon_connect.cpp \
gui/widgets/button.cpp \
gui/widgets/canvas.cpp \
gui/widgets/control.cpp \
gui/widgets/event_handler.cpp \
gui/widgets/grid.cpp \
gui/widgets/label.cpp \
@ -298,6 +299,7 @@ noinst_HEADERS = \
gui/dialogs/addon_connect.hpp \
gui/widgets/button.hpp \
gui/widgets/canvas.hpp \
gui/widgets/control.hpp \
gui/widgets/event_handler.hpp \
gui/widgets/grid.hpp \
gui/widgets/label.hpp \

View file

@ -15,6 +15,7 @@
#include "gui/widgets/button.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#define DBG_G LOG_STREAM(debug, gui)
#define LOG_G LOG_STREAM(info, gui)
@ -38,42 +39,6 @@
namespace gui2 {
void tbutton::set_width(const unsigned width)
{
// resize canvasses
canvas_enabled_.set_width(width);
canvas_disabled_.set_width(width);
canvas_pressed_.set_width(width);
canvas_focussed_.set_width(width);
// inherited
tcontrol::set_width(width);
}
void tbutton::set_height(const unsigned height)
{
// resize canvasses
canvas_enabled_.set_height(height);
canvas_disabled_.set_height(height);
canvas_pressed_.set_height(height);
canvas_focussed_.set_height(height);
// inherited
tcontrol::set_height(height);
}
void tbutton::set_label(const t_string& label)
{
// set label in canvases
canvas_enabled_.set_variable("text", variant(label.str()));
canvas_disabled_.set_variable("text", variant(label.str()));
canvas_pressed_.set_variable("text", variant(label.str()));
canvas_focussed_.set_variable("text", variant(label.str()));
// inherited
tcontrol::set_label(label);
}
void tbutton::mouse_enter(tevent_handler&)
{
@ -132,39 +97,6 @@ void tbutton::mouse_left_button_double_click(tevent_handler&)
DBG_G_E << "Button: left mouse button double click.\n";
}
void tbutton::draw(surface& canvas)
{
SDL_Rect rect = get_rect();
switch(state_) {
case ENABLED :
DBG_G_D << "Button: drawing enabled state.\n";
canvas_enabled_.draw(true);
SDL_BlitSurface(canvas_enabled_.surf(), 0, canvas, &rect);
break;
case DISABLED :
DBG_G_D << "Button: drawing disabled state.\n";
canvas_disabled_.draw(true);
SDL_BlitSurface(canvas_disabled_.surf(), 0, canvas, &rect);
break;
case PRESSED :
DBG_G_D << "Button: drawing pressed state.\n";
canvas_pressed_.draw(true);
SDL_BlitSurface(canvas_pressed_.surf(), 0, canvas, &rect);
break;
case FOCUSSED :
DBG_G_D << "Button: drawing focussed state.\n";
canvas_focussed_.draw(true);
SDL_BlitSurface(canvas_focussed_.surf(), 0, canvas, &rect);
break;
}
set_dirty(false);
}
tpoint tbutton::get_best_size() const
{
if(definition_ == std::vector<tbutton_definition::tresolution>::const_iterator()) {
@ -198,6 +130,20 @@ tbutton::RETVAL tbutton::get_retval_by_id(const std::string& id)
}
void tbutton::set_active(const bool active)
{
if(active && state_ == DISABLED) {
set_state(ENABLED);
} else if(!active && state_ != DISABLED) {
set_state(DISABLED);
}
}
bool tbutton::get_active() const
{
return state_ != DISABLED;
}
void tbutton::set_state(tstate state)
{
if(state != state_) {
@ -211,18 +157,14 @@ void tbutton::resolve_definition()
if(definition_ == std::vector<tbutton_definition::tresolution>::const_iterator()) {
definition_ = get_button(definition());
canvas_enabled_ = definition_->enabled.canvas;
canvas_disabled_ = definition_->disabled.canvas;
canvas_pressed_ = definition_->pressed.canvas;
canvas_focussed_ = definition_->focussed.canvas;
// FIXME we need some extra routines since a lot of code will
// be duplicated here otherwise.
canvas_enabled_.set_variable("text", variant(label()));
canvas_disabled_.set_variable("text", variant(label()));
canvas_pressed_.set_variable("text", variant(label()));
canvas_focussed_.set_variable("text", variant(label()));
canvas(0) = definition_->enabled.canvas;
canvas(1) = definition_->disabled.canvas;
canvas(2) = definition_->pressed.canvas;
canvas(3) = definition_->focussed.canvas;
// FIXME also an ugly hack, maybe set the stuff correct
// just prior to draw... only need to find out when needed.
set_label(label());
}
}

View file

@ -15,38 +15,24 @@
#ifndef __GUI_WIDGETS_BUTTON_HPP_INCLUDED__
#define __GUI_WIDGETS_BUTTON_HPP_INCLUDED__
#include "gui/widgets/widget.hpp"
#include "gui/widgets/control.hpp"
#include "gui/widgets/settings.hpp"
#include "log.hpp"
#include "tstring.hpp"
namespace gui2 {
// Class for a simple push button
class tbutton : public tcontrol
{
friend void load_settings();
public:
tbutton() :
tcontrol(),
canvas_enabled_(),
canvas_disabled_(),
canvas_pressed_(),
canvas_focussed_(),
tcontrol(COUNT),
state_(ENABLED),
definition_(),
retval_(0)
{
}
void set_width(const unsigned width);
void set_height(const unsigned height);
void set_label(const t_string& label);
void mouse_enter(tevent_handler&);
void mouse_hover(tevent_handler&);
void mouse_leave(tevent_handler&);
@ -56,8 +42,6 @@ public:
void mouse_left_button_click(tevent_handler&);
void mouse_left_button_double_click(tevent_handler&); //FIXME remove
void draw(surface& canvas);
// note we should check whether the label fits in the button
tpoint get_best_size() const;
@ -80,17 +64,16 @@ public:
//! Gets the retval for the default buttons.
static RETVAL get_retval_by_id(const std::string& id);
void set_active(const bool active);
bool get_active() const;
unsigned get_state() const { return state_; }
bool full_redraw() const { return false; /* FIXME IMPLEMENT */ }
protected:
private:
tcanvas
canvas_enabled_,
canvas_disabled_,
canvas_pressed_,
canvas_focussed_;
enum tstate { ENABLED, DISABLED, PRESSED, FOCUSSED };
enum tstate { ENABLED, DISABLED, PRESSED, FOCUSSED, COUNT };
void set_state(tstate state);
tstate state_;

103
src/gui/widgets/control.cpp Normal file
View file

@ -0,0 +1,103 @@
/* $Id$ */
/*
copyright (c) 2008 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 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/control.hpp"
#include "foreach.hpp"
#include "log.hpp"
#define DBG_G LOG_STREAM(debug, gui)
#define LOG_G LOG_STREAM(info, gui)
#define WRN_G LOG_STREAM(warn, gui)
#define ERR_G LOG_STREAM(err, gui)
#define DBG_G_D LOG_STREAM(debug, gui_draw)
#define LOG_G_D LOG_STREAM(info, gui_draw)
#define WRN_G_D LOG_STREAM(warn, gui_draw)
#define ERR_G_D LOG_STREAM(err, gui_draw)
#define DBG_G_E LOG_STREAM(debug, gui_event)
#define LOG_G_E LOG_STREAM(info, gui_event)
#define WRN_G_E LOG_STREAM(warn, gui_event)
#define ERR_G_E LOG_STREAM(err, gui_event)
#define DBG_G_P LOG_STREAM(debug, gui_parse)
#define LOG_G_P LOG_STREAM(info, gui_parse)
#define WRN_G_P LOG_STREAM(warn, gui_parse)
#define ERR_G_P LOG_STREAM(err, gui_parse)
namespace gui2 {
tcontrol::tcontrol(const unsigned canvas_count) :
visible_(true),
label_(),
tooltip_(),
help_message_(),
canvas_(canvas_count)
{
}
void tcontrol::set_width(const unsigned width)
{
// resize canvasses
foreach(tcanvas& canvas, canvas_) {
canvas.set_width(width);
}
// inherited
twidget::set_width(width);
}
void tcontrol::set_height(const unsigned height)
{
// resize canvasses
foreach(tcanvas& canvas, canvas_) {
canvas.set_height(height);
}
// inherited
twidget::set_height(height);
}
void tcontrol::set_label(const std::string& label)
{
// set label in canvases
foreach(tcanvas& canvas, canvas_) {
canvas.set_variable("text", variant(label));
}
label_ = label;
set_dirty();
}
void tcontrol::draw(surface& surface)
{
SDL_Rect rect = get_rect();
DBG_G_D << "Control: drawing.\n";
if(!restorer_) {
restorer_ = get_surface_portion(surface, rect);
}
if(full_redraw()) {
SDL_BlitSurface(restorer_, 0, surface, &rect);
rect = get_rect();
}
canvas(get_state()).draw(true);
SDL_BlitSurface(canvas(get_state()).surf(), 0, surface, &rect);
set_dirty(false);
}
} // namespace gui2

View file

@ -0,0 +1,98 @@
/* $Id$ */
/*
copyright (c) 2008 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 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_CONTROL_HPP_INCLUDED__
#define __GUI_WIDGETS_CONTROL_HPP_INCLUDED__
#include "gui/widgets/widget.hpp"
namespace gui2 {
//! Base class for all visible items.
class tcontrol : public virtual twidget
{
tcontrol();
public:
tcontrol(const unsigned canvas_count);
virtual ~tcontrol() {}
void set_width(const unsigned width);
void set_height(const unsigned height);
void set_visible(const bool visible) { visible_ = visible; set_dirty(); }
bool get_visible() const { return visible_; }
void set_label(const std::string& label);
const std::string& label() const { return label_; }
//! Note when modifing the label the caller should set the widget to
//! dirty.
std::string& label() { return label_; }
// Note setting the tooltip_ doesn't dirty an object.
void set_tooltip(const t_string& tooltip) { tooltip_ = tooltip; }
const t_string& tooltip() const { return tooltip_; }
// Note setting the help_message_ doesn't dirty an object.
void set_help_message(const t_string& help_message) { help_message_ = help_message; }
const t_string& help_message() const { return help_message_; }
std::vector<tcanvas>& canvas() { return canvas_; }
tcanvas& canvas(const unsigned index) { return canvas_[index]; } // FIXME an assert would be nice
//! Draws the widget.
void draw(surface& surface);
virtual void set_active(const bool active) = 0;
virtual bool get_active() const = 0;
protected:
//! Returns the id of the state, which is also the index for the canvas.
virtual unsigned get_state() const = 0;
//! Does the widget need to restore the surface before (re)painting?
virtual bool full_redraw() const = 0;
private:
//! Visible state of the widget, invisible isn't drawn.
bool visible_;
std::string label_;
//! When hovering a tooltip with extra information can show up. (FIXME implement)
t_string tooltip_;
//! When the user presses help a tooltip with even more info can be shown. (FIXME implement)
t_string help_message_;
//! Holds all canvas objects for a control.
std::vector<tcanvas> canvas_;
//! Holds a copy of the original background which can be used before
//! redrawing. This is needed for semi-tranparent items, the user
//! defines whether it's required or not.
surface restorer_;
};
} // namespace gui2
#endif

View file

@ -16,6 +16,7 @@
#define __GUI_WIDGETS_GRID_HPP_INCLUDED__
#include "gui/widgets/widget.hpp"
#include "gui/widgets/control.hpp"
namespace gui2 {
@ -200,7 +201,7 @@ class tpanel : public tgrid, public tcontrol
public:
tpanel() :
tgrid(0, 0, 0, 0),
tcontrol()
tcontrol(0)
{}
private:

View file

@ -39,34 +39,6 @@
namespace gui2 {
void tlabel::set_width(const unsigned width)
{
// resize canvasses
canvas_.set_width(width);
// inherited
tcontrol::set_width(width);
}
void tlabel::set_height(const unsigned height)
{
// resize canvasses
canvas_.set_height(height);
// inherited
tcontrol::set_height(height);
}
void tlabel::set_label(const t_string& label)
{
// set label in canvases
canvas_.set_variable("text", variant(label.str()));
// inherited
tcontrol::set_label(label);
}
tpoint tlabel::get_best_size() const
{
if(definition_ == std::vector<tlabel_definition::tresolution>::const_iterator()) {
@ -81,7 +53,7 @@ void tlabel::mouse_hover(tevent_handler&)
DBG_G_E << "Text_box: mouse hover.\n";
}
void tlabel::draw(surface& canvas)
void tlabel::draw(surface& surface)
{
SDL_Rect rect = get_rect();
@ -94,8 +66,8 @@ void tlabel::draw(surface& canvas)
rect = get_rect();
}
*/
canvas_.draw(true);
SDL_BlitSurface(canvas_.surf(), 0, canvas, &rect);
canvas(0).draw(true);
SDL_BlitSurface(canvas(0).surf(), 0, surface, &rect);
set_dirty(false);
}
@ -115,9 +87,9 @@ void tlabel::resolve_definition()
if(definition_ == std::vector<tlabel_definition::tresolution>::const_iterator()) {
definition_ = get_label(definition());
canvas_= definition_->enabled.canvas;
canvas(0) = definition_->enabled.canvas;
canvas_.set_variable("text", variant(label()));
canvas(0).set_variable("text", variant(label()));
}
}

View file

@ -15,7 +15,7 @@
#ifndef __GUI_WIDGETS_LABEL_HPP_INCLUDED__
#define __GUI_WIDGETS_LABEL_HPP_INCLUDED__
#include "gui/widgets/widget.hpp"
#include "gui/widgets/control.hpp"
#include "gui/widgets/settings.hpp"
@ -26,19 +26,17 @@ class tlabel : public tcontrol
public:
tlabel() :
tcontrol(),
canvas_()
tcontrol(1)
{}
void set_width(const unsigned width);
void set_height(const unsigned height);
void set_label(const t_string& label);
void set_active(const bool active) { /*FIXME IMPLEMENT*/ };
bool get_active() const { return true; /* FIXME IMPLEMENT */ }
unsigned get_state() const { return 0; /* FIXME IMPLEMENT */ }
bool full_redraw() const { return false; /* FIXME IMPLEMENT */ }
void mouse_hover(tevent_handler&);
void draw(surface& canvas);
void draw(surface& surface);
// note we should check whether the label fits in the label
tpoint get_best_size() const;
@ -46,8 +44,6 @@ public:
void set_best_size(const tpoint& origin);
private:
tcanvas canvas_;
std::vector<tlabel_definition::tresolution>::const_iterator definition_;
void resolve_definition();

View file

@ -58,32 +58,10 @@ void ttext_::set_cursor(const size_t offset, const bool select)
}
}
void ttext_::set_width(const unsigned width)
{
// resize canvasses
canvas_.set_width(width);
// inherited
tcontrol::set_width(width);
}
void ttext_::set_height(const unsigned height)
{
// resize canvasses
canvas_.set_height(height);
// inherited
tcontrol::set_height(height);
}
void ttext_::set_label(const t_string& label)
bool ttext_::full_redraw() const
{
// set label in canvases
canvas_.set_variable("text", variant(label.str()));
// inherited
tcontrol::set_label(label);
// FIXME make sure definition_ is valid before usage!
return definition_->enabled.full_redraw;
}
void ttext_::mouse_move(tevent_handler&)
@ -205,25 +183,6 @@ void ttext_::key_press(tevent_handler& event, bool& handled, SDLKey key, SDLMod
}
void ttext_::draw(surface& canvas)
{
SDL_Rect rect = get_rect();
DBG_G_D << "Text box: drawing enabled state.\n";
if(!restorer_) {
restorer_ = get_surface_portion(canvas, rect);
}
if(definition_->enabled.full_redraw) {
SDL_BlitSurface(restorer_, 0, canvas, &rect);
rect = get_rect();
}
canvas_.draw(true);
SDL_BlitSurface(canvas_.surf(), 0, canvas, &rect);
set_dirty(false);
}
tpoint ttext_::get_best_size() const
{
if(definition_ == std::vector<ttext_box_definition::tresolution>::const_iterator()) {
@ -248,11 +207,11 @@ void ttext_::resolve_definition()
if(definition_ == std::vector<ttext_box_definition::tresolution>::const_iterator()) {
definition_ = get_text_box(definition());
canvas_= definition_->enabled.canvas;
canvas(0) = definition_->enabled.canvas;
// FIXME we need some extra routines since a lot of code will
// be duplicated here otherwise.
canvas_.set_variable("text", variant(label()));
canvas(0).set_variable("text", variant(label()));
}
}

View file

@ -15,7 +15,7 @@
#ifndef __GUI_WIDGETS_TEXT_BOX_HPP_INCLUDED__
#define __GUI_WIDGETS_TEXT_BOX_HPP_INCLUDED__
#include "gui/widgets/widget.hpp"
#include "gui/widgets/control.hpp"
#include "gui/widgets/settings.hpp"
@ -32,9 +32,7 @@ class ttext_ : public tcontrol
public:
ttext_() :
tcontrol(),
canvas_(),
restorer_(),
tcontrol(1),
definition_(),
dragging_(false),
sel_start_(0),
@ -42,11 +40,10 @@ public:
max_length_(std::string::npos)
{}
void set_width(const unsigned width);
void set_height(const unsigned height);
void set_label(const t_string& label);
void set_active(const bool active) { /*FIXME IMPLEMENT*/ };
bool get_active() const { return true; /* FIXME IMPLEMENT */ }
unsigned get_state() const { return 0; /* FIXME IMPLEMENT */ }
bool full_redraw() const;
void mouse_move(tevent_handler&);
void mouse_hover(tevent_handler&);
@ -57,14 +54,10 @@ public:
void key_press(tevent_handler& event, bool& handled, SDLKey key, SDLMod modifier, Uint16 unicode);
void draw(surface& canvas);
tpoint get_best_size() const;
void set_best_size(const tpoint& origin);
protected:
virtual void goto_end_of_line(const bool select = false) = 0;
@ -82,9 +75,6 @@ protected:
void set_sel_len(const unsigned sel_len) { sel_len_ = sel_len; set_dirty(); }
private:
tcanvas canvas_;
surface restorer_;
std::vector<ttext_box_definition::tresolution>::const_iterator definition_;

View file

@ -88,13 +88,4 @@ twindow* twidget::get_window()
return dynamic_cast<twindow*>(result);
}
tcontrol::tcontrol() :
visible_(true),
label_(),
tooltip_(),
help_message_(),
canvas_()
{
}
} // namespace gui2

View file

@ -190,9 +190,9 @@ public:
void set_definition(const std::string& definition)
{ definition_ = definition; }
// draw event does nothing by default, maybe make pure virtual later
// fixme add force as parameter
virtual void draw(surface& /*canvas*/) {}
//! Draws a widget.
// FIXME add force as parameter
virtual void draw(surface& /*surface*/) = 0;
// invisible widgets never hit, only items with a size NOT here
// virtual bool does_hit(const int x, const int y) const { return false; }
@ -276,56 +276,6 @@ private:
bool dirty_;
};
//! Base class for all visible items.
class tcontrol : public virtual twidget
{
public:
tcontrol();
virtual ~tcontrol() {}
void set_visible(const bool visible) { visible_ = visible; set_dirty(); }
bool get_visible() const { return visible_; }
void set_label(const std::string& label) { label_ = label; set_dirty(); }
const std::string& label() const { return label_; }
//! Note when modifing the label the caller should set the widget to
//! dirty.
std::string& label() { return label_; }
// Note setting the tooltip_ doesn't dirty an object.
void set_tooltip(const t_string& tooltip) { tooltip_ = tooltip; }
const t_string& tooltip() const { return tooltip_; }
// Note setting the help_message_ doesn't dirty an object.
void set_help_message(const t_string& help_message) { help_message_ = help_message; }
const t_string& help_message() const { return help_message_; }
std::vector<tcanvas>& canvas() { return canvas_; }
// void set_active(const bool active) = 0;
// bool get_active() const = 0;
private:
//! Visible state of the widget, invisible isn't drawn.
bool visible_;
std::string label_;
//! When hovering a tooltip with extra information can show up. (FIXME implement)
t_string tooltip_;
//! When the user presses help a tooltip with even more info can be shown. (FIXME implement)
t_string help_message_;
//! Holds all canvas objects for a control. (FIXME implement)
std::vector<tcanvas> canvas_;
};
} // namespace gui2
#endif

View file

@ -82,6 +82,12 @@ public:
void window_resize(tevent_handler&,
const unsigned new_width, const unsigned new_height);
//! A window is always active atm so ignore the request.
void set_active(const bool active) {}
bool get_active() const { return true; }
unsigned get_state() const { return 0; }
bool full_redraw() const { return false; /* FIXME IMPLEMENT */ }
protected:
private: