Another refactoring.

- remove set_best_size()

- move more code in tcontrol.
This commit is contained in:
Mark de Wever 2008-04-08 16:04:03 +00:00
parent 9deecb8b7f
commit 8d2a2a6d54
14 changed files with 125 additions and 231 deletions

View file

@ -97,43 +97,6 @@ void tbutton::mouse_left_button_double_click(tevent_handler&)
DBG_G_E << "Button: left mouse button double click.\n";
}
tpoint tbutton::get_minimum_size() const
{
if(definition_ == std::vector<tbutton_definition::tresolution>::const_iterator()) {
return tpoint(get_button(definition())->min_width, get_button(definition())->min_height);
} else {
return tpoint(definition_->min_width, definition_->min_height);
}
}
tpoint tbutton::get_best_size() const
{
if(definition_ == std::vector<tbutton_definition::tresolution>::const_iterator()) {
return tpoint(get_button(definition())->default_width, get_button(definition())->default_height);
} else {
return tpoint(definition_->default_width, definition_->default_height);
}
}
tpoint tbutton::get_maximum_size() const
{
if(definition_ == std::vector<tbutton_definition::tresolution>::const_iterator()) {
return tpoint(get_button(definition())->max_width, get_button(definition())->max_height);
} else {
return tpoint(definition_->max_width, definition_->max_height);
}
}
void tbutton::set_best_size(const tpoint& origin)
{
resolve_definition();
set_x(origin.x);
set_y(origin.y);
set_width(definition_->default_width);
set_height(definition_->default_height);
}
tbutton::RETVAL tbutton::get_retval_by_id(const std::string& id)
{
//! Note it might change to a map later depending on the number
@ -145,7 +108,6 @@ tbutton::RETVAL tbutton::get_retval_by_id(const std::string& id)
} else {
return NONE;
}
}
void tbutton::set_active(const bool active)
@ -170,14 +132,14 @@ void tbutton::set_state(tstate state)
}
}
void tbutton::resolve_definition()
void tbutton::load_config()
{
if(definition_ == std::vector<tbutton_definition::tresolution>::const_iterator()) {
definition_ = get_button(definition());
if(!config()) {
set_config(get_button(definition()));
assert(canvas().size() == definition_->state.size());
assert(canvas().size() == config()->state.size());
for(size_t i = 0; i < canvas().size(); ++i) {
canvas(i) = definition_->state[i].canvas;
canvas(i) = config()->state[i].canvas;
}
set_canvas_text();

View file

@ -28,7 +28,6 @@ public:
tbutton() :
tcontrol(COUNT),
state_(ENABLED),
definition_(),
retval_(0)
{
}
@ -42,14 +41,6 @@ public:
void mouse_left_button_click(tevent_handler&);
void mouse_left_button_double_click(tevent_handler&); //FIXME remove
// note we should check whether the label fits in the button
// Inherited from twidget.
tpoint get_minimum_size() const;
tpoint get_best_size() const;
tpoint get_maximum_size() const;
void set_best_size(const tpoint& origin);
void set_retval(const int retval) { retval_ = retval; }
//! Default button values, values are subject to change.
@ -73,6 +64,9 @@ public:
unsigned get_state() const { return state_; }
bool full_redraw() const { return false; /* FIXME IMPLEMENT */ }
//! Inherited from twidget.
void load_config();
protected:
private:
@ -81,11 +75,7 @@ private:
void set_state(tstate state);
tstate state_;
std::vector<tbutton_definition::tresolution>::const_iterator definition_;
void resolve_definition();
int retval_;
};

View file

@ -44,7 +44,9 @@ tcontrol::tcontrol(const unsigned canvas_count) :
label_(),
tooltip_(),
help_message_(),
canvas_(canvas_count)
canvas_(canvas_count),
restorer_(),
config_(0)
{
}
@ -93,6 +95,25 @@ void tcontrol::set_label(const t_string& label)
set_dirty();
}
tpoint tcontrol::get_minimum_size() const
{
assert(config_);
return tpoint(config_->min_width, config_->min_height);
}
tpoint tcontrol::get_best_size() const
{
assert(config_);
return tpoint(config_->default_width, config_->default_height);
}
tpoint tcontrol::get_maximum_size() const
{
assert(config_);
return tpoint(config_->max_width, config_->max_height);
}
//! Sets the text variable for the canvases.
void tcontrol::set_canvas_text()
{

View file

@ -16,6 +16,7 @@
#define __GUI_WIDGETS_CONTROL_HPP_INCLUDED__
#include "gui/widgets/canvas.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/widget.hpp"
#include "tstring.hpp"
@ -70,6 +71,12 @@ public:
//! Gets the active state of the control.
virtual bool get_active() const = 0;
// note we should check whether the label fits in the button
// Inherited from twidget.
tpoint get_minimum_size() const;
tpoint get_best_size() const;
tpoint get_maximum_size() const;
protected:
//! Returns the id of the state, which is also the index for the canvas.
@ -81,6 +88,11 @@ protected:
//! Sets the text variable for the canvases.
virtual void set_canvas_text();
tresolution_definition_* config() { return config_; }
const tresolution_definition_* const config() const { return config_; }
void set_config(tresolution_definition_* config) { config_ = config; }
private:
//! Visible state of the control, invisible isn't drawn.
@ -102,6 +114,10 @@ private:
//! redrawing. This is needed for semi-tranparent items, the user
//! defines whether it's required or not.
surface restorer_;
//! Contains a pointer to the configuration of this button at the
//! current resolution.
tresolution_definition_* config_;
};
} // namespace gui2

View file

@ -212,21 +212,6 @@ tpoint tgrid::get_best_size() const
std::accumulate(best_row_height_.begin(), best_row_height_.end(), 0));
}
void tgrid::set_best_size(const tpoint& origin)
{
// Update the sizes.
get_best_size();
assert(best_col_width_.size() == cols_);
assert(best_row_height_.size() == rows_);
row_height_ = best_row_height_;
col_width_ = best_col_width_;
layout(origin);
return;
}
void tgrid::set_size(const SDL_Rect& rect)
{
twidget::set_size(rect);
@ -359,6 +344,17 @@ twidget* tgrid::get_widget_by_id(const std::string& id)
return twidget::get_widget_by_id(id);
}
void tgrid::load_config()
{
for(std::vector<tchild>::iterator itor = children_.begin();
itor != children_.end(); ++itor) {
if(itor->widget()) {
itor->widget()->load_config();
}
}
}
void tgrid::clear_cache()
{
best_row_height_.clear();
@ -415,7 +411,6 @@ tpoint tgrid::tchild::get_best_size() const
void tgrid::tchild::set_size(tpoint orig, tpoint size)
{
assert(widget());
widget()->set_best_size(orig); // needed for calling resolve_resolution()
if(border_size_) {
if(flags_ & BORDER_TOP) {

View file

@ -90,9 +90,6 @@ public:
tpoint get_best_size() const;
tpoint get_maximum_size() const { /*FIXME IMPLEMENT*/ return tpoint(0,0); }
//! Inherited from twidget.
void set_best_size(const tpoint& origin);
//! Inherited from twidget.
void set_size(const SDL_Rect& rect);
@ -107,6 +104,9 @@ public:
//! Inherited from twidget.
void draw(surface& surface) { /* FIXME IMPLEMENT */ }
//! Inherited from twidget.
void load_config();
private:
class tchild
{
@ -298,6 +298,10 @@ public:
void set_col_scaling(const unsigned col, const unsigned scale)
{ grid_.set_col_scaling(col, scale); }
//! Inherited from twidget.
//FIXME we also need to load our own config
void load_config() { grid_.load_config(); }
private:
tgrid grid_;

View file

@ -39,33 +39,6 @@
namespace gui2 {
tpoint tlabel::get_minimum_size() const
{
if(definition_ == std::vector<tlabel_definition::tresolution>::const_iterator()) {
return tpoint(get_label(definition())->min_width, get_label(definition())->min_height);
} else {
return tpoint(definition_->min_width, definition_->min_height);
}
}
tpoint tlabel::get_best_size() const
{
if(definition_ == std::vector<tlabel_definition::tresolution>::const_iterator()) {
return tpoint(get_label(definition())->default_width, get_label(definition())->default_height);
} else {
return tpoint(definition_->default_width, definition_->default_height);
}
}
tpoint tlabel::get_maximum_size() const
{
if(definition_ == std::vector<tlabel_definition::tresolution>::const_iterator()) {
return tpoint(get_label(definition())->max_width, get_label(definition())->max_height);
} else {
return tpoint(definition_->max_width, definition_->max_height);
}
}
void tlabel::mouse_hover(tevent_handler&)
{
DBG_G_E << "Text_box: mouse hover.\n";
@ -90,16 +63,6 @@ void tlabel::draw(surface& surface)
set_dirty(false);
}
void tlabel::set_best_size(const tpoint& origin)
{
resolve_definition();
set_x(origin.x);
set_y(origin.y);
set_width(definition_->default_width);
set_height(definition_->default_height);
}
void tlabel::set_state(tstate state)
{
if(state != state_) {
@ -108,17 +71,17 @@ void tlabel::set_state(tstate state)
}
}
void tlabel::resolve_definition()
void tlabel::load_config()
{
if(definition_ == std::vector<tlabel_definition::tresolution>::const_iterator()) {
definition_ = get_label(definition());
if(!config()) {
set_config(get_label(definition()));
assert(canvas().size() == definition_->state.size());
assert(canvas().size() == config()->state.size());
for(size_t i = 0; i < canvas().size(); ++i) {
canvas(i) = definition_->state[i].canvas;
canvas(i) = config()->state[i].canvas;
}
set_canvas_text();
set_canvas_text();
}
}

View file

@ -38,23 +38,15 @@ public:
void draw(surface& surface);
// note we should check whether the label fits in the label
// Inherited from twidget.
tpoint get_minimum_size() const;
tpoint get_best_size() const;
tpoint get_maximum_size() const;
//! Inherited from twidget.
void load_config();
void set_best_size(const tpoint& origin);
private:
//! Note the order of the states must be the same as defined in settings.hpp.
enum tstate { ENABLED, DISABLED, COUNT };
void set_state(tstate state);
tstate state_;
std::vector<tlabel_definition::tresolution>::const_iterator definition_;
void resolve_definition();
};
} // namespace gui2

View file

@ -241,7 +241,7 @@ const std::string& tbutton_definition::read(const config& cfg)
for(std::vector<config*>::const_iterator itor = cfgs.begin();
itor != cfgs.end(); ++itor) {
resolutions.push_back(tresolution(**itor));
resolutions.push_back(new tresolution(**itor));
}
return id;
@ -292,7 +292,7 @@ const std::string& tlabel_definition::read(const config& cfg)
for(std::vector<config*>::const_iterator itor = cfgs.begin();
itor != cfgs.end(); ++itor) {
resolutions.push_back(tresolution(**itor));
resolutions.push_back(new tresolution(**itor));
}
return id;
@ -421,7 +421,7 @@ const std::string& ttext_box_definition::read(const config& cfg)
for(std::vector<config*>::const_iterator itor = cfgs.begin();
itor != cfgs.end(); ++itor) {
resolutions.push_back(tresolution(**itor));
resolutions.push_back(new tresolution(**itor));
}
return id;
@ -523,7 +523,7 @@ twindow_definition::tresolution::tlayer::tlayer(const config* cfg) :
canvas.set_cfg(*draw);
}
std::vector<tbutton_definition::tresolution>::const_iterator get_button(const std::string& definition)
tresolution_definition_* get_button(const std::string& definition)
{
std::map<std::string, tbutton_definition>::const_iterator
button = current_gui->second.buttons.find(definition);
@ -533,25 +533,25 @@ std::vector<tbutton_definition::tresolution>::const_iterator get_button(const st
assert(button != current_gui->second.buttons.end());
}
for(std::vector<tbutton_definition::tresolution>::const_iterator
for(std::vector<tresolution_definition_*>::const_iterator
itor = button->second.resolutions.begin(),
end = button->second.resolutions.end();
itor != end;
++itor) {
if(screen_width <= itor->window_width &&
screen_height <= itor->window_height) {
if(screen_width <= (**itor).window_width &&
screen_height <= (**itor).window_height) {
return itor;
return *itor;
} else if (itor == end - 1) {
return itor;
return *itor;
}
}
assert(false);
}
std::vector<tlabel_definition::tresolution>::const_iterator get_label(const std::string& definition)
tresolution_definition_* get_label(const std::string& definition)
{
std::map<std::string, tlabel_definition>::const_iterator
label = current_gui->second.labels.find(definition);
@ -561,25 +561,25 @@ std::vector<tlabel_definition::tresolution>::const_iterator get_label(const std:
assert(label != current_gui->second.labels.end());
}
for(std::vector<tlabel_definition::tresolution>::const_iterator
for(std::vector<tresolution_definition_*>::const_iterator
itor = label->second.resolutions.begin(),
end = label->second.resolutions.end();
itor != end;
++itor) {
if(screen_width <= itor->window_width &&
screen_height <= itor->window_height) {
if(screen_width <= (**itor).window_width &&
screen_height <= (**itor).window_height) {
return itor;
return *itor;
} else if (itor == end - 1) {
return itor;
return *itor;
}
}
assert(false);
}
std::vector<ttext_box_definition::tresolution>::const_iterator get_text_box(const std::string& definition)
tresolution_definition_* get_text_box(const std::string& definition)
{
std::map<std::string, ttext_box_definition>::const_iterator
text_box = current_gui->second.text_boxs.find(definition);
@ -589,18 +589,18 @@ std::vector<ttext_box_definition::tresolution>::const_iterator get_text_box(cons
assert(text_box != current_gui->second.text_boxs.end());
}
for(std::vector<ttext_box_definition::tresolution>::const_iterator
for(std::vector<tresolution_definition_*>::const_iterator
itor = text_box->second.resolutions.begin(),
end = text_box->second.resolutions.end();
itor != end;
++itor) {
if(screen_width <= itor->window_width &&
screen_height <= itor->window_height) {
if(screen_width <= (**itor).window_width &&
screen_height <= (**itor).window_height) {
return itor;
return *itor;
} else if (itor == end - 1) {
return itor;
return *itor;
}
}

View file

@ -106,7 +106,7 @@ struct tbutton_definition
void read_extra(const config& cfg);
};
std::vector<tresolution> resolutions;
std::vector<tresolution_definition_*> resolutions;
};
struct tlabel_definition
@ -127,7 +127,7 @@ struct tlabel_definition
void read_extra(const config& cfg);
};
std::vector<tresolution> resolutions;
std::vector<tresolution_definition_*> resolutions;
};
struct ttext_box_definition
@ -147,7 +147,7 @@ struct ttext_box_definition
void read_extra(const config& cfg);
};
std::vector<tresolution> resolutions;
std::vector<tresolution_definition_*> resolutions;
};
struct twindow_definition
@ -212,9 +212,9 @@ struct tgui_definition
std::map<std::string, twindow_builder> window_types;
};
std::vector<tbutton_definition::tresolution>::const_iterator get_button(const std::string& definition);
std::vector<tlabel_definition::tresolution>::const_iterator get_label(const std::string& definition);
std::vector<ttext_box_definition::tresolution>::const_iterator get_text_box(const std::string& definition);
tresolution_definition_* get_button(const std::string& definition);
tresolution_definition_* get_label(const std::string& definition);
tresolution_definition_* get_text_box(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);

View file

@ -323,7 +323,7 @@ void ttext_box::insert_char(Uint16 unicode)
std::string tmp_text;
tmp_text.insert(tmp_text.begin(), unicode);
surface surf = render_text(tmp_text, definition_->text_font_size);
surface surf = render_text(tmp_text, config()->text_font_size);
assert(surf);
const unsigned width = surf->w;
@ -399,11 +399,8 @@ void ttext_box::delete_selection()
bool ttext_box::full_redraw() const
{
if(definition_ != std::vector<ttext_box_definition::tresolution>::const_iterator()) {
return definition_->state[get_state()].full_redraw;
} else {
return 0;
}
assert(config());
return config()->state[get_state()].full_redraw;
}
//! Inherited from tcontrol.
@ -420,52 +417,19 @@ void ttext_box::set_canvas_text()
}
}
tpoint ttext_box::get_minimum_size() const
{
if(definition_ == std::vector<ttext_box_definition::tresolution>::const_iterator()) {
return tpoint(get_text_box(definition())->min_width, get_text_box(definition())->min_height);
} else {
return tpoint(definition_->min_width, definition_->min_height);
}
}
tpoint ttext_box::get_best_size() const
{
if(definition_ == std::vector<ttext_box_definition::tresolution>::const_iterator()) {
return tpoint(get_text_box(definition())->default_width, get_text_box(definition())->default_height);
} else {
return tpoint(definition_->default_width, definition_->default_height);
}
}
tpoint ttext_box::get_maximum_size() const
{
if(definition_ == std::vector<ttext_box_definition::tresolution>::const_iterator()) {
return tpoint(get_text_box(definition())->max_width, get_text_box(definition())->max_height);
} else {
return tpoint(definition_->max_width, definition_->max_height);
}
}
void ttext_box::set_best_size(const tpoint& origin)
{
resolve_definition();
set_x(origin.x);
set_y(origin.y);
set_width(definition_->default_width);
set_height(definition_->default_height);
}
//! Calculates the offsets of all chars.
void ttext_box::calculate_char_offset()
{
// If the text is set before the config is loaded do it ourselves.
// This isn't really clean solution, maybe fix it later.
if(!config()) {
load_config();
}
assert(config());
character_offset_.clear();
std::string rendered_text;
const unsigned font_size =
definition_ == std::vector<ttext_box_definition::tresolution>::const_iterator() ?
0 : definition_->text_font_size;
const unsigned font_size = config()->text_font_size;
// FIXME we assume the text start at offset 0!!!
foreach(const wchar_t& unicode, utils::string_to_wstring(text())) {
@ -484,17 +448,16 @@ void ttext_box::handle_key_clear_line(SDLMod modifier, bool& handled)
set_text("");
}
void ttext_box::resolve_definition()
void ttext_box::load_config()
{
if(definition_ == std::vector<ttext_box_definition::tresolution>::const_iterator()) {
definition_ = get_text_box(definition());
if(!config()) {
set_config(get_text_box(definition()));
assert(canvas().size() == definition_->state.size());
assert(canvas().size() == config()->state.size());
for(size_t i = 0; i < canvas().size(); ++i) {
canvas(i) = definition_->state[i].canvas;
canvas(i) = config()->state[i].canvas;
}
calculate_char_offset();
set_canvas_text();
}
}

View file

@ -154,8 +154,6 @@ private:
// These are ignored by a single line edit box which is the default behaviour.
virtual void handle_key_page_up(SDLMod modifier, bool& handled) {}
virtual void handle_key_page_down(SDLMod modifier, bool& handled) {}
virtual void resolve_definition() = 0;
};
//! Class for a single line text area.
@ -166,8 +164,7 @@ public:
ttext_box() :
ttext_(),
character_offset_(),
definition_()
character_offset_()
{}
@ -198,13 +195,9 @@ protected:
void goto_end_of_line(const bool select = false) { goto_end_of_data(select); }
void goto_start_of_line(const bool select = false) { goto_start_of_data(select); }
// note we should check whether the label fits in the button
// Inherited from twidget.
tpoint get_minimum_size() const;
tpoint get_best_size() const;
tpoint get_maximum_size() const;
//! Inherited from twidget.
void load_config();
void set_best_size(const tpoint& origin);
private:
void handle_key_up_arrow(SDLMod modifier, bool& handled) {};
@ -218,10 +211,6 @@ private:
//! Inherited from ttext_.
void calculate_char_offset();
std::vector<ttext_box_definition::tresolution>::const_iterator definition_;
void resolve_definition();
};

View file

@ -203,14 +203,6 @@ public:
//! Is the widget dirty?
virtual bool dirty() const { return dirty_; }
//! Sets the best size for the object.
virtual void set_best_size(const tpoint& origin)
{ set_size(create_rect(origin, get_best_size())); }
//! Sets the minumum size for the object.
// virtual void set_minimum_size();
//! Gets the minimum size for the object should != 0,0.
virtual tpoint get_minimum_size() const = 0;
@ -244,6 +236,9 @@ public:
//! The toplevel item should always be a window if not null is returned
twindow* get_window();
//! loads the configuration of the widget, mainly used for controls.
virtual void load_config() {}
protected:
virtual void set_dirty(const bool dirty = true)
{
@ -270,6 +265,7 @@ private:
int x_, y_;
unsigned w_, h_;
bool dirty_;
};
} // namespace gui2

View file

@ -75,6 +75,9 @@ int twindow::show(const bool restore, void* /*flip_function*/)
// FIXME throw an exception
}
// Update all configs.
load_config();
// We cut a piece of the screen and use that, that way all coordinates
// are relative to the window.