-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathXL_Console.cpp
180 lines (148 loc) · 4.94 KB
/
XL_Console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "XL_Console.h"
#include <cstdio>
#include <cstdarg>
Console *XL_Console::s_pConsole;
static char _tmpStr[512];
void XL_Console::_DefaultConsoleFunc(const std::vector<std::string>& args, void *pUserData)
{
std::string errorStr;
errorStr = "^1'" + args[0] + "' is not a recognized command.";
s_pConsole->Print(errorStr);
}
void XL_Console::_Echo(const std::vector<std::string>& args, void *pUserData)
{
s_pConsole->Print(args[1]);
}
void XL_Console::_CmdList(const std::vector<std::string>& args, void *pUserData)
{
if ( args.size() <= 1 )
s_pConsole->PrintCommands();
else
s_pConsole->PrintCommands(args[1].c_str());
}
void XL_Console::_ConsoleTex(const std::vector<std::string>& args, void *pUserData)
{
if ( args.size() > 1 )
{
s_pConsole->LoadNewBackground( args[1].c_str() );
}
}
void XL_Console::_Help(const std::vector<std::string>& args, void *pUserData)
{
if ( args.size() == 1 )
{
s_pConsole->Print("^8---------------------------- Console Help ------------------------------");
s_pConsole->Print("^6To view the list of commands type: cmdlist");
s_pConsole->Print("^8To get help for a specific command type: help command");
s_pConsole->Print("^8To select previous commands, use the up and down arrows.");
s_pConsole->Print("^8To scroll the text window, use the Page Up and Page Down keys.");
s_pConsole->Print("^8------------------------------------------------------------------------");
}
else
{
s_pConsole->PrintCommandHelp(args[1]);
}
}
bool XL_Console::Init(IDriver3D *pDriver)
{
s_pConsole = xlNew Console(pDriver);
s_pConsole->SetDefaultCommand( _DefaultConsoleFunc );
s_pConsole->AddItem("echo", (void *)_Echo, Console::CTYPE_FUNCTION, "Echo the argument to the console.");
s_pConsole->AddItem("cmdlist", (void *)_CmdList, Console::CTYPE_FUNCTION, "Show all console commands.");
s_pConsole->AddItem("help", (void *)_Help, Console::CTYPE_FUNCTION,
"Show console help. If an argument is passed, it shows help for a specific command. '>help cmdList' will show help for the 'cmdList' command");
s_pConsole->AddItem("con_cmdecho", &s_pConsole->m_bEchoCommands, Console::CTYPE_BOOL,
"Sets whether command echo is enabled (0/1). If enabled, the command line is echoed in the console when enter is pressed.");
s_pConsole->AddItem("g_pause", &s_pConsole->m_bPaused, Console::CTYPE_BOOL, "Pauses the game if set to 1 (0/1).");
s_pConsole->AddItem("con_color", &s_pConsole->m_Color, Console::CTYPE_VEC4, "Console color (RGBA).");
s_pConsole->AddItem("con_tex", (void *)_ConsoleTex, Console::CTYPE_FUNCTION, "Load a new console background texture.");
Input::AddKeyDownCallback( _KeyDownCallback );
Input::AddCharDownCallback( _CharDownCallback );
return (s_pConsole ? true : false);
}
void XL_Console::Destroy()
{
xlDelete s_pConsole;
}
void XL_Console::_KeyDownCallback(int32_t key)
{
if ( s_pConsole == nullptr )
return;
if ( key == XL_BACK )
{
s_pConsole->PassBackspace();
}
else if ( key == XL_RETURN )
{
s_pConsole->PassEnter();
}
else
{
s_pConsole->PassVirtualKey(key);
}
}
void XL_Console::_CharDownCallback(int32_t key)
{
if ( s_pConsole == nullptr )
return;
s_pConsole->PassKey((char)key);
}
void XL_Console::Render()
{
if ( s_pConsole == nullptr )
return;
s_pConsole->Render();
}
void XL_Console::RegisterCmd(const std::string& itemName, void *ptr, Console::ConsoleItemType type, const std::string& itemHelp, void *pUserData)
{
s_pConsole->AddItem(itemName, ptr, type, itemHelp, pUserData);
}
bool XL_Console::IsActive()
{
if ( s_pConsole == nullptr )
return false;
return s_pConsole->IsActive();
}
bool XL_Console::IsChatActive()
{
if ( s_pConsole == nullptr )
return false;
return s_pConsole->IsChatActive();
}
bool XL_Console::IsPaused()
{
if ( s_pConsole == nullptr )
return false;
return s_pConsole->IsPaused();
}
void XL_Console::Print(const std::string& szMsg)
{
if ( s_pConsole == nullptr )
return;
s_pConsole->Print(szMsg);
}
void XL_Console::PrintF(const char *pszString, ...)
{
if ( s_pConsole == nullptr )
return;
va_list args;
va_start(args, pszString);
#if PLATFORM_WIN
_vsnprintf( _tmpStr, 512, pszString, args );
#else
vsnprintf( _tmpStr, 512, pszString, args );
#endif
va_end(args);
s_pConsole->Print(_tmpStr);
}
/*******************************
Plugin API
*******************************/
void Console_RegisterCommand(const char *pszItemName, void *ptr, uint32_t type, const char *pszItemHelp, void *pUserData)
{
XL_Console::RegisterCmd(pszItemName, ptr, (Console::ConsoleItemType)type, pszItemHelp, pUserData);
}
void XL_Console::SetConsoleColor(float fRed, float fGreen, float fBlue, float fAlpha)
{
s_pConsole->m_Color.Set( fRed, fGreen, fBlue, fAlpha );
}