hotkeys.hpp

Go to the documentation of this file.
00001 /* $Id: hotkeys.hpp 26242 2008-04-30 03:52:10Z alink $ */
00002 /*
00003    Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 #ifndef HOTKEYS_HPP_INCLUDED
00015 #define HOTKEYS_HPP_INCLUDED
00016 
00017 #include "events.hpp"
00018 #include "SDL.h"
00019 
00020 #include <string>
00021 #include <vector>
00022 
00023 class config;
00024 class display;
00025 
00026 //the hotkey system allows hotkey definitions to be loaded from
00027 //configuration objects, and then detect if a keyboard event
00028 //refers to a hotkey command being executed.
00029 namespace hotkey {
00030 
00031 enum HOTKEY_COMMAND {
00032     HOTKEY_CYCLE_UNITS,HOTKEY_CYCLE_BACK_UNITS, HOTKEY_UNIT_HOLD_POSITION,
00033     HOTKEY_END_UNIT_TURN, HOTKEY_LEADER,
00034     HOTKEY_UNDO, HOTKEY_REDO,
00035     HOTKEY_ZOOM_IN, HOTKEY_ZOOM_OUT, HOTKEY_ZOOM_DEFAULT,
00036     HOTKEY_FULLSCREEN, HOTKEY_SCREENSHOT, HOTKEY_MAP_SCREENSHOT, HOTKEY_ACCELERATED,
00037     HOTKEY_UNIT_DESCRIPTION, HOTKEY_RENAME_UNIT,
00038     HOTKEY_SAVE_GAME, HOTKEY_SAVE_REPLAY, HOTKEY_SAVE_MAP, HOTKEY_LOAD_GAME,
00039     HOTKEY_RECRUIT, HOTKEY_REPEAT_RECRUIT, HOTKEY_RECALL, HOTKEY_ENDTURN,
00040     HOTKEY_TOGGLE_GRID, HOTKEY_STATUS_TABLE, HOTKEY_MUTE, HOTKEY_MOUSE_SCROLL,
00041     HOTKEY_SPEAK, HOTKEY_CREATE_UNIT, HOTKEY_CHANGE_UNIT_SIDE, HOTKEY_PREFERENCES,
00042     HOTKEY_OBJECTIVES, HOTKEY_UNIT_LIST, HOTKEY_STATISTICS, HOTKEY_QUIT_GAME,
00043     HOTKEY_LABEL_TEAM_TERRAIN, HOTKEY_LABEL_TERRAIN, HOTKEY_CLEAR_LABELS,HOTKEY_SHOW_ENEMY_MOVES, HOTKEY_BEST_ENEMY_MOVES,
00044     HOTKEY_DELAY_SHROUD, HOTKEY_UPDATE_SHROUD, HOTKEY_CONTINUE_MOVE,
00045     HOTKEY_SEARCH, HOTKEY_SPEAK_ALLY, HOTKEY_SPEAK_ALL, HOTKEY_HELP,
00046     HOTKEY_CHAT_LOG, HOTKEY_LANGUAGE,
00047     HOTKEY_PLAY_REPLAY, HOTKEY_RESET_REPLAY, HOTKEY_STOP_REPLAY, HOTKEY_REPLAY_NEXT_TURN,
00048     HOTKEY_REPLAY_NEXT_SIDE, HOTKEY_REPLAY_SHOW_EVERYTHING,
00049     HOTKEY_REPLAY_SHOW_EACH, HOTKEY_REPLAY_SHOW_TEAM1,
00050     HOTKEY_REPLAY_SKIP_ANIMATION,
00051 
00052     //editing specific commands
00053     HOTKEY_EDIT_SET_TERRAIN,
00054     HOTKEY_EDIT_QUIT, HOTKEY_EDIT_SAVE_MAP,
00055     HOTKEY_EDIT_SAVE_AS, HOTKEY_EDIT_SET_START_POS,
00056     HOTKEY_EDIT_NEW_MAP, HOTKEY_EDIT_LOAD_MAP, HOTKEY_EDIT_FLOOD_FILL,
00057     HOTKEY_EDIT_FILL_SELECTION, HOTKEY_EDIT_ROTATE_SELECTION, HOTKEY_EDIT_CUT, HOTKEY_EDIT_COPY,
00058     HOTKEY_EDIT_PASTE, HOTKEY_EDIT_REVERT, HOTKEY_EDIT_RESIZE,
00059     HOTKEY_EDIT_FLIP, HOTKEY_EDIT_SELECT_ALL, HOTKEY_EDIT_DRAW,
00060     HOTKEY_EDIT_REFRESH, HOTKEY_EDIT_UPDATE, HOTKEY_EDIT_AUTO_UPDATE,
00061 
00062     //misc.
00063     HOTKEY_USER_CMD,
00064     HOTKEY_CUSTOM_CMD,
00065     HOTKEY_AI_FORMULA,
00066     HOTKEY_CLEAR_MSG,
00067 #ifdef USRCMD2
00068     HOTKEY_USER_CMD_2,
00069     HOTKEY_USER_CMD_3,
00070 #endif
00071     HOTKEY_NULL
00072 };
00073 
00074 class hotkey_item {
00075 public:
00076     hotkey_item() : 
00077         id_(HOTKEY_NULL),
00078         command_(),
00079         description_(),
00080         type_(UNBOUND),
00081         character_(0),
00082         ctrl_(false),
00083         alt_(false),
00084         cmd_(false),
00085         keycode_(0),
00086         shift_(false),
00087         hidden_(false)
00088         {}
00089 
00090     hotkey_item(HOTKEY_COMMAND id, const std::string& command, const std::string& description, bool hidden=false);
00091 
00092     HOTKEY_COMMAND get_id() const { return id_; };
00093     const std::string& get_command() const { return command_; };
00094     const std::string& get_description() const { return description_; };
00095 
00096     void load_from_config(const config& cfg);
00097 
00098     void set_description(const std::string& description);
00099     void clear_hotkey();
00100     void set_key(int character, int keycode, bool shift, bool ctrl, bool alt, bool cmd);
00101 
00102     enum type {
00103         UNBOUND,
00104         BY_KEYCODE,
00105         BY_CHARACTER,
00106         CLEARED
00107     };
00108 
00109     enum type get_type() const { return type_; }
00110 
00111     // Returns unicode value of keypress.
00112     int get_character() const { return character_; }
00113     bool get_alt() const { return alt_; }
00114     bool get_cmd() const { return cmd_; }
00115     bool get_ctrl() const { return ctrl_; }
00116 
00117     // Return the actual key code.
00118     int get_keycode() const { return keycode_; }
00119     bool get_shift() const { return shift_; }
00120 
00121     // Return "name" of hotkey for example :"ctrl+alt+g"
00122     std::string get_name() const;
00123 
00124     bool null() const { return id_ == HOTKEY_NULL; };
00125     bool hidden() const { return hidden_; };
00126 private:
00127     HOTKEY_COMMAND id_;
00128     std::string command_;
00129     std::string description_;
00130 
00131     // UNBOUND means unset, CHARACTER means see character_, KEY means keycode_.
00132     enum type type_;
00133 
00134     // Actual unicode character
00135     int character_;
00136     bool ctrl_;
00137     bool alt_;
00138     bool cmd_;
00139 
00140     // These used for function keys (which don't have a unicode value) or
00141     // space (which doesn't have a distinct unicode value when shifted).
00142     int keycode_;
00143     bool shift_;
00144 
00145     bool hidden_;
00146 };
00147 
00148 class manager {
00149 public:
00150     manager();
00151     ~manager();
00152 };
00153 
00154 void load_descriptions();
00155 
00156 void load_hotkeys(const config& cfg);
00157 void save_hotkeys(config& cfg);
00158 
00159 hotkey_item& get_hotkey(HOTKEY_COMMAND id);
00160 hotkey_item& get_hotkey(const std::string& command);
00161 
00162 hotkey_item& get_hotkey(int character, int keycode, bool shift, bool ctrl, bool alt, bool cmd);
00163 hotkey_item& get_hotkey(const SDL_KeyboardEvent& event);
00164 
00165 hotkey_item& get_visible_hotkey(int index);
00166 
00167 std::vector<hotkey_item>& get_hotkeys();
00168 
00169 enum ACTION_STATE { ACTION_STATELESS, ACTION_ON, ACTION_OFF };
00170 
00171 //abstract base class for objects that implement the ability
00172 //to execute hotkey commands.
00173 class command_executor
00174 {
00175 protected:
00176     virtual ~command_executor() {}
00177 public:
00178     virtual void cycle_units() {}
00179     virtual void cycle_back_units() {}
00180     virtual void end_turn() {}
00181     virtual void goto_leader() {}
00182     virtual void unit_hold_position() {}
00183     virtual void end_unit_turn() {}
00184     virtual void undo() {}
00185     virtual void redo() {}
00186     virtual void unit_description() {}
00187     virtual void rename_unit() {}
00188     virtual void save_game() {}
00189     virtual void save_replay() {}
00190     virtual void save_map() {}
00191     virtual void load_game() {}
00192     virtual void toggle_grid() {}
00193     virtual void status_table() {}
00194     virtual void recall() {}
00195     virtual void recruit() {}
00196     virtual void repeat_recruit() {}
00197     virtual void speak() {}
00198     virtual void whisper() {}
00199     virtual void shout() {}
00200     virtual void create_unit() {}
00201     virtual void change_unit_side() {}
00202     virtual void preferences() {}
00203     virtual void objectives() {}
00204     virtual void unit_list() {}
00205     virtual void show_statistics() {}
00206     virtual void label_terrain(bool /*team_only*/) {}
00207     virtual void clear_labels() {}
00208     virtual void show_enemy_moves(bool /*ignore_units*/) {}
00209     virtual void toggle_shroud_updates() {}
00210     virtual void update_shroud_now() {}
00211     virtual void continue_move() {}
00212     virtual void search() {}
00213     virtual void show_help() {}
00214     virtual void show_chat_log() {}
00215     virtual void user_command() {}
00216     virtual void custom_command() {}
00217     virtual void ai_formula() {}
00218     virtual void clear_messages() {}
00219 #ifdef USRCMD2
00220     virtual void user_command_2() {}
00221     virtual void user_command_3() {}
00222 #endif
00223     virtual void change_language() {}
00224     virtual void play_replay() {}
00225     virtual void reset_replay() {}
00226     virtual void stop_replay() {}
00227     virtual void replay_next_turn() {}
00228     virtual void replay_next_side() {}
00229     virtual void replay_show_everything() {}
00230     virtual void replay_show_each() {}
00231     virtual void replay_show_team1() {}
00232     virtual void replay_skip_animation() {}
00233 
00234     // Map editor stuff.
00235     virtual void edit_set_terrain() {}
00236     virtual void edit_quit() {}
00237     virtual void edit_new_map() {}
00238     virtual void edit_load_map() {}
00239     virtual void edit_save_map() {}
00240     virtual void edit_save_as() {}
00241     virtual void edit_set_start_pos() {}
00242     virtual void edit_flood_fill() {}
00243     virtual void edit_fill_selection() {}
00244     virtual void edit_rotate_selection() {}
00245     virtual void edit_cut() {}
00246     virtual void edit_copy() {}
00247     virtual void edit_paste() {}
00248     virtual void edit_revert() {}
00249     virtual void edit_resize() {}
00250     virtual void edit_flip() {}
00251     virtual void edit_select_all() {}
00252     virtual void edit_draw() {}
00253     virtual void edit_refresh() {}
00254     virtual void edit_update() {}
00255     virtual void edit_auto_update() {}
00256 
00257     //Gets the action's image (if any). Displayed left of the action text in menus.
00258     virtual std::string get_action_image(hotkey::HOTKEY_COMMAND /*command*/, int /*index*/) const { return ""; }
00259     //Does the action control a toggle switch? If so, return the state of the action (on or off)
00260     virtual ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND /*command*/) const { return ACTION_STATELESS; }
00261     //Returns the appropriate menu image. Checkable items will get a checked/unchecked image.
00262     std::string get_menu_image(hotkey::HOTKEY_COMMAND command, int index=-1) const;
00263     //Returns a vector of images for a given menu
00264     std::vector<std::string> get_menu_images(const std::vector<std::string>& items_arg);
00265 
00266     void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& gui);
00267 
00268     virtual bool can_execute_command(HOTKEY_COMMAND command, int index=-1) const = 0;
00269     virtual bool execute_command(HOTKEY_COMMAND command, int index=-1);
00270 };
00271 
00272 //function to be called every time a key event is intercepted. Will
00273 //call the relevant function in executor if the keyboard event is
00274 //not NULL. Also handles some events in the function itself, and so
00275 //is still meaningful to call with executor=NULL
00276 void key_event(display& disp, const SDL_KeyboardEvent& event, command_executor* executor);
00277 
00278 void execute_command(display& disp, HOTKEY_COMMAND command, command_executor* executor, int index=-1);
00279 
00280 //object which will ensure that basic keyboard events like escape
00281 //are handled properly for the duration of its lifetime
00282 struct basic_handler : public events::handler {
00283     basic_handler(display* disp, command_executor* exec=NULL);
00284 
00285     void handle_event(const SDL_Event& event);
00286 
00287 private:
00288     display* disp_;
00289     command_executor* exec_;
00290 };
00291 
00292 }
00293 
00294 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs