Merge lp:~widelands-dev/widelands/arrow-keys-map-movement into lp:widelands

Proposed by GunChleoc
Status: Merged
Merged at revision: 9167
Proposed branch: lp:~widelands-dev/widelands/arrow-keys-map-movement
Merge into: lp:widelands
Diff against target: 174 lines (+65/-23)
4 files modified
src/ui_basic/panel.cc (+5/-1)
src/ui_basic/panel.h (+19/-0)
src/ui_basic/window.cc (+2/-0)
src/wui/interactive_base.cc (+39/-22)
To merge this branch: bzr merge lp:~widelands-dev/widelands/arrow-keys-map-movement
Reviewer Review Type Date Requested Status
Klaus Halfmann Approve
Review via email: mp+370687@code.launchpad.net

Commit message

Fixes for keyboard map movement

- Stop minimized windows from handling key presses.
- Handle map movement by key at the proper place.

To post a comment you must log in.
Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

Code looks straing forward, wonnder that this was not implemented this way before.

Will now copile and testplay this a litte.

Gun: please take a look at my refactoring branch.

Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

Works as designed

review: Approve (compile testplay)
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 5282. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/564367725.
Appveyor build 5057. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_arrow_keys_map_movement-5057.

Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

@bunnybot merge

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/ui_basic/panel.cc'
2--- src/ui_basic/panel.cc 2019-06-01 08:18:58 +0000
3+++ src/ui_basic/panel.cc 2019-07-27 11:32:27 +0000
4@@ -57,7 +57,7 @@
5 last_child_(nullptr),
6 mousein_child_(nullptr),
7 focus_(nullptr),
8- flags_(pf_handle_mouse | pf_thinks | pf_visible),
9+ flags_(pf_handle_mouse | pf_thinks | pf_visible | pf_handle_keypresses),
10 x_(nx),
11 y_(ny),
12 w_(nw),
13@@ -912,6 +912,10 @@
14 return true;
15 }
16
17+ if (!handles_keypresses()) {
18+ return false;
19+ }
20+
21 // If we handle text, it does not matter if we handled this key
22 // or not, it should not propagate.
23 if (handle_key(down, code) || handles_textinput()) {
24
25=== modified file 'src/ui_basic/panel.h'
26--- src/ui_basic/panel.h 2019-06-01 08:18:58 +0000
27+++ src/ui_basic/panel.h 2019-07-27 11:32:27 +0000
28@@ -79,6 +79,8 @@
29 pf_layout_toplevel = 512,
30 /// whether widget wants to receive unicode textinput messages
31 pf_handle_textinput = 1024,
32+ /// whether widget and its children will handle any key presses
33+ pf_handle_keypresses = 2048,
34 };
35
36 Panel(Panel* const nparent,
37@@ -303,6 +305,15 @@
38 flags_ |= pf_handle_textinput;
39 }
40
41+ // If this is set to 'true', this panel ad its children will never receive keypresses (do_key) or textinput (do_textinput).
42+ void set_handle_keypresses(bool const on) {
43+ if (on) {
44+ flags_ |= pf_handle_keypresses;
45+ } else {
46+ flags_ &= ~pf_handle_keypresses;
47+ }
48+ }
49+
50 // Defines if think() should be called repeatedly. This is true on construction.
51 void set_thinks(bool yes);
52
53@@ -325,6 +336,14 @@
54 bool handles_mouse() const {
55 return (flags_ & pf_handle_mouse) != 0;
56 }
57+
58+ bool handles_keypresses() const {
59+ if (get_parent() != nullptr && !get_parent()->handles_keypresses()) {
60+ return false;
61+ }
62+ return (flags_ & pf_handle_keypresses) != 0;
63+ }
64+
65 bool handles_textinput() const {
66 return (flags_ & pf_handle_textinput) != 0;
67 }
68
69=== modified file 'src/ui_basic/window.cc'
70--- src/ui_basic/window.cc 2019-05-26 17:21:15 +0000
71+++ src/ui_basic/window.cc 2019-07-27 11:32:27 +0000
72@@ -431,6 +431,7 @@
73 set_inner_size(get_inner_w(), oldh_);
74 update_desired_size();
75 move_inside_parent();
76+ set_handle_keypresses(true);
77 }
78 void Window::minimize() {
79 assert(!is_minimal_);
80@@ -446,6 +447,7 @@
81 set_border(get_lborder(), get_rborder(), get_tborder(), 0);
82 set_size(get_w(), TP_B_PIXMAP_THICKNESS);
83 set_pos(Vector2i(x, y)); // If on border, this feels more natural
84+ set_handle_keypresses(false);
85 }
86
87 /**
88
89=== modified file 'src/wui/interactive_base.cc'
90--- src/wui/interactive_base.cc 2019-05-29 15:43:40 +0000
91+++ src/wui/interactive_base.cc 2019-07-27 11:32:27 +0000
92@@ -510,27 +510,6 @@
93 ===============
94 */
95 void InteractiveBase::think() {
96- // If one of the arrow keys is pressed, scroll here
97- const uint32_t scrollval = 10;
98-
99- if (keyboard_free() && Panel::allow_user_input()) {
100- if (get_key_state(SDL_SCANCODE_UP) ||
101- (get_key_state(SDL_SCANCODE_KP_8) && (SDL_GetModState() ^ KMOD_NUM))) {
102- map_view_.pan_by(Vector2i(0, -scrollval));
103- }
104- if (get_key_state(SDL_SCANCODE_DOWN) ||
105- (get_key_state(SDL_SCANCODE_KP_2) && (SDL_GetModState() ^ KMOD_NUM))) {
106- map_view_.pan_by(Vector2i(0, scrollval));
107- }
108- if (get_key_state(SDL_SCANCODE_LEFT) ||
109- (get_key_state(SDL_SCANCODE_KP_4) && (SDL_GetModState() ^ KMOD_NUM))) {
110- map_view_.pan_by(Vector2i(-scrollval, 0));
111- }
112- if (get_key_state(SDL_SCANCODE_RIGHT) ||
113- (get_key_state(SDL_SCANCODE_KP_6) && (SDL_GetModState() ^ KMOD_NUM))) {
114- map_view_.pan_by(Vector2i(scrollval, 0));
115- }
116- }
117 egbase().think(); // Call game logic here. The game advances.
118
119 // Cleanup found port spaces if the ship sailed on or was destroyed
120@@ -1011,8 +990,12 @@
121 }
122
123 bool InteractiveBase::handle_key(bool const down, SDL_Keysym const code) {
124- if (quick_navigation_.handle_key(down, code))
125+ if (quick_navigation_.handle_key(down, code)) {
126 return true;
127+ }
128+
129+ // If one of the arrow keys is pressed, scroll this distance
130+ constexpr uint32_t kScrollDistance = 10;
131
132 if (down) {
133 switch (code.sym) {
134@@ -1050,6 +1033,40 @@
135 }
136 }
137 return true;
138+ // Scroll the map
139+ case SDLK_KP_8:
140+ if (SDL_GetModState() & KMOD_NUM) {
141+ break;
142+ }
143+ FALLS_THROUGH;
144+ case SDLK_UP:
145+ map_view_.pan_by(Vector2i(0, -kScrollDistance));
146+ return true;
147+ case SDLK_KP_2:
148+ if (SDL_GetModState() & KMOD_NUM) {
149+ break;
150+ }
151+ FALLS_THROUGH;
152+ case SDLK_DOWN:
153+ map_view_.pan_by(Vector2i(0, kScrollDistance));
154+ return true;
155+ case SDLK_KP_4:
156+ if (SDL_GetModState() & KMOD_NUM) {
157+ break;
158+ }
159+ FALLS_THROUGH;
160+ case SDLK_LEFT:
161+ map_view_.pan_by(Vector2i(-kScrollDistance, 0));
162+ return true;
163+ case SDLK_KP_6:
164+ if (SDL_GetModState() & KMOD_NUM) {
165+ break;
166+ }
167+ FALLS_THROUGH;
168+ case SDLK_RIGHT:
169+ map_view_.pan_by(Vector2i(kScrollDistance, 0));
170+ return true;
171+
172 #ifndef NDEBUG // only in debug builds
173 case SDLK_F6:
174 GameChatMenu::create_script_console(

Subscribers

People subscribed via source and target branches

to status/vote changes: