๐Ÿ“ฆ RobLoach / nuklear_console

๐Ÿ“„ nuklear_console_file.h ยท 488 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488#ifndef NK_CONSOLE_FILE_H__
#define NK_CONSOLE_FILE_H__

#ifndef NK_CONSOLE_FILE_PATH_MAX
#ifdef PATH_MAX
#define NK_CONSOLE_FILE_PATH_MAX PATH_MAX
#else
#define NK_CONSOLE_FILE_PATH_MAX 4096
#endif
#endif // NK_CONSOLE_FILE_PATH_MAX

/**
 * Custom data for the file widget.
 */
typedef struct nk_console_file_data {
    nk_console_button_data button; /** Inherited from Button */
    char* file_path_buffer; /** The string buffer for the chosen file path. */
    int file_path_buffer_size; /** The size of the buffer. */
    char directory[NK_CONSOLE_FILE_PATH_MAX]; /** When selecting a file, this is the current directory. */
    void* file_user_data; /** Custom user data for the file system. */
    nk_bool select_directory; /** Flag indicating if we are selecting a directory. */
} nk_console_file_data;

#if defined(__cplusplus)
extern "C" {
#endif

/**
 * Creates a file widget that allows the user to select a file.
 *
 * @param parent The parent widget.
 * @param label The label for the file widget. For example: "Select a file".
 * @param file_path_buffer The buffer to store the file path.
 * @param file_path_buffer_size The size of the buffer.
 *
 * @return The new file widget.
 */
NK_API nk_console* nk_console_file(nk_console* parent, const char* label, char* file_path_buffer, int file_path_buffer_size);

/**
 * Creates a directory widget that allows the user to select a directory.
 *
 * @param parent The parent widget.
 * @param label The label for the file widget. For example: "Select a file".
 * @param dir_buffer The buffer to store the file path.
 * @param dir_buffer_size The size of the buffer.
 *
 * @return The new file widget.
 */
NK_API nk_console* nk_console_dir(nk_console* parent, const char* label, char* dir_buffer, int dir_buffer_size);

/**
 * Render callback to display the file widget.
 */
NK_API struct nk_rect nk_console_file_render(nk_console* widget);

/**
 * Sets any custom user data specifically for the file widget.
 *
 * This can be helpful for storing additional file system data for the file widget.
 *
 * @param file The file widget.
 * @param user_data The custom user data.
 */
NK_API void nk_console_file_set_file_user_data(nk_console* file, void* user_data);

/**
 * Gets the custom user data specifically for the file widget.
 *
 * @param file The file widget.
 *
 * @return The custom user data.
 */
NK_API void* nk_console_file_get_file_user_data(nk_console* file);

/**
 * Add a individual file or directory to the given file widget as a child.
 *
 * This should be called from the file system callbacks. See `nuklear_console_file_system.h` for examples.
 *
 * @param parent The file widget.
 * @param path The path to the file or directory.
 * @param is_directory True if the path is a directory. False otherwise.
 *
 * @return The new button if it was successfully added, NULL otherwise.
 *
 * @see nk_console_file_destroy_tinydir()
 * @see nk_console_file_add_files_raylib()
 */
NK_API nk_console* nk_console_file_add_entry(nk_console* parent, const char* path, nk_bool is_directory);

/**
 * Refreshes the file widget to display the contents of the current directory.
 *
 * @param widget The file widget to refresh.
 *
 * @see nk_console_file_data::directory
 */
NK_API void nk_console_file_refresh(nk_console* widget, void* user_data);

#if defined(__cplusplus)
}
#endif

#endif // NK_CONSOLE_FILE_H__

#if defined(NK_CONSOLE_IMPLEMENTATION) && !defined(NK_CONSOLE_HEADER_ONLY)
#ifndef NK_CONSOLE_FILE_IMPLEMENTATION_ONCE
#define NK_CONSOLE_FILE_IMPLEMENTATION_ONCE

#include "nuklear_console_file_system.h"

#if defined(__cplusplus)
extern "C" {
#endif

/**
 * Gets the base name of a file path.
 */
static const char* nk_console_file_basename(const char* path) {
    if (path == NULL) {
        return NULL;
    }

    // TODO: Ensure UTF-8 compatibility.
    int len = nk_strlen(path);
    for (int i = len - 1; i > 0; i--) {
        if (path[i] == '\\' || path[i] == '/') {
            path = path + i + 1;
            break;
        }
    }

    return path;
}

NK_API struct nk_rect nk_console_file_render(nk_console* console) {
    if (console == NULL || console->data == NULL) {
        return nk_rect(0, 0, 0, 0);
    }
    nk_console_file_data* data = (nk_console_file_data*)console->data;

    nk_console_layout_widget(console);

    // Display the label
    if (console->label != NULL && console->label[0] != '\0') {
        if (!nk_console_is_active_widget(console)) {
            nk_widget_disable_begin(console->ctx);
        }
        if (console->label_length > 0) {
            nk_text(console->ctx, console->label, console->label_length, NK_TEXT_LEFT);
        }
        else {
            nk_label(console->ctx, console->label, NK_TEXT_LEFT);
        }
        if (!nk_console_is_active_widget(console)) {
            nk_widget_disable_end(console->ctx);
        }
    }

    // Display the mocked button
    int swap_columns = console->columns;
    const char* swap_label = console->label;
    console->columns = 0;
    if (data->file_path_buffer != NULL && data->file_path_buffer[0] != '\0') {
        console->label = nk_console_file_basename(data->file_path_buffer);
    }
    else {
        console->label = data->select_directory ? "[Select Directory]" : "[Select a File]";
    }
    struct nk_rect widget_bounds = nk_console_button_render(console);
    console->columns = swap_columns;
    console->label = swap_label;

    return widget_bounds;
}

/**
 * Gets the file widget from a child button.
 */
static nk_console* nk_console_file_button_get_file_widget(nk_console* button) {
    if (button == NULL) {
        return NULL;
    }
    if (button->type == NK_CONSOLE_FILE) {
        return button;
    }
    while (button->parent != NULL) {
        button = button->parent;
        if (button->type == NK_CONSOLE_FILE) {
            return button;
        }
    }

    return NULL;
}

/**
 * Free the individual file entry buttons. This clears the label.
 */
NK_API void nk_console_file_free_entry(nk_console* button, void* user_data) {
    NK_UNUSED(user_data);
    if (button == NULL) {
        return;
    }

    if (button->label != NULL) {
        nk_console_mfree(nk_handle_id(0), (void*)button->label);
        button->label = NULL;
    }
}

NK_API void nk_console_file_entry_onclick(nk_console* button, void* user_data) {
    NK_UNUSED(user_data);
    if (button == NULL || button->label == NULL) {
        return;
    }

    nk_console* file = nk_console_file_button_get_file_widget(button);
    if (file == NULL || file->data == NULL) {
        return;
    }

    nk_console_file_data* data = (nk_console_file_data*)file->data;
    int len = nk_strlen(data->directory);

    // Append a slash if the directory is not empty.
    if (len == 1 && data->directory[0] == '.') {
        len = 0;
        data->directory[0] = '\0';
    }
    else if (len > 0) {
// TODO: file: Make sure this is cross-platform.
#if defined(_WIN32) || defined(WIN32)
        data->directory[len] = '\\';
#else
        data->directory[len] = '/';
#endif
        data->directory[len + 1] = '\0';
        len++;
    }

    // Concatenate the button label to the directory.
    // TODO: file: Resolve the path properly, so the paths don't recurse. For example: folder/../folder
    // TODO: file: Add UTF-8 support.
    NK_MEMCPY(data->directory + len, (void*)button->label, (nk_size)(nk_strlen(button->label) + 1));

    enum nk_symbol_type symbol = nk_console_button_get_symbol(button);
    switch (symbol) { // Directory
        case NK_SYMBOL_TRIANGLE_LEFT: // Back
        case NK_SYMBOL_TRIANGLE_RIGHT: // Folder
            nk_console_set_active_parent(file);
            nk_console_add_event(file, NK_CONSOLE_EVENT_POST_RENDER_ONCE, &nk_console_file_refresh);
            break;
        default: // File
        {
            // Copy the string to the file buffer.
            // TODO: Ensure UTF-8 compatibility.
            int desired_length = nk_strlen(data->directory);
            if (desired_length >= data->file_path_buffer_size) {
                NK_ASSERT(0); // File path is too long
                nk_console_show_message(file, "Error: File path is too long.");
            }
            else {
                NK_MEMCPY(data->file_path_buffer, data->directory, (nk_size)desired_length);
                data->file_path_buffer[desired_length] = '\0';

                // Trigger the onchange event and exit.
                nk_console_trigger_event(file, NK_CONSOLE_EVENT_CHANGED);
            }

            // Now that we selected a file, we can exit.
            nk_console_set_active_parent(file->parent);
        } break;
    }
}

NK_API nk_console* nk_console_file_add_entry(nk_console* parent, const char* path, nk_bool is_directory) {
    if (parent == NULL || path == NULL || path[0] == '\0' || parent->data == NULL) {
        return NULL;
    }

    // Are we only selecting directories?
    nk_console_file_data* data = (nk_console_file_data*)nk_console_file_button_get_file_widget(parent)->data;
    if (is_directory == nk_false && data->select_directory == nk_true) {
        return NULL;
    }

    int len = nk_strlen(path);

    // Ignore the current directory.
    if (len == 1 && path[0] == '.') {
        return NULL;
    }
    else if (len == 2 && path[0] == '.' && path[1] == '.') {
        // Ignore the parent directory.
        return NULL;
    }

    // Add the button.
    nk_console* button = nk_console_button(parent, NULL);

    // Copy the path for the label, and register an event to destroy it.
    // TODO: file: Ensure UTF-8 compatibility.
    button->label = (const char*)NK_CONSOLE_MALLOC(nk_handle_id(0), NULL, (nk_size)(sizeof(char)) * (nk_size)(len + 1));
    nk_console_add_event(button, NK_CONSOLE_EVENT_DESTROYED, &nk_console_file_free_entry);

    char* label = (char*)button->label;

    // Use the base name as the label.
    const char* basename = nk_console_file_basename(path);
    nk_size basename_len = (nk_size)nk_strlen(basename);
    NK_MEMCPY(label, basename, basename_len);
    label[basename_len] = '\0';

    // Symbol
    if (is_directory == nk_true) {
        nk_console_button_set_symbol(button, NK_SYMBOL_TRIANGLE_RIGHT);
    }

    // Event
    nk_console_add_event(button, NK_CONSOLE_EVENT_CLICKED, &nk_console_file_entry_onclick);
    return button;
}


/**
 * Gets the length of the directory string of the given file path.
 */
static int nk_console_file_get_directory_len(const char* file_path) {
    if (file_path == NULL) {
        return 0;
    }
    int len = nk_strlen(file_path);
    for (int i = len - 1; i > 0; i--) {
        if (file_path[i] == '\\' || file_path[i] == '/') {
            return i;
        }
    }
    return 0;
}

/**
 * Fills the files array with the files in the current directory.
 */
NK_API void nk_console_file_refresh(nk_console* widget, void* user_data) {
    NK_UNUSED(user_data);
    widget = nk_console_file_button_get_file_widget(widget);
    if (widget == NULL || widget->data == NULL) {
        return;
    }

    nk_console_file_data* data = (nk_console_file_data*)widget->data;

    // Clear out all the current entries.
    nk_console_free_children(widget);

    // Add the back/cancel button
    nk_console* cancelButton = nk_console_button_onclick(widget, "Cancel", &nk_console_button_back);
    nk_console_button_set_symbol(cancelButton, NK_SYMBOL_X);

    // Show the Active directory.
    if (!data->select_directory) {
        // Active directory label
        nk_console* activeLabel = nk_console_label(widget, data->directory);
        activeLabel->alignment = NK_TEXT_CENTERED;
    }
    else {
        // Add the select directory button.
        nk_console* button = nk_console_file_add_entry(widget, data->directory, nk_true);
        nk_console_button_set_symbol(button, NK_SYMBOL_CIRCLE_SOLID);
        nk_console_set_tooltip(button, "Use this directory");
    }

    // Add the parent directory button
    nk_console* parent_directory_button = nk_console_button_onclick(widget, "..", &nk_console_file_entry_onclick);
    nk_console_button_set_symbol(parent_directory_button, NK_SYMBOL_TRIANGLE_LEFT);
    nk_console_set_tooltip(parent_directory_button, "Navigate to the parent directory");
    nk_console_set_active_widget(parent_directory_button);

#ifdef NK_CONSOLE_FILE_ADD_FILES
    // Iterate through the files in the directory, and add them as entries.
    if (NK_CONSOLE_FILE_ADD_FILES(widget, data->directory) == nk_false) {
        nk_console_label(widget, "No files found.")->alignment = NK_TEXT_CENTERED;
    }
#else
    // NK_CONSOLE_FILE_ADD_FILES is undefined, so back out.
    nk_console_show_message(widget, "Error: File system not available.");

    // Go back to the parent widget, and disable the widget.
    if (widget->parent != NULL) {
        widget->disabled = nk_true;
        nk_console_set_active_parent(widget->parent);
    }
#endif
}

/**
 * Button callback for the main file button.
 */
static void nk_console_file_main_click(nk_console* button, void* user_data) {
    NK_UNUSED(user_data);
    if (button == NULL || button->data == NULL) {
        return;
    }

    nk_console* file = nk_console_file_button_get_file_widget(button);
    if (file == NULL || file->data == NULL) {
        return;
    }

    nk_console_file_data* data = (nk_console_file_data*)file->data;

    int directory_len = nk_console_file_get_directory_len(data->file_path_buffer);
    NK_MEMCPY(data->directory, data->file_path_buffer, (nk_size)directory_len);
    data->directory[directory_len] = '\0';

    if (nk_strlen(data->directory) == 0) {
        // TODO: file: Make get current working directory function.
        data->directory[0] = '.';
        data->directory[1] = '\0';
    }

    // Set the active parent to the file widget, and refresh it after rendering everything else.
    nk_console_set_active_parent(file);
    nk_console_add_event(file, NK_CONSOLE_EVENT_POST_RENDER_ONCE, &nk_console_file_refresh);
}

NK_API void nk_console_file_set_file_user_data(nk_console* file, void* user_data) {
    file = nk_console_file_button_get_file_widget(file);
    if (file == NULL || file->data == NULL) {
        return;
    }
    nk_console_file_data* data = (nk_console_file_data*)file->data;
    data->file_user_data = user_data;
}

NK_API void* nk_console_file_get_file_user_data(nk_console* file) {
    file = nk_console_file_button_get_file_widget(file);
    if (file == NULL || file->data == NULL) {
        return NULL;
    }
    nk_console_file_data* data = (nk_console_file_data*)file->data;
    return data->file_user_data;
}

NK_API nk_console* nk_console_file(nk_console* parent, const char* label, char* file_path_buffer, int file_path_buffer_size) {
    if (parent == NULL || file_path_buffer == NULL || file_path_buffer_size <= 0) {
        return NULL;
    }

    // Create the widget data.
    nk_console_file_data* data = (nk_console_file_data*)NK_CONSOLE_MALLOC(nk_handle_id(0), NULL, sizeof(nk_console_file_data));
    nk_zero(data, sizeof(nk_console_file_data));

    data->file_path_buffer = file_path_buffer;
    data->file_path_buffer_size = file_path_buffer_size;

    nk_console* widget = nk_console_label(parent, label);
    widget->type = NK_CONSOLE_FILE;
    widget->columns = label == NULL ? 1 : 2;
    widget->render = nk_console_file_render;
    widget->selectable = nk_true;
    widget->data = data;

    nk_console_add_event(widget, NK_CONSOLE_EVENT_CLICKED, &nk_console_file_main_click);
    return widget;
}

NK_API nk_console* nk_console_dir(nk_console* parent, const char* label, char* dir_buffer, int dir_buffer_size) {
    nk_console* widget = nk_console_file(parent, label, dir_buffer, dir_buffer_size);
    if (widget == NULL) {
        return NULL;
    }

    nk_console_file_data* data = (nk_console_file_data*)widget->data;
    data->select_directory = nk_true;

    return widget;
}

#if defined(__cplusplus)
}
#endif

#endif // NK_CONSOLE_FILE_IMPLEMENTATION_ONCE
#endif // NK_CONSOLE_IMPLEMENTATION