Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeki2DPackage.cpp
More file actions
Latest commit
309 lines (253 loc) · 9.66 KB
/
Copy pathDeki2DPackage.cpp
File metadata and controls
309 lines (253 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* @file Deki2DPackage.cpp
* @brief Package entry point for deki-2d DLL
*
* This file exports the standard Deki plugin interface so the editor
* can load deki-2d.dll and register its components.
*
* For linked DLLs (not dynamically loaded), Deki2D_EnsureRegistered()
* must be called from the main executable to trigger the static initializers.
*/
#include"Deki2DPackage.h"
#include"interop/DekiPlugin.h"
#include"SpriteComponent.h"
#include"TextComponent.h"
#include"GradientComponent.h"
#include"ButtonComponent.h"
#include"ButtonStyleComponent.h"
#include"ScrollComponent.h"
#include"RollerComponent.h"
#include"AnimationComponent.h"
#include"reflection/ComponentRegistry.h"
#include"reflection/ComponentFactory.h"
#ifdef DEKI_EDITOR
#include"editor/FontSyncHandler.h"
#include"editor/FontFileInspector.h"
#include"editor/BdfFileInspector.h"
#include"editor/FontCompiler.h"
#include"BitmapFont.h"
#include<deki-editor/AssetPipeline.h>
#include<deki-editor/EditorAssets.h>
#include"Guid.h"
#include"Scene.h"
#include"DekiObject.h"
#include"DekiLogSystem.h"
#include<cstdlib>
#include<functional>
#endif
#ifdef DEKI_EDITOR
// =============================================================================
// Linked DLL initialization
// =============================================================================
// When deki-2d is linked (not dynamically loaded), the editor must call
// this function to ensure the DLL code is actually loaded and the static
// initializers (REGISTER_COMPONENT) have run.
#ifndef DEKI_PLUGIN_EXPORTS
// Auto-generated registration helpers (standalone DLL only)
externvoidDeki2D_RegisterComponents();
externintDeki2D_GetAutoComponentCount();
externconst DekiComponentMeta* Deki2D_GetAutoComponentMeta(int index);
// Track if already registered to avoid duplicates
staticbool s_Registered = false;
#endif
namespaceDeki2D
{
// The editor preview font resolver, defined in editor/Deki2DEditorPreview.cpp.
BitmapFont* EditorFontResolve(TextComponent* tc);
}
extern"C" {
#ifndef DEKI_PLUGIN_EXPORTS
/**
* @brief Ensure deki-2d package is loaded and components are registered
*
* Call this from the editor at startup. Simply calling this function is enough
* to force the linker to include the DLL and trigger static initializers.
*
* @return Number of components registered by this package
*/
DEKI_2D_APIintDeki2D_EnsureRegistered(void)
{
if (s_Registered)
returnDeki2D_GetAutoComponentCount();
s_Registered = true;
// Auto-generated: registers all 2D components with ComponentRegistry + ComponentFactory
Deki2D_RegisterComponents();
// Clipping needs no pass: Standard2DRenderer pushes a clip rect for every
// object that provides IClipProvider (ClipComponent) while it draws.
// Register font-related editor features
Deki2D::RegisterFontSyncHandlers();
Deki2D::RegisterFontFileInspector();
Deki2D::RegisterBdfFileInspector();
// Initialize font preview callbacks for live editing in SceneView
Deki2D::InitializeFontPreviewCallbacks();
// Register font resolve callback for TextComponent (GUID sync, preview, baking)
TextComponent::SetFontResolveCallback(Deki2D::EditorFontResolve);
// Register image loader and font factory with EditorAssets
DekiEditor::EditorAssets::RegisterImageLoader(Texture2D::LoadAsRGBA);
DekiEditor::EditorAssets::RegisterFontFactory(
// Font factory: load a BitmapFont (handles v1/v2/v3/v4) and, separately,
// hand the editor the raw RGBA bytes of the atlas so it can upload a
// preview texture.
[](constchar* dfontPath, uint8_t** outAtlasRGBA, int32_t& outW, int32_t& outH) -> void* {
if (!dfontPath) returnnullptr;
BitmapFont* font = BitmapFont::Load(dfontPath);
if (!font) returnnullptr;
const std::string& atlasAbsPath = font->GetAtlasPath();
if (atlasAbsPath.empty()) { delete font; returnnullptr; }
int32_t atlasW = 0, atlasH = 0;
bool hasAlpha = false;
uint8_t* rgba = Texture2D::LoadAsRGBA(atlasAbsPath.c_str(), atlasW, atlasH, hasAlpha);
if (!rgba) { delete font; returnnullptr; }
if (outAtlasRGBA) *outAtlasRGBA = rgba; elsefree(rgba);
outW = atlasW;
outH = atlasH;
return font;
},
// Font destroyer
[](void* f) { deletestatic_cast<BitmapFont*>(f); }
);
returnDeki2D_GetAutoComponentCount();
}
#endif// DEKI_PLUGIN_EXPORTS
} // extern "C"
// =============================================================================
// Plugin metadata (for dynamic loading compatibility)
// =============================================================================
extern"C" {
#ifndef DEKI_PLUGIN_EXPORTS
DEKI_PLUGIN_APIconstchar* DekiPlugin_GetName(void)
{
return"Deki 2D Package";
}
DEKI_PLUGIN_APIconstchar* DekiPlugin_GetVersion(void)
{
#ifdef DEKI_PACKAGE_VERSION
returnDEKI_PACKAGE_VERSION;
#else
return"0.0.0-dev";
#endif
}
DEKI_PLUGIN_APIintDekiPlugin_Init(void)
{
// No special initialization needed
return0;
}
DEKI_PLUGIN_APIvoidDekiPlugin_Shutdown(void)
{
s_Registered = false;
Deki2D::ClearPreviewTextureCache();
}
DEKI_PLUGIN_APIintDekiPlugin_GetComponentCount(void)
{
returnDeki2D_GetAutoComponentCount();
}
DEKI_PLUGIN_APIconst DekiComponentMeta* DekiPlugin_GetComponentMeta(int index)
{
returnDeki2D_GetAutoComponentMeta(index);
}
DEKI_PLUGIN_APIvoidDekiPlugin_RegisterComponents(void)
{
Deki2D_EnsureRegistered();
}
#endif// DEKI_PLUGIN_EXPORTS
// =============================================================================
// Package-specific feature API (for linked DLL access without name conflicts)
// =============================================================================
DEKI_2D_APIconstchar* Deki2D_GetName(void)
{
return"2D";
}
// =============================================================================
// Play Mode Hook
// =============================================================================
#ifndef DEKI_PLUGIN_EXPORTS
DEKI_PLUGIN_APIvoidDekiPlugin_OnPlayModeStart(void* scenePtr)
{
if (!scenePtr)
return;
::Scene* scene = static_cast<::Scene*>(scenePtr);
// Set baked font GUIDs on TextComponents for play mode
std::function<void(DekiObject*)> setFontGuidsRecursive = [&](DekiObject* obj) {
if (!obj) return;
TextComponent* textComp = obj->GetComponent<TextComponent>();
if (textComp && !textComp->font.source.empty())
{
// Detect BDF vs TTF to use correct GUID convention
bool isBdf = false;
auto* pl = DekiEditor::AssetPipeline::Instance();
constauto* fi = pl ? pl->GetAssetInfoByGuid(textComp->font.source) : nullptr;
if (fi)
{
std::string ext = std::filesystem::path(fi->path).extension().string();
for (char& c : ext) c = static_cast<char>(std::tolower(static_cast<unsignedchar>(c)));
isBdf = (ext == ".bdf");
}
std::string seed = isBdf
? (textComp->font.source + ":bdf")
: (textComp->font.source + ":" + std::to_string(textComp->fontSize));
textComp->font.guid = Deki::GenerateDeterministicGuid(seed);
}
for (DekiObject* child : obj->GetChildren())
{
setFontGuidsRecursive(child);
}
};
for (DekiObject* obj : scene->GetObjects())
{
setFontGuidsRecursive(obj);
}
}
DEKI_PLUGIN_APIvoidDekiPlugin_OnPlayModeStop(void)
{
// Nothing to reset: AnimationComponent drives itself from Update().
}
// =============================================================================
// Font Compilation Wrappers (for AssetExporter via function pointers)
//
// Uses opaque handle pattern so the editor never needs FontCompiler headers.
// Flow: CompileFont → GetCompiledFontAtlas → WriteDfont → FreeCompileResult
// =============================================================================
DEKI_PLUGIN_APIvoid* DekiPlugin_CompileFont(constchar* ttfPath, int fontSize,
int firstChar, int lastChar, int padding, int maxAtlas)
{
if (!ttfPath)
returnnullptr;
auto* result = newDeki2D::FontCompiler::CompileResult();
Deki2D::FontCompiler::CompileOptions options;
options.fontSize = fontSize;
options.firstChar = firstChar;
options.lastChar = lastChar;
options.padding = padding;
options.maxAtlasSize = maxAtlas;
if (!Deki2D::FontCompiler::CompileTrueTypeFont(ttfPath, options, *result))
{
delete result;
returnnullptr;
}
return result;
}
DEKI_PLUGIN_APIboolDekiPlugin_GetCompiledFontAtlas(constvoid* handle,
constuint8_t** atlasData, int* width, int* height)
{
if (!handle || !atlasData || !width || !height)
returnfalse;
constauto* result = static_cast<const Deki2D::FontCompiler::CompileResult*>(handle);
*atlasData = result->atlasRGBA.data();
*width = result->atlasWidth;
*height = result->atlasHeight;
returntrue;
}
DEKI_PLUGIN_APIboolDekiPlugin_WriteDfont(constchar* path, constvoid* handle, constchar* atlasGuid)
{
if (!path || !handle || !atlasGuid)
returnfalse;
constauto* result = static_cast<const Deki2D::FontCompiler::CompileResult*>(handle);
returnDeki2D::FontCompiler::WriteDfontFile(path, *result, atlasGuid);
}
DEKI_PLUGIN_APIvoidDekiPlugin_FreeCompileResult(void* handle)
{
deletestatic_cast<Deki2D::FontCompiler::CompileResult*>(handle);
}
#endif// DEKI_PLUGIN_EXPORTS
} // extern "C"
#endif// DEKI_EDITOR