2121#include < libchess/board/File.hpp>
2222#include < libchess/board/Rank.hpp>
2323#include < libchess/board/Square.hpp>
24+ #include < libgui/ErrorPopup.hpp>
2425#include < libgui/FileDialogContext.hpp>
2526#include < libgui/PSTEditor.hpp>
2627#include < libutil/Console.hpp>
2728#include < libutil/Files.hpp>
2829#include < magic_enum/magic_enum.hpp>
2930#include < string>
3031#include < string_view>
32+ #include < utility>
3133
3234namespace ben_bot ::gui {
3335
@@ -103,6 +105,23 @@ namespace {
103105 ImGui::SetItemTooltip (" Reset to engine's current tables" );
104106 }
105107
108+ [[maybe_unused]] void render_save_default_button (
109+ const Tables& tables, ErrorPopup& error, const path& sourceTreeFile, const bool showTooltips)
110+ {
111+ if (ImGui::Button (" Save default" )) {
112+ [[maybe_unused]] const auto result
113+ = util::files::overwrite (sourceTreeFile, tables.to_string ())
114+ .transform_error ([&error](string&& message) {
115+ return error.set_error (std::move (message));
116+ });
117+ }
118+
119+ if (showTooltips)
120+ ImGui::SetItemTooltip (" Save tables as default in source tree" );
121+
122+ error.render ();
123+ }
124+
106125 void render_table_type_selector (
107126 Tables::TableType& selectedType, const bool showTooltips)
108127 {
@@ -160,24 +179,12 @@ namespace {
160179
161180 ImGui::EndTable ();
162181 }
163-
164- void render_pst_values (
165- Tables& tables, Tables::TableType& selectedType, const bool showTooltips)
166- {
167- render_table_type_selector (
168- selectedType, showTooltips);
169-
170- render_value_grid (
171- tables, selectedType);
172- }
173182} // namespace
174183
175184void render_pst_editor (
176185 PSTEditorState& state, Engine& engine, const bool showTooltips)
177186{
178187 if (ImGui::Begin (" Piece square tables" )) {
179- // overwrite default in source tree
180-
181188 render_reset_button (
182189 state.tables , showTooltips);
183190
@@ -187,8 +194,18 @@ void render_pst_editor(
187194 render_engine_interop_buttons (
188195 state.tables , engine, showTooltips);
189196
190- render_pst_values (
191- state.tables , state.selectedType , showTooltips);
197+ #ifdef BENBOT_PST_RES_SRC_TREE_PATH
198+ render_save_default_button (
199+ state.tables , state.defaultStateSaveError ,
200+ path { BENBOT_PST_RES_SRC_TREE_PATH },
201+ showTooltips);
202+ #endif
203+
204+ render_table_type_selector (
205+ state.selectedType , showTooltips);
206+
207+ render_value_grid (
208+ state.tables , state.selectedType );
192209 }
193210
194211 ImGui::End ();
0 commit comments