16using SurfaceMap = std::map<std::string, Surface, std::less<>>;
45 std::optional<Kinesis::AABB2D>
clip;
57 template <
typename Source,
typename Access>
75 using Prepared = std::vector<PreparedInstance>;
78 const Actions* actions =
nullptr,
const Calls* calls =
nullptr)
89 if (Access::name(
m_root) !=
"forma")
90 return fail(
m_root,
"root element must be <forma>");
94 bool templates_seen =
false;
95 const bool traversed = Access::for_each_child(
m_root, [&](
const Source& section) {
96 const auto name = Access::name(section);
97 if (name ==
"templates") {
99 return fail(section,
"<templates> may appear only once");
100 templates_seen =
true;
103 if (name ==
"surface")
105 return fail(section,
"<" + std::string(name) +
"> is not allowed in <forma>");
114 const auto bound = surfaces.find(entry.id);
115 if (bound == surfaces.end()) {
116 fail(entry.element,
"surface " + entry.id +
" is not bound by the host");
119 for (
size_t i = 0; i < entry.instances.size(); ++i) {
120 const Source& instance = entry.instances[i];
122 std::optional<Placer> placer;
126 placer = std::invoke(*make, *info.
form, instance, info.
hit,
m_error);
133 placer = std::invoke(*decode, factory_element, instance,
m_error);
137 fail(instance,
"could not prepare the instance");
140 std::vector<ActionBinding> actions;
150 .filter = spec.
filter, .action = std::move(*action) });
153 .surface_id = entry.id,
154 .surface = &bound->second,
156 .row = Access::location(instance),
157 .placer = std::move(*placer),
159 .actions = std::move(actions),
166 std::optional<std::vector<Placer>>
prepare_bulk(std::string_view template_id,
169 const auto found =
m_templates.find(std::string(template_id));
171 m_error =
"document has no template " + std::string(template_id);
174 const auto* bulk =
m_table.find_bulk(Access::name(found->second));
176 fail(found->second,
"factory " + std::string(Access::name(found->second))
177 +
" has no bulk placement; register it with expose_typed");
180 auto placers = std::invoke(*bulk, found->second, rows,
m_error);
181 if (!placers &&
m_error.empty())
182 fail(found->second,
"could not prepare instances");
191 static bool apply(
const Prepared& prepared,
const std::vector<uint32_t>& elements,
194 for (
size_t i = 0; i < prepared.size(); ++i) {
195 Element* element = prepared[i].surface->layer().get(elements[i]);
197 error =
Table::located(prepared[i].row,
"instance " + prepared[i].
id
198 +
" placed an element the surface does not hold");
201 element->
name = prepared[i].id;
204 for (
size_t i = 0; i < prepared.size(); ++i) {
205 const auto& relate = prepared[i].layer.relate;
208 const auto target = std::ranges::find_if(prepared, [&](
const PreparedInstance& other) {
209 return other.
surface_id == prepared[i].surface_id && other.
id == *relate;
211 const size_t j =
static_cast<size_t>(target - prepared.begin());
212 if (target == prepared.end()
213 || !prepared[i].surface->layer().relate(elements[j], elements[i])) {
214 error =
Table::located(prepared[i].row,
"could not relate " + prepared[i].
id
220 for (
size_t i = 0; i < prepared.size(); ++i) {
221 Layer& layer = prepared[i].surface->layer();
229 if (attributes.
front)
235 for (
size_t i = 0; i < prepared.size(); ++i) {
237 wire(*prepared[i].surface, elements[i], prepared[i].
id, binding);
263 std::vector<InstanceInfo>
info;
276 bool fail(
const Source& at, std::string message)
282 bool only_attributes(
const Source& source, std::initializer_list<std::string_view> allowed,
283 std::string_view allowed_prefix = {})
285 return Access::for_each_attribute(source, [&](std::string_view name, std::string_view) {
286 if (std::ranges::find(allowed, name) != allowed.end()
287 || (!allowed_prefix.empty() && name.starts_with(allowed_prefix))) {
290 return fail(source,
"<" + std::string(Access::name(source))
291 +
"> has an unsupported attribute " + std::string(name));
297 const auto text = Access::attribute(
m_root,
"version");
299 return fail(
m_root,
"<forma> needs a version attribute");
303 return fail(
m_root,
"unsupported Forma document version " + std::string(*text));
307 std::optional<std::string>
claim_id(
const Source& source)
312 if (
const auto held =
m_names.location_of(
id)) {
313 fail(source,
"id " +
id +
" is already used by an element at row " + std::to_string(*held));
324 if (
const auto held =
m_ids.location_of(name)) {
325 fail(source,
"name " + name +
" is already used as an id at row " + std::to_string(*held));
335 return Access::for_each_child(section, [&](
const Source& definition) {
336 if (Access::name(definition) !=
"template") {
337 return fail(definition,
"<" + std::string(Access::name(definition))
338 +
"> is not allowed in <templates>");
348 const auto id =
claim_id(definition);
352 std::optional<Source> factory_element;
354 Access::for_each_child(definition, [&](
const Source& child) {
355 if (!factory_element)
356 factory_element.emplace(child);
361 return fail(definition,
"<template> needs exactly one factory element");
363 const auto name = Access::name(*factory_element);
365 return fail(*factory_element,
"no registered factory " + std::string(name));
378 SurfaceInstances entry { .id = *id, .element = surface, .instances = {}, .direct = {}, .info = {} };
379 const bool traversed = Access::for_each_child(surface, [&](
const Source& instance) {
380 const auto kind = Access::name(instance);
381 if (kind !=
"instance" && kind !=
"element") {
382 return fail(instance,
"<" + std::string(kind) +
"> is not allowed in <surface>");
385 entry.
direct.push_back(kind ==
"element");
393 bool read_flag(
const Source& instance, std::string_view name, std::string_view value,
bool& out)
397 return fail(instance,
"<" + std::string(Access::name(instance)) +
"> has an invalid "
398 + std::string(name) +
" attribute");
404 const bool read = Access::for_each_attribute(instance,
405 [&](std::string_view name, std::string_view value) {
415 if (key ==
"interactive") {
417 if (!
read_flag(instance, name, value, flag))
422 if (key ==
"relate") {
424 return fail(instance,
"<instance> needs an instance id in " + std::string(name));
425 out.
relate = std::string(value);
432 return fail(instance,
"<instance> needs four numbers in " + std::string(name));
433 if (!(rect.z > 0.F) || !(rect.w > 0.F))
434 return fail(instance, std::string(name) +
" needs a positive width and height");
438 return fail(instance,
"<instance> has an unsupported attribute " + std::string(name));
451 return Access::for_each_child(instance, [&](
const Source& child) {
453 return fail(child,
"<" + std::string(Access::name(child))
454 +
"> is not allowed in <instance>");
465 return fail(on,
"<on> needs an action table, and none was given");
469 return fail(on,
"<on> needs an event attribute");
474 return fail(on,
"<on> has an unsupported event " + std::string(*event_text));
478 if (event != Type::MOUSE_BUTTON_PRESSED && event != Type::MOUSE_BUTTON_RELEASED
479 && event != Type::MOUSE_MOTION) {
480 return fail(on,
"<on> takes a button only for MOUSE_BUTTON_PRESSED, MOUSE_BUTTON_RELEASED and MOUSE_MOTION");
485 return fail(on,
"<on> has an unsupported button " + std::string(*button_text));
491 if (event != Type::KEY_PRESSED && event != Type::KEY_RELEASED && event != Type::KEY_REPEAT)
492 return fail(on,
"<on> takes a key only for KEY_PRESSED, KEY_RELEASED and KEY_REPEAT");
495 return fail(on,
"<on> has an unsupported key " + std::string(*key_text));
500 return fail(on,
"<on> event " + std::string(*event_text)
501 +
" is not delivered to an element, or needs a key attribute");
506 return fail(on,
"<on> needs an action attribute");
508 return fail(on,
"no registered action " + std::string(*name));
511 .element = on, .filter = filter, .action = std::string(*name) });
515 static void wire(
Surface& surface, uint32_t element,
const std::string& instance,
521 [layer, context, instance, action = binding.
action](
523 action(ActionEvent { *layer, *context, id, instance, event, ndc });
530 for (
size_t k = 0; k < entry.instances.size(); ++k) {
531 const Source& instance = entry.instances[k];
533 if (entry.direct[k]) {
542 [
this](std::string_view value) {
return m_templates.contains(std::string(value)); },
547 info =
InstanceInfo { .id = *id, .template_id = std::string(*reference), .layer = {}, .actions = {}, .form = std::nullopt };
551 entry.info.push_back(std::move(info));
573 const bool read = Access::for_each_attribute(element,
574 [&](std::string_view attribute, std::string_view value) {
577 if (attribute ==
"visible")
578 return read_flag(element, attribute, value, visible);
579 if (attribute ==
"front")
581 if (attribute ==
"back")
583 if (attribute ==
"interactive") {
585 if (!
read_flag(element, attribute, value, flag))
590 if (attribute ==
"relate") {
592 return fail(element,
"<element> needs an element name in relate");
596 if (attribute ==
"clip") {
600 return fail(element,
"<element> needs four numbers in clip");
601 if (!(region.
max.x > region.
min.x) || !(region.
max.y > region.
min.y))
602 return fail(element,
"clip needs a positive width and height");
606 return fail(element,
"<element> has an unsupported attribute " + std::string(attribute));
612 return fail(element,
"front and back cannot both be set");
614 const bool traversed = Access::for_each_child(element, [&](
const Source& child) {
615 const auto kind = Access::name(child);
616 if (kind ==
"form") {
618 return fail(child,
"<element> may hold only one <form>");
626 return fail(child,
"<" + std::string(kind) +
"> is not allowed in <element>");
631 return fail(element,
"<element> needs a <form>");
643 if (call && topology)
644 return fail(form,
"<form> may call a function or give a topology, not both");
647 return fail(form,
"<form> needs a call or a topology attribute");
651 return fail(form,
"<form> needs a call table, and none was given");
653 return fail(form,
"no registered call " + std::string(*call));
654 bool has_child =
false;
655 Access::for_each_child(form, [&](
const Source&) {
660 return fail(form,
"<form> that calls a function takes no children");
668 fail(element,
"a form of vertices has no state, so <element> may not give one");
684 for (
size_t i = 0; i < entry.
info.size(); ++i) {
685 const auto& relate = entry.
info[i].layer.relate;
689 if (*relate == entry.
info[i].id) {
690 return fail(entry.
instances[i], attribute +
" may not name the "
691 + (entry.
direct[i] ?
"element" :
"instance") +
" itself");
693 const bool known = std::ranges::any_of(entry.
info, [&](
const InstanceInfo& other) {
694 return other.id == *relate;
698 + *relate +
", which is not an " + (entry.
direct[i] ?
"element or instance" :
"instance")
699 +
" of surface " + entry.
id);
710 template <
typename Source,
typename Access>
713 const Source& root,
SurfaceMap& surfaces, std::string& error)
719 const auto prepared = walk.
prepare(surfaces);
724 std::vector<uint32_t> elements;
725 elements.reserve(prepared->size());
726 for (
const auto& item : *prepared) {
727 const auto element = item.placer(*item.surface, error);
730 elements.push_back(*element);
731 placed.emplace(item.id,
PlacedInstance { .surface = item.surface_id, .element = *element });
776template <
typename Source,
typename Access = Reflect::RecordAccess<Source>>
797template <
typename Source,
typename Access = Reflect::RecordAccess<Source>>
800 const Source& root,
SurfaceMap& surfaces, std::string& error)
813template <
typename Source,
typename Access = Reflect::RecordAccess<Source>>
829template <
typename Source,
typename Access = Reflect::RecordAccess<Source>>
851template <
typename Source,
typename Access = Reflect::RecordAccess<Source>>
855 Surface& surface, std::string& error)
861 const auto placers = walk.
prepare_bulk(template_id, rows);
865 std::vector<uint32_t> elements;
866 elements.reserve(placers->size());
867 for (
const auto& placer : *placers) {
868 const auto element = placer(surface, error);
871 elements.push_back(*element);
882template <
typename Source,
typename Access = Reflect::RecordAccess<Source>>
886 Surface& surface, std::string& error)
892 const auto placers = walk.
prepare_bulk(template_id, rows);
896 std::vector<uint32_t> elements;
897 elements.reserve(placers->size());
898 for (
const auto& placer : *placers) {
899 const auto element = placer(surface, error);
902 elements.push_back(*element);
Owns a sequence of values that share a CompositeLayout.
bool subscribe(uint32_t id, Vruta::WindowEventFilter filter, SubscriberFn fn)
Subscribe to the window events that reach an element, without replacing any of its own callbacks.
static bool receives(const Vruta::WindowEventFilter &filter) noexcept
Whether an element can receive what a filter asks for.
Event wiring between a Layer and a window surface.
The values of one attribute across a tree, each claimed by one element.
WindowEventType
Types of window and input events.
MouseButtons
Enumeration for mouse buttons.
std::optional< Keys > from_string(std::string_view str) noexcept
Converts a string to the corresponding Keys enum value.
std::optional< std::string_view > check_reference(const Source &source, std::string_view attribute, Exists &&exists, std::string &error)
Check that an element's attribute names a value that exists.
Event data for window and input events.
static AABB2D from_origin_size(glm::vec2 origin, glm::vec2 size) noexcept
Construct from a minimum corner and a size.
Axis-aligned bounding rectangle in a 2D coordinate space.
Conversion of authored text to one value type.
std::optional< IO::Keys > key_code
std::optional< Core::WindowEventType > event_type
std::optional< IO::MouseButtons > button
Filter criteria for window input events.