32#include <CustomFont.h>
34#include <imgui_stdlib.h>
41using namespace std::literals;
50 if (item.
name.empty())
return;
51 buf.appendf(
"%s.name=%s\n", name.c_str(), item.
name.c_str());
53 buf.appendf(
"%s.patch=%s\n", name.c_str(), patch.c_str());
56 buf.appendf(
"%s.romType=%s\n", name.c_str(),
61 saveItem(group.
edit, name);
62 auto recentName =
tmpStrCat(name,
".recent");
63 for (
const auto& item : group.
recent) {
64 saveItem(item, recentName);
71 for (
const auto& info : diskMediaInfo) {
76 if (info.show) buf.appendf(
"%s.show=1\n", name.c_str());
81 for (
const auto& info : cartridgeMediaInfo) {
85 if (info.show) buf.appendf(
"%s.show=1\n", name.c_str());
90 for (
const auto& info : hdMediaInfo) {
91 saveGroup(info, name);
96 for (
const auto& info : cdMediaInfo) {
97 saveGroup(info, name);
101 if (cassetteMediaInfo.
show) buf.append(
"cassette.show=1\n");
102 saveGroup(cassetteMediaInfo.
group,
"cassette");
104 saveGroup(extensionMediaInfo,
"extension");
105 saveGroup(laserdiscMediaInfo,
"laserdisc");
110 auto get = [&](std::string_view prefix,
auto& array) -> std::remove_reference_t<
decltype(array[0])>* {
111 if ((name.size() >= (prefix.size() + 2)) && name.starts_with(prefix) && (name[prefix.size() + 1] ==
'.')) {
112 char c = name[prefix.size()];
113 if ((
'a' <= c) && (c < char(
'a' + array.size()))) {
114 return &array[c -
'a'];
119 auto loadItem = [&](
MediaItem& item, std::string_view suffix) {
120 if (suffix ==
"name") {
122 }
else if (suffix ==
"patch") {
124 }
else if (suffix ==
"romType") {
130 auto loadGroup = [&](
ItemGroup& group, std::string_view suffix) {
131 if (suffix.starts_with(
"recent.")) {
132 if (suffix ==
"recent.name" && !group.
recent.full()) {
135 if (!group.
recent.empty()) {
136 loadItem(group.
recent.back(), suffix.substr(7));
139 loadItem(group.
edit, suffix);
145 }
else if (
auto* disk = get(
"disk", diskMediaInfo)) {
147 auto suffix = name.substr(6);
148 if (suffix.starts_with(
"image.")) {
149 loadGroup(disk->groups[
IMAGE], suffix.substr(6));
150 }
else if (suffix.starts_with(
"dirAsDsk.")) {
151 loadGroup(disk->groups[
DIR_AS_DISK], suffix.substr(9));
152 }
else if (suffix ==
"select") {
153 if (
auto i = StringOp::stringTo<unsigned>(value)) {
158 }
else if (suffix ==
"show") {
161 }
else if (
auto* cart = get(
"cart", cartridgeMediaInfo)) {
163 auto suffix = name.substr(6);
164 if (suffix.starts_with(
"rom.")) {
165 loadGroup(cart->groups[
IMAGE], suffix.substr(4));
166 }
else if (suffix.starts_with(
"extension.")) {
167 loadGroup(cart->groups[
EXTENSION], suffix.substr(10));
168 }
else if (suffix ==
"select") {
169 if (
auto i = StringOp::stringTo<unsigned>(value)) {
174 }
else if (suffix ==
"show") {
177 }
else if (
auto* hd = get(
"hd", hdMediaInfo)) {
178 loadGroup(*hd, name.substr(4));
179 }
else if (
auto* cd = get(
"cd", cdMediaInfo)) {
180 loadGroup(*cd, name.substr(4));
181 }
else if (name.starts_with(
"cassette.")) {
182 auto suffix = name.substr(9);
183 if (suffix ==
"show") {
186 loadGroup(cassetteMediaInfo.
group, suffix);
188 }
else if (name.starts_with(
"extension.")) {
189 loadGroup(extensionMediaInfo, name.substr(10));
190 }
else if (name.starts_with(
"laserdisc.")) {
191 loadGroup(laserdiscMediaInfo, name.substr(10));
195static std::string buildFilter(std::string_view description, std::span<const std::string_view> extensions)
197 auto formatExtensions = [&]() -> std::string {
198 if (extensions.size() <= 3) {
200 [](
const auto& ext) {
return strCat(
"*.", ext); }),
203 return join(extensions,
',');
207 description,
" (", formatExtensions(),
"){",
209 [](
const auto& ext) {
return strCat(
'.', ext); }),
219static std::string romFilter()
224static std::string cassetteFilter()
229static std::string hdFilter()
231 return buildFilter(
"Hard disk images", std::array{
"dsk"sv});
234static std::string cdFilter()
236 return buildFilter(
"CDROM images", std::array{
"iso"sv});
239template<std::invocable<const std::
string&> DisplayFunc = std::
identity>
240static std::string display(
const ImGuiMedia::MediaItem& item, DisplayFunc displayFunc = {})
242 std::string result = displayFunc(item.name);
246 if (
auto n = item.ipsPatches.size()) {
247 strAppend(result,
" (+", n,
" patch", (n == 1 ?
"" :
"es"),
')');
254 if (extensionInfo.empty()) {
255 extensionInfo = parseAllConfigFiles<ExtensionInfo>(
manager,
"extensions", {
"Manufacturer"sv,
"Product code"sv,
"Name"sv});
257 return extensionInfo;
262 extensionInfo.clear();
279 std::optional<MSXMotherBoard> mb;
283 mb->getMSXCliComm().setSuppressMessages(
true);
285 mb->loadMachine(
"C-BIOS_MSX1");
290 if (
const auto* current = reactor.getMotherBoard()) {
292 mb->getMSXCliComm().setSuppressMessages(true);
293 mb->loadMachine(std::string(current->getMachineName()));
302 auto ext = mb->loadExtension(info.
configName,
"any");
303 mb->insertExtension(info.
configName, std::move(ext));
318 return (it != allExtensions.end()) ? std::to_address(it) :
nullptr;
324 return info ? info->displayName
325 : std::string(config);
331 if (
auto sha1 = reactor.getFilePool().getSha1Sum(filename)) {
333 if (
const auto* romInfo = database.fetchRomInfo(*sha1)) {
334 if (
auto title = romInfo->getTitle(database.getBufferStart());
336 return std::string(title);
364 std::string result = slot
374 std::string_view display;
376 display = result->getListIndexUnchecked(1).getString();
378 return display.empty() ?
"Empty"
383void ImGuiMedia::printExtensionInfo(ExtensionInfo& info)
386 bool ok =
test.empty();
389 ImGui::TableSetupColumn(
"description", ImGuiTableColumnFlags_WidthFixed);
390 ImGui::TableSetupColumn(
"value", ImGuiTableColumnFlags_WidthStretch);
392 for (
const auto& [desc, value_] : info.configInfo) {
393 const auto& value = value_;
394 if (ImGui::TableNextColumn()) {
397 if (ImGui::TableNextColumn()) {
413void ImGuiMedia::extensionTooltip(ExtensionInfo& info)
416 printExtensionInfo(info);
420bool ImGuiMedia::drawExtensionFilter()
422 std::string filterDisplay =
"filter";
423 if (!filterType.empty() || !filterString.empty())
strAppend(filterDisplay,
':');
424 if (!filterType.empty())
strAppend(filterDisplay,
' ', filterType);
425 if (!filterString.empty())
strAppend(filterDisplay,
' ', filterString);
427 bool newFilterOpen = filterOpen;
428 im::TreeNode(filterDisplay.c_str(), &newFilterOpen, [&]{
429 displayFilterCombo(filterType,
"Type", getAllExtensions());
430 ImGui::InputText(ICON_IGFD_FILTER, &filterString);
431 simpleToolTip(
"A list of substrings that must be part of the extension.\n"
433 "For example: enter 'ko' to search for 'Konami' extensions. "
434 "Then refine the search by appending '<space>sc' to find the 'Konami SCC' extension.");
436 bool changed = filterOpen != newFilterOpen;
437 filterOpen = newFilterOpen;
443 im::Menu(
"Media", motherBoard !=
nullptr, [&]{
446 enum class Status {
NONE, ITEM, SEPARATOR };
448 Status status =
NONE;
450 auto endGroup = [&] {
451 if (status == ITEM) status = SEPARATOR;
453 auto elementInGroup = [&] {
454 if (status == SEPARATOR) {
460 auto showCurrent = [&](
TclObject current, std::string_view type) {
461 if (current.
empty()) {
469 auto showRecent = [&](std::string_view mediaName,
ItemGroup& group,
470 function_ref<std::string(
const std::string&)> displayFunc = std::identity{},
471 const std::function<void(
const std::string&)>& toolTip = {}) {
472 if (!group.recent.empty()) {
476 for (
const auto& item : group.recent) {
477 auto d =
strCat(display(item, displayFunc),
"##", count++);
478 if (ImGui::MenuItem(d.c_str())) {
480 insertMedia(mediaName, group.edit);
482 if (toolTip) toolTip(item.name);
492 bool anySlot =
false;
494 if (!slotManager.slotExists(i))
continue;
496 auto [ps, ss] = slotManager.
getPsSs(i);
497 std::string extraInfo = ss == -1 ?
"" :
strCat(
" (", slotManager.getPsSsString(i),
")");
498 auto displayName =
strCat(
"Cartridge Slot ",
char(
'A' + i), extraInfo);
499 ImGui::MenuItem(displayName.c_str(),
nullptr, &cartridgeMediaInfo[i].show);
503 ImGui::TextDisabled(
"No cartridge slots present");
510 auto mediaName =
"ext"sv;
511 auto& group = extensionMediaInfo;
514 HelpMarker(
"Note that some extensions are I/O only and will not occupy any cartridge slot when inserted. "
515 "These can only be removed via the 'Media > Extensions > Remove' menu. "
516 "To insert (non I/O-only) extensions in a specific slot, use the 'Media > Cartridge Slot' menu.");
517 drawExtensionFilter();
524 float width = 40.0f * ImGui::GetFontSize();
525 float height = 10.25f * ImGui::GetTextLineHeightWithSpacing();
528 auto& ext = allExtensions[filteredExtensions[i]];
529 bool ok = getTestResult(ext).empty();
530 im::StyleColor(!ok, ImGuiCol_Text, getColor(imColor::ERROR), [&]{
531 if (ImGui::Selectable(ext.displayName.c_str())) {
532 group.edit.name = ext.configName;
533 insertMedia(mediaName, group.edit);
534 ImGui::CloseCurrentPopup();
536 extensionTooltip(ext);
542 showRecent(mediaName, group,
543 [
this](
const std::string& config) {
546 [
this](
const std::string& e) {
548 extensionTooltip(*info);
556 im::Menu(
"Remove", [&]{
558 for (const auto& ext : extensions) {
559 auto name = strCat(slotAndNameForHardwareConfig(slotManager, *ext),
"##", count++);
560 if (ImGui::Selectable(name.c_str())) {
561 manager.executeDelayed(makeTclList(
"remove_extension", ext->getName()));
563 if (auto* info = findExtensionInfo(ext->getConfigName())) {
564 extensionTooltip(*info);
575 bool anyDrive =
false;
577 if (!(*drivesInUse)[i])
continue;
579 auto displayName =
strCat(
"Disk Drive ",
char(
'A' + i));
580 ImGui::MenuItem(displayName.c_str(),
nullptr, &diskMediaInfo[i].show);
581 simpleToolTip([&] {
return displayNameForDriveContent(i); });
584 ImGui::TextDisabled(
"No disk drives present");
590 if (
auto cmdResult = manager.execute(TclObject(
"cassetteplayer"))) {
591 ImGui::MenuItem(
"Tape Deck",
nullptr, &cassetteMediaInfo.show);
593 auto tip = cmdResult->getListIndexUnchecked(1).getString();
594 return !tip.empty() ? std::string(tip) :
"Empty";
597 ImGui::TextDisabled(
"No cassette port present");
603 std::string hdName =
"hdX";
605 if (!(*hdInUse)[i])
continue;
606 hdName.back() = char(
'a' + i);
607 auto displayName =
strCat(
"Hard Disk ",
char(
'A' + i));
608 if (
auto cmdResult = manager.execute(TclObject(hdName))) {
610 auto& group = hdMediaInfo[i];
612 auto currentImage = cmdResult->getListIndex(interp, 1);
613 showCurrent(currentImage,
"hard disk");
614 bool powered = motherBoard->isPowered();
615 im::Disabled(powered, [&]{
616 if (ImGui::MenuItem(
"Select hard disk image...")) {
617 manager.openFile->selectFile(
618 "Select image for " + displayName,
620 [this, &group, hdName](const auto& fn) {
621 group.edit.name = fn;
622 this->insertMedia(hdName, group.edit);
624 currentImage.getString());
628 HelpMarker(
"Hard disk image cannot be switched while the MSX is powered on.");
631 showRecent(hdName, group);
639 auto cdInUse = IDECDROM::getDrivesInUse(*motherBoard);
640 std::string cdName =
"cdX";
642 if (!(*cdInUse)[i])
continue;
643 cdName.back() = char(
'a' + i);
644 auto displayName =
strCat(
"CDROM Drive ",
char(
'A' + i));
645 if (
auto cmdResult = manager.execute(TclObject(cdName))) {
647 auto& group = cdMediaInfo[i];
649 auto currentImage = cmdResult->getListIndex(interp, 1);
650 showCurrent(currentImage,
"CDROM");
651 if (ImGui::MenuItem(
"Eject", nullptr, false, !currentImage.empty())) {
652 manager.executeDelayed(makeTclList(cdName,
"eject"));
654 if (ImGui::MenuItem(
"Insert CDROM image...")) {
655 manager.openFile->selectFile(
656 "Select CDROM image for " + displayName,
658 [
this, &group, cdName](
const auto& fn) {
659 group.edit.name = fn;
660 this->insertMedia(cdName, group.edit);
662 currentImage.getString());
664 showRecent(cdName, group);
671 if (
auto cmdResult = manager.execute(TclObject(
"laserdiscplayer"))) {
674 auto currentImage = cmdResult->getListIndex(interp, 1);
675 showCurrent(currentImage,
"laserdisc");
676 if (ImGui::MenuItem(
"eject",
nullptr,
false, !currentImage.empty())) {
677 manager.executeDelayed(
makeTclList(
"laserdiscplayer",
"eject"));
679 if (ImGui::MenuItem(
"Insert LaserDisc image...")) {
680 manager.openFile->selectFile(
681 "Select LaserDisc image",
682 buildFilter(
"LaserDisc images", std::array<std::string_view, 1>{
"ogv"}),
683 [
this](
const auto& fn) {
684 laserdiscMediaInfo.edit.name = fn;
685 this->insertMedia(
"laserdiscplayer", laserdiscMediaInfo.edit);
687 currentImage.getString());
689 showRecent(
"laserdiscplayer", laserdiscMediaInfo);
698 if (!motherBoard)
return;
700 auto drivesInUse = RealDrive::getDrivesInUse(*motherBoard);
701 for (
auto i :
xrange(RealDrive::MAX_DRIVES)) {
702 if (!(*drivesInUse)[i])
continue;
703 if (diskMediaInfo[i].show) {
709 for (
auto i :
xrange(CartridgeSlotManager::MAX_SLOTS)) {
710 if (!slotManager.slotExists(i))
continue;
711 if (cartridgeMediaInfo[i].show) {
716 if (cassetteMediaInfo.show) {
717 if (
auto cmdResult = manager.execute(
TclObject(
"cassetteplayer"))) {
718 cassetteMenu(*cmdResult);
728static void printPatches(
const TclObject& patches)
730 if (!patches.empty()) {
733 for (
const auto& patch : patches) {
740static std::string leftClip(std::string_view s,
float maxWidth)
743 if (fullWidth <= maxWidth)
return std::string(s);
746 if (maxWidth <= 0.0f)
return "...";
751 return strCat(
"...", s.substr(len - num));
754bool ImGuiMedia::selectRecent(ItemGroup& group,
function_ref<std::string(
const std::string&)> displayFunc,
float width)
const
756 bool interacted =
false;
757 ImGui::SetNextItemWidth(-width);
758 const auto& style = ImGui::GetStyle();
759 auto textWidth = ImGui::GetContentRegionAvail().x - (3.0f * style.FramePadding.x + ImGui::GetFrameHeight() + width);
760 auto preview = leftClip(displayFunc(group.edit.name), textWidth);
761 im::Combo(
"##recent", preview.c_str(), [&]{
763 for (const auto& item : group.recent) {
764 auto d = strCat(display(item, displayFunc),
"##", count++);
765 if (ImGui::Selectable(d.c_str())) {
771 interacted |= ImGui::IsItemActive();
775static float calcButtonWidth(std::string_view text1,
const char* text2)
777 const auto& style = ImGui::GetStyle();
778 float width = style.ItemSpacing.x + 2.0f * style.FramePadding.x +
ImGui::CalcTextSize(text1).x;
785bool ImGuiMedia::selectImage(ItemGroup& group,
const std::string& title,
787 function_ref<std::string(
const std::string&)> displayFunc,
788 const std::function<
void()>& createNewCallback)
790 bool interacted =
false;
792 auto width = calcButtonWidth(ICON_IGFD_FOLDER_OPEN, createNewCallback ? ICON_IGFD_ADD : nullptr);
793 interacted |= selectRecent(group, displayFunc, width);
794 if (createNewCallback) {
796 if (ImGui::Button(ICON_IGFD_ADD)) {
803 if (ImGui::Button(ICON_IGFD_FOLDER_OPEN)) {
805 manager.openFile->selectFile(
808 [&](
const auto& fn) { group.edit.name = fn; },
816bool ImGuiMedia::selectDirectory(ItemGroup& group,
const std::string& title,
zstring_view current,
817 const std::function<
void()>& createNewCallback)
819 bool interacted =
false;
821 auto width = calcButtonWidth(ICON_IGFD_FOLDER_OPEN, createNewCallback ? ICON_IGFD_ADD : nullptr);
822 interacted |= selectRecent(group, std::identity{}, width);
823 if (createNewCallback) {
825 if (ImGui::Button(ICON_IGFD_ADD)) {
832 if (ImGui::Button(ICON_IGFD_FOLDER_OPEN)) {
834 manager.openFile->selectDirectory(
836 [&](
const auto& fn) { group.edit.name = fn; },
844bool ImGuiMedia::selectMapperType(
const char* label,
RomType& romType)
846 bool interacted =
false;
847 bool isAutoDetect = romType == RomType::UNKNOWN;
848 constexpr const char* autoStr =
"auto detect";
849 std::string current = isAutoDetect ? autoStr : std::string(RomInfo::romTypeToName(romType));
851 if (ImGui::Selectable(autoStr, isAutoDetect)) {
853 romType = RomType::UNKNOWN;
856 for (
const auto& romInfo : RomInfo::getRomTypeInfo()) {
857 bool selected = romType ==
static_cast<RomType>(count);
858 if (ImGui::Selectable(std::string(romInfo.name).c_str(), selected)) {
860 romType =
static_cast<RomType>(count);
866 interacted |= ImGui::IsItemActive();
870bool ImGuiMedia::selectPatches(MediaItem& item,
int& patchIndex)
872 bool interacted =
false;
873 std::string patchesTitle =
"IPS patches";
874 if (!item.ipsPatches.empty()) {
875 strAppend(patchesTitle,
" (", item.ipsPatches.size(),
')');
879 const auto& style = ImGui::GetStyle();
880 auto width = style.ItemSpacing.x + 2.0f * style.FramePadding.x + ImGui::CalcTextSize(
"Remove"sv).x;
881 ImGui::SetNextItemWidth(-width);
883 im::ListBox(
"##", [&]{
885 for (const auto& patch : item.ipsPatches) {
886 auto preview = leftClip(patch, ImGui::GetContentRegionAvail().x);
887 if (ImGui::Selectable(strCat(preview,
"##", count).c_str(), count == patchIndex)) {
897 if (ImGui::Button(
"Add")) {
899 manager.openFile->selectFile(
900 "Select disk IPS patch",
901 buildFilter(
"IPS patches", std::array<std::string_view, 1>{
"ips"}),
902 [&](
const std::string& ips) {
903 patchIndex = narrow<int>(item.ipsPatches.size());
904 item.ipsPatches.push_back(ips);
907 auto size = narrow<int>(item.ipsPatches.size());
908 im::Disabled(patchIndex < 0 || patchIndex >= size, [&] {
909 if (ImGui::Button(
"Remove")) {
911 item.ipsPatches.erase(item.ipsPatches.begin() + patchIndex);
914 if (ImGui::ArrowButton(
"up", ImGuiDir_Up)) {
915 std::swap(item.ipsPatches[patchIndex], item.ipsPatches[patchIndex - 1]);
920 if (ImGui::ArrowButton(
"down", ImGuiDir_Down)) {
921 std::swap(item.ipsPatches[patchIndex], item.ipsPatches[patchIndex + 1]);
931bool ImGuiMedia::insertMediaButton(std::string_view mediaName, ItemGroup& group,
bool* showWindow)
933 bool clicked =
false;
934 im::Disabled(group.edit.name.empty() && !group.edit.isEject(), [&]{
935 const auto& style = ImGui::GetStyle();
936 auto width = 4.0f * style.FramePadding.x + style.ItemSpacing.x +
937 ImGui::CalcTextSize(
"Apply"sv).x + ImGui::CalcTextSize(
"Ok"sv).x;
938 ImGui::SetCursorPosX(ImGui::GetContentRegionAvail().x - width + style.WindowPadding.x);
939 clicked |= ImGui::Button(
"Apply");
941 if (ImGui::Button(
"Ok")) {
946 insertMedia(mediaName, group.edit);
952TclObject ImGuiMedia::showDiskInfo(std::string_view mediaName, DiskMediaInfo& info)
954 TclObject currentTarget;
955 auto cmdResult = manager.execute(
makeTclList(
"machine_info",
"media", mediaName));
956 if (!cmdResult)
return currentTarget;
958 using enum SelectDiskType;
959 auto selectType = [&]{
962 auto s = type->getString();
965 }
else if (s ==
"ramdsk") {
967 }
else if (s ==
"dirasdisk") {
974 std::string_view typeStr = [&]{
975 switch (selectType) {
976 case IMAGE:
return "Disk image:";
977 case DIR_AS_DISK:
return "Dir as disk:";
978 case RAMDISK:
return "RAM disk";
979 case EMPTY:
return "No disk inserted";
983 bool detailedInfo = selectType ==
one_of(DIR_AS_DISK, IMAGE);
984 auto currentPatches = getPatches(*cmdResult);
986 bool copyCurrent = ImGui::SmallButton(
"Current disk");
987 HelpMarker(
"Press to copy current disk to 'Select new disk' section.");
993 currentTarget = *target;
996 ImGui::GetContentRegionAvail().x));
998 std::string statusLine;
999 auto add = [&](std::string_view s) {
1000 if (statusLine.empty()) {
1007 if (ro->getOptionalBool().value_or(
false)) {
1012 add(doubleSided->getOptionalBool().value_or(
true) ?
"double-sided" :
"single-sided");
1015 add(
tmpStrCat(
size->getOptionalInt().value_or(0) / 1024,
"kB"));
1017 if (!statusLine.empty()) {
1020 printPatches(currentPatches);
1024 info.select = selectType;
1025 auto& edit = info.groups[selectType].edit;
1026 edit.name = currentTarget.getString();
1027 edit.ipsPatches = to_vector<std::string>(currentPatches);
1030 return currentTarget;
1033void ImGuiMedia::printDatabase(
const RomInfo& romInfo,
const char* buf)
1035 auto printRow = [](std::string_view description, std::string_view value) {
1036 if (value.empty())
return;
1037 if (ImGui::TableNextColumn()) {
1040 if (ImGui::TableNextColumn()) {
1045 printRow(
"Title", romInfo.
getTitle(buf));
1046 printRow(
"Year", romInfo.
getYear(buf));
1047 printRow(
"Company", romInfo.
getCompany(buf));
1048 printRow(
"Country", romInfo.
getCountry(buf));
1052 std::string result =
"Unmodified dump";
1054 strAppend(result,
" (confirmed by ", str,
')');
1058 return std::string(str);
1061 printRow(
"Status", status);
1062 printRow(
"Remark", romInfo.
getRemark(buf));
1068 ImGui::TableSetupColumn(
"description", ImGuiTableColumnFlags_WidthFixed);
1069 ImGui::TableSetupColumn(
"value", ImGuiTableColumnFlags_WidthStretch);
1071 if (ImGui::TableNextColumn()) {
1074 if (ImGui::TableNextColumn()) {
1079 const auto* romInfo = [&]() ->
const RomInfo* {
1081 if (
const auto* info = database.fetchRomInfo(Sha1Sum(actual->getString()))) {
1086 if (
const auto* info = database.fetchRomInfo(Sha1Sum(original->getString()))) {
1093 ImGuiMedia::printDatabase(*romInfo, database.getBufferStart());
1096 std::string mapperStr{RomInfo::romTypeToName(romType)};
1098 if (
auto dbType = romInfo->getRomType();
1099 dbType != RomType::UNKNOWN && dbType != romType) {
1100 strAppend(mapperStr,
" (database: ", RomInfo::romTypeToName(dbType),
')');
1103 if (ImGui::TableNextColumn()) {
1106 if (ImGui::TableNextColumn()) {
1112TclObject ImGuiMedia::showCartridgeInfo(std::string_view mediaName, CartridgeMediaInfo& info,
int slot)
1114 TclObject currentTarget;
1116 if (!cmdResult)
return currentTarget;
1118 using enum SelectCartridgeType;
1119 auto selectType = [&]{
1121 auto s = type->getString();
1122 if (s ==
"extension") {
1132 auto currentPatches = getPatches(*cmdResult);
1134 bool copyCurrent = ImGui::SmallButton(
"Current cartridge");
1139 RomType currentRomType = RomType::UNKNOWN;
1141 if (selectType == EMPTY) {
1144 currentTarget = *target;
1145 if (selectType == EXTENSION) {
1146 if (
auto* i = findExtensionInfo(target->getString())) {
1147 printExtensionInfo(*i);
1149 }
else if (selectType == IMAGE) {
1151 currentRomType = RomInfo::nameToRomType(mapper->getString());
1153 printRomInfo(manager, *cmdResult, target->getString(), currentRomType);
1154 printPatches(currentPatches);
1159 info.select = selectType;
1160 auto& edit = info.groups[selectType].edit;
1161 edit.name = currentTarget.getString();
1162 edit.ipsPatches = to_vector<std::string>(currentPatches);
1163 edit.romType = currentRomType;
1166 return currentTarget;
1169void ImGuiMedia::diskMenu(
int i)
1171 auto& info = diskMediaInfo[i];
1172 auto mediaName =
strCat(
"disk",
char(
'a' + i));
1173 auto displayName =
strCat(
"Disk Drive ",
char(
'A' + i));
1174 ImGui::SetNextWindowSize(
gl::vec2{29, 22} * ImGui::GetFontSize(), ImGuiCond_FirstUseEver);
1175 im::Window(displayName.c_str(), &info.show, [&]{
1176 auto current = showDiskInfo(mediaName, info);
1177 im::Child(
"select", {0, -ImGui::GetFrameHeightWithSpacing()}, [&]{
1178 using enum SelectDiskType;
1179 ImGui::TextUnformatted(
"Select new disk"sv);
1181 ImGui::RadioButton(
"disk image", std::bit_cast<int*>(&info.select), to_underlying(IMAGE));
1182 im::VisuallyDisabled(info.select != IMAGE, [&]{
1184 auto& group = info.groups[IMAGE];
1185 auto createNew = [&]{
1186 manager.openFile->selectNewFile(
1187 "Select name for new blank disk image",
1188 "Disk images (*.dsk){.dsk}",
1189 [&](const auto& fn) {
1190 group.edit.name = fn;
1191 auto& diskManipulator = manager.getReactor().getDiskManipulator();
1193 diskManipulator.create(fn, MSXBootSectorType::DOS2, {1440});
1194 } catch (MSXException& e) {
1195 manager.printError(
"Couldn't create new disk image: ", e.getMessage());
1198 current.getString());
1200 bool interacted = selectImage(
1201 group, strCat(
"Select disk image for ", displayName), &diskFilter,
1202 current.getString(), std::identity{}, createNew);
1203 interacted |= selectPatches(group.edit, group.patchIndex);
1204 if (interacted) info.select = IMAGE;
1207 ImGui::RadioButton(
"dir as disk", std::bit_cast<int*>(&info.select),
to_underlying(DIR_AS_DISK));
1210 auto& group = info.groups[DIR_AS_DISK];
1211 auto createNew = [&]{
1212 manager.openFile->selectNewFile(
1213 "Select name for new empty directory",
1215 [&](const auto& fn) {
1216 group.edit.name = fn;
1218 FileOperations::mkdirp(fn);
1219 } catch (MSXException& e) {
1220 manager.printError(
"Couldn't create directory: ", e.getMessage());
1223 current.getString());
1225 bool interacted = selectDirectory(
1226 group, strCat(
"Select directory for ", displayName),
1227 current.getString(), createNew);
1228 if (interacted) info.select = DIR_AS_DISK;
1231 ImGui::RadioButton(
"RAM disk", std::bit_cast<int*>(&info.select),
to_underlying(RAMDISK));
1232 if (!current.
empty()) {
1233 ImGui::RadioButton(
"Eject", std::bit_cast<int*>(&info.select),
to_underlying(EMPTY));
1236 insertMediaButton(mediaName, info.groups[info.select], &info.show);
1240void ImGuiMedia::cartridgeMenu(
int cartNum)
1242 auto& info = cartridgeMediaInfo[cartNum];
1243 auto displayName =
strCat(
"Cartridge Slot ",
char(
'A' + cartNum));
1244 ImGui::SetNextWindowSize(
gl::vec2{37, 30} * ImGui::GetFontSize(), ImGuiCond_FirstUseEver);
1245 im::Window(displayName.c_str(), &info.show, [&]{
1246 using enum SelectCartridgeType;
1247 auto cartName = strCat(
"cart", char(
'a' + cartNum));
1248 auto extName = strCat(
"ext", char(
'a' + cartNum));
1250 auto current = showCartridgeInfo(cartName, info, cartNum);
1252 im::Child(
"select", {0, -ImGui::GetFrameHeightWithSpacing()}, [&]{
1253 ImGui::TextUnformatted(
"Select new cartridge:"sv);
1255 ImGui::RadioButton(
"ROM image", std::bit_cast<int*>(&info.select), to_underlying(IMAGE));
1256 im::VisuallyDisabled(info.select != IMAGE, [&]{
1258 auto& group = info.groups[IMAGE];
1259 auto& item = group.edit;
1260 bool interacted = selectImage(
1261 group, strCat(
"Select ROM image for ", displayName), &romFilter, current.getString());
1263 const auto& style = ImGui::GetStyle();
1264 ImGui::SetNextItemWidth(-(ImGui::CalcTextSize(
"mapper-type").x + style.ItemInnerSpacing.x));
1265 interacted |= selectMapperType(
"mapper-type", item.romType);
1266 interacted |= selectPatches(item, group.patchIndex);
1267 interacted |= ImGui::Checkbox(
"Reset MSX on inserting ROM", &resetOnInsertRom);
1268 if (interacted) info.select = IMAGE;
1271 ImGui::RadioButton(
"extension", std::bit_cast<int*>(&info.select),
to_underlying(EXTENSION));
1274 auto& allExtensions = getAllExtensions();
1275 auto& group = info.groups[EXTENSION];
1276 auto& item = group.edit;
1278 bool interacted = drawExtensionFilter();
1280 auto drawExtensions = [&]{
1281 auto filteredExtensions = to_vector(xrange(allExtensions.size()));
1282 applyComboFilter(
"Type", filterType, allExtensions, filteredExtensions);
1283 applyDisplayNameFilter(filterString, allExtensions, filteredExtensions);
1285 im::ListClipper(filteredExtensions.size(), [&](int i) {
1286 auto& ext = allExtensions[filteredExtensions[i]];
1287 bool ok = getTestResult(ext).empty();
1288 im::StyleColor(!ok, ImGuiCol_Text, getColor(imColor::ERROR), [&]{
1289 if (ImGui::Selectable(ext.displayName.c_str(), item.name == ext.configName)) {
1291 item.name = ext.configName;
1293 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
1294 insertMedia(extName, group.edit);
1296 extensionTooltip(ext);
1301 im::ListBox(
"##list", [&]{
1305 im::Combo(
"##extension", displayNameForExtension(item.name).c_str(), [&]{
1310 interacted |= ImGui::IsItemActive();
1311 if (interacted) info.select = EXTENSION;
1314 if (!current.
empty()) {
1315 ImGui::RadioButton(
"Eject", std::bit_cast<int*>(&info.select),
to_underlying(EMPTY));
1318 if (insertMediaButton(info.select == EXTENSION ? extName : cartName,
1319 info.groups[info.select], &info.show)) {
1320 if (resetOnInsertRom && info.select == IMAGE) {
1327static void addRecentItem(ImGuiMedia::ItemGroup& group,
const ImGuiMedia::MediaItem& item)
1329 auto& recent = group.recent;
1330 if (
auto it2 =
ranges::find(recent, item); it2 != recent.end()) {
1332 std::rotate(recent.begin(), it2, it2 + 1);
1335 if (recent.full()) recent.pop_back();
1336 recent.push_front(item);
1340static bool ButtonWithCustomRendering(
1341 const char* label,
gl::vec2 size,
bool pressed,
1342 std::invocable<gl::vec2 /*center*/, ImDrawList*>
auto render)
1344 bool result =
false;
1345 im::StyleColor(pressed, ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_ButtonActive), [&]{
1346 gl::vec2 topLeft = ImGui::GetCursorScreenPos();
1348 result = ImGui::Button(label, size);
1349 render(center, ImGui::GetWindowDrawList());
1354static void RenderPlay(
gl::vec2 center, ImDrawList* drawList)
1356 float half = 0.4f * ImGui::GetTextLineHeight();
1357 auto p1 = center +
gl::vec2(half, 0.0f);
1358 auto p2 = center +
gl::vec2(-half, half);
1360 drawList->AddTriangleFilled(p1, p2,
p3,
getColor(imColor::TEXT));
1362static void RenderRewind(
gl::vec2 center, ImDrawList* drawList)
1364 float size = 0.8f * ImGui::GetTextLineHeight();
1365 float half =
size * 0.5f;
1366 auto color =
getColor(imColor::TEXT);
1367 auto p1 = center +
gl::vec2(-size, 0.0f);
1368 auto p2 = center +
gl::vec2(0.0f, -half);
1370 drawList->AddTriangleFilled(p1, p2,
p3, color);
1375 drawList->AddTriangleFilled(p1, p2,
p3, color);
1377static void RenderStop(
gl::vec2 center, ImDrawList* drawList)
1379 gl::vec2 half{0.4f * ImGui::GetTextLineHeight()};
1380 drawList->AddRectFilled(center - half, center + half,
getColor(imColor::TEXT));
1382static void RenderRecord(
gl::vec2 center, ImDrawList* drawList)
1384 float radius = 0.4f * ImGui::GetTextLineHeight();
1385 drawList->AddCircleFilled(center, radius,
getColor(imColor::TEXT));
1389void ImGuiMedia::cassetteMenu(
const TclObject& cmdResult)
1391 ImGui::SetNextWindowSize(
gl::vec2{29, 20} * ImGui::GetFontSize(), ImGuiCond_FirstUseEver);
1392 auto& info = cassetteMediaInfo;
1393 auto& group = info.group;
1395 ImGui::TextUnformatted(
"Current tape"sv);
1396 auto current = cmdResult.getListIndexUnchecked(1).getString();
1398 if (current.empty()) {
1399 ImGui::TextUnformatted(
"No tape inserted"sv);
1401 ImGui::TextUnformatted(
"Tape image:"sv);
1403 ImGui::TextUnformatted(leftClip(current, ImGui::GetContentRegionAvail().x));
1407 if (ImGui::Button(
"Eject")) {
1408 manager.executeDelayed(makeTclList(
"cassetteplayer",
"eject"));
1415 auto status = cmdResult.getListIndexUnchecked(2).getString();
1416 auto size = ImGui::GetFrameHeightWithSpacing();
1417 if (ButtonWithCustomRendering(
"##Play", {2.0f *
size,
size}, status ==
"play", RenderPlay)) {
1421 if (ButtonWithCustomRendering(
"##Rewind", {2.0f *
size,
size},
false, RenderRewind)) {
1425 if (ButtonWithCustomRendering(
"##Stop", {2.0f *
size,
size}, status ==
"stop", RenderStop)) {
1429 if (ButtonWithCustomRendering(
"##Record", {2.0f *
size,
size}, status ==
"record", RenderRecord)) {
1431 "Select new wav file for record",
1432 "Tape images (*.wav){.wav}",
1433 [&](
const auto& fn) {
1434 group.edit.name = fn;
1436 [&group](
const TclObject&) {
1438 addRecentItem(group, group.edit);
1444 const auto& style = ImGui::GetStyle();
1445 ImGui::SameLine(0.0f, 3.0f * style.ItemSpacing.x);
1446 auto getFloat = [&](std::string_view subCmd) {
1447 auto r = manager.
execute(
makeTclList(
"cassetteplayer", subCmd)).value_or(TclObject(0.0));
1448 return r.getOptionalFloat().value_or(0.0f);
1450 auto length = getFloat(
"getlength");
1451 auto pos = getFloat(
"getpos");
1452 auto format = [](
float time) {
1453 int t = narrow_cast<int>(time);
1454 int s =
t % 60;
t /= 60;
1455 int m =
t % 60;
t /= 60;
1456 std::ostringstream os;
1457 os << std::setfill(
'0');
1458 if (
t) os << std::setw(2) <<
t <<
':';
1459 os << std::setw(2) << m <<
':';
1460 os << std::setw(2) << s;
1463 auto parse = [](std::string_view str) -> std::optional<unsigned> {
1465 auto s = StringOp::stringTo<unsigned>(seconds);
1467 unsigned result = *s;
1469 if (!head.empty()) {
1471 auto m = StringOp::stringTo<unsigned>(minutes);
1475 if (!hours.empty()) {
1476 auto h = StringOp::stringTo<unsigned>(hours);
1478 result += *h * 60 * 60;
1483 auto posStr =
format(pos);
1484 ImGui::SetNextItemWidth(
ImGui::CalcTextSize(std::string_view(posStr)).x + 2.0f * style.FramePadding.x);
1485 if (ImGui::InputText(
"##pos", &posStr, ImGuiInputTextFlags_EnterReturnsTrue)) {
1486 if (
auto newPos =
parse(posStr)) {
1490 simpleToolTip(
"Indicates the current position of the tape, but can be edited to change the position manual (like fast forward)");
1493 ImGui::Text(
"/ %s",
format(length).c_str());
1497 const auto& hotKey = reactor.getHotKey();
1498 if (
auto* autoRun =
dynamic_cast<BooleanSetting*
>(controller.findSetting(
"autoruncassettes"))) {
1499 Checkbox(hotKey,
"(try to) Auto Run", *autoRun);
1501 if (
auto* mute =
dynamic_cast<BooleanSetting*
>(controller.findSetting(
"cassetteplayer_ch1_mute"))) {
1502 Checkbox(hotKey,
"Mute tape audio", *mute, [](
const Setting&) {
return std::string{}; });
1507 im::Child(
"select", {0, -ImGui::GetFrameHeightWithSpacing()}, [&]{
1510 selectImage(group,
"Select tape image", &cassetteFilter, current);
1513 insertMediaButton(
"cassetteplayer", group, &info.show);
1517void ImGuiMedia::insertMedia(std::string_view mediaName,
const MediaItem& item)
1520 if (item.isEject()) {
1521 cmd.addListElement(
"eject");
1523 if (item.name.empty())
return;
1524 cmd.addListElement(
"insert", item.name);
1525 for (
const auto& patch : item.ipsPatches) {
1526 cmd.addListElement(
"-ips", patch);
1528 if (item.romType != RomType::UNKNOWN) {
1529 cmd.addListElement(
"-romtype", RomInfo::romTypeToName(item.romType));
1533 [
this, cmd](
const TclObject&) {
1541 auto n = cmd.
size();
1547 if (mediaName.starts_with(
"cart")) {
1548 if (
int i = mediaName[4] -
'a'; 0 <= i && i < int(CartridgeSlotManager::MAX_SLOTS)) {
1549 return &cartridgeMediaInfo[i].groups[SelectCartridgeType::IMAGE];
1551 }
else if (mediaName.starts_with(
"disk")) {
1552 if (
int i = mediaName[4] -
'a'; 0 <= i && i < int(RealDrive::MAX_DRIVES)) {
1553 return &diskMediaInfo[i].groups[SelectDiskType::IMAGE];
1555 }
else if (mediaName.starts_with(
"hd")) {
1556 if (
int i = mediaName[2] -
'a'; 0 <= i && i < int(HD::MAX_HD)) {
1557 return &hdMediaInfo[i];
1559 }
else if (mediaName.starts_with(
"cd")) {
1560 if (
int i = mediaName[2] -
'a'; 0 <= i && i < int(IDECDROM::MAX_CD)) {
1561 return &cdMediaInfo[i];
1563 }
else if (mediaName ==
"cassetteplayer") {
1564 return &cassetteMediaInfo.group;
1565 }
else if (mediaName ==
"laserdiscplayer") {
1566 return &laserdiscMediaInfo;
1567 }
else if (mediaName ==
"ext") {
1568 return &extensionMediaInfo;
1570 return static_cast<ItemGroup*
>(
nullptr);
1580 if (option ==
"-ips" && i < n) {
1584 if (option ==
"-romtype" && i < n) {
1590 addRecentItem(*group, item);
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()
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)
CartridgeSlotManager & getSlotManager()
MSXCommandController & getMSXCommandController()
const Extensions & getExtensions() const
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
std::string_view getCompany(const char *buf) const
std::string_view getRemark(const char *buf) const
static std::string_view romTypeToName(RomType type)
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 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)
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)