1 //-----------------------------------------------------------------------------
2 // Console
3 //-----------------------------------------------------------------------------
4 
5 #ifndef __CONSOLE_H__
6 #define __CONSOLE_H__
7 
8 #include "shader.h"
9 
10 #define C_NMESSAGELINES       8     /**< max number of message lines */
11 #define C_NCOMMHISTORYLINES   1024  /**< max number of lines of the commands history */
12 
13 /**
14  * Console states.
15  */
16 typedef enum
17 {
18   OPEN,             /**< opened console */
19   OPENING,          /**< onening console */
20   CLOSED,           /**< closed console */
21   CLOSING           /**< closing console */
22 } enum_ConsoleStates;
23 
24 /**
25  * Vertical scrolling directions.
26  */
27 typedef enum
28 {
29   UP,               /**< scroll up */
30   DOWN,             /**< scroll down */
31   TOP,              /**< jump tp top */
32   BOTTOM            /**< jump tp bottom */
33 } enum_ConsoleScrollDir;
34 
35 /**
36  * Horizontal cursor scrolling directions
37  */
38 typedef enum
39 {
40   LEFT = -1,                                /**< move left */
41   RIGHT = 1,                                /**< move right */
42   C_EXTREM_LEFT = -(CONSOLE_LINELENGTH+1),  /**< go to extrem left */
43   C_EXTREM_RIGHT = CONSOLE_LINELENGTH+1     /**< go to extrem right */
44 } enum_ConsoleCursorScroll;
45 
46 /**
47 * Define a console line.
48 */
49 class ConsLine
50 {
51   private:
52     char buffer[CONSOLE_LINELENGTH];
53 
54   public:
55     char content[CONSOLE_LINELENGTH];       /**< Line content. */
56     int length;                             /**< Line length. */
57     int nlines;                 /**< Number of lines that content takes when displayed. */
58     int *breakPos;              /**< Postions of line breaks. */
59     int breakPosSize;           /**< Break position table size. */
60 
61     /**
62     * Initialize the line.
63     */
64     void Init(void);
65 
66     /**
67     * Destroy the line.
68     */
69     void Shut(void);
70 
71     /**
72     * Reinitialize the line.
73     * Don't realloc breakPos table.
74     */
75     void ReInit(void);
76 
77     /**
78     * Update the line.
79     * The line update itself in function of given line length.
80     * @param linelen The line length.
81     * @return The number of lines occupied by the line
82     */
83     int Update(int linelen);
84 
85     /**
86     * Set the line content.
87     * @param c The line content.
88     */
89     void SetContent(const char *c);
90 
91     /**
92     * Add content to line.
93     * @param c The content to add at current line.
94     * @return The new length of content, and 0 if new string couldn't be added.
95     */
96     int AddContent(const char *c);
97 
98     /**
99     * Get the line content.
100     * @param p The number of desired content line.
101     * @return A pointer to a null ended line. If parameter is negative or bigger
102     * than the number of lines, the full line content is returned.
103     */
104     char *GetContent(int p = -1);
105 };
106 
107 /**
108  * Console class.
109  * The console appears on the top of the screen and can be used to enter
110  * commands. It is also used to display messages or variables values.
111  * @bug The engine stops sometimes without any error message when the user
112  *      press a key in the console.
113  * @todo Merge the <a href="http://www.calodox.org/morbac/console">console
114  *       library project</a> in cake console
115  */
116 class Console
117 {
118   private:
119     float cursorSpeed;              /**< blinking cursor speed (number of blink per second) */
120     char* prompt;                   /**< prompt symbol */
121     char cursorSymbol;              /**< cursor symbol */
122 
123     bool movingConsole;
124 
125     char MessageLines[C_NMESSAGELINES][CONSOLE_LINELENGTH];   /**< console lines */
126     float MessageLife[C_NMESSAGELINES];                       /**< message lifetime */
127     bool startNewMessageLine;         /**< start a new message line */
128 
129     ConsLine *ConsoleLines;           /**< console content */
130     long NbrUsedLines;                /**< number of console lines */
131     /**
132      * Number of lines that covers the console lines.
133      * During rendering, lines can be splitted in more than only one line in console.
134      * This variable stores the number of lines that are necessary to display all the
135      * console.
136      */
137     int NbrTrueLines;
138     int MaxTextLineLength;
139     bool startNewLine;                /**< defines if a new line must be started for insert function */
140 
141     long NbrAllocatedLines;           /**< number of allocated lines */
142 
143     int cursorPos;                    /**< cursor position (0 = at the end of line) */
144     int scrollVal;                    /**< scrolling cursor value */
145 
146     enum_ConsoleStates state;         /**< console state */
147 
148     int height;                       /**< console height */
149     int width;                        /**< console width */
150     int leftPos;                      /**< console left position */
151     int topPos;                       /**< console top position */
152     int NbrMaxLines;                  /**< max number of displayable lines */
153 
154     /**
155      * Recalculates the number of displayable lines in the console.
156      * The function automatically calculates the number of lines that can
157      * be displayed in the console, in function of console height.
158      */
159     void Recalculate_NLines(void);
160 
161     int VScrollY;                     /**< vertical scroll position */
162 
163     bool ConsoleIsMaximized;          /**< flying window state */
164     int HBackup, WBackup, LBackup, TBackup;   /**< console window dimensions backup */
165 
166     char title[CONSOLE_LINELENGTH];
167 
168     int font;                   /**< Font shader */
169     int back;                   /**< Back shader */
170     int titlebar;               /**< TitleBar shader */
171     int scrollUp;               /**< ScrollUp button shader */
172     int scrollDown;             /**< ScrollDown button shader */
173     int scroll;                 /**< Scroll cursor shader */
174     int resize;                 /**< Resize logo shader */
175     int maximise;               /**< Maximize shader */
176     int reduce;                 /**< Reduce shader */
177     int close;                  /**< Close shader */
178 
179     int ConsoleFontSizeX, ConsoleFontSizeY,
180       MiniConsoleFontSizeX, MiniConsoleFontSizeY; /**< console font size */
181     int fontRows, fontCols;
182     float Coeff;
183 
184     void AddLine(void);                   /**< Add a new line to console. */
185     void AddMessageLine(char* s);         /**< Add a line to the messages list. */
186     void AddToLastMessageLine(char *s);   /**< Add content to the last message line. */
187     void UpdateMessageLines(void);        /**< Updates each message lines */
188 
189     GLfloat openSpeed;                    /**< opening console speed */
190     GLfloat closeSpeed;                   /**< closing console speed */
191     bool enableOpeningClosingAnimations;  /**< activate opening and closing animation */
192 
193   public:
194     ShaderManager shaders;
195 
196     bool isActive;                        /**< is console active */
197 
198     bool showMessages;              /**< display messages (mini console) when console is inactive */
199     bool addToMessages;             /**< add console messages to messages list (mini console) */
200     GLfloat messageMaxLife;         /**< messages lifetime */
201 
202     bool autoCut;                   /**< enable automatic lines autocut */
203 
204     bool showVScroll;               /**< display the vertical scroll */
205     bool showTitleBar;              /**< display the title bar */
206     int titleBarHeight;             /**< title bar height */
207 
208     GLfloat ActiveBorderColor[4];   /**< border color for active console */
209     GLfloat InactiveBorderColor[4]; /**< border color for inactive console */
210 
211     Console(void);
212     ~Console(void);
213 
214     void Init(void);
215     void Shut(void);
216 
217     void Update(void);
218     void Render(void);
219 
220     void Clear(void);
221     void Insert(const char* s, ...);      /**< Add text to current line */
222     void Insertln(const char* s, ...);    /**< Add a text line to console */
223 
224     // Command line management
225     void ReInitCurrentCommand(void);
226     char* GetCurrentCommand(void);
227     void AddChar(char c);
228     void DelChar(void);
229     void SetCurrentCommand(char* s, ...);
230     int GetNbrUsedLines(void);
231 
232     // Title, prompt
233     void SetTitle(char *t);
234     void SetPrompt(char *p);
235     char* GetPrompt(void);
236 
237     // Opening and closing
238     void Open(void);
239     void Close(void);
240     enum_ConsoleStates GetState(void);
241     enum_ConsoleStates ToggleState(void);
242     void SetState(enum_ConsoleStates s);
243 
244     // Scrolling
245     void ScrollConsole(enum_ConsoleScrollDir dir);
246     void SetVScrollYPos(int y, bool center = false);
247 
248     // Type management
249     void ToggleType(void);
250     void SetType(bool console_type);
251 
252     // Resizing and positioning
253     void Resize(int w, int h);
254     void Maximize(void);
255     void Unmaximize(void);
256     bool IsMaximized(void);
257     void ToggleMaximisation(void);
258     int GetWidth(void);
259     int GetHeight(void);
260 
261     // Position management
262     void SetTopPos(int top, int test = 1);
263     void SetLeftPos(int left, int test = 1);
264     int GetLeft(void);
265     int GetTop(void);
266 
267     // Cursor management
268     void MoveCursor(enum_ConsoleCursorScroll d);
269     void SetCursorSpeed(float f);
270     void SetCursorSymbol(char c);
271 
272     // Font sizing
273     void SetFontSize(int xsize = -1, int ysize = -1, int n = 1);
274     int GetFontSizeX(int n = 1);
275     int GetFontSizeY(int n = 1);
276     void SetCoeff(float c);
277 
278     // Console shaders
279     void SetFont(int f, int rows = -1, int cols = -1);
280     void SetBack(int b);
281     void SetClose(int c);
282     void SetMaximise(int m);
283     void SetReduce(int r);
284     void SetTitleBar(int t);
285     void SetScrollUp(int s);
286     void SetScrollDown(int s);
287     void SetScroll(int s);
288     void SetResize(int r);
289 
290     int GetFont(void);
291 };
292 
293 #endif  /* __CONSOLE_H__ */
294