hotkeys.hpp

Go to the documentation of this file.
00001 /* $Id: hotkeys.hpp 53694 2012-03-30 12:35:40Z fendrin $ */
00002 /*
00003    Copyright (C) 2003 - 2012 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 as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 #ifndef HOTKEYS_HPP_INCLUDED
00016 #define HOTKEYS_HPP_INCLUDED
00017 
00018 #include "events.hpp"
00019 #include "tstring.hpp"
00020 
00021 class config;
00022 class display;
00023 
00024 //the hotkey system allows hotkey definitions to be loaded from
00025 //configuration objects, and then detect if a keyboard event
00026 //refers to a hotkey command being executed.
00027 namespace hotkey {
00028 
00029 /** Available hotkey scopes. The scope is used to allow command from
00030  * non-overlapping areas of the game share the same key
00031  */
00032 enum scope {
00033     SCOPE_GENERAL,
00034     SCOPE_GAME,
00035     SCOPE_EDITOR,
00036     SCOPE_COUNT
00037 };
00038 
00039 
00040 enum HOTKEY_COMMAND {
00041     HOTKEY_CYCLE_UNITS,HOTKEY_CYCLE_BACK_UNITS,
00042     HOTKEY_UNIT_HOLD_POSITION,
00043     HOTKEY_END_UNIT_TURN, HOTKEY_LEADER,
00044     HOTKEY_UNDO, HOTKEY_REDO,
00045     HOTKEY_ZOOM_IN, HOTKEY_ZOOM_OUT, HOTKEY_ZOOM_DEFAULT,
00046     HOTKEY_FULLSCREEN, HOTKEY_SCREENSHOT, HOTKEY_MAP_SCREENSHOT, HOTKEY_ACCELERATED,
00047     HOTKEY_UNIT_DESCRIPTION, HOTKEY_RENAME_UNIT,
00048     HOTKEY_SAVE_GAME, HOTKEY_SAVE_REPLAY, HOTKEY_SAVE_MAP, HOTKEY_LOAD_GAME,
00049     HOTKEY_RECRUIT, HOTKEY_REPEAT_RECRUIT, HOTKEY_RECALL, HOTKEY_ENDTURN,
00050     HOTKEY_TOGGLE_ELLIPSES, HOTKEY_TOGGLE_GRID, HOTKEY_STATUS_TABLE, HOTKEY_MUTE, HOTKEY_MOUSE_SCROLL,
00051     HOTKEY_SPEAK, HOTKEY_CREATE_UNIT, HOTKEY_CHANGE_SIDE, HOTKEY_PREFERENCES,
00052     HOTKEY_OBJECTIVES, HOTKEY_UNIT_LIST, HOTKEY_STATISTICS, HOTKEY_STOP_NETWORK, HOTKEY_START_NETWORK, HOTKEY_QUIT_GAME,
00053     HOTKEY_LABEL_TEAM_TERRAIN, HOTKEY_LABEL_TERRAIN, HOTKEY_CLEAR_LABELS,HOTKEY_SHOW_ENEMY_MOVES, HOTKEY_BEST_ENEMY_MOVES,
00054     HOTKEY_DELAY_SHROUD, HOTKEY_UPDATE_SHROUD, HOTKEY_CONTINUE_MOVE,
00055     HOTKEY_SEARCH, HOTKEY_SPEAK_ALLY, HOTKEY_SPEAK_ALL, HOTKEY_HELP,
00056     HOTKEY_CHAT_LOG, HOTKEY_LANGUAGE,
00057     HOTKEY_PLAY_REPLAY, HOTKEY_RESET_REPLAY, HOTKEY_STOP_REPLAY, HOTKEY_REPLAY_NEXT_TURN,
00058     HOTKEY_REPLAY_NEXT_SIDE, HOTKEY_REPLAY_SHOW_EVERYTHING,
00059     HOTKEY_REPLAY_SHOW_EACH, HOTKEY_REPLAY_SHOW_TEAM1,
00060     HOTKEY_REPLAY_SKIP_ANIMATION,
00061     HOTKEY_ANIMATE_MAP,
00062     HOTKEY_LEFT_MOUSE_CLICK, HOTKEY_RIGHT_MOUSE_CLICK,
00063     HOTKEY_CANCEL, HOTKEY_OKAY,
00064 
00065     // Whiteboard commands
00066     HOTKEY_WB_TOGGLE,
00067     HOTKEY_WB_EXECUTE_ACTION,
00068     HOTKEY_WB_EXECUTE_ALL_ACTIONS,
00069     HOTKEY_WB_DELETE_ACTION,
00070     HOTKEY_WB_BUMP_UP_ACTION,
00071     HOTKEY_WB_BUMP_DOWN_ACTION,
00072     HOTKEY_WB_SUPPOSE_DEAD,
00073 
00074     HOTKEY_EDITOR_QUIT_TO_DESKTOP,
00075     HOTKEY_EDITOR_CLOSE_MAP, HOTKEY_EDITOR_SWITCH_MAP,
00076     HOTKEY_EDITOR_SETTINGS,
00077     HOTKEY_EDITOR_PARTIAL_UNDO,
00078     HOTKEY_EDITOR_MAP_NEW, HOTKEY_EDITOR_MAP_LOAD, HOTKEY_EDITOR_MAP_SAVE,
00079     HOTKEY_EDITOR_MAP_SAVE_AS, HOTKEY_EDITOR_MAP_SAVE_ALL,
00080     HOTKEY_EDITOR_MAP_REVERT, HOTKEY_EDITOR_MAP_INFO,
00081     HOTKEY_EDITOR_PALETTE_ITEM_SWAP,
00082     HOTKEY_EDITOR_PALETTE_GROUPS, HOTKEY_EDITOR_PALETTE_UPSCROLL, HOTKEY_EDITOR_PALETTE_DOWNSCROLL,
00083     HOTKEY_EDITOR_TOOL_NEXT,
00084     HOTKEY_EDITOR_TOOL_PAINT, HOTKEY_EDITOR_TOOL_FILL,
00085     HOTKEY_EDITOR_TOOL_SELECT, HOTKEY_EDITOR_TOOL_STARTING_POSITION, HOTKEY_EDITOR_TOOL_LABEL,
00086     HOTKEY_EDITOR_TOOL_UNIT,
00087     HOTKEY_EDITOR_BRUSH_NEXT, HOTKEY_EDITOR_BRUSH_DEFAULT,
00088     HOTKEY_EDITOR_CUT, HOTKEY_EDITOR_COPY, HOTKEY_EDITOR_PASTE,
00089     HOTKEY_EDITOR_EXPORT_SELECTION_COORDS,
00090     HOTKEY_EDITOR_SELECT_ALL, HOTKEY_EDITOR_SELECT_INVERSE,
00091     HOTKEY_EDITOR_SELECT_NONE,
00092     HOTKEY_EDITOR_CLIPBOARD_ROTATE_CW, HOTKEY_EDITOR_CLIPBOARD_ROTATE_CCW,
00093     HOTKEY_EDITOR_CLIPBOARD_FLIP_HORIZONTAL, HOTKEY_EDITOR_CLIPBOARD_FLIP_VERTICAL,
00094     HOTKEY_EDITOR_SELECTION_ROTATE, HOTKEY_EDITOR_SELECTION_FLIP,
00095     HOTKEY_EDITOR_SELECTION_FILL,
00096     HOTKEY_EDITOR_SELECTION_GENERATE, HOTKEY_EDITOR_SELECTION_RANDOMIZE,
00097     HOTKEY_EDITOR_MAP_RESIZE, HOTKEY_EDITOR_MAP_ROTATE,
00098     HOTKEY_EDITOR_MAP_GENERATE, HOTKEY_EDITOR_MAP_APPLY_MASK,
00099     HOTKEY_EDITOR_MAP_CREATE_MASK_TO,
00100     HOTKEY_EDITOR_REFRESH, HOTKEY_EDITOR_UPDATE_TRANSITIONS,
00101     HOTKEY_EDITOR_AUTO_UPDATE_TRANSITIONS,
00102     HOTKEY_EDITOR_REFRESH_IMAGE_CACHE,
00103     HOTKEY_EDITOR_DRAW_COORDINATES, HOTKEY_EDITOR_DRAW_TERRAIN_CODES,
00104 
00105     //misc.
00106     HOTKEY_USER_CMD,
00107     HOTKEY_CUSTOM_CMD,
00108     HOTKEY_AI_FORMULA,
00109     HOTKEY_CLEAR_MSG,
00110     /* Gui2 specific hotkeys. */
00111     TITLE_SCREEN__RELOAD_WML,
00112     TITLE_SCREEN__NEXT_TIP,
00113     TITLE_SCREEN__PREVIOUS_TIP,
00114     TITLE_SCREEN__TUTORIAL,
00115     TITLE_SCREEN__CAMPAIGN,
00116     TITLE_SCREEN__MULTIPLAYER,
00117     TITLE_SCREEN__ADDONS,
00118     TITLE_SCREEN__EDITOR,
00119     TITLE_SCREEN__CREDITS,
00120     GLOBAL__HELPTIP,
00121     HOTKEY_NULL
00122 };
00123 
00124 void deactivate_all_scopes();
00125 void set_scope_active(scope s, bool set = true);
00126 bool is_scope_active(scope s);
00127 
00128 class hotkey_item {
00129 public:
00130 
00131     hotkey_item() :
00132         id_(HOTKEY_NULL),
00133         command_(),
00134         description_(),
00135         scope_(SCOPE_GENERAL),
00136         type_(UNBOUND),
00137         character_(0),
00138         ctrl_(false),
00139         alt_(false),
00140         cmd_(false),
00141         shift_(false),
00142         keycode_(0),
00143         button_(0),
00144         joystick_(0),
00145         hat_(0),
00146         value_(0),
00147         hidden_(false)
00148         {}
00149 
00150     hotkey_item(HOTKEY_COMMAND id, const std::string& command,
00151         const t_string &description, bool hidden = false,
00152         scope s=SCOPE_GENERAL);
00153 
00154     HOTKEY_COMMAND get_id() const { return id_; };
00155     const std::string& get_command() const { return command_; };
00156     const t_string &get_description() const { return description_; };
00157 
00158     void load_from_config(const config& cfg);
00159 
00160     void set_description(const t_string &description);
00161     void clear_hotkey();
00162 
00163     void set_button(int button, int joystick);
00164     void set_hat(int joystick, int hat, int value);
00165 
00166     void set_key(int character, int keycode, bool shift, bool ctrl, bool alt, bool cmd);
00167 
00168     enum type {
00169         UNBOUND,
00170         BY_KEYCODE,
00171         BY_CHARACTER,
00172         CLEARED,
00173         BUTTON,
00174         HAT
00175     };
00176 
00177     enum type get_type() const { return type_; }
00178 
00179 
00180     /** @return the scope of this hotkey */
00181     scope get_scope() const { return scope_; }
00182 
00183     bool is_in_active_scope() const { return is_scope_active(get_scope()); }
00184 
00185     // Returns unicode value of keypress.
00186     int get_character() const { return character_; }
00187     int get_button() const { return button_; }
00188     int get_joystick() const { return joystick_; }
00189     int get_hat() const { return hat_; }
00190     int get_value() const { return value_; }
00191     bool get_alt() const { return alt_; }
00192     bool get_cmd() const { return cmd_; }
00193     bool get_ctrl() const { return ctrl_; }
00194 
00195     // Return the actual key code.
00196     int get_keycode() const { return keycode_; }
00197     bool get_shift() const { return shift_; }
00198 
00199     // Return "name" of hotkey for example :"ctrl+alt+g"
00200     std::string get_name() const;
00201 
00202     bool null() const { return id_ == HOTKEY_NULL; };
00203     bool hidden() const { return hidden_; };
00204 private:
00205     HOTKEY_COMMAND id_;
00206     std::string command_;
00207     t_string description_;
00208     scope scope_;
00209 
00210     // UNBOUND means unset, CHARACTER means see character_, KEY means keycode_.
00211     // BUTTON means gamepad/joystick button_.
00212     enum type type_;
00213 
00214     // Actual unicode character
00215     int character_;
00216     bool ctrl_;
00217     bool alt_;
00218     bool cmd_;
00219 
00220     // These used for function keys (which don't have a unicode value) or
00221     // space (which doesn't have a distinct unicode value when shifted).
00222     bool shift_;
00223     int keycode_;
00224 
00225     // In case type=BUTTON
00226     int button_;
00227     int joystick_;
00228     int hat_;
00229     int value_;
00230 
00231     bool hidden_;
00232 
00233 };
00234 
00235 
00236 
00237 class manager {
00238 public:
00239     manager();
00240     static void init();
00241     static void wipe();
00242     ~manager();
00243 };
00244 
00245 class scope_changer {
00246 public:
00247     scope_changer(const config& cfg, const std::string& hotkey_tag);
00248     ~scope_changer();
00249 private:
00250     const config& cfg_;
00251     std::string prev_tag_name_;
00252     std::vector<bool> prev_scope_active_;
00253 };
00254 
00255 void load_descriptions();
00256 
00257 void set_hotkey_tag_name(const std::string& name);
00258 void load_hotkeys(const config& cfg, bool set_as_default = false);
00259 void reset_default_hotkeys();
00260 void save_hotkeys(config& cfg);
00261 
00262 hotkey_item& get_hotkey(HOTKEY_COMMAND id);
00263 hotkey_item& get_hotkey(const std::string& command);
00264 
00265 hotkey_item& get_hotkey(int joy_num, int button_num);
00266 hotkey_item& get_hotkey(int joy_num, int hat_num, int hat_value);
00267 hotkey_item& get_hotkey(int character, int keycode, bool shift,
00268         bool ctrl, bool alt, bool cmd);
00269 hotkey_item& get_hotkey(const SDL_JoyButtonEvent& event);
00270 hotkey_item& get_hotkey(const SDL_JoyHatEvent& event);
00271 hotkey_item& get_hotkey(const SDL_KeyboardEvent& event);
00272 
00273 HOTKEY_COMMAND get_hotkey_command(const std::string& command);
00274 
00275 hotkey_item& get_visible_hotkey(int index);
00276 
00277 std::vector<hotkey_item>& get_hotkeys();
00278 
00279 enum ACTION_STATE { ACTION_STATELESS, ACTION_ON, ACTION_OFF };
00280 
00281 //abstract base class for objects that implement the ability
00282 //to execute hotkey commands.
00283 class command_executor
00284 {
00285 protected:
00286     virtual ~command_executor() {}
00287 public:
00288     virtual void cycle_units() {}
00289     virtual void cycle_back_units() {}
00290     virtual void end_turn() {}
00291     virtual void goto_leader() {}
00292     virtual void unit_hold_position() {}
00293     virtual void end_unit_turn() {}
00294     virtual void undo() {}
00295     virtual void redo() {}
00296     virtual void unit_description() {}
00297     virtual void rename_unit() {}
00298     virtual void save_game() {}
00299     virtual void save_replay() {}
00300     virtual void save_map() {}
00301     virtual void load_game() {}
00302     virtual void toggle_ellipses() {}
00303     virtual void toggle_grid() {}
00304     virtual void status_table() {}
00305     virtual void recall() {}
00306     virtual void recruit() {}
00307     virtual void repeat_recruit() {}
00308     virtual void speak() {}
00309     virtual void whisper() {}
00310     virtual void shout() {}
00311     virtual void create_unit() {}
00312     virtual void change_side() {}
00313     virtual void preferences() {}
00314     virtual void objectives() {}
00315     virtual void unit_list() {}
00316     virtual void show_statistics() {}
00317     virtual void stop_network() {}
00318     virtual void start_network() {}
00319     virtual void label_terrain(bool /*team_only*/) {}
00320     virtual void clear_labels() {}
00321     virtual void show_enemy_moves(bool /*ignore_units*/) {}
00322     virtual void toggle_shroud_updates() {}
00323     virtual void update_shroud_now() {}
00324     virtual void continue_move() {}
00325     virtual void search() {}
00326     virtual void show_help() {}
00327     virtual void show_chat_log() {}
00328     virtual void user_command() {}
00329     virtual void custom_command() {}
00330     virtual void ai_formula() {}
00331     virtual void clear_messages() {}
00332     virtual void change_language() {}
00333     virtual void play_replay() {}
00334     virtual void reset_replay() {}
00335     virtual void stop_replay() {}
00336     virtual void replay_next_turn() {}
00337     virtual void replay_next_side() {}
00338     virtual void replay_show_everything() {}
00339     virtual void replay_show_each() {}
00340     virtual void replay_show_team1() {}
00341     virtual void replay_skip_animation() {}
00342     virtual void whiteboard_toggle() {}
00343     virtual void whiteboard_execute_action() {}
00344     virtual void whiteboard_execute_all_actions() {}
00345     virtual void whiteboard_delete_action() {}
00346     virtual void whiteboard_bump_up_action() {}
00347     virtual void whiteboard_bump_down_action() {}
00348     virtual void whiteboard_suppose_dead() {}
00349     virtual void left_mouse_click() {}
00350     virtual void right_mouse_click() {}
00351 
00352     //Gets the action's image (if any). Displayed left of the action text in menus.
00353     virtual std::string get_action_image(hotkey::HOTKEY_COMMAND /*command*/, int /*index*/) const { return ""; }
00354     //Does the action control a toggle switch? If so, return the state of the action (on or off)
00355     virtual ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND /*command*/, int /*index*/) const { return ACTION_STATELESS; }
00356     //Returns the appropriate menu image. Checkable items will get a checked/unchecked image.
00357     std::string get_menu_image(hotkey::HOTKEY_COMMAND command, int index=-1) const;
00358     //Returns a vector of images for a given menu
00359     std::vector<std::string> get_menu_images(display &, const std::vector<std::string>& items_arg);
00360 
00361     void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& gui);
00362 
00363     virtual bool can_execute_command(HOTKEY_COMMAND command, int index=-1) const = 0;
00364     virtual bool execute_command(HOTKEY_COMMAND command, int index=-1);
00365 };
00366 
00367 //function to be called every time a key event is intercepted. Will
00368 //call the relevant function in executor if the keyboard event is
00369 //not NULL. Also handles some events in the function itself, and so
00370 //is still meaningful to call with executor=NULL
00371 void key_event(display& disp, const SDL_KeyboardEvent& event, command_executor* executor);
00372 void button_event(display& disp, const SDL_JoyButtonEvent& event, command_executor* executor);
00373 void hat_event(display& disp, const SDL_JoyHatEvent& event, command_executor* executor);
00374 
00375 void execute_command(display& disp, HOTKEY_COMMAND command, command_executor* executor, int index=-1);
00376 
00377 //object which will ensure that basic keyboard events like escape
00378 //are handled properly for the duration of its lifetime
00379 struct basic_handler : public events::handler {
00380     basic_handler(display* disp, command_executor* exec=NULL);
00381 
00382     void handle_event(const SDL_Event& event);
00383 
00384 private:
00385     display* disp_;
00386     command_executor* exec_;
00387 };
00388 
00389 }
00390 
00391 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:01 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs