Browse Source

Menu now works like a Filemanager

Thomas Buck 10 years ago
parent
commit
846d0c034d
10 changed files with 174 additions and 112 deletions
  1. 6
    0
      ChangeLog.md
  2. 3
    0
      include/Menu.h
  3. 10
    4
      include/utils/Folder.h
  4. 2
    4
      src/Command.cpp
  5. 74
    54
      src/Menu.cpp
  6. 3
    1
      src/OpenRaider.cpp
  7. 1
    0
      src/utils/CMakeLists.txt
  8. 5
    49
      src/utils/Folder.cpp
  9. 65
    0
      src/utils/FolderRecursive.cpp
  10. 5
    0
      test/ScriptPayload.h

+ 6
- 0
ChangeLog.md View File

2
 
2
 
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4
 
4
 
5
+    [ 20140810 ]
6
+    * Removed recursive Folder access method implementations
7
+      into their own file
8
+    * Menu now acts like a file-manager, only showing one folder,
9
+      and has the ability to go to the parent folder, and do mouse scrolling
10
+
5
     [ 20140809 ]
11
     [ 20140809 ]
6
     * Script Unit Test brings it’s own scripts to test
12
     * Script Unit Test brings it’s own scripts to test
7
     * Fixed binary Unit Test not deleting it’s tmp file in case of error
13
     * Fixed binary Unit Test not deleting it’s tmp file in case of error

+ 3
- 0
include/Menu.h View File

30
     ~Menu();
30
     ~Menu();
31
 
31
 
32
     int initialize();
32
     int initialize();
33
+    int initialize(Folder folder);
33
 
34
 
34
     void setVisible(bool visible);
35
     void setVisible(bool visible);
35
 
36
 
41
 
42
 
42
     void handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released);
43
     void handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released);
43
 
44
 
45
+    void handleMouseScroll(int xrel, int yrel);
46
+
44
 private:
47
 private:
45
 
48
 
46
     void play();
49
     void play();

+ 10
- 4
include/utils/Folder.h View File

29
     unsigned long folderCount();
29
     unsigned long folderCount();
30
     Folder &getFolder(unsigned long i);
30
     Folder &getFolder(unsigned long i);
31
 
31
 
32
-    unsigned long countRecursiveItems();
33
-    void executeRemoveRecursiveItems(std::function<bool (File &f)> func);
34
-    std::string getRecursiveItemName(unsigned long i);
35
-    //File &getRecursiveItem(unsigned long i);
32
+    Folder getParent();
33
+
34
+    void executeRemoveFiles(std::function<bool (File &f)> func);
35
+
36
+    // Accessing a folder recursively
37
+    // This treats all files in all subfolders as if they were in this folder
38
+    unsigned long countRecursiveFiles();
39
+    void executeRemoveRecursiveFiles(std::function<bool (File &f)> func);
40
+    std::string getRecursiveFileName(unsigned long i);
41
+    File &getRecursiveFile(unsigned long i);
36
 
42
 
37
 private:
43
 private:
38
 
44
 

+ 2
- 4
src/Command.cpp View File

91
         }
91
         }
92
         std::string temp;
92
         std::string temp;
93
         command >> temp;
93
         command >> temp;
94
-        char *tmp = bufferString("%s/%s", mPakDir, temp.c_str());
95
-        int error = getGame().loadLevel(tmp);
96
-        delete [] tmp;
94
+        int error = getGame().loadLevel(temp.c_str());
97
         return error;
95
         return error;
98
     } else if (cmd.compare("help") == 0) {
96
     } else if (cmd.compare("help") == 0) {
99
         std::string tmp;
97
         std::string tmp;
445
         getConsole().print("  \"leftctrl\"  for symbols and special keys");
443
         getConsole().print("  \"leftctrl\"  for symbols and special keys");
446
     } else if (cmd.compare("load") == 0) {
444
     } else if (cmd.compare("load") == 0) {
447
         getConsole().print("load-Command Usage:");
445
         getConsole().print("load-Command Usage:");
448
-        getConsole().print("  load levelfile.name");
446
+        getConsole().print("  load /path/to/level");
449
 /*
447
 /*
450
     } else if (cmd.compare("sshot") == 0) {
448
     } else if (cmd.compare("sshot") == 0) {
451
         getConsole().print("sshot-Command Usage:");
449
         getConsole().print("sshot-Command Usage:");

+ 74
- 54
src/Menu.cpp View File

16
 #include "Window.h"
16
 #include "Window.h"
17
 #include "Menu.h"
17
 #include "Menu.h"
18
 
18
 
19
+// TODO
20
+// Going up to / leads to the current working directory
19
 
21
 
20
 Menu::Menu() {
22
 Menu::Menu() {
21
     mVisible = false;
23
     mVisible = false;
43
 }
45
 }
44
 
46
 
45
 int Menu::initialize() {
47
 int Menu::initialize() {
48
+    return initialize(Folder(getOpenRaider().mPakDir));
49
+}
50
+
51
+int Menu::initialize(Folder folder) {
46
     if (mapFolder != nullptr)
52
     if (mapFolder != nullptr)
47
         delete mapFolder;
53
         delete mapFolder;
48
-    mapFolder = new Folder(getOpenRaider().mPakDir);
54
+    mapFolder = new Folder(folder);
55
+    mMin = mCursor = 0;
49
 
56
 
50
-    mapFolder->executeRemoveRecursiveItems([](File &f) {
57
+    mapFolder->executeRemoveFiles([](File &f) {
51
         // Filter files based on file name
58
         // Filter files based on file name
52
         if ((f.getName().compare(f.getName().length() - 4, 4, ".phd") != 0)
59
         if ((f.getName().compare(f.getName().length() - 4, 4, ".phd") != 0)
53
             && (f.getName().compare(f.getName().length() - 4, 4, ".tr2") != 0)
60
             && (f.getName().compare(f.getName().length() - 4, 4, ".tr2") != 0)
67
         return false; // keep file on list
74
         return false; // keep file on list
68
     });
75
     });
69
 
76
 
77
+    if ((mapFolder->fileCount() + mapFolder->folderCount()) > 0)
78
+        mCursor = 1; // Don't select ".." by default
79
+
70
     return 0;
80
     return 0;
71
 }
81
 }
72
 
82
 
93
     mainText.x = (getWindow().getWidth() / 2) - ((unsigned int)(mainText.w / 2));
103
     mainText.x = (getWindow().getWidth() / 2) - ((unsigned int)(mainText.w / 2));
94
     getFont().writeString(mainText);
104
     getFont().writeString(mainText);
95
 
105
 
96
-    if ((mapFolder == nullptr) || (mapFolder->countRecursiveItems() == 0)) {
97
-        getFont().drawText(25, (getWindow().getHeight() / 2) - 20, 0.75f, RED, "No maps found! See README.md");
98
-        return;
99
-    } else {
100
-        // Estimate displayable number of items
101
-        int items = (getWindow().getHeight() - 60) / 25;
102
-
103
-        // Select which part of the list to show
104
-        long min, max;
105
-        if (((long)mCursor - (items / 2)) > 0)
106
-            min = mCursor - (items / 2);
107
-        else
108
-            min = 0;
109
-
110
-        if ((mCursor + (items / 2)) < (long)mapFolder->countRecursiveItems())
111
-            max = mCursor + (items / 2);
112
-        else
113
-            max = (long)mapFolder->countRecursiveItems();
114
-
115
-        while ((max - min) < items) {
116
-            if (min > 0)
117
-                min--;
118
-            else if (max < ((int)mapFolder->countRecursiveItems()))
119
-                max++;
120
-            else
121
-                break;
122
-        }
123
-
124
-        mMin = min;
106
+    // Estimate displayable number of items
107
+    int items = (getWindow().getHeight() - 60) / 25;
125
 
108
 
126
-        for (long i = 0; i < (max - min); i++) {
127
-            const char *map = mapFolder->getRecursiveItemName((unsigned long)(i + min)).c_str();
128
-            getFont().drawText(25, (unsigned int)(50 + (25 * i)), 0.75f,
129
-                    ((i + min) == mCursor) ? RED : BLUE, "%s", map);
109
+    // Print list of "..", folders, files
110
+    for (long i = mMin; (i < (mMin + items))
111
+                && (i < (mapFolder->folderCount() + mapFolder->fileCount() + 1)); i++) {
112
+        if (i == 0) {
113
+            getFont().drawText(25, 50, 0.75f, (mCursor == i) ? RED : BLUE, "..");
114
+        } else {
115
+            getFont().drawText(25, (unsigned int)(50 + (25 * (i - mMin))), 0.75f,
116
+                (mCursor == i) ? RED : BLUE, "%s",
117
+                ((i - 1) < mapFolder->folderCount()) ?
118
+                    (mapFolder->getFolder(i - 1).getName() + "/").c_str()
119
+                    : mapFolder->getFile(i - 1 - mapFolder->folderCount()).getName().c_str());
130
         }
120
         }
131
     }
121
     }
132
 }
122
 }
133
 
123
 
134
 void Menu::play() {
124
 void Menu::play() {
135
-    char *tmp = bufferString("load %s", mapFolder->getRecursiveItemName((unsigned long)mCursor).c_str());
136
-    if (getOpenRaider().command(tmp) == 0) {
137
-        setVisible(false);
125
+    if (mCursor == 0) {
126
+        if (initialize(mapFolder->getParent().getPath()) != 0) {
127
+            //! \todo Display something if an error occurs
128
+        }
129
+    } else if ((mCursor - 1) < mapFolder->folderCount()) {
130
+        if (initialize(mapFolder->getFolder(mCursor - 1).getPath()) != 0) {
131
+            //! \todo Display something if an error occurs
132
+        }
138
     } else {
133
     } else {
139
-        //! \todo Display something if an error occurs
134
+        char *tmp = bufferString("load %s",
135
+                mapFolder->getFile((unsigned long)mCursor - 1 - mapFolder->folderCount()).getPath().c_str());
136
+        if (getOpenRaider().command(tmp) == 0) {
137
+            setVisible(false);
138
+        } else {
139
+            //! \todo Display something if an error occurs
140
+        }
141
+        delete [] tmp;
140
     }
142
     }
141
-    delete [] tmp;
142
 }
143
 }
143
 
144
 
144
 void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
145
 void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
145
     if (!pressed)
146
     if (!pressed)
146
         return;
147
         return;
147
 
148
 
149
+    assert(mapFolder != nullptr);
150
+    int items = (getWindow().getHeight() - 60) / 25;
151
+
148
     if (key == upKey) {
152
     if (key == upKey) {
149
         if (mCursor > 0)
153
         if (mCursor > 0)
150
             mCursor--;
154
             mCursor--;
151
         else
155
         else
152
-            mCursor = (long)mapFolder->countRecursiveItems() - 1;
156
+            mCursor = (long)(mapFolder->folderCount() + mapFolder->fileCount());
153
     } else if (key == downKey) {
157
     } else if (key == downKey) {
154
-        if (mCursor < (long)(mapFolder->countRecursiveItems() - 1))
158
+        if (mCursor < (long)(mapFolder->folderCount() + mapFolder->fileCount()))
155
             mCursor++;
159
             mCursor++;
156
         else
160
         else
157
             mCursor = 0;
161
             mCursor = 0;
158
-    } else if (key == rightKey) {
159
-        long i = 10;
160
-        if (mCursor > (long)(mapFolder->countRecursiveItems() - 11))
161
-            i = ((long)mapFolder->countRecursiveItems()) - 1 - mCursor;
162
-        while (i-- > 0)
163
-            handleKeyboard(downKey, true);
164
-    } else if (key == leftKey) {
165
-        long i = 10;
166
-        if (mCursor < 10)
167
-            i = mCursor;
168
-        while (i-- > 0)
169
-            handleKeyboard(upKey, true);
170
     } else if (key == enterKey) {
162
     } else if (key == enterKey) {
171
         play();
163
         play();
172
     }
164
     }
165
+
166
+    if (mCursor > (mMin + items - 1)) {
167
+        mMin = mCursor - items + 1;
168
+    } else if (mCursor < mMin) {
169
+        mMin = mCursor;
170
+    }
173
 }
171
 }
174
 
172
 
175
 void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
173
 void Menu::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
187
     }
185
     }
188
 }
186
 }
189
 
187
 
188
+void Menu::handleMouseScroll(int xrel, int yrel) {
189
+    assert((xrel != 0) || (yrel != 0));
190
+    assert(mapFolder != nullptr);
191
+    int items = (getWindow().getHeight() - 60) / 25;
192
+
193
+    if ((mapFolder->folderCount() + mapFolder->fileCount() + 1) > items) {
194
+        if (yrel < 0) {
195
+            if (mMin < (mapFolder->folderCount() + mapFolder->fileCount() + 1 - items))
196
+                mMin++;
197
+        } else if (yrel > 0) {
198
+            if (mMin > 0)
199
+                mMin--;
200
+        }
201
+
202
+        if (mCursor < mMin) {
203
+            mCursor = mMin;
204
+        } else if (mCursor > (mMin + items - 1)) {
205
+            mCursor = mMin + items - 1;
206
+        }
207
+    }
208
+}
209
+

+ 3
- 1
src/OpenRaider.cpp View File

239
     assert((xrel != 0) || (yrel != 0));
239
     assert((xrel != 0) || (yrel != 0));
240
     assert(mRunning == true);
240
     assert(mRunning == true);
241
 
241
 
242
-    if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
242
+    if (getMenu().isVisible()) {
243
+        getMenu().handleMouseScroll(xrel, yrel);
244
+    } else if (getConsole().isVisible()) {
243
         getConsole().handleMouseScroll(xrel, yrel);
245
         getConsole().handleMouseScroll(xrel, yrel);
244
     }
246
     }
245
 
247
 

+ 1
- 0
src/utils/CMakeLists.txt View File

3
 set (UTIL_SRCS ${UTIL_SRCS} "File.cpp")
3
 set (UTIL_SRCS ${UTIL_SRCS} "File.cpp")
4
 set (UTIL_SRCS ${UTIL_SRCS} "filesystem.cpp")
4
 set (UTIL_SRCS ${UTIL_SRCS} "filesystem.cpp")
5
 set (UTIL_SRCS ${UTIL_SRCS} "Folder.cpp")
5
 set (UTIL_SRCS ${UTIL_SRCS} "Folder.cpp")
6
+set (UTIL_SRCS ${UTIL_SRCS} "FolderRecursive.cpp")
6
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp")
7
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp")
7
 set (UTIL_SRCS ${UTIL_SRCS} "pixel.cpp")
8
 set (UTIL_SRCS ${UTIL_SRCS} "pixel.cpp")
8
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp")
9
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp")

+ 5
- 49
src/utils/Folder.cpp View File

98
     return folders.at(i);
98
     return folders.at(i);
99
 }
99
 }
100
 
100
 
101
-unsigned long Folder::countRecursiveItems() {
102
-    unsigned long count = fileCount();
103
-    for (unsigned long i = 0; i < folderCount(); i++)
104
-        count += getFolder(i).countRecursiveItems();
105
-    return count;
101
+Folder Folder::getParent() {
102
+    size_t last = path.rfind('/', path.length() - 2);
103
+    return Folder(path.substr(0, last), listDot);
106
 }
104
 }
107
 
105
 
108
-void Folder::executeRemoveRecursiveItems(std::function<bool (File &f)> func) {
106
+void Folder::executeRemoveFiles(std::function<bool (File &f)> func) {
107
+    createFolderItems();
109
     for (unsigned long i = 0; i < fileCount(); i++) {
108
     for (unsigned long i = 0; i < fileCount(); i++) {
110
         if (func(getFile(i))) {
109
         if (func(getFile(i))) {
111
             files.erase(files.begin() + (long)i--);
110
             files.erase(files.begin() + (long)i--);
112
         }
111
         }
113
     }
112
     }
114
-
115
-    for (unsigned long i = 0; i < folderCount(); i++) {
116
-        getFolder(i).executeRemoveRecursiveItems(func);
117
-    }
118
-}
119
-
120
-std::string Folder::getRecursiveItemName(unsigned long i) {
121
-    assert(i < countRecursiveItems());
122
-    if (i < fileCount()) {
123
-        return getFile(i).getName();
124
-    } else {
125
-        unsigned long count = fileCount();
126
-        for (unsigned long n = 0; n < folderCount(); n++) {
127
-            if ((i - count) < getFolder(n).countRecursiveItems()) {
128
-                return getFolder(n).getName() + '/'
129
-                    + getFolder(n).getRecursiveItemName(i - count);
130
-            }
131
-            count += getFolder(n).countRecursiveItems();
132
-        }
133
-    }
134
-
135
-    assert(false);
136
-    return "";
137
-}
138
-
139
-/*
140
-File &Folder::getRecursiveItem(unsigned long i) {
141
-    assert(i < countRecursiveItems());
142
-    if (i < fileCount()) {
143
-        return getFile(i);
144
-    } else {
145
-        unsigned long count = fileCount();
146
-        for (unsigned long n = 0; n < folderCount(); n++) {
147
-            if ((i - count) < getFolder(n).countRecursiveItems()) {
148
-                return getFolder(n).getRecursiveItem(i - count);
149
-            }
150
-            count += getFolder(n).countRecursiveItems();
151
-        }
152
-    }
153
-
154
-    assert(false);
155
-    return files.at(0);
156
 }
113
 }
157
-*/
158
 
114
 
159
 void Folder::createFolderItems() {
115
 void Folder::createFolderItems() {
160
     if (hasListed)
116
     if (hasListed)

+ 65
- 0
src/utils/FolderRecursive.cpp View File

1
+/*!
2
+ * \file src/utils/Folder.cpp
3
+ * \brief Recursive file-system walking utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "utils/File.h"
10
+#include "utils/Folder.h"
11
+
12
+unsigned long Folder::countRecursiveFiles() {
13
+    createFolderItems();
14
+    unsigned long count = fileCount();
15
+    for (unsigned long i = 0; i < folderCount(); i++)
16
+        count += getFolder(i).countRecursiveFiles();
17
+    return count;
18
+}
19
+
20
+void Folder::executeRemoveRecursiveFiles(std::function<bool (File &f)> func) {
21
+    executeRemoveFiles(func);
22
+    for (unsigned long i = 0; i < folderCount(); i++) {
23
+        getFolder(i).executeRemoveRecursiveFiles(func);
24
+    }
25
+}
26
+
27
+std::string Folder::getRecursiveFileName(unsigned long i) {
28
+    createFolderItems();
29
+    assert(i < countRecursiveFiles());
30
+    if (i < fileCount()) {
31
+        return getFile(i).getName();
32
+    } else {
33
+        unsigned long count = fileCount();
34
+        for (unsigned long n = 0; n < folderCount(); n++) {
35
+            if ((i - count) < getFolder(n).countRecursiveFiles()) {
36
+                return getFolder(n).getName() + '/'
37
+                    + getFolder(n).getRecursiveFileName(i - count);
38
+            }
39
+            count += getFolder(n).countRecursiveFiles();
40
+        }
41
+    }
42
+
43
+    assert(false);
44
+    return "";
45
+}
46
+
47
+File &Folder::getRecursiveFile(unsigned long i) {
48
+    createFolderItems();
49
+    assert(i < countRecursiveFiles());
50
+    if (i < fileCount()) {
51
+        return getFile(i);
52
+    } else {
53
+        unsigned long count = fileCount();
54
+        for (unsigned long n = 0; n < folderCount(); n++) {
55
+            if ((i - count) < getFolder(n).countRecursiveFiles()) {
56
+                return getFolder(n).getRecursiveFile(i - count);
57
+            }
58
+            count += getFolder(n).countRecursiveFiles();
59
+        }
60
+    }
61
+
62
+    assert(false);
63
+    return files.at(0);
64
+}
65
+

+ 5
- 0
test/ScriptPayload.h View File

8
 #ifndef _SCRIPT_PAYLOAD_H
8
 #ifndef _SCRIPT_PAYLOAD_H
9
 #define _SCRIPT_PAYLOAD_H
9
 #define _SCRIPT_PAYLOAD_H
10
 
10
 
11
+/* Payload creation:
12
+ * cat TOMBPC.DAT | python -c 'import sys,zlib; sys.stdout.write(zlib.compress(sys.stdin.read()))' > tr2_pc.z
13
+ * bin2header tr2_pc.z
14
+ */
15
+
11
 static const unsigned char tr2_pc_dat_z[] = {
16
 static const unsigned char tr2_pc_dat_z[] = {
12
     0x78, 0x9c, 0xd5, 0x57, 0x6b, 0x8c, 0x1c, 0xc5, 0x11, 0xae, 0x9e, 0x3d,
17
     0x78, 0x9c, 0xd5, 0x57, 0x6b, 0x8c, 0x1c, 0xc5, 0x11, 0xae, 0x9e, 0x3d,
13
     0xdb, 0x3c, 0x7c, 0x7e, 0xf1, 0x08, 0xe1, 0xe5, 0x85, 0xf0, 0x30, 0x04,
18
     0xdb, 0x3c, 0x7c, 0x7e, 0xf1, 0x08, 0xe1, 0xe5, 0x85, 0xf0, 0x30, 0x04,

Loading…
Cancel
Save