-
Notifications
You must be signed in to change notification settings - Fork 2
/
FEConsoleWindow.cpp
197 lines (159 loc) · 5.14 KB
/
FEConsoleWindow.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "FEConsoleWindow.h"
using namespace FocalEngine;
FEConsoleWindow::FEConsoleWindow(std::function<void(void* UserData)> MainFunc, void* UserData)
{
bConsoleActive = true;
UserConsoleMainFunc = MainFunc;
UserConsoleMainFuncData = UserData;
bConsoleInitializationStarted = true;
ConsoleThreadHandler = std::thread(&FEConsoleWindow::ConsoleMainFunc, this);
ConsoleThreadHandler.detach();
}
HWND FEConsoleWindow::GetHandle() const
{
return ConsoleWindow;
}
void FEConsoleWindow::ConsoleMainFunc()
{
// Allocate a console
AllocConsole();
// Get a handle to the console window
ConsoleWindow = GetConsoleWindow();
bConsoleInitializationStarted = false;
// Redirect standard I/O to the console
FILE* pCout;
freopen_s(&pCout, "CONOUT$", "w", stdout);
FILE* pCin;
freopen_s(&pCin, "CONIN$", "r", stdin);
UserConsoleMainFunc(UserConsoleMainFuncData);
fclose(pCout);
fclose(pCin);
FreeConsole();
bConsoleActive = false;
}
bool FEConsoleWindow::DisableCloseButton() const
{
if (ConsoleWindow == nullptr)
return false;
// Get the system menu of the console window
HMENU SysMenu = GetSystemMenu(ConsoleWindow, FALSE);
// Disable the close option
if (SysMenu != NULL)
{
RemoveMenu(SysMenu, SC_CLOSE, MF_BYCOMMAND);
return true;
}
return false;
}
void FEConsoleWindow::WaitForCreation()
{
if (!bConsoleInitializationStarted)
return;
while (ConsoleWindow == nullptr)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
bool FEConsoleWindow::Hide() const
{
if (ConsoleWindow == nullptr)
return false;
ShowWindow(ConsoleWindow, SW_HIDE);
return true;
}
bool FEConsoleWindow::Show() const
{
if (ConsoleWindow == nullptr)
return false;
ShowWindow(ConsoleWindow, SW_SHOW);
return true;
}
bool FEConsoleWindow::IsHidden() const
{
if (ConsoleWindow == nullptr)
return false;
WINDOWPLACEMENT Placement;
Placement.length = sizeof(WINDOWPLACEMENT);
if (GetWindowPlacement(ConsoleWindow, &Placement))
{
if (Placement.showCmd == SW_HIDE)
return true;
}
return false;
}
bool FEConsoleWindow::SetTitle(const std::string Title) const
{
if (ConsoleWindow == nullptr)
return false;
SetConsoleTitleA(Title.c_str());
return true;
}
WORD FEConsoleWindow::RGBToConsoleColor(int R, int G, int B) const
{
// Define the basic console colors with their RGB values
static const std::tuple<int, int, int, WORD> consoleColors[] = {
{0, 0, 0, 0}, // Black
{0, 0, 128, FOREGROUND_BLUE}, // Dark Blue
{0, 128, 0, FOREGROUND_GREEN}, // Dark Green
{0, 128, 128, FOREGROUND_GREEN | FOREGROUND_BLUE}, // Dark Cyan
{128, 0, 0, FOREGROUND_RED}, // Dark Red
{128, 0, 128, FOREGROUND_RED | FOREGROUND_BLUE}, // Dark Magenta
{128, 128, 0, FOREGROUND_RED | FOREGROUND_GREEN}, // Dark Yellow
{192, 192, 192, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE}, // Light Gray
{128, 128, 128, FOREGROUND_INTENSITY}, // Dark Gray (Bright Black)
{0, 0, 255, FOREGROUND_BLUE | FOREGROUND_INTENSITY}, // Bright Blue
{0, 255, 0, FOREGROUND_GREEN | FOREGROUND_INTENSITY}, // Bright Green
{0, 255, 255, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY}, // Bright Cyan
{255, 0, 0, FOREGROUND_RED | FOREGROUND_INTENSITY}, // Bright Red
{255, 0, 255, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY}, // Bright Magenta
{255, 255, 0, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY}, // Bright Yellow
{255, 255, 255, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY} // White
};
WORD nearestColor = 0; // Default to black if no match is found
double minDistance = DBL_MAX;
for (const auto& color : consoleColors)
{
// Calculate the Euclidean distance between the two colors
double distance = std::sqrt(std::pow(std::get<0>(color) - R, 2) +
std::pow(std::get<1>(color) - G, 2) +
std::pow(std::get<2>(color) - B, 2));
if (distance < minDistance)
{
minDistance = distance;
nearestColor = std::get<3>(color);
}
}
return nearestColor;
}
std::vector<char> FEConsoleWindow::GetConsoleTextColor() const
{
// Get the current console screen buffer info, which contains the color attributes
CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ConsoleInfo);
// Extract the foreground color
WORD ColorWord = ConsoleInfo.wAttributes & 0x0F;
// Map the console foreground color to a rough RGB representation
// Note: This mapping is quite arbitrary and simplistic
std::vector<char> ColorRGB(3, 0); // Initialize RGB vector with 0s
// Mapping the color attributes to RGB values
if (ColorWord & FOREGROUND_RED)
ColorRGB[0] = static_cast<char>(255); // R
if (ColorWord & FOREGROUND_GREEN)
ColorRGB[1] = static_cast<char>(255); // G
if (ColorWord & FOREGROUND_BLUE)
ColorRGB[2] = static_cast<char>(255); // B
// Adjust brightness based on FOREGROUND_INTENSITY
if (!(ColorWord & FOREGROUND_INTENSITY))
{
for (auto& Component : ColorRGB)
{
Component = Component > 0 ? 128 : 0; // Dim the color if intensity is not set
}
}
return ColorRGB;
}
void FEConsoleWindow::SetNearestConsoleTextColor(int R, int G, int B) const
{
WORD Color = RGBToConsoleColor(R, G, B);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Color);
}