Rise
The Vieneo Province
Toggle main menu visibility
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.
9
class
CargoManifestList
10
{
11
short
repeatKey
= -1;
12
int
repeatDirection
= 0;
13
double
repeatRemaining
= 0.0;
14
double
repeatInterval
= 1.0 / 30.0;
15
16
public
:
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),
39
visibleRows
(
controlsY
/
LineHeight
)
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
48
void
UpdateView
()
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
)
56
offset
=
selected
-
HeaderRows
;
57
else
if
(
selected
>=
visibleRows
+
offset
)
58
offset
=
selected
-
visibleRows
+ 1;
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
94
void
CancelKeyRepeat
()
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
{
106
CancelKeyRepeat
();
107
return
false
;
108
}
109
repeatRemaining
-= (std::max)(0.0f, elapsed);
110
if
(
repeatRemaining
<= 0.0f)
111
{
112
MoveSelection
(
repeatDirection
);
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.
115
repeatRemaining
+=
repeatInterval
;
116
if
(
repeatRemaining
<= 0.0)
117
repeatRemaining
=
repeatInterval
;
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
};
CargoManifestList::CancelKeyRepeat
void CancelKeyRepeat()
Definition
CargoManifestList.h:94
CargoManifestList::Add
void Add(const wchar_t *text, std::uint32_t color, const std::string &type="", int value=0)
Definition
CargoManifestList.h:43
CargoManifestList::selected
int selected
Definition
CargoManifestList.h:29
CargoManifestList::MoveSelection
void MoveSelection(int direction)
Definition
CargoManifestList.h:61
CargoManifestList::UpdateView
void UpdateView()
Definition
CargoManifestList.h:48
CargoManifestList::repeatDirection
int repeatDirection
Definition
CargoManifestList.h:12
CargoManifestList::ScreenRow
int ScreenRow(int row) const
Definition
CargoManifestList.h:122
CargoManifestList::controlsY
const int controlsY
Definition
CargoManifestList.h:31
CargoManifestList::rows
std::vector< Row > rows
Definition
CargoManifestList.h:28
CargoManifestList::LineHeight
static constexpr int LineHeight
Definition
CargoManifestList.h:18
CargoManifestList::UpdateKeyRepeat
bool UpdateKeyRepeat(const unsigned char *keys, bool shift, float elapsed)
Definition
CargoManifestList.h:101
CargoManifestList::repeatInterval
double repeatInterval
Definition
CargoManifestList.h:14
CargoManifestList::offset
int offset
Definition
CargoManifestList.h:30
CargoManifestList::HeaderRows
static constexpr int HeaderRows
Definition
CargoManifestList.h:17
CargoManifestList::MoreBelow
bool MoreBelow() const
Definition
CargoManifestList.h:129
CargoManifestList::moreY
const int moreY
Definition
CargoManifestList.h:32
CargoManifestList::repeatKey
short repeatKey
Definition
CargoManifestList.h:11
CargoManifestList::HandleNavigationKey
bool HandleNavigationKey(short key, bool shift, unsigned int keyboardDelay=1, unsigned int keyboardSpeed=31)
Definition
CargoManifestList.h:73
CargoManifestList::visibleRows
const int visibleRows
Definition
CargoManifestList.h:33
CargoManifestList::CargoManifestList
CargoManifestList(int height)
Definition
CargoManifestList.h:35
CargoManifestList::repeatRemaining
double repeatRemaining
Definition
CargoManifestList.h:13
CargoManifestList::Row
Definition
CargoManifestList.h:21
CargoManifestList::Row::type
std::string type
Definition
CargoManifestList.h:24
CargoManifestList::Row::value
int value
Definition
CargoManifestList.h:25
CargoManifestList::Row::text
std::wstring text
Definition
CargoManifestList.h:22
CargoManifestList::Row::color
std::uint32_t color
Definition
CargoManifestList.h:23
Instruments
MFD
CargoManifestList.h
Generated on
for Rise by
1.18.0