48#include <imgui_stdlib.h>
55using namespace std::literals;
79void ImGuiSettings::setStyle()
const
81 switch (selectedStyle) {
82 case 0: ImGui::StyleColorsDark();
break;
83 case 1: ImGui::StyleColorsLight();
break;
84 case 2: ImGui::StyleColorsClassic();
break;
93[[nodiscard]]
static ImGuiKeyChord getCurrentlyPressedKeyChord()
95 static constexpr auto mods = std::array{
96 ImGuiKey_LeftCtrl, ImGuiKey_LeftShift, ImGuiKey_LeftAlt, ImGuiKey_LeftSuper,
97 ImGuiKey_RightCtrl, ImGuiKey_RightShift, ImGuiKey_RightAlt, ImGuiKey_RightSuper,
98 ImGuiKey_ReservedForModCtrl, ImGuiKey_ReservedForModShift, ImGuiKey_ReservedForModAlt,
99 ImGuiKey_ReservedForModSuper, ImGuiKey_MouseLeft, ImGuiKey_MouseRight, ImGuiKey_MouseMiddle,
100 ImGuiKey_MouseX1, ImGuiKey_MouseX2, ImGuiKey_MouseWheelX, ImGuiKey_MouseWheelY,
102 for (
int key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; ++key) {
105 if (ImGui::IsKeyPressed(
static_cast<ImGuiKey
>(key))) {
106 const ImGuiIO& io = ImGui::GetIO();
108 | (io.KeyCtrl ? ImGuiMod_Ctrl : 0)
109 | (io.KeyShift ? ImGuiMod_Shift : 0)
110 | (io.KeyAlt ? ImGuiMod_Alt : 0)
111 | (io.KeySuper ? ImGuiMod_Super : 0);
114 return ImGuiKey_None;
119 bool openConfirmPopup =
false;
124 auto& renderSettings = reactor.getDisplay().getRenderSettings();
125 const auto& settingsManager = reactor.getGlobalCommandController().getSettingsManager();
126 const auto& hotKey = reactor.getHotKey();
129 im::TreeNode(
"Look and feel", ImGuiTreeNodeFlags_DefaultOpen, [&]{
130 auto& scaler = renderSettings.getScaleAlgorithmSetting();
139 static constexpr std::array algoEnables = {
141 AlgoEnable{SIMPLE,
true,
true },
142 AlgoEnable{SCALE,
false,
false},
143 AlgoEnable{HQ,
false,
false},
144 AlgoEnable{RGBTRIPLET,
true,
true },
145 AlgoEnable{TV,
true,
false},
147 auto it =
ranges::find(algoEnables, scaler.getEnum(), &AlgoEnable::algo);
148 assert(it != algoEnables.end());
150 SliderInt(
"Scanline (%)", renderSettings.getScanlineSetting());
153 SliderInt(
"Blur (%)", renderSettings.getBlurSetting());
157 SliderInt(
"Scale factor", renderSettings.getScaleFactorSetting());
158 Checkbox(hotKey,
"Deinterlace", renderSettings.getDeinterlaceSetting());
159 Checkbox(hotKey,
"Deflicker", renderSettings.getDeflickerSetting());
161 im::TreeNode(
"Colors", ImGuiTreeNodeFlags_DefaultOpen, [&]{
162 SliderFloat(
"Noise (%)", renderSettings.getNoiseSetting());
163 SliderFloat(
"Brightness", renderSettings.getBrightnessSetting());
164 SliderFloat(
"Contrast", renderSettings.getContrastSetting());
165 SliderFloat(
"Gamma", renderSettings.getGammaSetting());
166 SliderInt(
"Glow (%)", renderSettings.getGlowSetting());
167 if (
auto* monitor =
dynamic_cast<Setting*
>(settingsManager.findSetting(
"monitor_type"))) {
168 ComboBox(
"Monitor type", *monitor, [](std::string s) {
174 im::TreeNode(
"Shape", ImGuiTreeNodeFlags_DefaultOpen, [&]{
175 SliderFloat(
"Horizontal stretch", renderSettings.getHorizontalStretchSetting(),
"%.0f");
176 ComboBox(
"Display deformation", renderSettings.getDisplayDeformSetting());
178 im::TreeNode(
"Misc", ImGuiTreeNodeFlags_DefaultOpen, [&]{
179 Checkbox(hotKey,
"Full screen", renderSettings.getFullScreenSetting());
183 Checkbox(hotKey,
"VSync", renderSettings.getVSyncSetting());
184 SliderInt(
"Minimum frame-skip", renderSettings.getMinFrameSkipSetting());
185 SliderInt(
"Maximum frame-skip", renderSettings.getMaxFrameSkipSetting());
188 Checkbox(hotKey,
"Enforce VDP sprites-per-line limit", renderSettings.getLimitSpritesSetting());
189 Checkbox(hotKey,
"Disable sprites", renderSettings.getDisableSpritesSetting());
190 ComboBox(
"Way to handle too fast VDP access", renderSettings.getTooFastAccessSetting());
191 ComboBox(
"Emulate VDP command timing", renderSettings.getCmdTimingSetting());
192 ComboBox(
"Rendering accuracy", renderSettings.getAccuracySetting());
196 auto& mixer = reactor.getMixer();
197 auto& muteSetting = mixer.getMuteSetting();
199 SliderInt(
"Master volume", mixer.getMasterVolume());
201 Checkbox(hotKey,
"Mute", muteSetting);
203 static constexpr std::array resamplerToolTips = {
205 EnumToolTip{
"blip",
"good speed/quality tradeoff"},
208 ComboBox(
"Resampler", globalSettings.getResampleSetting(), resamplerToolTips);
211 ImGui::MenuItem(
"Show sound chip settings",
nullptr, &
manager.
soundChip->showSoundChipSettings);
214 im::TreeNode(
"Emulation", ImGuiTreeNodeFlags_DefaultOpen, [&]{
216 HelpMarker(
"These control the speed of the whole MSX machine, "
217 "the running MSX software can't tell the difference.");
219 auto& speedManager = globalSettings.getSpeedManager();
220 auto& fwdSetting = speedManager.getFastForwardSetting();
221 int fastForward = fwdSetting.getBoolean() ? 1 : 0;
224 bool fwdChanged = ImGui::RadioButton(
"normal", &fastForward, 0);
226 fwdChanged |= ImGui::RadioButton(
"fast forward", &fastForward, 1);
228 !fastForwardShortCut.empty()) {
229 HelpMarker(
strCat(
"Use '", fastForwardShortCut ,
"' to quickly toggle between these two"));
232 fwdSetting.setBoolean(fastForward != 0);
236 SliderFloat(
"Speed (%)", speedManager.getSpeedSetting(),
"%.1f", ImGuiSliderFlags_Logarithmic);
239 SliderFloat(
"Fast forward speed (%)", speedManager.getFastForwardSpeedSetting(),
"%.1f", ImGuiSliderFlags_Logarithmic);
242 Checkbox(hotKey,
"Go full speed when loading", globalSettings.getThrottleManager().getFullSpeedLoadingSetting());
245 im::TreeNode(
"MSX devices", ImGuiTreeNodeFlags_DefaultOpen, [&]{
247 HelpMarker(
"These control the speed of the specific components in the MSX machine. "
248 "So the relative speed between components can change. "
249 "And this may lead the emulation problems.");
252 auto showFreqSettings = [&](std::string_view name,
auto* core) {
254 auto& locked = core->getFreqLockedSetting();
255 auto& value = core->getFreqValueSetting();
257 bool unlocked = !locked.getBoolean();
258 if (ImGui::Checkbox(
tmpStrCat(
"unlock custom ", name,
" frequency").c_str(), &unlocked)) {
259 locked.setBoolean(!unlocked);
264 float fval = float(value.getInt()) / 1.0e6f;
265 if (ImGui::InputFloat(
tmpStrCat(
"frequency (MHz)##", name).c_str(), &fval, 0.01f, 1.0f,
"%.2f")) {
266 value.setInt(
int(fval * 1.0e6f));
269 const char* F358 = name ==
"Z80" ?
"3.58 MHz (default)"
271 if (ImGui::Selectable(F358)) {
272 value.setInt(3'579'545);
274 if (ImGui::Selectable(
"5.37 MHz")) {
275 value.setInt(5'369'318);
277 const char* F716 = name ==
"R800" ?
"7.16 MHz (default)"
279 if (ImGui::Selectable(F716)) {
280 value.setInt(7'159'090);
284 HelpMarker(
"Right-click to select commonly used values");
288 showFreqSettings(
"Z80", cpu.
getZ80());
289 showFreqSettings(
"R800", cpu.
getR800());
294 static constexpr std::array kbdModeToolTips = {
295 EnumToolTip{
"CHARACTER",
"Tries to understand the character you are typing and then attempts to type that character using the current MSX keyboard. May not work very well when using a non-US host keyboard."},
296 EnumToolTip{
"KEY",
"Tries to map a key you press to the corresponding MSX key"},
297 EnumToolTip{
"POSITIONAL",
"Tries to map the keyboard key positions to the MSX keyboard key positions"},
301 if (
auto* turbo =
dynamic_cast<IntegerSetting*
>(controller.findSetting(
"renshaturbo"))) {
302 SliderInt(
"Ren Sha Turbo (%)", *turbo);
305 ComboBox(
"Keyboard mapping mode", *mappingModeSetting, kbdModeToolTips);
308 ImGui::MenuItem(
"Configure MSX joysticks...",
nullptr, &showConfigureJoystick);
311 auto getExistingLayouts = [] {
312 std::vector<std::string> names;
314 const auto& path : context.getPaths()) {
315 foreach_file(path, [&](
const std::string& fullName, std::string_view name) {
316 if (name.ends_with(
".ini")) {
317 names.emplace_back(fullName);
324 auto listExistingLayouts = [&](
const std::vector<std::string>& names) {
325 std::optional<std::pair<std::string, std::string>> selectedLayout;
327 for (
const auto& name : names) {
329 if (ImGui::Selectable(displayName.c_str())) {
330 selectedLayout = std::pair{name, displayName};
333 if (ImGui::MenuItem(
"delete")) {
334 confirmText =
strCat(
"Delete layout: ", displayName);
336 openConfirmPopup =
true;
341 return selectedLayout;
344 if (
auto names = getExistingLayouts(); !names.empty()) {
346 if (
auto selectedLayout = listExistingLayouts(names)) {
347 const auto& [name, displayName] = *selectedLayout;
348 saveLayoutName = displayName;
352 ImGui::InputText(
"##save-layout-name", &saveLayoutName);
355 if (ImGui::Button(
"Save as")) {
356 (void)reactor.getDisplay().getWindowPosition();
357 ImGui::CloseCurrentPopup();
359 auto filename = FileOperations::parseCommandFileArgument(
360 saveLayoutName,
"layouts",
"",
".ini");
361 if (FileOperations::exists(filename)) {
362 confirmText = strCat(
"Overwrite layout: ", saveLayoutName);
363 confirmAction = [filename]{
364 ImGui::SaveIniSettingsToDisk(filename.c_str());
366 openConfirmPopup = true;
368 ImGui::SaveIniSettingsToDisk(filename.c_str());
375 auto names = getExistingLayouts();
376 if (
auto selectedLayout = listExistingLayouts(names)) {
377 const auto& [name, displayName] = *selectedLayout;
379 ImGui::CloseCurrentPopup();
384 std::optional<int> newStyle;
385 static constexpr std::array names = {
"Dark",
"Light",
"Classic"};
386 for (
auto i :
xrange(narrow<int>(names.size()))) {
387 if (ImGui::Selectable(names[i], selectedStyle == i)) {
392 selectedStyle = *newStyle;
396 ImGui::MenuItem(
"Select font...",
nullptr, &showFont);
397 ImGui::MenuItem(
"Edit shortcuts...",
nullptr, &showShortcut);
398 ImGui::MenuItem(
"Configure OSD icons...",
nullptr, &
manager.
osdIcons->showConfigureIcons);
403 manager.configStatusBarVisibilityItems();
406 ImGui::MenuItem(
"Configure messages...",
nullptr, &
manager.
messages->configureWindow.open);
412 std::vector<Setting*> settings;
413 for (
auto*
setting : settingsManager.getAllSettings()) {
416 settings.push_back(checked_cast<Setting*>(
setting));
419 for (
auto*
setting : settings) {
443 const auto confirmTitle =
"Confirm##settings";
444 if (openConfirmPopup) {
445 ImGui::OpenPopup(confirmTitle);
447 im::PopupModal(confirmTitle,
nullptr, ImGuiWindowFlags_AlwaysAutoResize, [&]{
451 if (ImGui::Button(
"Ok")) {
456 close |= ImGui::Button(
"Cancel");
458 ImGui::CloseCurrentPopup();
467[[nodiscard]]
static std::string settingName(
unsigned joystick)
469 return (joystick < 2) ?
strCat(
"msxjoystick", joystick + 1,
"_config")
470 :
strCat(
"joymega", joystick - 1,
"_config");
474[[nodiscard]]
static std::string joystickToGuiString(
unsigned joystick)
476 return (joystick < 2) ?
strCat(
"MSX joystick ", joystick + 1)
477 :
strCat(
"JoyMega controller ", joystick - 1);
480[[nodiscard]]
static std::string toGuiString(
const BooleanInput& input,
const JoystickManager& joystickManager)
483 [](
const BooleanKeyboard& k) {
484 return strCat(
"keyboard key ", SDLKey::toString(k.getKeyCode()));
486 [](
const BooleanMouseButton& m) {
487 return strCat(
"mouse button ", m.getButton());
489 [&](
const BooleanJoystickButton& j) {
490 return strCat(joystickManager.getDisplayName(j.getJoystick()),
" button ", j.getButton());
492 [&](
const BooleanJoystickHat& h) {
493 return strCat(joystickManager.getDisplayName(h.getJoystick()),
" D-pad ", h.getHat(),
' ',
toString(h.getValue()));
495 [&](
const BooleanJoystickAxis& a) {
496 return strCat(joystickManager.getDisplayName(a.getJoystick()),
497 " stick axis ", a.getAxis(),
", ",
498 (a.getDirection() == BooleanJoystickAxis::Direction::POS ?
"positive" :
"negative"),
" direction");
503[[nodiscard]]
static bool insideCircle(
gl::vec2 mouse,
gl::vec2 center,
float radius)
505 auto delta = center - mouse;
506 return gl::sum(delta * delta) <= (radius * radius);
508[[nodiscard]]
static bool between(
float x,
float min,
float max)
510 return (min <= x) && (x <=
max);
519 return between(mouse.x, r.topLeft.x, r.bottomRight.x) &&
520 between(mouse.y, r.topLeft.y, r.bottomRight.y);
524static constexpr auto fractionDPad = 1.0f / 3.0f;
525static constexpr auto thickness = 3.0f;
527static void drawDPad(
gl::vec2 center,
float size, std::span<const uint8_t, 4> hovered,
int hoveredRow)
529 const auto F = fractionDPad;
530 std::array<std::array<ImVec2, 5 + 1>, 4> points = {
531 std::array<ImVec2, 5 + 1>{
539 std::array<ImVec2, 5 + 1>{
547 std::array<ImVec2, 5 + 1>{
555 std::array<ImVec2, 5 + 1>{
565 auto* drawList = ImGui::GetWindowDrawList();
566 auto hoverColor = ImGui::GetColorU32(ImGuiCol_ButtonHovered);
568 auto color =
getColor(imColor::TEXT);
569 for (
auto i :
xrange(4)) {
570 if (hovered[i] || (hoveredRow == i)) {
571 drawList->AddConvexPolyFilled(points[i].data(), 5, hoverColor);
573 drawList->AddPolyline(points[i].data(), 5 + 1, color, 0, thickness);
577static void drawFilledCircle(
gl::vec2 center,
float radius,
bool fill)
579 auto* drawList = ImGui::GetWindowDrawList();
581 auto hoverColor = ImGui::GetColorU32(ImGuiCol_ButtonHovered);
582 drawList->AddCircleFilled(center, radius, hoverColor);
584 auto color =
getColor(imColor::TEXT);
585 drawList->AddCircle(center, radius, color, 0, thickness);
587static void drawFilledRectangle(Rectangle r,
float corner,
bool fill)
589 auto* drawList = ImGui::GetWindowDrawList();
591 auto hoverColor = ImGui::GetColorU32(ImGuiCol_ButtonHovered);
592 drawList->AddRectFilled(r.topLeft, r.bottomRight, hoverColor, corner);
594 auto color =
getColor(imColor::TEXT);
595 drawList->AddRect(r.topLeft, r.bottomRight, color, corner, 0, thickness);
598static void drawLetterA(
gl::vec2 center)
600 auto* drawList = ImGui::GetWindowDrawList();
601 auto tr = [&](
gl::vec2 p) {
return center + p; };
602 const std::array<ImVec2, 3> lines = { tr({-6, 7}), tr({0, -7}), tr({6, 7}) };
603 auto color =
getColor(imColor::TEXT);
604 drawList->AddPolyline(lines.data(), narrow<int>(lines.size()), color, 0, thickness);
605 drawList->AddLine(tr({-3, 1}), tr({3, 1}), color, thickness);
607static void drawLetterB(
gl::vec2 center)
609 auto* drawList = ImGui::GetWindowDrawList();
610 auto tr = [&](
gl::vec2 p) {
return center + p; };
611 const std::array<ImVec2, 4> lines = { tr({1, -7}), tr({-4, -7}), tr({-4, 7}), tr({2, 7}) };
612 auto color =
getColor(imColor::TEXT);
613 drawList->AddPolyline(lines.data(), narrow<int>(lines.size()), color, 0, thickness);
614 drawList->AddLine(tr({-4, -1}), tr({2, -1}), color, thickness);
615 drawList->AddBezierQuadratic(tr({1, -7}), tr({4, -7}), tr({4, -4}), color, thickness);
616 drawList->AddBezierQuadratic(tr({4, -4}), tr({4, -1}), tr({1, -1}), color, thickness);
617 drawList->AddBezierQuadratic(tr({2, -1}), tr({6, -1}), tr({6, 3}), color, thickness);
618 drawList->AddBezierQuadratic(tr({6, 3}), tr({6, 7}), tr({2, 7}), color, thickness);
620static void drawLetterC(
gl::vec2 center)
622 auto* drawList = ImGui::GetWindowDrawList();
623 auto tr = [&](
gl::vec2 p) {
return center + p; };
624 auto color =
getColor(imColor::TEXT);
625 drawList->AddBezierCubic(tr({5, -5}), tr({-8, -16}), tr({-8, 16}), tr({5, 5}), color, thickness);
627static void drawLetterX(
gl::vec2 center)
629 auto* drawList = ImGui::GetWindowDrawList();
630 auto tr = [&](
gl::vec2 p) {
return center + p; };
631 auto color =
getColor(imColor::TEXT);
632 drawList->AddLine(tr({-4, -6}), tr({4, 6}), color, thickness);
633 drawList->AddLine(tr({-4, 6}), tr({4, -6}), color, thickness);
635static void drawLetterY(
gl::vec2 center)
637 auto* drawList = ImGui::GetWindowDrawList();
638 auto tr = [&](
gl::vec2 p) {
return center + p; };
639 auto color =
getColor(imColor::TEXT);
640 drawList->AddLine(tr({-4, -6}), tr({0, 0}), color, thickness);
641 drawList->AddLine(tr({-4, 6}), tr({4, -6}), color, thickness);
643static void drawLetterZ(
gl::vec2 center)
645 auto* drawList = ImGui::GetWindowDrawList();
646 auto tr = [&](
gl::vec2 p) {
return center + p; };
647 const std::array<ImVec2, 4> linesZ2 = { tr({-4, -6}), tr({4, -6}), tr({-4, 6}), tr({4, 6}) };
648 auto color =
getColor(imColor::TEXT);
649 drawList->AddPolyline(linesZ2.data(), 4, color, 0, thickness);
652namespace msxjoystick {
656static constexpr std::array<zstring_view, NUM_BUTTONS> buttonNames = {
657 "Up",
"Down",
"Left",
"Right",
"A",
"B"
659static constexpr std::array<zstring_view, NUM_BUTTONS> keyNames = {
660 "UP",
"DOWN",
"LEFT",
"RIGHT",
"A",
"B"
664static constexpr auto boundingBox =
gl::vec2{300.0f, 100.0f};
665static constexpr auto radius = 20.0f;
666static constexpr auto corner = 10.0f;
667static constexpr auto centerA =
gl::vec2{200.0f, 50.0f};
668static constexpr auto centerB =
gl::vec2{260.0f, 50.0f};
669static constexpr auto centerDPad =
gl::vec2{50.0f, 50.0f};
670static constexpr auto sizeDPad = 30.0f;
672[[nodiscard]]
static std::vector<uint8_t> buttonsHovered(
gl::vec2 mouse)
674 std::vector<uint8_t> result(NUM_BUTTONS);
675 auto mouseDPad = (mouse - centerDPad) * (1.0f / sizeDPad);
676 if (insideRectangle(mouseDPad, Rectangle{{-1, -1}, {1, 1}}) &&
677 (between(mouseDPad.x, -fractionDPad, fractionDPad) ||
678 between(mouseDPad.y, -fractionDPad, fractionDPad))) {
679 bool t1 = mouseDPad.x < mouseDPad.y;
680 bool t2 = mouseDPad.x < -mouseDPad.y;
681 result[
UP] = !t1 && t2;
682 result[
DOWN] = t1 && !t2;
683 result[
LEFT] = t1 && t2;
684 result[
RIGHT] = !t1 && !t2;
686 result[
TRIG_A] = insideCircle(mouse, centerA, radius);
687 result[
TRIG_B] = insideCircle(mouse, centerB, radius);
691static void draw(
gl::vec2 scrnPos, std::span<uint8_t> hovered,
int hoveredRow)
693 auto* drawList = ImGui::GetWindowDrawList();
695 auto color =
getColor(imColor::TEXT);
696 drawList->AddRect(scrnPos, scrnPos + boundingBox, color, corner, 0, thickness);
698 drawDPad(scrnPos + centerDPad, sizeDPad, subspan<4>(hovered), hoveredRow);
700 auto scrnCenterA = scrnPos + centerA;
701 drawFilledCircle(scrnCenterA, radius, hovered[TRIG_A] || (hoveredRow == TRIG_A));
702 drawLetterA(scrnCenterA);
704 auto scrnCenterB = scrnPos + centerB;
705 drawFilledCircle(scrnCenterB, radius, hovered[TRIG_B] || (hoveredRow == TRIG_B));
706 drawLetterB(scrnCenterB);
713enum {UP, DOWN, LEFT, RIGHT,
719static constexpr std::array<zstring_view, NUM_BUTTONS> buttonNames = {
720 "Up",
"Down",
"Left",
"Right",
725static constexpr std::array<zstring_view, NUM_BUTTONS> keyNames = {
726 "UP",
"DOWN",
"LEFT",
"RIGHT",
733static constexpr auto thickness = 3.0f;
734static constexpr auto boundingBox =
gl::vec2{300.0f, 158.0f};
735static constexpr auto centerA =
gl::vec2{205.0f, 109.9f};
736static constexpr auto centerB =
gl::vec2{235.9f, 93.5f};
737static constexpr auto centerC =
gl::vec2{269.7f, 83.9f};
738static constexpr auto centerX =
gl::vec2{194.8f, 75.2f};
739static constexpr auto centerY =
gl::vec2{223.0f, 61.3f};
740static constexpr auto centerZ =
gl::vec2{252.2f, 52.9f};
741static constexpr auto selectBox = Rectangle{
gl::vec2{130.0f, 60.0f},
gl::vec2{160.0f, 70.0f}};
742static constexpr auto startBox = Rectangle{
gl::vec2{130.0f, 86.0f},
gl::vec2{160.0f, 96.0f}};
743static constexpr auto radiusABC = 16.2f;
744static constexpr auto radiusXYZ = 12.2f;
745static constexpr auto centerDPad =
gl::vec2{65.6f, 82.7f};
746static constexpr auto sizeDPad = 34.0f;
747static constexpr auto fractionDPad = 1.0f / 3.0f;
749[[nodiscard]]
static std::vector<uint8_t> buttonsHovered(
gl::vec2 mouse)
751 std::vector<uint8_t> result(NUM_BUTTONS);
752 auto mouseDPad = (mouse - centerDPad) * (1.0f / sizeDPad);
753 if (insideRectangle(mouseDPad, Rectangle{{-1, -1}, {1, 1}}) &&
754 (between(mouseDPad.x, -fractionDPad, fractionDPad) ||
755 between(mouseDPad.y, -fractionDPad, fractionDPad))) {
756 bool t1 = mouseDPad.x < mouseDPad.y;
757 bool t2 = mouseDPad.x < -mouseDPad.y;
758 result[
UP] = !t1 && t2;
759 result[
DOWN] = t1 && !t2;
760 result[
LEFT] = t1 && t2;
761 result[
RIGHT] = !t1 && !t2;
763 result[
TRIG_A] = insideCircle(mouse, centerA, radiusABC);
764 result[
TRIG_B] = insideCircle(mouse, centerB, radiusABC);
765 result[
TRIG_C] = insideCircle(mouse, centerC, radiusABC);
766 result[
TRIG_X] = insideCircle(mouse, centerX, radiusXYZ);
767 result[
TRIG_Y] = insideCircle(mouse, centerY, radiusXYZ);
768 result[
TRIG_Z] = insideCircle(mouse, centerZ, radiusXYZ);
769 result[
TRIG_START] = insideRectangle(mouse, startBox);
770 result[
TRIG_SELECT] = insideRectangle(mouse, selectBox);
774static void draw(
gl::vec2 scrnPos, std::span<uint8_t> hovered,
int hoveredRow)
776 auto* drawList = ImGui::GetWindowDrawList();
777 auto tr = [&](
gl::vec2 p) {
return scrnPos + p; };
778 auto color =
getColor(imColor::TEXT);
780 auto drawBezierCurve = [&](std::span<const gl::vec2> points,
float thick = 1.0f) {
781 assert((points.size() % 2) == 0);
782 for (
size_t i = 0; i < points.size() - 2; i += 2) {
783 auto ap = points[i + 0];
784 auto ad = points[i + 1];
785 auto bp = points[i + 2];
786 auto bd = points[i + 3];
787 drawList->AddBezierCubic(tr(ap), tr(ap + ad), tr(bp - bd), tr(bp), color, thick);
791 std::array outLine = {
806 drawBezierCurve(outLine, thickness);
808 drawDPad(tr(centerDPad), sizeDPad, subspan<4>(hovered), hoveredRow);
809 drawList->AddCircle(tr(centerDPad), 43.0f, color);
810 std::array dPadCurve = {
815 drawBezierCurve(dPadCurve);
817 drawFilledCircle(tr(centerA), radiusABC, hovered[TRIG_A] || (hoveredRow == TRIG_A));
818 drawLetterA(tr(centerA));
819 drawFilledCircle(tr(centerB), radiusABC, hovered[TRIG_B] || (hoveredRow == TRIG_B));
820 drawLetterB(tr(centerB));
821 drawFilledCircle(tr(centerC), radiusABC, hovered[TRIG_C] || (hoveredRow == TRIG_C));
822 drawLetterC(tr(centerC));
823 drawFilledCircle(tr(centerX), radiusXYZ, hovered[TRIG_X] || (hoveredRow == TRIG_X));
824 drawLetterX(tr(centerX));
825 drawFilledCircle(tr(centerY), radiusXYZ, hovered[TRIG_Y] || (hoveredRow == TRIG_Y));
826 drawLetterY(tr(centerY));
827 drawFilledCircle(tr(centerZ), radiusXYZ, hovered[TRIG_Z] || (hoveredRow == TRIG_Z));
828 drawLetterZ(tr(centerZ));
829 std::array buttonCurve = {
834 drawBezierCurve(buttonCurve);
836 auto corner = (selectBox.bottomRight.y - selectBox.topLeft.y) * 0.5f;
837 auto trR = [&](Rectangle r) {
return Rectangle{tr(r.topLeft), tr(r.bottomRight)}; };
838 drawFilledRectangle(trR(selectBox), corner, hovered[TRIG_SELECT] || (hoveredRow == TRIG_SELECT));
839 drawList->AddText(ImGui::GetFont(), ImGui::GetFontSize(), tr({123.0f, 46.0f}), color,
"Select");
840 drawFilledRectangle(trR(startBox), corner, hovered[TRIG_START] || (hoveredRow == TRIG_START));
841 drawList->AddText(ImGui::GetFont(), ImGui::GetFontSize(), tr({128.0f, 97.0f}), color,
"Start");
846void ImGuiSettings::paintJoystick(MSXMotherBoard& motherBoard)
848 ImGui::SetNextWindowSize(
gl::vec2{316, 323}, ImGuiCond_FirstUseEver);
849 im::Window(
"Configure MSX joysticks", &showConfigureJoystick, [&]{
850 ImGui::SetNextItemWidth(13.0f * ImGui::GetFontSize());
851 im::Combo(
"Select joystick", joystickToGuiString(joystick).c_str(), [&]{
852 for (
const auto& j :
xrange(4)) {
853 if (ImGui::Selectable(joystickToGuiString(j).c_str())) {
859 const auto& joystickManager = manager.getReactor().getInputEventGenerator().getJoystickManager();
860 const auto& controller = motherBoard.getMSXCommandController();
861 auto*
setting =
dynamic_cast<StringSetting*
>(controller.findSetting(settingName(joystick)));
863 auto& interp =
setting->getInterpreter();
864 TclObject bindings =
setting->getValue();
866 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
870 bool msxOrMega = joystick < 2;
871 auto hovered = msxOrMega ? msxjoystick::buttonsHovered(mouse)
872 : joymega ::buttonsHovered(mouse);
873 const auto numButtons = hovered.size();
874 using SP = std::span<const zstring_view>;
875 auto keyNames = msxOrMega ?
SP{msxjoystick::keyNames}
876 :
SP{joymega ::keyNames};
877 auto buttonNames = msxOrMega ?
SP{msxjoystick::buttonNames}
878 :
SP{joymega ::buttonNames};
881 std::optional<int> addAction;
882 std::optional<int> removeAction;
883 if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
884 for (
auto i :
xrange(numButtons)) {
885 if (hovered[i]) addAction = narrow<int>(i);
889 ImGui::Dummy(msxOrMega ? msxjoystick::boundingBox : joymega::boundingBox);
893 const auto& style = ImGui::GetStyle();
894 auto textHeight = ImGui::GetTextLineHeight();
895 float rowHeight = 2.0f * style.FramePadding.y + textHeight;
896 float bottomHeight = style.ItemSpacing.y + 2.0f * style.FramePadding.y + textHeight;
897 im::Table(
"##joystick-table", 2, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollX, {0.0f, -bottomHeight}, [&]{
899 TclObject key(keyNames[i]);
900 TclObject bindingList = bindings.getDictValue(interp, key);
901 if (ImGui::TableNextColumn()) {
902 auto pos = ImGui::GetCursorPos();
903 ImGui::Selectable(
"##row", hovered[i], ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap, ImVec2(0, rowHeight));
904 if (ImGui::IsItemHovered()) {
908 ImGui::SetCursorPos(pos);
909 ImGui::AlignTextToFramePadding();
912 if (ImGui::TableNextColumn()) {
913 if (ImGui::Button(
"Add")) {
917 auto numBindings = bindingList.size();
919 if (ImGui::Button(
"Remove")) {
920 if (numBindings == 1) {
921 bindings.setDictValue(interp, key, TclObject{});
929 if (numBindings == 0) {
930 ImGui::TextDisabled(
"no bindings");
932 size_t lastBindingIndex = numBindings - 1;
933 size_t bindingIndex = 0;
934 for (
auto binding: bindingList) {
937 if (bindingIndex < lastBindingIndex) {
948 msxOrMega ? msxjoystick::draw(scrnPos, hovered, hoveredRow)
949 : joymega ::draw(scrnPos, hovered, hoveredRow);
951 if (ImGui::Button(
"Default bindings...")) {
952 ImGui::OpenPopup(
"bindings");
955 auto addOrSet = [&](
auto getBindings) {
956 if (ImGui::MenuItem(
"Add to current bindings")) {
958 auto newBindings = getBindings();
959 for (
auto k :
xrange(int(numButtons))) {
960 TclObject key(keyNames[k]);
961 TclObject dstList = bindings.getDictValue(interp, key);
962 TclObject srcList = newBindings.getDictValue(interp, key);
964 for (
auto b : srcList) {
966 dstList.addListElement(b);
969 bindings.setDictValue(interp, key, dstList);
973 if (ImGui::MenuItem(
"Replace current bindings")) {
974 setting->setValue(getBindings());
979 return TclObject(TclObject::MakeDictTag{},
988 for (
auto joyId : joystickManager.getConnectedJoysticks()) {
989 im::Menu(joystickManager.getDisplayName(joyId).c_str(), [&]{
992 ? MSXJoystick::getDefaultConfig(joyId, joystickManager)
993 : JoyMega::getDefaultConfig(joyId, joystickManager);
1000 static constexpr auto addTitle =
"Waiting for input";
1002 popupForKey = *addAction;
1003 popupTimeout = 5.0f;
1005 ImGui::OpenPopup(addTitle);
1007 im::PopupModal(addTitle,
nullptr, ImGuiWindowFlags_NoSavedSettings, [&]{
1009 ImGui::CloseCurrentPopup();
1010 popupForKey = unsigned(-1);
1013 if (popupForKey >= numButtons) {
1018 ImGui::Text(
"Enter event for joystick button '%s'", buttonNames[popupForKey].c_str());
1019 ImGui::Text(
"Or press ESC to cancel. Timeout in %d seconds.",
int(popupTimeout));
1021 popupTimeout -= ImGui::GetIO().DeltaTime;
1022 if (popupTimeout <= 0.0f) {
1029 popupForKey = *removeAction;
1030 ImGui::OpenPopup(
"remove");
1034 ImGui::CloseCurrentPopup();
1035 popupForKey = unsigned(-1);
1037 if (popupForKey >= numButtons) {
1041 TclObject key(keyNames[popupForKey]);
1042 TclObject bindingList = bindings.getDictValue(interp, key);
1044 unsigned remove = unsigned(-1);
1045 unsigned counter = 0;
1046 for (
const auto& b : bindingList) {
1047 if (ImGui::Selectable(b.c_str())) {
1053 if (remove !=
unsigned(-1)) {
1054 bindingList.removeListIndex(interp, remove);
1055 bindings.setDictValue(interp, key, bindingList);
1060 if (ImGui::Selectable(
"all bindings")) {
1061 bindings.setDictValue(interp, key, TclObject{});
1069void ImGuiSettings::paintFont()
1072 auto selectFilename = [&](FilenameSetting&
setting,
float width) {
1073 auto display = [](std::string_view name) {
1074 if (name.ends_with(
".gz" )) name.remove_suffix(3);
1075 if (name.ends_with(
".ttf")) name.remove_suffix(4);
1076 return std::string(name);
1078 auto current =
setting.getString();
1079 ImGui::SetNextItemWidth(width);
1081 for (
const auto& font : getAvailableFonts()) {
1082 if (ImGui::Selectable(display(font).c_str(), current == font)) {
1088 auto selectSize = [](IntegerSetting&
setting) {
1089 auto display = [](
int s) {
return strCat(s); };
1090 auto current =
setting.getInt();
1091 ImGui::SetNextItemWidth(4.0f * ImGui::GetFontSize());
1093 for (
int size : {9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32}) {
1094 if (ImGui::Selectable(display(size).c_str(), current == size)) {
1102 auto width = 12.0f * ImGui::GetFontSize();
1104 ImGui::AlignTextToFramePadding();
1106 ImGui::SameLine(pos);
1107 selectFilename(manager.fontPropFilename, width);
1109 selectSize(manager.fontPropSize);
1110 HelpMarker(
"You can install more fonts by copying .ttf file(s) to your \"<openmsx>/share/skins\" directory.");
1112 ImGui::AlignTextToFramePadding();
1114 ImGui::SameLine(pos);
1116 selectFilename(manager.fontMonoFilename, width);
1119 selectSize(manager.fontMonoSize);
1120 HelpMarker(
"Some GUI elements (e.g. the console) require a monospaced font.");
1124[[nodiscard]]
static std::string formatShortcutWithAnnotations(
const Shortcuts::Shortcut& shortcut)
1128 if (shortcut.type == Shortcuts::Type::GLOBAL) result +=
", global";
1132[[nodiscard]]
static gl::vec2 buttonSize(std::string_view text,
float defaultSize_)
1134 const auto& style = ImGui::GetStyle();
1136 auto defaultSize = ImGui::GetFontSize() * defaultSize_;
1137 return {std::max(textSize, defaultSize), 0.0f};
1140void ImGuiSettings::paintEditShortcut()
1142 using enum Shortcuts::Type;
1144 bool editShortcutWindow = editShortcutId != Shortcuts::ID::INVALID;
1145 if (!editShortcutWindow)
return;
1147 im::Window(
"Edit shortcut", &editShortcutWindow, ImGuiWindowFlags_AlwaysAutoResize, [&]{
1148 auto& shortcuts = manager.getShortcuts();
1149 auto shortcut = shortcuts.getShortcut(editShortcutId);
1152 ImGui::TableSetupColumn(
"", ImGuiTableColumnFlags_WidthFixed);
1153 ImGui::TableSetupColumn(
"", ImGuiTableColumnFlags_WidthStretch);
1155 if (ImGui::TableNextColumn()) {
1156 ImGui::AlignTextToFramePadding();
1159 static constexpr auto waitKeyTitle =
"Waiting for key";
1160 if (ImGui::TableNextColumn()) {
1162 if (ImGui::Button(text.c_str(), buttonSize(text, 4.0f))) {
1163 popupTimeout = 10.0f;
1165 ImGui::OpenPopup(waitKeyTitle);
1169 im::PopupModal(waitKeyTitle, &isOpen, ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize, [&]{
1170 ImGui::Text(
"Enter key combination for shortcut '%s'",
1171 Shortcuts::getShortcutDescription(editShortcutId).c_str());
1172 ImGui::Text(
"Timeout in %d seconds.",
int(popupTimeout));
1174 popupTimeout -= ImGui::GetIO().DeltaTime;
1175 if (!isOpen || popupTimeout <= 0.0f) {
1176 ImGui::CloseCurrentPopup();
1178 if (
auto keyChord = getCurrentlyPressedKeyChord(); keyChord != ImGuiKey_None) {
1179 shortcut.keyChord = keyChord;
1180 shortcuts.setShortcut(editShortcutId, shortcut);
1181 editShortcutWindow =
false;
1182 ImGui::CloseCurrentPopup();
1186 if (shortcut.type ==
one_of(LOCAL, GLOBAL)) {
1187 if (ImGui::TableNextColumn()) {
1188 ImGui::AlignTextToFramePadding();
1191 if (ImGui::TableNextColumn()) {
1192 bool global = shortcut.type == GLOBAL;
1193 if (ImGui::Checkbox(
"##global", &global)) {
1194 shortcut.type = global ? GLOBAL : LOCAL;
1195 shortcuts.setShortcut(editShortcutId, shortcut);
1198 "Global shortcuts react when any GUI window has focus.\n"
1199 "Local shortcuts only react when the specific GUI window has focus.\n"sv);
1204 const auto& defaultShortcut = Shortcuts::getDefaultShortcut(editShortcutId);
1206 if (ImGui::Button(
"Restore default")) {
1207 shortcuts.setShortcut(editShortcutId, defaultShortcut);
1208 editShortcutWindow =
false;
1210 simpleToolTip([&]{
return formatShortcutWithAnnotations(defaultShortcut); });
1215 if (ImGui::Button(
"Set None")) {
1216 shortcuts.setShortcut(editShortcutId, Shortcuts::Shortcut{});
1217 editShortcutWindow =
false;
1222 if (!editShortcutWindow) editShortcutId = Shortcuts::ID::INVALID;
1225void ImGuiSettings::paintShortcut()
1227 im::Window(
"Edit shortcuts", &showShortcut, [&]{
1228 int flags = ImGuiTableFlags_Resizable
1229 | ImGuiTableFlags_RowBg
1230 | ImGuiTableFlags_NoBordersInBodyUntilResize
1231 | ImGuiTableFlags_SizingStretchProp;
1232 im::Table(
"table", 2, flags, {-FLT_MIN, 0.0f}, [&]{
1233 ImGui::TableSetupColumn(
"description");
1234 ImGui::TableSetupColumn(
"key");
1236 const auto& shortcuts = manager.getShortcuts();
1238 auto id =
static_cast<Shortcuts::ID
>(i);
1239 auto shortcut = shortcuts.getShortcut(
id);
1241 if (ImGui::TableNextColumn()) {
1242 ImGui::AlignTextToFramePadding();
1245 if (ImGui::TableNextColumn()) {
1246 auto text = formatShortcutWithAnnotations(shortcut);
1247 if (ImGui::Button(text.c_str(), buttonSize(text, 9.0f))) {
1248 editShortcutId =
id;
1255 paintEditShortcut();
1260 if (selectedStyle < 0) {
1265 if (motherBoard && showConfigureJoystick) paintJoystick(*motherBoard);
1266 if (showFont) paintFont();
1267 if (showShortcut) paintShortcut();
1270std::span<const std::string> ImGuiSettings::getAvailableFonts()
1272 if (availableFonts.empty()) {
1274 const auto& path : context.getPaths()) {
1275 foreach_file(FileOperations::join(path,
"skins"), [&](
const std::string& , std::string_view name) {
1276 if (name.ends_with(
".ttf.gz") || name.ends_with(
".ttf")) {
1277 availableFonts.emplace_back(name);
1285 return availableFonts;
1288bool ImGuiSettings::signalEvent(
const Event& event)
1290 bool msxOrMega = joystick < 2;
1291 using SP = std::span<const zstring_view>;
1292 auto keyNames = msxOrMega ?
SP{msxjoystick::keyNames}
1293 :
SP{joymega ::keyNames};
1294 if (
const auto numButtons = keyNames.size(); popupForKey >= numButtons) {
1299 bool escape =
false;
1300 if (
const auto* keyDown = get_event_if<KeyDownEvent>(event)) {
1301 escape = keyDown->getKeyCode() == SDLK_ESCAPE;
1304 auto getJoyDeadZone = [&](JoystickId joyId) {
1305 const auto& joyMan = manager.getReactor().getInputEventGenerator().getJoystickManager();
1306 const auto*
setting = joyMan.getJoyDeadZoneSetting(joyId);
1310 if (!b)
return true;
1313 auto* motherBoard = manager.getReactor().getMotherBoard();
1314 if (!motherBoard)
return true;
1315 const auto& controller = motherBoard->getMSXCommandController();
1316 auto*
setting =
dynamic_cast<StringSetting*
>(controller.findSetting(settingName(joystick)));
1318 auto& interp =
setting->getInterpreter();
1320 TclObject bindings =
setting->getValue();
1321 TclObject key(keyNames[popupForKey]);
1322 TclObject bindingList = bindings.getDictValue(interp, key);
1325 bindingList.addListElement(bs);
1326 bindings.setDictValue(interp, key, bindingList);
1331 popupForKey = unsigned(-1);
1335void ImGuiSettings::initListener()
1337 if (listening)
return;
1340 auto& distributor = manager.getReactor().getEventDistributor();
1345 distributor.registerEventListener(type, *
this);
1349void ImGuiSettings::deinitListener()
1351 if (!listening)
return;
1354 auto& distributor = manager.getReactor().getEventDistributor();
1358 distributor.unregisterEventListener(type, *
this);
const char * c_str() const
std::string_view getBaseName() const
A Setting with a floating point value.
std::unique_ptr< ImGuiSoundChip > soundChip
std::unique_ptr< ImGuiMessages > messages
std::unique_ptr< ImGuiOsdIcons > osdIcons
void save(ImGuiTextBuffer &buf) override
void showMenu(MSXMotherBoard *motherBoard) override
void loadLine(std::string_view name, zstring_view value) override
A Setting with an integer value.
VideoSourceSetting & getVideoSource()
MSXCommandController & getMSXCommandController()
GlobalSettings & getGlobalSettings()
ScaleAlgorithm
Scaler algorithm.
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
auto CalcTextSize(std::string_view str)
void TextUnformatted(const std::string &str)
constexpr T sum(const vecN< N, T > &x)
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
void DisabledIndent(bool b, std::invocable<> auto next)
void Table(const char *str_id, int column, ImGuiTableFlags flags, const ImVec2 &outer_size, float inner_width, std::invocable<> auto next)
void Window(const char *name, bool *p_open, ImGuiWindowFlags flags, std::invocable<> auto next)
void PopupContextItem(const char *str_id, ImGuiPopupFlags popup_flags, std::invocable<> auto next)
bool TreeNode(const char *label, ImGuiTreeNodeFlags flags, std::invocable<> auto next)
void Combo(const char *label, const char *preview_value, ImGuiComboFlags flags, std::invocable<> auto next)
void ListBox(const char *label, const ImVec2 &size, std::invocable<> auto next)
void PopupModal(const char *name, bool *p_open, ImGuiWindowFlags flags, std::invocable<> auto next)
bool Menu(const char *label, bool enabled, std::invocable<> auto next)
void Disabled(bool b, std::invocable<> auto next)
void Font(ImFont *font, std::invocable<> auto next)
void Indent(float indent_w, std::invocable<> auto next)
void Popup(const char *str_id, ImGuiWindowFlags flags, std::invocable<> auto next)
void ID_for_range(std::integral auto count, std::invocable< int > auto next)
string_view stripExtension(string_view path)
Returns the path without extension.
string_view getFilename(string_view path)
Returns the file portion of a path name.
int unlink(zstring_view path)
Call unlink() in a platform-independent manner.
This file implemented 3 utility functions:
const FileContext & systemFileContext()
std::optional< BooleanInput > captureBooleanInput(const Event &event, function_ref< int(JoystickId)> getJoyDeadZone)
bool Checkbox(const HotKey &hotKey, BooleanSetting &setting)
bool SliderFloat(FloatSetting &setting, const char *format, ImGuiSliderFlags flags)
void centerNextWindowOverCurrent()
bool SliderInt(IntegerSetting &setting, ImGuiSliderFlags flags)
void ComboBox(const char *label, Setting &setting, function_ref< std::string(const std::string &)> displayValue, EnumToolTips toolTips)
bool loadOnePersistent(std::string_view name, zstring_view value, C &c, const std::tuple< Elements... > &tup)
void simpleToolTip(std::string_view desc)
void savePersistent(ImGuiTextBuffer &buf, C &c, const std::tuple< Elements... > &tup)
std::string getShortCutForCommand(const HotKey &hotkey, std::string_view command)
void HelpMarker(std::string_view desc)
std::optional< BooleanInput > parseBooleanInput(std::string_view text)
bool InputText(Setting &setting)
ImU32 getColor(imColor col)
void setColors(int style)
std::string getKeyChordName(ImGuiKeyChord keyChord)
std::string toString(const BooleanInput &input)
bool foreach_file(std::string path, FileAction fileAction)
TclObject makeTclList(Args &&... args)
FileContext userDataFileContext(string_view subDir)
auto unique(ForwardRange &&range)
auto remove(ForwardRange &&range, const T &value)
auto find(InputRange &&range, const T &value)
constexpr void replace(ForwardRange &&range, const T &old_value, const T &new_value)
constexpr void sort(RandomAccessRange &&range)
size_t size(std::string_view utf8)
constexpr bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
TemporaryString tmpStrCat(Ts &&... ts)
constexpr auto xrange(T e)
constexpr auto end(const zstring_view &x)