Added the ability to change hotkeys from within the map editor.

This commit is contained in:
Kristoffer Erlandsson 2004-05-10 22:05:25 +00:00
parent ac23eca720
commit 7c1dc4a536
6 changed files with 50 additions and 17 deletions

View file

@ -48,6 +48,9 @@ namespace {
const unsigned int undo_limit = 100;
// Milliseconds to sleep in every iteration of the main loop.
const unsigned int sdl_delay = 20;
const std::string prefs_filename = get_dir(get_user_data_dir() + "/editor")
+ "/preferences";
void terrain_changed(gamemap &map, const gamemap::location &hex) {
// If we painted something else than a keep on a starting position,
@ -77,7 +80,7 @@ map_editor::map_editor(display &gui, gamemap &map, config &theme, config &game_c
: gui_(gui), map_(map), abort_(DONT_ABORT),
num_operations_since_save_(0), theme_(theme), game_config_(game_config),
map_dirty_(false), palette_(gui, size_specs_, map), brush_(gui, size_specs_),
l_button_func_(NONE) {
l_button_func_(NONE), prefs_disp_manager_(&gui_) {
// Set size specs.
adjust_sizes(gui_, size_specs_);
@ -85,10 +88,19 @@ map_editor::map_editor(display &gui, gamemap &map, config &theme, config &game_c
brush_.adjust_size();
// Clear the current hotkeys. Alot of hotkeys are already set
// through other configuration files (e.g. english.cfg) and we need
// to clear these or they will overlap.
// through other configuration files (e.g. english.cfg and
// preferences) and we need to clear these or they will overlap.
hotkey::get_hotkeys().clear();
hotkey::add_hotkeys(theme_, true);
try {
prefs_.read(read_file(prefs_filename));
}
catch (config::error e) {
std::cerr << "Error when reading " << prefs_filename << ": "
<< e.message << std::endl;
}
hotkey::add_hotkeys(prefs_, true);
recalculate_starting_pos_labels();
gui_.begin_game();
@ -97,6 +109,16 @@ map_editor::map_editor(display &gui, gamemap &map, config &theme, config &game_c
events::raise_draw_event();
}
map_editor::~map_editor() {
try {
write_file(prefs_filename, prefs_.write());
}
catch (io_exception e) {
std::cerr << "Error when writing to " << prefs_filename << ": "
<< e.what() << std::endl;
}
}
void map_editor::handle_event(const SDL_Event &event) {
int mousex, mousey;
SDL_GetMouseState(&mousex,&mousey);
@ -467,7 +489,7 @@ void map_editor::redo() {
}
void map_editor::preferences() {
preferences_dialog(gui_);
preferences_dialog(gui_, prefs_);
// Sizes and stuff may have changed, we need to redraw and
// recalculate everything if that is the case.
adjust_sizes(gui_, size_specs_);
@ -858,7 +880,6 @@ void map_editor::update_mouse_over_hexes(const gamemap::location mouse_over_hex)
void map_editor::main_loop() {
unsigned int last_brush_size = brush_.selected_brush_size();
const preferences::display_manager prefs_disp_manager(&gui_);
while (abort_ == DONT_ABORT) {
int mousex, mousey;
const int scroll_speed = preferences::scroll_speed();

View file

@ -18,6 +18,7 @@
#include "../display.hpp"
#include "../events.hpp"
#include "../hotkeys.hpp"
#include "../preferences.hpp"
#include <map>
#include <queue>
@ -82,6 +83,7 @@ class map_editor : public events::handler,
public hotkey::command_executor {
public:
map_editor(display &gui, gamemap &map, config &theme, config &game_config);
virtual ~map_editor();
/// Enter the main loop. The loop runs until set_abort() is called
/// to set an abort mode which makes the loop exit.
@ -298,6 +300,8 @@ private:
// mouse_moved_ will be true if the mouse have moved between two
// cycles.
bool mouse_moved_;
const preferences::display_manager prefs_disp_manager_;
config prefs_;
};

View file

@ -254,7 +254,7 @@ std::string load_map_dialog(display &disp) {
return filename;
}
void preferences_dialog(display &disp) {
void preferences_dialog(display &disp, config &prefs) {
const events::resize_lock prevent_resizing;
const events::event_context dialog_events_context;
const gui::dialog_manager dialog_mgr;
@ -317,8 +317,9 @@ void preferences_dialog(display &disp) {
gui::button resolution_button(disp,string_table["video_mode"]);
resolution_button.set_location(slider_left,scroll_pos + 80 + 50);
//gui::button hotkeys_button (disp,string_table["hotkeys_button"]);
//hotkeys_button.set_location(slider_left + fullscreen_button.width() + 100,scroll_pos + 80 + 100);
gui::button hotkeys_button (disp,string_table["hotkeys_button"]);
hotkeys_button.set_location(slider_left + fullscreen_button.width() + 100,
scroll_pos + 80 + 50);
bool redraw_all = true;
@ -347,7 +348,7 @@ void preferences_dialog(display &disp) {
close_button.set_dirty();
resolution_button.set_dirty();
grid_button.set_dirty();
//hotkeys_button.set_dirty();
hotkeys_button.set_dirty();
scroll_slider.set_dirty();
font::draw_text(&disp,clip_rect,14,font::NORMAL_COLOUR,scroll_label,
@ -372,10 +373,10 @@ void preferences_dialog(display &disp) {
break;
}
//if(hotkeys_button.process(mousex,mousey,left_button)) {
// show_hotkeys_dialog (disp);
// break;
//}
if(hotkeys_button.process(mousex,mousey,left_button)) {
preferences::show_hotkeys_dialog(disp, &prefs);
break;
}
events::pump();
events::raise_process_event();

View file

@ -42,7 +42,7 @@ std::string load_map_dialog(display &disp);
/// Show a dialog where the user may set the preferences used in the
/// editor.
void preferences_dialog(display &disp);
void preferences_dialog(display &disp, config &prefs);

View file

@ -740,7 +740,7 @@ bool show_video_mode_dialog(display& disp)
}
}
void show_hotkeys_dialog (display & disp)
void show_hotkeys_dialog (display & disp, config *save_config)
{
log_scope ("show_hotkeys_dialog");
@ -868,7 +868,12 @@ void show_hotkeys_dialog (display & disp)
}
if (save_button.process (mousex, mousey, left_button))
{
hotkey::save_hotkeys(prefs);
if (save_config == NULL) {
hotkey::save_hotkeys(prefs);
}
else {
hotkey::save_hotkeys(*save_config);
}
redraw_all = true;
}

View file

@ -101,7 +101,9 @@ namespace preferences {
void show_preferences_dialog(display& disp);
bool show_video_mode_dialog(display& disp);
void show_hotkeys_dialog (display & disp);
// If prefs is non-null, save the hotkeys in that config instead of
// the default.
void show_hotkeys_dialog (display & disp, config *prefs=NULL);
// Ask for end turn confirmation
bool yellow_confirm();