console_view.vala 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2012-2022 Daniele Bartolini et al.
  3. * License: https://github.com/crownengine/crown/blob/master/LICENSE
  4. */
  5. using Gtk;
  6. namespace Crown
  7. {
  8. public class EntryHistory
  9. {
  10. public uint _capacity;
  11. public uint _size;
  12. public uint _index;
  13. public string[] _data;
  14. // Creates a new history with room for capacity records.
  15. public EntryHistory(uint capacity)
  16. {
  17. _capacity = capacity;
  18. _size = 0;
  19. _index = 0;
  20. _data = new string[capacity];
  21. }
  22. // Push a new string into the history.
  23. public void push(string text)
  24. {
  25. // Add command to history
  26. _data[_index] = text;
  27. _index = (_index + 1) % _capacity;
  28. if (_size < _capacity)
  29. ++_size;
  30. }
  31. public void clear()
  32. {
  33. _size = 0;
  34. _index = 0;
  35. }
  36. // Returns the element at @a distance slots from the current index.
  37. // Distance must be in the [1; _size] range.
  38. public string element(uint distance)
  39. {
  40. if (distance < 1 || distance > _size)
  41. return "ERROR";
  42. if (_index >= distance)
  43. return _data[_index - distance];
  44. else
  45. return _data[_capacity - (distance - _index)];
  46. }
  47. public void save(string path)
  48. {
  49. FileStream fs = FileStream.open(path, "wb");
  50. if (fs == null)
  51. return;
  52. uint first_entry = _index + (_capacity - _size);
  53. for (uint ii = 0; ii < _size; ++ii)
  54. fs.printf("%s\n", _data[(first_entry + ii) % _capacity]);
  55. }
  56. public void load(string path)
  57. {
  58. FileStream fs = FileStream.open(path, "rb");
  59. if (fs == null)
  60. return;
  61. string? line = null;
  62. while ((line = fs.read_line()) != null)
  63. push(line);
  64. }
  65. }
  66. public class ConsoleView : Gtk.Box
  67. {
  68. // Data
  69. public EntryHistory _entry_history;
  70. public uint _distance;
  71. public Project _project;
  72. public PreferencesDialog _preferences_dialog;
  73. // Widgets
  74. public Gdk.Cursor _text_cursor;
  75. public Gdk.Cursor _pointer_cursor;
  76. public bool _cursor_is_hovering_link;
  77. public Gtk.TextView _text_view;
  78. public Gtk.ScrolledWindow _scrolled_window;
  79. public EntryText _entry;
  80. public Gtk.Box _entry_hbox;
  81. public ConsoleView(Project project, Gtk.ComboBoxText combo, PreferencesDialog preferences_dialog)
  82. {
  83. Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0);
  84. // Data
  85. _entry_history = new EntryHistory(256);
  86. _distance = 0;
  87. _project = project;
  88. _preferences_dialog = preferences_dialog;
  89. // Widgets
  90. _text_cursor = new Gdk.Cursor.from_name(this.get_display(), "text");
  91. _pointer_cursor = new Gdk.Cursor.from_name(this.get_display(), "pointer");
  92. _cursor_is_hovering_link = false;
  93. _text_view = new Gtk.TextView();
  94. _text_view.editable = false;
  95. _text_view.can_focus = false;
  96. // // Create tags for color-formatted text
  97. Gtk.TextTag tag_info = new Gtk.TextTag("info");
  98. tag_info.foreground_rgba = { 0.7, 0.7, 0.7, 1.0 };
  99. Gtk.TextTag tag_warning = new Gtk.TextTag("warning");
  100. tag_warning.foreground_rgba = { 1.0, 1.0, 0.4, 1.0 };
  101. Gtk.TextTag tag_error = new Gtk.TextTag("error");
  102. tag_error.foreground_rgba = { 1.0, 0.4, 0.4, 1.0 };
  103. Gtk.TextBuffer tb = _text_view.buffer;
  104. tb.tag_table.add(tag_info);
  105. tb.tag_table.add(tag_warning);
  106. tb.tag_table.add(tag_error);
  107. Gtk.TextIter end_iter;
  108. tb.get_end_iter(out end_iter);
  109. tb.create_mark("scroll", end_iter, true);
  110. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  111. _scrolled_window.vscrollbar_policy = Gtk.PolicyType.ALWAYS;
  112. _scrolled_window.add(_text_view);
  113. _entry = new EntryText();
  114. _entry.key_press_event.connect(on_entry_key_pressed);
  115. _entry.activate.connect(on_entry_activated);
  116. _entry_hbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  117. _entry_hbox.pack_start(_entry, true, true);
  118. _entry_hbox.pack_end(combo, false, false);
  119. this.pack_start(_scrolled_window, true, true, 0);
  120. this.pack_start(_entry_hbox, false, true, 0);
  121. this.destroy.connect(on_destroy);
  122. this.button_release_event.connect(on_button_released);
  123. this.motion_notify_event.connect(on_motion_notify);
  124. this.get_style_context().add_class("console-view");
  125. _console_view_valid = true;
  126. }
  127. private void on_entry_activated()
  128. {
  129. string text = _entry.text;
  130. text = text.strip();
  131. if (text.length > 0) {
  132. _entry_history.push(text);
  133. _distance = 0;
  134. Gtk.Application app = ((Gtk.Window)this.get_toplevel()).application;
  135. ConsoleClient? client = ((LevelEditorApplication)app).current_selected_client();
  136. if (text[0] == ':') {
  137. string[] args = text[1 : text.length].split(" ");
  138. if (args.length > 0) {
  139. if (client != null) {
  140. client.send(DeviceApi.command(args));
  141. client.send(DeviceApi.frame());
  142. }
  143. }
  144. } else {
  145. if (client != null) {
  146. logi("> %s".printf(text));
  147. client.send_script(text);
  148. client.send(DeviceApi.frame());
  149. }
  150. }
  151. }
  152. _entry.text = "";
  153. }
  154. private bool on_entry_key_pressed(Gdk.EventKey ev)
  155. {
  156. if (ev.keyval == Gdk.Key.Down) {
  157. if (_distance > 1) {
  158. --_distance;
  159. _entry.text = _entry_history.element(_distance);
  160. } else {
  161. _entry.text = "";
  162. }
  163. _entry.set_position(_entry.text.length);
  164. return Gdk.EVENT_STOP;
  165. } else if (ev.keyval == Gdk.Key.Up) {
  166. if (_distance < _entry_history._size) {
  167. ++_distance;
  168. _entry.text = _entry_history.element(_distance);
  169. }
  170. _entry.set_position(_entry.text.length);
  171. return Gdk.EVENT_STOP;
  172. }
  173. return Gdk.EVENT_PROPAGATE;
  174. }
  175. private void on_destroy()
  176. {
  177. _console_view_valid = false;
  178. }
  179. private bool on_button_released(Gdk.EventButton ev)
  180. {
  181. if (ev.button == Gdk.BUTTON_PRIMARY) {
  182. // Do not handle click if some text is selected.
  183. Gtk.TextIter dummy_iter;
  184. if (_text_view.buffer.get_selection_bounds(out dummy_iter, out dummy_iter))
  185. return Gdk.EVENT_PROPAGATE;
  186. int buffer_x;
  187. int buffer_y;
  188. _text_view.window_to_buffer_coords(Gtk.TextWindowType.WIDGET
  189. , (int)ev.x
  190. , (int)ev.y
  191. , out buffer_x
  192. , out buffer_y
  193. );
  194. Gtk.TextIter iter;
  195. if (_text_view.get_iter_at_location(out iter, buffer_x, buffer_y)) {
  196. // Check whether the text under the mouse pointer has a link tag.
  197. GLib.SList<unowned TextTag> tags = iter.get_tags();
  198. foreach (var item in tags) {
  199. string resource_name = item.get_data<string>("resource_name");
  200. if (resource_name != null) {
  201. Gtk.Application app = ((Gtk.Window)this.get_toplevel()).application;
  202. app.activate_action("open-resource", new GLib.Variant.string(resource_name));
  203. }
  204. }
  205. }
  206. }
  207. return Gdk.EVENT_PROPAGATE;
  208. }
  209. private bool on_motion_notify(Gdk.EventMotion ev)
  210. {
  211. bool hovering = false;
  212. int buffer_x;
  213. int buffer_y;
  214. _text_view.window_to_buffer_coords(TextWindowType.WIDGET
  215. , (int)ev.x
  216. , (int)ev.y
  217. , out buffer_x
  218. , out buffer_y
  219. );
  220. Gtk.TextIter iter;
  221. if (_text_view.get_iter_at_location(out iter, buffer_x, buffer_y)) {
  222. // Check whether the text under the mouse pointer has a link tag.
  223. GLib.SList<unowned TextTag> tags = iter.get_tags();
  224. foreach (var item in tags) {
  225. string resource_name = item.get_data<string>("resource_name");
  226. if (resource_name != null) {
  227. hovering = true;
  228. }
  229. }
  230. }
  231. if (_cursor_is_hovering_link != hovering) {
  232. _cursor_is_hovering_link = hovering;
  233. if (_cursor_is_hovering_link)
  234. _text_view.get_window(Gtk.TextWindowType.TEXT).set_cursor(_pointer_cursor);
  235. else
  236. _text_view.get_window(Gtk.TextWindowType.TEXT).set_cursor(_text_cursor);
  237. }
  238. return Gdk.EVENT_PROPAGATE;
  239. }
  240. public void log(string severity, string message)
  241. {
  242. Gtk.TextBuffer buffer = _text_view.buffer;
  243. // Limit number of lines recorded.
  244. int max_lines = (int)_preferences_dialog._console_max_lines.value;
  245. if (buffer.get_line_count() - 1 >= max_lines) {
  246. Gtk.TextIter start_of_first_line;
  247. buffer.get_iter_at_line(out start_of_first_line, 0);
  248. Gtk.TextIter end_of_first_line = start_of_first_line;
  249. start_of_first_line.forward_line();
  250. buffer.delete(ref start_of_first_line, ref end_of_first_line);
  251. }
  252. Gtk.TextIter end_iter;
  253. buffer.get_end_iter(out end_iter);
  254. // Replace all IDs with corresponding human-readable names.
  255. int id_index = 0;
  256. do {
  257. // Search for occurrences of the ID string.
  258. int id_index_orig = id_index;
  259. id_index = message.index_of("#ID(", id_index);
  260. if (id_index != -1) {
  261. // If an occurrenct is found, insert the preceding text as usual.
  262. string line_chunk = message.substring(id_index_orig, id_index - id_index_orig);
  263. buffer.insert_with_tags(ref end_iter
  264. , line_chunk
  265. , line_chunk.length
  266. , buffer.tag_table.lookup(severity)
  267. , null
  268. );
  269. // Try to extract the ID from the ID string.
  270. int id_closing_parentheses = message.index_of(")", id_index + 4);
  271. if (id_closing_parentheses == -1) {
  272. // If the ID is malformed, insert the whole line as-is.
  273. buffer.insert_with_tags(ref end_iter
  274. , message.substring(id_index)
  275. , -1
  276. , buffer.tag_table.lookup(severity)
  277. , null
  278. );
  279. break;
  280. }
  281. // Convert the resource ID to human-readable resource name.
  282. string resource_name;
  283. string resource_id = message.substring(id_index + 4, id_closing_parentheses - (id_index + 4));
  284. _project.resource_id_to_name(out resource_name, resource_id);
  285. // Create a tag for hyperlink.
  286. Gtk.TextTag hyperlink = null;
  287. hyperlink = buffer.create_tag(null, "underline", Pango.Underline.SINGLE, null);
  288. hyperlink.set_data("resource_name", resource_name);
  289. buffer.insert_with_tags(ref end_iter
  290. , resource_name
  291. , -1
  292. , buffer.tag_table.lookup(severity)
  293. , hyperlink
  294. , null
  295. );
  296. id_index += 4 + resource_id.length;
  297. continue;
  298. } else {
  299. buffer.insert_with_tags(ref end_iter
  300. , message.substring(id_index_orig)
  301. , -1
  302. , buffer.tag_table.lookup(severity)
  303. , null
  304. );
  305. }
  306. } while (id_index++ >= 0);
  307. // Scroll to bottom.
  308. // See: gtk3-demo "Automatic Scrolling".
  309. end_iter.set_line_offset(0);
  310. Gtk.TextMark mark = buffer.get_mark("scroll");
  311. buffer.move_mark(mark, end_iter);
  312. _text_view.scroll_mark_onscreen(mark);
  313. }
  314. }
  315. } /* namespace Crown */