Polish teditor_settings.

- Use register functions.

- Remove use_mdi from caller in favour of the preferences.
This commit is contained in:
Mark de Wever 2011-03-20 18:26:16 +00:00
parent 81e22cfc92
commit a2097ad852
5 changed files with 97 additions and 131 deletions

View file

@ -103,7 +103,6 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
, background_terrain_(t_translation::GRASS_LAND)
, clipboard_()
, auto_update_transitions_(preferences::editor::auto_update_transitions())
, use_mdi_(preferences::editor::use_mdi())
, default_dir_(preferences::editor::default_dir())
{
create_default_context();
@ -385,23 +384,8 @@ void editor_controller::editor_settings_dialog()
return;
}
gui2::teditor_settings dialog;
dialog.set_tods(tods_);
dialog.set_current_adjustment(preferences::editor::tod_r(), preferences::editor::tod_g(), preferences::editor::tod_b());
dialog.set_redraw_callback(boost::bind(&editor_controller::editor_settings_dialog_redraw_callback, this, _1, _2, _3));
image::color_adjustment_resetter adjust_resetter;
dialog.set_use_mdi(use_mdi_);
dialog.show(gui().video());
int res = dialog.get_retval();
if(res == gui2::twindow::OK) {
image::set_color_adjustment(dialog.get_red(), dialog.get_green(), dialog.get_blue());
preferences::editor::set_tod_r(dialog.get_red());
preferences::editor::set_tod_g(dialog.get_green());
preferences::editor::set_tod_b(dialog.get_blue());
use_mdi_ = dialog.get_use_mdi();
preferences::editor::set_use_mdi(use_mdi_);
} else {
if(!gui2::teditor_settings::execute(&(gui()), tods_, gui().video())) {
adjust_resetter.reset();
}
refresh_all();
@ -431,20 +415,25 @@ void editor_controller::set_default_dir(const std::string& str)
void editor_controller::load_map_dialog(bool force_same_context /* = false */)
{
if (!use_mdi_ && !confirm_discard()) return;
if (!preferences::editor::use_mdi() && !confirm_discard()) {
return;
}
std::string fn = directory_name(get_map_context().get_filename());
if (fn.empty()) {
fn = default_dir_;
}
int res = dialogs::show_file_chooser_dialog(gui(), fn, _("Choose a Map to Open"));
if (res == 0) {
load_map(fn, force_same_context ? false : use_mdi_);
load_map(fn, force_same_context
? false
: preferences::editor::use_mdi());
}
}
void editor_controller::new_map_dialog()
{
if (!use_mdi_ && !confirm_discard()) {
if (!preferences::editor::use_mdi() && !confirm_discard()) {
return;
}
@ -452,7 +441,7 @@ void editor_controller::new_map_dialog()
int h = get_map().h();
if(gui2::teditor_new_map::execute(w, h, gui().video())) {
const t_translation::t_terrain fill = t_translation::GRASS_LAND;
new_map(w, h, fill, use_mdi_);
new_map(w, h, fill, preferences::editor::use_mdi());
}
}

View file

@ -439,8 +439,6 @@ class editor_controller : public controller_base,
/** Flag to rebuild terrain on every terrain change */
int auto_update_transitions_;
bool use_mdi_;
/** Default directory for map load/save as dialogs */
std::string default_dir_;
};

View file

@ -17,20 +17,20 @@
#include "gui/dialogs/editor_settings.hpp"
#include "editor/editor_preferences.hpp"
#include "editor/editor_display.hpp"
#include "gui/dialogs/field.hpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/slider.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gettext.hpp"
#include "image.hpp"
#include <boost/bind.hpp>
#define ERR_ED LOG_STREAM_INDENT(err, editor)
namespace gui2 {
/*WIKI
@ -76,23 +76,33 @@ namespace gui2 {
REGISTER_DIALOG(editor_settings)
teditor_settings::teditor_settings()
: redraw_callback_()
, tods_()
teditor_settings::teditor_settings(editor::editor_display* display
, const std::vector<time_of_day>& tods)
: tods_(tods)
, current_tod_(0)
, current_tod_label_(NULL)
, current_tod_image_(NULL)
, custom_tod_toggle_(NULL)
, custom_tod_auto_refresh_(NULL)
, custom_tod_toggle_field_(register_bool("custom_tod_toggle", false))
, custom_tod_red_(NULL)
, custom_tod_green_(NULL)
, custom_tod_blue_(NULL)
, custom_tod_red_field_(register_integer("custom_tod_red", false))
, custom_tod_green_field_(register_integer("custom_tod_green", false))
, custom_tod_blue_field_(register_integer("custom_tod_blue", false))
, use_mdi_field_(register_bool("use_mdi"))
, custom_tod_red_field_(register_integer("custom_tod_red"
, false
, &preferences::editor::tod_r
, &preferences::editor::set_tod_r))
, custom_tod_green_field_(register_integer("custom_tod_green"
, false
, &preferences::editor::tod_g
, &preferences::editor::set_tod_g))
, custom_tod_blue_field_(register_integer("custom_tod_blue"
, false
, &preferences::editor::tod_b
, &preferences::editor::set_tod_b))
, display_(display)
{
register_bool("use_mdi"
, false
, &preferences::editor::use_mdi
, &preferences::editor::set_use_mdi);
}
void teditor_settings::do_next_tod(twindow& window)
@ -109,34 +119,17 @@ const time_of_day& teditor_settings::get_selected_tod() const
return tods_[current_tod_];
}
int teditor_settings::get_red() const
{
return custom_tod_red_field_->get_cache_value();
}
int teditor_settings::get_green() const
{
return custom_tod_green_field_->get_cache_value();
}
int teditor_settings::get_blue() const
{
return custom_tod_blue_field_->get_cache_value();
}
void teditor_settings::set_use_mdi(bool value)
{
use_mdi_field_->set_cache_value(value);
}
bool teditor_settings::get_use_mdi() const
{
return use_mdi_field_->get_cache_value();
}
void teditor_settings::update_tod_display(twindow& window)
{
redraw_callback_(custom_tod_red_->get_value(),
custom_tod_green_->get_value(),
custom_tod_blue_->get_value());
image::set_color_adjustment(
custom_tod_red_field_->get_widget_value(window)
, custom_tod_green_field_->get_widget_value(window)
, custom_tod_blue_field_->get_widget_value(window));
if(display_) {
display_->redraw_everything();
}
window.set_dirty(true);
}
@ -147,23 +140,6 @@ void teditor_settings::slider_update_callback(twindow& window)
}
}
void teditor_settings::set_current_adjustment(int r, int g, int b)
{
for (size_t i = 0; i < tods_.size(); ++i) {
time_of_day& tod = tods_[i];
if (tod.red == r && tod.green == g && tod.blue == b) {
current_tod_ = i;
custom_tod_toggle_field_->set_cache_value(false);
return;
}
}
/* custom tod */
custom_tod_red_field_->set_cache_value(r);
custom_tod_green_field_->set_cache_value(g);
custom_tod_blue_field_->set_cache_value(b);
custom_tod_toggle_field_->set_cache_value(true);
}
void teditor_settings::update_selected_tod_info(twindow& window)
{
bool custom = custom_tod_toggle_->get_value();
@ -182,16 +158,19 @@ void teditor_settings::update_selected_tod_info(twindow& window)
* image widget.
*/
//current_tod_image_->set_icon_name(get_selected_tod().image);
custom_tod_red_->set_value(get_selected_tod().red);
custom_tod_green_->set_value(get_selected_tod().green);
custom_tod_blue_->set_value(get_selected_tod().blue);
custom_tod_red_field_->set_cache_value(get_selected_tod().red);
custom_tod_green_field_->set_cache_value(get_selected_tod().green);
custom_tod_blue_field_->set_cache_value(get_selected_tod().blue);
custom_tod_red_field_->set_widget_value(
window
, get_selected_tod().red);
custom_tod_green_field_->set_widget_value(
window
, get_selected_tod().green);
custom_tod_blue_field_->set_widget_value(
window
, get_selected_tod().blue);
}
custom_tod_red_->set_active(custom);
custom_tod_green_->set_active(custom);
custom_tod_blue_->set_active(custom);
custom_tod_red_field_->widget()->set_active(custom);
custom_tod_green_field_->widget()->set_active(custom);
custom_tod_blue_field_->widget()->set_active(custom);
current_tod_label_->set_active(!custom);
update_tod_display(window);
window.invalidate_layout();
@ -208,12 +187,6 @@ void teditor_settings::pre_show(CVideo& /*video*/, twindow& window)
&window, "custom_tod_toggle", false, true);
custom_tod_auto_refresh_ = find_widget<ttoggle_button>(
&window, "custom_tod_auto_refresh", false, true);
custom_tod_red_ = find_widget<tslider>(
&window, "custom_tod_red", false, true);
custom_tod_green_ = find_widget<tslider>(
&window, "custom_tod_green", false, true);
custom_tod_blue_ = find_widget<tslider>(
&window, "custom_tod_blue", false, true);
tbutton& next_tod_button = find_widget<tbutton>(
&window, "next_tod", false);
@ -234,24 +207,40 @@ void teditor_settings::pre_show(CVideo& /*video*/, twindow& window)
dialog_callback<teditor_settings
, &teditor_settings::update_selected_tod_info>);
connect_signal_notify_modified(*custom_tod_red_
connect_signal_notify_modified(*(custom_tod_red_field_->widget())
, boost::bind(
&teditor_settings::slider_update_callback
, this
, boost::ref(window)));
connect_signal_notify_modified(*custom_tod_green_
connect_signal_notify_modified(*(custom_tod_green_field_->widget())
, boost::bind(
&teditor_settings::slider_update_callback
, this
, boost::ref(window)));
connect_signal_notify_modified(*custom_tod_blue_
connect_signal_notify_modified(*(custom_tod_blue_field_->widget())
, boost::bind(
&teditor_settings::slider_update_callback
, this
, boost::ref(window)));
for (size_t i = 0; i < tods_.size(); ++i) {
time_of_day& tod = tods_[i];
const int r = custom_tod_red_field_->get_widget_value(window);
const int g = custom_tod_green_field_->get_widget_value(window);
const int b = custom_tod_blue_field_->get_widget_value(window);
if (tod.red == r && tod.green == g && tod.blue == b) {
current_tod_ = i;
custom_tod_toggle_->set_value(false);
return;
}
}
/* custom tod */
custom_tod_toggle_->set_value(true);
update_selected_tod_info(window);
}

View file

@ -17,24 +17,35 @@
#define GUI_DIALOGS_EDITOR_SETTINGS_HPP_INCLUDED
#include "time_of_day.hpp"
#include "gui/auxiliary/notifiee.hpp"
#include "gui/dialogs/dialog.hpp"
#include <vector>
#include <boost/function.hpp>
namespace editor {
class editor_display;
} // namespace editor
namespace gui2 {
class tlabel;
class ttoggle_button;
class tslider;
class teditor_settings : public tdialog
{
public:
teditor_settings();
teditor_settings(editor::editor_display* display
, const std::vector<time_of_day>& tods);
void set_redraw_callback(boost::function<void (int, int, int)> callback) { redraw_callback_ = callback; }
static bool execute(editor::editor_display* display
, const std::vector<time_of_day>& tods
, CVideo& video)
{
return teditor_settings(display, tods).show(video);
}
private:
/** Callback for the next tod button */
void do_next_tod(twindow& window);
@ -43,23 +54,11 @@ public:
void slider_update_callback(twindow& window);
void set_tods(const std::vector<time_of_day>& tods) { tods_ = tods; }
const std::vector<time_of_day>& get_tods() const { return tods_; }
void set_current_adjustment(int r, int g, int b);
void set_selected_tod(time_of_day tod);
const time_of_day& get_selected_tod() const;
int get_red() const;
int get_green() const;
int get_blue() const;
void update_selected_tod_info(twindow& window);
bool get_use_mdi() const;
void set_use_mdi(bool value);
private:
/** Inherited from tdialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const;
@ -67,8 +66,6 @@ private:
/** Inherited from tdialog. */
void pre_show(CVideo& video, twindow& window);
boost::function<void (int, int, int)> redraw_callback_;
/** Available time_of_days */
std::vector<time_of_day> tods_;
@ -86,15 +83,17 @@ private:
tfield_bool* custom_tod_toggle_field_;
tslider* custom_tod_red_;
tslider* custom_tod_green_;
tslider* custom_tod_blue_;
tfield_integer* custom_tod_red_field_;
tfield_integer* custom_tod_green_field_;
tfield_integer* custom_tod_blue_field_;
tfield_bool* use_mdi_field_;
/**
* The display to update when the ToD changes.
*
* The pointer may be NULL, in the unit tests, but normally it should be a
* pointer to a valid object.
*/
editor::editor_display* display_;
};
} // namespace gui2

View file

@ -665,13 +665,8 @@ struct twrapper<gui2::teditor_resize_map>
template<>
struct twrapper<gui2::teditor_settings>
{
static void dummy_callback(int, int, int) {}
static gui2::teditor_settings* create()
{
gui2::teditor_settings* result = new gui2::teditor_settings();
BOOST_REQUIRE_MESSAGE(result, "Failed to create a dialog.");
const config &cfg = main_config.child("editor_times");
BOOST_REQUIRE_MESSAGE(cfg, "No editor time-of-day defined");
@ -679,11 +674,7 @@ struct twrapper<gui2::teditor_settings>
foreach (const config &i, cfg.child_range("time")) {
tods.push_back(time_of_day(i));
}
result->set_tods(tods);
result->set_redraw_callback(boost::bind(dummy_callback, _1, _2, _3));
return result;
return new gui2::teditor_settings(NULL, tods);
}
};