Launcher, based on PickleLauncher
[mupen64plus-pandora.git] / source / mupen64launcher / src / cselector.h
1 /**
2  *  @section LICENSE
3  *
4  *  PickleLauncher
5  *  Copyright (C) 2010-2011 Scott Smith
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  *  @section LOCATION
21  */
22
23 #ifndef CSELECTOR_H
24 #define CSELECTOR_H
25
26 #include "version.h"
27 #include "cbase.h"
28 #include "cconfig.h"
29 #include "cprofile.h"
30 #include "csystem.h"
31
32 using namespace std;
33
34 #define ARG_RESETGUI            "--resetgui"                    /** Flag to reset GUI settings based on the current resolution. */
35 #define ARG_CONFIG              "--config"                      /** Flag to override the config file. */
36 #define DEF_CONFIG              "config.txt"                    /** Default config filename. */
37 #define ARG_PROFILE             "--profile"                     /** Flag to override the profile file. */
38 #define DEF_PROFILE             "profile.txt"                   /** Default profile filename. */
39 #define ARG_ZIPLIST             "--ziplist"                     /** Flag to override the ziplist file. */
40 #define DEF_ZIPLIST             "ziplist.txt"                   /** Default ziplist filename. */
41
42 #define ENTRY_ARROW             "-> "                           /** Ascii fallback for the entry arrow selector. */
43 #define BUTTON_LABEL_ONE_UP     "<"                             /** Ascii text fallback for the one up button label. */
44 #define BUTTON_LABEL_ONE_DOWN   ">"                             /** Ascii text fallback for the one down button label. */
45 #define BUTTON_LABEL_PAGE_UP    "<<"                            /** Ascii text fallback for the page up button label. */
46 #define BUTTON_LABEL_PAGE_DOWN  ">>"                            /** Ascii text fallback for the page down button label. */
47 #define BUTTON_LABEL_DIR_UP     "U"                             /** Ascii text fallback for the directory up button label. */
48 #define BUTTON_LABEL_DIR_DOWN   "D"                             /** Ascii text fallback for the directory down button label. */
49 #define BUTTON_LABEL_ZIP_MODE   "Z"                             /** Ascii text fallback for the zip mode label. */
50 #define BUTTON_LABEL_CONFIG     "Def. Plugins"                  /** Ascii text fallback for the options button label. */
51 #define BUTTON_LABEL_EDIT       "ROM Plugins"                   /** Ascii text fallback for the edit item button label. */
52 #define BUTTON_LABEL_SET_ONE    "Set"                           /** Ascii text fallback for the set button label. */
53 #define BUTTON_LABEL_SET_ALL    "Default"                       /** Ascii text fallback for the default button label. */
54 #define BUTTON_LABEL_BACK       "Back"                          /** Ascii text fallback for the back button label. */
55 #define BUTTON_LABEL_SELECT     "Select"                        /** Ascii text fallback for the select button label. */
56 #define BUTTON_LABEL_QUIT       "Quit"                          /** Ascii text fallback for the quit button label. */
57
58 #define EMPTY_DIR_LABEL         "<no files detected in dir>"    /** Ascii text for display if no files or dirs are detected. */
59 #define EMPTY_ZIP_LABEL         "<no files detected in zip>"    /** Ascii text for display if no files in a zip are detected. */
60
61 #define EVENT_LOOPS_ON          0                               /** Value set to loops to indentify an active event. */
62 #define EVENT_LOOPS_OFF         0x7F                            /** Value set to loops to indentify an inactive event. */
63 #define EVENT_LOOPS             10                              /** Number of loops an event must be active to be detected to be on. */
64 #define IsEventOn(x)    ((EventPressCount.at(x) <= EVENT_LOOPS_ON)  ? true : false) /** Determines if an event is active. */
65 #define IsEventOff(x)   ((EventPressCount.at(x) == EVENT_LOOPS_OFF) ? true : false) /** Determines if an event is inactive. */
66
67 #define MS_PER_SEC              1000                            /** Milliseconds in 1 Second. */
68 #define FRAMES_PER_SEC          60                              /** Frames in 1 Second. */
69 #define FRAME_SKIP_RATIO        4                               /** Maximum frames skip ratio, draw 1 frame for every X number of skipped frames. */
70
71 #define FREE_IMAGE(X)   if (X != NULL) { SDL_FreeSurface(X); X = NULL; } /** Macro for checking and releasing pointers to pixel data. */
72
73 /** @brief Modes of the launcher.
74  */
75 enum MODES_T {
76     MODE_SELECT_ENTRY=0,    /** Main mode for selecting an file to run. */
77     MODE_SELECT_ARGUMENT,   /** Select an argument or command belonging to an entry. */
78     MODE_SELECT_VALUE,      /** Select an value belonging to an argument. */
79     MODE_SELECT_OPTION,     /** Select a Def. Plugin type */
80     MODE_SELECT_PLUGIN,     /** Selct one Def. Plugin */
81     MODE_ROM_OPTION,
82     MODE_ROM_PLUGIN,
83     MODE_TOTAL              /** Number of modes. */
84 };
85
86 /** @brief List positions for the current item and first and last items.
87  */
88 struct item_pos_t {
89     item_pos_t() : first(0), last(0), absolute(0), relative(0), total(0) {};
90     int16_t first;          /** Index of the first item on the display list. */
91     int16_t last;           /** Index of the last item on the display list. */
92     int16_t absolute;       /** Absolute index of the currently selected item in the display list. */
93     int16_t relative;       /** Relative index of the currently selected item in the display list. */
94     int16_t total;          /** Total number of items. */
95 };
96
97 /** @brief The font, color, text for a item in the display list
98  */
99 struct listtext_t {
100     listtext_t() : font(0), color(0), text("") {};
101     int8_t font;            /** Index of the selected font size for the text. */
102     int8_t color;           /** Index of the color for the text. */
103     string text;            /** Text for an item for the display list. */
104 };
105
106 /** @brief This class controls resources, logic for gui, interaction with the user.
107  */
108 class CSelector : public CBase
109 {
110     public:
111         /** Constructor. */
112         CSelector();
113         /** Destructor. */
114         virtual ~CSelector();
115
116         /** @brief Run the main loop of the application.
117          * @param argc : number of arguments.
118          * @param argv : arguments passed to the application.
119          * @return 0 if passed 1 if failed.
120          */
121         int8_t Run( int argc, char** argv );
122
123     private:
124         /** @brief Parse and handle arguments sent to the application.
125          * @param argc : number of arguments.
126          * @param argv : arguments passed to the application.
127          */
128         void    ProcessArguments    ( int32_t argc, char** argv );
129
130         /** @brief Open system interfaces and load configuration information.
131          * @return 0 if passed 1 if failed.
132          */
133         int8_t  OpenResources       ( void );
134
135         /** @brief Close system interfaces and save configuration information.
136          * @param result : result of the application, do not save config files if an error occurs.
137          * @return 0 if passed 1 if failed.
138          */
139         void    CloseResources      ( int8_t result );
140
141         /** @brief Main loop of the application, polls input, runs current mode, and freshes the screen.
142          * @return -1  for no selection otherwise the entry selection number.
143          */
144         int16_t DisplayScreen       ( void );
145
146         /** @brief Check which screen rects a rect overlaps. The marked screen rects will only be updated for the screen.
147          */
148         void    UpdateRect          ( int16_t x, int16_t y, int16_t w, int16_t h );
149
150         /** @brief Handles logic for measuring and counting frame drawing to the screen.
151          */
152         void    UpdateScreen        ( void );
153
154         /** @brief Determines the mode to run based on user input events.
155          */
156         void    SelectMode          ( void );
157
158         /** @brief Mode to display list of input files.
159          * @return 0 if passed 1 if failed.
160          */
161         int8_t  DisplaySelector     ( void );
162
163         /** @brief Loads the preview image if any exists.
164          */
165         void    LoadPreview         ( const string& name );
166
167         /** @brief Moves the current path one directory level up.
168          */
169         void    DirectoryUp         ( void );
170
171         /** @brief Moves the current path one directory level down.
172          */
173         void    DirectoryDown       ( void );
174
175         /** @brief Moves the current path out of a zip file.
176          */
177         void    ZipUp               ( void );
178
179         /** @brief Moves the current path into a zip file.
180          */
181         void    ZipDown             ( void );
182
183         /** @brief Load the display list with text labels and font info.
184          */
185         void    PopulateList        ( void );
186
187         /** @brief Load the display with items with entries.
188          */
189         void    PopModeEntry        ( void );
190
191         /** @brief Load the display with items with entry arguments.
192          */
193         void    PopModeArgument     ( void );
194
195         /** @brief Load the display list with items with argument values.
196          */
197         void    PopModeValue        ( void );
198
199         /** @brief Load the display list with items with type of plugin.
200          */
201         void    PopModeOption       ( void );
202
203         void    PopModeRomOption    ( void );
204
205         /** @brief Load the display list with items with Plugin detail for 1 type.
206          */
207         void    PopModePlugin       ( void );
208
209         void    PopModeRomPlugin    ( void );
210
211         /** @brief Will redetect items for the current mode.
212          */
213         void    RescanItems         ( void );
214
215         /** @brief Draws the names for the items in the display list for the current mode.
216          * @return 0 if passed 1 if failed.
217          */
218         int8_t  DrawNames           ( SDL_Rect& location );
219
220         /** @brief Calculates the indices of the entries that should be displayed based on the current selection
221          * @param pos : reference to the position selections in the current list of entries
222          */
223         void    SelectionLimits     ( item_pos_t& pos );
224
225         /** @brief Will either fill the screen with a color or blit a specified image to the screen
226          */
227         void    DrawBackground      ( void );
228
229         /** @brief Configures the buttons to be displayed on the screen for the current mode
230          * @return 0 if passed 1 if failed
231          */
232         int8_t  ConfigureButtons    ( void );
233
234         /** @brief Draws the buttons to the screen, in either fill color mode or bitmap's
235          * @param location : rect contains the starting coordinates for the buttons
236          */
237         int8_t  DrawButtons         ( SDL_Rect& location );
238
239         /** @brief Draw a button to the screen
240          * @param button : the button to draw (index is defined in EVENT_T)
241          * @param font : the font used for render
242          * @param location : the coordinates and dimensions of the button
243          * @return 0 if passed 1 if failed
244          */
245         int8_t  DrawButton          ( uint8_t button, TTF_Font* font, SDL_Rect& location );
246
247         /** @brief Draws text labels to the screen, like title and author info
248          * @param location : contains the starting coordinates for the text
249          * @return 0 if passed 1 if failed
250          */
251         int8_t  DrawText            ( SDL_Rect& location );
252
253         /** @brief Creates a command script to run the target application
254          * @param selection : the entry selection to use for input to the target application
255          * @return 0 if passed 1 if failed
256          */
257         int8_t  RunExec             ( uint16_t selection );
258
259         /** @brief Collect all input events from the user
260          * @return 0 if passed 1 if failed
261          */
262         int8_t  PollInputs          ( void );
263
264         CSelector(const CSelector &);
265         CSelector & operator=(const CSelector&);
266
267         bool                    Redraw;
268         bool                    SkipFrame;
269         bool                    Rescan;             /**< Set to cause the current directory to be rescaned. */
270         bool                    RefreshList;        /**< Set to cause the current display list to be populated. */
271         bool                    SetOneEntryValue;   /**< Set value for the current entry to the selected value. */
272         bool                    SetAllEntryValue;   /**< Set default for all entries to the selected value. */
273         bool                    TextScrollDir;      /**< Determines the direction of the horizontal scroll, left or right. */
274         bool                    ExtractAllFiles;    /**< True if all files should be extracted from a zip, if false only the selected file is. */
275
276         bool                    DrawState_Title;
277         bool                    DrawState_About;
278         bool                    DrawState_Filter;
279         bool                    DrawState_FilePath;
280         bool                    DrawState_Index;
281         bool                    DrawState_ZipMode;
282         bool                    DrawState_Preview;
283         bool                    DrawState_ButtonL;
284         bool                    DrawState_ButtonR;
285
286         uint8_t                 Mode;               /**< The current mode of the application. */
287         uint8_t                 LastSelectedEntry;  /**< Stores the index of the last seclected entry so scrolling can be restarted. */
288         uint16_t                TextScrollOffset;   /**< Number of pixels to offset the entry text surface when it will blit to the screen. */
289         uint16_t                CurScrollSpeed;     /**< Current number of loops used to decide when to offset the entry text scroll effect. */
290         uint16_t                CurScrollPause;     /**< Current number of loops used to decide when to pause the entry text scroll effect. */
291         uint16_t                ListNameHeight;
292         int16_t                 FramesDrawn;        /**< Counter for the number frames drawn to the screen. */
293         int16_t                 FramesSkipped;      /**< Counter for the number frames not drawn to the screen. */
294         int16_t                 FramesSleep;
295 #if defined(DEBUG)
296         int16_t                 FPSDrawn;           /**< Number frames drawn to the screen per second. */
297         int16_t                 FPSSkip;            /**< Number frames not drawn to the screen per second. */
298         int16_t                 FPSSleep;
299         int32_t                 FrameCountTime;     /**< Tick count from measureing FPS. */
300         int16_t                 LoopTimeAverage;    /**< Average loop time. */
301 #endif
302         int32_t                 FrameEndTime;       /**< Tick count at the end of the frame. */
303         int32_t                 FrameStartTime;     /**< Tick count at the start of the frame. */
304         int16_t                 FrameDelay;         /**< Tick duration of the frame. */
305         SDL_Rect                Mouse;              /**< Stores the absolute position of the mouse pointer. */
306         SDL_Joystick*           Joystick;           /**< SDL surface reference to the first joystick device. */
307         SDL_Surface*            Screen;             /**< SDL surface reference to the screen. */
308         SDL_Surface*            ImageBackground;    /**< SDL surface reference to the background pixel data (optional). */
309         SDL_Surface*            ImagePointer;       /**< SDL surface reference to the pointer pixel data (optional). */
310         SDL_Surface*            ImageSelectPointer; /**< SDL surface reference to the list select pointer pixel data (optional). */
311         SDL_Surface*            ImagePreview;       /**< SDL surface reference to the preview pixel data. */
312         SDL_Surface*            ImageTitle;         /**< SDL surface reference to the title text pixel data. */
313         SDL_Surface*            ImageAbout;         /**< SDL surface reference to the about text pixel data. */
314         SDL_Surface*            ImageFilePath;      /**< SDL surface reference to the about text pixel data. */
315         SDL_Surface*            ImageFilter;        /**< SDL surface reference to the about text pixel data. */
316         SDL_Surface*            ImageIndex;         /**< SDL surface reference to the about text pixel data. */
317         SDL_Surface*            ImageZipMode;       /**< SDL surface reference to the about text pixel data. */
318 #if defined(DEBUG)
319         SDL_Surface*            ImageDebug;         /**< SDL surface reference to the about text pixel data. */
320 #endif
321         vector<SDL_Surface*>    ImageButtons;       /**< SDL surface references to the button's pixel data (optional). */
322         vector<TTF_Font*>       Fonts;              /**< SDL-TTF references to the rendered font in different size. */
323         CConfig                 Config;             /**< The configuration data. */
324         CProfile                Profile;            /**< The extension and entries data. */
325         CSystem                 System;             /**< System specific controls and methods. */
326         string                  ConfigPath;         /**< Contains the file path to the config.txt. */
327         string                  ProfilePath;        /**< Contains the file path to the profile.txt. */
328         string                  ZipListPath;        /**< Contains the path and name of the files that have been unzipped. */
329         vector<bool>            EventReleased;      /**< Collection of the states if a release event was detected. */
330         vector<int8_t>          EventPressCount;    /**< Collection of the loop counts for when an event can act again. */
331         vector<uint8_t>         ButtonModesLeft;    /**< Collection of the state of the buttons on the left side. */
332         vector<uint8_t>         ButtonModesRight;   /**< Collection of the state of the buttons on the right side. */
333         vector<item_pos_t>      DisplayList;        /**< Collection of the positions and limits of list selection for each mode. */
334         vector<string>          LabelButtons;       /**< Collection of text labels for the buttons. */
335         vector<listtext_t>      ListNames;          /**< Collection of text and font information for the entry currently displayed. */
336         vector<listitem_t>      ItemsEntry;         /**< Collection of directories and filenames detected in the current path. */
337         vector<listoption_t>    ItemsArgument;      /**< Collection of options for an entry or config. */
338         vector<listitem_t>      ItemsDefPlugin;     /**< Collection of Plugin for selected plugin type. */
339         uint8_t                 WhichPlugin;        /**< Wich Plugin is selected */
340         vector<listoption_t>    ItemsRomOption;     /**< Collection of options for an entry or config. */
341         vector<listitem_t>      ItemsRomPlugin;     /**< Collection of Plugin for selected plugin type. */
342         uint8_t                 WhichRomPlugin;     /**< Wich Plugin is selected */
343         vector<string>          ItemsValue;         /**< Collection of values for an option. */
344         vector<SDL_Rect>        RectEntries;        /**< Collection of position rects for the displayed entries. */
345         vector<SDL_Rect>        RectButtonsLeft;    /**< Collection of position rects for the displayed buttons on left. */
346         vector<SDL_Rect>        RectButtonsRight;   /**< Collection of position rects for the displayed buttons on right. */
347         vector<SDL_Rect>        ScreenRectsDirty;   /**< Collection of rects for the areas of the screen that will be updated. */
348 };
349
350 #endif // CSELECTOR_H