The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hotkey_preferences_display.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Fabian Mueller <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 /**
18  * @file
19  * Manages the hotkey bindings.
20  */
21 
22 #include "hotkey/hotkey_item.hpp"
23 
24 #include "construct_dialog.hpp"
25 #include "gettext.hpp"
26 #include "marked-up_text.hpp"
27 #include "sdl/rect.hpp"
28 
29 namespace hotkey {
30 
32 {
33  // Lets bind a hotkey...
34 
35  const std::string text = _("Press desired hotkey (Esc cancels)");
36 
37  SDL_Rect clip_rect = sdl::create_rect(0, 0, video.getx(), video.gety());
38  SDL_Rect text_size = font::draw_text(nullptr, clip_rect, font::SIZE_LARGE,
39  font::NORMAL_COLOR, text, 0, 0);
40 
41  const int centerx = video.getx() / 2;
42  const int centery = video.gety() / 2;
43 
44  SDL_Rect dlgr = sdl::create_rect(centerx - text_size.w / 2 - 30,
45  centery - text_size.h / 2 - 16, text_size.w + 60,
46  text_size.h + 32);
47 
48  surface_restorer restorer(&video, dlgr);
49  gui::dialog_frame mini_frame(video);
50  mini_frame.layout(centerx - text_size.w / 2 - 20,
51  centery - text_size.h / 2 - 6, text_size.w + 40,
52  text_size.h + 12);
53  mini_frame.draw_background();
54  mini_frame.draw_border();
55  font::draw_text(&video, clip_rect, font::SIZE_LARGE,
56  font::NORMAL_COLOR, text,
57  centerx - text_size.w / 2, centery - text_size.h / 2);
58  video.flip();
59  SDL_Event event;
60  event.type = 0;
61  int keycode = -1;
62 
63  do {
64  switch (event.type) {
65 
66  case SDL_KEYDOWN:
67  keycode = event.key.keysym.sym;
68  break;
69  }
70 
71  SDL_PollEvent(&event);
73 
74  } while (event.type != SDL_KEYUP && event.type != SDL_JOYBUTTONUP
75  && event.type != SDL_JOYHATMOTION
76  && event.type != SDL_MOUSEBUTTONUP);
77 
78  restorer.restore();
79  return keycode == SDLK_ESCAPE ? hotkey::hotkey_ptr() : hotkey::create_hotkey(id, event);
80 }
81 
82 } //end namespace hotkey
Definition: video.hpp:58
void flip()
Definition: video.cpp:496
hotkey_ptr show_binding_dialog(CVideo &video, const std::string &id)
const SDL_Color NORMAL_COLOR
Definition: font.cpp:564
Keyboard shortcuts for game actions.
dimension_measurements layout(int x, int y, int w, int h)
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
void restore() const
Definition: utils.cpp:2496
int gety() const
Definition: video.cpp:481
hotkey_ptr create_hotkey(const std::string &id, SDL_Event &event)
Create a new hotkey item for a command from an SDL_Event.
void peek_for_resize()
Definition: events.cpp:662
SDL_Rect draw_text(surface &dst, const SDL_Rect &area, int size, const SDL_Color &color, const std::string &txt, int x, int y, bool use_tooltips, int style)
Function to draw text on a surface.
int getx() const
Definition: video.cpp:472
boost::shared_ptr< hotkey_base > hotkey_ptr
Definition: hotkey_item.hpp:32
const int SIZE_LARGE
Definition: font.hpp:66
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
Definition: rect.cpp:28
Contains the SDL_Rect helper code.
cl_event event
Definition: glew.h:3070
GLsizei const GLcharARB ** string
Definition: glew.h:4503