|
| 1 | +#include <hex/api/content_registry/user_interface.hpp> |
| 2 | +#include <hex/api/theme_manager.hpp> |
| 3 | +#include <hex/helpers/logger.hpp> |
| 4 | +#include <fonts/vscode_icons.hpp> |
| 5 | +#include <fonts/tabler_icons.hpp> |
| 6 | +#include <fmt/format.h> |
| 7 | +#include "lib/core/globals.h" |
| 8 | +#include "lib/config/config.h" |
| 9 | +#include "lib/data/disk.h" |
| 10 | +#include "lib/data/sector.h" |
| 11 | +#include "lib/config/proto.h" |
| 12 | +#include "globals.h" |
| 13 | +#include "visualiserview.h" |
| 14 | +#include "datastore.h" |
| 15 | +#include "utils.h" |
| 16 | +#include <implot.h> |
| 17 | +#include <implot_internal.h> |
| 18 | +#include <algorithm> |
| 19 | + |
| 20 | +using namespace hex; |
| 21 | + |
| 22 | +static constexpr float INNER_RADIUS = 50; |
| 23 | +static constexpr float SEGMENTS_PER_RADIAN = 24; |
| 24 | + |
| 25 | +VisualiserView::VisualiserView(): |
| 26 | + View::Window("fluxengine.view.visualiser.name", ICON_VS_PREVIEW) |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +void VisualiserView::drawContent() |
| 31 | +{ |
| 32 | + const auto& diskLayout = Datastore::getDiskLayout(); |
| 33 | + const auto& disk = Datastore::getDisk(); |
| 34 | + |
| 35 | + ImDrawList* drawList = ImGui::GetWindowDrawList(); |
| 36 | + auto pos = ImGui::GetCursorScreenPos(); |
| 37 | + auto size = ImGui::GetContentRegionAvail(); |
| 38 | + auto padding = ImGui::GetStyle().WindowPadding; |
| 39 | + uint32_t fg = ImGui::GetColorU32(ImGuiCol_Text); |
| 40 | + uint32_t bg = ImGui::GetColorU32(ImGuiCol_WindowBg); |
| 41 | + uint32_t diskbg = ImGui::GetColorU32(ImGuiCol_FrameBg); |
| 42 | + |
| 43 | + ImVec2 centre(pos.x + size.x / 2, pos.y + size.y / 2); |
| 44 | + float outerRadius = (size.x - padding.x * 2) / 2; |
| 45 | + ImVec2 side0pos(centre.x, centre.y - outerRadius - padding.y); |
| 46 | + ImVec2 side1pos(centre.x, centre.y + outerRadius + padding.y); |
| 47 | + |
| 48 | + auto drawCentered = |
| 49 | + [&](const ImVec2& pos, uint32_t colour, const std::string& s) |
| 50 | + { |
| 51 | + auto size = ImGui::CalcTextSize(s.c_str()); |
| 52 | + drawList->AddText({pos.x - size.x / 2, pos.y - size.y / 2}, |
| 53 | + colour, |
| 54 | + &*s.begin(), |
| 55 | + &*s.end()); |
| 56 | + }; |
| 57 | + |
| 58 | + auto drawSide = [&](int head, ImVec2 pos) |
| 59 | + { |
| 60 | + drawList->AddCircleFilled(pos, outerRadius, diskbg, 0); |
| 61 | + drawList->AddCircleFilled(pos, INNER_RADIUS, bg, 0); |
| 62 | + drawList->AddCircle(pos, outerRadius, fg, 0); |
| 63 | + drawList->AddCircle(pos, INNER_RADIUS, fg, 0); |
| 64 | + drawCentered(pos, fg, fmt::format("h{}", head)); |
| 65 | + |
| 66 | + if (!diskLayout || !disk) |
| 67 | + return; |
| 68 | + |
| 69 | + int numPhysicalTracks = |
| 70 | + diskLayout->maxPhysicalCylinder - diskLayout->minPhysicalCylinder; |
| 71 | + float trackSpacing = (float)(outerRadius - INNER_RADIUS) / |
| 72 | + (float)(numPhysicalTracks + 2); |
| 73 | + |
| 74 | + for (const auto& [ch, track] : disk->tracksByPhysicalLocation) |
| 75 | + { |
| 76 | + if (ch.head != head) |
| 77 | + continue; |
| 78 | + if (!track->fluxmap) |
| 79 | + continue; |
| 80 | + |
| 81 | + const auto& indexMarks = track->fluxmap->getIndexMarks(); |
| 82 | + nanoseconds_t rotationalPeriod; |
| 83 | + if (indexMarks.empty()) |
| 84 | + continue; |
| 85 | + if (indexMarks.size() >= 2) |
| 86 | + rotationalPeriod = indexMarks[1] - indexMarks[0]; |
| 87 | + else |
| 88 | + { |
| 89 | + rotationalPeriod = disk->rotationalPeriod; |
| 90 | + if (rotationalPeriod == 0) |
| 91 | + continue; |
| 92 | + } |
| 93 | + float radiansPerNano = IM_PI * 2.0 / rotationalPeriod; |
| 94 | + |
| 95 | + float radius = |
| 96 | + (float)outerRadius - ((float)ch.cylinder + 0.5) * trackSpacing; |
| 97 | + |
| 98 | + auto normalisePosition = [&](nanoseconds_t timestamp) |
| 99 | + { |
| 100 | + if (timestamp < indexMarks[0]) |
| 101 | + return timestamp - (indexMarks[0] - rotationalPeriod); |
| 102 | + |
| 103 | + auto it = std::upper_bound( |
| 104 | + indexMarks.begin(), indexMarks.end(), timestamp); |
| 105 | + it--; |
| 106 | + return timestamp - *it; |
| 107 | + }; |
| 108 | + |
| 109 | + auto drawArcRadians = |
| 110 | + [&](float startRadians, float endRadians, uint32_t colour) |
| 111 | + { |
| 112 | + int segments = |
| 113 | + ImClamp((endRadians - startRadians) * SEGMENTS_PER_RADIAN, |
| 114 | + 6.0F, |
| 115 | + SEGMENTS_PER_RADIAN * IM_PI * 2); |
| 116 | + drawList->PathArcTo(pos, |
| 117 | + radius, |
| 118 | + startRadians - IM_PI / 2, |
| 119 | + endRadians - IM_PI / 2, |
| 120 | + segments); |
| 121 | + drawList->PathStroke( |
| 122 | + colour, ImDrawFlags_None, trackSpacing * 0.75); |
| 123 | + }; |
| 124 | + |
| 125 | + auto drawArcNs = |
| 126 | + [&](nanoseconds_t startNs, nanoseconds_t endNs, uint32_t colour) |
| 127 | + { |
| 128 | + float startRadians = |
| 129 | + normalisePosition(startNs) * radiansPerNano; |
| 130 | + float endRadians = normalisePosition(endNs) * radiansPerNano; |
| 131 | + drawArcRadians(startRadians, endRadians, colour); |
| 132 | + }; |
| 133 | + |
| 134 | + for (const auto& sector : track->normalisedSectors) |
| 135 | + { |
| 136 | + if (sector->headerStartTime && sector->headerEndTime) |
| 137 | + drawArcNs(sector->headerStartTime, |
| 138 | + sector->headerEndTime, |
| 139 | + ImGuiExt::GetCustomColorU32( |
| 140 | + ImGuiCustomCol_AdvancedEncodingMultiChar)); |
| 141 | + if (sector->dataStartTime && sector->dataEndTime) |
| 142 | + drawArcNs(sector->dataStartTime, |
| 143 | + sector->dataEndTime, |
| 144 | + ImGuiExt::GetCustomColorU32( |
| 145 | + sector->status == Sector::OK |
| 146 | + ? ImGuiCustomCol_AdvancedEncodingASCII |
| 147 | + : ImGuiCustomCol_DiffRemoved)); |
| 148 | + } |
| 149 | + if (track->normalisedSectors.empty()) |
| 150 | + drawArcRadians(0, |
| 151 | + IM_PI * 2, |
| 152 | + ImGuiExt::GetCustomColorU32(ImGuiCustomCol_DiffRemoved)); |
| 153 | + } |
| 154 | + |
| 155 | + drawList->AddLine({pos.x, pos.y - INNER_RADIUS}, |
| 156 | + {pos.x, pos.y - outerRadius}, |
| 157 | + ImGui::GetColorU32(ImGuiCol_PlotHistogram), |
| 158 | + 1.0); |
| 159 | + }; |
| 160 | + |
| 161 | + drawSide(0, side0pos); |
| 162 | + drawSide(1, side1pos); |
| 163 | +} |
0 commit comments