commit d8639a3bbcfff52999055fe529cc95b690657763
parent c5af9d18a76c2ee352daabd0f315ba65f6892977
Author: bsandro <[email protected]>
Date: Sat, 7 Sep 2024 11:20:42 +0300
- Working save/load modal window
- Path input field is disabled to type in
- Filelist is being filtered for only directories and .webp files
Diffstat:
M | gui/main.c | | | 120 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------- |
1 file changed, 88 insertions(+), 32 deletions(-)
diff --git a/gui/main.c b/gui/main.c
@@ -20,6 +20,7 @@
#include <strings.h>
#include <assert.h>
#include <dirent.h>
+#include <sys/prctl.h>
#include "luigi.h"
#include "animation.h"
@@ -179,7 +180,7 @@ int ButtonDialogSaveEvent(UIElement *element, UIMessage msg, int di, void *dp) {
UIElementDestroy(element->window);
}
return 0;
-}
+ }
int ButtonDialogOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) {
if (msg == UI_MSG_CLICKED) {
@@ -212,59 +213,113 @@ int ButtonDialogOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) {
/*
@todo
-- display list of files and directories in open directory
-- click on directory 'opens' it
-- OR first click on directory highlights it, second - opens it
-- open directory full path is being copied into the path field above
-- list has .. element to move up in the hierarchy
-- click on filename highlights it and copies the name into file name field above
+- display/update resulting spritesheet dimensions in pixels
++ display list of files and directories in open directory
++ first click on directory highlights it, second - opens it
++ open directory full path is being copied into the path field above
++ list has .. element to move up in the hierarchy
++ click on filename highlights it and copies the name into file name field above
- unify path and filename fields (more canonical way)
+- `-> copy the full path+name into the unified field
- use system open/save dialog on Windows
++ sort directories before files in list
++ do not display "." item
++ filter files to only show those with .webp extension
+- (bug) displayed file name is truncated
+- move the whole subroutine into a separate file
+- (bug) reset scroll on directory change
+- sort filenames alphabetically
++ (bug) path and name are being concatenated during copying
*/
+
+static int FilelistFilter(const struct dirent *f) {
+ if (strncmp(f->d_name, ".", 255)==0) return 0;
+ if (f->d_type==DT_DIR) return 1;
+ //@todo not very optimal
+ size_t len = strlen(f->d_name);
+ // checking for the ".webp" at the very end
+ printf("name: %s, len: %u, extension: %s\n", f->d_name, len, f->d_name+len-5);
+ if (len<6) return 0;
+ return strncmp(f->d_name+len-5, ".webp", 255)==0;
+}
+
+static int FilelistCompare(const struct dirent **a, const struct dirent **b) {
+ if ((*a)->d_type==DT_DIR && (*b)->d_type==DT_DIR) {
+ if (strncmp((*a)->d_name, "..", 255)==0) return -1;
+ if (strncmp((*b)->d_name, "..", 255)==0) return 1;
+ }
+ if ((*a)->d_type==DT_DIR) return -1;
+ if ((*b)->d_type==DT_DIR) return 1;
+ return 0;
+}
+
int TableEvent(UIElement *element, UIMessage msg, int di, void *dp) {
static int selected = -1;
static struct dirent **filelist = NULL;
char *dir_name = element->cp;
UITable *table = (UITable *)element;
if (filelist==NULL) {
- //@todo sort directories before files in list
- table->itemCount = scandir(dir_name, &filelist, NULL, alphasort);
+ table->itemCount = scandir(dir_name, &filelist, FilelistFilter, FilelistCompare);
printf("populated dir %s with %d items\n", dir_name, table->itemCount);
}
if (msg==UI_MSG_TABLE_GET_ITEM) {
UITableGetItem *m = (UITableGetItem *)dp;
m->isSelected = selected==m->index;
bool is_dir = filelist[m->index]->d_type==DT_DIR;
+ //printf("render %s (%d)\n", filelist[m->index]->d_name, m->bufferBytes);
return snprintf(m->buffer, m->bufferBytes, is_dir?"[%s]":"%s", filelist[m->index]->d_name);
} else if (msg==UI_MSG_CLICKED) {
int hit = UITableHitTest(table, element->window->cursorX, element->window->cursorY);
- if (selected==hit) {
- if (hit!=-1) {
- printf("second click on selected item %d (%s)\n", hit, filelist[hit]->d_name);
- if (filelist[hit]->d_type==DT_DIR) {
- char newpath[PATH_MAX];
- bzero(newpath, PATH_MAX);
- strcpy(newpath, element->cp);
- strcat(newpath, "/");
- strcat(newpath, filelist[hit]->d_name);
- //element->cp = realpath(filelist[hit]->d_name, NULL);
- element->cp = realpath(newpath, NULL);
- printf("new dir: %s\n", element->cp);
- //@todo cleanup filelist!
- filelist = NULL;
- selected = -1;
- }
+ if (hit!=-1 && selected==hit) {
+ if (filelist[hit]->d_type==DT_DIR) {
+ char newpath[PATH_MAX];
+ bzero(newpath, PATH_MAX);
+ strcpy(newpath, element->cp);
+ strcat(newpath, "/");
+ strcat(newpath, filelist[hit]->d_name);
+ //element->cp = realpath(filelist[hit]->d_name, NULL);
+ //@todo memory leak
+ element->cp = realpath(newpath, NULL);
+ printf("new dir: %s\n", element->cp);
+ //@todo cleanup filelist!
+ filelist = NULL;
+ selected = -1;
+ //@todo duplicated code
+ UIPanel *panel_out = (UIPanel *)element->parent;
+ UILabel *label = (UILabel *)panel_out->e.children;
+ UIPanel *panel_top = (UIPanel *)label->e.next;
+ UITextbox *path_input = (UITextbox *)panel_top->e.children;
+ UITextboxClear(path_input, false);
+ UITextboxReplace(path_input, (char *)element->cp, -1, false);
+ UIElementRepaint(path_input, NULL);
}
} else {
selected = hit;
if (!UITableEnsureVisible(table, selected)) {
UIElementRepaint(element, NULL);
}
+ //@todo remove copypasta
+ if (hit!=-1 && filelist[hit]->d_type!=DT_DIR) {
+ UIPanel *panel_out = (UIPanel *)element->parent;
+ UILabel *label = (UILabel *)panel_out->e.children;
+ UIPanel *panel_top = (UIPanel *)label->e.next;
+ UITextbox *path_input = (UITextbox *)panel_top->e.children;
+ UITextbox *file_input = (UITextbox *)path_input->e.next;
+ // string, bytes, UITextboxReplace
+ printf("path: %s (%d bytes)\nfile: %s(%d)\n", path_input->string, path_input->bytes, file_input->string, file_input->bytes);
+ printf("old path: %s\n", (char *)element->cp);
+ UITextboxClear(path_input, false);
+ UITextboxClear(file_input, false);
+ UITextboxReplace(path_input, (char *)element->cp, -1, false);
+ UITextboxReplace(file_input, filelist[hit]->d_name, -1, false);
+ UIElementRepaint(path_input, NULL);
+ UIElementRepaint(file_input, NULL);
+ }
}
} else if (msg==UI_MSG_DESTROY) {
- printf("destroy table\n");
- filelist = NULL;
- selected = -1;
+ printf("destroy table\n");
+ filelist = NULL;
+ selected = -1;
}
return 0;
}
@@ -284,12 +339,9 @@ void ShowModalWindow(UIWindow *parent, const char *def_dir, const char *def_file
modal_win = UIWindowCreate(parent, NULL, def_file?"Save File":"Open File", WIN_WIDTH, WIN_HEIGHT);
modal_win->e.messageUser = WinModalEvent;
UIPanel *panel_out = UIPanelCreate(&modal_win->e, UI_PANEL_GRAY|UI_PANEL_EXPAND);
-
UILabel *lbl_title = UILabelCreate(&panel_out->e, 0, def_file?"Save File":"Open File", -1);
-
UIPanel *panel_top = UIPanelCreate(&panel_out->e, UI_PANEL_GRAY|UI_PANEL_MEDIUM_SPACING|UI_PANEL_HORIZONTAL);
-
- UITextbox *path_input = UITextboxCreate(&panel_top->e, UI_ELEMENT_TAB_STOP|UI_ELEMENT_H_FILL);
+ UITextbox *path_input = UITextboxCreate(&panel_top->e, UI_ELEMENT_DISABLED|UI_ELEMENT_H_FILL);
if (def_dir != NULL) {
UITextboxReplace(path_input, def_dir, -1, false);
}
@@ -393,6 +445,10 @@ int main(int argc, const char **argv) {
int WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, int showCommand) {
#endif
+ // just some silly stuff, always wanted to try that
+ prctl(PR_SET_NAME, "webp-anim-2-spritesheet");
+ printf("argv[0]: %s\n", argv[0]);
+
atexit(print_webp_version);
UIInitialise();