-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenu.cpp
More file actions
334 lines (280 loc) · 9.75 KB
/
Copy pathMenu.cpp
File metadata and controls
334 lines (280 loc) · 9.75 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "Menu.h"
#undef max
#undef min
ImVec4 Menu::ARGBToImVec4(const uint32_t& color)
{
const float
a = (color >> 24 & 0xFF) / 255.0f,
r = (color >> 16 & 0xFF) / 255.0f,
g = (color >> 8 & 0xFF) / 255.0f,
b = (color & 0xFF) / 255.0f;
return ImVec4(r, g, b, a);
}
uint32_t Menu::ImVec4ToARGB(const ImVec4& color)
{
const uint32_t
a = static_cast<uint32_t>(color.w * 255.0f) & 0xFF,
r = static_cast<uint32_t>(color.x * 255.0f) & 0xFF,
g = static_cast<uint32_t>(color.y * 255.0f) & 0xFF,
b = static_cast<uint32_t>(color.z * 255.0f) & 0xFF;
return (a << 24) | (r << 16) | (g << 8) | b;
}
void Menu::Render()
{
const auto& mSelectedLine = Chat::getInstance().mSelectedEntry;
const auto& io = ImGui::GetIO();
if (IsEditLineActive()) Chat::setSampCursorMode(2);
const int cursorMode = Chat::getSampCursorMode();
const bool cursorEnabled = cursorMode >= 2 && cursorMode <= 3;
// Stop render
if (Chat::isGTAMenuActive()) return;
if (!cursorEnabled && !IsPopupActive()) return;
if (mSelectedLine > -1)
{
if (ImGui::Begin("##SelectedLine", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground))
{
auto* position = Chat::getInstance().getChatEntryManager().getByEntryId(mSelectedLine);
if (!position)
return;
const auto& rect = position->rect;
const auto& charHeight = static_cast<size_t>(Chat::getInstance().pChat->m_nCharHeight);
const auto
xPos = static_cast<float>(rect.x1) - ImGui::CalcTextSize("> ").x,
yPos = static_cast<float>(rect.y1) + std::ceil(static_cast<float>(rect.y2 - rect.y1) / 2.0f) - Chat::getSampFontSize() + (Chat::getSampFontSizeParam() + static_cast<float>(charHeight) / 6);
ImGui::SetWindowPos(ImVec2(xPos, yPos));
ImGui::SetCursorPosX(0.0f);
ImGui::Text(">");
ImGui::End();
}
}
const auto& chat = Chat::getInstance();
if (ImGui::Begin("##PopupWrapper", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoTitleBar))
{
if (this->openPopup && !ImGui::IsPopupOpen("TextContextMenu"))
{
ImGui::OpenPopup("TextContextMenu");
this->openPopup = false;
this->popupActive = true;
} else if (this->popupActive && (!ImGui::IsPopupOpen("TextContextMenu") || mSelectedLine < 0) )
{
ClosePopup();
}
if (ImGui::BeginPopupContextItem("TextContextMenu"))
{
if (chat.mSelectedEntry != -1)
{
ImGui::PushItemWidth(350.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(12.0f, 12.0f));
ImGui::PushStyleColor(ImGuiCol_PopupBg, ImVec4(0.12f, 0.12f, 0.12f, 0.9f));
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
if (ImGui::MenuItem("Copy"))
{
const bool with_colors = (GetKeyState(VK_SHIFT) & 0x8000) != 0; // Hold shift
const auto& entry = chat.pChat->m_entry[chat.mSelectedEntry];
std::string text;
if (entry.m_prefixColor != 0 && entry.m_szPrefix[0] != '\0')
{
text += entry.m_szPrefix;
text += " ";
}
text += entry.m_szText;
if (!with_colors)
{
for (size_t pos = 0; pos + 7 < text.size();)
{
if (text[pos] == '{' && text[pos + 7] == '}')
{
bool is_hex = true;
for (size_t i = 1; i < 7; ++i)
{
if (!std::isxdigit(static_cast<unsigned char>(text[pos + i])))
{
is_hex = false;
break;
}
}
if (is_hex)
{
text.erase(pos, 8);
continue;
}
}
++pos;
}
}
ImGui::SetClipboardText(Chat::convertToUTF8(text).c_str());
}
if (ImGui::MenuItem(u8"Delete"))
{
Chat::sampDeleteChatLine(chat.mSelectedEntry);
}
if (ImGui::MenuItem("Edit"))
{
const auto& pChat = Chat::getInstance().pChat;
auto& chatEntry = pChat->m_entry[chat.mSelectedEntry];
// text
editLineBuffer[0] = '\0';
strcat(editLineBuffer, chat.convertToUTF8(chatEntry.m_szText).c_str());
editLineColor = ARGBToImVec4(chatEntry.m_textColor);
// prefix
editPrefixBuffer[0] = '\0';
strcat(editPrefixBuffer, chat.convertToUTF8(chatEntry.m_szPrefix).c_str());
editPrefixColor = ARGBToImVec4(chatEntry.m_prefixColor);
editLineActive = true;
}
if (ImGui::MenuItem("Clear"))
{
Chat::sampDeleteChatLineAll();
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
ImGui::PopStyleColor(2);
}
ImGui::EndPopup();
ImGui::End();
}
}
if (IsEditLineActive() && chat.mSelectedEntry > -1)
{
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.12f, 0.12f, 0.12f, 0.9f));
if (ImGui::Begin("##EditLine", &editLineActive, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground))
{
ImGui::SetWindowPos(ImVec2((io.DisplaySize.x - ImGui::GetWindowWidth()) * 0.5f,
(io.DisplaySize.y - ImGui::GetWindowHeight()) * 0.5f));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8.0f, 4.0f));
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(4.0f, 12.0f));
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
ImGui::PushItemWidth(io.DisplaySize.x * (750.0f / 1920));
const bool hasPrefix = (chat.pChat->m_entry[mSelectedLine].m_prefixColor != 0);
if (hasPrefix)
{
// ----- Prefix color button -----
if (ImGui::ColorButton("##PrefixColorBtn", editPrefixColor, ImGuiColorEditFlags_NoTooltip))
{
ImGui::OpenPopup("##PrefixColorPopup");
}
if (ImGui::BeginPopup("##PrefixColorPopup"))
{
if (ImGui::ColorPicker3("##PrefixPicker",
(float*)&editPrefixColor,
ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoSmallPreview))
{
auto& entry = chat.pChat->m_entry[mSelectedLine];
entry.m_prefixColor = ImVec4ToARGB(editPrefixColor);
chat.chatUpdate();
}
ImGui::EndPopup();
}
ImGui::SameLine(0.0f, 5.0f);
// ----- Prefix text -----
if (ImGui::InputText("##PrefixText", editPrefixBuffer, IM_ARRAYSIZE(editPrefixBuffer)))
{
auto& entry = chat.pChat->m_entry[mSelectedLine];
entry.m_szPrefix[0] = '\0';
const std::string prefix(Chat::convertFromUTF8(editPrefixBuffer));
strcat(entry.m_szPrefix, prefix.c_str());
chat.chatUpdate();
}
// ----- Outline -----
ImVec2 pos = ImGui::GetItemRectMin();
ImVec2 size = ImGui::GetItemRectSize();
ImGui::GetWindowDrawList()->AddRect(
pos,
ImVec2(pos.x + size.x, pos.y + size.y),
IM_COL32(255, 255, 255, 255)
);
}
if (ImGui::ColorButton("ColorButton", editLineColor, ImGuiColorEditFlags_NoTooltip))
{
ImGui::OpenPopup("ColorPickerPopup");
}
if (ImGui::BeginPopup("ColorPickerPopup"))
{
this->colorPopupActive = true;
if (ImGui::ColorPicker3("##picker", (float*)&editLineColor, ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoSmallPreview))
{
auto& pChat = chat.pChat;
pChat->m_entry[mSelectedLine].m_textColor = ImVec4ToARGB(editLineColor);
chat.chatUpdate();
}
ImGui::EndPopup();
} else this->colorPopupActive = false;
ImGui::SameLine(0.0f, 5.0f);
if (ImGui::InputText("##ChatLine", editLineBuffer, IM_ARRAYSIZE(editLineBuffer)))
{
if (chat.mSelectedEntry > -1)
{
const auto& pChat = chat.pChat;
pChat->m_entry[chat.mSelectedEntry].m_szText[0] = '\0';
const std::string text(Chat::convertFromUTF8(editLineBuffer));
strcat(pChat->m_entry[chat.mSelectedEntry].m_szText, text.c_str());
chat.chatUpdate();
}
}
// Input outline
ImVec2 inputPos = ImGui::GetItemRectMin();
ImVec2 inputSize = ImGui::GetItemRectSize();
ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddRect(inputPos, ImVec2(inputPos.x + inputSize.x, inputPos.y + inputSize.y), IM_COL32(255, 255, 255, 255));
ImGui::PopItemWidth();
ImGui::PopStyleVar(2);
ImGui::PopStyleColor();
ImGui::End();
}
ImGui::PopStyleColor();
}
}
void Menu::RebuildFonts()
{
std::string fontPath(256, '\0');
if (SHGetSpecialFolderPathA(Chat::getGameHWND(), fontPath.data(), CSIDL_FONTS, false))
{
ImGui_ImplDX9_InvalidateDeviceObjects();
fontPath.resize(fontPath.find('\0'));
fontPath += "\\";
std::string fontFileName{ Chat::getFontRelativePathByName(Chat::getSampFontName()) };
if (fontFileName.empty() || fontFileName.find("arial") != std::string::npos) fontFileName = "ArialBd.ttf";
fontPath += fontFileName;
auto& io = ImGui::GetIO();
ImVector<ImWchar> ranges;
ImFontGlyphRangesBuilder builder;
io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
builder.AddRanges(io.Fonts->GetGlyphRangesCyrillic());
builder.AddText(u8"‚„…†‡ˆ‰‹‘’“”•–—™›¹");
builder.BuildRanges(&ranges);
ImFontConfig cfg;
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_LightHinting | ImGuiFreeTypeBuilderFlags_ForceAutoHint;
io.Fonts->Clear();
io.Fonts->AddFontFromFileTTF(fontPath.c_str(), Chat::getSampFontSize() - 2, &cfg, ranges.Data);
io.Fonts->Build();
ImGui::GetStyle().ItemSpacing.y = 2;
ImGui_ImplDX9_CreateDeviceObjects();
}
}
void Menu::ShowPopup()
{
this->openPopup = true;
}
void Menu::ClosePopup()
{
ImGui::CloseCurrentPopup();
this->popupActive = false;
}
bool Menu::IsPopupActive() const
{
return popupActive;
}
bool Menu::IsEditLineActive() const
{
return editLineActive;
}
bool Menu::IsColorPopupActive() const
{
return this->colorPopupActive;
}
void Menu::CloseEditLine()
{
this->editLineActive = false;
Chat::setSampCursorMode(0);
Chat::getInstance().mSelectedEntry = -1;
}