TnxViewModel.h file
View models: values a game shows that live in no component (docs/ui/Widgets.md, Contexts).
struct ConnectionInfo { float PingMs = 0.0f; int32_t Players = 0; TNX_VIEW_MODEL(ConnectionInfo, "Net.Connection", PingMs, Players) }; TNX_REGISTER_VIEW_MODEL(ConnectionInfo) TnxViewModel<ConnectionInfo> Connection; Connection.Set(&ConnectionInfo::PingMs, 42.0f); // any thread ui.Label(TnxBind(Connection, &ConnectionInfo::Players)); // bound in code TnxContextOf<ConnectionInfo> Net; // in a widget class: "=Net.PingMs" in its asset
Read by bindings like entity fields: a changed value repaints what shows it, with no rebuild and no polling of its own. For display only: gameplay state belongs in components, where it serializes and rolls back. A view model's fields are numbers, flags, enums, and Fixed32.
Namespaces
- namespace TnxViewModels
- The registry of view model types, and how bindings read their values.
Classes
- struct TnxViewModels::Field
- A field of a view model, where it is in its struct.
- struct TnxViewModels::TypeInfo
- A registered view model type.
- class TnxViewModelBase
- The non-template part of a view model: its target, and how readers reach its values.
-
template<typename T>class TnxViewModel
- One set of a view model's values, readable by bindings while it lives. Set from any thread.