-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibBtn.h
214 lines (200 loc) · 6.78 KB
/
libBtn.h
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Copyright (c) 2024
Sqrt404
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
Every changes of this code should write a document for it.
*/
#include <windows.h>
#define KeyDown(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
#include <bits/stdc++.h>
using namespace std;
namespace Ccs
{
HANDLE consoleMutex;
struct button
{
HANDLE Bthread;
bool stat = false;
int nx, ny, stats = -1, r, g, b;
string text;
HWND hwndbaba;
void (*func)();
void Goto(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a, coord);
}
void rgbb(int br, int bg, int bb)
{
printf("\033[48;2;%d;%d;%dm", br, bg, bb);
}
void rgbw(int wr, int wg, int wb)
{
printf("\033[38;2;%d;%d;%dm", wr, wg, wb);
}
void HideCursor()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
static DWORD WINAPI ButtonThread(LPVOID lpParam)
{
button* self = static_cast<button*>(lpParam);
self->HideCursor();
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
mode &= ~ENABLE_INSERT_MODE;
mode &= ~ENABLE_MOUSE_INPUT;
SetConsoleMode(hStdin, mode);
self->HideCursor();
CONSOLE_FONT_INFO k;
POINT p;
while (self->stat == true)
{
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), false, &k);
GetCursorPos(&p);
ScreenToClient(GetConsoleWindow(), &p);
WaitForSingleObject(consoleMutex, INFINITE);
self->Goto(self->nx, self->ny);
if (p.x / k.dwFontSize.X >= self->nx && p.y / k.dwFontSize.Y >= self->ny && p.x / k.dwFontSize.X <= self->nx + self->text.size() + 1 && p.y / k.dwFontSize.Y <= self->ny)
{
if (self->hwndbaba == GetForegroundWindow() && KeyDown(VK_LBUTTON))
{
self->stats = 2;
self->rgbw(12, 12, 12);
self->rgbb(self->r, self->g, self->b);
cout << "-" << self->text << "-";
while (KeyDown(VK_LBUTTON));
self->Goto(self->nx, self->ny);
self->rgbw(self->r, self->g, self->b);
self->rgbb(12, 12, 12);
cout << "<" << self->text << ">";
self->Goto(self->nx, self->ny);
self->rgbw(255, 255, 255);
self->rgbb(12, 12, 12);
if(self->func != NULL) self->func();
}
else
{
self->stats = 1;
self->rgbw(self->r, self->g, self->b);
self->rgbb(12, 12, 12);
cout << "<" << self->text << ">";
self->rgbw(self->r, self->g, self->b);
self->rgbw(255, 255, 255);
self->rgbb(12, 12, 12);
}
}
else
{
self->rgbw(self->r / 2, self->g / 2, self->b / 2);
self->rgbb(12, 12, 12);
self->stats = 0;
cout << "<" << self->text << ">";
self->rgbw(255, 255, 255);
self->rgbb(12, 12, 12);
}
ReleaseMutex(consoleMutex);
Sleep(50);
}
WaitForSingleObject(consoleMutex, INFINITE);
self->Goto(self->nx, self->ny);
self->stats = -1;
cout << " ";
for(int i = 0; i < self->text.size(); i++) cout << " ";
ReleaseMutex(consoleMutex);
return 0;
}
void startb(void (*f)(), int x, int y, string text1, int a1 = 255, int b1 = 255, int c1 = 255)
{
func = f;
hwndbaba = GetConsoleWindow();
nx = x;
ny = y;
r = a1;
g = b1;
b = c1;
text = text1;
stat = true;
Bthread = CreateThread(NULL, 0, ButtonThread, this, 0, NULL);
}
void stopb()
{
stat = false;
WaitForSingleObject(Bthread, INFINITE);
CloseHandle(Bthread);
}
void ChangeColor(int ar = 255, int ag = 255, int ab = 255)
{
r = ar;
g = ag;
b = ab;
}
void ChangeText(string text1)
{
text = text1;
}
};
void Goto_OutOfStruct(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a, coord);
}
void rgbw_OutOfStruct(int wr, int wg, int wb)
{
printf("\033[38;2;%d;%d;%dm", wr, wg, wb);
}
void ColorSet()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwOutMode;
GetConsoleMode(hOut, &dwOutMode);
dwOutMode |= 0x0004;
SetConsoleMode(hOut, dwOutMode);
rgbw_OutOfStruct(255, 255, 255);
}
void StartSet()
{
consoleMutex = CreateMutex(NULL, FALSE, NULL);
srand(time(0));
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void StartMutex()
{
consoleMutex = CreateMutex(NULL, FALSE, NULL);
}
void mlock()
{
WaitForSingleObject(consoleMutex, INFINITE);
}
void munlock()
{
ReleaseMutex(consoleMutex);
}
}
template<typename... Args>
void printm(Args... args)
{
Ccs::mlock();
int writem[] = {(cout << args, 0)...};
static_cast<void>(writem);
Ccs::munlock();
}