yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
gui_action_generator.cc
Go to the documentation of this file.
2
3#include "absl/strings/str_cat.h"
4#include "absl/strings/str_format.h"
5
6namespace yaze {
7namespace cli {
8namespace gui {
9
10absl::StatusOr<std::string> GuiActionGenerator::GenerateTestScript(
11 const std::vector<ai::AIAction>& actions) {
12#ifdef YAZE_WITH_JSON
13 auto json_result = GenerateTestJSON(actions);
14 if (!json_result.ok()) {
15 return json_result.status();
16 }
17 return json_result->dump(2); // Pretty-print with 2-space indent
18#else
19 return absl::UnimplementedError("JSON support required for test script generation");
20#endif
21}
22
23#ifdef YAZE_WITH_JSON
24absl::StatusOr<nlohmann::json> GuiActionGenerator::GenerateTestJSON(
25 const std::vector<ai::AIAction>& actions) {
26 nlohmann::json test_script;
27 test_script["test_name"] = "ai_generated_test";
28 test_script["description"] = "Automatically generated from AI actions";
29 test_script["steps"] = nlohmann::json::array();
30
31 for (size_t i = 0; i < actions.size(); ++i) {
32 nlohmann::json step = ActionToJSON(actions[i]);
33 step["step_number"] = i + 1;
34 test_script["steps"].push_back(step);
35 }
36
37 return test_script;
38}
39
40nlohmann::json GuiActionGenerator::ActionToJSON(const ai::AIAction& action) {
41 nlohmann::json step;
42
43 switch (action.type) {
45 step["action"] = "click";
46 auto it = action.parameters.find("editor");
47 if (it != action.parameters.end()) {
48 step["target"] = absl::StrCat("button:", it->second, " Editor");
49 step["wait_after"] = 500; // Wait 500ms for editor to open
50 }
51 break;
52 }
53
55 step["action"] = "click";
56 auto it = action.parameters.find("tile_id");
57 if (it != action.parameters.end()) {
58 int tile_id = std::stoi(it->second);
59 // Calculate position in tile selector (8 tiles per row, 16x16 pixels each)
60 int tile_x = (tile_id % 8) * 16 + 8; // Center of tile
61 int tile_y = (tile_id / 8) * 16 + 8;
62
63 step["target"] = "canvas:tile16_selector";
64 step["position"] = {{"x", tile_x}, {"y", tile_y}};
65 step["wait_after"] = 200;
66 }
67 break;
68 }
69
71 step["action"] = "click";
72 auto x_it = action.parameters.find("x");
73 auto y_it = action.parameters.find("y");
74
75 if (x_it != action.parameters.end() && y_it != action.parameters.end()) {
76 // Convert map coordinates to screen coordinates
77 // Assuming 16x16 tile size and some offset for the canvas
78 int screen_x = std::stoi(x_it->second) * 16 + 8;
79 int screen_y = std::stoi(y_it->second) * 16 + 8;
80
81 step["target"] = "canvas:overworld_map";
82 step["position"] = {{"x", screen_x}, {"y", screen_y}};
83 step["wait_after"] = 200;
84 }
85 break;
86 }
87
89 step["action"] = "click";
90 step["target"] = "button:Save to ROM";
91 step["wait_after"] = 300;
92 break;
93 }
94
96 step["action"] = "click";
97 auto it = action.parameters.find("button");
98 if (it != action.parameters.end()) {
99 step["target"] = absl::StrCat("button:", it->second);
100 step["wait_after"] = 200;
101 }
102 break;
103 }
104
106 step["action"] = "wait";
107 auto it = action.parameters.find("duration_ms");
108 int duration = it != action.parameters.end() ? std::stoi(it->second) : 500;
109 step["duration_ms"] = duration;
110 break;
111 }
112
114 step["action"] = "screenshot";
115 auto it = action.parameters.find("filename");
116 if (it != action.parameters.end()) {
117 step["filename"] = it->second;
118 } else {
119 step["filename"] = "verification.png";
120 }
121 break;
122 }
123
125 step["action"] = "verify";
126 step["target"] = "tile_placement";
127 // Add verification parameters from action.parameters
128 for (const auto& [key, value] : action.parameters) {
129 step[key] = value;
130 }
131 break;
132 }
133
135 step["action"] = "error";
136 step["message"] = "Invalid action type";
137 break;
138 }
139
140 return step;
141}
142#endif
143
145 int step_number) {
146 switch (action.type) {
148 return GenerateOpenEditorStep(action);
150 return GenerateSelectTileStep(action);
152 return GeneratePlaceTileStep(action);
154 return GenerateSaveTileStep(action);
156 return GenerateClickButtonStep(action);
158 return GenerateWaitStep(action);
160 return GenerateScreenshotStep(action);
161 default:
162 return absl::StrFormat("# Step %d: Unknown action", step_number);
163 }
164}
165
167 auto it = action.parameters.find("editor");
168 if (it != action.parameters.end()) {
169 return absl::StrFormat("Click button:'%s Editor'\nWait 500ms", it->second);
170 }
171 return "Click button:'Editor'\nWait 500ms";
172}
173
175 auto it = action.parameters.find("tile_id");
176 if (it != action.parameters.end()) {
177 int tile_id = std::stoi(it->second);
178 int tile_x = (tile_id % 8) * 16 + 8;
179 int tile_y = (tile_id / 8) * 16 + 8;
180 return absl::StrFormat("Click canvas:'tile16_selector' at (%d, %d)\nWait 200ms",
181 tile_x, tile_y);
182 }
183 return "Click canvas:'tile16_selector'\nWait 200ms";
184}
185
187 auto x_it = action.parameters.find("x");
188 auto y_it = action.parameters.find("y");
189
190 if (x_it != action.parameters.end() && y_it != action.parameters.end()) {
191 int screen_x = std::stoi(x_it->second) * 16 + 8;
192 int screen_y = std::stoi(y_it->second) * 16 + 8;
193 return absl::StrFormat("Click canvas:'overworld_map' at (%d, %d)\nWait 200ms",
194 screen_x, screen_y);
195 }
196 return "Click canvas:'overworld_map'\nWait 200ms";
197}
198
200 return "Click button:'Save to ROM'\nWait 300ms";
201}
202
204 auto it = action.parameters.find("button");
205 if (it != action.parameters.end()) {
206 return absl::StrFormat("Click button:'%s'\nWait 200ms", it->second);
207 }
208 return "Click button\nWait 200ms";
209}
210
212 auto it = action.parameters.find("duration_ms");
213 int duration = it != action.parameters.end() ? std::stoi(it->second) : 500;
214 return absl::StrFormat("Wait %dms", duration);
215}
216
218 auto it = action.parameters.find("filename");
219 if (it != action.parameters.end()) {
220 return absl::StrFormat("Screenshot '%s'", it->second);
221 }
222 return "Screenshot 'verification.png'";
223}
224
225} // namespace gui
226} // namespace cli
227} // namespace yaze
std::string GenerateSelectTileStep(const ai::AIAction &action)
std::string GenerateOpenEditorStep(const ai::AIAction &action)
std::string ActionToTestStep(const ai::AIAction &action, int step_number)
std::string GeneratePlaceTileStep(const ai::AIAction &action)
std::string GenerateClickButtonStep(const ai::AIAction &action)
absl::StatusOr< std::string > GenerateTestScript(const std::vector< ai::AIAction > &actions)
std::string GenerateSaveTileStep(const ai::AIAction &action)
std::string GenerateScreenshotStep(const ai::AIAction &action)
std::string GenerateWaitStep(const ai::AIAction &action)
Main namespace for the application.
Definition controller.cc:20
Represents a single action to be performed in the GUI.
std::map< std::string, std::string > parameters