Skip to content

Commit

Permalink
sent font (without deleting for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
AGulev committed Nov 15, 2021
1 parent 26e6b8e commit 2731c37
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
20 changes: 17 additions & 3 deletions engine/gamesys/src/gamesys/components/comp_gui.cpp
Expand Up @@ -397,7 +397,7 @@ namespace dmGameSystem
for (uint32_t i = 0; i < scene_resource->m_FontMaps.Size(); ++i)
{
const char* name = scene_desc->m_Fonts[i].m_Name;
dmGui::Result r = dmGui::AddFont(scene, name, (void*) scene_resource->m_FontMaps[i], scene_resource->m_FontMapPaths[i]);
dmGui::Result r = dmGui::AddFont(scene, dmHashString64(name), (void*) scene_resource->m_FontMaps[i], scene_resource->m_FontMapPaths[i]);
if (r != dmGui::RESULT_OK) {
dmLogError("Unable to add font '%s' to scene (%d)", name, r);
return false;
Expand Down Expand Up @@ -659,8 +659,9 @@ namespace dmGameSystem
{
if (gui_world->m_Components[i] == gui_component)
{
dmResource::HFactory factory = dmGameObject::GetFactory(params.m_Instance);
if (gui_component->m_Material) {
dmResource::Release(dmGameObject::GetFactory(params.m_Instance), gui_component->m_Material);
dmResource::Release(factory, gui_component->m_Material);
}
dmGui::DeleteScene(gui_component->m_Scene);
delete gui_component;
Expand Down Expand Up @@ -2116,9 +2117,22 @@ namespace dmGameSystem

dmGameObject::PropertyResult CompGuiSetProperty(const dmGameObject::ComponentSetPropertyParams& params) {
GuiComponent* gui_component = (GuiComponent*)*params.m_UserData;
if (params.m_PropertyId == PROP_MATERIAL) {
dmhash_t set_property = params.m_PropertyId;
if (set_property == PROP_MATERIAL) {
return SetResourceProperty(dmGameObject::GetFactory(params.m_Instance), params.m_Value, MATERIAL_EXT_HASH, (void**)&gui_component->m_Material);
}
else if (set_property == PROP_FONT) {
dmRender::HFontMap font;
memset(&font, 0, sizeof(dmRender::HFontMap));
dmGameObject::PropertyResult res = SetResourceProperty(dmGameObject::GetFactory(params.m_Instance), params.m_Value, FONT_EXT_HASH, (void**)&font);
dmGui::HScene scene = gui_component->m_Scene;
dmGui::Result r = dmGui::AddFont(scene, params.m_Value.m_Hash, (void*) font, params.m_Value.m_Hash);
if (r != dmGui::RESULT_OK) {
dmLogError("Unable to set font property");
return dmGameObject::PROPERTY_RESULT_BUFFER_OVERFLOW;
}
return res;
}
return dmGameObject::PROPERTY_RESULT_NOT_FOUND;
}

Expand Down
17 changes: 7 additions & 10 deletions engine/gui/src/gui.cpp
Expand Up @@ -715,7 +715,7 @@ Result DeleteDynamicTexture(HScene scene, const dmhash_t texture_hash)
return 0;
}

Result AddFont(HScene scene, const char* font_name, void* font, dmhash_t path_hash)
Result AddFont(HScene scene, dmhash_t font_name_hash, void* font, dmhash_t path_hash)
{
if (scene->m_Fonts.Full())
return RESULT_OUT_OF_RESOURCES;
Expand All @@ -724,33 +724,30 @@ Result DeleteDynamicTexture(HScene scene, const dmhash_t texture_hash)
scene->m_DefaultFont = font;

AddResourcePath(scene, font, path_hash);

uint64_t font_hash = dmHashString64(font_name);
scene->m_Fonts.Put(font_hash, font);
scene->m_Fonts.Put(font_name_hash, font);
uint32_t n = scene->m_Nodes.Size();
InternalNode* nodes = scene->m_Nodes.Begin();
for (uint32_t i = 0; i < n; ++i)
{
if (nodes[i].m_Node.m_FontHash == font_hash)
if (nodes[i].m_Node.m_FontHash == font_name_hash)
nodes[i].m_Node.m_Font = font;
}
return RESULT_OK;
}

void RemoveFont(HScene scene, const char* font_name)
void RemoveFont(HScene scene, dmhash_t font_name_hash)
{
uint64_t font_hash = dmHashString64(font_name);
void** font = scene->m_Fonts.Get(font_hash);
void** font = scene->m_Fonts.Get(font_name_hash);
if (font)
{
RemoveResourcePath(scene, *font);
}
scene->m_Fonts.Erase(font_hash);
scene->m_Fonts.Erase(font_name_hash);
uint32_t n = scene->m_Nodes.Size();
InternalNode* nodes = scene->m_Nodes.Begin();
for (uint32_t i = 0; i < n; ++i)
{
if (nodes[i].m_Node.m_FontHash == font_hash)
if (nodes[i].m_Node.m_FontHash == font_name_hash)
nodes[i].m_Node.m_Font = 0;
}
}
Expand Down
8 changes: 4 additions & 4 deletions engine/gui/src/gui.h
Expand Up @@ -653,18 +653,18 @@ namespace dmGui
* Adds a font with the specified name to the scene.
* @note Any nodes connected to the same font_name will also be connected to the new font. This makes this function O(n), where n is #nodes.
* @param scene Scene to add texture to
* @param font_name Name of the font that will be used in the gui scripts
* @param font_name_hash Hash of the font name that will be used in the gui scripts
* @param font The font to add
* @return Outcome of the operation
*/
Result AddFont(HScene scene, const char* font_name, void* font, dmhash_t path_hash);
Result AddFont(HScene scene, dmhash_t font_name_hash, void* font, dmhash_t path_hash);
/**
* Removes a font with the specified name from the scene.
* @note Any nodes connected to the same font_name will also be disconnected from the font. This makes this function O(n), where n is #nodes.
* @param scene Scene to remove font from
* @param font_name Name of the font that will be used in the gui scripts
* @param font_name_hash Hash of the font name that will be used in the gui scripts
*/
void RemoveFont(HScene scene, const char* font_name);
void RemoveFont(HScene scene, dmhash_t font_name_hash);

/**
* Gets a font by name hash
Expand Down
16 changes: 8 additions & 8 deletions engine/gui/src/test/test_gui.cpp
Expand Up @@ -999,9 +999,9 @@ TEST_F(dmGuiTest, TextureFontLayer)

dmGui::AddTexture(m_Scene, "t1", (void*) &t1, dmGui::NODE_TEXTURE_TYPE_TEXTURE_SET, 1, 1);
dmGui::AddTexture(m_Scene, "t2", (void*) &t2, dmGui::NODE_TEXTURE_TYPE_TEXTURE_SET, 1, 1);
dmGui::AddFont(m_Scene, "f1", &f1, 0);
dmGui::AddFont(m_Scene, "f2", &f2, 0);
dmGui::AddFont(m_Scene, "test_font_id", &f2, test_font_path);
dmGui::AddFont(m_Scene, dmHashString64("f1"), &f1, 0);
dmGui::AddFont(m_Scene, dmHashString64("f2"), &f2, 0);
dmGui::AddFont(m_Scene, dmHashString64("test_font_id"), &f2, test_font_path);
dmGui::AddLayer(m_Scene, "l1");
dmGui::AddLayer(m_Scene, "l2");

Expand Down Expand Up @@ -1049,10 +1049,10 @@ TEST_F(dmGuiTest, TextureFontLayer)
r = dmGui::SetNodeFont(m_Scene, node, "f2");
ASSERT_EQ(r, dmGui::RESULT_OK);

dmGui::AddFont(m_Scene, "f2", &f1, 0);
dmGui::AddFont(m_Scene, dmHashString64("f2"), &f1, 0);
ASSERT_EQ(&f1, m_Scene->m_Nodes[node & 0xffff].m_Node.m_Font);

dmGui::RemoveFont(m_Scene, "f2");
dmGui::RemoveFont(m_Scene, dmHashString64("f2"));
ASSERT_EQ((void*)0, m_Scene->m_Nodes[node & 0xffff].m_Node.m_Font);

// Font path
Expand Down Expand Up @@ -1300,7 +1300,7 @@ TEST_F(dmGuiTest, ScriptTextureFontLayer)
int f;

dmGui::AddTexture(m_Scene, "t", (void*) &t, dmGui::NODE_TEXTURE_TYPE_TEXTURE_SET, 1, 1);
dmGui::AddFont(m_Scene, "f", &f, 0);
dmGui::AddFont(m_Scene, dmHashString64("f"), &f, 0);
dmGui::AddLayer(m_Scene, "l");

const char* id = "n";
Expand Down Expand Up @@ -2813,8 +2813,8 @@ TEST_F(dmGuiTest, DeltaTime)

TEST_F(dmGuiTest, Bug352)
{
dmGui::AddFont(m_Scene, "big_score", 0, 0);
dmGui::AddFont(m_Scene, "score", 0, 0);
dmGui::AddFont(m_Scene, dmHashString64("big_score"), 0, 0);
dmGui::AddFont(m_Scene, dmHashString64("score"), 0, 0);
dmGui::AddTexture(m_Scene, "left_hud", 0, dmGui::NODE_TEXTURE_TYPE_NONE, 1, 1);
dmGui::AddTexture(m_Scene, "right_hud", 0, dmGui::NODE_TEXTURE_TYPE_NONE, 1, 1);

Expand Down

0 comments on commit 2731c37

Please sign in to comment.