forked from Dovahseod/Bongobs-Cat-Plugin-Edition-D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VtuberPlugin.cpp
377 lines (313 loc) · 13.9 KB
/
VtuberPlugin.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
Created by Weng Y on 2020/05/25.
Copyright © 2020 Weng Y. Under GNU General Public License v2.0.
*/
#include "VtuberPlugin.hpp"
#include <obs-module.h>
#include <windows.h>
#include <codecvt>
#include <string>
#include "Live2DManager.hpp"
#include "VtuberDelegate.hpp"
#include "VtuberFrameWork.hpp"
namespace {
constexpr char kPropertyNameCaptureSpecificWindow[] = "capture_specific_window";
constexpr char kPropertyNameCaptureWindow[] = "capture_window";
struct Vtuber_data {
obs_source_t *source;
uint16_t modelId;
};
bool CaptureSpecificWindowCallback(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings) {
bool value = obs_data_get_bool(settings, kPropertyNameCaptureSpecificWindow);
obs_property_set_visible(obs_properties_get(ppts, kPropertyNameCaptureWindow),
value);
return true;
}
bool IsWindowValid(HWND window) {
if (!IsWindowVisible(window)) {
return false;
}
DWORD styles = GetWindowLongPtrW(window, GWL_STYLE);
if (styles & WS_CHILD) {
return false;
}
DWORD ex_styles = GetWindowLongPtrW(window, GWL_EXSTYLE);
if (ex_styles & WS_EX_TOOLWINDOW) {
return false;
}
return true;
}
HWND NextWindow(HWND window, bool use_findwindowex) {
do {
if (use_findwindowex) {
window = FindWindowExW(GetDesktopWindow(), window, nullptr, nullptr);
} else {
window = GetWindow(window, GW_HWNDNEXT);
}
} while (window != nullptr && !IsWindowValid(window));
return window;
}
void FillWindowList(obs_property_t *p) {
bool use_findwindowex = true;
HWND window = FindWindowExW(GetDesktopWindow(), nullptr, nullptr, nullptr);
if (window == nullptr) {
use_findwindowex = false;
window = GetWindow(GetDesktopWindow(), GW_CHILD);
}
if (!IsWindowValid(window)) {
window = NextWindow(window, use_findwindowex);
if (window == nullptr && use_findwindowex) {
use_findwindowex = false;
window = GetWindow(GetDesktopWindow(), GW_CHILD);
if (!IsWindowValid(window)) {
window = NextWindow(window, use_findwindowex);
}
}
}
while (window != nullptr) {
int length = GetWindowTextLengthW(window);
if (length != 0) {
std::vector<wchar_t> buffer(length + 1);
if (GetWindowTextW(window, buffer.data(), buffer.size())) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
std::string title_utf8 = convert.to_bytes(buffer.data());
obs_property_list_add_string(p, title_utf8.c_str(), title_utf8.c_str());
}
}
window = NextWindow(window, use_findwindowex);
}
}
bool InsertSelectedWindowIfNotPresent(obs_property_t *p, obs_data_t *settings) {
const char *current_value =
obs_data_get_string(settings, kPropertyNameCaptureWindow);
if (current_value == nullptr) {
return false;
}
const char *value = nullptr;
bool found = false;
for (int i = 0; (value = obs_property_list_item_string(p, i)) != nullptr;
++i) {
if (std::strcmp(value, current_value) == 0) {
found = true;
break;
}
}
if (*current_value != '\0' && !found) {
obs_property_list_insert_string(p, /*idx=*/1, current_value, current_value);
obs_property_list_item_disable(p, /*idx=*/1, /*disabled=*/true);
return true;
}
return false;
}
bool OnCaptureWindowChanged(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings) {
return InsertSelectedWindowIfNotPresent(p, settings);
}
void UpdateSettings(obs_data_t *settings) {
std::string capture_window_utf8(
obs_data_get_string(settings, kPropertyNameCaptureWindow));
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
VtuberDelegate::GetInstance()->UpdateSettings({
/*capture_specific_window=*/obs_data_get_bool(
settings, kPropertyNameCaptureSpecificWindow),
/*capture_window=*/convert.from_bytes(capture_window_utf8),
});
}
} // namespace
const char *VtuberPlugin::VtuberPlugin::VtuberGetName(void *unused) {
UNUSED_PARAMETER(unused);
return "bongobs cat";
}
void *VtuberPlugin::VtuberPlugin::VtuberCreate(obs_data_t *settings,
obs_source_t *source) {
static int id = 0;
Vtuber_data *vtb = new Vtuber_data();
vtb->source = source;
vtb->modelId = id++;
double x = obs_data_get_double(settings, "x");
double y = obs_data_get_double(settings, "y");
int width = obs_data_get_int(settings, "width");
int height = obs_data_get_int(settings, "height");
double scale = obs_data_get_double(settings, "scale");
double delayTime = obs_data_get_double(settings, "delaytime");
bool random_motion = obs_data_get_bool(settings, "random_motion");
bool breath = obs_data_get_bool(settings, "breath");
bool eyeblink = obs_data_get_bool(settings, "eyeblink");
bool track = obs_data_get_bool(settings, "track");
const char *mode = obs_data_get_string(settings, "mode");
bool live2D = obs_data_get_bool(settings, "live2d");
bool relative_mouse = obs_data_get_bool(settings, "relative_mouse");
bool mouse_horizontal_flip =
obs_data_get_bool(settings, "mouse_horizontal_flip");
bool mouse_vertical_flip = obs_data_get_bool(settings, "mouse_vertical_flip");
bool mask = obs_data_get_bool(settings, "mask");
int screen_top_override = obs_data_get_int(settings, "screen_top_override");
int screen_bottom_override =
obs_data_get_int(settings, "screen_bottom_override");
int screen_left_override = obs_data_get_int(settings, "screen_left_override");
int screen_right_override =
obs_data_get_int(settings, "screen_right_override");
VtuberFrameWork::InitVtuber(vtb->modelId);
VtuberFrameWork::UpData(vtb->modelId, x, y, width, height, scale, delayTime,
random_motion, breath, eyeblink, NULL, track, mode,
live2D, relative_mouse, mouse_horizontal_flip,
mouse_vertical_flip, mask);
Live2DManager::GetInstance()->SetScreenOverride(
vtb->modelId, screen_top_override, screen_bottom_override,
screen_left_override, screen_right_override);
UpdateSettings(settings);
return vtb;
}
void VtuberPlugin::VtuberPlugin::VtuberDestroy(void *data) {
Vtuber_data *spv = (Vtuber_data *)data;
VtuberFrameWork::UinitVtuber(spv->modelId);
delete spv;
}
void VtuberPlugin::VtuberPlugin::VtuberRender(void *data, gs_effect_t *effect) {
Vtuber_data *spv = (Vtuber_data *)data;
int width, height;
width = VtuberFrameWork::GetWidth(spv->modelId);
height = VtuberFrameWork::GetHeight(spv->modelId);
obs_enter_graphics();
gs_texture_t *tex =
gs_texture_create(width, height, GS_RGBA, 1, NULL, GS_DYNAMIC);
uint8_t *ptr;
uint32_t linesize;
if (gs_texture_map(tex, &ptr, &linesize))
VtuberFrameWork::ReanderVtuber(spv->modelId, (char *)ptr, width, height);
gs_texture_unmap(tex);
obs_source_draw(tex, 0, 0, 0, 0, true);
gs_texture_destroy(tex);
obs_leave_graphics();
}
uint32_t VtuberPlugin::VtuberPlugin::VtuberWidth(void *data) {
Vtuber_data *vtb = (Vtuber_data *)data;
return VtuberFrameWork::GetWidth(vtb->modelId);
}
uint32_t VtuberPlugin::VtuberPlugin::VtuberHeight(void *data) {
Vtuber_data *vtb = (Vtuber_data *)data;
return VtuberFrameWork::GetHeight(vtb->modelId);
}
static void fill_vtuber_model_list(obs_property_t *p, void *data) {
Vtuber_data *vtb = (Vtuber_data *)data;
int _size;
const char **_modeNames = VtuberFrameWork::GetModeDefine(_size);
for (int index = 0; index < _size; index++) {
obs_property_list_add_string(p, _modeNames[index], _modeNames[index]);
}
}
obs_properties_t *VtuberPlugin::VtuberPlugin::VtuberGetProperties(void *data) {
Vtuber_data *vtb = (Vtuber_data *)data;
obs_properties_t *ppts = obs_properties_create();
obs_properties_set_param(ppts, vtb, NULL);
obs_property_t *p;
// obs_properties_add_path(ppts, "models_path",obs_module_text("ModelsPath"),
// OBS_PATH_FILE,"*.model3.json", "Resources/");
p = obs_properties_add_list(ppts, "mode", obs_module_text("Mode"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
fill_vtuber_model_list(p, data);
// obs_property_set_modified_callback(p, vtuber_model_callback);
// obs_properties_add_int(ppts, "width", obs_module_text("Width"), 32, 1900,
// 32); obs_properties_add_int(ppts, "height", obs_module_text("Height"), 32,
// 1050, 32);
// obs_properties_add_float_slider(ppts,"scale",obs_module_text("Scale"),0.1,10.0,0.1);
// obs_properties_add_float_slider(ppts, "x", obs_module_text("X"), -3.0, 3.0,
// 0.1); obs_properties_add_float_slider(ppts, "y", obs_module_text("Y"),
// -3.0, 3.0, 0.1);
obs_properties_add_bool(ppts, "relative_mouse",
obs_module_text("Relative Mouse Movement"));
obs_properties_add_bool(ppts, "mouse_horizontal_flip",
obs_module_text("Mouse Horizontal Flip"));
obs_properties_add_bool(ppts, "mouse_vertical_flip",
obs_module_text("Mouse Vertical Flip"));
obs_properties_add_bool(ppts, "mask", obs_module_text("Use Mask"));
obs_properties_add_bool(ppts, "live2d", obs_module_text("Live2D"));
obs_properties_add_float_slider(ppts, "delay", obs_module_text("Speed"), 0.0,
10.0, 0.1);
obs_properties_add_bool(ppts, "random_motion",
obs_module_text("RandomMotion"));
obs_properties_add_bool(ppts, "breath", obs_module_text("Breath"));
obs_properties_add_bool(ppts, "eyeblink", obs_module_text("EyeBlink"));
obs_properties_add_bool(ppts, "track", obs_module_text("Mouse Tracktion"));
obs_properties_add_int(ppts, "screen_top_override",
obs_module_text("Screen Top Override"), -1,
1'000'000'000, 1);
obs_properties_add_int(ppts, "screen_bottom_override",
obs_module_text("Screen Bottom Override"), -1,
1'000'000'000, 1);
obs_properties_add_int(ppts, "screen_left_override",
obs_module_text("Screen Left Override"), -1,
1'000'000'000, 1);
obs_properties_add_int(ppts, "screen_right_override",
obs_module_text("Screen Right Override"), -1,
1'000'000'000, 1);
p = obs_properties_add_bool(ppts, kPropertyNameCaptureSpecificWindow,
obs_module_text("Capture specific window"));
obs_property_set_modified_callback(p, CaptureSpecificWindowCallback);
p = obs_properties_add_list(ppts, kPropertyNameCaptureWindow,
obs_module_text("Window"), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
obs_property_list_add_string(p, "", "");
FillWindowList(p);
obs_property_set_modified_callback(p, OnCaptureWindowChanged);
return ppts;
}
void VtuberPlugin::VtuberPlugin::Vtuber_update(void *data,
obs_data_t *settings) {
Vtuber_data *vtb = (Vtuber_data *)data;
double x = obs_data_get_double(settings, "x");
double y = obs_data_get_double(settings, "y");
int width = obs_data_get_int(settings, "width");
int height = obs_data_get_int(settings, "height");
double vscale = obs_data_get_double(settings, "scale");
double delayTime = obs_data_get_double(settings, "delay");
bool random_motion = obs_data_get_bool(settings, "random_motion");
bool breath = obs_data_get_bool(settings, "breath");
bool eyeblink = obs_data_get_bool(settings, "eyeblink");
bool track = obs_data_get_bool(settings, "track");
const char *mode = obs_data_get_string(settings, "mode");
bool live2D = obs_data_get_bool(settings, "live2d");
bool relative_mouse = obs_data_get_bool(settings, "relative_mouse");
bool mouse_horizontal_flip =
obs_data_get_bool(settings, "mouse_horizontal_flip");
bool mouse_vertical_flip = obs_data_get_bool(settings, "mouse_vertical_flip");
bool mask = obs_data_get_bool(settings, "mask");
int screen_top_override = obs_data_get_int(settings, "screen_top_override");
int screen_bottom_override =
obs_data_get_int(settings, "screen_bottom_override");
int screen_left_override = obs_data_get_int(settings, "screen_left_override");
int screen_right_override =
obs_data_get_int(settings, "screen_right_override");
const char *vtb_str = NULL; // obs_data_get_string(settings, "models_path");
VtuberFrameWork::UpData(vtb->modelId, x, y, width, height, vscale, delayTime,
random_motion, breath, eyeblink, vtb_str, track, mode,
live2D, relative_mouse, mouse_horizontal_flip,
mouse_vertical_flip, mask);
Live2DManager::GetInstance()->SetScreenOverride(
vtb->modelId, screen_top_override, screen_bottom_override,
screen_left_override, screen_right_override);
UpdateSettings(settings);
}
void VtuberPlugin::VtuberPlugin::Vtuber_defaults(obs_data_t *settings) {
obs_data_set_default_int(settings, "width", 1280);
obs_data_set_default_int(settings, "height", 768);
obs_data_set_default_double(settings, "x", 0);
obs_data_set_default_double(settings, "y", 0.02);
obs_data_set_default_double(settings, "scale", 1.83);
obs_data_set_default_double(settings, "delaytime", 1.0);
obs_data_set_default_bool(settings, "random_motion", true);
obs_data_set_default_bool(settings, "breath", true);
obs_data_set_default_bool(settings, "eyeblink", true);
obs_data_set_default_bool(settings, "track", true);
obs_data_set_default_string(settings, "Mode", "standard");
obs_data_set_default_bool(settings, "live2d", true);
obs_data_set_default_bool(settings, "relative_mouse", false);
obs_data_set_default_bool(settings, "mouse_horizontal_flip", true);
obs_data_set_default_bool(settings, "mouse_vertical_flip", true);
obs_data_set_default_bool(settings, "mask", false);
obs_data_set_default_int(settings, "screen_top_override", -1);
obs_data_set_default_int(settings, "screen_bottom_override", -1);
obs_data_set_default_int(settings, "screen_left_override", -1);
obs_data_set_default_int(settings, "screen_right_override", -1);
}