window-basic-main.cpp 8.9 KB
Newer Older
1 2 3 4 5
/******************************************************************************
    Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7 8 9
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 12 13 14 15 16 17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

J
jp9000 已提交
18 19
#include <obs.hpp>

20 21
#include <wx/msgdlg.h>

22
#include "obs-app.hpp"
J
jp9000 已提交
23
#include "wx-wrappers.hpp"
24 25
#include "window-basic-settings.hpp"
#include "window-basic-main.hpp"
26 27
#include "window-namedialog.hpp"
using namespace std;
J
jp9000 已提交
28

29 30 31 32 33 34 35
void OBSBasic::SceneAdded(obs_source_t source)
{
	const char *name  = obs_source_getname(source);
	obs_scene_t scene = obs_scene_fromsource(source);
	scenes->Append(wxString(name, wxConvUTF8), scene);
}

J
jp9000 已提交
36 37 38 39 40
void OBSBasic::SceneRemoved(obs_source_t source)
{
	const char *name = obs_source_getname(source);

	int item = scenes->FindString(name);
J
jp9000 已提交
41 42
	if (item != wxNOT_FOUND) {
		scenes->Delete(item);
J
jp9000 已提交
43 44 45
		return;
	}

J
jp9000 已提交
46 47 48
	item = sources->FindString(name);
	if (item != wxNOT_FOUND)
		sources->Delete(item);
J
jp9000 已提交
49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
void OBSBasic::SourceAdded(void *data, calldata_t params)
{
	OBSBasic *window = (OBSBasic*)data;

	obs_source_t source;
	calldata_getptr(params, "source", (void**)&source);

	obs_source_type type;
	obs_source_gettype(source, &type, NULL);

	if (type == SOURCE_SCENE)
		window->SceneAdded(source);
}

void OBSBasic::SourceDestroyed(void *data, calldata_t params)
{
	OBSBasic *window = (OBSBasic*)data;

69
	obs_source_t source;
70 71
	calldata_getptr(params, "source", (void**)&source);

J
jp9000 已提交
72 73 74 75 76
	obs_source_type type;
	obs_source_gettype(source, &type, NULL);

	if (type == SOURCE_SCENE)
		window->SceneRemoved(source);
77 78
}

J
jp9000 已提交
79 80 81 82 83 84 85
bool OBSBasic::Init()
{
	if (!obs_startup())
		return false;
	if (!InitGraphics())
		return false;

86 87 88 89 90 91 92 93
	signal_handler_connect(obs_signalhandler(), "source-add",
			OBSBasic::SourceAdded, this);
	signal_handler_connect(obs_signalhandler(), "source-destroy",
			OBSBasic::SourceDestroyed, this);

	//obs_scene_t scene = obs_scene_create("test scene");
	//obs_add_source(obs_scene_getsource(scene));

94 95
	/* TODO: this is a test */
	obs_load_module("test-input");
96

J
jp9000 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	obs_source_t test = obs_source_create(SOURCE_INPUT, "random", "test",
			NULL);
	obs_add_source(test);
	obs_set_output_source(0, test);
	/*obs_scene_t scene = obs_scene_create("test2");
	obs_set_output_source(0, obs_scene_getsource(scene));

	obs_sceneitem_t bla = obs_scene_add(scene, test);

	struct vec2 ddd = {100.0f, 100.0f};
	obs_sceneitem_setscale(bla, &ddd);

	obs_scene_release(scene);*/
	obs_source_release(test);

J
jp9000 已提交
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
	return true;
}

OBSBasic::~OBSBasic()
{
	obs_shutdown();
}

bool OBSBasic::InitGraphics()
{
	struct obs_video_info ovi;
	wxGetApp().GetConfigFPS(ovi.fps_num, ovi.fps_den);
	ovi.graphics_module = wxGetApp().GetRenderModule();
	ovi.base_width    = (uint32_t)config_get_uint(GetGlobalConfig(),
			"Video", "BaseCX");
	ovi.base_height   = (uint32_t)config_get_uint(GetGlobalConfig(),
			"Video", "BaseCY");
	ovi.output_width  = (uint32_t)config_get_uint(GetGlobalConfig(),
			"Video", "OutputCX");
	ovi.output_height = (uint32_t)config_get_uint(GetGlobalConfig(),
			"Video", "OutputCY");
	ovi.output_format = VIDEO_FORMAT_RGBA;
	ovi.adapter       = 0;
	ovi.window        = WxToGSWindow(previewPanel);

	//required to make opengl display stuff on osx(?)
J
jp9000 已提交
138
	ResizePreview(ovi.base_width, ovi.base_height);
J
jp9000 已提交
139 140
	SendSizeEvent();

141
	wxSize size = previewPanel->GetClientSize();
J
jp9000 已提交
142 143 144 145 146 147
	ovi.window_width  = size.x;
	ovi.window_height = size.y;

	if (!obs_reset_video(&ovi))
		return false;

J
jp9000 已提交
148 149 150
	return true;
}

151
void OBSBasic::OnClose(wxCloseEvent &event)
152 153
{
	wxGetApp().ExitMainLoop();
154 155 156
	event.Skip();
}

157
void OBSBasic::OnMinimize(wxIconizeEvent &event)
158 159 160 161
{
	event.Skip();
}

J
jp9000 已提交
162
void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
163
{
164
	/* resize preview panel to fix to the top section of the window */
165 166
	wxSize targetSize   = GetPreviewContainer()->GetSize();
	double targetAspect = double(targetSize.x) / double(targetSize.y);
J
jp9000 已提交
167
	double baseAspect   = double(cx) / double(cy);
168
	wxSize newSize;
169 170

	if (targetAspect > baseAspect)
171
		newSize = wxSize(targetSize.y * baseAspect, targetSize.y);
172
	else
173 174 175
		newSize = wxSize(targetSize.x, targetSize.x / baseAspect);

	GetPreviewPanel()->SetMinSize(newSize);
J
jp9000 已提交
176 177 178 179 180 181 182 183 184 185 186 187
}

void OBSBasic::OnSize(wxSizeEvent &event)
{
	struct obs_video_info ovi;

	event.Skip();

	if (!obs_get_video_info(&ovi))
		return;

	ResizePreview(ovi.base_width, ovi.base_height);
188 189 190 191 192 193
}

void OBSBasic::OnResizePreview(wxSizeEvent &event)
{
	event.Skip();

194
	wxSize newSize = previewPanel->GetClientSize();
J
jp9000 已提交
195

196 197 198 199 200 201
	graphics_t graphics = obs_graphics();
	if (graphics) {
		gs_entercontext(graphics);
		gs_resize(newSize.x, newSize.y);
		gs_leavecontext();
	}
202 203
}

204
void OBSBasic::fileNewClicked(wxCommandEvent &event)
205 206 207
{
}

208
void OBSBasic::fileOpenClicked(wxCommandEvent &event)
209 210 211
{
}

212
void OBSBasic::fileSaveClicked(wxCommandEvent &event)
213 214 215
{
}

216
void OBSBasic::fileExitClicked(wxCommandEvent &event)
217
{
J
jp9000 已提交
218
	wxGetApp().ExitMainLoop();
219 220
}

221 222 223 224 225 226 227 228 229 230 231 232 233
void OBSBasic::scenesClicked(wxCommandEvent &event)
{
	int sel = scenes->GetSelection();

	obs_source_t source = NULL;
	if (sel != wxNOT_FOUND) {
		obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
		source = obs_scene_getsource(scene);
	}

	obs_set_output_source(0, source);
}

234
void OBSBasic::scenesRDown(wxMouseEvent &event)
235 236 237
{
}

238
void OBSBasic::sceneAddClicked(wxCommandEvent &event)
239
{
240 241 242 243 244 245 246
	string name;
	int ret = NameDialog::AskForName(this,
			Str("MainWindow.AddSceneDlg.Title"),
			Str("MainWindow.AddSceneDlg.Text"),
			name);

	if (ret == wxID_OK) {
247 248 249 250 251 252 253 254 255 256 257
		obs_source_t source = obs_get_source_by_name(name.c_str());
		if (source) {
			wxMessageBox(WXStr("MainWindow.NameExists.Text"),
			             WXStr("MainWindow.NameExists.Title"),
			             wxOK|wxCENTRE, this);

			obs_source_release(source);
			sceneAddClicked(event);
			return;
		}

258 259 260 261
		obs_scene_t scene = obs_scene_create(name.c_str());
		obs_add_source(obs_scene_getsource(scene));
		obs_scene_release(scene);
	}
262 263
}

264
void OBSBasic::sceneRemoveClicked(wxCommandEvent &event)
265
{
J
jp9000 已提交
266 267 268 269 270 271 272
	int sel = scenes->GetSelection();
	if (sel == wxNOT_FOUND)
		return;

	obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
	obs_source_t source = obs_scene_getsource(scene);
	obs_source_remove(source);
273 274
}

275
void OBSBasic::scenePropertiesClicked(wxCommandEvent &event)
276 277 278
{
}

279
void OBSBasic::sceneUpClicked(wxCommandEvent &event)
280 281 282
{
}

283
void OBSBasic::sceneDownClicked(wxCommandEvent &event)
284 285 286
{
}

287 288 289 290 291 292 293 294
void OBSBasic::sourcesClicked(wxCommandEvent &event)
{
}

void OBSBasic::sourcesToggled(wxCommandEvent &event)
{
}

295
void OBSBasic::sourcesRDown(wxMouseEvent &event)
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
void OBSBasic::AddSource(obs_scene_t scene, const char *id)
{
	string name;

	bool success = false;
	while (!success) {
		int ret = NameDialog::AskForName(this,
				Str("MainWindow.AddSourceDlg.Title"),
				Str("MainWindow.AddSourceDlg.Text"),
				name);

		if (ret == wxID_CANCEL)
			break;

		obs_source_t source = obs_get_source_by_name(
				name.c_str());
		if (!source) {
			success = true;
		} else {
			wxMessageBox(WXStr("MainWindow.NameExists.Text"),
				     WXStr("MainWindow.NameExists.Title"),
				     wxOK|wxCENTRE, this);
			obs_source_release(source);
		}
	}

	if (success) {
		obs_source_t source = obs_source_create(SOURCE_INPUT, id,
				name.c_str(), NULL);
		obs_add_source(source);
		obs_sceneitem_t item = obs_scene_add(scene, source);
		obs_source_release(source);
	}
}

void OBSBasic::AddSourcePopup()
335
{
336
	int sceneSel = scenes->GetSelection();
337 338 339 340
	size_t idx = 0;
	const char *type;
	vector<const char *> types;

341 342 343
	if (sceneSel == wxNOT_FOUND)
		return;

344 345 346 347 348 349 350 351 352
	obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sceneSel);
	obs_scene_addref(scene);

	unique_ptr<wxMenu> popup(new wxMenu());
	while (obs_enum_input_types(idx, &type)) {
		const char *name = obs_source_getdisplayname(SOURCE_INPUT,
				type, wxGetApp().GetLocale());

		types.push_back(type);
353
		popup->Append((int)idx+1, wxString(name, wxConvUTF8));
354 355 356 357 358 359 360

		idx++;
	}

	if (idx) {
		int id = WXDoPopupMenu(this, popup.get());
		if (id != -1)
361
			AddSource(scene, types[id-1]);
362 363 364 365 366 367 368 369
	}

	obs_scene_release(scene);
}

void OBSBasic::sourceAddClicked(wxCommandEvent &event)
{
	AddSourcePopup();
370 371
}

372
void OBSBasic::sourceRemoveClicked(wxCommandEvent &event)
373 374 375
{
}

376
void OBSBasic::sourcePropertiesClicked(wxCommandEvent &event)
377 378 379
{
}

380
void OBSBasic::sourceUpClicked(wxCommandEvent &event)
381 382 383
{
}

384
void OBSBasic::sourceDownClicked(wxCommandEvent &event)
385 386
{
}
J
jp9000 已提交
387

388 389
void OBSBasic::settingsClicked(wxCommandEvent &event)
{
390 391
	OBSBasicSettings test(this);
	test.ShowModal();
392 393 394
}

void OBSBasic::exitClicked(wxCommandEvent &event)
J
jp9000 已提交
395 396 397
{
	wxGetApp().ExitMainLoop();
}