Browse Source

Press dot key in Menu to see hidden files

Thomas Buck 9 years ago
parent
commit
3088d9685a
7 changed files with 72 additions and 33 deletions
  1. 3
    0
      ChangeLog.md
  2. 7
    2
      include/Font.h
  3. 3
    4
      include/Menu.h
  4. 32
    1
      src/Font.cpp
  5. 16
    24
      src/Menu.cpp
  6. 1
    0
      src/OpenRaider.cpp
  7. 10
    2
      src/utils/Folder.cpp

+ 3
- 0
ChangeLog.md View File

@@ -7,6 +7,9 @@
7 7
       into their own file
8 8
     * Menu now acts like a file-manager, only showing one folder,
9 9
       and has the ability to go to the parent folder, and do mouse scrolling
10
+    * Added API to Font to draw strings centered
11
+    * Folder sorts its items alphabetically
12
+    * Press dot key in Menu to see hidden files and folders
10 13
 
11 14
     [ 20140809 ]
12 15
     * Script Unit Test brings it’s own scripts to test

+ 7
- 2
include/Font.h View File

@@ -35,8 +35,13 @@ public:
35 35
 
36 36
     virtual void writeString(FontString &s) = 0;
37 37
 
38
-    virtual void drawText(unsigned int x, unsigned int y, float scale, const unsigned char color[4], const char *s, ...)
39
-        __attribute__((format(printf, 6, 0)));
38
+    virtual void drawText(unsigned int x, unsigned int y, float scale,
39
+            const unsigned char color[4], const char *s, ...)
40
+            __attribute__((format(printf, 6, 0)));
41
+
42
+    virtual void drawTextCentered(unsigned int x, unsigned int y, float scale,
43
+            const unsigned char color[4], unsigned int width, const char *s, ...)
44
+            __attribute__((format(printf, 7, 0)));
40 45
 
41 46
 protected:
42 47
     bool mFontInit;

+ 3
- 4
include/Menu.h View File

@@ -10,7 +10,6 @@
10 10
 
11 11
 #include <memory>
12 12
 
13
-#include "Font.h"
14 13
 #include "utils/Folder.h"
15 14
 
16 15
 /*!
@@ -30,7 +29,8 @@ public:
30 29
     ~Menu();
31 30
 
32 31
     int initialize();
33
-    int initialize(Folder folder);
32
+    int initialize(std::string folder);
33
+    int initialize(Folder *folder);
34 34
 
35 35
     void setVisible(bool visible);
36 36
 
@@ -52,9 +52,8 @@ private:
52 52
     long mCursor;
53 53
     long mMin;
54 54
 
55
-    FontString mainText; //!< Used to draw heading centered
56
-
57 55
     Folder *mapFolder;
56
+    bool hiddenState;
58 57
 };
59 58
 
60 59
 Menu &getMenu();

+ 32
- 1
src/Font.cpp View File

@@ -10,6 +10,7 @@
10 10
 
11 11
 #include "global.h"
12 12
 #include "utils/strings.h"
13
+#include "Window.h"
13 14
 #include "Font.h"
14 15
 
15 16
 Font::~Font() {
@@ -25,7 +26,8 @@ void Font::setFont(const char *font) {
25 26
     mFontName = fullPath(font, 0);
26 27
 }
27 28
 
28
-void Font::drawText(unsigned int x, unsigned int y, float scale, const unsigned char color[4], const char *s, ...) {
29
+void Font::drawText(unsigned int x, unsigned int y, float scale,
30
+        const unsigned char color[4], const char *s, ...) {
29 31
     FontString tempText;
30 32
     va_list args;
31 33
     va_start(args, s);
@@ -49,3 +51,32 @@ void Font::drawText(unsigned int x, unsigned int y, float scale, const unsigned
49 51
     va_end(args);
50 52
 }
51 53
 
54
+void Font::drawTextCentered(unsigned int x, unsigned int y, float scale,
55
+        const unsigned char color[4], unsigned int width, const char *s, ...) {
56
+    FontString tempText;
57
+    va_list args;
58
+    va_start(args, s);
59
+
60
+    tempText.text = new char[256];
61
+    tempText.scale = scale;
62
+    tempText.w = 0;
63
+    tempText.h = 0;
64
+    tempText.x = x;
65
+    tempText.y = y + getWindow().getHeight(); //! \fixme Ugly hack to hide first draw
66
+    tempText.color[0] = color[0];
67
+    tempText.color[1] = color[1];
68
+    tempText.color[2] = color[2];
69
+    tempText.color[3] = color[3];
70
+
71
+    vsnprintf(tempText.text, 256, s, args);
72
+    tempText.text[255] = '\0';
73
+    writeString(tempText);
74
+
75
+    tempText.x = (width / 2) - ((unsigned int)(tempText.w / 2));
76
+    tempText.y = y;
77
+    writeString(tempText);
78
+
79
+    delete [] tempText.text;
80
+    va_end(args);
81
+}
82
+

+ 16
- 24
src/Menu.cpp View File

@@ -10,53 +10,44 @@
10 10
 
11 11
 #include "global.h"
12 12
 #include "Console.h"
13
+#include "Font.h"
13 14
 #include "OpenRaider.h"
14 15
 #include "utils/strings.h"
15 16
 #include "TombRaider.h"
16 17
 #include "Window.h"
17 18
 #include "Menu.h"
18 19
 
19
-// TODO
20
-// Going up to / leads to the current working directory
21
-
22 20
 Menu::Menu() {
23 21
     mVisible = false;
24 22
     mCursor = 0;
25 23
     mMin = 0;
26 24
     mapFolder = nullptr;
27
-
28
-    mainText.text = bufferString("%s", VERSION);
29
-    mainText.color[0] = BLUE[0];
30
-    mainText.color[1] = BLUE[1];
31
-    mainText.color[2] = BLUE[2];
32
-    mainText.color[3] = BLUE[3];
33
-    mainText.scale = 1.2f;
34
-    mainText.y = 10;
35
-    mainText.w = 0;
36
-    mainText.h = 0;
25
+    hiddenState = false;
37 26
 }
38 27
 
39 28
 Menu::~Menu() {
40
-    delete [] mainText.text;
41
-    mainText.text = nullptr;
42
-
43 29
     delete mapFolder;
44 30
     mapFolder = nullptr;
45 31
 }
46 32
 
47 33
 int Menu::initialize() {
48
-    return initialize(Folder(getOpenRaider().mPakDir));
34
+    return initialize(getOpenRaider().mPakDir);
35
+}
36
+
37
+int Menu::initialize(std::string folder) {
38
+    return initialize(new Folder(folder, hiddenState));
49 39
 }
50 40
 
51
-int Menu::initialize(Folder folder) {
41
+int Menu::initialize(Folder *folder) {
52 42
     if (mapFolder != nullptr)
53 43
         delete mapFolder;
54
-    mapFolder = new Folder(folder);
44
+    mapFolder = folder;
55 45
     mMin = mCursor = 0;
56 46
 
57 47
     mapFolder->executeRemoveFiles([](File &f) {
58 48
         // Filter files based on file name
59
-        if ((f.getName().compare(f.getName().length() - 4, 4, ".phd") != 0)
49
+        if ((f.getName().length() > 4)
50
+            && (f.getName().compare(f.getName().length() - 4, 4, ".phd") != 0)
60 51
             && (f.getName().compare(f.getName().length() - 4, 4, ".tr2") != 0)
61 52
             && (f.getName().compare(f.getName().length() - 4, 4, ".tr4") != 0)
62 53
             && (f.getName().compare(f.getName().length() - 4, 4, ".trc") != 0)) {
@@ -98,10 +89,8 @@ void Menu::display() {
98 89
     glRecti(0, 0, (GLint)getWindow().getWidth(), (GLint)getWindow().getHeight());
99 90
     glEnable(GL_TEXTURE_2D);
100 91
 
101
-    // Draw heading text, using FontString so we can get the
102
-    // width of the drawn text to center it
103
-    mainText.x = (getWindow().getWidth() / 2) - ((unsigned int)(mainText.w / 2));
104
-    getFont().writeString(mainText);
92
+    // Draw heading
93
+    getFont().drawTextCentered(0, 10, 1.2f, BLUE, getWindow().getWidth(), "%s", VERSION);
105 94
 
106 95
     // Estimate displayable number of items
107 96
     int items = (getWindow().getHeight() - 60) / 25;
@@ -161,6 +150,9 @@ void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
161 150
             mCursor = 0;
162 151
     } else if (key == enterKey) {
163 152
         play();
153
+    } else if (key == dotKey) {
154
+        hiddenState = !hiddenState;
155
+        initialize(mapFolder->getPath());
164 156
     }
165 157
 
166 158
     if (mCursor > (mMin + items - 1)) {

+ 1
- 0
src/OpenRaider.cpp View File

@@ -10,6 +10,7 @@
10 10
 
11 11
 #include "global.h"
12 12
 #include "Console.h"
13
+#include "Font.h"
13 14
 #include "Game.h"
14 15
 #include "Menu.h"
15 16
 #include "Render.h"

+ 10
- 2
src/utils/Folder.cpp View File

@@ -54,7 +54,9 @@ Folder::Folder(std::string folder, bool listDotFiles) {
54 54
         pos = path.find('\\');
55 55
     }
56 56
 
57
-    size_t last = path.rfind('/', path.length() - 2);
57
+    size_t last = 0;
58
+    if (path.length() > 1)
59
+        last = path.rfind('/', path.length() - 2);
58 60
     name = path.substr(last + 1);
59 61
     if (name.back() == '/')
60 62
         name.erase(name.length() - 1);
@@ -100,7 +102,10 @@ Folder &Folder::getFolder(unsigned long i) {
100 102
 
101 103
 Folder Folder::getParent() {
102 104
     size_t last = path.rfind('/', path.length() - 2);
103
-    return Folder(path.substr(0, last), listDot);
105
+    std::string parent = path.substr(0, last);
106
+    if (parent.length() == 0)
107
+        parent = "/";
108
+    return Folder(parent, listDot);
104 109
 }
105 110
 
106 111
 void Folder::executeRemoveFiles(std::function<bool (File &f)> func) {
@@ -140,6 +145,9 @@ void Folder::createFolderItems() {
140 145
         }
141 146
     }
142 147
 
148
+    std::sort(foundFiles.begin(), foundFiles.end());
149
+    std::sort(foundFolders.begin(), foundFolders.end());
150
+
143 151
     for (unsigned long i = 0; i < foundFiles.size(); i++)
144 152
         files.emplace_back(File(foundFiles.at(i)));
145 153
 

Loading…
Cancel
Save