// The Insert menu's mouse: what a point over it is on. The menu records each control's rectangle as it draws it (Map::add, in // the window image's pixels), and the next frame's mouse is tested against them (Map::at): the picture and the hit-test cannot // drift apart, at any size. Plain arithmetic, no Vulkan or ImGui: tests/test_menu_hit.cpp checks it, and // tests/test_menu_page_slider.py the page sliders (Bar: the scroll bar beside a list, held and drawn along by the mouse). #pragma once namespace mkwvk::menu_hit { enum Part { kNone, // off the menu's controls (still over the menu: a click does nothing) kOutside, // off the menu kTabPrev, // the tab strip's left end kTab, // a tab: index kTabNext, // its right end kRow, // an option's row: index (the tab's own), side -1 the value's left half, 1 its right half, 0 the name; // (Triforce) halves: a toggle's two halves (On | Off), side by where they meet kSlider, // a slider row's track: index, permille 0 to 1000 along it kCtlOption, // the Controls page: one of its own rows above the grid (index), side -1 the value's left half, 1 its right // half, 0 the name kCtlRow, // the Controls page: a row's name (index; index == the count: Reset to defaults) kCtlCell, // a binding: index the row, side 0 the gamepad's, 1 the keyboard's kListLine, // a list (the teleport list, the inventory): a line as shown (index, 0 the top one), side -1 the left half, 1 the right half kButton, // the restart question's buttons: index 0 Later, 1 Restart now kSearch, // ((Triforce) the Mods screen's search bar) kKey, // ((Triforce) a key of its on-screen keyboard: index row * 10 + column, the wide keys' row 4 by their first column) kScroll, // ((Triforce) a list's page slider, the scroll bar beside it: index the list's number, Map::bar_of(index) the bar) }; struct Hit { Part part = kNone; int index = -1; int side = 0; int permille = 0; bool halves = false; // ((Triforce) side is which of a toggle's halves: Map::Box::vm) }; /// A point in the window's pixels (the cursor's, in a window win_w x win_h pixels) in the window's image (img_w x img_h: /// the swapchain's; the same size as a rule, but not while a resize is on its way) inline void to_image(int x, int y, int win_w, int win_h, int img_w, int img_h, int* ix, int* iy) { *ix = win_w > 0 ? (int)((long long)x * img_w / win_w) : x; *iy = win_h > 0 ? (int)((long long)y * img_h / win_h) : y; } /// A slider's value for a point along its track (lo to hi in steps of step), nearest step, held to its ends inline int slider_value(int permille, int lo, int hi, int step) { if (step <= 0 || hi <= lo) return lo; const int steps = (hi - lo) / step; const int k = (permille * steps + 500) / 1000; return lo + (k < 0 ? 0 : k > steps ? steps : k) * step; } /// Where along the track a value is, in permille (the knob's place) inline int slider_permille(int v, int lo, int hi) { if (hi <= lo) return 0; const int p = (int)((long long)(v - lo) * 1000 / (hi - lo)); return p < 0 ? 0 : p > 1000 ? 1000 : p; } /// ((Triforce) A list's page slider, the scroll bar beside it, as it was drawn: its track from y0 down to y1, beside a list n /// long of which `shown` is in view, the first of it in view at `first` (rows, or pixels: the same unit for the three). Its /// thumb is the part of the track the view is of the list. The mouse takes it as a touch slider: the thumb held with the /// left button is drawn along with the cursor (grab at the press, drag while it is held), a press on the track off the /// thumb brings the thumb's middle under the cursor first, so the list jumps to there, and is held the same way struct Bar { int id = -1; // (the list's number: the menu's own, vk_menu.cpp; -1 none) float y0 = 0, y1 = 0; // (the track) float n = 0, shown = 0, first = 0; // (the list) float reach = 0; // (a short thumb is taken hold of this far either side of its middle, in pixels) /// (a list longer than its view, on a track that has a length: there is somewhere to scroll to) bool scrolls() const { return y1 > y0 && shown > 0 && n > shown; } /// (the furthest the first in view can go: the list's end at the view's) float most() const { return scrolls() ? n - shown : 0; } /// The thumb's top with f the first in view, and the thumb's length float thumb(float f) const { return scrolls() ? y0 + (y1 - y0) * f / n : y0; } float length() const { return scrolls() ? (y1 - y0) * shown / n : y1 - y0; } /// The first in view with the thumb's top at y, held to the list's ends float first_at(float y) const { if (!scrolls()) return 0; const float f = (y - y0) * n / (y1 - y0), m = most(); return f < 0 ? 0 : f > m ? m : f; } /// A press at y: how far under the thumb's top the cursor has hold of it. On the thumb (a short one: within reach of its /// middle), where the cursor is, so that the press alone moves nothing; elsewhere on the track, the thumb's middle, so /// that the thumb comes under the cursor float grab(float y) const { const float top = thumb(first), len = length(); float a = top, b = top + len; if (len < 2 * reach) a = top + len / 2 - reach, b = top + len / 2 + reach; return y >= a && y < b ? y - top : len / 2; } /// Held by `grab`, the cursor at y: the first in view, held to the list's ends wherever the cursor went float drag(float y, float grab) const { return first_at(y - grab); } }; /// The controls as last drawn struct Map { struct Box { float x0, y0, x1, y1; Part part; int index; float v0, v1; // (a value's span, for its halves: side -1 left of the middle, 1 right; v0 >= v1: none) float t0, t1; // (a slider's track: kSlider along it; t0 >= t1: none) int side; // (a fixed side: the Controls page's cells) float vm; // ((Triforce) where a toggle's halves meet, v0 < vm < v1: side by it, not by the middle; else none) }; static constexpr int kMax = 96; static constexpr float kGrab = 6; // (a click this far past either end of a track still takes the end, in pixels) static constexpr int kBars = 4; // ((Triforce) the page sliders a page can have: the Mods screen has two) Box box[kMax]; int n = 0; float ax0 = 0, ay0 = 0, ax1 = 0, ay1 = 0; // (the menu's area: off it, kOutside) Bar bar[kBars]; // ((Triforce) the page sliders drawn: add_bar, bar_of) int bars = 0; void clear(float x0, float y0, float x1, float y1) { n = 0, bars = 0, ax0 = x0, ay0 = y0, ax1 = x1, ay1 = y1; } void add(float x0, float y0, float x1, float y1, Part part, int index, float v0 = 0, float v1 = 0, float t0 = 0, float t1 = 0, int side = 0, float vm = 0) { if (n < kMax) box[n++] = {x0, y0, x1, y1, part, index, v0, v1, t0, t1, side, vm}; } /// ((Triforce) A page slider as drawn: its box from x0 to x1 (the track, and room either side of it to take hold of it: /// the track is thin), `past` pixels past the track's ends too (a press there takes the end), a kScroll of the bar's /// number; and the bar, for the drag. One with nowhere to scroll to, or with no number, is not recorded: no hit void add_bar(float x0, float x1, const Bar& b, float past = kGrab) { if (b.id < 0 || !b.scrolls() || bars >= kBars || n >= kMax) return; bar[bars++] = b; add(x0, b.y0 - past, x1, b.y1 + past, kScroll, b.id); } /// (the page slider of that number as last drawn; null when the page drew none) const Bar* bar_of(int id) const { for (int i = 0; i < bars; i++) if (bar[i].id == id) return &bar[i]; return nullptr; } Hit at(float x, float y) const { Hit hit; if (x < ax0 || y < ay0 || x >= ax1 || y >= ay1) return hit.part = kOutside, hit; for (int i = n - 1; i >= 0; i--) { // (the last drawn on top) const Box& b = box[i]; if (x < b.x0 || y < b.y0 || x >= b.x1 || y >= b.y1) continue; hit.part = b.part, hit.index = b.index, hit.side = b.side; if (b.t0 < b.t1 && x >= b.t0 - kGrab && x < b.t1 + kGrab) { hit.part = kSlider; hit.permille = permille(b, x); return hit; } if (b.v0 < b.v1 && x >= b.v0 && x < b.v1) { hit.halves = b.vm > b.v0 && b.vm < b.v1; hit.side = x < (hit.halves ? b.vm : (b.v0 + b.v1) / 2) ? -1 : 1; } return hit; } return hit; } /// A drag that started on row's slider: the point's place along its track, held to its ends, wherever the cursor went; /// -1 when the row has no track now int drag(float x, int row) const { for (int i = 0; i < n; i++) if (box[i].part == kRow && box[i].index == row && box[i].t0 < box[i].t1) return permille(box[i], x); return -1; } static int permille(const Box& b, float x) { const int p = (int)((x - b.t0) * 1000 / (b.t1 - b.t0)); return p < 0 ? 0 : p > 1000 ? 1000 : p; } }; } // namespace mkwvk::menu_hit