1 //  Construo - A wire-frame construction gamee
2 //  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 #include "construo.hpp"
18 #include "system_context.hpp"
19 #include "world_button.hpp"
20 #include "gui_directory_button.hpp"
21 #include "gui_file_manager.hpp"
22 #include "gui_new_file_button.hpp"
23 #include "gui_directory.hpp"
24 
GUIDirectory(const std::string & arg_pathname,Mode m)25 GUIDirectory::GUIDirectory (const std::string& arg_pathname, Mode m)
26   : GUIChildManager (0, 0, 800, 600),
27     pathname (arg_pathname),
28     mode (m)
29 {
30   mtime = system_context->get_mtime(pathname);
31   std::vector<std::string> dir = system_context->read_directory(pathname);
32 
33   if (mode == SAVE_DIRECTORY && pathname != "/")
34     files.push_back(new GUINewFileButton(pathname));
35 
36   for (std::vector<std::string>::iterator i = dir.begin(); i != dir.end(); ++i)
37     {
38       std::string filename = pathname + *i;
39 
40       FileType type = system_context->get_file_type (filename);
41 
42       //std::cout << "Creating object for: " << filename << std::endl;
43 
44       if (type == FT_DIRECTORY)
45         {
46           if (*(filename.end()-1) == '/') // FIXME: Hack
47             files.push_back (new GUIDirectoryButton (filename));
48           else
49             files.push_back (new GUIDirectoryButton (filename + "/"));
50         }
51       else if (type == FT_CONSTRUO_FILE)
52         {
53           if (mode == SAVE_DIRECTORY)
54             files.push_back (new WorldButton (filename, WorldButton::SAVE_BUTTON));
55           else
56             files.push_back (new WorldButton (filename, WorldButton::LOAD_BUTTON));
57         }
58       else // (type == FT_UNKNOWN_FILE)
59         {
60           // ignore unknown files
61           std::cout << "GUIFileManager: ignoring '" << filename
62                     << "' since it has unknown filetype" << std::endl;
63         }
64     }
65 
66   offset = 0;
67   place_components ();
68 }
69 
~GUIDirectory()70 GUIDirectory::~GUIDirectory ()
71 {
72   for(std::vector<GUIFileButton*>::iterator i = files.begin();
73       i != files.end(); ++i)
74     {
75       // FIXME: Very ugly, we remove all components from the manager so that he doesn't delete them twice
76       remove(*i);
77       delete *i;
78     }
79 }
80 
81 void
place_components()82 GUIDirectory::place_components ()
83 {
84   // Remove all file components
85   for(std::vector<GUIFileButton*>::iterator i = files.begin();
86       i != files.end(); ++i)
87     {
88       remove(*i);
89     }
90 
91   int row = 0;
92   int column = 0;
93   int count = 0;
94 
95   //std::cout << "OFFSET: " << offset << std::endl;
96 
97   for(std::vector<GUIFileButton*>::size_type i = 0 + offset;
98       i < files.size() && count < 9;
99       ++i)
100     {
101       files[i]->set_position(column * (200 + 50) + 50,
102                              row * (150 + 37) + 30);
103       add(files[i]);
104 
105       column += 1;
106       if (column >= 3) // row is full
107         {
108           column = 0;
109           row += 1;
110         }
111       if (row >= 3)
112         return;
113 
114       ++count;
115     }
116 }
117 
118 void
draw_overlay(GraphicContext * gc)119 GUIDirectory::draw_overlay (GraphicContext* gc)
120 {
121 }
122 
123 void
move_up()124 GUIDirectory::move_up ()
125 {
126   if (offset >= 3)
127     offset -= 3;
128 
129   place_components ();
130 }
131 
132 void
move_down()133 GUIDirectory::move_down ()
134 {
135   offset += 3;
136 
137   if (offset >= int(files.size()))
138     offset -= 3;
139 
140   place_components ();
141 }
142 
143 void
wheel_up(int x,int y)144 GUIDirectory::wheel_up (int x, int y)
145 {
146   move_up();
147 }
148 
149 void
wheel_down(int x,int y)150 GUIDirectory::wheel_down (int x, int y)
151 {
152   move_down();
153 }
154 
155 /* EOF */
156