nguyen nam / SD_PlayerSkeleton

Dependencies:   SDFileSystem_Warning_Fixed

Revision:
2:511479736d6e
Parent:
0:d310bb78455d
Child:
3:dd3b8a21417b
--- a/FileSelectorWav.cpp	Mon Aug 15 06:06:52 2016 +0000
+++ b/FileSelectorWav.cpp	Sun Oct 02 03:26:52 2016 +0000
@@ -2,13 +2,42 @@
 //  FileSelector class
 //      SD カード内のファイル名の一覧を表示し,ファイルを選択する
 //
-//  2016/07/27, Copyright (c) 2016 MIKAMI, Naoki
+//  2016/10/02, Copyright (c) 2016 MIKAMI, Naoki
 //--------------------------------------------------------------
 
 #include "FileSelectorWav.hpp"
 
 namespace Mikami
 {
+    FileSelector::FileSelector(uint8_t x0, uint8_t y0, int maxFiles,
+                               int maxNameLength, SD_WavReader &reader)
+        : X_(x0), Y_(y0),
+          MAX_FILES_(maxFiles), MAX_NAME_LENGTH_(maxNameLength),
+          fileNames_(maxFiles), fileNameLabels_(MAX_LIST_PAGE_),
+          next_(50, 238, 64, 36, 2, (string[]){"<", ">"}, 10, 0, 2, -1, Font24),
+          pageLabel_(210, 250, Label::LEFT, Font16),
+          lcd_(GuiBase::GetLcdPtr()), sdReader_(reader), page_(1)
+    {
+        Array<string> nonString(MAX_LIST_PAGE_, "");
+        rect_ = new ButtonGroup(X_, Y_, W_H_, W_H_, MAX_LIST_PAGE_,
+                                nonString, 0, V_L_-W_H_, 1,
+                                -1, Font12, 0, GuiBase::ENUM_BACK,
+                                BASE_COLOR_, TOUCHED_COLOR_);
+        rect_->EraseAll();
+        next_.EraseAll();
+
+        // Label を生成
+        for (int n=0; n<MAX_LIST_PAGE_; n++)
+            fileNameLabels_[n] = new Label(X_+30, Y_+5+V_L_*n, "",
+                                           Label::LEFT, Font16, BASE_COLOR_);
+    }
+
+    FileSelector::~FileSelector()
+    {   
+        delete rect_;
+        for (int n=0; n<MAX_LIST_PAGE_; n++) delete fileNameLabels_[n];
+    }
+
     bool FileSelector::CreateTable()
     {
         DIR* dp = opendir("/sd");
@@ -32,39 +61,37 @@
                         fileNames_[fileCount_++] = strName;
                     sdReader_.Close();
                 }
-
                 if (fileCount_ >= MAX_FILES_) break;
             }
             closedir(dp); 
         }
         else
             return false;
-
         if (fileCount_ == 0) return false;
 
-        if (rect_ != NULL) delete rect_;
-        Array<string> nonString(fileCount_, "");
-        rect_ = new ButtonGroup(X_, Y_, W_H_, W_H_, fileCount_,
-                                nonString, 0, V_L_-W_H_, 1,
-                                -1, Font12, 0, GuiBase::ENUM_BACK,
-                                BASE_COLOR_, TOUCHED_COLOR_);
-        for (int n=0; n<fileCount_; n++) rect_->Erase(n);
-        CreateLabels();   
-        prevFileCount_ = fileCount_;
+        div_t m = div(fileCount_, MAX_LIST_PAGE_);
+        maxPage_ = (m.rem == 0) ? m.quot : m.quot+1;
+        rect_->EraseAll();
         return true;
     }
 
     // ファイルを選択する
     bool FileSelector::Select(string &fileName)
     {
+        int m;
+        if (next_.GetTouchedNumber(m))
+        {
+            if (m==0) page_--;
+            else      page_++;
+            DisplayFileList(false);
+        }
+        
         int n;
         if (rect_->GetTouchedNumber(n))
         {
-            fileNameLabels_[n]->Draw(GetFileNameNoExt(n), TOUCHED_COLOR_);
-            if ((prev_ >= 0) && (prev_ != n))
-                fileNameLabels_[prev_]->Draw(GetFileNameNoExt(prev_));
-            prev_ = n;
-            fileName = fileNames_[n];
+            int k = (page_ - 1)*MAX_LIST_PAGE_ + n;
+            fileNameLabels_[n]->Draw(GetFileNameNoExt(k), TOUCHED_COLOR_);
+            fileName = fileNames_[k];
             return true;
         }
         else
@@ -79,9 +106,21 @@
                       (string *)fileNames_+fileCount_); 
         
         Erase(X_, Y_, MAX_NAME_LENGTH_*((sFONT *)(&Font16))->Width, 272-Y_);
-        rect_->DrawAll();
-        for (int n=0; n<fileCount_; n++)
-            fileNameLabels_[n]->Draw(GetFileNameNoExt(n));
+
+        div_t m = div(fileCount_, MAX_LIST_PAGE_);
+        int count = (m.quot >= page_) ? MAX_LIST_PAGE_ : m.rem;
+        for (int n=0; n<count; n++) rect_->Draw(n);
+        for (int n=0; n<count; n++)
+            fileNameLabels_[n]->Draw(GetFileNameNoExt(n+(page_-1)*MAX_LIST_PAGE_));
+
+        // 前のページ,次のページの選択ボタンなどを表示する
+        next_.ActivateAll();
+        next_.DrawAll();
+        if (page_ == 1) next_.Inactivate(0);
+        if (page_ == maxPage_) next_.Inactivate(1);
+        char page[6];
+        sprintf(page, "%d/%d", page_, maxPage_);
+        pageLabel_.Draw(page);
     }
 
     // ファイルの一覧の消去
@@ -92,17 +131,6 @@
         lcd_->FillRect(x, y, width, height);
     }
 
-    // Label を生成
-    void FileSelector::CreateLabels()
-    {
-        fileNameLabels_.SetSize(fileCount_);
-                    
-        for (int n=0; n<fileCount_; n++)
-            fileNameLabels_[n] = new Label(X_+30, Y_+5+V_L_*n, "",
-                                           Label::LEFT, Font16,
-                                           BASE_COLOR_);
-    }
-
     // 拡張子を削除した文字列を取得
     string FileSelector::GetFileNameNoExt(int n)
     {