// The Insert menu's hit-test (gpu/vk/vk_menu_hit.h): the controls as recorded while drawing (Map::add), and where a mouse // position falls on them, in the window image's pixels: the tabs, a row's name and its value's halves, a slider's track and a // drag along it, the Controls page's cells, a list line, the restart question's buttons; off the controls and off the menu; // through a window whose pixels are not the swapchain's. #include "../gpu/vk/vk_menu_hit.h" #include #define CHECK(name, condition) do { if (!(condition)) { std::fprintf(stderr, "FAIL %s at line %d\n", name, __LINE__); return 1; } } while (0) using namespace mkwvk::menu_hit; int main() { Map m; Hit h; // ---- the options page of a 1920 x 1080 window: the tab keys, three tabs, rows 40 high every 48 from y 100 ---- m.clear(0, 0, 1920, 1080); m.add(0, 0, 640, 87, kTabPrev, -1); m.add(1280, 0, 1920, 87, kTabNext, -1); for (int i = 0; i < 3; i++) m.add(660 + i * 200, 0, 860 + i * 200, 87, kTab, i); for (int i = 0; i < 10; i++) { const float y = 100 + i * 48; if (i == 7) m.add(100, y, 1000, y + 40, kRow, i, 600, 1000, 640, 960); // (Sharpness: a slider's track 640 to 960) else m.add(100, y, 1000, y + 40, kRow, i, 600, 1000); } h = m.at(100, 40); CHECK("tab key, left", h.part == kTabPrev); h = m.at(1500, 40); CHECK("tab key, right", h.part == kTabNext); h = m.at(700, 40); CHECK("tab 0", h.part == kTab && h.index == 0); h = m.at(1000, 40); CHECK("tab 1", h.part == kTab && h.index == 1); h = m.at(1200, 40); CHECK("tab 2", h.part == kTab && h.index == 2); h = m.at(200, 110); CHECK("row 0's name", h.part == kRow && h.index == 0 && h.side == 0); h = m.at(650, 110 + 48 * 3); CHECK("row 3's value, left half", h.part == kRow && h.index == 3 && h.side == -1); h = m.at(950, 110 + 48 * 3); CHECK("row 3's value, right half", h.part == kRow && h.index == 3 && h.side == 1); h = m.at(500, 100 + 48 * 2 + 44); CHECK("between two rows: the menu, no control", h.part == kNone && h.index == -1); h = m.at(800, 110 + 48 * 7); CHECK("the slider's track, its middle", h.part == kSlider && h.index == 7 && h.permille == 500); h = m.at(636, 110 + 48 * 7); CHECK("just before the track: its start", h.part == kSlider && h.permille == 0); h = m.at(965, 110 + 48 * 7); CHECK("just past the track: its end", h.part == kSlider && h.permille == 1000); h = m.at(620, 110 + 48 * 7); CHECK("the slider row's value, off the track", h.part == kRow && h.index == 7 && h.side == -1); CHECK("a drag along the track", m.drag(720, 7) == 250); CHECK("a drag past either end", m.drag(100, 7) == 0 && m.drag(1900, 7) == 1000); CHECK("a drag on a row without a track", m.drag(720, 3) == -1); CHECK("the slider's value", slider_value(m.drag(720, 7), 0, 100, 10) == 30 && slider_value(1000, 0, 100, 10) == 100); CHECK("the knob's place", slider_permille(60, 0, 100) == 600 && slider_permille(500, 10, 300) == 1000); // ---- the Controls page: a name and two cells (gamepad, keyboard) a row, Reset to defaults at the end ---- m.clear(0, 0, 1920, 1080); for (int i = 0; i < 3; i++) { const float y = 120 + i * 42; m.add(100, y, 400, y + 36, kCtlRow, i); m.add(400, y, 700, y + 36, kCtlCell, i, 0, 0, 0, 0, 0); m.add(700, y, 1000, y + 36, kCtlCell, i, 0, 0, 0, 0, 1); } m.add(100, 120 + 3 * 42, 390, 120 + 3 * 42 + 36, kCtlRow, 3); h = m.at(200, 130 + 42); CHECK("a control's name", h.part == kCtlRow && h.index == 1); h = m.at(500, 130 + 84); CHECK("its gamepad cell", h.part == kCtlCell && h.index == 2 && h.side == 0); h = m.at(800, 130); CHECK("its keyboard cell", h.part == kCtlCell && h.index == 0 && h.side == 1); h = m.at(200, 130 + 126); CHECK("Reset to defaults", h.part == kCtlRow && h.index == 3); m.add(100, 60, 1000, 96, kCtlOption, 1, 600, 1000); // (a row of the page's own, above the grid: Button prompts) h = m.at(650, 70); CHECK("the page's own row, its value's left half", h.part == kCtlOption && h.index == 1 && h.side == -1); h = m.at(200, 70); CHECK("its name", h.part == kCtlOption && h.side == 0); // ---- a list line with an amount (its halves), and one without ---- m.clear(0, 0, 1920, 1080); m.add(100, 100, 1000, 130, kListLine, 0, 900, 1000); m.add(100, 134, 1000, 164, kListLine, 1); h = m.at(920, 110); CHECK("an amount, its left half", h.part == kListLine && h.index == 0 && h.side == -1); h = m.at(990, 110); CHECK("an amount, its right half", h.side == 1); h = m.at(990, 140); CHECK("a line without an amount", h.part == kListLine && h.index == 1 && h.side == 0); // ---- the restart question: the menu is the dialog; two buttons ---- m.clear(680, 424, 1240, 656); m.add(706, 584, 916, 630, kButton, 0); m.add(940, 584, 1150, 630, kButton, 1); h = m.at(800, 600); CHECK("Later", h.part == kButton && h.index == 0); h = m.at(1000, 600); CHECK("Restart now", h.part == kButton && h.index == 1); h = m.at(900, 450); CHECK("the dialog, off its buttons", h.part == kNone); h = m.at(100, 100); CHECK("off the dialog", h.part == kOutside); // ---- what was drawn last is on top; the map holds kMax boxes and drops the rest ---- m.clear(0, 0, 100, 100); m.add(0, 0, 100, 100, kNone, -1); m.add(10, 10, 20, 20, kButton, 1); h = m.at(15, 15); CHECK("the last drawn, on top", h.part == kButton); for (int i = 0; i < Map::kMax + 10; i++) m.add(0, 0, 1, 1, kRow, i); CHECK("no more than kMax", m.n == Map::kMax); // ---- a window of 1280 x 720 pixels over a 2560 x 1440 image (the swapchain not yet resized, or a scaled window) ---- int ix = 0, iy = 0; to_image(640, 360, 1280, 720, 2560, 1440, &ix, &iy); CHECK("to the image's pixels", ix == 1280 && iy == 720); to_image(5, 7, 0, 0, 2560, 1440, &ix, &iy); CHECK("no window size: as it is", ix == 5 && iy == 7); std::printf("PASS: the hit-test over the controls as drawn\n"); return 0; }