samples: Tweaks
[sdk] / samples / guiAndGfx / slideShow / eShow.ec
1 import "ecere"
2
3 class PictureFile : struct
4 {
5    PictureFile prev, next;
6    char name[MAX_LOCATION];
7    int dirID;
8    bool dir;
9 };
10
11 class SlideShow : Window
12 {
13    OldList fileNames {};
14    Bitmap bitmap {};
15    Timer timer
16    {
17       this, delay = Seconds { 2 };
18
19       bool DelayExpired()
20       {
21          NextImage(direction);
22          return true;
23       }
24    };
25
26    PictureFile fileName;
27    int dirID;
28    int direction;
29
30    background = black;
31    anchor = Anchor { 0, 0, 0, 0 };
32    
33    void NextImage(int direction)
34    {
35       timer.Stop();
36       bitmap.Free();
37       if(!fileName) fileName = fileNames.first;
38       else if(fileName)
39       {
40          PictureFile oldFileName = fileName;
41          int oldDirID = fileName.dirID;
42          while(fileName.dirID == oldDirID || 
43             (direction == -2 && 
44                (fileName.prev ? fileName.prev : (PictureFile)fileNames.last).dirID == fileName.dirID))
45          {
46             if(Sgn(direction) == 1)
47             {
48                fileName = fileName.next;
49                if(!fileName) fileName = fileNames.first;
50             }
51             else if(Sgn(direction) == -1)
52             {
53                fileName = fileName.prev;
54                if(!fileName) fileName = fileNames.last;
55             }
56             if(Abs(direction) == 1) break;
57             
58             if(fileName == oldFileName) break;
59          }
60       }
61
62       if(fileName)
63          bitmap.Load(fileName.name, null, displaySystem);
64       Update(null);
65       if(this.direction)
66          timer.Start();
67    }
68
69    int ::Compare(PictureFile a, PictureFile b, void * data)
70    {
71       if(a.dir && !b.dir)
72          return 1;
73       else if(b.dir && !a.dir)
74          return -1;
75       else
76          return strcmp(a.name, b.name);
77    }
78
79    void AddDirectory(OldList files, char * directory)
80    {
81       FileListing listing { directory, "pcx, jpg, bmp, png" };
82       OldList list { };
83    
84       PictureFile fileName;
85       int thisDirID = dirID++;
86
87       while(listing.Find())
88       {
89          fileName = PictureFile {};
90          strcpy(fileName.name, listing.path);
91          list.Add(fileName);
92          fileName.dir = listing.stats.attribs.isDirectory;
93       }
94
95       list.Sort(Compare, null);
96
97       for(;(fileName = list.first);)
98       {
99          list.Remove(fileName);
100          
101          if(fileName.dir)
102          {
103             AddDirectory(files, fileName.name);
104             delete fileName;
105          }
106          else
107          {
108             files.Add(fileName);
109             fileName.dirID = thisDirID;
110          }
111       }
112    }
113
114    bool OnCreate()
115    {
116       AddDirectory(fileNames, "");
117       NextImage(0);
118       return true;
119    }
120
121    void OnRedraw(Surface surface)
122    {
123       if(bitmap && bitmap.width)
124       {
125          float scale = Min((float)clientSize.w / (float)bitmap.width, (float)clientSize.h / (float)bitmap.height);
126          int w = (int)(bitmap.width * scale);
127          int h = (int)(bitmap.height * scale);
128 #ifndef __linux__
129          surface.Filter(bitmap, (clientSize.w - w) / 2,(clientSize.h - h) / 2, 0,0, w, h, bitmap.width, bitmap.height);
130 #else
131          // Until Filter / Stretch works with X
132          surface.Blit(bitmap, (clientSize.w - bitmap.width) / 2,(clientSize.h - bitmap.height) / 2, 0,0, bitmap.width, bitmap.height);
133 #endif
134       }
135    }
136
137    bool OnKeyHit(Key key, unichar ch)
138    {
139       switch(key)
140       {
141          case left: NextImage(-2); break;
142          case right: NextImage(2); break;
143          case pageDown: case down: NextImage(1); break;
144          case pageUp: case up: NextImage(-1); break;
145       }
146       return true;
147    }
148
149    bool OnKeyDown(Key key, unichar ch)
150    {
151       switch(key)
152       {
153          case escape: Destroy(0); break;
154          case space: direction ^=1; if(direction) timer.Start(); else timer.Stop(); break;
155          case home: fileName = fileNames.first; NextImage(0); break;
156          case end: fileName = fileNames.last; NextImage(0); break;
157       }
158       return true;
159    }
160 }
161
162 class eShow : GuiApplication
163 {
164    fullScreen = true;
165    SlideShow slideShow {};
166 }