The Battle for Wesnoth  1.19.7+dev
formula_debugger.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/window.hpp"
23 #include "formula/debugger.hpp"
24 #include "font/pango/escape.hpp"
25 #include "serialization/markup.hpp"
26 
27 #include <functional>
28 
29 namespace gui2::dialogs
30 {
31 
32 REGISTER_DIALOG(formula_debugger)
33 
34 void formula_debugger::pre_show()
35 {
36  // stack label
37  scroll_label* stack_label
38  = find_widget<scroll_label>("stack", false, true);
39 
40  std::stringstream stack_text;
41  std::string indent = " ";
42  int c = 0;
43  for(const auto & i : fdb_.get_call_stack())
44  {
45  for(int d = 0; d < c; ++d) {
46  stack_text << indent;
47  }
48  stack_text << "#" << markup::span_color("#00ff00", i.counter())
49  << ": \"" << markup::span_color("#00ff00", font::escape_text(i.name()))
50  << "\": (" << font::escape_text(i.str()) << ") " << std::endl;
51  ++c;
52  }
53 
54  stack_label->set_use_markup(true);
55  stack_label->set_label(stack_text.str());
57  keyboard_capture(stack_label);
58 
59  // execution trace label
60  scroll_label* execution_label = find_widget<scroll_label>("execution", false, true);
61 
62  std::stringstream execution_text;
63  for(const auto & i : fdb_.get_execution_trace())
64  {
65  for(int d = 0; d < i.level(); ++d) {
66  execution_text << indent;
67  }
68  if(!i.evaluated()) {
69  execution_text << "#" << markup::span_color("#00ff00", i.counter())
70  << ": \"" << markup::span_color("#00ff00", font::escape_text(i.name()))
71  << "\": (" << font::escape_text(i.str()) << ") " << std::endl;
72  } else {
73  execution_text << "#" << markup::span_color("#ffff00", i.counter())
74  << ": \"" << markup::span_color("#ffff00", font::escape_text(i.name()))
75  << "\": (" << font::escape_text(i.str()) << ") ="
76  << markup::span_color("#ffa500", font::escape_text(i.value().to_debug_string()))
77  << std::endl;
78  }
79  }
80 
81  execution_label->set_use_markup(true);
82  execution_label->set_label(execution_text.str());
84  // state
85  std::string state_str;
86  bool is_end = false;
87  if(!fdb_.get_current_breakpoint()) {
88  state_str = "";
89  } else {
90  state_str = fdb_.get_current_breakpoint()->name();
91  if(state_str == "End") {
92  is_end = true;
93  }
94  }
95 
96  find_widget<styled_widget>("state").set_label(state_str);
97 
98  // callbacks
99  button& step_button = find_widget<button>("step");
101  step_button,
102  std::bind(&formula_debugger::callback_step_button, this));
103 
104  button& stepout_button = find_widget<button>("stepout");
106  stepout_button,
108 
109  button& next_button = find_widget<button>("next");
111  next_button,
112  std::bind(&formula_debugger::callback_next_button, this));
113 
114  button& continue_button = find_widget<button>("continue");
116  continue_button,
118 
119  if(is_end) {
120  step_button.set_active(false);
121  stepout_button.set_active(false);
122  next_button.set_active(false);
123  continue_button.set_active(false);
124  }
125 }
126 
128 {
131 }
132 
134 {
137 }
138 
140 {
143 }
144 
146 {
149 }
150 
151 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:64
wfl::formula_debugger & fdb_
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
@ END
Go to the end position.
Definition: scrollbar.hpp:58
void scroll_vertical_scrollbar(const scrollbar_base::scroll_mode scroll)
Scrolls the vertical scrollbar.
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:394
void add_breakpoint_step_into()
Definition: debugger.cpp:367
void add_breakpoint_continue_to_end()
Definition: debugger.cpp:360
void add_breakpoint_step_out()
Definition: debugger.cpp:374
Formula AI debugger.
std::size_t i
Definition: function.cpp:1029
This file contains the window object, this object is a top level container which has the event manage...
static int indent
Definition: log.cpp:60
std::string escape_text(std::string_view text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:33
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::string span_color(const color_t &color, Args &&... data)
Definition: markup.hpp:68
mock_char c
#define d