Skip to content

Commit

Permalink
feat(ui): improve the event and loading logic of image loader
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Feb 10, 2024
1 parent 96ef3c0 commit 6a7578b
Show file tree
Hide file tree
Showing 4 changed files with 653 additions and 432 deletions.
95 changes: 80 additions & 15 deletions lib/ui/include/ui/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,36 @@
#include "common.h"
#include "types.h"

#define ui_image_on_event ui_image_add_event_listener
#define ui_image_off_event ui_image_remove_event_listener

typedef struct ui_image_t ui_image_t;
typedef void (*ui_image_event_handler_t)(ui_image_t *, void *);
#define ui_image_on ui_image_add_event_listener
#define ui_image_off ui_image_remove_event_listener

typedef enum ui_image_state_t {
UI_IMAGE_STATE_PENDING,
UI_IMAGE_STATE_LOADING,
UI_IMAGE_STATE_COMPLETE
} ui_image_state_t;

typedef enum ui_image_event_type_t {
UI_IMAGE_EVENT_LOAD,
UI_IMAGE_EVENT_PROGRESS,
UI_IMAGE_EVENT_ERROR,
} ui_image_event_type_t;

typedef struct ui_image_t {
pd_canvas_t data;
pd_error_t error;
ui_image_state_t state;
char *path;
float progress;
} ui_image_t;

typedef struct ui_image_event_t {
ui_image_event_type_t type;
ui_image_t *image;
void *data;
} ui_image_event_t;

typedef void (*ui_image_event_handler_t)(ui_image_event_t *);

LIBUI_BEGIN_DECLS

Expand All @@ -31,21 +56,59 @@ LIBUI_PUBLIC ui_image_t *ui_get_image(const char *path);
**/
LIBUI_PUBLIC ui_image_t *ui_image_create(const char *path);

/**
* 获取已加载的图像数据
*/
LIBUI_PUBLIC pd_canvas_t *ui_image_get_data(ui_image_t *image);

LIBUI_PUBLIC const char *ui_image_get_path(ui_image_t *image);

LIBUI_PUBLIC bool ui_image_is_loaded(ui_image_t *image);

LIBUI_PUBLIC void ui_image_destroy(ui_image_t *image);
LIBUI_PUBLIC int ui_image_add_event_listener(ui_image_t *image,
ui_image_event_type_t type,
ui_image_event_handler_t handler,
void *data);
LIBUI_PUBLIC int ui_image_remove_event_listener(
ui_image_t *image, ui_image_event_handler_t handler, void *data);
ui_image_t *image, ui_image_event_type_t type,
ui_image_event_handler_t handler, void *data);

LIBUI_INLINE int ui_image_on_load(ui_image_t *image,
ui_image_event_handler_t handler, void *data)
{
return ui_image_add_event_listener(image, UI_IMAGE_EVENT_LOAD, handler,
data);
}

LIBUI_INLINE int ui_image_on_error(ui_image_t *image,
ui_image_event_handler_t handler, void *data)
{
return ui_image_add_event_listener(image, UI_IMAGE_EVENT_ERROR, handler,
data);
}

LIBUI_INLINE int ui_image_on_progress(ui_image_t *image,
ui_image_event_handler_t handler,
void *data)
{
return ui_image_add_event_listener(image, UI_IMAGE_EVENT_PROGRESS,
handler, data);
}

LIBUI_INLINE int ui_image_off_load(ui_image_t *image,
ui_image_event_handler_t handler, void *data)
{
return ui_image_remove_event_listener(image, UI_IMAGE_EVENT_LOAD,
handler, data);
}

LIBUI_INLINE int ui_image_off_error(ui_image_t *image,
ui_image_event_handler_t handler,
void *data)
{
return ui_image_remove_event_listener(image, UI_IMAGE_EVENT_ERROR,
handler, data);
}

LIBUI_INLINE int ui_image_off_progress(ui_image_t *image,
ui_image_event_handler_t handler,
void *data)
{
return ui_image_remove_event_listener(image, UI_IMAGE_EVENT_PROGRESS,
handler, data);
}

/**
* 为所有 UIImage 对象加载数据
Expand All @@ -65,6 +128,8 @@ LIBUI_PUBLIC void ui_process_image_events(void);
*/
LIBUI_PUBLIC void ui_clear_images(void);

LIBUI_PUBLIC void ui_set_image_loader_callback(void (*callback)(ui_image_t *));

LIBUI_END_DECLS

#endif
Loading

0 comments on commit 6a7578b

Please sign in to comment.