Skip to content

Commit

Permalink
[add] (lua) expose palette resources
Browse files Browse the repository at this point in the history
  • Loading branch information
begla committed Mar 30, 2024
1 parent 7cdfd83 commit 8a6acdd
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
102 changes: 102 additions & 0 deletions iolite_plugins/lua_plugin/init_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,52 @@ inline auto calc_random_float_fast(io_uint64_t& seed) -> float
// @summary Lists all properties exposed by this component interface.
// @return table value Table containing the names and types of all the exposed properties.

// @namespace Resources
// @category Resources Dummy category.
// @hidden

// @function get_type_id
// @summary Returns the type ID of the resource.
// @param component Ref The resource.
// @return number value The type ID of the resource.

// @function create
// @summary Creates a new resource with the given name.
// @param name string The name of the new resource.

// @function destroy
// @summary Destroys the provided resource.
// @param resource Ref The resource to destroy.

// @function commit_changes
// @summary Commits all changes to the properties of the provided resource.
// @param resource Ref The resource to update.

// @function get_num_active_resources
// @summary Returns the total number of active resources of this type.
// @return number value Total total number of active resources of this type.

// @function is_alive
// @summary Returns true if the referenced resource is alive.
// @param resource Ref The resource.
// @return boolean value True if the resource is alive.

// @function get_property
// @summary Returns the requested property as a variant.
// @param resource Ref The resource.
// @param property_name string The name of the property to retrieve.
// @return Variant value The property as a variant.

// @function set_property
// @summary Sets the requested property to the provided variant value.
// @param resource Ref The resource.
// @param property_name string The name of the property to set.
// @param value Variant The value to set.

// @function list_properties
// @summary Lists all properties exposed by this resource interface.
// @return table value Table containing the names and types of all the exposed properties.

// @namespace Interface
// @category Interface Dummy category.
// @hidden
Expand Down Expand Up @@ -2597,6 +2643,23 @@ void script_init_state(sol::state& s)
return properties; \
}

#define SHARED_RESOURCE_INTERFACE_IMPL(t_, i_) \
t_["get_type_id"] = i_->base.get_type_id; \
t_["create"] = i_->base.create; \
t_["destroy"] = i_->base.destroy; \
t_["commit_changes"] = [](io_ref_t resource) { if (i_->base.commit_changes) \
i_->base.commit_changes(resource); }; \
t_["get_num_active_resources"] = i_->base.get_num_active_resources; \
t_["set_property"] = i_->base.set_property; \
t_["get_property"] = i_->base.get_property; \
t_["list_properties"] = []() { \
uint32_t numProperties; \
i_->base.list_properties(nullptr, &numProperties); \
std::vector<io_property_desc_t> properties(numProperties); \
i_->base.list_properties(properties.data(), &numProperties); \
return properties; \
}

s["CustomData"] = s.create_table();
s["CustomData"]["load"] = [&s]() {

Expand Down Expand Up @@ -3249,6 +3312,45 @@ void script_init_state(sol::state& s)

};

s["Palette"] = s.create_table();
s["Palette"]["load"] = [&s]() {

SHARED_RESOURCE_INTERFACE_IMPL(s["Palette"], io_resource_palette);

// @namespace Palette
// @category Palette_Resource Functions to interact with palettes.
// @copy_category Interface
// @copy_category Resources

// @function set_color
// @summary Sets the color for the given palette index of the given palette.
// @param palette Ref The palette in question.
// @param palette_index number The palette index of the color to change.
// @param color Vec4 The color to set.
s["Particle"]["set_color"] = io_resource_palette->set_color;

// @function get_color
// @summary Gets the color for the given palette index of the given palette.
// @param palette Ref The palette in question.
// @param palette_index number The palette index of the color to change.
// @return Vec4 value The color at the given palette index.
s["Particle"]["get_color"] = io_resource_palette->get_color;

// @function set_material_parameters
// @summary Sets the material parameters for the given palette index of the given palette.
// @param palette Ref The palette in question.
// @param palette_index number The palette index of the material_parameters to change.
// @param material_parameters Vec4 The material parameters to set (roughness, matalmask, hardness, and emissive).
s["Particle"]["set_material_parameters"] = io_resource_palette->set_material_parameters;

// @function get_material_parameters
// @summary Gets the material parameters for the given palette index of the given palette.
// @param palette Ref The palette in question.
// @param palette_index number The palette index of the material_parameters to change.
// @return Vec4 value The material parameters at the given palette index (roughness, metalmask, hardness, and emissive).
s["Particle"]["get_material_parameters"] = io_resource_palette->get_material_parameters;
};

// Call into plugins and let them init. the state
auto lua_callback_interface =
(const io_plugin_lua_user_callack_i*)io_api_manager->find_first(
Expand Down
6 changes: 6 additions & 0 deletions iolite_plugins/lua_plugin/lua_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const io_component_vehicle_i* io_component_vehicle = {};
const io_component_vehicle_wheel_i* io_component_vehicle_wheel = {};
const io_component_joint_i* io_component_joint = {};

const io_resource_palette_i* io_resource_palette = {};

// Interfaces we provide
//----------------------------------------------------------------------------//
io_user_events_i io_user_events = {};
Expand Down Expand Up @@ -828,6 +830,10 @@ IO_API_EXPORT int IO_API_CALL load_plugin(void* api_manager)
io_component_joint = (const io_component_joint_i*)io_api_manager->find_first(
IO_COMPONENT_JOINT_API_NAME);

io_resource_palette =
(const io_resource_palette_i*)io_api_manager->find_first(
IO_RESOURCE_PALETTE_API_NAME);

// Factory plugin interfaces
io_plugin_terrain = (const io_plugin_terrain_i*)io_api_manager->find_first(
IO_PLUGIN_TERRAIN_API_NAME);
Expand Down
2 changes: 2 additions & 0 deletions iolite_plugins/lua_plugin/lua_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ extern const io_component_vehicle_i* io_component_vehicle;
extern const io_component_vehicle_wheel_i* io_component_vehicle_wheel;
extern const io_component_joint_i* io_component_joint;

extern const io_resource_palette_i* io_resource_palette;

// Custom types and helpers
//----------------------------------------------------------------------------//
template <typename T> struct lua_array_wrapper_t
Expand Down

0 comments on commit 8a6acdd

Please sign in to comment.