ui.c (13368B)
1 #define _DEFAULT_SOURCE 2 3 #include <dirent.h> 4 #include "fileops.h" 5 #include "animation.h" 6 #include "ui.h" 7 #include "spritesheet.h" 8 9 //@todo might be rubbish to keep important stuff in a pile of static globals 10 static int cols = 1; 11 static UIWindow *modal_win = NULL; 12 static UIImageDisplay *img_disp = NULL; 13 static UILabel *lbl_header = NULL; 14 static Animation *img = NULL; 15 static char *current_path = NULL; 16 17 static int FilelistFilter(const struct dirent *f) { 18 if (strncmp(f->d_name, ".", 255)==0) return 0; 19 if (f->d_type==DT_DIR) return 1; 20 //@todo not very efficient but dirent doesn't have the length :c 21 size_t len = strlen(f->d_name); 22 // checking for the ".webp" at the very end 23 if (len<6) return 0; 24 return strncmp(f->d_name+len-5, ".webp", 255)==0; 25 } 26 27 static int FilelistCompare(const struct dirent **a, const struct dirent **b) { 28 if ((*a)->d_type==DT_DIR && (*b)->d_type==DT_DIR) { 29 if (strncmp((*a)->d_name, "..", 255)==0) return -1; 30 else if (strncmp((*b)->d_name, "..", 255)==0) return 1; 31 else return alphasort(a, b); 32 } 33 if ((*a)->d_type==DT_DIR) return -1; 34 if ((*b)->d_type==DT_DIR) return 1; 35 return alphasort(a, b); 36 } 37 38 UIWindow * MainWindowCreate(const char *wname, int w, int h) { 39 //UIFont *font = UIFontActivate(UIFontCreate("../font.ttf", 19)); 40 //assert(font!=NULL); 41 UIWindow *win = UIWindowCreate(0, 0, wname, w, h); 42 win->e.messageUser = WinMainEvent; 43 UIPanel *panelv = UIPanelCreate(&win->e, UI_PANEL_GRAY|UI_PANEL_MEDIUM_SPACING); 44 // label with filename, buttons open/save/exit 45 UIPanel *panelh_top = UIPanelCreate(&panelv->e, UI_PANEL_GRAY|UI_PANEL_MEDIUM_SPACING|UI_PANEL_HORIZONTAL|UI_ELEMENT_H_FILL); 46 UIButton *btnopen = UIButtonCreate(&panelh_top->e, 0, "Open", -1); 47 btnopen->e.messageUser = ButtonOpenEvent; 48 UIButton *btnsave = UIButtonCreate(&panelh_top->e, 0, "Save", -1); 49 btnsave->e.messageUser = ButtonSaveEvent; 50 btnsave->e.cp = win; 51 UIButton *btn_exit = UIButtonCreate(&panelh_top->e, 0, "Exit", -1); 52 btn_exit->e.messageUser = ButtonCloseEvent; 53 lbl_header = UILabelCreate(&panelh_top->e, 0, "webp to spritesheet converter", -1); 54 55 // scroll with dimensions label 56 UIPanel *panelh_bottom = UIPanelCreate(&panelv->e, UI_PANEL_GRAY|UI_PANEL_SMALL_SPACING|UI_PANEL_HORIZONTAL|UI_PANEL_EXPAND|UI_ELEMENT_H_FILL); 57 UILabel *label = UILabelCreate(&panelh_bottom->e, 0, "Size: 0x0px", -1); 58 UIButton *btnminus = UIButtonCreate(&panelh_bottom->e, 0, "-", -1); 59 UIButton *btnplus = UIButtonCreate(&panelh_bottom->e, 0, "+", -1); 60 UISlider *slider = UISliderCreate(&panelh_bottom->e, UI_ELEMENT_H_FILL); 61 62 btnminus->e.messageUser = ButtonMinusEvent; 63 btnminus->e.cp = slider; 64 btnplus->e.messageUser = ButtonPlusEvent; 65 btnplus->e.cp = slider; 66 67 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); 68 img_disp->e.cp = label; 69 slider->e.messageUser = SliderEvent; 70 slider->e.cp = img_disp; 71 return win; 72 } 73 74 int WinModalEvent(UIElement *element, UIMessage msg, int di, void *dp) { 75 if (msg==UI_MSG_DESTROY) { 76 //printf("modal bye\n"); 77 assert(element==(UIElement *)modal_win); 78 modal_win = NULL; 79 } 80 return 0; 81 } 82 83 int ButtonMinusEvent(UIElement *element, UIMessage msg, int di, void *dp) { 84 if (msg==UI_MSG_CLICKED) { 85 if (img==NULL) return 1; 86 UISlider *slider = (UISlider *)element->cp; 87 float step = 1.0f / (float)img->frame_count; 88 slider->position -= step; 89 cols--; 90 if (slider->position<0) slider->position=0; 91 if (cols<1) cols=1; 92 UIElementRefresh(&slider->e); 93 PreviewUpdate(img, img_disp); 94 } 95 return 0; 96 } 97 int ButtonPlusEvent(UIElement *element, UIMessage msg, int di, void *dp) { 98 if (msg==UI_MSG_CLICKED) { 99 if (img==NULL) return 1; 100 UISlider *slider = (UISlider *)element->cp; 101 float step = 1.0f / (float)img->frame_count; 102 slider->position += step; 103 cols++; 104 if (slider->position>1) slider->position=1; 105 if (cols>img->frame_count) cols=img->frame_count; 106 UIElementRefresh(&slider->e); 107 PreviewUpdate(img, img_disp); 108 } 109 return 0; 110 } 111 112 int ButtonDialogSaveEvent(UIElement *element, UIMessage msg, int di, void *dp) { 113 if (msg==UI_MSG_CLICKED) { 114 // get values of path and filename inputs 115 UITextbox *path_input = (UITextbox *)element->parent->children; 116 UITextbox *filename_input = (UITextbox *)path_input->e.next; 117 // printf("path_input: %p\nfilename_input: %p\n", path_input, filename_input); 118 // that printf might work incorrectly because path_input->string might not contain valid C string with \0 at the end 119 // printf("path_input: %s(%d)\nfilename_input: %s(%d)\n", path_input->string, path_input->bytes, filename_input->string, filename_input->bytes); 120 int path_len = path_input->bytes + filename_input->bytes + 2; // DIR_SEPARATOR and '\0' 121 // printf("path_len: %d\n", path_len); 122 char out_name[path_len]; 123 out_name[path_input->bytes] = DIR_SEPARATOR; 124 out_name[path_len-1] = '\0'; 125 memcpy(out_name, path_input->string, path_input->bytes); 126 memcpy(out_name+path_input->bytes+1, filename_input->string, filename_input->bytes); 127 // printf("strlen(out_name): %d\n", strlen(out_name)); 128 // printf("out_name: %s\n", out_name); 129 WebpWrite(out_name, img, cols); 130 // printf("save dialog window close\n"); 131 assert(element->window==modal_win); 132 UIElementDestroy(&element->window->e); 133 } 134 return 0; 135 } 136 137 int ButtonDialogOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) { 138 if (msg==UI_MSG_CLICKED) { 139 // printf("open dialog window close\n"); 140 UITextbox *path_input = (UITextbox *)element->parent->children; 141 UITextbox *filename_input = (UITextbox *)path_input->e.next; 142 // printf("path_input: %p\nfilename_input: %p\n", path_input, filename_input); 143 // printf("path_input: %s(%d)\nfilename_input: %s(%d)\n", path_input->string, strlen(path_input->string), filename_input->string, strlen(filename_input->string)); 144 145 UIElementDestroy(&element->window->e); 146 147 int path_len = path_input->bytes + filename_input->bytes + 2; // DIR_SEPARATOR and '\0' 148 char filepath[path_len]; 149 filepath[path_input->bytes] = DIR_SEPARATOR; 150 filepath[path_len-1] = '\0'; 151 memcpy(filepath, path_input->string, path_input->bytes); 152 memcpy(filepath+path_input->bytes+1, filename_input->string, filename_input->bytes); 153 154 if (img != NULL) { 155 ImageUnload(&img); 156 assert(img==NULL); 157 } 158 img = ImageLoad(filepath); 159 assert(img!=NULL); 160 UILabelSetContent(lbl_header, img->path, -1); 161 PreviewUpdate(img, img_disp); 162 } 163 return 0; 164 } 165 166 int TableEvent(UIElement *element, UIMessage msg, int di, void *dp) { 167 static int selected = -1; 168 static struct dirent **filelist = NULL; 169 UITable *table = (UITable *)element; 170 if (filelist==NULL) { 171 table->itemCount = scandir(element->cp, &filelist, FilelistFilter, FilelistCompare); 172 // printf("populated dir %s with %d items\n", dir_name, table->itemCount); 173 } 174 if (msg==UI_MSG_TABLE_GET_ITEM) { 175 UITableGetItem *m = (UITableGetItem *)dp; 176 m->isSelected = selected==m->index; 177 bool is_dir = filelist[m->index]->d_type==DT_DIR; 178 //printf("render %s (%d)\n", filelist[m->index]->d_name, m->bufferBytes); 179 int ret = snprintf(m->buffer, m->bufferBytes, is_dir?"[%s]":"%s", filelist[m->index]->d_name); 180 //printf("%s - %d (%d)\n", filelist[m->index]->d_name, ret, m->bufferBytes); 181 return ret; 182 } else if (msg==UI_MSG_CLICKED) { 183 int hit = UITableHitTest(table, element->window->cursorX, element->window->cursorY); 184 if (hit!=-1 && selected==hit) { 185 if (filelist[hit]->d_type==DT_DIR) { 186 char newpath[PATH_MAX]; 187 bzero(newpath, PATH_MAX); 188 strcpy(newpath, element->cp); 189 strcat(newpath, "/"); 190 strcat(newpath, filelist[hit]->d_name); 191 free(element->cp); 192 element->cp = realpath(newpath, NULL); 193 current_path = element->cp; 194 //free(filelist); 195 filelist = NULL; 196 selected = -1; 197 //@todo duplicated code 198 UIPanel *panel_out = (UIPanel *)element->parent; 199 UILabel *label = (UILabel *)panel_out->e.children; 200 UIPanel *panel_top = (UIPanel *)label->e.next; 201 UITextbox *path_input = (UITextbox *)panel_top->e.children; 202 UITextboxClear(path_input, false); 203 UITextboxReplace(path_input, (char *)element->cp, -1, false); 204 UIElementRepaint(&path_input->e, NULL); 205 UITableEnsureVisible(table, -1); 206 } 207 } else { 208 selected = hit; 209 if (!UITableEnsureVisible(table, selected)) { 210 UIElementRepaint(element, NULL); 211 } 212 //@todo remove copypasta 213 if (hit!=-1 && filelist[hit]->d_type!=DT_DIR) { 214 UIPanel *panel_out = (UIPanel *)element->parent; 215 UILabel *label = (UILabel *)panel_out->e.children; 216 UIPanel *panel_top = (UIPanel *)label->e.next; 217 UITextbox *path_input = (UITextbox *)panel_top->e.children; 218 UITextbox *file_input = (UITextbox *)path_input->e.next; 219 UITextboxClear(path_input, false); 220 UITextboxClear(file_input, false); 221 UITextboxReplace(path_input, (char *)element->cp, -1, false); 222 UITextboxReplace(file_input, filelist[hit]->d_name, -1, false); 223 UIElementRepaint(&path_input->e, NULL); 224 UIElementRepaint(&file_input->e, NULL); 225 } 226 } 227 } else if (msg==UI_MSG_DESTROY) { 228 free(filelist); 229 filelist = NULL; 230 selected = -1; 231 UIElement *el = table->e.children; 232 printf("child %p\n", el); 233 while (el!=NULL) { 234 printf("child %p\n", el); 235 } 236 } else if (msg==UI_MSG_UPDATE) { 237 UITableResizeColumns(table); 238 } 239 return 0; 240 } 241 242 int ButtonCloseEvent(UIElement *element, UIMessage msg, int di, void *dp) { 243 if (msg==UI_MSG_CLICKED) { 244 UIElementDestroy(&element->window->e); 245 } 246 return 0; 247 } 248 249 void ShowModalWindow(UIWindow *parent, char *def_dir, const char *def_file, CallbackFn cb) { 250 assert(def_dir!=NULL); 251 if (modal_win==NULL) { 252 // printf("create modal window\n"); 253 int w = UIElementMessage(&parent->e, UI_MSG_GET_WIDTH, 0, 0); 254 int h = UIElementMessage(&parent->e, UI_MSG_GET_HEIGHT, 0, 0); 255 modal_win = UIWindowCreate(parent, 0, def_file?"Save File":"Open File", w, h); 256 modal_win->e.messageUser = WinModalEvent; 257 UIPanel *panel_out = UIPanelCreate(&modal_win->e, UI_PANEL_GRAY|UI_PANEL_EXPAND); 258 UILabel *lbl_title = UILabelCreate(&panel_out->e, 0, def_file?"Save File":"Open File", -1); 259 UIPanel *panel_top = UIPanelCreate(&panel_out->e, UI_PANEL_GRAY|UI_PANEL_MEDIUM_SPACING|UI_PANEL_HORIZONTAL); 260 UITextbox *path_input = UITextboxCreate(&panel_top->e, UI_ELEMENT_DISABLED|UI_ELEMENT_H_FILL); 261 UITextboxReplace(path_input, def_dir, -1, false); 262 UITextbox *filename_input = UITextboxCreate(&panel_top->e, UI_ELEMENT_TAB_STOP|UI_ELEMENT_H_FILL); 263 264 printf("path_input: %p (%s)\nfilename_input: %p (%s)\n", path_input, def_dir, filename_input, def_file); 265 if (def_file!=NULL) { 266 UITextboxReplace(filename_input, def_file, -1, false); 267 } 268 269 UIButton *btn_save = UIButtonCreate(&panel_top->e, UI_ELEMENT_TAB_STOP, def_file?"Save":"Open", -1); 270 btn_save->e.messageUser = cb; 271 272 UIButton *btn_cancel = UIButtonCreate(&panel_top->e, UI_ELEMENT_TAB_STOP, "Cancel", -1); 273 btn_cancel->e.messageUser = ButtonCloseEvent; 274 275 UITable *table = UITableCreate(&panel_out->e, UI_ELEMENT_V_FILL, "Directory"); 276 table->itemCount = 1; // at least '..' element 277 table->e.cp = def_dir; 278 table->e.messageUser = TableEvent; 279 UITableResizeColumns(table); 280 } else { 281 UIElementFocus(&modal_win->e); 282 } 283 } 284 285 int ButtonSaveEvent(UIElement *element, UIMessage msg, int di, void *dp) { 286 if (msg==UI_MSG_CLICKED && img!=NULL) { 287 // printf("save button clicked\n"); 288 char fname[strlen(img->filename)+7]; 289 int n = snprintf(fname, NAME_MAX, "atlas_%s", img->filename); 290 assert(n>0); 291 ShowModalWindow(element->window, img->dirname, fname, ButtonDialogSaveEvent); 292 } 293 return 0; 294 } 295 296 int ButtonOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) { 297 if (msg==UI_MSG_CLICKED) { 298 char *dir = current_path==NULL ? strndup(getenv("HOME"), strlen(getenv("HOME"))) : current_path; 299 // printf("open button clicked\n"); 300 ShowModalWindow(element->window, dir, NULL, ButtonDialogOpenEvent); 301 } 302 return 0; 303 } 304 305 int SliderEvent(UIElement *element, UIMessage msg, int di, void *dp) { 306 if (msg==UI_MSG_VALUE_CHANGED) { 307 if (img==NULL) return 1; 308 309 float slider_pos = ((UISlider *)element)->position; 310 float step = 1.0f / (float)img->frame_count; 311 int new_cols = (int)(slider_pos / step); 312 if (new_cols>0 && cols!=new_cols) { 313 //printf("new_cols: %d\n", new_cols); 314 cols = new_cols; 315 PreviewUpdate(img, element->cp); 316 } 317 } 318 319 return 0; 320 } 321 322 int WinMainEvent(UIElement *element, UIMessage msg, int di, void *dp) { 323 if (msg==UI_MSG_DESTROY) { 324 //printf("main win bye\n"); 325 free(img); 326 exit(0); 327 } 328 return 0; 329 } 330 331 void PreviewUpdate(Animation *img, UIImageDisplay *img_disp) { 332 // gen new spritesheet and refresh the preview 333 Spritesheet ss = SpritesheetGen(img, cols); 334 //printf("spritesheet width: %d, height: %d, stride: %d, len: %zu\n", ss.width, ss.height, ss.stride, ss.len); 335 336 uint32_t *frame0 = calloc(ss.width*ss.height, sizeof(uint32_t)); 337 assert(frame0!=NULL); 338 339 for (uint32_t i=0; i<ss.width*ss.height; ++i) { 340 frame0[i] = UI_COLOR_FROM_RGBA(ss.data[i*4+0], ss.data[i*4+1], ss.data[i*4+2], ss.data[i*4+3]); 341 } 342 UIImageDisplaySetContent(img_disp, frame0, ss.width, ss.height, ss.stride); 343 UIElementRefresh(img_disp->e.parent); 344 UIElementRefresh(&img_disp->e); 345 346 free(frame0); 347 free(ss.data); 348 349 UILabel *label = (UILabel *)img_disp->e.cp; 350 char label_text[256] = {0}; 351 snprintf((char * restrict)&label_text, 255, "Size: %4dx%4dpx", ss.width, ss.height); 352 UILabelSetContent(label, label_text, -1); 353 UIElementRefresh(label->e.parent); 354 }