Rise
The Vieneo Province
Loading...
Searching...
No Matches
CargoManifestList.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstdint>
5#include <string>
6#include <vector>
7
8// Logical rows keep their inventory/bay identity while only the visible window is drawn.
10{
11 short repeatKey = -1;
13 double repeatRemaining = 0.0;
14 double repeatInterval = 1.0 / 30.0;
15
16public:
17 static constexpr int HeaderRows = 2;
18 static constexpr int LineHeight = 12;
19
20 struct Row
21 {
22 std::wstring text;
23 std::uint32_t color;
24 std::string type;
25 int value;
26 };
27
28 std::vector<Row> rows;
29 int selected = 0;
30 int offset = 0; // Number of body rows hidden above the pinned headings.
31 const int controlsY;
32 const int moreY;
33 const int visibleRows;
34
35 explicit CargoManifestList(int height) :
36 // Leave 24 pixels for the shortcuts and 13 for the small MORE label.
37 controlsY((std::min)(300, (height - 24 - 13) / LineHeight * LineHeight)),
38 moreY(controlsY + 24),
40 {
41 }
42
43 void Add(const wchar_t* text, std::uint32_t color, const std::string& type = "", int value = 0)
44 {
45 rows.push_back({ text, color, type, value });
46 }
47
49 {
50 const int count = static_cast<int>(rows.size());
51 selected = (std::clamp)(selected, 0, (std::max)(0, count - 1));
52 offset = (std::clamp)(offset, 0, (std::max)(0, count - visibleRows));
53 if (selected < HeaderRows)
54 offset = 0;
55 else if (selected < HeaderRows + offset)
57 else if (selected >= visibleRows + offset)
59 }
60
61 void MoveSelection(int direction)
62 {
63 UpdateView();
64 if (rows.empty()) return;
65 const int last = static_cast<int>(rows.size()) - 1;
66 selected = (std::clamp)(selected + direction, 0, last);
67 // The title and final total are navigation stops, so every row is reachable.
68 while (selected > 0 && selected < last && rows[selected].type.empty())
69 selected += direction;
70 UpdateView();
71 }
72
73 bool HandleNavigationKey(short key, bool shift, unsigned int keyboardDelay = 1, unsigned int keyboardSpeed = 31)
74 {
75 if (!shift || rows.empty()) return false;
76 int direction;
77 switch (key)
78 {
79 case 208: case 80: direction = 1; break; // DirectInput Down / numpad 2
80 case 200: case 72: direction = -1; break; // DirectInput Up / numpad 8
81 default: return false;
82 }
83 MoveSelection(direction);
84 repeatKey = key;
85 repeatDirection = direction;
86 // Windows SPI_GETKEYBOARDDELAY: 0..3 represents approximately 250..1000 ms.
87 // SPI_GETKEYBOARDSPEED: 0..31 represents approximately 2.5..30 repeats/sec.
88 // https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-systemparametersinfow
89 repeatRemaining = 0.25 * (1 + (std::min)(keyboardDelay, 3u));
90 repeatInterval = 1.0 / (2.5 + 27.5 * (std::min)(keyboardSpeed, 31u) / 31.0);
91 return true;
92 }
93
95 {
96 repeatKey = -1;
97 }
98
99 // Consume the held navigation key even during the delay, so it cannot also
100 // activate a vehicle binding. A fresh press is required after cancellation.
101 bool UpdateKeyRepeat(const unsigned char* keys, bool shift, float elapsed)
102 {
103 if (repeatKey < 0) return false;
104 if (!keys || !shift || rows.empty() || !(keys[repeatKey] & 0x80))
105 {
107 return false;
108 }
109 repeatRemaining -= (std::max)(0.0f, elapsed);
110 if (repeatRemaining <= 0.0f)
111 {
113 // Preserve fractional frame time so fast repeat does not slow down to
114 // a whole number of frames per step. Drop overdue repeats after a stall.
116 if (repeatRemaining <= 0.0)
118 }
119 return true;
120 }
121
122 int ScreenRow(int row) const
123 {
124 if (row < HeaderRows) return row;
125 const int screenRow = row - offset;
126 return screenRow >= HeaderRows && screenRow < visibleRows ? screenRow : -1;
127 }
128
129 bool MoreBelow() const
130 {
131 return static_cast<int>(rows.size()) > offset + visibleRows;
132 }
133};
void Add(const wchar_t *text, std::uint32_t color, const std::string &type="", int value=0)
void MoveSelection(int direction)
int ScreenRow(int row) const
std::vector< Row > rows
static constexpr int LineHeight
bool UpdateKeyRepeat(const unsigned char *keys, bool shift, float elapsed)
static constexpr int HeaderRows
bool HandleNavigationKey(short key, bool shift, unsigned int keyboardDelay=1, unsigned int keyboardSpeed=31)
CargoManifestList(int height)