UI element attributes

This commit is contained in:
Federico Cecchetto 2021-09-15 03:34:43 +02:00
parent fb90cc22dc
commit b66bee5f82
2 changed files with 100 additions and 0 deletions

View File

@ -61,6 +61,7 @@ namespace ui_scripting
alignment horzalign = alignment::start; alignment horzalign = alignment::start;
alignment vertalign = alignment::start; alignment vertalign = alignment::start;
std::unordered_map<std::string, std::string> attributes = {};
std::string font = "default"; std::string font = "default";
std::string material = "white"; std::string material = "white";
std::string border_material = "white"; std::string border_material = "white";

View File

@ -426,6 +426,21 @@ namespace ui_scripting::lua
handler.dispatch(event); handler.dispatch(event);
}; };
element_type[sol::meta_function::new_index] = [](element& element, const std::string& attribute, const std::string& value)
{
element.attributes[attribute] = value;
};
element_type[sol::meta_function::index] = [](element& element, const sol::this_state s, const std::string& attribute)
{
if (element.attributes.find(attribute) == element.attributes.end())
{
return sol::lua_value{s, sol::lua_nil};
}
return sol::lua_value{s, element.attributes[attribute]};
};
auto menu_type = state.new_usertype<menu>("menu"); auto menu_type = state.new_usertype<menu>("menu");
menu_type["onnotify"] = [&handler](menu& menu, const std::string& event, menu_type["onnotify"] = [&handler](menu& menu, const std::string& event,
@ -516,12 +531,96 @@ namespace ui_scripting::lua
menu.close(); menu.close();
}; };
menu_type["getelement"] = [](menu& menu, const sol::this_state s, const std::string& value, const std::string& attribute)
{
for (const auto& element : menu.children)
{
if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value)
{
return sol::lua_value{s, element};
}
}
return sol::lua_value{s, sol::lua_nil};
};
menu_type["getelements"] = sol::overload
(
[](menu& menu, const sol::this_state s, const std::string& value, const std::string& attribute)
{
auto result = sol::table::create(s.lua_state());
for (const auto& element : menu.children)
{
if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value)
{
result.add(element);
}
}
return result;
},
[](menu& menu, const sol::this_state s)
{
auto result = sol::table::create(s.lua_state());
for (const auto& element : menu.children)
{
result.add(element);
}
return result;
}
);
struct game struct game
{ {
}; };
auto game_type = state.new_usertype<game>("game_"); auto game_type = state.new_usertype<game>("game_");
state["game"] = game(); state["game"] = game();
game_type["getelement"] = [](const game&, const sol::this_state s, const std::string& value, const std::string& attribute)
{
for (const auto& element : elements)
{
if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value)
{
return sol::lua_value{s, element};
}
}
return sol::lua_value{s, sol::lua_nil};
};
game_type["getelements"] = sol::overload
(
[](const game&, const sol::this_state s, const std::string& value, const std::string& attribute)
{
auto result = sol::table::create(s.lua_state());
for (const auto& element : elements)
{
if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value)
{
result.add(element);
}
}
return result;
},
[](const game&, const sol::this_state s)
{
auto result = sol::table::create(s.lua_state());
for (const auto& element : elements)
{
result.add(element);
}
return result;
}
);
game_type["time"] = []() game_type["time"] = []()
{ {
const auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()); const auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());