Files
sx/library/modules/ui/state.sx
agra dc8529e3ea ui: port game UI framework into library/modules/ui
20 files (~3,830 lines): view protocol, layout, renderer, glyph cache,
fonts, gestures, animation, scroll, stacks, modifiers, etc.

Internal imports rewritten from "ui/..." to "modules/ui/...".
Consumers now `#import "modules/ui"` from any project; no symlink
hacks needed. Verified by compiling game/main.sx without its local
ui/ — resolves via the Phase 6 stdlib fallback.
2026-05-17 13:54:11 +03:00

60 lines
1.5 KiB
Plaintext
Executable File

#import "modules/std.sx";
// --- State(T) — a handle to persistent storage ---
State :: struct ($T: Type) {
ptr: *T;
get :: (self: State(T)) -> T { self.ptr.*; }
set :: (self: State(T), val: T) { self.ptr.* = val; }
}
// --- StateEntry — type-erased storage ---
StateEntry :: struct {
id: s64;
data: [*]u8;
size: s64;
generation: s64;
}
// --- StateStore — manages persistent state ---
StateStore :: struct {
entries: List(StateEntry);
current_generation: s64;
init :: (self: *StateStore) {
self.entries = List(StateEntry).{};
self.current_generation = 0;
}
get_or_create :: (self: *StateStore, id: s64, $T: Type, default: T) -> State(T) {
// Search for existing entry
i : s64 = 0;
while i < self.entries.len {
if self.entries.items[i].id == id {
self.entries.items[i].generation = self.current_generation;
return State(T).{ ptr = xx self.entries.items[i].data };
}
i += 1;
}
// Create new entry
data : [*]u8 = xx context.allocator.alloc(size_of(T));
memcpy(data, @default, size_of(T));
self.entries.append(.{
id = id,
data = data,
size = size_of(T),
generation = self.current_generation
});
State(T).{ ptr = xx data };
}
next_frame :: (self: *StateStore) {
self.current_generation += 1;
}
}