commit afd88118a1d6e1c014fc9906eb3eb1b484b8d238
parent e7da0f9bfb9bcbb8284552715c35a98050f924da
Author: bsandro <[email protected]>
Date: Wed, 23 Aug 2023 02:05:42 +0300
Actual image file loading/reloading/unloading via command line or gui button.
Diffstat:
M | gui/main.c | | | 97 | +++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ |
1 file changed, 60 insertions(+), 37 deletions(-)
diff --git a/gui/main.c b/gui/main.c
@@ -70,6 +70,12 @@ typedef struct {
static Animation *img = NULL;
static int cols = 1;
static volatile UIWindow *modal_win = NULL;
+static UIImageDisplay *img_disp = NULL;
+
+//@todo headers!
+Animation * ImageLoad(const char *);
+void ImageUnload(Animation **);
+void PreviewUpdate(Animation *, UIImageDisplay *);
void AnimationCreate(Animation *img, uint32_t frame_count) {
assert(frame_count > 0);
@@ -274,6 +280,21 @@ int ButtonDialogOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) {
printf("path_input: %s(%d)\nfilename_input: %s(%d)\n", path_input->string, strlen(path_input->string), filename_input->string, strlen(filename_input->string));
UIElementDestroy(element->window);
+
+ int path_len = path_input->bytes + filename_input->bytes + 2; // DIR_SEPARATOR and '\0'
+ char filepath[path_len];
+ filepath[path_len-1] = '\0';
+ int n = snprintf(filepath, path_len, "%s%c%s", path_input->string, DIR_SEPARATOR, filename_input->string);
+ assert(n>0);
+
+ if (img != NULL) {
+ ImageUnload(&img);
+ assert(img==NULL);
+ }
+ img = ImageLoad(filepath);
+ assert(img!=NULL);
+ PreviewUpdate(img, img_disp);
+
}
return 0;
}
@@ -364,7 +385,7 @@ int ButtonOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) {
return 0;
}
-void PreviewUpdate(UIImageDisplay *img_disp) {
+void PreviewUpdate(Animation *img, UIImageDisplay *img_disp) {
// gen new spritesheet and refresh the preview
Spritesheet ss = GenSpritesheet(img, cols);
//printf("spritesheet width: %d, height: %d, stride: %d, len: %zu\n", ss.width, ss.height, ss.stride, ss.len);
@@ -397,7 +418,7 @@ int SliderEvent(UIElement *element, UIMessage msg, int di, void *dp) {
if (new_cols > 0 && cols != new_cols) {
//printf("new_cols: %d\n", new_cols);
cols = new_cols;
- PreviewUpdate(element->cp);
+ PreviewUpdate(img, element->cp);
}
}
@@ -413,6 +434,38 @@ int WinMainEvent(UIElement *element, UIMessage msg, int di, void *dp) {
return 0;
}
+void ImageUnload(Animation **img) {
+ free((*img)->path);
+ free((*img)->dirname);
+ free((*img)->filename);
+ free(*img);
+ *img = NULL;
+}
+
+Animation * ImageLoad(const char *path) {
+ Animation *img = malloc(sizeof(Animation));
+ assert(img!=NULL);
+ bzero(img, sizeof(Animation));
+ img->path = realpath(path, NULL);
+ assert(img->path!=NULL);
+ printf("img->path: %s\n", img->path);
+ int r = WebpRead(img->path, img);
+ assert(r==0);
+
+ char *name_copy = strndup(img->path, PATH_MAX);
+ char *path_copy = strndup(img->path, PATH_MAX);
+ img->filename = strndup(basename(name_copy), NAME_MAX);
+ img->dirname = strndup(dirname(path_copy), PATH_MAX);
+ free(name_copy);
+ free(path_copy);
+
+ printf("[%s/%s]\ncols: %d\ndimensions: %dx%dpx\nframes: %d\n", img->dirname, img->filename, cols, img->width, img->height, img->frame_count);
+
+ //UILabelSetContent(lbl_title, img->path, -1);
+
+ return img;
+}
+
#ifdef UI_LINUX
int main(int argc, const char **argv) {
#else
@@ -439,46 +492,16 @@ int WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, i
UIButton *btn_exit = UIButtonCreate(&panelh->e, 0, "Close", -1);
btn_exit->e.messageUser = ButtonCloseEvent;
- UIImageDisplay *img_disp = UIImageDisplayCreate(&panelv->e, UI_ELEMENT_V_FILL|UI_ELEMENT_H_FILL|UI_IMAGE_DISPLAY_INTERACTIVE|_UI_IMAGE_DISPLAY_ZOOM_FIT, NULL, 0, 0, 0);
+ img_disp = UIImageDisplayCreate(&panelv->e, UI_ELEMENT_V_FILL|UI_ELEMENT_H_FILL|UI_IMAGE_DISPLAY_INTERACTIVE|_UI_IMAGE_DISPLAY_ZOOM_FIT, NULL, 0, 0, 0);
img_disp->e.cp = label;
slider->e.messageUser = SliderEvent;
slider->e.cp = img_disp;
- if (argc < 2) {
- printf("Usage: %s anim_file.webp\n", argc>0?argv[0]:"emote2ss_gui");
- return 0;
+ if (argc > 1) {
+ img = ImageLoad(argv[1]);
+ assert(img!=NULL);
+ PreviewUpdate(img, img_disp);
}
- img = malloc(sizeof(Animation));
- assert(img!=NULL);
- bzero(img, sizeof(Animation));
- img->path = realpath(argv[1], NULL);
- assert(img->path!=NULL);
- printf("img->path: %s\n", img->path);
- int r = WebpRead(img->path, img);
- assert(r==0);
-
- UILabelSetContent(lbl_title, img->path, -1);
-
- char *name_copy = strndup(img->path, PATH_MAX);
- char *path_copy = strndup(img->path, PATH_MAX);
- img->filename = strndup(basename(name_copy), NAME_MAX);
- img->dirname = strndup(dirname(path_copy), PATH_MAX);
- free(name_copy);
- free(path_copy);
-
- char out_name[strlen(img->filename)+7];
- int n = snprintf(out_name, NAME_MAX, "atlas_%s", img->filename);
- assert(n>0);
- printf("[%s/%s -> %s]\ncols: %d\ndimensions: %dx%dpx\nframes: %d\n", img->dirname, img->filename, out_name, cols, img->width, img->height, img->frame_count);
-
- PreviewUpdate(img_disp);
-
- //@todo cleanup on new image load!
- //free(img->path);
- //free(img->dirname);
- //free(img->filename);
- //free(img);
-
return UIMessageLoop();
}