chrome letters first attempt
[vrfileman] / src / fs.cc
index b8f7420..5795cd0 100644 (file)
--- a/src/fs.cc
+++ b/src/fs.cc
@@ -4,6 +4,7 @@
 #include <errno.h>
 #include <string>
 #include <map>
+#include <algorithm>
 #include <unistd.h>
 #include <dirent.h>
 #include <sys/stat.h>
@@ -13,6 +14,7 @@
 #include "opengl.h"
 #include "app.h"
 #include "drawtext.h"
+#include "sdr.h"
 
 static IconRenderer *iconrend;
 
@@ -22,21 +24,39 @@ static int start_child;
 
 static dtx_font *fat_font;
 #define FAT_FONT_SZ    32
+static unsigned int font_sdr;
 
 
-bool init_fs()
+bool init_fs(const char *path)
 {
        iconrend = new ShapesIcons;
        if(!iconrend->init()) {
                return false;
        }
 
-       if(!(fat_font = dtx_open_font("data/fat.font", FAT_FONT_SZ))) {
-               fprintf(stderr, "failed to open font file data/fat.font\n");
+       if(!(fat_font = dtx_open_font_glyphmap("data/fat.glyphmap")) ||
+                       dtx_get_glyphmap_ptsize(dtx_get_glyphmap(fat_font, 0)) != FAT_FONT_SZ) {
+
+               dtx_set(DTX_PADDING, 64);
+
+               if(!(fat_font = dtx_open_font("data/fat.font", 0))) {
+                       fprintf(stderr, "failed to open font file data/fat.font\n");
+                       return false;
+               }
+               dtx_prepare_range(fat_font, FAT_FONT_SZ * 8, 32, 127);
+               dtx_calc_font_distfield(fat_font, 1, 8);
+               dtx_save_glyphmap("data/fat.glyphmap", dtx_get_glyphmap(fat_font, 0));
+       }
+       dtx_use_font(fat_font, FAT_FONT_SZ);
+
+       if(!(font_sdr = create_program_load("sdr/dfont.v.glsl", "sdr/dfont.p.glsl"))) {
                return false;
        }
+       set_uniform_float(font_sdr, "smoothness", 0.01);
 
-       cur_node = get_fsnode(0);
+       if(!(cur_node = get_fsnode(path))) {
+               return false;
+       }
        cur_node->expand();
        return true;
 }
@@ -49,6 +69,7 @@ void cleanup_fs()
                delete node;
        }
        node_cache.clear();
+       dtx_close_font(fat_font);
        delete iconrend;
 }
 
@@ -61,22 +82,34 @@ static Vec3 icon_pos(int row, int col, int ncols, float row_spacing, float radiu
        return Vec3(x, y, z);
 }
 
+static float icon_angle(int col, int ncols, float max_angle = 0.0f)
+{
+       if(max_angle > 0) {
+               return max_angle * ((float)col / (float)(ncols - 1) - 0.5);
+       }
+       return 2.0 * M_PI * (float)col / (float)ncols;
+}
+
 void draw_fs()
 {
-       static const int ncols = 8;
-       static const float row_spacing = 2.0;
-       static const float radius = 5;
+       static const float row_spacing = 0.25;
+       static const float radius = 0.6;
+       static const float umax = 0.42;
+       static const float max_icon_angle = M_PI * 2.0 * umax;
+
+       int max_ncols = std::max<int>(1, umax * 12);
 
        Mat4 base_xform;
        base_xform.rotate(time_sec, 0, 0);
        base_xform.rotate(0, 0, time_sec * 0.5);
-       base_xform.translate(0, 2, 0);
+       base_xform.translate(0, 1.65, 0);
 
-       glUseProgram(0);
        glDisable(GL_TEXTURE_2D);
 
-       int first = start_child % ncols;
        int nchildren = (int)cur_node->children.size();
+       int ncols = std::min(cur_node->nfiles, max_ncols);
+
+       int first = start_child % ncols;
        int col = 0, row = 0;
 
        for(int i=0; i<nchildren; i++) {
@@ -87,7 +120,9 @@ void draw_fs()
                        continue;
                }
 
-               float angle = -2.0 * M_PI * (float)col / (float)ncols;
+               glUseProgram(0);
+
+               float angle = icon_angle(col, ncols, max_icon_angle);
 
                Mat4 xform = base_xform;
                xform.translate(0, row * row_spacing, -radius);
@@ -101,11 +136,13 @@ void draw_fs()
                glPushMatrix();
                xform = Mat4::identity;
                xform.translate(-dtx_string_width(node->path.get_name()) / 2.0, 0, 0);
-               xform.scale(0.01);
-               xform.translate(0, 1 + row * row_spacing, -radius);
+               xform.scale(0.001);
+               xform.translate(0, 1.54 + row * row_spacing, -radius);
                xform.rotate_y(angle);
                glMultMatrixf(xform[0]);
 
+               glUseProgram(font_sdr);
+               set_uniform_float(font_sdr, "height", dtx_line_height());
                dtx_string(node->path.get_name());
                glPopMatrix();
 
@@ -179,6 +216,7 @@ FSNode::FSNode()
        type = FSTYPE_UNKNOWN;
        size = 0;
        parent = 0;
+       nfiles = ndirs = 0;
 }
 
 bool FSNode::expand()
@@ -203,6 +241,15 @@ bool FSNode::expand()
                if(!node) continue;
 
                children.push_back(node);
+               switch(node->type) {
+               case FSTYPE_FILE:
+                       ++nfiles;
+                       break;
+               case FSTYPE_DIR:
+                       ++ndirs;
+               default:
+                       break;
+               }
        }
        printf("expanded %d children\n", (int)children.size());