Monopoly — Portage moderne 0.1.0
Portage moderne C/C++23 du Monopoly de 1999
Loading...
Searching...
No Matches
DebugDialogs.cpp
Go to the documentation of this file.
1#include "DebugDialogs.hpp"
2
3#include <SDL3/SDL.h>
4
5#include <array>
6#include <cstdlib>
7
9{
10 namespace
11 {
12 [[nodiscard]] std::string sdlFailure(
13 std::string_view operation)
14 {
15 return std::string(operation) + ": " + SDL_GetError();
16 }
17
18 [[nodiscard]] std::expected<bool, std::string> ask(
19 std::string_view title,
20 std::string_view message,
21 const QuestionPlan& plan,
23 {
24 const std::array<SDL_MessageBoxButtonData, 2> buttons{{
25 {
26 plan.positiveDefault
28 1,
29 plan.positiveText.c_str()
30 },
31 {
32 plan.negativeDefault
36 0,
37 plan.negativeText.c_str()
38 }
39 }};
40
41 const std::string titleText(title);
42 const std::string messageText(message);
45 data.window = window;
46 data.title = titleText.c_str();
47 data.message = messageText.c_str();
48 data.numbuttons = static_cast<int>(buttons.size());
49 data.buttons = buttons.data();
50
51 int selected = -1;
52 if (!SDL_ShowMessageBox(&data, &selected))
53 return std::unexpected(
54 sdlFailure("SDL_ShowMessageBox"));
55 return selected == 1;
56 }
57 }
58
61 {
63 switch (question)
64 {
66 plan.positiveText = "Retry";
67 plan.negativeText = "Cancel";
68 break;
70 plan.positiveText = "OK";
71 plan.negativeText = "Cancel";
72 break;
73 case Question::YesNo:
74 plan.positiveText = "Yes";
75 plan.negativeText = "No";
76 break;
77 }
78 plan.positiveDefault = defaultPositive;
79 plan.negativeDefault = !defaultPositive;
80 return plan;
81 }
82
83 std::expected<void, std::string> displayMessage(
84 std::string_view title,
85 std::string_view message,
87 {
88 const std::string titleText(
89 title.empty() ? std::string_view("Error:") : title);
90 const std::string messageText(message);
93 titleText.c_str(),
94 messageText.c_str(),
95 window))
96 return std::unexpected(
97 sdlFailure("SDL_ShowSimpleMessageBox"));
98 return {};
99 }
100
101 std::expected<bool, std::string> retryMessage(
102 std::string_view title,
103 std::string_view message,
105 {
106 return ask(
107 title, message,
109 window);
110 }
111
112 std::expected<bool, std::string> okCancelMessage(
113 std::string_view title,
114 std::string_view message,
116 {
117 return ask(
118 title, message,
120 window);
121 }
122
123 std::expected<bool, std::string> yesNoMessage(
124 std::string_view title,
125 std::string_view message,
126 bool defaultYes,
128 {
129 return ask(
130 title, message,
132 window);
133 }
134
136 std::string_view message,
138 {
140 "Something went wrong! Error Exit Message:",
141 message,
142 window);
143 std::exit(ErrorExitStatus);
144 }
145}
decision::EconomicActionPlan plan
std::vector< std::uint8_t > data
unsigned index
void errorExit(std::string_view message, SDL_Window *window)
std::expected< bool, std::string > retryMessage(std::string_view title, std::string_view message, SDL_Window *window)
std::expected< bool, std::string > okCancelMessage(std::string_view title, std::string_view message, SDL_Window *window)
QuestionPlan questionPlan(Question question, bool defaultPositive)
constexpr int ErrorExitStatus
std::expected< void, std::string > displayMessage(std::string_view title, std::string_view message, SDL_Window *window)
std::expected< bool, std::string > yesNoMessage(std::string_view title, std::string_view message, bool defaultYes, SDL_Window *window)