File size: 1,364 Bytes
2edf277 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | // FCS Glass - src/core/state.cpp
//
// Implements: C2, C3. See src/core/state.h.
#include "core/state.h"
namespace fcs {
namespace core {
// REQ C2 / C3 - one roll per frame. Swap rather than copy: it is O(1) instead
// of O(widgets), and it makes "previous" exactly the map that was current a
// moment ago, with no chance of the two drifting apart.
void StateStore::BeginFrame() {
prev_.swap(cur_);
cur_.clear();
++frame_;
}
// REQ C2 - created on first sight, default-initialised. operator[] does the
// insert; the map is the single owner of the state, so a widget cannot hold a
// copy that outlives its key.
WidgetState& StateStore::Get(Id id) { return cur_[id]; }
// REQ C3 - the previous frame's copy. An id that did not exist last frame reads
// as a default-constructed WidgetState, which is the honest answer: it was not
// hovered, because it was not there to be hovered.
const WidgetState& StateStore::prev(Id id) const {
const auto it = prev_.find(id);
return (it == prev_.end()) ? unknown_ : it->second;
}
// REQ T6 - surface rebuild. Deliberately wipes BOTH maps: keeping `prev_` would
// leave widgets reporting hover against a surface that no longer exists.
void StateStore::Clear() {
cur_.clear();
prev_.clear();
unknown_ = WidgetState{};
}
} // namespace core
} // namespace fcs
|