commit b3ca1887a06544b3d4a0609dc6f91a64fa2d99bf
parent 72ad246486dbf31a66e024a23ea94141ab036ad2
Author: bsandro <email@bsandro.tech>
Date: Thu, 17 Aug 2023 01:04:31 +0300
Save dialog has cancel button now
Diffstat:
M | gui/main.c | | | 70 | ++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 30 insertions(+), 40 deletions(-)
diff --git a/gui/main.c b/gui/main.c
@@ -71,7 +71,7 @@ static Animation *img = NULL;
static int cols = 1;
static volatile UIWindow *modal_win = NULL;
-void alloc_image(Animation *img, uint32_t frame_count) {
+void AnimationCreate(Animation *img, uint32_t frame_count) {
assert(frame_count > 0);
uint8_t *mem = NULL;
Frame *frames = NULL;
@@ -99,7 +99,7 @@ void alloc_image(Animation *img, uint32_t frame_count) {
img->raw_mem = mem;
}
-int read_file(const char *filename, const uint8_t **data, size_t *size) {
+int ReadFile(const char *filename, const uint8_t **data, size_t *size) {
assert(data != NULL);
assert(size != NULL);
@@ -128,14 +128,14 @@ int read_file(const char *filename, const uint8_t **data, size_t *size) {
return 0;
}
-int read_webp(const char *filename, Animation *anim) {
- printf("read_webp(%s)\n", filename);
+int WebpRead(const char *filename, Animation *anim) {
+ printf("WebpRead(%s)\n", filename);
WebPData webp_data;
WebPDataInit(&webp_data);
- if (read_file(filename, &webp_data.bytes, &webp_data.size) == -1) {
- fprintf(stderr, "read_file error\n");
+ if (ReadFile(filename, &webp_data.bytes, &webp_data.size) == -1) {
+ fprintf(stderr, "ReadFile error\n");
return -1;
}
@@ -159,7 +159,7 @@ int read_webp(const char *filename, Animation *anim) {
anim->height = anim_info.canvas_height;
anim->loop_count = anim_info.loop_count;
anim->bgcolor = anim_info.bgcolor;
- alloc_image(anim, anim_info.frame_count);
+ AnimationCreate(anim, anim_info.frame_count);
uint32_t frame_index = 0;
int prev_ts = 0;
@@ -186,16 +186,7 @@ int read_webp(const char *filename, Animation *anim) {
return 0;
}
-void write_file(const char *filename, uint8_t *data, size_t len) {
- FILE *fp = fopen(filename, "wb");
- assert(fp!=NULL);
- size_t written = fwrite(data, sizeof(uint8_t), len, fp);
- assert(written==len);
- fclose(fp);
-}
-
-
-Spritesheet gen_spritesheet(Animation *img, int cols) {
+Spritesheet GenSpritesheet(Animation *img, int cols) {
int rows = (int)img->frame_count / cols;
if ((int)img->frame_count % cols > 0) {
++rows;
@@ -225,13 +216,11 @@ Spritesheet gen_spritesheet(Animation *img, int cols) {
ss.height = rows * img->height;
ss.len = ss.width * ss.height * sizeof(uint32_t);
- //free(merged_orig);
-
return ss;
}
-void write_webp(const char *path, Animation *img) {
- Spritesheet ss = gen_spritesheet(img, cols);
+void WebpWrite(const char *path, Animation *img) {
+ Spritesheet ss = GenSpritesheet(img, cols);
uint8_t *out;
FILE *fp = fopen(path, "wb");
assert(fp!=NULL);
@@ -265,8 +254,7 @@ int ButtonDialogSaveEvent(UIElement *element, UIMessage msg, int di, void *dp) {
int n = snprintf(out_name, PATH_MAX, "%s%c%s", path_input->string, DIR_SEPARATOR, filename_input->string);
printf("strlen(out_name): %d\n", strlen(out_name));
printf("out_name: %s\n", out_name);
- write_webp(out_name, img);
-
+ WebpWrite(out_name, img);
printf("save dialog window close\n");
assert(element->window==modal_win);
@@ -295,6 +283,13 @@ int TableEvent(UIElement *element, UIMessage msg, int di, void *dp) {
return 0;
}
+int ButtonCloseEvent(UIElement *element, UIMessage msg, int di, void *dp) {
+ if (msg == UI_MSG_CLICKED) {
+ UIElementDestroy(element->window);
+ }
+ return 0;
+}
+
void ShowSaveWindow(UIWindow *parent) {
if (modal_win == NULL) {
printf("create save window\n");
@@ -317,6 +312,9 @@ void ShowSaveWindow(UIWindow *parent) {
UIButton *btn_save = UIButtonCreate(&panel_top->e, UI_ELEMENT_TAB_STOP, "Save", -1);
btn_save->e.messageUser = ButtonDialogSaveEvent;
+ UIButton *btn_cancel = UIButtonCreate(&panel_top->e, UI_ELEMENT_TAB_STOP, "Cancel", -1);
+ btn_cancel->e.messageUser = ButtonCloseEvent;
+
UITable *table = UITableCreate(&panel_out->e, UI_ELEMENT_V_FILL, "Directory");
table->itemCount = 100;
table->e.messageUser = TableEvent;
@@ -328,27 +326,23 @@ void ShowSaveWindow(UIWindow *parent) {
int ButtonSaveEvent(UIElement *element, UIMessage msg, int di, void *dp) {
if (msg == UI_MSG_CLICKED) {
- printf("save dialog window open\n");
+ printf("save button clicked\n");
ShowSaveWindow(element->window);
}
return 0;
}
-int ButtonCloseEvent(UIElement *element, UIMessage msg, int di, void *dp) {
+int ButtonOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) {
if (msg == UI_MSG_CLICKED) {
- free(img);
- UIWindow *win_main = (UIWindow *)element->cp;
- assert(win_main!=NULL);
- UIElementDestroy(win_main);
- //exit(0);
+ printf("open button clicked\n");
+ //ShowLoadWindow(element->window);
}
-
return 0;
}
void update_preview(UIImageDisplay *img_disp) {
// gen new spritesheet and refresh the preview
- Spritesheet ss = gen_spritesheet(img, cols);
+ Spritesheet ss = GenSpritesheet(img, cols);
//printf("spritesheet width: %d, height: %d, stride: %d, len: %zu\n", ss.width, ss.height, ss.stride, ss.len);
uint32_t *frame0 = calloc(ss.width*ss.height, sizeof(uint32_t));
@@ -389,6 +383,7 @@ int SliderEvent(UIElement *element, UIMessage msg, int di, void *dp) {
int WinMainEvent(UIElement *element, UIMessage msg, int di, void *dp) {
if (msg == UI_MSG_DESTROY) {
printf("bye\n");
+ free(img);
exit(0);
}
return 0;
@@ -399,6 +394,7 @@ int main(int argc, const char **argv) {
#else
int WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, int showCommand) {
#endif
+
atexit(print_webp_version);
UIInitialise();
@@ -412,13 +408,12 @@ int WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, i
UILabel *label = UILabelCreate(&panelh->e, 0, "webp to spritesheet converter", -1);
UISlider *slider = UISliderCreate(&panelh->e, 0);
UIButton *btnopen = UIButtonCreate(&panelh->e, 0, "Open", -1);
- //btnopen->e.messageUser = ButtonOpenEvent;
+ btnopen->e.messageUser = ButtonOpenEvent;
UIButton *btnsave = UIButtonCreate(&panelh->e, 0, "Save", -1);
btnsave->e.messageUser = ButtonSaveEvent;
btnsave->e.cp = win;
UIButton *btn_exit = UIButtonCreate(&panelh->e, 0, "Close", -1);
btn_exit->e.messageUser = ButtonCloseEvent;
- btn_exit->e.cp = win;
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->e.cp = label;
@@ -436,7 +431,7 @@ int WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, i
img->path = realpath(argv[1], NULL);
assert(img->path!=NULL);
printf("img->path: %s\n", img->path);
- int r = read_webp(img->path, img);
+ int r = WebpRead(img->path, img);
assert(r==0);
UILabelSetContent(lbl_title, img->path, -1);
@@ -461,10 +456,5 @@ int WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, i
//free(img->filename);
//free(img);
- // temporary
return UIMessageLoop();
-
- //write_webp(out_name, img);
-
- return 0;
}