VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
layerComposition.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 11.09.2025.
5//
6#pragma once
7
8#include <map>
9#include <string>
10#include <unordered_map>
11#include <vector>
12
13#include "renderAction.h"
14#include "core/eventHandler.h"
15
16namespace visutwin::canvas
17{
18 /*
19 * Layer Composition is a collection of Layer that is fed to Scene#layers to define rendering.
20 */
22 {
23 public:
24 LayerComposition(const std::string& name = "Untitled"): _name(name) {}
26
27 // Adds part of the layer with opaque (non semi-transparent) objects to the end of the layerList
28 void pushOpaque(const std::shared_ptr<Layer>& layer);
29
30 // Adds part of the layer with semi-transparent objects to the end of the layerList
31 void pushTransparent(const std::shared_ptr<Layer>& layer);
32
33 const std::vector<RenderAction*>& renderActions();
34 std::shared_ptr<Layer> getLayerById(int layerId) const;
35 std::shared_ptr<Layer> getLayerByName(const std::string& name) const;
36 bool isEnabled(const Layer* layer, bool transparent) const;
37 void markDirty() { _dirty = true; }
38
39 private:
40 bool isSublayerAdded(const std::shared_ptr<Layer>& layer, bool transparent) const;
41 int getOpaqueIndex(const std::shared_ptr<Layer>& layer) const;
42 int getTransparentIndex(const std::shared_ptr<Layer>& layer) const;
43
44 void updateLayerMaps();
45 void rebuildRenderActions();
46 void clearRenderActions();
47
48 std::string _name;
49
50 // True if the composition needs to be updated before rendering.
51 bool _dirty = true;
52
53 std::vector<std::shared_ptr<Layer>> _layerList;
54
55 // A read-only array of boolean values, matching layerList.
56 // True means only semi-transparent objects are rendered, and false means opaque.
57 std::vector<bool> _subLayerList;
58
59 // A read-only array of boolean values, matching layerList.
60 // True means the layer is rendered; false means it's skipped.
61 std::vector<bool> _subLayerEnabled;
62
63 std::vector<RenderAction*> _renderActions;
64 size_t _lastCameraCount = 0;
65
66 // Opaque sublayer order mapping (layer id -> index)
67 std::unordered_map<int, int> _opaqueOrder;
68
69 // Transparent sublayer order mapping (layer id -> index)
70 std::unordered_map<int, int> _transparentOrder;
71
72 // A mapping of Layer to its transparent index in layerList.
73 std::map<std::shared_ptr<Layer>, int> _layerTransparentIndexMap;
74
75 // A mapping of Layer to its opaque index in layerList.
76 std::map<std::shared_ptr<Layer>, int> _layerOpaqueIndexMap;
77
78 // A mapping of Layer::id to Layer.
79 std::map<int, std::shared_ptr<Layer>> _layerIdMap;
80
81 // A mapping of Layer::name to Layer.
82 std::map<std::string, std::shared_ptr<Layer>> _layerNameMap;
83 };
84}
std::shared_ptr< Layer > getLayerById(int layerId) const
std::shared_ptr< Layer > getLayerByName(const std::string &name) const
const std::vector< RenderAction * > & renderActions()
bool isEnabled(const Layer *layer, bool transparent) const
LayerComposition(const std::string &name="Untitled")
void pushOpaque(const std::shared_ptr< Layer > &layer)
void pushTransparent(const std::shared_ptr< Layer > &layer)