engine_view.vala 5.4 KB

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