engine_view.vala 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. using Gdk;
  6. using Gee;
  7. using Gtk;
  8. namespace Crown
  9. {
  10. public class EngineView : Gtk.Alignment
  11. {
  12. // Data
  13. private ConsoleClient _client;
  14. private int _mouse_curr_x;
  15. private int _mouse_curr_y;
  16. private bool _mouse_left;
  17. private bool _mouse_middle;
  18. private bool _mouse_right;
  19. private X.Window _window_id;
  20. public uint window_id
  21. {
  22. get { return (uint)_window_id; }
  23. }
  24. private HashMap<uint, bool> _keys;
  25. // Widgets
  26. private Gtk.Socket _socket;
  27. public Gtk.EventBox _event_box;
  28. // Signals
  29. public signal void realized();
  30. private string key_to_string(uint k)
  31. {
  32. switch (k)
  33. {
  34. case Gdk.Key.w: return "w";
  35. case Gdk.Key.a: return "a";
  36. case Gdk.Key.s: return "s";
  37. case Gdk.Key.d: return "d";
  38. default: return "<unknown>";
  39. }
  40. }
  41. public EngineView(ConsoleClient client, bool input_enabled = true)
  42. {
  43. this.xalign = 0;
  44. this.yalign = 0;
  45. this.xscale = 1;
  46. this.yscale = 1;
  47. _client = client;
  48. _mouse_curr_x = 0;
  49. _mouse_curr_y = 0;
  50. _mouse_left = false;
  51. _mouse_middle = false;
  52. _mouse_right = false;
  53. _window_id = 0;
  54. _keys = new HashMap<uint, bool>();
  55. _keys[Gdk.Key.w] = false;
  56. _keys[Gdk.Key.a] = false;
  57. _keys[Gdk.Key.s] = false;
  58. _keys[Gdk.Key.d] = false;
  59. // Widgets
  60. _socket = new Gtk.Socket();
  61. _socket.set_visual(Gdk.Screen.get_default().get_system_visual());
  62. _socket.realize.connect(on_socket_realized);
  63. _socket.plug_removed.connect(on_socket_plug_removed);
  64. _socket.set_size_request(128, 128);
  65. _event_box = new Gtk.EventBox();
  66. _event_box.can_focus = true;
  67. _event_box.events |= Gdk.EventMask.POINTER_MOTION_MASK
  68. | Gdk.EventMask.KEY_PRESS_MASK
  69. | Gdk.EventMask.KEY_RELEASE_MASK
  70. | Gdk.EventMask.FOCUS_CHANGE_MASK
  71. | Gdk.EventMask.SCROLL_MASK
  72. ;
  73. if (input_enabled)
  74. {
  75. _event_box.button_release_event.connect(on_button_release);
  76. _event_box.button_press_event.connect(on_button_press);
  77. _event_box.key_press_event.connect(on_key_press);
  78. _event_box.key_release_event.connect(on_key_release);
  79. _event_box.motion_notify_event.connect(on_motion_notify);
  80. _event_box.scroll_event.connect(on_scroll);
  81. }
  82. _event_box.add(_socket);
  83. add(_event_box);
  84. show_all();
  85. }
  86. private bool on_button_release(Gdk.EventButton ev)
  87. {
  88. _mouse_left = ev.button == 1 ? false : _mouse_left;
  89. _mouse_middle = ev.button == 2 ? false : _mouse_middle;
  90. _mouse_right = ev.button == 3 ? false : _mouse_right;
  91. string s = LevelEditorApi.set_mouse_state(_mouse_curr_x
  92. , _mouse_curr_y
  93. , _mouse_left
  94. , _mouse_middle
  95. , _mouse_right
  96. );
  97. if (ev.button == 1)
  98. s += LevelEditorApi.mouse_up((int)ev.x, (int)ev.y);
  99. _client.send_script(s);
  100. return false;
  101. }
  102. private bool on_button_press(Gdk.EventButton ev)
  103. {
  104. // Grab keyboard focus
  105. _event_box.grab_focus();
  106. _mouse_left = ev.button == 1 ? true : _mouse_left;
  107. _mouse_middle = ev.button == 2 ? true : _mouse_middle;
  108. _mouse_right = ev.button == 3 ? true : _mouse_right;
  109. string s = LevelEditorApi.set_mouse_state(_mouse_curr_x
  110. , _mouse_curr_y
  111. , _mouse_left
  112. , _mouse_middle
  113. , _mouse_right
  114. );
  115. if (ev.button == 1)
  116. s += LevelEditorApi.mouse_down((int)ev.x, (int)ev.y);
  117. _client.send_script(s);
  118. return false;
  119. }
  120. private bool on_key_press(Gdk.EventKey ev)
  121. {
  122. if (ev.keyval == Gdk.Key.Up)
  123. _client.send_script("LevelEditor:key_down(\"move_up\")");
  124. if (ev.keyval == Gdk.Key.Down)
  125. _client.send_script("LevelEditor:key_down(\"move_down\")");
  126. if (ev.keyval == Gdk.Key.Right)
  127. _client.send_script("LevelEditor:key_down(\"move_right\")");
  128. if (ev.keyval == Gdk.Key.Left)
  129. _client.send_script("LevelEditor:key_down(\"move_left\")");
  130. if (!_keys.has_key(ev.keyval))
  131. return true;
  132. if (!_keys[ev.keyval])
  133. _client.send_script(LevelEditorApi.key_down(key_to_string(ev.keyval)));
  134. _keys[ev.keyval] = true;
  135. return false;
  136. }
  137. private bool on_key_release(Gdk.EventKey ev)
  138. {
  139. if (!_keys.has_key(ev.keyval))
  140. return false;
  141. if (_keys[ev.keyval])
  142. _client.send_script(LevelEditorApi.key_up(key_to_string(ev.keyval)));
  143. _keys[ev.keyval] = false;
  144. return false;
  145. }
  146. private bool on_motion_notify(Gdk.EventMotion ev)
  147. {
  148. _mouse_curr_x = (int)ev.x;
  149. _mouse_curr_y = (int)ev.y;
  150. _client.send_script(LevelEditorApi.set_mouse_state(_mouse_curr_x
  151. , _mouse_curr_y
  152. , _mouse_left
  153. , _mouse_middle
  154. , _mouse_right
  155. ));
  156. return false;
  157. }
  158. private bool on_scroll(Gdk.EventScroll ev)
  159. {
  160. _client.send_script(LevelEditorApi.mouse_wheel(ev.direction == Gdk.ScrollDirection.UP ? 1.0 : -1.0));
  161. return false;
  162. }
  163. private void on_socket_realized()
  164. {
  165. // We do not have window XID until socket is realized...
  166. _window_id = _socket.get_id();
  167. realized();
  168. }
  169. private bool on_socket_plug_removed()
  170. {
  171. // Prevent the default handler from destroying the Socket.
  172. return true;
  173. }
  174. }
  175. }