Cogs.Core
CADAssetTexture.h
1#pragma once
2
3#include "CADAttributeMap.h"
4
5#include "Context.h"
6#include "Bridge/BridgeApi.h"
7#include "Bridge/ResourceFunctions.h"
8#include "Resources/ResourceFlags.h"
9#include "Resources/Texture.h"
10
11#include "Cogs.Rendering/Rendering/DataFormat.h"
12
13#include <glm/vec2.hpp>
14#include <glm/vec3.hpp>
15
16#include <span>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace Cogs::Editor
22{
26 struct ITagEntry {
28 std::string_view name;
29
32
34 std::span<const int> children;
35 };
36
44
45 struct TagEntry {
47 size_t nameOffset;
48 size_t nameLength;
49
52
55 size_t childrenLength;
56 };
57
58 public:
60 void clear()
61 {
62 tagsTxtEntries.clear();
63 allNames.clear();
64 allChildren.clear();
65 }
66
74 void addTag(std::string_view name, int parentId, std::span<const int> children)
75 {
76 TagEntry& tag = tagsTxtEntries.emplace_back();
77 tag.nameOffset = allNames.size();
78 allNames += name;
79 tag.nameLength = allNames.size() - tag.nameOffset;
80 tag.parentId = parentId;
81 tag.childrenOffset = allChildren.size();
82 allChildren.insert(allChildren.end(), children.begin(), children.end());
83 tag.childrenLength = allChildren.size() - tag.childrenOffset;
84 }
85
87 size_t size() const { return tagsTxtEntries.size(); }
88
95 ITagEntry getTagAt(size_t index) const
96 {
97 const TagEntry& e = this->tagsTxtEntries[index];
98 return ITagEntry{
99 .name = std::string_view(allNames.data() + e.nameOffset, e.nameLength),
100 .parentId = e.parentId,
101 .children = std::span<const int>(allChildren.data() + e.childrenOffset, e.childrenLength)
102 };
103 }
104
105 private:
106 std::vector<TagEntry> tagsTxtEntries;
107 std::string allNames;
108 std::vector<int> allChildren;
109 };
110
116 class CADAssetTexture final {
117 public:
123 CADAssetTexture(Cogs::Core::Context* context, bool powerOfTwoTextures = true)
124 : context(context), _attributeMap(powerOfTwoTextures)
125
126 {
127 textureId = ::getNextTextureId(context);
128 currentAttrSize = { 2, 2 };
129 _attributeMap.resize(currentAttrSize);
131 }
132
133 CADAssetTexture(const CADAssetTexture&) = delete;
134
136 {
137 release();
138 }
139
140 CADAssetTexture& operator=(const CADAssetTexture&) = delete;
141
143 ResourceId getTextureId() const { return this->textureId; }
144
146 CADAttributeMap& getAttributeMap() { return this->_attributeMap; }
147
149 glm::uvec2 getAttrSize() const { return this->_attributeMap.getAttrSize(); }
150
151 size_t getTagCount() const { return this->tagsTxtEntries.size(); }
152
153 ITagEntry getTagAt(size_t index) const
154 {
155 return tagsTxtEntries.getTagAt(index);
156 }
157
158 bool empty() const { return this->tagsTxtEntries.size() == 0U; }
159
163 void showAll() {
164 this->_attributeMap.init(this->_attributeMap.getDefaultColor(), this->_attributeMap.getDefaultAlpha());
165 this->updateAttributeMap();
166 }
167
174 void updateColorById(size_t index, glm::uvec3 color, bool recurse)
175 {
176 this->_attributeMap.growToContainIndex(index);
177 this->_attributeMap.setColor(index, color);
178
179 if (recurse && this->getTagCount() > index) {
180 const ITagEntry tag = this->getTagAt(index);
181 for (const int childIndex : tag.children) {
182 this->updateColorById(childIndex, color, recurse);
183 }
184 }
185 }
186
193 void updateAlphaById(size_t index, AttributeMapAlpha alpha, bool recurse)
194 {
195 this->_attributeMap.growToContainIndex(index);
196 this->_attributeMap.setAlpha(index, alpha);
197
198 if (recurse && this->getTagCount() > index) {
199 const ITagEntry tag = this->getTagAt(index);
200 for (const int childIndex : tag.children) {
201 this->updateAlphaById(childIndex, alpha, recurse);
202 }
203 }
204 }
205
211 {
212 bool resized = currentAttrSize != _attributeMap.getAttrSize();
213 currentAttrSize = _attributeMap.getAttrSize();
214 ::loadTextureResource(context, textureId, _attributeMap.getAttributeMap().data(),
215 int(currentAttrSize.x), int(currentAttrSize.y),
216 int(Cogs::TextureFormat::R8G8B8A8_UNORM), 0,
218
219 return resized;
220 }
221
229 void addTag(std::string_view name, int parentId, std::span<const int> children)
230 {
231 tagsTxtEntries.addTag(name, parentId, children);
232 }
233
234 private:
235 void release()
236 {
237 if (textureId != Cogs::Core::NoResourceId) {
238 ::releaseTexture(context, textureId);
239 textureId = Cogs::Core::NoResourceId;
240 }
241 }
242
243 private:
244 Cogs::Core::Context* context;
245 CADAttributeMap _attributeMap;
246 TagContainer tagsTxtEntries;
247 ResourceId textureId;
248 glm::uvec2 currentAttrSize;
249 };
250
251}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
void updateColorById(size_t index, glm::uvec3 color, bool recurse)
CADAttributeMap & getAttributeMap()
CADAssetTexture(Cogs::Core::Context *context, bool powerOfTwoTextures=true)
void updateAlphaById(size_t index, AttributeMapAlpha alpha, bool recurse)
glm::uvec2 getAttrSize() const
void addTag(std::string_view name, int parentId, std::span< const int > children)
ResourceId getTextureId() const
void init(glm::uvec3 color, AttributeMapAlpha alpha)
glm::uvec2 getAttrSize() const
glm::uvec3 getDefaultColor() const
Get default color value.
void resize(glm::uvec2 attrSize)
bool growToContainIndex(size_t index)
std::span< const uint8_t > getAttributeMap() const
void setAlpha(size_t index, AttributeMapAlpha alpha)
void setColor(size_t index, glm::uvec3 color)
size_t size() const
Gets number of tags.
ITagEntry getTagAt(size_t index) const
void clear()
Clear tags storage.
void addTag(std::string_view name, int parentId, std::span< const int > children)
@ LinearColorSpace
For textures with RGBA format without color space information, mark the data as being in linear color...
@ NoMipMaps
Do not generate mipmaps.
std::string_view name
std::span< const int > children