summaryrefslogtreecommitdiff
path: root/src/puzzles/collection.c
blob: c38225942dea2d05caae401be06a328267a96388 (plain)
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
#include <dirent.h>
#include "puzzle.h"
#include "../resources/resources.h"
#include "../ui_elements/UI.h"
#include <raylib.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "../utils/arena.h"
#include <string.h>

static const char *IMAGE_NAME = "icon.png";
static const float NAME_IN_CARD_RATIO = 0.2f;

CollectionElementType getTypeFromPath(char *fullPath) {
    struct stat st;
    if (stat(fullPath, &st) != 0) return COLLECTION_ELEMENT_UNKNOWN;

    if (S_ISDIR(st.st_mode)) {
        return COLLECTION_ELEMENT_COLLECTION;
    } else if (S_ISREG(st.st_mode)) {
        return COLLECTION_ELEMENT_PUZZLE;
    }
    return COLLECTION_ELEMENT_UNKNOWN;
}

bool isIcon(struct dirent *entry) {
    return strncmp(entry->d_name, IMAGE_NAME, strlen(IMAGE_NAME)) == 0;
}

void addIcon(CollectionElement *el, struct dirent *entry) {
    if (el == NULL || entry == NULL) return;
    char fullPath[1024];
    snprintf(fullPath, sizeof(fullPath), "%s/%s", el->path, entry->d_name);
    TraceLog(LOG_INFO, "Loading texture %s for element %s", fullPath, el->path); 
    el->icon = LoadTexture(fullPath);
    TraceLog(LOG_INFO, "Texture dimentions: %d;%d, id %u", el->icon.width, el->icon.height, el->icon.id); 
    el->hasIcon = true;
}

bool isPuzzle(char *path) {
    size_t entry_name_len = strlen(path);
    if (entry_name_len < 4) return false;
    char *last_bytes = path + entry_name_len - 3;
    if (strncmp(last_bytes, ".pz", 3) != 0) return false;
    return true;
}

bool isElement(struct dirent *entry, char *basePath) {
    if (entry == NULL) return false;
    if (strncmp(".", entry->d_name, 1) == 0) return false;
    if (isIcon(entry)) return false;

    char full_path_buf[1024];
    snprintf(full_path_buf, sizeof(full_path_buf), "%s/%s", basePath, entry->d_name);
    struct stat st;
    if (stat(full_path_buf, &st) != 0) return false;
    if (S_ISDIR(st.st_mode)) return true;

    return isPuzzle(entry->d_name);
}

char *getName(char *name) {
    char buf[1024];
    strncpy(buf, name, sizeof(buf));
    size_t len = strlen(name);

    // Cutting a file extention
    for (size_t i = 0; i < len; ++i) {
        if (buf[i] == '.') {
            buf[i] = 0x0;
            break;
        }
    }

    return strdup(buf);
}

size_t getElementCount(DIR *dir, CollectionElement *el) {
    struct dirent *entry;
    size_t outp = 0;
    while ((entry = readdir(dir)) != NULL) {
        if (isElement(entry, el->path)) outp += 1;
    }
    rewinddir(dir);
    return outp;
}

void initCollectionFromDir(CollectionElement *el) {
    if (el == NULL) return;

    TraceLog(LOG_INFO, "Initializing a collection element from %s/", el->path);
    DIR *dir = opendir(el->path);
    if (dir == NULL) {
        TraceLog(LOG_ERROR, "Failed to open the directory");
        return;
    }

    el->hasIcon = false;
    el->childCount = getElementCount(dir, el);
    el->children = malloc(sizeof(CollectionElement) * el->childCount);
    // printf("element count: %zu\n", el->childCount);
    if (el->children == NULL) {
        TraceLog(LOG_ERROR, "Failed to allocate the collection element children array");
        closedir(dir);
        return;
    }

    struct dirent *entry;
    char full_path_buf[1024];
    size_t idx = 0;

    while ((entry = readdir(dir)) != NULL) {
        if (isIcon(entry)) {
            addIcon(el, entry);
            continue;
        }
        if (!isElement(entry, el->path)) continue;
        snprintf(full_path_buf, sizeof(full_path_buf), "%s/%s", el->path, entry->d_name);

        struct stat st;
        if (stat(full_path_buf, &st) != 0) continue;

        CollectionElement *read_el = &el->children[idx];
        // printf("idx = %zu\n", idx);
        if (S_ISDIR(st.st_mode)) {
            read_el->type = COLLECTION_ELEMENT_COLLECTION;
        } else if (S_ISREG(st.st_mode)) {
            read_el->type = COLLECTION_ELEMENT_PUZZLE;
        }
        read_el->IsInitialized = false;
        read_el->path = strdup(full_path_buf);
        read_el->name = getName(entry->d_name);

        idx += 1;
    }

    closedir(dir);
}

void InitializeCollection(CollectionElement *el) {
    if (el == NULL) {
        TraceLog(LOG_ERROR, "Failed to initialize collection element: the element is NULL");
        return;
    }
    switch (el->type) {
        case COLLECTION_ELEMENT_PUZZLE: break;
        case COLLECTION_ELEMENT_COLLECTION: 
                     initCollectionFromDir(el);
                     break;
        default:
                     TraceLog(LOG_WARNING, "Invalid CollectionElement type");
                     break;
    }
}

void DrawCollectionElement(CollectionElement *el, CollectionElement *parent, Rectangle rec) {
    if (el == NULL || parent == NULL) return;
    DrawRecBordered(rec, COLORSCHEME_WHITE, COLORSCHEME_ORANGE, BORDER);
    if (el->type == COLLECTION_ELEMENT_PUZZLE && parent->hasIcon) {
        Rectangle texture_rec = {
            .x = rec.x + BORDER,
            .y = rec.y + BORDER + rec.height * NAME_IN_CARD_RATIO,
            .width = rec.width - BORDER * 2,
            .height = rec.height * (1 - NAME_IN_CARD_RATIO) - BORDER * 2,
        };
        DrawTexturePro(parent->icon, (Rectangle) {
                .x = 0,
                .y = 0,
                .width = parent->icon.width,
                .height = parent->icon.height,
                }, texture_rec, (Vector2) { .x = 0, .y = 0 }, 0, WHITE);
    } 
    int scroll = 0;
    DrawTextInRec((Rectangle) {
            .x = rec.x + BORDER,
            .y = rec.y + BORDER,
            .width = rec.width - BORDER * 2,
            .height = rec.height * NAME_IN_CARD_RATIO - BORDER * 2,
            }, el->name, GetMainFont(), 20, COLORSCHEME_BROWN, 1, &scroll);
}