-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplication.cpp
252 lines (233 loc) · 9.37 KB
/
Application.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
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
#include "Application.h"
using namespace boost::algorithm;
constexpr auto helptext = "Warning - Input document has to be UTF-8 encoded!\nEnter path to tags you want to translate. Example:\n\n\
<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
<DialogsResource>\n\
<Reply\n\
name=\"Dlg_npc_002712\"\n\
text=\"Hallo!\"\n\
role=\"NPC\"/>\n\
<info name=\"global_confirm\">Bevestig</info>\n\
</DialogsResource>\n\n\
To translate word Bevestig in <info> tag, write: DialogsResource.info\n\
To translate Hallo! (text attribute of tag Reply), write: DialogsResource.Reply:text\n\
Every command has to be written in a new line.";
Application::Application()
{
fileDialog.SetTitle("File browser");
fileDialog.SetTypeFilters({ ".xml" });
Load_configuration();
}
void Application::HelpMarker(const char* desc)
{
ImGui::TextDisabled("(HELP)");
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
std::vector<std::string> Application::Process_raw_commands(std::span<char> text)
{
//auto end = std::find(text, text + raw_commands_size, '\0');
auto end = std::find(text.begin(), text.end(), '\0');
std::string raw_commands = std::string(text.begin(), end);
trim(raw_commands);
std::vector<std::string> output;
boost::split(output, raw_commands, boost::is_any_of("\n"));
return output;
}
void Application::Save_configuration()
{
std::ofstream ouf(program_conf_name, std::ofstream::trunc);
ouf << input_locale_index << std::endl;
ouf << output_locale_index << std::endl;
ouf << text;
}
void Application::Load_configuration()
{
if (!boost::filesystem::exists(program_conf_name))
Save_configuration();
std::ifstream inf(program_conf_name);
std::string line;
std::getline(inf, line);
input_locale_index = std::stoi(line);
std::getline(inf, line);
output_locale_index = std::stoi(line);
std::getline(inf, line);
strcpy(text, line.c_str());
}
void Application::Loop()
{
// XML translator fullscreen
//-----------------
static bool use_work_area = true;
static ImGuiWindowFlags flags =
ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoCollapse;
// We demonstrate using the full viewport area or the work area (without menu-bars, task-bars etc.)
// Based on your use case you may want one of the other.
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(use_work_area ? viewport->WorkPos : viewport->Pos);
ImGui::SetNextWindowSize(use_work_area ? viewport->WorkSize : viewport->Size);
static int combo_flags = ImGuiComboFlags_PopupAlignLeft | ImGuiComboFlags_HeightLargest;
if (ImGui::Begin("Example: Fullscreen window", &show_fullscreen, flags))
{
if (curl_wrapper)
if (ImGui::Button(("Auth-key: " + curl_wrapper->auth_key).c_str())) {
curl_wrapper.reset();
got_auth_key = false;
try_read_auth_key = false;
}
if (!got_auth_key)
{
//
if (try_read_auth_key)
{
try {
curl_wrapper = std::make_unique<cURL_wrapper>();
}
catch (...)
{
}
try_read_auth_key = false;
}
if (!curl_wrapper)
{
ImGui::SetNextWindowSize(ImVec2(700, 70));
ImGui::Begin("Enter API auth key to deepl account", &show_enter_auth_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::SetWindowFocus();
if (ImGui::InputText("##label3", auth_key_buffer, auth_key_buffer_size, ImGuiInputTextFlags_EnterReturnsTrue))
{
try
{
curl_wrapper = std::make_unique<cURL_wrapper>(auth_key_buffer);
}
catch (...)
{
// if provided auth_key is bad
}
}
ImGui::SetWindowFocus();
ImGui::End();
}
}
//ImGui::SameLine();
const char* input_combo_preview_value = locales[input_locale_index]->name.c_str(); // Pass in the preview value visible before opening the combo (it could be anything)
ImGui::Text("Input: ");
ImGui::SameLine();
if (ImGui::BeginCombo("##label", input_combo_preview_value, combo_flags))
{
for (int n = 0; n < locales.size(); ++n)
{
const bool is_selected = (input_locale_index == n);
if (ImGui::Selectable(locales[n]->name.c_str(), is_selected))
{
if (n == output_locale_index) {
std::swap(output_locale_index, input_locale_index);
}
else
input_locale_index = n;
}
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
// combobox
const char* output_combo_preview_value = locales[output_locale_index]->name.c_str(); // Pass in the preview value visible before opening the combo (it could be anything)
ImGui::Text("Output:");
ImGui::SameLine();
if (ImGui::BeginCombo("##label2", output_combo_preview_value, combo_flags))
{
for (int n = 0; n < locales.size(); ++n)
{
const bool is_selected = (output_locale_index == n);
if (ImGui::Selectable(locales[n]->name.c_str(), is_selected))
{
if (n == input_locale_index) {
std::swap(output_locale_index, input_locale_index);
}
else
output_locale_index = n;
}
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
// open and read XML file
// Note: we are using a fixed-sized buffer for simplicity here. See ImGuiInputTextFlags_CallbackResize
// and the code in misc/cpp/imgui_stdlib.h for how to setup InputText() for dynamically resizing strings.
ImGui::Text("Command field");
ImGui::SameLine();
HelpMarker(helptext);
ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text),
ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_AllowTabInput);
if (ImGui::Button("Translate!"))
{
fileDialog.Open();
Save_configuration();
}
fileDialog.Display();
if (fileDialog.HasSelected())
{
static bool translating_window = true;
ImGui::SetNextWindowSize(ImVec2(700, 200));
//ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetWindowFocus();
ImGui::Begin("Translating...", &translating_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text("Translating in progress.. \nOutput file will be available in the same directory as original file. ");
ImGui::SetWindowFocus();
ImGui::End();
if (refreshed_translate_window)
{
std::string path = fileDialog.GetSelected().string();
std::cout << "Selected filename: " << path << std::endl;
auto commands = Process_raw_commands(std::span(text, raw_commands_size));
try {
xml_parser.translate(
path,
commands,
*locales[input_locale_index],
*locales[output_locale_index]
);
}
catch (std::exception& e)
{
show_error_message = true;
error_message_string = e.what();
}
fileDialog.ClearSelected();
refreshed_translate_window = false;
}
else
refreshed_translate_window = true;
}
}
if (show_error_message)
{
ImGui::Begin("Translation aborted. ", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text(error_message_string.c_str());
ImGui::SetWindowFocus();
if (ImGui::Button("OK"))
{
show_error_message = false;
show_another_window = false;
}
ImGui::End();
}
ImGui::End();
// demo window for feature reference
/* if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);*/
}