[iagno] Use an EventControllerMotion.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [iagno] Use an EventControllerMotion.
- Date: Mon, 15 Jul 2019 21:44:52 +0000 (UTC)
commit d3edc195c3e31707314b35174ebfa013421b5f3b
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Wed Jun 26 16:57:56 2019 +0200
Use an EventControllerMotion.
Bump Gtk+ required
version to 3.24.0.
meson.build | 2 +-
src/game-view.vala | 90 +++++++++++++++++++++++++++++-------------------------
2 files changed, 50 insertions(+), 42 deletions(-)
---
diff --git a/meson.build b/meson.build
index 244dda7..d010e8e 100644
--- a/meson.build
+++ b/meson.build
@@ -14,7 +14,7 @@ canberra_dependency = dependency('libcanberra')
canberra_gtk3_dependency = dependency('libcanberra-gtk3', version: '>= 0.26')
gio_dependency = dependency('gio-2.0', version: '>= 2.40.0')
glib_dependency = dependency('glib-2.0', version: '>= 2.40.0')
-gtk_dependency = dependency('gtk+-3.0', version: '>= 3.22.23')
+gtk_dependency = dependency('gtk+-3.0', version: '>= 3.24.0')
posix_dependency = meson.get_compiler('vala').find_library('posix')
rsvg_dependency = dependency('librsvg-2.0', version: '>= 2.32.0')
diff --git a/src/game-view.vala b/src/game-view.vala
index 1f96d84..86fd23c 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -168,6 +168,8 @@ private class GameView : Gtk.DrawingArea
| Gdk.EventMask.BUTTON_RELEASE_MASK
| Gdk.EventMask.POINTER_MOTION_MASK);
set_size_request (350, 350);
+
+ init_mouse ();
}
private Iagno iagno_instance;
@@ -642,6 +644,16 @@ private class GameView : Gtk.DrawingArea
cr.fill ();
}
+ private void queue_draw_tile (uint8 x, uint8 y)
+ requires (x < game_size)
+ requires (y < game_size)
+ {
+ queue_draw_area (board_x + tile_xs [x, y],
+ board_y + tile_ys [x, y],
+ tile_size,
+ tile_size);
+ }
+
/*\
* * turning tiles
\*/
@@ -782,7 +794,7 @@ private class GameView : Gtk.DrawingArea
{
animate_timeout = 0;
if (!show_highlight)
- _motion_notify_event (mouse_position_x, mouse_position_y, /* force redraw */
true);
+ _on_motion (mouse_position_x, mouse_position_y, /* force redraw */ true);
return Source.REMOVE;
}
});
@@ -890,35 +902,18 @@ private class GameView : Gtk.DrawingArea
* * user actions
\*/
- internal override bool button_press_event (Gdk.EventButton event)
- {
- if (!game_is_set)
- return false;
+ private Gtk.EventControllerMotion motion_controller; // for keeping in memory
- if (event.button == Gdk.BUTTON_PRIMARY || event.button == Gdk.BUTTON_SECONDARY)
- {
- int8 x = (int8) ((event.x - board_x) / paving_size);
- int8 y = (int8) ((event.y - board_y) / paving_size);
- if (game.current_state.is_valid_location_signed (x, y))
- {
- show_highlight = false;
- old_highlight_x = highlight_x;
- old_highlight_y = highlight_y;
- queue_draw ();
- highlight_set = true;
- highlight_x = (uint8) x;
- highlight_y = (uint8) y;
- move_if_possible (highlight_x, highlight_y);
- }
- }
-
- return true;
+ private void init_mouse () // called on construct
+ {
+ motion_controller = new Gtk.EventControllerMotion (this);
+ motion_controller.motion.connect (on_motion);
}
- internal override bool motion_notify_event (Gdk.EventMotion event)
+ private void on_motion (Gtk.EventControllerMotion _motion_controller, double event_x, double event_y)
{
- uint8 x = (uint8) ((event.x - board_x) / paving_size);
- uint8 y = (uint8) ((event.y - board_y) / paving_size);
+ uint8 x = (uint8) ((event_x - board_x) / paving_size);
+ uint8 y = (uint8) ((event_y - board_y) / paving_size);
if ((x >= 0 && x < game_size)
&& (y >= 0 && y < game_size)
&& ((x != mouse_position_x)
@@ -930,12 +925,10 @@ private class GameView : Gtk.DrawingArea
if (show_highlight
|| (x != mouse_highlight_x)
|| (y != mouse_highlight_y))
- Timeout.add (200, () => { _motion_notify_event (x, y); return Source.REMOVE; });
+ Timeout.add (200, () => { _on_motion (x, y); return Source.REMOVE; });
}
-
- return base.motion_notify_event (event);
}
- private void _motion_notify_event (uint8 x, uint8 y, bool force_redraw = false)
+ private void _on_motion (uint8 x, uint8 y, bool force_redraw = false)
{
if (!force_redraw
&& ((x != mouse_position_x)
@@ -975,6 +968,31 @@ private class GameView : Gtk.DrawingArea
}
}
+ internal override bool button_press_event (Gdk.EventButton event)
+ {
+ if (!game_is_set)
+ return false;
+
+ if (event.button == Gdk.BUTTON_PRIMARY || event.button == Gdk.BUTTON_SECONDARY)
+ {
+ int8 x = (int8) ((event.x - board_x) / paving_size);
+ int8 y = (int8) ((event.y - board_y) / paving_size);
+ if (game.current_state.is_valid_location_signed (x, y))
+ {
+ show_highlight = false;
+ old_highlight_x = highlight_x;
+ old_highlight_y = highlight_y;
+ queue_draw ();
+ highlight_set = true;
+ highlight_x = (uint8) x;
+ highlight_y = (uint8) y;
+ move_if_possible (highlight_x, highlight_y);
+ }
+ }
+
+ return true;
+ }
+
internal override bool key_press_event (Gdk.EventKey event)
{
if (!game_is_set)
@@ -1120,21 +1138,11 @@ private class GameView : Gtk.DrawingArea
{
highlight_x = mouse_position_x;
highlight_y = mouse_position_y;
- _motion_notify_event (highlight_x, highlight_y, /* force redraw */ true);
+ _on_motion (highlight_x, highlight_y, /* force redraw */ true);
}
return true;
}
- private void queue_draw_tile (uint8 x, uint8 y)
- requires (x < game_size)
- requires (y < game_size)
- {
- queue_draw_area (board_x + tile_xs [x, y],
- board_y + tile_ys [x, y],
- tile_size,
- tile_size);
- }
-
/*\
* * testing move
\*/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]