-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.c
173 lines (158 loc) · 5.95 KB
/
Search.c
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
#include "Search.h"
#include "debugmalloc.h"
ModuleList search_load_modules(char *moduleDir, TTF_Font *font, SDL_Renderer *renderer) {
FileList files = dir_getfiles(moduleDir);
ModuleList result;
result.num = files.num / 3;
result.modules = (struct ModuleData *) malloc(sizeof(struct ModuleData) * result.num);
for (size_t i = 0; i < result.num; i++) {
dir_remove_extension(files.files[i * 3]);
result.modules[i].name = (char *) malloc(sizeof(char) * (strlen(files.files[i * 3]) + 1));
strcpy(result.modules[i].name, files.files[i * 3]);
SDL_Surface *surface = TTF_RenderText_Blended(
font,
result.modules[i].name,
(SDL_Color) {255, 255, 255, 255});
result.modules[i].text = SDL_CreateTextureFromSurface(renderer, surface);
result.modules[i].w = surface->w;
result.modules[i].h = surface->h;
for (int j = 0; result.modules[i].name[j] != '\0'; j++)
result.modules[i].name[j] = (char) tolower(result.modules[i].name[j]);
SDL_FreeSurface(surface);
}
dir_free_filelist(&files);
return result;
}
void search_free_modules(ModuleList *moduleList) {
for (size_t i = 0; i < moduleList->num; i++) {
free(moduleList->modules[i].name);
SDL_DestroyTexture(moduleList->modules[i].text);
}
free(moduleList->modules);
}
void search_render_module(struct ModuleData *module, int x, int y, SDL_Renderer *renderer) {
SDL_Rect dst = {x, y, module->w, module->h};
SDL_RenderCopy(renderer, module->text, NULL, &dst);
}
void search_render_modulelist(ModuleList *modules, SDL_Rect clipRect, char *search, int yOffset, SDL_Renderer *renderer) {
SDL_RenderSetClipRect(renderer, &clipRect);
size_t len = strlen(search);
if (len == 0) {
for (size_t i = 0; i < modules->num; i++) {
search_render_module(&modules->modules[i],
clipRect.x,
clipRect.y - yOffset + (int) i * modules->modules[i].h,
renderer);
}
SDL_RenderSetClipRect(renderer, NULL);
return;
}
char *lowerSearch = (char *) malloc(sizeof(char) * (strlen(search) + 1));
strcpy(lowerSearch, search);
for (int i = 0; lowerSearch[i] != '\0'; i++)
lowerSearch[i] = (char) tolower(lowerSearch[i]);
int found = 0;
for (size_t i = 0; i < modules->num; i++) {
if (strstr(modules->modules[i].name, lowerSearch)) {
search_render_module(&modules->modules[i],
clipRect.x,
clipRect.y - yOffset + found * modules->modules[i].h,
renderer);
found++;
}
}
free(lowerSearch);
SDL_RenderSetClipRect(renderer, NULL);
}
SearchData search_create() {
SearchData result;
result.textInput = textinput_create();
return result;
}
void search_start(SearchData *searchData, char *moduleDir, TTF_Font *font, SDL_Renderer *renderer) {
searchData->selected = 0;
searchData->modules = search_load_modules(moduleDir, font, renderer);
searchData->yOffset = 0;
searchData->searchOver = false;
textinput_start();
}
SearchResult search_end(SearchData *searchData) {
SearchResult result;
result.selectedModule = NULL;
if (searchData->selected < 0 || searchData->selected >= searchData->modules.num) {
search_free_modules(&searchData->modules);
return result;
}
result.selectedModule = (char *)malloc(sizeof(char) * (strlen(searchData->modules.modules[searchData->selected].name) + 1));
strcpy(result.selectedModule, searchData->modules.modules[searchData->selected].name);
search_free_modules(&searchData->modules);
textinput_end();
textinput_clear(&searchData->textInput);
return result;
}
void search_handle_event(SearchData *searchData, SDL_Event *e) {
if (e->type == SDL_KEYDOWN && e->key.keysym.scancode == SDL_SCANCODE_RETURN) {
searchData->searchOver = true;
} else if (e->type == SDL_KEYDOWN && e->key.keysym.scancode == SDL_SCANCODE_DOWN) {
for (size_t i = searchData->selected + 1; i < searchData->modules.num; i++) {
if (strstr(searchData->modules.modules[i].name, searchData->textInput.text)) {
searchData->selected = i;
break;
}
}
} else if (e->type == SDL_KEYDOWN && e->key.keysym.scancode == SDL_SCANCODE_UP) {
for (int i = searchData->selected - 1; i >= 0; i--) {
if (strstr(searchData->modules.modules[i].name, searchData->textInput.text)) {
searchData->selected = i;
break;
}
}
} else {
if (textinput_handle_event(&searchData->textInput, e)) {
searchData->selected = -1;
for (size_t i = 0; i < searchData->modules.num; i++) {
if (strstr(searchData->modules.modules[i].name, searchData->textInput.text)) {
searchData->selected = i;
break;
}
}
searchData->yOffset = 0;
}
}
}
void search_render(SearchData *searchData, SDL_Rect moduleClipRect, TTF_Font *font, int searchTextX, int searchTextY, SDL_Renderer *renderer) {
textinput_update_graphic(&searchData->textInput, font);
int offset = 0;
for (int i = 0; i < searchData->selected; i++) {
if (strstr(searchData->modules.modules[i].name, searchData->textInput.text)) {
offset++;
}
}
if (moduleClipRect.y + searchData->modules.modules[0].h * (offset - searchData->yOffset) +
searchData->modules.modules[0].h > moduleClipRect.y + moduleClipRect.h) {
searchData->yOffset++;
}
else if (moduleClipRect.y + searchData->modules.modules[0].h * (offset - searchData->yOffset) <
moduleClipRect.y) {
searchData->yOffset--;
}
search_render_modulelist(&searchData->modules, moduleClipRect,
searchData->textInput.text, searchData->yOffset * searchData->modules.modules[0].h, renderer);
if (searchData->selected != -1) {
SDL_Rect selectedRect = {
moduleClipRect.x,
moduleClipRect.y + searchData->modules.modules[0].h * (offset - searchData->yOffset),
moduleClipRect.w,
searchData->modules.modules[0].h
};
SDL_SetRenderDrawColor(renderer, 200, 0, 255, 100);
SDL_RenderFillRect(renderer, &selectedRect);
}
textinput_render(&searchData->textInput, font, searchTextX, searchTextY, renderer);
}
void search_free_result(SearchResult *result) {
free(result->selectedModule);
}
void search_free(SearchData *searchData) {
textinput_free(&searchData->textInput);
}