Polyphase Game Engine
Loading...
Searching...
No Matches
InputPromptResolver.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <list>
6#include <string>
7#include <unordered_map>
8
10class Texture;
11class Font;
12
14{
16 Texture* sprite = nullptr;
17 Font* font = nullptr;
18 uint32_t codepoint = 0;
19 std::string label;
20};
21
22// Owns the resolve-cache that turns an action id + device into a renderable
23// prompt. Singleton, created alongside PlayerInputSystem; PlayerInputSystem
24// calls Tick() at the end of its Update() so the cache flushes itself on
25// device change.
27{
28public:
29
30 static void Create();
31 static void Destroy();
32 static InputPromptResolver* Get();
33
34 // Per-frame tick. Drops the cache when PlayerInputSystem's
35 // GetDeviceChangeFrame() has advanced since the last call.
36 void Tick();
37
38 // Resolve an action to a renderable prompt. Returned pointer is valid until
39 // the next Tick() invalidation OR a fresh Resolve() that evicts via LRU.
40 // Callers should re-resolve every PreRender — that's the whole point of the
41 // cache.
42 //
43 // When `deviceOverride` is non-null, uses it instead of
44 // PlayerInputSystem::GetLastActiveDevice(); the editor inspector uses this
45 // to preview against a specific test device.
47 InputPromptStyle* style,
48 const std::string& actionCategory,
49 const std::string& actionName,
50 const struct InputDeviceDescriptor* deviceOverride = nullptr);
51
52 // Pre-resolve every action listed in style->mPrewarmActions ("Category/Name").
53 void Prewarm(InputPromptMap* map, InputPromptStyle* style);
54
55 // Force cache flush — InputPromptMap inspector calls this after the artist
56 // edits an entry so previews refresh.
57 void Invalidate();
58
59private:
60
61 static InputPromptResolver* sInstance;
62
63 InputPromptResolver() = default;
64
65 struct CacheKey
66 {
67 uint64_t mapUuid = 0;
68 uint64_t styleUuid = 0;
69 uint32_t deviceEpoch = 0;
70 std::string action; // "Category/Name"
71 std::string deviceTag; // serialized device descriptor
72
73 bool operator==(const CacheKey& o) const
74 {
75 return mapUuid == o.mapUuid && styleUuid == o.styleUuid &&
76 deviceEpoch == o.deviceEpoch && action == o.action &&
77 deviceTag == o.deviceTag;
78 }
79 };
80
81 struct CacheKeyHash
82 {
83 size_t operator()(const CacheKey& k) const
84 {
85 size_t h = std::hash<uint64_t>()(k.mapUuid);
86 h ^= std::hash<uint64_t>()(k.styleUuid) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
87 h ^= std::hash<uint32_t>()(k.deviceEpoch) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
88 h ^= std::hash<std::string>()(k.action) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
89 h ^= std::hash<std::string>()(k.deviceTag) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
90 return h;
91 }
92 };
93
94 struct CacheEntry
95 {
96 CacheKey key;
97 ResolvedPrompt prompt;
98 };
99
100 static constexpr size_t kCacheLimit = 256;
101
102 std::list<CacheEntry> mLru; // front = most recent
103 std::unordered_map<CacheKey, std::list<CacheEntry>::iterator, CacheKeyHash> mLookup;
104 uint32_t mLastSeenDeviceFrame = 0;
105};
InputPromptKind
Definition InputPromptMap.h:16
Definition Font.h:20
Definition InputPromptMap.h:49
Definition InputPromptResolver.h:27
static InputPromptResolver * Get()
Definition InputPromptResolver.cpp:30
void Prewarm(InputPromptMap *map, InputPromptStyle *style)
Definition InputPromptResolver.cpp:272
const ResolvedPrompt * Resolve(InputPromptMap *map, InputPromptStyle *style, const std::string &actionCategory, const std::string &actionName, const struct InputDeviceDescriptor *deviceOverride=nullptr)
Definition InputPromptResolver.cpp:123
static void Create()
Definition InputPromptResolver.cpp:15
static void Destroy()
Definition InputPromptResolver.cpp:21
void Invalidate()
Definition InputPromptResolver.cpp:49
void Tick()
Definition InputPromptResolver.cpp:35
Definition InputPromptStyle.h:11
Definition Texture.h:10
Definition PlayerInputSystem.h:29
Definition InputPromptResolver.h:14
InputPromptKind kind
Definition InputPromptResolver.h:15
Texture * sprite
Definition InputPromptResolver.h:16
uint32_t codepoint
Definition InputPromptResolver.h:18
Font * font
Definition InputPromptResolver.h:17
std::string label
Definition InputPromptResolver.h:19