]> icculus.org git repositories - btb/d2x.git/blob - include/CON_console.h
function prototypes
[btb/d2x.git] / include / CON_console.h
1 #ifndef CON_console_H
2 #define CON_console_H
3
4 /*! \mainpage
5  
6 \section intro Introduction
7 SDL_Console is a console that can be added to any SDL application. It is similar to Quake and other games consoles.
8 A console is meant to be a very simple way of interacting with a program and executing commands. You can also have 
9 more than one console at a time. 
10  
11 \section docs Documentation
12 For a detailed description of all functions see \ref CON_console.h. Remark that functions that have the mark "Internal" 
13 are only used internally. There's not much use of calling these functions.
14  
15 Have Fun!
16  
17 \author Garett Banuk <mongoose@mongeese.org> (Original Version)
18 \author Clemens Wacha <reflex-2000@gmx.net> (Version 2.x, Documentation)
19 \author Boris Lesner <talanthyr@tuxfamily.org> (Package Maintainer)
20 \author Bradley Bell <btb@icculus.org> (Descent Version)
21 */
22
23
24 #include "gr.h"
25 #include "key.h"
26
27 //! Cut the buffer line if it becomes longer than this
28 #define CON_CHARS_PER_LINE   128
29 //! Cursor blink frequency in ms
30 #define CON_BLINK_RATE       500
31 //! Border in pixels from the most left to the first letter
32 #define CON_CHAR_BORDER      4
33 //! Spacing in pixels between lines
34 #define CON_LINE_SPACE       1
35 //! Default prompt used at the commandline
36 #define CON_DEFAULT_PROMPT      "]"
37 //! Scroll this many lines at a time (when pressing PGUP or PGDOWN)
38 #define CON_LINE_SCROLL 2
39 //! Indicator showing that you scrolled up the history
40 #define CON_SCROLL_INDICATOR "^"
41 //! Cursor shown if we are in insert mode
42 #define CON_INS_CURSOR "_"
43 //! Cursor shown if we are in overwrite mode
44 #define CON_OVR_CURSOR "|"
45 //! Defines the default hide key (Hide() the console if pressed)
46 #define CON_DEFAULT_HIDEKEY     KEY_ESC
47 //! Defines the opening/closing speed
48 #define CON_OPENCLOSE_SPEED 25
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54         enum {
55             CON_CLOSED, //! The console is closed (and not shown)
56             CON_CLOSING,        //! The console is still open and visible but closing
57             CON_OPENING,        //! The console is visible and opening but not yet fully open
58             CON_OPEN    //! The console is open and visible
59         };
60
61         /*! This is a struct for each consoles data */
62         typedef struct console_information_td {
63                 int Visible;                    //! enum that tells which visible state we are in CON_HIDE, CON_SHOW, CON_RAISE, CON_LOWER
64                 int RaiseOffset;                        //! Offset used when scrolling in the console
65                 int HideKey;                    //! the key that can hide the console
66                 char **ConsoleLines;            //! List of all the past lines
67                 char **CommandLines;            //! List of all the past commands
68                 int TotalConsoleLines;          //! Total number of lines in the console
69                 int ConsoleScrollBack;          //! How much the user scrolled back in the console
70                 int TotalCommands;              //! Number of commands in the Back Commands
71                 int LineBuffer;                 //! The number of visible lines in the console (autocalculated)
72                 int VChars;                     //! The number of visible characters in one console line (autocalculated)
73                 char* Prompt;                   //! Prompt displayed in command line
74                 char Command[CON_CHARS_PER_LINE];       //! current command in command line = lcommand + rcommand
75                 char RCommand[CON_CHARS_PER_LINE];      //! left hand side of cursor
76                 char LCommand[CON_CHARS_PER_LINE];      //! right hand side of cursor
77                 char VCommand[CON_CHARS_PER_LINE];      //! current visible command line
78                 int CursorPos;                  //! Current cursor position in CurrentCommand
79                 int Offset;                     //! CommandOffset (first visible char of command) - if command is too long to fit into console
80                 int InsMode;                    //! Insert or Overwrite characters?
81                 grs_canvas *ConsoleSurface;     //! Canvas of the console
82                 grs_screen *OutputScreen;       //! This is the screen to draw the console to
83                 grs_bitmap *BackgroundImage;    //! Background image for the console
84                 grs_bitmap *InputBackground;    //! Dirty rectangle to draw over behind the users background
85                 int DispX, DispY;               //! The top left x and y coords of the console on the display screen
86 #if 0
87                 unsigned char ConsoleAlpha;     //! The consoles alpha level
88 #endif
89                 int CommandScrollBack;          //! How much the users scrolled back in the command lines
90                 void(*CmdFunction)(struct console_information_td *console, char* command);      //! The Function that is executed if you press <Return> in the console
91                 char*(*TabFunction)(char* command);     //! The Function that is executed if you press <Tab> in the console
92         }
93         ConsoleInformation;
94
95         /*! Takes keys from the keyboard and inputs them to the console if the console isVisible().
96                 If the event was not handled (i.e. WM events or unknown ctrl- or alt-sequences) 
97                 the function returns the event for further processing. */
98         int CON_Events(int event);
99         /*! Makes the console visible */
100         void CON_Show(ConsoleInformation *console);
101         /*! Hides the console */
102         void CON_Hide(ConsoleInformation *console);
103         /*! Returns 1 if the console is visible, 0 else */
104         int CON_isVisible(ConsoleInformation *console);
105         /*! Internal: Updates visible state. Used in CON_DrawConsole() */
106         void CON_UpdateOffset(ConsoleInformation* console);
107         /*! Draws the console to the screen if it isVisible()*/
108         void CON_DrawConsole(ConsoleInformation *console);
109         /*! Initializes a new console */
110         ConsoleInformation *CON_Init(grs_font *Font, grs_screen *DisplayScreen, int lines, int x, int y, int w, int h);
111         /*! Calls CON_Free */
112         void CON_Destroy(ConsoleInformation *console);
113         /*! Frees all the memory loaded by the console */
114         void CON_Free(ConsoleInformation *console);
115         /*! printf for the console */
116         void CON_Out(ConsoleInformation *console, const char *str, ...);
117 #if 0
118         /*! Sets the alpha channel of an SDL_Surface to the specified value (0 - transparend,
119                 255 - opaque). Use this function also for OpenGL. */
120         void CON_Alpha(ConsoleInformation *console, unsigned char alpha);
121         /*! Internal: Sets the alpha channel of an SDL_Surface to the specified value.
122                 Preconditions: the surface in question is RGBA. 0 <= a <= 255, where 0 is transparent and 255 opaque */
123         void CON_AlphaGL(SDL_Surface *s, int alpha);
124         /*! Sets a background image for the console */
125 #endif
126         int CON_Background(ConsoleInformation *console, grs_bitmap *image);
127         /*! Sets font info for the console */
128         void CON_Font(ConsoleInformation *console, grs_font *font, int fg, int bg);
129         /*! Changes current position of the console */
130         void CON_Position(ConsoleInformation *console, int x, int y);
131         /*! Changes the size of the console */
132         int CON_Resize(ConsoleInformation *console, int x, int y, int w, int h);
133         /*! Beams a console to another screen surface. Needed if you want to make a Video restart in your program. This
134                 function first changes the OutputScreen Pointer then calls CON_Resize to adjust the new size. */
135         int CON_Transfer(ConsoleInformation* console, grs_screen* new_outputscreen, int x, int y, int w, int h);
136         /*! Give focus to a console. Make it the "topmost" console. This console will receive events
137                 sent with CON_Events() */
138         void CON_Topmost(ConsoleInformation *console);
139         /*! Modify the prompt of the console */
140         void CON_SetPrompt(ConsoleInformation *console, char* newprompt);
141         /*! Set the key, that invokes a CON_Hide() after press. default is ESCAPE and you can always hide using
142                 ESCAPE and the HideKey. compared against event->key.keysym.sym !! */
143         void CON_SetHideKey(ConsoleInformation *console, int key);
144         /*! Internal: executes the command typed in at the console (called if you press ENTER)*/
145         void CON_Execute(ConsoleInformation *console, char* command);
146         /*! Sets the callback function that is called if a command was typed in. The function could look like this:
147                 void my_command_handler(ConsoleInformation* console, char* command). @param console: the console the command
148                 came from. @param command: the command string that was typed in. */
149         void CON_SetExecuteFunction(ConsoleInformation *console, void(*CmdFunction)(ConsoleInformation *console2, char* command));
150         /*! Sets the callback tabulator completion function. char* my_tabcompletion(char* command). If Tab is
151                 pressed, the function gets called with the already typed in command. my_tabcompletion then checks if if can
152                 complete the command or if it should display a list of all matching commands (with CON_Out()). Returns the 
153                 completed command or NULL if no completion was made. */
154         void CON_SetTabCompletion(ConsoleInformation *console, char*(*TabFunction)(char* command));
155         /*! Internal: Gets called when TAB was pressed */
156         void CON_TabCompletion(ConsoleInformation *console);
157         /*! Internal: makes newline (same as printf("\n") or CON_Out(console, "\n") ) */
158         void CON_NewLineConsole(ConsoleInformation *console);
159         /*! Internal: shift command history (the one you can switch with the up/down keys) */
160         void CON_NewLineCommand(ConsoleInformation *console);
161         /*! Internal: updates console after resize etc. */
162         void CON_UpdateConsole(ConsoleInformation *console);
163
164
165         /*! Internal: Default Execute callback */
166         void Default_CmdFunction(ConsoleInformation *console, char* command);
167         /*! Internal: Default TabCompletion callback */
168         char* Default_TabFunction(char* command);
169
170         /*! Internal: draws the commandline the user is typing in to the screen. called by update? */
171         void DrawCommandLine();
172
173         /*! Internal: Gets called if you press the LEFT key (move cursor left) */
174         void Cursor_Left(ConsoleInformation *console);
175         /*! Internal: Gets called if you press the RIGHT key (move cursor right) */
176         void Cursor_Right(ConsoleInformation *console);
177         /*! Internal: Gets called if you press the HOME key (move cursor to the beginning
178         of the line */
179         void Cursor_Home(ConsoleInformation *console);
180         /*! Internal: Gets called if you press the END key (move cursor to the end of the line*/
181         void Cursor_End(ConsoleInformation *console);
182         /*! Internal: Called if you press DELETE (deletes character under the cursor) */
183         void Cursor_Del(ConsoleInformation *console);
184         /*! Internal: Called if you press BACKSPACE (deletes character left of cursor) */
185         void Cursor_BSpace(ConsoleInformation *console);
186         /*! Internal: Called if you type in a character (add the char to the command) */
187         void Cursor_Add(ConsoleInformation *console, int event);
188
189         /*! Internal: Called if you press Ctrl-C (deletes the commandline) */
190         void Clear_Command(ConsoleInformation *console);
191         /*! Internal: Called if you press Ctrl-L (deletes the History) */
192         void Clear_History(ConsoleInformation *console);
193
194         /*! Internal: Called if you press UP key (switches through recent typed in commands */
195         void Command_Up(ConsoleInformation *console);
196         /*! Internal: Called if you press DOWN key (switches through recent typed in commands */
197         void Command_Down(ConsoleInformation *console);
198
199 #ifdef __cplusplus
200 };
201 #endif
202
203 #endif
204
205