| 1 |
#include "ImGuiUtils.hh" |
| 2 |
|
| 3 |
#include "ImGuiCpp.hh" |
| 4 |
#include "ImGuiManager.hh" |
| 5 |
#include "KeyMappings.hh" |
| 6 |
|
| 7 |
#include "BooleanSetting.hh" |
| 8 |
#include "EnumSetting.hh" |
| 9 |
#include "FloatSetting.hh" |
| 10 |
#include "HotKey.hh" |
| 11 |
#include "IntegerSetting.hh" |
| 12 |
#include "VideoSourceSetting.hh" |
| 13 |
|
| 14 |
#include <imgui.h> |
| 15 |
#include <imgui_stdlib.h> |
| 16 |
|
| 17 |
#include <SDL.h> |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <variant> |
| 21 |
|
| 22 |
namespace openmsx { |
| 23 |
|
| 24 |
using namespace std::literals; |
| 25 |
|
| 26 |
void HelpMarker(std::string_view desc, float spacing) |
| 27 |
{ |
| 28 |
ImGui::SameLine(0.0f, spacing); |
| 29 |
HelpMarkerNewLine(desc); |
| 30 |
} |
| 31 |
|
| 32 |
void HelpMarkerNewLine(std::string_view desc) |
| 33 |
{ |
| 34 |
ImGui::TextDisabledUnformatted("(?)"); |
| 35 |
simpleToolTip(desc); |
| 36 |
} |
| 37 |
|
| 38 |
void ConfirmDialog::execute() |
| 39 |
{ |
| 40 |
if (doOpen) { |
| 41 |
doOpen = false; |
| 42 |
ImGui::OpenPopup(title.c_str()); |
| 43 |
} |
| 44 |
im::PopupModal(title.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize, [&]{ |
| 45 |
ImGui::TextUnformatted(text); |
| 46 |
|
| 47 |
bool close = false; |
| 48 |
if (ImGui::Button("Ok")) { |
| 49 |
action(); |
| 50 |
close = true; |
| 51 |
} |
| 52 |
ImGui::SameLine(); |
| 53 |
close |= ImGui::Button("Cancel"); |
| 54 |
if (close) { |
| 55 |
ImGui::CloseCurrentPopup(); |
| 56 |
action = {}; |
| 57 |
} |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
void ConfirmDialogTclCommand::open(std::string text_, TclObject cmd_) |
| 62 |
{ |
| 63 |
ConfirmDialog::open( |
| 64 |
std::move(text_), |
| 65 |
[manager = this->manager, cmd = std::move(cmd_)] { manager->executeDelayed(cmd); } |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
std::string GetSettingDescription::operator()(const Setting& setting) const |
| 70 |
{ |
| 71 |
return std::string(setting.getDescription()); |
| 72 |
} |
| 73 |
|
| 74 |
template<std::invocable<const Setting&> GetTooltip = GetSettingDescription> |
| 75 |
static void settingStuff(Setting& setting, GetTooltip getTooltip = {}) |
| 76 |
{ |
| 77 |
simpleToolTip([&] { return getTooltip(setting); }); |
| 78 |
im::PopupContextItem([&]{ |
| 79 |
auto defaultValue = setting.getDefaultValue(); |
| 80 |
auto defaultString = defaultValue.getString(); |
| 81 |
ImGui::StrCat("Default value: ", defaultString); |
| 82 |
if (defaultString.empty()) { |
| 83 |
ImGui::SameLine(); |
| 84 |
ImGui::TextDisabledUnformatted("<empty>"); |
| 85 |
} |
| 86 |
if (ImGui::Button("Restore default")) { |
| 87 |
try { |
| 88 |
setting.setValue(defaultValue); |
| 89 |
} catch (MSXException&) { |
| 90 |
|
| 91 |
} |
| 92 |
ImGui::CloseCurrentPopup(); |
| 93 |
} |
| 94 |
}); |
| 95 |
} |
| 96 |
|
| 97 |
bool Checkbox(const HotKey& hotKey, BooleanSetting& setting) |
| 98 |
{ |
| 99 |
std::string name(setting.getBaseName()); |
| 100 |
return Checkbox(hotKey, name.c_str(), setting); |
| 101 |
} |
| 102 |
bool Checkbox(const HotKey& hotKey, const char* label, BooleanSetting& setting, function_ref<std::string(const Setting&)> getTooltip) |
| 103 |
{ |
| 104 |
bool value = setting.getBoolean(); |
| 105 |
bool changed = ImGui::Checkbox(label, &value); |
| 106 |
try { |
| 107 |
if (changed) setting.setBoolean(value); |
| 108 |
} catch (MSXException&) { |
| 109 |
|
| 110 |
} |
| 111 |
settingStuff(setting, getTooltip); |
| 112 |
|
| 113 |
ImGui::SameLine(); |
| 114 |
auto shortCut = getShortCutForCommand(hotKey, strCat("toggle ", setting.getBaseName())); |
| 115 |
auto spacing = std::max(0.0f, ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(shortCut).x); |
| 116 |
ImGui::SameLine(0.0f, spacing); |
| 117 |
ImGui::TextDisabledUnformatted(shortCut); |
| 118 |
|
| 119 |
return changed; |
| 120 |
} |
| 121 |
|
| 122 |
bool SliderInt(IntegerSetting& setting, ImGuiSliderFlags flags) |
| 123 |
{ |
| 124 |
std::string name(setting.getBaseName()); |
| 125 |
return SliderInt(name.c_str(), setting, flags); |
| 126 |
} |
| 127 |
bool SliderInt(const char* label, IntegerSetting& setting, ImGuiSliderFlags flags) |
| 128 |
{ |
| 129 |
int value = setting.getInt(); |
| 130 |
int min = setting.getMinValue(); |
| 131 |
int max = setting.getMaxValue(); |
| 132 |
bool changed = ImGui::SliderInt(label, &value, min, max, "%d", flags); |
| 133 |
try { |
| 134 |
if (changed) setting.setInt(value); |
| 135 |
} catch (MSXException&) { |
| 136 |
|
| 137 |
} |
| 138 |
settingStuff(setting); |
| 139 |
return changed; |
| 140 |
} |
| 141 |
|
| 142 |
bool SliderFloat(FloatSetting& setting, const char* format, ImGuiSliderFlags flags) |
| 143 |
{ |
| 144 |
std::string name(setting.getBaseName()); |
| 145 |
return SliderFloat(name.c_str(), setting, format, flags); |
| 146 |
} |
| 147 |
bool SliderFloat(const char* label, FloatSetting& setting, const char* format, ImGuiSliderFlags flags) |
| 148 |
{ |
| 149 |
auto value = setting.getFloat(); |
| 150 |
auto min = narrow_cast<float>(setting.getMinValue()); |
| 151 |
auto max = narrow_cast<float>(setting.getMaxValue()); |
| 152 |
bool changed = ImGui::SliderFloat(label, &value, min, max, format, flags); |
| 153 |
try { |
| 154 |
if (changed) setting.setFloat(value); |
| 155 |
} catch (MSXException&) { |
| 156 |
|
| 157 |
} |
| 158 |
settingStuff(setting); |
| 159 |
return changed; |
| 160 |
} |
| 161 |
|
| 162 |
bool InputText(Setting& setting) |
| 163 |
{ |
| 164 |
std::string name(setting.getBaseName()); |
| 165 |
return InputText(name.c_str(), setting); |
| 166 |
} |
| 167 |
bool InputText(const char* label, Setting& setting) |
| 168 |
{ |
| 169 |
auto value = std::string(setting.getValue().getString()); |
| 170 |
bool changed = ImGui::InputText(label, &value, ImGuiInputTextFlags_EnterReturnsTrue) || ImGui::IsItemDeactivatedAfterEdit(); |
| 171 |
try { |
| 172 |
if (changed) setting.setValue(TclObject(value)); |
| 173 |
} catch (MSXException&) { |
| 174 |
|
| 175 |
} |
| 176 |
settingStuff(setting); |
| 177 |
return changed; |
| 178 |
} |
| 179 |
|
| 180 |
void ComboBox(const char* label, Setting& setting, function_ref<std::string(const std::string&)> displayValue, EnumToolTips toolTips) |
| 181 |
{ |
| 182 |
const auto* enumSetting = dynamic_cast<const EnumSettingBase*>(&setting); |
| 183 |
assert(enumSetting); |
| 184 |
auto current = setting.getValue().getString(); |
| 185 |
im::Combo(label, current.c_str(), [&]{ |
| 186 |
for (const auto& entry : enumSetting->getMap()) { |
| 187 |
bool selected = entry.name == current; |
| 188 |
if (const auto& display = displayValue(entry.name); |
| 189 |
ImGui::Selectable(display.c_str(), selected)) { |
| 190 |
try { |
| 191 |
setting.setValue(TclObject(entry.name)); |
| 192 |
} catch (MSXException&) { |
| 193 |
|
| 194 |
} |
| 195 |
} |
| 196 |
if (auto it = std::ranges::find(toolTips, entry.name, &EnumToolTip::value); |
| 197 |
it != toolTips.end()) { |
| 198 |
simpleToolTip(it->tip); |
| 199 |
} |
| 200 |
} |
| 201 |
}); |
| 202 |
settingStuff(setting); |
| 203 |
} |
| 204 |
void ComboBox(const char* label, Setting& setting, EnumToolTips toolTips) |
| 205 |
{ |
| 206 |
ComboBox(label, setting, std::identity{}, toolTips); |
| 207 |
} |
| 208 |
void ComboBox(Setting& setting, EnumToolTips toolTips) |
| 209 |
{ |
| 210 |
std::string name(setting.getBaseName()); |
| 211 |
ComboBox(name.c_str(), setting, toolTips); |
| 212 |
} |
| 213 |
|
| 214 |
void ComboBox(VideoSourceSetting& setting) |
| 215 |
{ |
| 216 |
std::string name(setting.getBaseName()); |
| 217 |
ComboBox(name.c_str(), setting); |
| 218 |
} |
| 219 |
void ComboBox(const char* label, VideoSourceSetting& setting) |
| 220 |
{ |
| 221 |
auto current = setting.getValue().getString(); |
| 222 |
im::Combo(label, current.c_str(), [&]{ |
| 223 |
for (const auto& value : setting.getPossibleValues()) { |
| 224 |
bool selected = value == current; |
| 225 |
if (ImGui::Selectable(std::string(value).c_str(), selected)) { |
| 226 |
try { |
| 227 |
setting.setValue(TclObject(value)); |
| 228 |
} catch (MSXException&) { |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
}); |
| 234 |
settingStuff(setting); |
| 235 |
} |
| 236 |
|
| 237 |
const char* getComboString(int item, const char* itemsSeparatedByZeros) |
| 238 |
{ |
| 239 |
const char* p = itemsSeparatedByZeros; |
| 240 |
while (true) { |
| 241 |
assert(*p); |
| 242 |
if (item == 0) return p; |
| 243 |
--item; |
| 244 |
while (*p) ++p; |
| 245 |
++p; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
std::string formatTime(std::optional<double> time) |
| 250 |
{ |
| 251 |
if (!time) return "--:--:--.--"; |
| 252 |
auto remainingTime = *time; |
| 253 |
assert(remainingTime >= 0.0); |
| 254 |
auto hours = int(remainingTime * (1.0 / 3600.0)); |
| 255 |
remainingTime -= double(hours * 3600); |
| 256 |
auto minutes = int(remainingTime * (1.0 / 60.0)); |
| 257 |
remainingTime -= double(minutes * 60); |
| 258 |
auto seconds = int(remainingTime); |
| 259 |
remainingTime -= double(seconds); |
| 260 |
auto hundreds = int(100.0 * remainingTime); |
| 261 |
|
| 262 |
std::string result = "00:00:00.00"; |
| 263 |
auto insert = [&](size_t pos, unsigned value) { |
| 264 |
assert(value < 100); |
| 265 |
result[pos + 0] = char('0' + (value / 10)); |
| 266 |
result[pos + 1] = char('0' + (value % 10)); |
| 267 |
}; |
| 268 |
insert(0, hours % 100); |
| 269 |
insert(3, minutes); |
| 270 |
insert(6, seconds); |
| 271 |
insert(9, hundreds); |
| 272 |
return result; |
| 273 |
} |
| 274 |
|
| 275 |
float calculateFade(float current, float target, float period) |
| 276 |
{ |
| 277 |
const auto& io = ImGui::GetIO(); |
| 278 |
auto step = io.DeltaTime / period; |
| 279 |
if (target > current) { |
| 280 |
return std::min(target, current + step); |
| 281 |
} else { |
| 282 |
return std::max(target, current - step); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
TemporaryString getShortCutForCommand(const HotKey& hotkey, std::string_view command) |
| 287 |
{ |
| 288 |
for (const auto& info : hotkey.getGlobalBindings()) { |
| 289 |
if (info.command != command) continue; |
| 290 |
if (const auto* keyDown = std::get_if<KeyDownEvent>(&info.event)) { |
| 291 |
auto modifiers = keyDown->getModifiers(); |
| 292 |
return tmpStrCat( |
| 293 |
strCat_if(modifiers & KMOD_CTRL, "CTRL+"), |
| 294 |
strCat_if(modifiers & KMOD_SHIFT, "SHIFT+"), |
| 295 |
strCat_if(modifiers & KMOD_ALT, "ALT+"), |
| 296 |
strCat_if(modifiers & KMOD_GUI, "GUI+"), |
| 297 |
SDL_GetKeyName(keyDown->getKeyCode())); |
| 298 |
} |
| 299 |
} |
| 300 |
return TemporaryString{}; |
| 301 |
} |
| 302 |
|
| 303 |
[[nodiscard]] static std::string_view superName() |
| 304 |
{ |
| 305 |
return ImGui::GetIO().ConfigMacOSXBehaviors ? "Cmd+"sv : "Super+"sv; |
| 306 |
} |
| 307 |
|
| 308 |
std::string getKeyChordName(ImGuiKeyChord keyChord) |
| 309 |
{ |
| 310 |
int keyCode = ImGuiKey2SDL(ImGuiKey(keyChord & ~ImGuiMod_Mask_)); |
| 311 |
const auto* name = SDL_GetKeyName(keyCode); |
| 312 |
if (!name || (*name == '\0')) return "None"; |
| 313 |
return strCat( |
| 314 |
(keyChord & ImGuiMod_Ctrl ? "Ctrl+"sv : ""sv), |
| 315 |
(keyChord & ImGuiMod_Shift ? "Shift+"sv : ""sv), |
| 316 |
(keyChord & ImGuiMod_Alt ? "Alt+"sv : ""sv), |
| 317 |
(keyChord & ImGuiMod_Super ? superName() : ""sv), |
| 318 |
name); |
| 319 |
} |
| 320 |
|
| 321 |
std::optional<ImGuiKeyChord> parseKeyChord(std::string_view name) |
| 322 |
{ |
| 323 |
if (name == "None") return ImGuiKey_None; |
| 324 |
|
| 325 |
|
| 326 |
auto [modifiers, key] = [&] -> std::pair<std::string_view, std::string_view> { |
| 327 |
if (auto pos = name.find_last_of('+'); pos == std::string_view::npos) { |
| 328 |
return {std::string_view{}, name}; |
| 329 |
} else { |
| 330 |
return {name.substr(0, pos + 1), name.substr(pos + 1)}; |
| 331 |
} |
| 332 |
}(); |
| 333 |
|
| 334 |
SDL_Keycode keyCode = SDL_GetKeyFromName(std::string(key).c_str()); |
| 335 |
if (keyCode == SDLK_UNKNOWN) return {}; |
| 336 |
|
| 337 |
ImGuiKeyChord keyMods = |
| 338 |
(modifiers.contains("Ctrl+" ) ? ImGuiMod_Ctrl : 0) | |
| 339 |
(modifiers.contains("Shift+") ? ImGuiMod_Shift : 0) | |
| 340 |
(modifiers.contains("Alt+" ) ? ImGuiMod_Alt : 0) | |
| 341 |
(modifiers.contains(superName()) ? ImGuiMod_Super : 0); |
| 342 |
|
| 343 |
return SDLKey2ImGui(keyCode) | keyMods; |
| 344 |
} |
| 345 |
|
| 346 |
void setColors(int style) |
| 347 |
{ |
| 348 |
|
| 349 |
bool light = style == 1; |
| 350 |
bool colorblind = style == 3; |
| 351 |
using enum imColor; |
| 352 |
|
| 353 |
|
| 354 |
imColors[size_t(TRANSPARENT )] = 0x00'00'00'00; |
| 355 |
imColors[size_t(BLACK )] = 0xff'00'00'00; |
| 356 |
imColors[size_t(WHITE )] = 0xff'ff'ff'ff; |
| 357 |
imColors[size_t(GRAY )] = 0xff'80'80'80; |
| 358 |
imColors[size_t(YELLOW )] = 0xff'00'ff'ff; |
| 359 |
imColors[size_t(RED_BG )] = 0x40'00'00'ff; |
| 360 |
imColors[size_t(YELLOW_BG )] = 0x80'00'ff'ff; |
| 361 |
|
| 362 |
imColors[size_t(TEXT )] = ImGui::GetColorU32(ImGuiCol_Text); |
| 363 |
imColors[size_t(TEXT_DISABLED )] = ImGui::GetColorU32(ImGuiCol_TextDisabled); |
| 364 |
|
| 365 |
imColors[size_t(ERROR )] = colorblind ? 0xff'00'ff'ff : 0xff'00'00'ff; |
| 366 |
imColors[size_t(WARNING )] = 0xff'33'b3'ff; |
| 367 |
|
| 368 |
imColors[size_t(COMMENT )] = 0xff'5c'ff'5c; |
| 369 |
imColors[size_t(VARIABLE )] = 0xff'ff'ff'00; |
| 370 |
imColors[size_t(LITERAL )] = light ? 0xff'9c'5d'27 : 0xff'00'ff'ff; |
| 371 |
imColors[size_t(PROC )] = colorblind ? 0xff'00'ee'aa : 0xff'cd'00'cd; |
| 372 |
|
| 373 |
imColors[size_t(OPERATOR )] = 0xff'cd'cd'00; |
| 374 |
|
| 375 |
imColors[size_t(KEY_ACTIVE )] = 0xff'10'40'ff; |
| 376 |
imColors[size_t(KEY_NOT_ACTIVE)] = 0x80'00'00'00; |
| 377 |
} |
| 378 |
|
| 379 |
std::string formatToString(function_ref<uint8_t(unsigned)> fetch, unsigned begin, unsigned end, std::string_view prefix, |
| 380 |
unsigned columns, std::string_view suffix, std::string_view separator, std::function<std::string(uint8_t)> formatFunc) |
| 381 |
{ |
| 382 |
std::string result; |
| 383 |
unsigned col = 0; |
| 384 |
for (unsigned addr = begin; addr <= end; ++addr) { |
| 385 |
if (col == 0) strAppend(result, prefix); |
| 386 |
|
| 387 |
auto val = fetch(addr); |
| 388 |
strAppend(result, formatFunc(val)); |
| 389 |
|
| 390 |
if (++col == columns) { |
| 391 |
col = 0; |
| 392 |
strAppend(result, suffix, '\n'); |
| 393 |
} else if (addr != end) { |
| 394 |
strAppend(result, separator); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
if (col != 0) { |
| 399 |
strAppend(result, suffix, '\n'); |
| 400 |
} |
| 401 |
return result; |
| 402 |
} |
| 403 |
|
| 404 |
std::string formatToString(function_ref<uint8_t(unsigned)> fetch, unsigned begin, unsigned end, std::string_view prefix, |
| 405 |
unsigned columns, std::string_view suffix, std::string_view separator, std::string_view formatStr, Interpreter& interp) |
| 406 |
{ |
| 407 |
return formatToString(fetch, begin, end, prefix, columns, suffix, separator, [formatStr, &interp] (uint8_t val) { |
| 408 |
auto cmd = makeTclList("format", formatStr, val); |
| 409 |
auto formatted = cmd.executeCommand(interp); |
| 410 |
return std::string(formatted.getString()); |
| 411 |
}); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
[[nodiscard]] std::string rawToString( |
| 416 |
function_ref<uint8_t(unsigned)> fetch, |
| 417 |
unsigned begin, unsigned end) |
| 418 |
{ |
| 419 |
std::string result; |
| 420 |
result.reserve(end - begin + 1); |
| 421 |
for (unsigned addr = begin; addr <= end; ++addr) { |
| 422 |
auto val = fetch(addr); |
| 423 |
result += static_cast<char>(val); |
| 424 |
} |
| 425 |
return result; |
| 426 |
} |
| 427 |
|
| 428 |
} |