Skip to main content

khora_data/ui/
layout_view.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Implementation of `UiLayoutView` for the ECS `World`.
16
17use crate::ecs::World;
18use crate::ecs::{Children, Parent};
19use crate::ui::components::{UiNode, UiTransform};
20use khora_core::ecs::entity::EntityId;
21use khora_core::ui::layout::UiLayoutView;
22use khora_core::ui::types::{UiNode as CoreUiNode, UiTransform as CoreUiTransform};
23
24impl UiLayoutView for World {
25    fn get_all_ui_entities(&self) -> Vec<EntityId> {
26        self.iter_entities()
27            .filter(|&e| self.get::<crate::ui::components::UiNode>(e).is_some())
28            .collect()
29    }
30
31    fn get_node(&self, entity: EntityId) -> Option<CoreUiNode> {
32        self.get::<UiNode>(entity)
33            .map(|n| CoreUiNode::from(n.clone()))
34    }
35
36    fn get_children(&self, entity: EntityId) -> Vec<EntityId> {
37        self.get::<Children>(entity)
38            .map(|c| c.0.clone())
39            .unwrap_or_default()
40    }
41
42    fn has_parent(&self, entity: EntityId) -> bool {
43        self.get::<Parent>(entity).is_some()
44    }
45
46    fn set_transform(&mut self, entity: EntityId, transform: CoreUiTransform) {
47        if let Some(existing) = self.get_mut::<UiTransform>(entity) {
48            *existing = UiTransform::from(transform);
49        } else {
50            let _ = self.add_component(entity, UiTransform::from(transform));
51        }
52    }
53}