diff --git a/file/file_util.cpp b/file/file_util.cpp index eeacc9ae55..90b3728b60 100644 --- a/file/file_util.cpp +++ b/file/file_util.cpp @@ -61,8 +61,7 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str) #ifndef METRO -size_t getFilesInDir(const char *directory, std::vector *files) -{ +size_t getFilesInDir(const char *directory, std::vector *files) { size_t foundEntries = 0; #ifdef _WIN32 // Find the first file in the directory. @@ -72,10 +71,9 @@ size_t getFilesInDir(const char *directory, std::vector *files) #else HANDLE hFind = FindFirstFile((std::string(directory) + "\\*").c_str(), &ffd); #endif - if (hFind == INVALID_HANDLE_VALUE) - { + if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); - return foundEntries; + return 0; } // windows loop do @@ -98,8 +96,10 @@ size_t getFilesInDir(const char *directory, std::vector *files) ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) continue; - - files->push_back(std::string(directory) + virtualName); + FileInfo info; + info.name = std::string(directory) + virtualName; + info.isDirectory = false; // TODO + files->push_back(info); #ifdef _WIN32 } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); @@ -136,4 +136,4 @@ std::string getDir(const std::string &path) if (cutpath[i] == '\\') cutpath[i] = '/'; } return cutpath; -} \ No newline at end of file +} diff --git a/file/file_util.h b/file/file_util.h index d5d3e4450d..62202155a9 100644 --- a/file/file_util.h +++ b/file/file_util.h @@ -8,7 +8,14 @@ bool writeStringToFile(bool text_file, const std::string &str, const char *filen bool readFileToString(bool text_file, const char *filename, std::string &str); // Beginnings of a directory utility system. TODO: Improve. -size_t getFilesInDir(const char *directory, std::vector *files); + +struct FileInfo +{ + std::string name; + bool isDirectory; +}; + +size_t getFilesInDir(const char *directory, std::vector *files); void deleteFile(const char *file); std::string getDir(const std::string &path); diff --git a/ui/screen.h b/ui/screen.h index 57a92ca443..87d2b00b75 100644 --- a/ui/screen.h +++ b/ui/screen.h @@ -63,5 +63,5 @@ private: std::list dialog_; }; -// Yeah, an old school non enforced singleton. Good enough. +// Yeah, an old school non enforced singleton. Good enough (not?) extern ScreenManager screenManager; diff --git a/ui/ui.cpp b/ui/ui.cpp index d6a0df757d..1d7c8dec65 100644 --- a/ui/ui.cpp +++ b/ui/ui.cpp @@ -248,7 +248,7 @@ void StringVectorListAdapter::drawItem(int item, int x, int y, int w, int h, boo ui_draw2d.DrawTextShadow(theme.uiFont, (*items_)[item].c_str(), x + UI_SPACE , y, 0xFFFFFFFF, ALIGN_LEFT | ALIGN_VCENTER); } -int UIList(int id, int x, int y, int w, int h, UIListAdapter *adapter, UIListState *state) { +int UIList::Do(int id, int x, int y, int w, int h, UIListAdapter *adapter) { const int item_h = 64; int clicked = 0; @@ -262,12 +262,12 @@ int UIList(int id, int x, int y, int w, int h, UIListAdapter *adapter, UIListSta } // If button is hot and active, but mouse button is not - // down, the user must have clicked the button. + // down, the user must have clicked a list item (unless after the last item). if (uistate.mousedown[i] == 0 && - uistate.hotitem[i] == id && - uistate.activeitem[i] == id && - state->selected != -1) { - clicked = 1; + uistate.hotitem[i] == id && + uistate.activeitem[i] == id && + selected != -1) { + clicked = 1; } } @@ -275,13 +275,15 @@ int UIList(int id, int x, int y, int w, int h, UIListAdapter *adapter, UIListSta int itemHeight = adapter->itemHeight(0); int numItems = adapter->getCount(); for (int i = 0; i < numItems; i++) { - int item_y = y + i * itemHeight - state->scrollY; - if (uistate.mousedown && adapter->itemEnabled(i) && item_y >= y - itemHeight && item_y <= y + h && - UIRegionHit(i, x, item_y, w, h, 0)) { - // ultra fast touch response - state->selected = i; + int item_y = y + i * itemHeight - scrollY; + if (uistate.mousedown && + adapter->itemEnabled(i) && + item_y >= y - itemHeight && + item_y <= y + h && + UIRegionHit(i, x, item_y, w, h, 0)) { + selected = i; } - adapter->drawItem(i, x, item_y, w, itemHeight, i == state->selected); + adapter->drawItem(i, x, item_y, w, itemHeight, i == selected); } uistate.lastwidget = id; diff --git a/ui/ui.h b/ui/ui.h index 78c16da1b5..2e4faed7e3 100644 --- a/ui/ui.h +++ b/ui/ui.h @@ -162,18 +162,12 @@ public: class StringVectorListAdapter : public UIListAdapter { public: - StringVectorListAdapter(std::vector *items) : items_(items) {} + StringVectorListAdapter(const std::vector *items) : items_(items) {} virtual size_t getCount() const { return items_->size(); } virtual void drawItem(int item, int x, int y, int w, int h, bool active) const; private: - std::vector *items_; -}; - -struct UIListState { - UIListState() : scrollY(0.0f), selected(-1) {} - float scrollY; - int selected; + const std::vector *items_; }; @@ -205,9 +199,20 @@ void UIText(int x, int y, const char *text, uint32_t color, float scale = 1.0f, // Slide choice, like the Angry Birds level selector. Not yet working. void UISlideChoice(int id, int y, const SlideItem *items, int numItems, UISlideState *state); -// List view. -// return -1 = no selection -int UIList(int id, int x, int y, int w, int h, UIListAdapter *adapter, UIListState *state); + +class UIList { + public: + UIList() : scrollY(0.0f), startDragY(0.0f), dragFinger(-1), selected(-1) {} + float scrollY; + float startDragY; + int dragFinger; + int selected; + // List view. + // return -1 = no selection + int Do(int id, int x, int y, int w, int h, UIListAdapter *adapter); + private: + DISALLOW_COPY_AND_ASSIGN(UIList); +}; // Call at end of frame. // Do this afterwards (or similar):