34#include <CustomFont.h>
36#include <imgui_stdlib.h>
43using namespace std::literals;
52 if (item.
name.empty())
return;
53 buf.appendf(
"%s.name=%s\n", name.c_str(), item.
name.c_str());
55 buf.appendf(
"%s.patch=%s\n", name.c_str(), patch.c_str());
58 buf.appendf(
"%s.romType=%s\n", name.c_str(),
63 saveItem(group.
edit, name);
64 auto recentName =
tmpStrCat(name,
".recent");
65 for (
const auto& item : group.
recent) {
66 saveItem(item, recentName);
73 for (
const auto& info : diskMediaInfo) {
78 if (info.show) buf.appendf(
"%s.show=1\n", name.c_str());
83 for (
const auto& info : cartridgeMediaInfo) {
87 if (info.show) buf.appendf(
"%s.show=1\n", name.c_str());
92 for (
const auto& info : hdMediaInfo) {
93 saveGroup(info, name);
98 for (
const auto& info : cdMediaInfo) {
99 saveGroup(info, name);
103 if (cassetteMediaInfo.
show) buf.append(
"cassette.show=1\n");
104 saveGroup(cassetteMediaInfo.
group,
"cassette");
106 saveGroup(extensionMediaInfo,
"extension");
107 saveGroup(laserdiscMediaInfo,
"laserdisc");
112 auto get = [&](std::string_view prefix,
auto& array) -> std::remove_reference_t<
decltype(array[0])>* {
113 if ((name.size() >= (prefix.size() + 2)) && name.starts_with(prefix) && (name[prefix.size() + 1] ==
'.')) {
114 char c = name[prefix.size()];
115 if ((
'a' <= c) && (c < char(
'a' + array.size()))) {
116 return &array[c -
'a'];
121 auto loadItem = [&](
MediaItem& item, std::string_view suffix) {
122 if (suffix ==
"name") {
124 }
else if (suffix ==
"patch") {
126 }
else if (suffix ==
"romType") {
132 auto loadGroup = [&](
ItemGroup& group, std::string_view suffix) {
133 if (suffix.starts_with(
"recent.")) {
134 if (suffix ==
"recent.name" && !group.
recent.full()) {
137 if (!group.
recent.empty()) {
138 loadItem(group.
recent.back(), suffix.substr(7));
141 loadItem(group.
edit, suffix);
147 }
else if (
auto* disk = get(
"disk", diskMediaInfo)) {
149 auto suffix = name.substr(6);
150 if (suffix.starts_with(
"image.")) {
151 loadGroup(disk->groups[
IMAGE], suffix.substr(6));
152 }
else if (suffix.starts_with(
"dirAsDsk.")) {
153 loadGroup(disk->groups[
DIR_AS_DISK], suffix.substr(9));
154 }
else if (suffix ==
"select") {
155 if (
auto i = StringOp::stringTo<unsigned>(value)) {
160 }
else if (suffix ==
"show") {
163 }
else if (
auto* cart = get(
"cart", cartridgeMediaInfo)) {
165 auto suffix = name.substr(6);
166 if (suffix.starts_with(
"rom.")) {
167 loadGroup(cart->groups[
IMAGE], suffix.substr(4));
168 }
else if (suffix.starts_with(
"extension.")) {
169 loadGroup(cart->groups[
EXTENSION], suffix.substr(10));
170 }
else if (suffix ==
"select") {
171 if (
auto i = StringOp::stringTo<unsigned>(value)) {
176 }
else if (suffix ==
"show") {
179 }
else if (
auto* hd = get(
"hd", hdMediaInfo)) {
180 loadGroup(*hd, name.substr(4));
181 }
else if (
auto* cd = get(
"cd", cdMediaInfo)) {
182 loadGroup(*cd, name.substr(4));
183 }
else if (name.starts_with(
"cassette.")) {
184 auto suffix = name.substr(9);
185 if (suffix ==
"show") {
188 loadGroup(cassetteMediaInfo.
group, suffix);
190 }
else if (name.starts_with(
"extension.")) {
191 loadGroup(extensionMediaInfo, name.substr(10));
192 }
else if (name.starts_with(
"laserdisc.")) {
193 loadGroup(laserdiscMediaInfo, name.substr(10));
197static std::string buildFilter(std::string_view description, std::span<const std::string_view> extensions)
199 auto formatExtensions = [&]() -> std::string {
200 if (extensions.size() <= 3) {
202 [](
const auto& ext) {
return strCat(
"*.", ext); }),
205 return join(extensions,
',');
209 description,
" (", formatExtensions(),
"){",
211 [](
const auto& ext) {
return strCat(
'.', ext); }),
221static std::string romFilter()
226static std::string cassetteFilter()
231static std::string hdFilter()
233 return buildFilter(
"Hard disk images", std::array{
"dsk"sv});
236static std::string cdFilter()
238 return buildFilter(
"CDROM images", std::array{
"iso"sv});
241template<std::invocable<const std::
string&> DisplayFunc = std::
identity>
242static std::string display(
const ImGuiMedia::MediaItem& item, DisplayFunc displayFunc = {})
244 std::string result = displayFunc(item.name);
248 if (
auto n = item.ipsPatches.size()) {
249 strAppend(result,
" (+", n,
" patch", (n == 1 ?
"" :
"es"),
')');
256 if (extensionInfo.empty()) {
257 extensionInfo = parseAllConfigFiles<ExtensionInfo>(
manager,
"extensions", {
"Manufacturer"sv,
"Product code"sv,
"Name"sv});
259 return extensionInfo;
264 extensionInfo.clear();
281 std::optional<MSXMotherBoard> mb;
285 mb->getMSXCliComm().setSuppressMessages(
true);
287 mb->loadMachine(
"C-BIOS_MSX1");
292 if (
const auto* current = reactor.getMotherBoard()) {
294 mb->getMSXCliComm().setSuppressMessages(true);
295 mb->loadMachine(std::string(current->getMachineName()));
304 auto ext = mb->loadExtension(info.
configName,
"any");
305 mb->insertExtension(info.
configName, std::move(ext));
320 return (it != allExtensions.end()) ? std::to_address(it) :
nullptr;
326 return info ? info->displayName
327 : std::string(config);
333 if (
auto sha1 = reactor.getFilePool().getSha1Sum(filename)) {
335 if (
const auto* romInfo = database.fetchRomInfo(*sha1)) {
336 if (
auto title = romInfo->getTitle(database.getBufferStart());
338 return std::string(title);
366 std::string result = slot
376 std::string_view display;
378 display = result->getListIndexUnchecked(1).getString();
380 return display.empty() ?
"Empty"
385void ImGuiMedia::printExtensionInfo(ExtensionInfo& info)
388 bool ok =
test.empty();
391 ImGui::TableSetupColumn(
"description", ImGuiTableColumnFlags_WidthFixed);
392 ImGui::TableSetupColumn(
"value", ImGuiTableColumnFlags_WidthStretch);
394 for (
const auto& [desc, value_] : info.configInfo) {
395 const auto& value = value_;
396 if (ImGui::TableNextColumn()) {
399 if (ImGui::TableNextColumn()) {
415void ImGuiMedia::extensionTooltip(ExtensionInfo& info)
418 printExtensionInfo(info);
422bool ImGuiMedia::drawExtensionFilter()
424 std::string filterDisplay =
"filter";
425 if (!filterType.empty() || !filterString.empty())
strAppend(filterDisplay,
':');
426 if (!filterType.empty())
strAppend(filterDisplay,
' ', filterType);
427 if (!filterString.empty())
strAppend(filterDisplay,
' ', filterString);
429 bool newFilterOpen = filterOpen;
430 im::TreeNode(filterDisplay.c_str(), &newFilterOpen, [&]{
431 displayFilterCombo(filterType,
"Type", getAllExtensions());
432 ImGui::InputText(ICON_IGFD_FILTER, &filterString);
433 simpleToolTip(
"A list of substrings that must be part of the extension.\n"
435 "For example: enter 'ko' to search for 'Konami' extensions. "
436 "Then refine the search by appending '<space>sc' to find the 'Konami SCC' extension.");
438 bool changed = filterOpen != newFilterOpen;
439 filterOpen = newFilterOpen;
445 im::Menu(
"Media", motherBoard !=
nullptr, [&]{
448 enum class Status {
NONE, ITEM, SEPARATOR };
450 Status status =
NONE;
452 auto endGroup = [&] {
453 if (status == ITEM) status = SEPARATOR;
455 auto elementInGroup = [&] {
456 if (status == SEPARATOR) {
462 auto showCurrent = [&](
const TclObject& current, std::string_view type) {
463 if (current.
empty()) {
471 auto showRecent = [&](std::string_view mediaName,
ItemGroup& group,
472 function_ref<std::string(
const std::string&)> displayFunc = std::identity{},
473 const std::function<void(
const std::string&)>& toolTip = {}) {
474 if (!group.recent.empty()) {
478 for (
const auto& item : group.recent) {
479 auto d =
strCat(display(item, displayFunc),
"##", count++);
480 if (ImGui::MenuItem(d.c_str())) {
482 insertMedia(mediaName, group.edit);
484 if (toolTip) toolTip(item.name);
494 bool anySlot =
false;
496 if (!slotManager.slotExists(i))
continue;
498 auto [ps, ss] = slotManager.
getPsSs(i);
499 std::string extraInfo = ss == -1 ?
"" :
strCat(
" (", slotManager.getPsSsString(i),
")");
500 auto displayName =
strCat(
"Cartridge Slot ",
char(
'A' + i), extraInfo);
501 ImGui::MenuItem(displayName.c_str(),
nullptr, &cartridgeMediaInfo[i].show);
505 ImGui::TextDisabled(
"No cartridge slots present");
512 auto mediaName =
"ext"sv;
513 auto& group = extensionMediaInfo;
516 HelpMarker(
"Note that some extensions are I/O only and will not occupy any cartridge slot when inserted. "
517 "These can only be removed via the 'Media > Extensions > Remove' menu. "
518 "To insert (non I/O-only) extensions in a specific slot, use the 'Media > Cartridge Slot' menu.");
519 drawExtensionFilter();
526 float width = 40.0f * ImGui::GetFontSize();
527 float height = 10.25f * ImGui::GetTextLineHeightWithSpacing();
530 auto& ext = allExtensions[filteredExtensions[i]];
531 bool ok = getTestResult(ext).empty();
532 im::StyleColor(!ok, ImGuiCol_Text, getColor(imColor::ERROR), [&]{
533 if (ImGui::Selectable(ext.displayName.c_str())) {
534 group.edit.name = ext.configName;
535 insertMedia(mediaName, group.edit);
536 ImGui::CloseCurrentPopup();
538 extensionTooltip(ext);
544 showRecent(mediaName, group,
545 [
this](
const std::string& config) {
548 [
this](
const std::string& e) {
550 extensionTooltip(*info);
558 im::Menu(
"Remove", [&]{
560 for (const auto& ext : extensions) {
561 auto name = strCat(slotAndNameForHardwareConfig(slotManager, *ext),
"##", count++);
562 if (ImGui::Selectable(name.c_str())) {
563 manager.executeDelayed(makeTclList(
"remove_extension", ext->getName()));
565 if (auto* info = findExtensionInfo(ext->getConfigName())) {
566 extensionTooltip(*info);
577 bool anyDrive =
false;
579 if (!(*drivesInUse)[i])
continue;
581 auto displayName =
strCat(
"Disk Drive ",
char(
'A' + i));
582 ImGui::MenuItem(displayName.c_str(),
nullptr, &diskMediaInfo[i].show);
583 simpleToolTip([&] {
return displayNameForDriveContent(i); });
586 ImGui::TextDisabled(
"No disk drives present");
592 if (
auto* player = motherBoard->getCassettePort().getCassettePlayer()) {
593 ImGui::MenuItem(
"Tape Deck",
nullptr, &cassetteMediaInfo.show);
595 auto current = player->getImageName().getResolved();
596 return current.empty() ?
"Empty" : current;
599 ImGui::TextDisabled(
"No cassette port present");
605 std::string hdName =
"hdX";
607 if (!(*hdInUse)[i])
continue;
608 hdName.back() = char(
'a' + i);
609 auto displayName =
strCat(
"Hard Disk ",
char(
'A' + i));
610 if (
auto cmdResult = manager.execute(TclObject(hdName))) {
612 auto& group = hdMediaInfo[i];
614 auto currentImage = cmdResult->getListIndex(interp, 1);
615 showCurrent(currentImage,
"hard disk");
616 bool powered = motherBoard->isPowered();
617 im::Disabled(powered, [&]{
618 if (ImGui::MenuItem(
"Select hard disk image...")) {
619 manager.openFile->selectFile(
620 "Select image for " + displayName,
622 [this, &group, hdName](const auto& fn) {
623 group.edit.name = fn;
624 this->insertMedia(hdName, group.edit);
626 currentImage.getString());
630 HelpMarker(
"Hard disk image cannot be switched while the MSX is powered on.");
633 showRecent(hdName, group);
641 auto cdInUse = IDECDROM::getDrivesInUse(*motherBoard);
642 std::string cdName =
"cdX";
644 if (!(*cdInUse)[i])
continue;
645 cdName.back() = char(
'a' + i);
646 auto displayName =
strCat(
"CDROM Drive ",
char(
'A' + i));
647 if (
auto cmdResult = manager.execute(TclObject(cdName))) {
649 auto& group = cdMediaInfo[i];
651 auto currentImage = cmdResult->getListIndex(interp, 1);
652 showCurrent(currentImage,
"CDROM");
653 if (ImGui::MenuItem(
"Eject", nullptr, false, !currentImage.empty())) {
654 manager.executeDelayed(makeTclList(cdName,
"eject"));
656 if (ImGui::MenuItem(
"Insert CDROM image...")) {
657 manager.openFile->selectFile(
658 "Select CDROM image for " + displayName,
660 [
this, &group, cdName](
const auto& fn) {
661 group.edit.name = fn;
662 this->insertMedia(cdName, group.edit);
664 currentImage.getString());
666 showRecent(cdName, group);
673 if (
auto cmdResult = manager.execute(TclObject(
"laserdiscplayer"))) {
676 auto currentImage = cmdResult->getListIndex(interp, 1);
677 showCurrent(currentImage,
"laserdisc");
678 if (ImGui::MenuItem(
"eject",
nullptr,
false, !currentImage.empty())) {
679 manager.executeDelayed(
makeTclList(
"laserdiscplayer",
"eject"));
681 if (ImGui::MenuItem(
"Insert LaserDisc image...")) {
682 manager.openFile->selectFile(
683 "Select LaserDisc image",
684 buildFilter(
"LaserDisc images", std::array<std::string_view, 1>{
"ogv"}),
685 [
this](
const auto& fn) {
686 laserdiscMediaInfo.edit.name = fn;
687 this->insertMedia(
"laserdiscplayer", laserdiscMediaInfo.edit);
689 currentImage.getString());
691 showRecent(
"laserdiscplayer", laserdiscMediaInfo);
700 if (!motherBoard)
return;
702 auto drivesInUse = RealDrive::getDrivesInUse(*motherBoard);
703 for (
auto i :
xrange(RealDrive::MAX_DRIVES)) {
704 if (!(*drivesInUse)[i])
continue;
705 if (diskMediaInfo[i].show) {
711 for (
auto i :
xrange(CartridgeSlotManager::MAX_SLOTS)) {
712 if (!slotManager.slotExists(i))
continue;
713 if (cartridgeMediaInfo[i].show) {
718 if (cassetteMediaInfo.show) {
720 cassetteMenu(*player);
730static void printPatches(
const TclObject& patches)
732 if (!patches.empty()) {
735 for (
const auto& patch : patches) {
742static std::string leftClip(std::string_view s,
float maxWidth)
745 if (fullWidth <= maxWidth)
return std::string(s);
748 if (maxWidth <= 0.0f)
return "...";
753 return strCat(
"...", s.substr(len - num));
756bool ImGuiMedia::selectRecent(ItemGroup& group,
function_ref<std::string(
const std::string&)> displayFunc,
float width)
const
758 bool interacted =
false;
759 ImGui::SetNextItemWidth(-width);
760 const auto& style = ImGui::GetStyle();
761 auto textWidth = ImGui::GetContentRegionAvail().x - (3.0f * style.FramePadding.x + ImGui::GetFrameHeight() + width);
762 auto preview = leftClip(displayFunc(group.edit.name), textWidth);
763 im::Combo(
"##recent", preview.c_str(), [&]{
765 for (const auto& item : group.recent) {
766 auto d = strCat(display(item, displayFunc),
"##", count++);
767 if (ImGui::Selectable(d.c_str())) {
773 interacted |= ImGui::IsItemActive();
777static float calcButtonWidth(std::string_view text1,
const char* text2)
779 const auto& style = ImGui::GetStyle();
780 float width = style.ItemSpacing.x + 2.0f * style.FramePadding.x +
ImGui::CalcTextSize(text1).x;
787bool ImGuiMedia::selectImage(ItemGroup& group,
const std::string& title,
789 function_ref<std::string(
const std::string&)> displayFunc,
790 const std::function<
void()>& createNewCallback)
792 bool interacted =
false;
794 auto width = calcButtonWidth(ICON_IGFD_FOLDER_OPEN, createNewCallback ? ICON_IGFD_ADD : nullptr);
795 interacted |= selectRecent(group, displayFunc, width);
796 if (createNewCallback) {
798 if (ImGui::Button(ICON_IGFD_ADD)) {
805 if (ImGui::Button(ICON_IGFD_FOLDER_OPEN)) {
807 manager.openFile->selectFile(
810 [&](
const auto& fn) {
811 group.edit.name = fn;
812 group.edit.romType = RomType::UNKNOWN;
821bool ImGuiMedia::selectDirectory(ItemGroup& group,
const std::string& title,
zstring_view current,
822 const std::function<
void()>& createNewCallback)
824 bool interacted =
false;
826 auto width = calcButtonWidth(ICON_IGFD_FOLDER_OPEN, createNewCallback ? ICON_IGFD_ADD : nullptr);
827 interacted |= selectRecent(group, std::identity{}, width);
828 if (createNewCallback) {
830 if (ImGui::Button(ICON_IGFD_ADD)) {
837 if (ImGui::Button(ICON_IGFD_FOLDER_OPEN)) {
839 manager.openFile->selectDirectory(
841 [&](
const auto& fn) { group.edit.name = fn; },
849bool ImGuiMedia::selectMapperType(
const char* label,
RomType& romType)
851 bool interacted =
false;
852 bool isAutoDetect = romType == RomType::UNKNOWN;
853 constexpr const char* autoStr =
"auto detect";
854 std::string current = isAutoDetect ? autoStr : std::string(RomInfo::romTypeToName(romType));
856 if (ImGui::Selectable(autoStr, isAutoDetect)) {
858 romType = RomType::UNKNOWN;
861 for (
const auto& romInfo : RomInfo::getRomTypeInfo()) {
862 bool selected = romType ==
static_cast<RomType>(count);
863 if (ImGui::Selectable(romInfo.name.c_str(), selected)) {
865 romType =
static_cast<RomType>(count);
871 interacted |= ImGui::IsItemActive();
875bool ImGuiMedia::selectPatches(MediaItem& item,
int& patchIndex)
877 bool interacted =
false;
878 std::string patchesTitle =
"IPS patches";
879 if (!item.ipsPatches.empty()) {
880 strAppend(patchesTitle,
" (", item.ipsPatches.size(),
')');
884 const auto& style = ImGui::GetStyle();
885 auto width = style.ItemSpacing.x + 2.0f * style.FramePadding.x + ImGui::CalcTextSize(
"Remove"sv).x;
886 ImGui::SetNextItemWidth(-width);
888 im::ListBox(
"##", [&]{
890 for (const auto& patch : item.ipsPatches) {
891 auto preview = leftClip(patch, ImGui::GetContentRegionAvail().x);
892 if (ImGui::Selectable(strCat(preview,
"##", count).c_str(), count == patchIndex)) {
902 if (ImGui::Button(
"Add")) {
904 manager.openFile->selectFile(
905 "Select disk IPS patch",
906 buildFilter(
"IPS patches", std::array<std::string_view, 1>{
"ips"}),
907 [&](
const std::string& ips) {
908 patchIndex = narrow<int>(item.ipsPatches.size());
909 item.ipsPatches.push_back(ips);
912 auto size = narrow<int>(item.ipsPatches.size());
913 im::Disabled(patchIndex < 0 || patchIndex >= size, [&] {
914 if (ImGui::Button(
"Remove")) {
916 item.ipsPatches.erase(item.ipsPatches.begin() + patchIndex);
919 if (ImGui::ArrowButton(
"up", ImGuiDir_Up)) {
920 std::swap(item.ipsPatches[patchIndex], item.ipsPatches[patchIndex - 1]);
925 if (ImGui::ArrowButton(
"down", ImGuiDir_Down)) {
926 std::swap(item.ipsPatches[patchIndex], item.ipsPatches[patchIndex + 1]);
936bool ImGuiMedia::insertMediaButton(std::string_view mediaName,
const ItemGroup& group,
bool* showWindow)
938 bool clicked =
false;
939 im::Disabled(group.edit.name.empty() && !group.edit.isEject(), [&]{
940 const auto& style = ImGui::GetStyle();
941 auto width = 4.0f * style.FramePadding.x + style.ItemSpacing.x +
942 ImGui::CalcTextSize(
"Apply"sv).x + ImGui::CalcTextSize(
"Ok"sv).x;
943 ImGui::SetCursorPosX(ImGui::GetContentRegionAvail().x - width + style.WindowPadding.x);
944 clicked |= ImGui::Button(
"Apply");
946 if (ImGui::Button(
"Ok")) {
951 insertMedia(mediaName, group.edit);
957TclObject ImGuiMedia::showDiskInfo(std::string_view mediaName, DiskMediaInfo& info)
959 TclObject currentTarget;
960 auto cmdResult = manager.execute(
makeTclList(
"machine_info",
"media", mediaName));
961 if (!cmdResult)
return currentTarget;
963 using enum SelectDiskType;
964 auto selectType = [&]{
967 auto s = type->getString();
970 }
else if (s ==
"ramdsk") {
972 }
else if (s ==
"dirasdisk") {
979 std::string_view typeStr = [&]{
980 switch (selectType) {
981 case IMAGE:
return "Disk image:";
982 case DIR_AS_DISK:
return "Dir as disk:";
983 case RAMDISK:
return "RAM disk";
984 case EMPTY:
return "No disk inserted";
988 bool detailedInfo = selectType ==
one_of(DIR_AS_DISK, IMAGE);
989 auto currentPatches = getPatches(*cmdResult);
991 bool copyCurrent = ImGui::SmallButton(
"Current disk");
992 HelpMarker(
"Press to copy current disk to 'Select new disk' section.");
998 currentTarget = *target;
1001 ImGui::GetContentRegionAvail().x));
1003 std::string statusLine;
1004 auto add = [&](std::string_view s) {
1005 if (statusLine.empty()) {
1012 if (ro->getOptionalBool().value_or(
false)) {
1017 add(doubleSided->getOptionalBool().value_or(
true) ?
"double-sided" :
"single-sided");
1020 add(
tmpStrCat(
size->getOptionalInt().value_or(0) / 1024,
"kB"));
1022 if (!statusLine.empty()) {
1025 printPatches(currentPatches);
1029 info.select = selectType;
1030 auto& edit = info.groups[selectType].edit;
1031 edit.name = currentTarget.getString();
1032 edit.ipsPatches = to_vector<std::string>(currentPatches);
1035 return currentTarget;
1038void ImGuiMedia::printDatabase(
const RomInfo& romInfo,
const char* buf)
1040 auto printRow = [](std::string_view description, std::string_view value) {
1041 if (value.empty())
return;
1042 if (ImGui::TableNextColumn()) {
1045 if (ImGui::TableNextColumn()) {
1050 printRow(
"Title", romInfo.
getTitle(buf));
1051 printRow(
"Year", romInfo.
getYear(buf));
1052 printRow(
"Company", romInfo.
getCompany(buf));
1053 printRow(
"Country", romInfo.
getCountry(buf));
1057 std::string result =
"Unmodified dump";
1059 strAppend(result,
" (confirmed by ", str,
')');
1063 return std::string(str);
1066 printRow(
"Status", status);
1067 printRow(
"Remark", romInfo.
getRemark(buf));
1073 ImGui::TableSetupColumn(
"description", ImGuiTableColumnFlags_WidthFixed);
1074 ImGui::TableSetupColumn(
"value", ImGuiTableColumnFlags_WidthStretch);
1076 if (ImGui::TableNextColumn()) {
1079 if (ImGui::TableNextColumn()) {
1084 const auto* romInfo = [&]() ->
const RomInfo* {
1086 if (
const auto* info = database.fetchRomInfo(Sha1Sum(actual->getString()))) {
1091 if (
const auto* info = database.fetchRomInfo(Sha1Sum(original->getString()))) {
1098 ImGuiMedia::printDatabase(*romInfo, database.getBufferStart());
1101 std::string mapperStr{RomInfo::romTypeToName(romType)};
1103 if (
auto dbType = romInfo->getRomType();
1104 dbType != RomType::UNKNOWN && dbType != romType) {
1105 strAppend(mapperStr,
" (database: ", RomInfo::romTypeToName(dbType),
')');
1108 if (ImGui::TableNextColumn()) {
1111 if (ImGui::TableNextColumn()) {
1117TclObject ImGuiMedia::showCartridgeInfo(std::string_view mediaName, CartridgeMediaInfo& info,
int slot)
1119 TclObject currentTarget;
1121 if (!cmdResult)
return currentTarget;
1123 using enum SelectCartridgeType;
1124 auto selectType = [&]{
1126 auto s = type->getString();
1127 if (s ==
"extension") {
1137 auto currentPatches = getPatches(*cmdResult);
1139 bool copyCurrent = ImGui::SmallButton(
"Current cartridge");
1144 RomType currentRomType = RomType::UNKNOWN;
1146 if (selectType == EMPTY) {
1149 currentTarget = *target;
1150 if (selectType == EXTENSION) {
1151 if (
auto* i = findExtensionInfo(target->getString())) {
1152 printExtensionInfo(*i);
1154 }
else if (selectType == IMAGE) {
1156 currentRomType = RomInfo::nameToRomType(mapper->getString());
1158 printRomInfo(manager, *cmdResult, target->getString(), currentRomType);
1159 printPatches(currentPatches);
1164 info.select = selectType;
1165 auto& edit = info.groups[selectType].edit;
1166 edit.name = currentTarget.getString();
1167 edit.ipsPatches = to_vector<std::string>(currentPatches);
1168 edit.romType = currentRomType;
1171 return currentTarget;
1174void ImGuiMedia::diskMenu(
int i)
1176 auto& info = diskMediaInfo[i];
1177 auto mediaName =
strCat(
"disk",
char(
'a' + i));
1178 auto displayName =
strCat(
"Disk Drive ",
char(
'A' + i));
1179 ImGui::SetNextWindowSize(
gl::vec2{29, 22} * ImGui::GetFontSize(), ImGuiCond_FirstUseEver);
1180 im::Window(displayName.c_str(), &info.show, [&]{
1181 auto current = showDiskInfo(mediaName, info);
1182 im::Child(
"select", {0, -ImGui::GetFrameHeightWithSpacing()}, [&]{
1183 using enum SelectDiskType;
1184 ImGui::TextUnformatted(
"Select new disk"sv);
1186 ImGui::RadioButton(
"disk image", std::bit_cast<int*>(&info.select), to_underlying(IMAGE));
1187 im::VisuallyDisabled(info.select != IMAGE, [&]{
1189 auto& group = info.groups[IMAGE];
1190 auto createNew = [&]{
1191 manager.openFile->selectNewFile(
1192 "Select name for new blank disk image",
1193 "Disk images (*.dsk){.dsk}",
1194 [&](const auto& fn) {
1195 group.edit.name = fn;
1196 auto& diskManipulator = manager.getReactor().getDiskManipulator();
1198 diskManipulator.create(fn, MSXBootSectorType::DOS2, {1440});
1199 } catch (MSXException& e) {
1200 manager.printError(
"Couldn't create new disk image: ", e.getMessage());
1203 current.getString());
1205 bool interacted = selectImage(
1206 group, strCat(
"Select disk image for ", displayName), &diskFilter,
1207 current.getString(), std::identity{}, createNew);
1208 interacted |= selectPatches(group.edit, group.patchIndex);
1209 if (interacted) info.select = IMAGE;
1212 ImGui::RadioButton(
"dir as disk", std::bit_cast<int*>(&info.select),
to_underlying(DIR_AS_DISK));
1215 auto& group = info.groups[DIR_AS_DISK];
1216 auto createNew = [&]{
1217 manager.openFile->selectNewFile(
1218 "Select name for new empty directory",
1220 [&](const auto& fn) {
1221 group.edit.name = fn;
1223 FileOperations::mkdirp(fn);
1224 } catch (MSXException& e) {
1225 manager.printError(
"Couldn't create directory: ", e.getMessage());
1228 current.getString());
1230 bool interacted = selectDirectory(
1231 group, strCat(
"Select directory for ", displayName),
1232 current.getString(), createNew);
1233 if (interacted) info.select = DIR_AS_DISK;
1236 ImGui::RadioButton(
"RAM disk", std::bit_cast<int*>(&info.select),
to_underlying(RAMDISK));
1237 if (!current.
empty()) {
1238 ImGui::RadioButton(
"Eject", std::bit_cast<int*>(&info.select),
to_underlying(EMPTY));
1241 insertMediaButton(mediaName, info.groups[info.select], &info.show);
1245void ImGuiMedia::cartridgeMenu(
int cartNum)
1247 auto& info = cartridgeMediaInfo[cartNum];
1248 auto displayName =
strCat(
"Cartridge Slot ",
char(
'A' + cartNum));
1249 ImGui::SetNextWindowSize(
gl::vec2{37, 30} * ImGui::GetFontSize(), ImGuiCond_FirstUseEver);
1250 im::Window(displayName.c_str(), &info.show, [&]{
1251 using enum SelectCartridgeType;
1252 auto cartName = strCat(
"cart", char(
'a' + cartNum));
1253 auto extName = strCat(
"ext", char(
'a' + cartNum));
1255 auto current = showCartridgeInfo(cartName, info, cartNum);
1257 im::Child(
"select", {0, -ImGui::GetFrameHeightWithSpacing()}, [&]{
1258 ImGui::TextUnformatted(
"Select new cartridge:"sv);
1260 ImGui::RadioButton(
"ROM image", std::bit_cast<int*>(&info.select), to_underlying(IMAGE));
1261 im::VisuallyDisabled(info.select != IMAGE, [&]{
1263 auto& group = info.groups[IMAGE];
1264 auto& item = group.edit;
1265 bool interacted = selectImage(
1266 group, strCat(
"Select ROM image for ", displayName), &romFilter, current.getString());
1268 const auto& style = ImGui::GetStyle();
1269 ImGui::SetNextItemWidth(-(ImGui::CalcTextSize(
"mapper-type").x + style.ItemInnerSpacing.x));
1270 interacted |= selectMapperType(
"mapper-type", item.romType);
1271 interacted |= selectPatches(item, group.patchIndex);
1272 if (interacted) info.select = IMAGE;
1275 ImGui::RadioButton(
"extension", std::bit_cast<int*>(&info.select),
to_underlying(EXTENSION));
1278 auto& allExtensions = getAllExtensions();
1279 auto& group = info.groups[EXTENSION];
1280 auto& item = group.edit;
1282 bool interacted = drawExtensionFilter();
1284 auto drawExtensions = [&]{
1285 auto filteredExtensions = to_vector(xrange(allExtensions.size()));
1286 applyComboFilter(
"Type", filterType, allExtensions, filteredExtensions);
1287 applyDisplayNameFilter(filterString, allExtensions, filteredExtensions);
1289 im::ListClipper(filteredExtensions.size(), [&](int i) {
1290 auto& ext = allExtensions[filteredExtensions[i]];
1291 bool ok = getTestResult(ext).empty();
1292 im::StyleColor(!ok, ImGuiCol_Text, getColor(imColor::ERROR), [&]{
1293 if (ImGui::Selectable(ext.displayName.c_str(), item.name == ext.configName)) {
1295 item.name = ext.configName;
1297 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
1298 insertMedia(extName, group.edit);
1300 extensionTooltip(ext);
1305 im::ListBox(
"##list", [&]{
1309 im::Combo(
"##extension", displayNameForExtension(item.name).c_str(), [&]{
1314 interacted |= ImGui::IsItemActive();
1315 if (interacted) info.select = EXTENSION;
1318 if (!current.
empty()) {
1319 ImGui::RadioButton(
"Eject", std::bit_cast<int*>(&info.select),
to_underlying(EMPTY));
1321 ImGui::Checkbox(
"Reset MSX on changes", &resetOnCartChanges);
1323 if (insertMediaButton(info.select == EXTENSION ? extName : cartName,
1324 info.groups[info.select], &info.show)) {
1325 if (resetOnCartChanges) {
1332static void RenderPlay(
gl::vec2 center, ImDrawList* drawList)
1334 float half = 0.4f * ImGui::GetTextLineHeight();
1335 auto p1 = center +
gl::vec2(half, 0.0f);
1336 auto p2 = center +
gl::vec2(-half, half);
1338 drawList->AddTriangleFilled(p1, p2,
p3,
getColor(imColor::TEXT));
1340static void RenderRewind(
gl::vec2 center, ImDrawList* drawList)
1342 float size = 0.8f * ImGui::GetTextLineHeight();
1343 float half =
size * 0.5f;
1344 auto color =
getColor(imColor::TEXT);
1345 auto p1 = center +
gl::vec2(-size, 0.0f);
1346 auto p2 = center +
gl::vec2(0.0f, -half);
1348 drawList->AddTriangleFilled(p1, p2,
p3, color);
1353 drawList->AddTriangleFilled(p1, p2,
p3, color);
1355static void RenderStop(
gl::vec2 center, ImDrawList* drawList)
1357 gl::vec2 half{0.4f * ImGui::GetTextLineHeight()};
1358 drawList->AddRectFilled(center - half, center + half,
getColor(imColor::TEXT));
1360static void RenderRecord(
gl::vec2 center, ImDrawList* drawList)
1362 float radius = 0.4f * ImGui::GetTextLineHeight();
1363 drawList->AddCircleFilled(center, radius,
getColor(imColor::TEXT));
1367void ImGuiMedia::cassetteMenu(CassettePlayer& cassettePlayer)
1369 ImGui::SetNextWindowSize(
gl::vec2{29, 21} * ImGui::GetFontSize(), ImGuiCond_FirstUseEver);
1370 auto& info = cassetteMediaInfo;
1371 auto& group = info.group;
1373 ImGui::TextUnformatted(
"Current tape"sv);
1374 auto current = cassettePlayer.getImageName().getResolved();
1376 if (current.empty()) {
1377 ImGui::TextUnformatted(
"No tape inserted"sv);
1379 ImGui::TextUnformatted(
"Tape image:"sv);
1381 ImGui::TextUnformatted(leftClip(current, ImGui::GetContentRegionAvail().x));
1385 if (ImGui::Button(
"Eject")) {
1386 manager.executeDelayed(makeTclList(
"cassetteplayer",
"eject"));
1393 auto status = cassettePlayer.getState();
1394 auto size = ImGui::GetFrameHeightWithSpacing();
1409 "Select new wav file for record",
1410 "Tape images (*.wav){.wav}",
1411 [&](
const auto& fn) {
1412 group.edit.name = fn;
1414 [&group](
const TclObject&) {
1422 const auto& style = ImGui::GetStyle();
1423 ImGui::SameLine(0.0f, 3.0f * style.ItemSpacing.x);
1427 auto length = cassettePlayer.getTapeLength(now);
1428 auto pos = cassettePlayer.getTapePos(now);
1429 auto format = [](
double time) {
1430 int t = narrow_cast<int>(time);
1431 int s =
t % 60;
t /= 60;
1432 int m =
t % 60;
t /= 60;
1433 std::ostringstream os;
1434 os << std::setfill(
'0');
1435 if (
t) os << std::setw(2) <<
t <<
':';
1436 os << std::setw(2) << m <<
':';
1437 os << std::setw(2) << s;
1440 auto parse = [](std::string_view str) -> std::optional<unsigned> {
1442 auto s = StringOp::stringTo<unsigned>(seconds);
1444 unsigned result = *s;
1446 if (!head.empty()) {
1448 auto m = StringOp::stringTo<unsigned>(minutes);
1452 if (!hours.empty()) {
1453 auto h = StringOp::stringTo<unsigned>(hours);
1455 result += *h * 60 * 60;
1460 auto posStr =
format(pos);
1461 ImGui::SetNextItemWidth(
ImGui::CalcTextSize(std::string_view(posStr)).x + 2.0f * style.FramePadding.x);
1462 if (ImGui::InputText(
"##pos", &posStr, ImGuiInputTextFlags_EnterReturnsTrue)) {
1463 if (
auto newPos =
parse(posStr)) {
1467 simpleToolTip(
"Indicates the current position of the tape, but can be edited to change the position manual (like fast forward)");
1470 ImGui::Text(
"/ %s",
format(length).c_str());
1472 const auto& controller = motherBoard->getMSXCommandController();
1473 const auto& hotKey = reactor.getHotKey();
1474 if (
auto* autoRun =
dynamic_cast<BooleanSetting*
>(controller.findSetting(
"autoruncassettes"))) {
1475 Checkbox(hotKey,
"(try to) Auto Run", *autoRun);
1477 if (
auto* mute =
dynamic_cast<BooleanSetting*
>(controller.findSetting(
"cassetteplayer_ch1_mute"))) {
1478 Checkbox(hotKey,
"Mute tape audio", *mute, [](
const Setting&) {
return std::string{}; });
1480 bool enabled = cassettePlayer.isMotorControlEnabled();
1481 bool changed = ImGui::Checkbox(
"Motor control enabled", &enabled);
1483 manager.
execute(
makeTclList(
"cassetteplayer",
"motorcontrol", enabled ?
"on" :
"off"));
1485 simpleToolTip(
"Enable or disable motor control. Disable in some rare cases where you don't want the motor of the player to be controlled by the MSX, e.g. for CD-Sequential.");
1490 im::Child(
"select", {0, -ImGui::GetFrameHeightWithSpacing()}, [&]{
1493 selectImage(group,
"Select tape image", &cassetteFilter, current);
1496 insertMediaButton(
"cassetteplayer", group, &info.show);
1500void ImGuiMedia::insertMedia(std::string_view mediaName,
const MediaItem& item)
1503 if (item.isEject()) {
1504 cmd.addListElement(
"eject");
1506 if (item.name.empty())
return;
1507 cmd.addListElement(
"insert", item.name);
1508 for (
const auto& patch : item.ipsPatches) {
1509 cmd.addListElement(
"-ips", patch);
1511 if (item.romType != RomType::UNKNOWN) {
1512 cmd.addListElement(
"-romtype", RomInfo::romTypeToName(item.romType));
1516 [
this, cmd](
const TclObject&) {
1524 auto n = cmd.
size();
1530 if (mediaName.starts_with(
"cart")) {
1531 if (
int i = mediaName[4] -
'a'; 0 <= i && i < int(CartridgeSlotManager::MAX_SLOTS)) {
1532 return &cartridgeMediaInfo[i].groups[SelectCartridgeType::IMAGE];
1534 }
else if (mediaName.starts_with(
"disk")) {
1535 if (
int i = mediaName[4] -
'a'; 0 <= i && i < int(RealDrive::MAX_DRIVES)) {
1536 return &diskMediaInfo[i].groups[SelectDiskType::IMAGE];
1538 }
else if (mediaName.starts_with(
"hd")) {
1539 if (
int i = mediaName[2] -
'a'; 0 <= i && i < int(HD::MAX_HD)) {
1540 return &hdMediaInfo[i];
1542 }
else if (mediaName.starts_with(
"cd")) {
1543 if (
int i = mediaName[2] -
'a'; 0 <= i && i < int(IDECDROM::MAX_CD)) {
1544 return &cdMediaInfo[i];
1546 }
else if (mediaName ==
"cassetteplayer") {
1547 return &cassetteMediaInfo.group;
1548 }
else if (mediaName ==
"laserdiscplayer") {
1549 return &laserdiscMediaInfo;
1550 }
else if (mediaName ==
"ext") {
1551 return &extensionMediaInfo;
1553 return static_cast<ItemGroup*
>(
nullptr);
1563 if (option ==
"-ips" && i < n) {
1567 if (option ==
"-romtype" && i < n) {
void test(const IterableBitSet< N > &s, std::initializer_list< size_t > list)
static constexpr unsigned MAX_SLOTS
std::pair< int, int > getPsSs(unsigned slot) const
std::optional< unsigned > findSlotWith(const HardwareConfig &config) const
std::string getPsSsString(unsigned slot) const
const HardwareConfig * getConfigForSlot(unsigned slot) const
static std::span< const std::string_view > getExtensions()
virtual CassettePlayer * getCassettePlayer()=0
Get the cassette player (if available)
static std::span< const std::string_view > getExtensions()
static std::shared_ptr< HDInUse > getDrivesInUse(MSXMotherBoard &motherBoard)
const std::string & getConfigName() const
std::string_view getRomFilename() const
std::optional< TclObject > execute(TclObject command)
Interpreter & getInterpreter()
std::unique_ptr< ImGuiOpenFile > openFile
void executeDelayed(std::function< void()> action)
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
CartridgeSlotManager & getSlotManager()
const Extensions & getExtensions() const
CassettePortInterface & getCassettePort()
static std::span< const std::string_view > getExtensions()
MSXMotherBoard * getMotherBoard() const
RomDatabase & getSoftwareDatabase()
This class implements a real drive, single or double sided.
static std::shared_ptr< DrivesInUse > getDrivesInUse(MSXMotherBoard &motherBoard)
std::string_view getYear(const char *buf) const
std::string_view getTitle(const char *buf) const
static zstring_view romTypeToName(RomType type)
std::string_view getCompany(const char *buf) const
std::string_view getRemark(const char *buf) const
std::string_view getCountry(const char *buf) const
static RomType nameToRomType(std::string_view name)
std::string_view getOrigType(const char *buf) const
TclObject getListIndexUnchecked(unsigned index) const
std::optional< TclObject > getOptionalDictValue(const TclObject &key) const
zstring_view getString() const
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr auto empty() const
mat3 p3(vec3(1, 2, 3), vec3(4, 5, 6), vec3(7, 0, 9))
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
auto CalcTextSize(std::string_view str)
void TextUnformatted(const std::string &str)
bool stringToBool(string_view str)
std::pair< string_view, string_view > splitOnLast(string_view str, string_view chars)
T length(const vecN< N, T > &x)
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 VisuallyDisabled(bool b, std::invocable<> auto next)
void ID(const char *str_id, 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 StyleColor(bool active, Args &&...args)
void Child(const char *str_id, const ImVec2 &size, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags, std::invocable<> auto next)
void TextWrapPos(float wrap_local_pos_x, std::invocable<> auto next)
bool Menu(const char *label, bool enabled, std::invocable<> auto next)
void Disabled(bool b, std::invocable<> auto next)
void Group(std::invocable<> auto next)
void Indent(float indent_w, std::invocable<> auto next)
void ListClipper(size_t count, int forceIndex, float lineHeight, std::invocable< int > auto next)
void ItemTooltip(std::invocable<> auto next)
void format(SectorAccessibleDisk &disk, MSXBootSectorType bootType)
Format the given disk (= a single partition).
string_view getFilename(string_view path)
Returns the file portion of a path name.
This file implemented 3 utility functions:
bool Checkbox(const HotKey &hotKey, BooleanSetting &setting)
bool loadOnePersistent(std::string_view name, zstring_view value, C &c, const std::tuple< Elements... > &tup)
void applyComboFilter(std::string_view key, std::string_view value, const std::vector< T > &items, std::vector< size_t > &indices)
void addRecentItem(circular_buffer< T > &recentItems, const T &item)
void simpleToolTip(std::string_view desc)
void savePersistent(ImGuiTextBuffer &buf, C &c, const std::tuple< Elements... > &tup)
void HelpMarker(std::string_view desc)
ImU32 getColor(imColor col)
void applyDisplayNameFilter(std::string_view filterString, const std::vector< T > &items, std::vector< size_t > &indices)
TclObject makeTclList(Args &&... args)
bool ButtonWithCustomRendering(const char *label, gl::vec2 size, bool pressed, std::invocable< gl::vec2, ImDrawList * > auto render)
auto find(InputRange &&range, const T &value)
auto lower_bound(ForwardRange &&range, const T &value, Compare comp={}, Proj proj={})
void parse(HANDLER &handler, char *xml)
size_t size(std::string_view utf8)
constexpr auto transform(Range &&range, UnaryOp op)
auto to_vector(Range &&range) -> std::vector< detail::ToVectorType< T, decltype(std::begin(range))> >
constexpr auto to_underlying(E e) noexcept
TemporaryString tmpStrCat(Ts &&... ts)
void strAppend(std::string &result, Ts &&...ts)
constexpr auto xrange(T e)