Skip to content

Commit 2a657e1

Browse files
committed
feat: piece square table editor has optional button to write default state to source tree (#570)
1 parent 1ecc34d commit 2a657e1

4 files changed

Lines changed: 40 additions & 17 deletions

File tree

gui/libgui/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ target_compile_definitions (
8181
libgui
8282
PRIVATE
8383
"$<${config_debug}:BENBOT_RES_SRC_TREE_PATH=\"${CMAKE_CURRENT_LIST_DIR}/resources/res\">"
84+
"$<${config_debug}:BENBOT_PST_RES_SRC_TREE_PATH=\"${PROJECT_SOURCE_DIR}/libbenbot/resources/res/piece_square_tables.json\">"
8485
)
8586

8687
add_library (ben_bot::libgui ALIAS libgui)

gui/libgui/include/libgui/AppSettingsPanel.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ struct AppSettings final {
4444
};
4545

4646
/** Error popup context used for writing the current application state
47-
to the resource files in the source tree. This is only used if the
48-
``BENBOT_RES_SRC_TREE_PATH`` symbol is defined.
47+
to the resource files in the source tree.
4948
*/
50-
[[maybe_unused]] ErrorPopup defaultStateSaveError { "State save error" };
49+
[[maybe_unused]] ErrorPopup defaultStateSaveError { "Default state save error" };
5150

5251
/** Serializes this state to a JSON string. */
5352
[[nodiscard]] auto to_string() const -> std::string;

gui/libgui/include/libgui/PSTEditor.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#pragma once
2121

2222
#include <libbenbot/eval/PieceSquareTables.hpp>
23+
#include <libgui/ErrorPopup.hpp>
2324
#include <libgui/FileDialogContext.hpp>
2425

2526
namespace ben_bot {
@@ -48,6 +49,11 @@ struct PSTEditorState final {
4849
"PieceSquareTables.json",
4950
{ { "JSON", "json" } }
5051
};
52+
53+
/** Error popup context used for writing the default tables
54+
to the source tree.
55+
*/
56+
[[maybe_unused]] ErrorPopup defaultStateSaveError { "Default PST save error" };
5157
};
5258

5359
/** Renders a piece square table editor.

gui/libgui/src/PSTEditor.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
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

3234
namespace 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

175184
void 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

Comments
 (0)