capi: exposed Accessor apis

This patch adds basic Accessor API to the C bindings
This commit is contained in:
Dragoș Tiselice 2025-03-25 15:18:02 +02:00 committed by Hermet Park
parent 6ee410e042
commit bc5d90c157
2 changed files with 73 additions and 4 deletions

View file

@ -101,6 +101,11 @@ typedef struct _Tvg_Saver Tvg_Saver;
*/
typedef struct _Tvg_Animation Tvg_Animation;
/**
* @brief A structure representing an object that enables iterating through a scene's descendents.
*/
typedef struct _Tvg_Accessor Tvg_Accessor;
/**
* @brief Enumeration specifying the engine type used for the graphics backend. For multiple backends bitwise operation is allowed.
@ -666,7 +671,7 @@ TVG_API Tvg_Result tvg_canvas_update_paint(Tvg_Canvas* canvas, Tvg_Paint* paint)
* @return Tvg_Result enumeration.
* @retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Canvas pointer.
*
* @note Clearing the buffer is unnecessary if the canvas will be fully covered
* @note Clearing the buffer is unnecessary if the canvas will be fully covered
* with opaque content, which can improve performance.
* @note Drawing may be asynchronous if the thread count is greater than zero.
* To ensure drawing is complete, call tvg_canvas_sync() afterwards.
@ -925,7 +930,7 @@ TVG_API Tvg_Paint* tvg_paint_duplicate(Tvg_Paint* paint);
* @return Tvg_Result enumeration.
* @retval TVG_RESULT_INVALID_ARGUMENT An invalid @p paint.
* @retval TVG_RESULT_INSUFFICIENT_CONDITION If it failed to compute the bounding box (mostly due to invalid path information).
*
*
* @see tvg_paint_get_obb()
* @see tvg_canvas_update_paint()
*/
@ -934,7 +939,7 @@ TVG_API Tvg_Result tvg_paint_get_aabb(const Tvg_Paint* paint, float* x, float* y
/**
* @brief Retrieves the object-oriented bounding box (OBB) of the paint object in canvas space.
*
*
* This function returns the bounding box of the paint, as an oriented bounding box (OBB) after transformations are applied.
*
* @param[in] paint The Tvg_Paint object of which to get the bounds.
@ -943,7 +948,7 @@ TVG_API Tvg_Result tvg_paint_get_aabb(const Tvg_Paint* paint, float* x, float* y
* @return Tvg_Result enumeration.
* @retval TVG_RESULT_INVALID_ARGUMENT @p paint or @p pt4 is invalid.
* @retval TVG_RESULT_INSUFFICIENT_CONDITION If it failed to compute the bounding box (mostly due to invalid path information).
*
*
* @see tvg_paint_get_aabb()
* @see tvg_canvas_update_paint()
*
@ -2448,6 +2453,48 @@ TVG_API Tvg_Result tvg_animation_del(Tvg_Animation* animation);
/************************************************************************/
/* Accessor API */
/************************************************************************/
/*!
* @brief Creates a new accessor object.
*
* @return A new accessor object.
*
* @note Experimental API
*/
TVG_API Tvg_Accessor* tvg_accessor_new();
/*!
* @brief Deletes the given accessor object.
*
* @param[in] accessor The accessor object to be deleted.
*
* @return Tvg_Result enumeration.
* @retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Accessor pointer.
*
* @note Experimental API
*/
TVG_API Tvg_Result tvg_accessor_del(Tvg_Accessor* accessor);
/*!
* @brief Sets the paint of the accessor then iterates through its descendents.
*
* Iterates through all descendents of the scene passed through the paint argument
* while calling func on each and passing the data pointer to this function. When
* func returns false iteration stops and the function returns.
*
* @param[in] accessor A Tvg_Accessor pointer to the accessor object.
* @param[in] paint A Tvg_Paint pointer to the scene object.
* @param[in] func A function pointer to the function that will be execute for each child.
* @param[in] data A void pointer to data that will be passed to the func.
*
* @return Tvg_Result enumeration.
* @retval TVG_RESULT_INVALID_ARGUMENT An invalid Tvg_Accessor, Tvg_Paint, or function pointer.
*
* @note Experimental API
*/
TVG_API Tvg_Result tvg_accessor_set_paint(Tvg_Accessor* accessor, Tvg_Paint* paint, bool (*func)(Tvg_Paint* paint, void* data), void* data);
/*!
* @brief Generate a unique ID (hash key) from a given name.

View file

@ -903,6 +903,28 @@ TVG_API Tvg_Result tvg_animation_del(Tvg_Animation* animation)
/* Accessor API */
/************************************************************************/
TVG_API Tvg_Accessor* tvg_accessor_new()
{
return (Tvg_Accessor*) Accessor::gen();
}
TVG_API Tvg_Result tvg_accessor_del(Tvg_Accessor* accessor)
{
if (!accessor) return TVG_RESULT_INVALID_ARGUMENT;
delete(reinterpret_cast<Accessor*>(accessor));
return TVG_RESULT_SUCCESS;
}
TVG_API Tvg_Result tvg_accessor_set(Tvg_Accessor* accessor, Tvg_Paint* paint, bool (*func)(Tvg_Paint* paint, void* data), void* data)
{
if (!accessor) return TVG_RESULT_INVALID_ARGUMENT;
return (Tvg_Result) reinterpret_cast<Accessor*>(accessor)->set(static_cast<Picture*>(reinterpret_cast<Paint*>(paint)),
[func](const Paint* paint, void* data) { return func((Tvg_Paint*) paint, data); }, data);
}
TVG_API uint32_t tvg_accessor_generate_id(const char* name)
{
return Accessor::id(name);