333 UINode(std::shared_ptr<Layout> root, std::shared_ptr<BuildContext> ctx)
334 : root_(std::move(root)), ctx_(std::move(ctx)) {}
336 std::shared_ptr<Layout> root()
const {
return root_; }
337 std::shared_ptr<BuildContext> context()
const {
return ctx_; }
351 std::shared_ptr<Layout>
find(
const std::string &
id)
const;
354 template <
typename T = Layout>
355 std::shared_ptr<T>
find(
const std::string &
id)
const
358 return n ? std::static_pointer_cast<T>(n) :
nullptr;
362 template <
typename T = Layout>
363 std::shared_ptr<T>
ref(
const std::string &name)
const
365 auto it = ctx_->byRef.find(name);
366 if (it == ctx_->byRef.end() || it->second.empty())
368 warnOnce(
"ref:" + name,
369 "UINode::ref('" + name +
"'): no node with this ref");
372 std::shared_ptr<Layout> firstLive;
373 size_t liveCount = 0;
374 for (
const auto &wp : it->second)
376 if (
auto sp = wp.lock())
387 warnOnce(
"ref-ambig:" + name,
388 "UINode::ref('" + name +
"'): " +
389 std::to_string(liveCount) +
390 " nodes match, returning the first. "
391 "Use refAll for the full list.");
393 return std::static_pointer_cast<T>(firstLive);
397 template <
typename T = Layout>
398 std::vector<std::shared_ptr<T>>
refAll(
const std::string &name)
const
400 auto it = ctx_->byRef.find(name);
401 if (it == ctx_->byRef.end() || it->second.empty())
403 logDebug(
"ZMarkup",
"", 0, 0,
404 "UINode::refAll('" + name +
"'): empty");
407 std::vector<std::shared_ptr<T>> out;
408 out.reserve(it->second.size());
409 for (
const auto &wp : it->second)
411 if (
auto sp = wp.lock())
412 out.push_back(std::static_pointer_cast<T>(sp));
419 UINode &onClick(
const std::string &
id, std::function<
void()> cb);
420 UINode &onPress(
const std::string &
id, std::function<
void()> cb);
421 UINode &onRelease(
const std::string &
id, std::function<
void()> cb);
422 UINode &onHoverEnter(
const std::string &
id, std::function<
void()> cb);
423 UINode &onHoverExit(
const std::string &
id, std::function<
void()> cb);
424 UINode &onValueChanged(
const std::string &
id, std::function<
void(
float)> cb);
425 UINode &onToggle(
const std::string &
id, std::function<
void(
bool)> cb);
429 UINode &setTextRef(
const std::string &refName,
const std::string &text);
430 UINode &setButtonTextRef(
const std::string &refName,
const std::string &text);
431 UINode &addClassRef(
const std::string &refName,
const std::string &cls);
432 UINode &removeClassRef(
const std::string &refName,
const std::string &cls);
433 UINode &toggleClassRef(
const std::string &refName,
const std::string &cls,
bool active);
434 UINode &setEnabledRef(
const std::string &refName,
bool enabled);
435 UINode &setInteractiveRef(
const std::string &refName,
bool interactive);
436 UINode &setVisibleRef(
const std::string &refName,
bool visible);
437 UINode &onClickRef(
const std::string &refName, std::function<
void()> cb);
438 UINode &onPressRef(
const std::string &refName, std::function<
void()> cb);
439 UINode &onReleaseRef(
const std::string &refName, std::function<
void()> cb);
440 UINode &onHoverEnterRef(
const std::string &refName, std::function<
void()> cb);
441 UINode &onHoverExitRef(
const std::string &refName, std::function<
void()> cb);
442 UINode &onTextChangedRef(
const std::string &refName, std::function<
void(
const std::string &)> cb);
443 UINode &onValueChangedRef(
const std::string &refName, std::function<
void(
float)> cb);
444 UINode &onToggleRef(
const std::string &refName, std::function<
void(
bool)> cb);
448 UINode &setCondition(
const std::string &condRef,
bool value);
449 UINode &bindCondition(
const std::string &condRef,
451 bool fireImmediately =
true);
453 template <
typename Range,
typename Fn>
454 UINode &syncEach(
const std::string &eachRef,
const Range &items, Fn &&binder)
456 auto it = ctx_->eaches.find(eachRef);
457 if (it == ctx_->eaches.end())
459 warnOnce(
"each:" + eachRef,
460 "UINode::syncEach('" + eachRef +
"'): no Each with this ref");
464 auto &binding = it->second;
465 auto anchor = binding.anchor.lock();
466 auto parent = anchor ? anchor->getParent() :
nullptr;
470 std::shared_ptr<const Registry> regKeepAlive;
471 const Registry *reg = ctx_->resolveRegistry(regKeepAlive);
475 binding.keyedStore.reset();
476 clearEachNodes_(binding, *parent);
478 const bool effVis = ctx_->isConditionVisible(binding.parentCondition);
479 const bool isEmpty = (std::begin(items) == std::end(items));
483 mountEachFallback_(*reg, binding, *parent, anchor.get(), effVis);
487 binding.showingFallback =
false;
489 for (
const auto &elem : items)
491 UINode itemUi = reg->instantiate(binding.prefabName);
492 auto itemRoot = itemUi.root();
496 invokeEachCallback_(binder, itemUi, elem, idx);
498 itemRoot->setVisible(
false);
500 parent->insertChildBefore(anchor.get(), itemRoot);
501 binding.spawnedNodes.push_back(itemRoot);
507 template <
typename T,
typename Fn>
508 UINode &bindEach(
const std::string &eachRef,
509 Bindable<std::vector<T>> &source,
511 bool fireImmediately =
true)
513 if (ctx_->eaches.find(eachRef) == ctx_->eaches.end())
515 warnOnce(
"each:" + eachRef,
516 "UINode::bindEach('" + eachRef +
"'): no Each with this ref");
522 std::weak_ptr<Layout> weakRoot = root_;
523 std::weak_ptr<BuildContext> weakCtx = ctx_;
524 auto sub = source.onChanged(
525 [weakRoot, weakCtx, eachRef, binder = std::move(binder)](
const std::vector<T> &vec)
mutable
527 auto r = weakRoot.lock();
528 auto c = weakCtx.lock();
531 UINode ui(std::move(r), std::move(c));
532 ui.syncEach(eachRef, vec, binder);
536 ctx_->eachSubs[eachRef] =
537 std::make_shared<typename Bindable<std::vector<T>>::Subscription>(std::move(sub));
541 template <
typename Range,
typename KeyFn,
typename OnCreateFn,
typename OnUpdateFn>
542 UINode &syncEachKeyed(
const std::string &eachRef,
545 OnCreateFn &&onCreate,
546 OnUpdateFn &&onUpdate)
548 auto it = ctx_->eaches.find(eachRef);
549 if (it == ctx_->eaches.end())
551 warnOnce(
"each:" + eachRef,
552 "UINode::syncEachKeyed('" + eachRef +
"'): no Each with this ref");
556 auto &binding = it->second;
557 auto anchor = binding.anchor.lock();
558 auto parent = anchor ? anchor->getParent() :
nullptr;
562 std::shared_ptr<const Registry> regKeepAlive;
563 const Registry *reg = ctx_->resolveRegistry(regKeepAlive);
567 using ElemT = std::decay_t<
decltype(*std::begin(items))>;
568 using KeyT = std::decay_t<std::invoke_result_t<KeyFn &, const ElemT &>>;
570 const bool effVis = ctx_->isConditionVisible(binding.parentCondition);
571 const bool isEmpty = (std::begin(items) == std::end(items));
575 binding.keyedStore.reset();
576 clearEachNodes_(binding, *parent);
577 mountEachFallback_(*reg, binding, *parent, anchor.get(), effVis);
581 if (binding.showingFallback)
582 clearEachNodes_(binding, *parent);
584 auto *store =
dynamic_cast<KeyedStore<KeyT> *
>(binding.keyedStore.get());
587 auto newStore = std::make_shared<KeyedStore<KeyT>>();
588 store = newStore.get();
589 binding.keyedStore = std::move(newStore);
592 std::unordered_map<KeyT, typename KeyedStore<KeyT>::Entry> nextEntries;
593 nextEntries.reserve(store->entries.size() + 4);
594 std::vector<std::shared_ptr<Layout>> nextNodes;
597 for (
const auto &elem : items)
599 KeyT key = keyFn(elem);
600 if (nextEntries.count(key) > 0)
602 logWarn(
"ZMarkup",
"", 0, 0,
603 "UINode::syncEachKeyed('" + eachRef +
"'): duplicate key detected, skipping duplicate item");
608 auto existing = store->entries.find(key);
609 std::shared_ptr<Layout> existingRoot =
610 (existing != store->entries.end()) ? existing->second.root.lock() :
nullptr;
614 auto entry = std::move(existing->second);
615 UINode itemUi(existingRoot, entry.ctx);
616 invokeEachCallback_(onUpdate, itemUi, elem, idx);
618 existingRoot->setVisible(
false);
620 nextNodes.push_back(std::move(existingRoot));
621 nextEntries.emplace(std::move(key), std::move(entry));
625 UINode itemUi = reg->instantiate(binding.prefabName);
626 auto itemRoot = itemUi.root();
630 invokeEachCallback_(onCreate, itemUi, elem, idx);
631 invokeEachCallback_(onUpdate, itemUi, elem, idx);
633 itemRoot->setVisible(
false);
635 nextEntries.emplace(std::move(key),
636 typename KeyedStore<KeyT>::Entry{itemRoot, itemUi.context()});
637 nextNodes.push_back(std::move(itemRoot));
642 bool sameOrder = (nextNodes.size() == binding.spawnedNodes.size());
645 for (
size_t i = 0; i < nextNodes.size(); ++i)
647 if (nextNodes[i].get() != binding.spawnedNodes[i].lock().get())
657 std::unordered_set<const Layout *> keepSet;
658 keepSet.reserve(nextNodes.size());
659 for (
const auto &n : nextNodes)
660 keepSet.insert(n.get());
662 for (
auto &oldWp : binding.spawnedNodes)
664 if (
auto oldNode = oldWp.lock())
666 if (keepSet.find(oldNode.get()) == keepSet.end())
667 parent->removeChild(oldNode.get());
671 for (
auto &newNode : nextNodes)
673 if (newNode->getParent().get() == parent.get())
674 parent->moveChildBefore(anchor.get(), newNode.get());
676 parent->insertChildBefore(anchor.get(), newNode);
680 store->entries = std::move(nextEntries);
681 binding.spawnedNodes.clear();
682 binding.spawnedNodes.reserve(nextNodes.size());
683 for (
const auto &n : nextNodes)
684 binding.spawnedNodes.push_back(n);
685 binding.showingFallback =
false;
689 template <
typename T,
typename KeyFn,
typename OnCreateFn,
typename OnUpdateFn>
690 UINode &bindEachKeyed(
const std::string &eachRef,
691 Bindable<std::vector<T>> &source,
695 bool fireImmediately =
true)
697 if (ctx_->eaches.find(eachRef) == ctx_->eaches.end())
699 warnOnce(
"each:" + eachRef,
700 "UINode::bindEachKeyed('" + eachRef +
"'): no Each with this ref");
706 std::weak_ptr<Layout> weakRoot = root_;
707 std::weak_ptr<BuildContext> weakCtx = ctx_;
708 auto sub = source.onChanged(
709 [weakRoot, weakCtx, eachRef,
710 keyFn = std::move(keyFn),
711 onCreate = std::move(onCreate),
712 onUpdate = std::move(onUpdate)](
const std::vector<T> &vec)
mutable
714 auto r = weakRoot.lock();
715 auto c = weakCtx.lock();
718 UINode ui(std::move(r), std::move(c));
719 ui.syncEachKeyed(eachRef, vec, keyFn, onCreate, onUpdate);
723 ctx_->eachSubs[eachRef] =
724 std::make_shared<typename Bindable<std::vector<T>>::Subscription>(std::move(sub));
729 void attachToRoot_()
const;
730 void warnOnce(
const std::string &key,
const std::string &msg)
const;
732 static void propagateCondition_(BuildContext &ctx,
const std::string &condName,
bool parentEffective);
733 static void clearEachNodes_(EachBinding &binding, Layout &parent);
734 static void mountEachFallback_(
const Registry ®, EachBinding &binding, Layout &parent,
const Layout *anchor,
bool effVis);
736 template <
typename Fn,
typename Elem>
737 static void invokeEachCallback_(Fn &fn, UINode &itemUi,
const Elem &elem,
size_t idx)
739 if constexpr (std::is_invocable_v<Fn &, UINode &, const Elem &, size_t>)
740 fn(itemUi, elem, idx);
741 else if constexpr (std::is_invocable_v<Fn &, UINode &, const Elem &>)
747 std::shared_ptr<Layout> root_;
748 std::shared_ptr<BuildContext> ctx_;
749 mutable std::unordered_set<std::string> warnedKeys_;