obs/Source/API.cpp

307 lines
8.8 KiB
C++

/********************************************************************************
Copyright (C) 2012 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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
********************************************************************************/
#include "Main.h"
void OBS::RegisterSceneClass(CTSTR lpClassName, CTSTR lpDisplayName, OBSCREATEPROC createProc, OBSCONFIGPROC configProc)
{
if(!lpClassName || !*lpClassName)
{
AppWarning(TEXT("OBS::RegisterSceneClass: No class name specified"));
return;
}
if(!createProc)
{
AppWarning(TEXT("OBS::RegisterSceneClass: No create procedure specified"));
return;
}
if(GetSceneClass(lpClassName))
{
AppWarning(TEXT("OBS::RegisterSceneClass: Tried to register '%s', but it already exists"), lpClassName);
return;
}
ClassInfo *classInfo = sceneClasses.CreateNew();
classInfo->strClass = lpClassName;
classInfo->strName = lpDisplayName;
classInfo->createProc = createProc;
classInfo->configProc = configProc;
}
void OBS::RegisterImageSourceClass(CTSTR lpClassName, CTSTR lpDisplayName, OBSCREATEPROC createProc, OBSCONFIGPROC configProc)
{
if(!lpClassName || !*lpClassName)
{
AppWarning(TEXT("OBS::RegisterImageSourceClass: No class name specified"));
return;
}
if(!createProc)
{
AppWarning(TEXT("OBS::RegisterImageSourceClass: No create procedure specified"));
return;
}
if(GetImageSourceClass(lpClassName))
{
AppWarning(TEXT("OBS::RegisterImageSourceClass: Tried to register '%s', but it already exists"), lpClassName);
return;
}
ClassInfo *classInfo = imageSourceClasses.CreateNew();
classInfo->strClass = lpClassName;
classInfo->strName = lpDisplayName;
classInfo->createProc = createProc;
classInfo->configProc = configProc;
}
Scene* OBS::CreateScene(CTSTR lpClassName, XElement *data)
{
for(UINT i=0; i<sceneClasses.Num(); i++)
{
if(sceneClasses[i].strClass.CompareI(lpClassName))
return (Scene*)sceneClasses[i].createProc(data);
}
AppWarning(TEXT("OBS::CreateScene: Could not find scene class '%s'"), lpClassName);
return NULL;
}
ImageSource* OBS::CreateImageSource(CTSTR lpClassName, XElement *data)
{
for(UINT i=0; i<imageSourceClasses.Num(); i++)
{
if(imageSourceClasses[i].strClass.CompareI(lpClassName))
return (ImageSource*)imageSourceClasses[i].createProc(data);
}
AppWarning(TEXT("OBS::CreateImageSource: Could not find image source class '%s'"), lpClassName);
return NULL;
}
void OBS::ConfigureScene(XElement *element)
{
if(!element)
{
AppWarning(TEXT("OBS::ConfigureScene: NULL element specified"));
return;
}
CTSTR lpClassName = element->GetString(TEXT("class"));
if(!lpClassName)
{
AppWarning(TEXT("OBS::ConfigureScene: No class specified for scene '%s'"), element->GetName());
return;
}
for(UINT i=0; i<sceneClasses.Num(); i++)
{
if(sceneClasses[i].strClass.CompareI(lpClassName))
{
if(sceneClasses[i].configProc)
sceneClasses[i].configProc(element, false);
return;
}
}
AppWarning(TEXT("OBS::ConfigureScene: Could not find scene class '%s'"), lpClassName);
}
void OBS::ConfigureImageSource(XElement *element)
{
if(!element)
{
AppWarning(TEXT("OBS::ConfigureImageSource: NULL element specified"));
return;
}
CTSTR lpClassName = element->GetString(TEXT("class"));
if(!lpClassName)
{
AppWarning(TEXT("OBS::ConfigureImageSource: No class specified for image source '%s'"), element->GetName());
return;
}
for(UINT i=0; i<imageSourceClasses.Num(); i++)
{
if(imageSourceClasses[i].strClass.CompareI(lpClassName))
{
if(imageSourceClasses[i].configProc)
imageSourceClasses[i].configProc(element, false);
return;
}
}
AppWarning(TEXT("OBS::ConfigureImageSource: Could not find scene class '%s'"), lpClassName);
}
void OBS::AddSourceItem(LPWSTR name, bool checked, UINT index)
{
LVITEM lvI;
// Initialize LVITEM members that are common to all items.
lvI.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
lvI.stateMask = 0;
lvI.iSubItem = 0;
lvI.state = 0;
lvI.pszText = name;
lvI.iItem = index;
HWND hwndSources = GetDlgItem(hwndMain, ID_SOURCES);
ListView_InsertItem(hwndSources, &lvI);
ListView_SetCheckState(hwndSources, index, checked);
ListView_SetColumnWidth(hwndSources, 0, LVSCW_AUTOSIZE_USEHEADER);
ListView_SetColumnWidth(hwndSources, 1, LVSCW_AUTOSIZE_USEHEADER);
}
bool OBS::SetScene(CTSTR lpScene)
{
if(bDisableSceneSwitching)
return false;
HWND hwndScenes = GetDlgItem(hwndMain, ID_SCENES);
UINT curSel = (UINT)SendMessage(hwndScenes, LB_GETCURSEL, 0, 0);
//-------------------------
if(curSel != LB_ERR)
{
UINT textLen = (UINT)SendMessage(hwndScenes, LB_GETTEXTLEN, curSel, 0);
String strLBName;
strLBName.SetLength(textLen);
SendMessage(hwndScenes, LB_GETTEXT, curSel, (LPARAM)strLBName.Array());
if(!strLBName.CompareI(lpScene))
{
UINT id = (UINT)SendMessage(hwndScenes, LB_FINDSTRINGEXACT, -1, (LPARAM)lpScene);
if(id == LB_ERR)
return false;
SendMessage(hwndScenes, LB_SETCURSEL, id, 0);
}
}
else
{
UINT id = (UINT)SendMessage(hwndScenes, LB_FINDSTRINGEXACT, -1, (LPARAM)lpScene);
if(id == LB_ERR)
return false;
SendMessage(hwndScenes, LB_SETCURSEL, id, 0);
}
//-------------------------
XElement *scenes = scenesConfig.GetElement(TEXT("scenes"));
XElement *newSceneElement = scenes->GetElement(lpScene);
if(!newSceneElement)
return false;
if(sceneElement == newSceneElement)
return true;
sceneElement = newSceneElement;
CTSTR lpClass = sceneElement->GetString(TEXT("class"));
if(!lpClass)
{
AppWarning(TEXT("OBS::SetScene: no class found for scene '%s'"), newSceneElement->GetName());
return false;
}
if(bRunning)
{
Log(TEXT("++++++++++++++++++++++++++++++++++++++++++++++++++++++"));
Log(TEXT(" New Scene"));
}
XElement *sceneData = newSceneElement->GetElement(TEXT("data"));
//-------------------------
Scene *newScene = NULL;
if(bRunning)
newScene = CreateScene(lpClass, sceneData);
//-------------------------
HWND hwndSources = GetDlgItem(hwndMain, ID_SOURCES);
SendMessage(hwndSources, WM_SETREDRAW, (WPARAM)FALSE, (LPARAM) 0);
bChangingSources = true;
ListView_DeleteAllItems(hwndSources);
XElement *sources = sceneElement->GetElement(TEXT("sources"));
if(sources)
{
UINT numSources = sources->NumElements();
ListView_SetItemCount(hwndSources, numSources);
for(UINT i=0; i<numSources; i++)
{
XElement *sourceElement = sources->GetElementByID(i);
bool render = sourceElement->GetInt(TEXT("render"), 1) > 0;
AddSourceItem((LPWSTR)sourceElement->GetName(), render, i);
if(bRunning && newScene)
newScene->AddImageSource(sourceElement);
}
}
bChangingSources = false;
SendMessage(hwndSources, WM_SETREDRAW, (WPARAM)TRUE, (LPARAM) 0);
RedrawWindow(hwndSources, NULL, NULL, RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
if(scene && newScene->HasMissingSources())
MessageBox(hwndMain, Str("Scene.MissingSources"), NULL, 0);
if(bRunning)
{
//todo: cache scenes maybe? undecided. not really as necessary with global sources
OSEnterMutex(hSceneMutex);
if(scene)
scene->EndScene();
Scene *previousScene = scene;
scene = newScene;
scene->BeginScene();
if(!bTransitioning)
{
bTransitioning = true;
transitionAlpha = 0.0f;
}
OSLeaveMutex(hSceneMutex);
delete previousScene;
}
return true;
}