Rise
The Vieneo Province
Loading...
Searching...
No Matches
KeyboardChart.cpp
Go to the documentation of this file.
1#include "KeyboardChart.h"
2#include <algorithm>
3#include <cmath>
4
6{
7 // Mirror the Control-specific branches in Viewscreen::RunBindingFunction.
8 // Thrust bindings use Control only for TVM/DOR closing speed; ordinary
9 // throttle changes are not multiplied. The warning test changes action.
10 switch (function)
11 {
12 case THRUST0TVMMINUS1KM: return {L"TVM/DOR -10 km/s", L"-10 km/s"};
13 case THRUST100TVMPLUS1KM: return {L"TVM/DOR +10 km/s", L"+10 km/s"};
14 case THRUST_MINUS10_TVMMINUS: return {L"TVM/DOR -10 m/s", L"-10 m/s"};
15 case THRUST_PLUS10_TVMPLUS: return {L"TVM/DOR +10 m/s", L"+10 m/s"};
16 case AURALWARNINGTEST: return {L"TCAS system test", L"TCAS test"};
19 case VERTICALMODE_UP:
21 case PITCHTRIM_UP:
22 case PITCHTRIM_DOWN:
23 // Pitch trim already uses this fast rate on the ground.
24 return {normal.full + L" (10x)",
25 (normal.shortName.empty() || normal.shortName == L"//" ? normal.full : normal.shortName) + L" x10"};
26 default: return {};
27 }
28}
29
30Keychart::MarqueePosition Keychart::Marquee(double elapsedSeconds, LONG textWidth, LONG availableWidth)
31{
32 const double distance = static_cast<double>(textWidth) - availableWidth;
33 if (distance <= 0 || availableWidth <= 0 || !std::isfinite(elapsedSeconds) || elapsedSeconds < 0)
34 return {};
35 const double travelSeconds = distance / MarqueePixelsPerSecond;
36 const double phase = std::fmod(elapsedSeconds, MarqueeStartHold + travelSeconds + MarqueeEndHold);
37 if (phase < MarqueeStartHold)
38 return {};
39 return {(std::min)(distance, (phase - MarqueeStartHold) * MarqueePixelsPerSecond), false};
40}
41
42const std::vector<Keychart::Key>& Keychart::Layout()
43{
44 static const std::vector<Key> keys = []
45 {
46 std::vector<Key> result;
47 auto key = [&](short code, float column, int row, const wchar_t* caption,
48 float width = 1.0f, int height = 1)
49 {
50 const LONG x = 20 + static_cast<LONG>(column * 78);
51 const LONG y = 68 + row * KeyRowHeight;
52 result.push_back({code, {x, y, x + static_cast<LONG>(width * 78) - 6, y + height * KeyRowHeight - 8}, caption});
53 };
54 auto row = [&](const std::vector<short>& codes, float column, int y)
55 {
56 for (short code : codes)
57 key(code, column++, y, nullptr);
58 };
59 key(DIK_ESCAPE, 0, 0, L"Esc");
60 for (int i = 0; i < 10; ++i)
61 key(static_cast<short>(DIK_F1 + i), 2.0f + i + (i / 4) * 0.5f, 0, nullptr);
62 key(DIK_F11, 13, 0, L"F11");
63 key(DIK_F12, 14, 0, L"F12");
64 row({DIK_GRAVE, DIK_1, DIK_2, DIK_3, DIK_4, DIK_5, DIK_6, DIK_7,
65 DIK_8, DIK_9, DIK_0, DIK_MINUS, DIK_EQUALS}, 0, 1);
66 key(DIK_BACK, 13, 1, L"Backspace", 2);
67 key(DIK_TAB, 0, 2, L"Tab", 1.5f);
68 row({DIK_Q, DIK_W, DIK_E, DIK_R, DIK_T, DIK_Y, DIK_U, DIK_I,
69 DIK_O, DIK_P, DIK_LBRACKET, DIK_RBRACKET}, 1.5f, 2);
70 key(DIK_BACKSLASH, 13.5f, 2, nullptr, 1.5f);
71 key(DIK_CAPITAL, 0, 3, L"Caps Lock", 1.75f);
72 row({DIK_A, DIK_S, DIK_D, DIK_F, DIK_G, DIK_H, DIK_J, DIK_K,
73 DIK_L, DIK_SEMICOLON, DIK_APOSTROPHE}, 1.75f, 3);
74 key(DIK_RETURN, 12.75f, 3, L"Enter", 2.25f);
75 key(DIK_LSHIFT, 0, 4, L"Shift", 1.25f);
76 key(DIK_OEM_102, 1.25f, 4, nullptr); // Extra ISO/UK key, distinct from DIK_BACKSLASH.
77 row({DIK_Z, DIK_X, DIK_C, DIK_V, DIK_B, DIK_N, DIK_M, DIK_COMMA,
78 DIK_PERIOD, DIK_SLASH}, 2.25f, 4);
79 key(DIK_RSHIFT, 12.25f, 4, L"Shift", 2.75f);
80 key(DIK_LCONTROL, 0, 5, L"Ctrl", 1.25f);
81 key(DIK_LWIN, 1.25f, 5, L"Win", 1.25f);
82 key(DIK_LALT, 2.5f, 5, L"Alt", 1.25f);
83 key(DIK_SPACE, 3.75f, 5, L"Space", 6.25f);
84 key(DIK_RALT, 10, 5, L"Alt", 1.25f);
85 key(DIK_RWIN, 11.25f, 5, L"Win", 1.25f);
86 key(DIK_APPS, 12.5f, 5, L"Menu", 1.25f);
87 key(DIK_RCONTROL, 13.75f, 5, L"Ctrl", 1.25f);
88
89 key(DIK_SYSRQ, 15.3f, 0, L"PrtSc");
90 key(DIK_SCROLL, 16.3f, 0, L"Scroll");
91 key(DIK_PAUSE, 17.3f, 0, L"Pause");
92 key(DIK_INSERT, 15.3f, 1, L"Insert");
93 key(DIK_HOME, 16.3f, 1, L"Home");
94 key(DIK_PRIOR, 17.3f, 1, L"Page Up");
95 key(DIK_DELETE, 15.3f, 2, L"Delete");
96 key(DIK_END, 16.3f, 2, L"End");
97 key(DIK_NEXT, 17.3f, 2, L"Page Down");
98 key(DIK_UP, 16.3f, 4, L"\x2191");
99 key(DIK_LEFT, 15.3f, 5, L"\x2190");
100 key(DIK_DOWN, 16.3f, 5, L"\x2193");
101 key(DIK_RIGHT, 17.3f, 5, L"\x2192");
102
103 key(DIK_NUMLOCK, 18.6f, 1, L"Num Lock");
104 key(DIK_DIVIDE, 19.6f, 1, L"Num /");
105 key(DIK_MULTIPLY, 20.6f, 1, L"Num *");
106 key(DIK_SUBTRACT, 21.6f, 1, L"Num -");
107 key(DIK_NUMPAD7, 18.6f, 2, L"Num 7");
108 key(DIK_NUMPAD8, 19.6f, 2, L"Num 8");
109 key(DIK_NUMPAD9, 20.6f, 2, L"Num 9");
110 key(DIK_ADD, 21.6f, 2, L"Num +", 1, 2);
111 key(DIK_NUMPAD4, 18.6f, 3, L"Num 4");
112 key(DIK_NUMPAD5, 19.6f, 3, L"Num 5");
113 key(DIK_NUMPAD6, 20.6f, 3, L"Num 6");
114 key(DIK_NUMPAD1, 18.6f, 4, L"Num 1");
115 key(DIK_NUMPAD2, 19.6f, 4, L"Num 2");
116 key(DIK_NUMPAD3, 20.6f, 4, L"Num 3");
117 key(DIK_NUMPADENTER, 21.6f, 4, L"Num Enter", 1, 2);
118 key(DIK_NUMPAD0, 18.6f, 5, L"Num 0", 2);
119 key(DIK_DECIMAL, 20.6f, 5, L"Num .");
120 return result;
121 }();
122 return keys;
123}
124
126{
128 HRESULT hr = Rendering::CreateFontW(device, 18, 0, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET,
129 OUT_TT_ONLY_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH, L"Segoe UI", &font);
130 if (FAILED(hr))
131 return hr;
132 // One generic blank keyboard image, generated from the same rectangles as
133 // the text. Managed storage survives a normal D3D9 device reset.
136 if (FAILED(hr))
137 {
139 return hr;
140 }
142 hr = blankKeyboard->GetLevelDesc(0, &description);
143 if (FAILED(hr) || description.Width < Keychart::Width || description.Height < Keychart::Height)
144 {
146 return FAILED(hr) ? hr : DXGI_ERROR_UNSUPPORTED;
147 }
149 hr = blankKeyboard->LockRect(0, &locked, nullptr, 0);
150 if (FAILED(hr))
151 {
153 return hr;
154 }
155 auto fill = [&](const RECT& rect, DWORD color)
156 {
157 for (LONG y = rect.top; y < rect.bottom; ++y)
158 {
159 auto pixels = reinterpret_cast<DWORD*>(static_cast<BYTE*>(locked.pBits) + y * locked.Pitch);
160 std::fill(pixels + rect.left, pixels + rect.right, color);
161 }
162 };
163 fill({0, 0, Keychart::Width, Keychart::Height}, 0xED101820);
164 for (const auto& key : Keychart::Layout())
165 {
166 fill(key.bounds, 0xFF657684);
167 RECT inside = key.bounds;
168 InflateRect(&inside, -1, -1);
169 fill(inside, 0xFF202D38);
170 }
171 return blankKeyboard->UnlockRect(0);
172}
173
175{
176 if (font) font->OnLostDevice();
177}
178
183
185{
186 if (font) { font->Release(); font = nullptr; }
187 if (blankKeyboard) { blankKeyboard->Release(); blankKeyboard = nullptr; }
188 wasVisible = false;
189 visibleSeconds = 0.0;
190 textLayouts.clear();
191}
192
193void KeyboardChart::Update(bool visible, float elapsedSeconds)
194{
195 if (!visible || !wasVisible)
196 visibleSeconds = 0.0;
197 else if (std::isfinite(elapsedSeconds) && elapsedSeconds > 0)
198 visibleSeconds += elapsedSeconds;
199 wasVisible = visible;
200}
201
203 const std::wstring& title, int displayWidth, int displayHeight)
204{
205 if (!sprite || !font || !blankKeyboard || displayWidth <= 0 || displayHeight <= 0)
206 return;
207 if (labels != previousLabels || title != previousTitle)
208 {
209 visibleSeconds = 0.0;
210 previousLabels = labels;
211 previousTitle = title;
212 textLayouts.clear();
213 }
214 Rendering::RenderDevice* device = &sprite->Device();
215 RECT previousScissor;
216 DWORD previousScissorEnabled = FALSE;
217 device->GetScissorRect(&previousScissor);
218 device->GetRenderState(Rendering::RenderState::SCISSORTESTENABLE, &previousScissorEnabled);
219 RiseMath::Matrix previous, transform;
220 sprite->GetTransform(&previous);
221 const float scale = (std::min)(displayWidth / static_cast<float>(Keychart::Width),
222 displayHeight / static_cast<float>(Keychart::Height)) * 0.98f;
223 RiseMath::MatrixScaling(&transform, scale, scale, 1);
224 transform._41 = (displayWidth - Keychart::Width * scale) * 0.5f;
225 transform._42 = (displayHeight - Keychart::Height * scale) * 0.5f;
226 sprite->SetTransform(&transform);
227 const RECT source = {0, 0, Keychart::Width, Keychart::Height};
228 sprite->Draw(blankKeyboard, &source, nullptr, nullptr, 0xFFFFFFFF);
229 auto text = [&](const std::wstring& value, RECT bounds, DWORD color, DWORD align,
230 bool marquee = false, const std::wstring& shortName = std::wstring())
231 {
232 if (value.empty()) return;
233 // Rendering::RenderFont supports only a subset of GDI's DrawText flags (neither
234 // DT_END_ELLIPSIS nor DT_NOPREFIX). Fit the string ourselves, then clip.
235 auto width = [&](const std::wstring& string)
236 {
237 RECT measured{};
238 font->DrawTextW(nullptr, string.c_str(), -1, &measured, DT_CALCRECT | DT_SINGLELINE, color);
239 return measured.right - measured.left;
240 };
241 const LONG available = bounds.right - bounds.left;
242 const auto cacheKey = std::make_tuple(value, shortName, available);
243 auto cached = textLayouts.find(cacheKey);
244 if (cached == textLayouts.end())
245 {
246 TextLayout layout = {width(value), value, value};
247 if (layout.width > available && !shortName.empty() && shortName != L"//")
248 {
249 const LONG shortWidth = width(shortName);
250 if (shortWidth < layout.width)
251 {
252 layout.width = shortWidth;
253 layout.selected = layout.fitted = shortName;
254 }
255 }
256 if (layout.width > available)
257 {
258 const std::wstring ellipsis = L"\x2026";
259 do
260 {
261 layout.fitted.pop_back();
262 // Do not split a UTF-16 surrogate pair when trimming.
263 if (!layout.fitted.empty() && layout.fitted.back() >= 0xd800 && layout.fitted.back() <= 0xdbff)
264 layout.fitted.pop_back();
265 } while (!layout.fitted.empty() && width(layout.fitted + ellipsis) > available);
266 layout.fitted += ellipsis;
267 }
268 cached = textLayouts.emplace(cacheKey, std::move(layout)).first;
269 }
270 const LONG textWidth = cached->second.width;
271 if (textWidth > available)
272 {
273 const auto position = Keychart::Marquee(visibleSeconds, textWidth, available);
274 if (marquee && !position.showEllipsis)
275 {
276 // D3DXFont clips relative to its text rectangle. Use a fixed
277 // screen-space scissor while the full text moves behind it.
278 RECT clip = {
279 static_cast<LONG>(std::ceil(bounds.left * scale + transform._41)),
280 static_cast<LONG>(std::ceil(bounds.top * scale + transform._42)),
281 static_cast<LONG>(std::floor(bounds.right * scale + transform._41)),
282 static_cast<LONG>(std::floor(bounds.bottom * scale + transform._42))};
283 if (previousScissorEnabled)
284 {
285 RECT intersection;
286 if (!IntersectRect(&intersection, &clip, &previousScissor)) return;
287 clip = intersection;
288 }
289 if (IsRectEmpty(&clip)) return;
290 sprite->Flush(); // Submit earlier keys before changing their clipping state.
291 if (SUCCEEDED(device->SetScissorRect(&clip)))
292 {
294 RECT moving = bounds;
295 moving.left -= static_cast<LONG>(position.offset);
296 moving.right = moving.left + textWidth;
297 font->DrawTextW(sprite, cached->second.selected.c_str(), -1, &moving,
298 DT_SINGLELINE | DT_VCENTER | DT_LEFT | DT_NOCLIP, color);
299 sprite->Flush();
300 device->SetScissorRect(&previousScissor);
301 device->SetRenderState(Rendering::RenderState::SCISSORTESTENABLE, previousScissorEnabled);
302 return;
303 }
304 }
305 }
306 font->DrawTextW(sprite, cached->second.fitted.c_str(), -1, &bounds, DT_SINGLELINE | DT_VCENTER | align, color);
307 };
308 const DWORD colors[Keychart::ModifierCount] = {0xFFF2F6FA, 0xFFFFCF78, 0xFF78D9FF, 0xFFA9EF94};
309 text(title, {20, 14, 1045, 50}, colors[0], DT_LEFT);
310 text(L"Key", {1070, 14, 1125, 50}, colors[0], DT_LEFT);
311 text(L"Shift + Key", {1145, 14, 1280, 50}, colors[1], DT_LEFT);
312 text(L"Control + Key", {1300, 14, 1490, 50}, colors[2], DT_LEFT);
313 text(L"Control + Shift + Key", {1510, 14, 1795, 50}, colors[3], DT_LEFT);
314 for (const auto& key : Keychart::Layout())
315 {
316 wchar_t keyName[64] = {};
317 if (!key.caption)
318 {
319 // DirectInput's high bit identifies E0 extended scan codes.
320 const LONG scan = ((key.scancode & 0x7f) << 16) | ((key.scancode & 0x80) ? (1 << 24) : 0);
321 if (!GetKeyNameTextW(scan, keyName, 64))
322 swprintf_s(keyName, L"0x%02X", key.scancode);
323 }
324 RECT name = key.bounds;
325 name.left += 5; name.right -= 5; name.top += 2; name.bottom = name.top + 20;
326 text(key.caption ? key.caption : keyName, name, 0xFF9DB0BF, DT_LEFT);
327 for (int modifier = 0; modifier < Keychart::ModifierCount; ++modifier)
328 {
329 RECT bounds = key.bounds;
330 bounds.left += 3; bounds.right -= 3;
331 bounds.top += Keychart::LabelTop + modifier * Keychart::LabelHeight;
332 bounds.bottom = bounds.top + Keychart::LabelHeight;
333 const auto& label = labels[key.scancode][modifier];
334 text(label.full, bounds, colors[modifier], DT_CENTER, true, label.shortName);
335 }
336 }
337 sprite->SetTransform(&previous);
338}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
BindingEnum
Definition binding.h:6
@ LATERALMODE_LEFT
Definition binding.h:93
@ THRUST0TVMMINUS1KM
Definition binding.h:21
@ THRUST_MINUS10_TVMMINUS
Definition binding.h:69
@ THRUST_PLUS10_TVMPLUS
Definition binding.h:70
@ PITCHTRIM_UP
Definition binding.h:97
@ LATERALMODE_RIGHT
Definition binding.h:94
@ THRUST100TVMPLUS1KM
Definition binding.h:22
@ AURALWARNINGTEST
Definition binding.h:72
@ VERTICALMODE_DOWN
Definition binding.h:96
@ PITCHTRIM_DOWN
Definition binding.h:98
@ VERTICALMODE_UP
Definition binding.h:95
void Draw(Rendering::RenderSprite *sprite, const Keychart::Labels &labels, const std::wstring &title, int displayWidth, int displayHeight)
Rendering::RenderFont * font
void Update(bool visible, float elapsedSeconds)
double visibleSeconds
std::map< std::tuple< std::wstring, std::wstring, LONG >, TextLayout > textLayouts
HRESULT OnCreateDevice(Rendering::RenderDevice *device)
Rendering::RenderTexture * blankKeyboard
std::wstring previousTitle
Keychart::Labels previousLabels
HRESULT GetRenderState(DWORD state, DWORD *value) const
HRESULT SetRenderState(DWORD state, DWORD value)
HRESULT SetScissorRect(const RECT *value)
HRESULT GetScissorRect(RECT *value) const
INT DrawTextW(RenderSprite *sprite, const wchar_t *text, INT count, RECT *rectangle, DWORD flags, PackedColor color)
HRESULT SetTransform(const RiseMath::Matrix *value)
HRESULT GetTransform(RiseMath::Matrix *value) const
HRESULT Draw(RenderTexture *image, const RECT *rectangle, const RiseMath::Vector3 *center, const RiseMath::Vector3 *position, PackedColor color)
HRESULT GetLevelDesc(UINT level, SurfaceDescription *result) const
HRESULT LockRect(UINT level, LockedRectangle *result, const RECT *region, DWORD flags)
HRESULT UnlockRect(UINT level)
const std::vector< Key > & Layout()
constexpr double MarqueeEndHold
Label ControlLabel(BindingEnum function, const Label &normal)
MarqueePosition Marquee(double elapsedSeconds, LONG textWidth, LONG availableWidth)
constexpr int LabelTop
constexpr double MarqueePixelsPerSecond
constexpr double MarqueeStartHold
constexpr int Width
std::array< std::array< Label, ModifierCount >, 256 > Labels
constexpr int Height
constexpr int ModifierCount
constexpr int KeyRowHeight
constexpr int LabelHeight
constexpr uint32_t SCISSORTESTENABLE
HRESULT CreateFontW(RenderDevice *owner, INT height, UINT width, UINT weight, UINT mipLevels, BOOL italic, DWORD charset, DWORD precision, DWORD quality, DWORD pitch, const wchar_t *face, RenderFont **result)
HRESULT CreateTexture(RenderDevice *owner, UINT width, UINT height, UINT levels, DWORD usage, ImageFormat format, ResourcePool pool, RenderTexture **result)
Matrix * MatrixScaling(Matrix *out, float x, float y, float z)
Definition ClientMath.h:124
std::wstring full
std::wstring shortName