13 #include "gmath/gmath.h"
19 static void draw_node_name(FSNode *node, float angle, float ypos, float dist, bool full);
21 static IconRenderer *iconrend;
23 static std::map<std::string, FSNode*> node_cache;
24 static FSNode *cur_node;
25 static int start_child;
27 static dtx_font *fat_font;
28 #define FAT_FONT_SZ 32
29 static unsigned int glow_link_sdr;
30 static unsigned int chrome_font_sdr, glow_font_sdr;
33 bool init_fs(const char *path)
35 iconrend = new ShapesIcons;
36 if(!iconrend->init()) {
40 if(!(fat_font = dtx_open_font_glyphmap("data/fat.glyphmap")) ||
41 dtx_get_glyphmap_ptsize(dtx_get_glyphmap(fat_font, 0)) != FAT_FONT_SZ) {
43 dtx_set(DTX_PADDING, 64);
45 if(!(fat_font = dtx_open_font("data/fat.font", 0))) {
46 fprintf(stderr, "failed to open font file data/fat.font\n");
49 dtx_prepare_range(fat_font, FAT_FONT_SZ * 8, 32, 127);
50 dtx_calc_font_distfield(fat_font, 1, 8);
51 dtx_save_glyphmap("data/fat.glyphmap", dtx_get_glyphmap(fat_font, 0));
53 dtx_use_font(fat_font, FAT_FONT_SZ);
55 struct dtx_glyphmap *fat_gmap = dtx_get_glyphmap(fat_font, 0);
57 pixsz.x = 1.0 / dtx_get_glyphmap_width(fat_gmap);
58 pixsz.y = 1.0 / dtx_get_glyphmap_height(fat_gmap);
60 if(!(chrome_font_sdr = create_program_load("sdr/chrome_font.v.glsl", "sdr/chrome_font.p.glsl"))) {
63 set_uniform_float(chrome_font_sdr, "height", dtx_line_height());
64 set_uniform_float(chrome_font_sdr, "smoothness", 0.01);
65 set_uniform_float2(chrome_font_sdr, "pix_sz", pixsz.x, pixsz.y);
67 if(!(glow_font_sdr = create_program_load("sdr/dfont.v.glsl", "sdr/glow_font.p.glsl"))) {
70 set_uniform_float(glow_font_sdr, "smoothness", 0.01);
71 set_uniform_float2(glow_font_sdr, "pix_sz", pixsz.x, pixsz.y);
73 if(!(glow_link_sdr = create_program_load("sdr/glink.v.glsl", "sdr/glink.p.glsl"))) {
77 if(!(cur_node = get_fsnode(path))) {
86 std::map<std::string, FSNode*>::iterator it = node_cache.begin();
87 while(it != node_cache.end()) {
88 FSNode *node = it++->second;
92 dtx_close_font(fat_font);
96 static float icon_angle(int col, int ncols, float max_angle = 0.0f)
99 return max_angle * ((float)col / (float)(ncols - 1) - 0.5);
101 return 2.0 * M_PI * (float)col / (float)ncols;
106 static const float row_spacing = 0.25;
107 static const float radius = 0.6;
108 static const float umax = 0.42;
109 static const float max_icon_angle = M_PI * 2.0 * umax;
111 int max_ncols = std::max<int>(1, umax * 12);
114 glTranslatef(0, cam_height, 0);
117 rot_xform.rotate(time_sec, 0, 0);
118 rot_xform.rotate(0, 0, time_sec * 0.5);
120 glDisable(GL_TEXTURE_2D);
122 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
124 int nchildren = (int)cur_node->children.size();
125 int ncols = std::max(std::min(cur_node->nfiles, max_ncols), 1);
127 int first = start_child % ncols;
128 int col = 0, row = 0;
131 // count directories ...
132 for(int i=0; i<nchildren; i++) {
133 FSNode *node = cur_node->children[i];
134 if(node->type == FSTYPE_DIR) {
139 // draw the directory link lines
140 glUseProgram(glow_link_sdr);
141 set_uniform_float(glow_link_sdr, "tsec", time_sec);
142 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
144 for(int i=0; i<nchildren; i++) {
145 FSNode *node = cur_node->children[i];
147 if(node->type != FSTYPE_DIR) {
151 float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
154 xform.rotate_y(angle);
155 xform.translate(0, -0.3, 0);
158 glMultMatrixf(xform[0]);
161 set_uniform_float(glow_link_sdr, "phase", col * 42.0);
164 glColor3f(0.2, 0.3, 0.8);
166 glVertex3f(-0.2, 0, 0.05);
168 glVertex3f(0.2, 0, 0.05);
170 glVertex3f(0.2, 0, -5.0);
172 glVertex3f(-0.2, 0, -5.0);
180 // draw the directory labels
181 glUseProgram(glow_font_sdr);
183 for(int i=0; i<nchildren; i++) {
184 FSNode *node = cur_node->children[i];
186 if(node->type != FSTYPE_DIR) {
190 float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
192 draw_node_name(node, angle, -0.3, radius, false);
195 // then draw file icons
199 for(int i=0; i<nchildren; i++) {
200 int idx = (i + first) % nchildren;
201 FSNode *node = cur_node->children[idx];
203 if(node->type == FSTYPE_DIR) {
208 float angle = icon_angle(col, ncols, max_icon_angle);
210 Mat4 xform = rot_xform;
211 xform.translate(0, row * row_spacing, -radius);
212 xform.rotate_y(angle);
215 glMultMatrixf(xform[0]);
216 iconrend->draw(node);
225 // then draw the file labels
226 glUseProgram(chrome_font_sdr);
229 for(int i=0; i<nchildren; i++) {
230 int idx = (i + first) % nchildren;
231 FSNode *node = cur_node->children[idx];
233 if(node->type == FSTYPE_DIR) {
238 float angle = icon_angle(col, ncols, max_icon_angle);
240 draw_node_name(node, angle, row * row_spacing - 0.1, radius, false);
251 static void draw_node_name(FSNode *node, float angle, float ypos, float dist, bool full)
253 dtx_use_font(fat_font, FAT_FONT_SZ);
254 int line_height = dtx_line_height();
256 int nlines = full ? node->name_lines.size() : 1;
257 for(int i=0; i<nlines; i++) {
258 const char *name = full ? node->name_lines[i].c_str() : node->short_name.c_str();
261 xform.translate(-dtx_string_width(name) / 2.0, -line_height * i, 0);
262 if(node->type == FSTYPE_DIR) {
263 xform.rotate_z(deg_to_rad(90));
264 xform.rotate_x(deg_to_rad(-90));
269 xform.translate(0, ypos, -dist);
270 xform.rotate_y(angle);
271 glMultMatrixf(xform[0]);
278 #define MAX_NAME_SZ 16
280 FSNode *get_fsnode(const char *path)
282 char *abspath = make_abs_path(path);
284 FSNode *node = node_cache[abspath];
289 const char *name = node->path.get_name();
291 const char *ptr = name;
293 if(ptr - name >= MAX_NAME_SZ) {
294 int len = ptr - name;
295 std::string s = std::string(name, len);
296 if(node->short_name.empty()) {
297 node->short_name = s;
298 node->short_name[len - 1] = node->short_name[len - 2] = '.';
300 node->name_lines.push_back(s);
306 if(node->short_name.empty()) {
307 node->short_name = name;
309 node->name_lines.push_back(name);
314 if(stat(node->path, &st) == -1) {
315 fprintf(stderr, "failed to stat: %s\n", node->path.get_path());
319 node->size = st.st_size;
321 switch(st.st_mode & S_IFMT) {
323 node->type = FSTYPE_FILE;
327 node->type = FSTYPE_DIR;
332 node->type = FSTYPE_DEV;
336 node->type = FSTYPE_UNKNOWN;
338 node_cache[abspath] = node;
344 FSNode *get_fsnode(const char *dir, const char *name)
347 return get_fsnode(name);
349 if(!name || *name == '/') {
353 int len = strlen(dir) + 1 + strlen(name);
354 char *buf = new char[len + 1];
355 sprintf(buf, "%s/%s", dir, name);
356 FSNode *res = get_fsnode(buf);
361 // ---- FSNode implementation ----
364 type = FSTYPE_UNKNOWN;
370 bool FSNode::expand()
372 if(type != FSTYPE_DIR) {
376 DIR *dir = opendir(path);
378 fprintf(stderr, "failed to open dir: %s: %s\n", path.get_path(), strerror(errno));
383 while((dent = readdir(dir))) {
384 if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
388 FSNode *node = get_fsnode(path, dent->d_name);
391 children.push_back(node);
402 printf("expanded %d children\n", (int)children.size());
404 parent = get_fsnode(path.get_parent());