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 font_sdr;
32 bool init_fs(const char *path)
34 iconrend = new ShapesIcons;
35 if(!iconrend->init()) {
39 if(!(fat_font = dtx_open_font_glyphmap("data/fat.glyphmap")) ||
40 dtx_get_glyphmap_ptsize(dtx_get_glyphmap(fat_font, 0)) != FAT_FONT_SZ) {
42 dtx_set(DTX_PADDING, 64);
44 if(!(fat_font = dtx_open_font("data/fat.font", 0))) {
45 fprintf(stderr, "failed to open font file data/fat.font\n");
48 dtx_prepare_range(fat_font, FAT_FONT_SZ * 8, 32, 127);
49 dtx_calc_font_distfield(fat_font, 1, 8);
50 dtx_save_glyphmap("data/fat.glyphmap", dtx_get_glyphmap(fat_font, 0));
52 dtx_use_font(fat_font, FAT_FONT_SZ);
54 if(!(font_sdr = create_program_load("sdr/dfont.v.glsl", "sdr/dfont.p.glsl"))) {
57 set_uniform_float(font_sdr, "smoothness", 0.01);
59 if(!(cur_node = get_fsnode(path))) {
68 std::map<std::string, FSNode*>::iterator it = node_cache.begin();
69 while(it != node_cache.end()) {
70 FSNode *node = it++->second;
74 dtx_close_font(fat_font);
78 static float icon_angle(int col, int ncols, float max_angle = 0.0f)
81 return max_angle * ((float)col / (float)(ncols - 1) - 0.5);
83 return 2.0 * M_PI * (float)col / (float)ncols;
88 static const float row_spacing = 0.25;
89 static const float radius = 0.6;
90 static const float umax = 0.42;
91 static const float max_icon_angle = M_PI * 2.0 * umax;
93 int max_ncols = std::max<int>(1, umax * 12);
96 glTranslatef(0, cam_height, 0);
99 rot_xform.rotate(time_sec, 0, 0);
100 rot_xform.rotate(0, 0, time_sec * 0.5);
102 glDisable(GL_TEXTURE_2D);
104 int nchildren = (int)cur_node->children.size();
105 int ncols = std::min(cur_node->nfiles, max_ncols);
107 int first = start_child % ncols;
108 int col = 0, row = 0;
111 // count directories ...
112 for(int i=0; i<nchildren; i++) {
113 FSNode *node = cur_node->children[i];
114 if(node->type == FSTYPE_DIR) {
121 for(int i=0; i<nchildren; i++) {
122 FSNode *node = cur_node->children[i];
124 if(node->type != FSTYPE_DIR) {
128 float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
131 xform.rotate_y(angle);
132 xform.translate(0, -0.3, 0);
136 glMultMatrixf(xform[0]);
139 glColor3f(0.2, 0.3, 0.8);
140 glVertex3f(0, 0, -0.3);
141 glVertex3f(0, 0, -2);
146 draw_node_name(node, angle, -0.3, radius, false);
152 for(int i=0; i<nchildren; i++) {
153 int idx = (i + first) % nchildren;
154 FSNode *node = cur_node->children[idx];
156 if(node->type == FSTYPE_DIR) {
163 float angle = icon_angle(col, ncols, max_icon_angle);
165 Mat4 xform = rot_xform;
166 xform.translate(0, row * row_spacing, -radius);
167 xform.rotate_y(angle);
170 glMultMatrixf(xform[0]);
171 iconrend->draw(node);
174 draw_node_name(node, angle, row * row_spacing - 0.1, radius, false);
185 static void draw_node_name(FSNode *node, float angle, float ypos, float dist, bool full)
187 dtx_use_font(fat_font, FAT_FONT_SZ);
188 int line_height = dtx_line_height();
190 int nlines = full ? node->name_lines.size() : 1;
191 for(int i=0; i<nlines; i++) {
192 const char *name = full ? node->name_lines[i].c_str() : node->short_name.c_str();
195 xform.translate(-dtx_string_width(name) / 2.0, -line_height * i, 0);
196 if(node->type == FSTYPE_DIR) {
197 xform.rotate_z(deg_to_rad(90));
198 xform.rotate_x(deg_to_rad(-90));
203 xform.translate(0, ypos, -dist);
204 xform.rotate_y(angle);
205 glMultMatrixf(xform[0]);
207 glUseProgram(font_sdr);
208 set_uniform_float(font_sdr, "height", line_height);
214 #define MAX_NAME_SZ 16
216 FSNode *get_fsnode(const char *path)
218 char *abspath = make_abs_path(path);
220 FSNode *node = node_cache[abspath];
225 const char *name = node->path.get_name();
227 const char *ptr = name;
229 if(ptr - name >= MAX_NAME_SZ) {
230 int len = ptr - name;
231 std::string s = std::string(name, len);
232 if(node->short_name.empty()) {
233 node->short_name = s;
234 node->short_name[len - 1] = node->short_name[len - 2] = '.';
236 node->name_lines.push_back(s);
242 if(node->short_name.empty()) {
243 node->short_name = name;
245 node->name_lines.push_back(name);
250 if(stat(node->path, &st) == -1) {
251 fprintf(stderr, "failed to stat: %s\n", node->path.get_path());
255 node->size = st.st_size;
257 switch(st.st_mode & S_IFMT) {
259 node->type = FSTYPE_FILE;
263 node->type = FSTYPE_DIR;
268 node->type = FSTYPE_DEV;
272 node->type = FSTYPE_UNKNOWN;
274 node_cache[abspath] = node;
280 FSNode *get_fsnode(const char *dir, const char *name)
283 return get_fsnode(name);
285 if(!name || *name == '/') {
289 int len = strlen(dir) + 1 + strlen(name);
290 char *buf = new char[len + 1];
291 sprintf(buf, "%s/%s", dir, name);
292 FSNode *res = get_fsnode(buf);
297 // ---- FSNode implementation ----
300 type = FSTYPE_UNKNOWN;
306 bool FSNode::expand()
308 if(type != FSTYPE_DIR) {
312 DIR *dir = opendir(path);
314 fprintf(stderr, "failed to open dir: %s: %s\n", path.get_path(), strerror(errno));
319 while((dent = readdir(dir))) {
320 if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
324 FSNode *node = get_fsnode(path, dent->d_name);
327 children.push_back(node);
338 printf("expanded %d children\n", (int)children.size());
340 parent = get_fsnode(path.get_parent());