VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
component.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 05.09.2025.
5//
6#pragma once
7
8#include <cstddef>
9#include <memory>
10#include <core/eventHandler.h>
11
12namespace visutwin::canvas
13{
14 class IComponentSystem;
15 class Entity;
16
17 using ComponentTypeID = std::size_t;
18
20 static ComponentTypeID lastID = 0;
21 return lastID++;
22 }
23
24 template <typename T>
27 return id;
28 }
29
39 class Component : public EventHandler
40 {
41 public:
43 virtual ~Component() = default;
44
45 Entity* entity() const;
46
47 IComponentSystem* system() const { return _system; }
48
49 virtual bool enabled() const { return _enabled; }
50 virtual void setEnabled(bool value);
51
52 virtual void initializeComponentData() = 0;
53
54 // Lifecycle methods matching upstream Component.
55 // Called when the component becomes active (component enabled AND entity enabled).
56 virtual void onEnable() {}
57
58 // Called when the component becomes inactive (component disabled OR entity disabled).
59 virtual void onDisable() {}
60
61 // Called after all hierarchy state changes have been processed.
62 virtual void onPostStateChange() {}
63
69 virtual void cloneFrom(const Component* source) {}
70
71 protected:
72 // Called internally when the enabled setter changes the value.
73 // Matching Upstream: only fires onEnable/onDisable if entity is also enabled.
74 virtual void onSetEnabled(bool oldValue, bool newValue);
75
77 bool _enabled = true;
78
79 private:
80 IComponentSystem* _system;
81 };
82}
Entity * entity() const
Definition component.cpp:16
virtual ~Component()=default
virtual void onPostStateChange()
Definition component.h:62
virtual void onSetEnabled(bool oldValue, bool newValue)
Definition component.cpp:28
virtual void initializeComponentData()=0
virtual bool enabled() const
Definition component.h:49
virtual void cloneFrom(const Component *source)
Definition component.h:69
Component(IComponentSystem *system, Entity *entity)
Definition component.cpp:12
virtual void onEnable()
Definition component.h:56
virtual void onDisable()
Definition component.h:59
virtual void setEnabled(bool value)
Definition component.cpp:21
IComponentSystem * system() const
Definition component.h:47
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
ComponentTypeID componentTypeID()
Definition component.h:25
std::size_t ComponentTypeID
Definition component.h:17
ComponentTypeID nextComponentTypeID()
Definition component.h:19