16#include <imgui_stdlib.h>
26 ImGui::TextDisabled(
"(?)");
32 return std::string(
setting.getDescription());
35template<std::invocable<const Setting&> GetTooltip = GetSettingDescription>
36static void settingStuff(
Setting&
setting, GetTooltip getTooltip = {})
41 auto defaultString = defaultValue.
getString();
43 if (defaultString.empty()) {
45 ImGui::TextDisabled(
"<empty>");
47 if (ImGui::Button(
"Restore default")) {
50 }
catch (MSXException&) {
53 ImGui::CloseCurrentPopup();
65 bool value =
setting.getBoolean();
66 bool changed = ImGui::Checkbox(label, &value);
68 if (changed)
setting.setBoolean(value);
72 settingStuff(
setting, getTooltip);
76 auto spacing = std::max(0.0f, ImGui::GetContentRegionAvail().x -
ImGui::CalcTextSize(shortCut).x);
77 ImGui::SameLine(0.0f, spacing);
78 ImGui::TextDisabled(
"%s", shortCut.c_str());
91 int min =
setting.getMinValue();
92 int max =
setting.getMaxValue();
93 bool changed = ImGui::SliderInt(label, &value, min, max,
"%d", flags);
95 if (changed)
setting.setInt(value);
110 auto value =
setting.getFloat();
111 auto min = narrow_cast<float>(
setting.getMinValue());
112 auto max = narrow_cast<float>(
setting.getMaxValue());
113 bool changed = ImGui::SliderFloat(label, &value, min, max,
format, flags);
115 if (changed)
setting.setFloat(value);
131 bool changed = ImGui::InputText(label, &value, ImGuiInputTextFlags_EnterReturnsTrue) || ImGui::IsItemDeactivatedAfterEdit();
147 for (const auto& entry : enumSetting->getMap()) {
148 bool selected = entry.name == current;
149 if (const auto& display = displayValue(entry.name);
150 ImGui::Selectable(display.c_str(), selected)) {
152 setting.setValue(TclObject(entry.name));
153 } catch (MSXException&) {
157 if (auto it = ranges::find(toolTips, entry.name, &EnumToolTip::value);
158 it != toolTips.end()) {
159 simpleToolTip(it->tip);
171 std::string name(
setting.getBaseName());
177 std::string name(
setting.getBaseName());
182 auto current =
setting.getValue().getString();
184 for (const auto& value : setting.getPossibleValues()) {
185 bool selected = value == current;
186 if (ImGui::Selectable(std::string(value).c_str(), selected)) {
188 setting.setValue(TclObject(value));
189 } catch (MSXException&) {
200 const char* p = itemsSeparatedByZeros;
203 if (item == 0)
return p;
212 if (!time)
return "--:--:--.--";
213 auto remainingTime = *time;
214 assert(remainingTime >= 0.0);
215 auto hours = int(remainingTime * (1.0 / 3600.0));
216 remainingTime -= double(hours * 3600);
217 auto minutes = int(remainingTime * (1.0 / 60.0));
218 remainingTime -= double(minutes * 60);
219 auto seconds = int(remainingTime);
220 remainingTime -= double(seconds);
221 auto hundreds = int(100.0 * remainingTime);
223 std::string result =
"00:00:00.00";
224 auto insert = [&](
size_t pos,
unsigned value) {
226 result[pos + 0] = char(
'0' + (value / 10));
227 result[pos + 1] = char(
'0' + (value % 10));
229 insert(0, hours % 100);
238 const auto& io = ImGui::GetIO();
239 auto step = io.DeltaTime / period;
240 if (target > current) {
241 return std::min(target, current + step);
243 return std::max(target, current - step);
250 if (info.command != command)
continue;
251 if (
const auto* keyDown = std::get_if<KeyDownEvent>(&info.event)) {
253 auto modifiers = keyDown->getModifiers();
254 if (modifiers & KMOD_CTRL)
strAppend(result,
"CTRL+");
255 if (modifiers & KMOD_SHIFT)
strAppend(result,
"SHIFT+");
256 if (modifiers & KMOD_ALT)
strAppend(result,
"ALT+");
257 if (modifiers & KMOD_GUI)
strAppend(result,
"GUI+");
258 strAppend(result, SDL_GetKeyName(keyDown->getKeyCode()));
265[[nodiscard]]
static std::string_view superName()
267 return ImGui::GetIO().ConfigMacOSXBehaviors ?
"Cmd+" :
"Super+";
272 int keyCode =
ImGuiKey2SDL(ImGuiKey(keyChord & ~ImGuiMod_Mask_));
273 const auto* name = SDL_GetKeyName(keyCode);
274 if (!name || (*name ==
'\0'))
return "None";
276 (keyChord & ImGuiMod_Ctrl ?
"Ctrl+" :
""),
277 (keyChord & ImGuiMod_Shift ?
"Shift+" :
""),
278 (keyChord & ImGuiMod_Alt ?
"Alt+" :
""),
279 (keyChord & ImGuiMod_Super ? superName() :
""),
285 if (name ==
"None")
return ImGuiKey_None;
288 auto [modifiers, key] = [&]() -> std::pair<std::string_view, std::string_view> {
289 if (
auto pos = name.find_last_of(
'+'); pos == std::string_view::npos) {
290 return {std::string_view{}, name};
292 return {name.substr(0, pos + 1), name.substr(pos + 1)};
296 SDL_Keycode keyCode = SDL_GetKeyFromName(std::string(key).c_str());
297 if (keyCode == SDLK_UNKNOWN)
return {};
299 auto contains = [](std::string_view haystack, std::string_view needle) {
301 return haystack.find(needle) != std::string_view::npos;
303 ImGuiKeyChord keyMods =
304 (
contains(modifiers,
"Ctrl+" ) ? ImGuiMod_Ctrl : 0) |
305 (
contains(modifiers,
"Shift+") ? ImGuiMod_Shift : 0) |
306 (
contains(modifiers,
"Alt+" ) ? ImGuiMod_Alt : 0) |
307 (
contains(modifiers, superName()) ? ImGuiMod_Super : 0);
315 bool light = style == 1;
327 imColors[size_t(
TEXT )] = ImGui::GetColorU32(ImGuiCol_Text);
virtual void setValue(const TclObject &value)=0
Change the value of this setting to the given value.
virtual const TclObject & getValue() const =0
Get current value as a TclObject.
virtual TclObject getDefaultValue() const =0
Get the default value of this setting.
std::string_view getBaseName() const
A Setting with a floating point value.
const auto & getGlobalBindings() const
A Setting with an integer value.
zstring_view getString() const
auto CalcTextSize(std::string_view str)
void PopupContextItem(const char *str_id, ImGuiPopupFlags popup_flags, std::invocable<> auto next)
void Combo(const char *label, const char *preview_value, ImGuiComboFlags flags, std::invocable<> auto next)
void format(SectorAccessibleDisk &disk, MSXBootSectorType bootType)
Format the given disk (= a single partition).
This file implemented 3 utility functions:
bool Checkbox(const HotKey &hotKey, BooleanSetting &setting)
bool SliderFloat(FloatSetting &setting, const char *format, ImGuiSliderFlags flags)
std::span< const EnumToolTip > EnumToolTips
bool SliderInt(IntegerSetting &setting, ImGuiSliderFlags flags)
void ComboBox(const char *label, Setting &setting, function_ref< std::string(const std::string &)> displayValue, EnumToolTips toolTips)
void simpleToolTip(std::string_view desc)
std::string getShortCutForCommand(const HotKey &hotkey, std::string_view command)
ImGuiKey SDLKey2ImGui(SDL_Keycode sdl)
std::array< ImU32, size_t(imColor::NUM_COLORS)> imColors
void HelpMarker(std::string_view desc)
SDL_Keycode ImGuiKey2SDL(ImGuiKey imgui)
bool InputText(Setting &setting)
std::optional< ImGuiKeyChord > parseKeyChord(std::string_view name)
const char * getComboString(int item, const char *itemsSeparatedByZeros)
void setColors(int style)
std::string getKeyChordName(ImGuiKeyChord keyChord)
std::string formatTime(std::optional< double > time)
float calculateFade(float current, float target, float period)
constexpr bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
void strAppend(std::string &result, Ts &&...ts)
std::string operator()(const Setting &setting) const