Added Options UI (using OBS source), support for new settings file for cross platform support

This commit is contained in:
WarmUpTill 2016-06-28 05:42:20 +02:00 committed by GitHub
parent b906917531
commit cf5e32561e
2 changed files with 151 additions and 32 deletions

View File

@ -3,12 +3,15 @@
#include <obs-data.h>
#include <fstream>
#include <string>
#include <sstream>
#include <boost/filesystem.hpp>
using namespace std;
//Swicthing done in here
Switcher *switcher = new Switcher();
//settings source
struct obs_source_info sceneSwitcherOptionsSource;
//Hotkeys
const char *PAUSE_HOTKEY_NAME = "pauseHotkey";
const char *OPTIONS_HOTKEY_NAME = "optionsHotkey";
@ -21,15 +24,12 @@ string configPath;
//save settings (the only hotkey for now)
void saveKeybinding(string name, obs_data_array_t *hotkeyData) {
name.append(".txt");
//check if config dir exists
if (boost::filesystem::create_directories(configPath)){
if (hotkeyData != NULL) {
name.append(".txt");
//save hotkey data
fstream file;
file.open(obs_module_config_path(name.c_str()), fstream::out);
//doesnt seem to work in obs_module_unload (hotkey data freed already (<- Jim))
//hotkeyData = obs_hotkey_save(pauseHotkeyId);
ofstream file;
file.open(string(configPath).append(name), ofstream::trunc);
//hotkeyData = obs_hotkey_save(pauseHotkeyId); //doesnt seem to work in obs_module_unload (hotkey data freed already (<- Jim))
size_t num = obs_data_array_count(hotkeyData);
for (int i = 0; i < num; i++) {
string temp = obs_data_get_json(obs_data_array_item(hotkeyData, i));
@ -38,27 +38,33 @@ void saveKeybinding(string name, obs_data_array_t *hotkeyData) {
}
}
void loadKeybinding(string name, obs_data_array_t *hotkeyData, obs_hotkey_id hotkeyId) {
name.append(".txt");
ifstream file(obs_module_config_path(name.c_str()));
if (file.is_open())
string loadConfigFile(string filename)
{
ifstream settingsFile;
settingsFile.open(string(configPath).append(filename));
string value;
if (settingsFile.is_open())
{
string temp;
file.seekg(0, std::ios::end);
temp.reserve(file.tellg());
file.seekg(0, std::ios::beg);
temp.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
hotkeyData = obs_data_array_create();
if (!temp.empty()) {
//fails if hotkey not set
obs_data_array_insert(hotkeyData, 0, obs_data_create_from_json(temp.c_str()));
obs_hotkey_load(hotkeyId, hotkeyData);
}
settingsFile.seekg(0, ios::end);
value.reserve(settingsFile.tellg());
settingsFile.seekg(0, ios::beg);
value.assign((istreambuf_iterator<char>(settingsFile)), istreambuf_iterator<char>());
}
return value;
}
void loadKeybinding(string name, obs_data_array_t *hotkeyData, obs_hotkey_id hotkeyId) {
string temp = loadConfigFile(name.append(".txt"));
hotkeyData = obs_data_array_create();
if (!temp.empty())
{
obs_data_array_insert(hotkeyData, 0, obs_data_create_from_json(temp.c_str()));
obs_hotkey_load(hotkeyId, hotkeyData);
}
}
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("SceneSwitcher", "en-US")
void SceneSwitcherPauseHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
{
@ -81,11 +87,123 @@ void SceneSwitcherPauseHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey
pauseHotkeyData = obs_hotkey_save(pauseHotkeyId);
}
const char *sceneSwitcherOptionsGetName(void *type_data)
{
UNUSED_PARAMETER(type_data);
return "Scene Switcher Options";
}
void *sceneSwitcherOptionsCreate(obs_data_t *settings, obs_source_t *source) {
UNUSED_PARAMETER(settings);
UNUSED_PARAMETER(source);
return "";
}
void sceneSwitcherOptionsDestroy(void *data) {
UNUSED_PARAMETER(data);
}
uint32_t sceneSwitcherOptionsGetWidth(void *data)
{
UNUSED_PARAMETER(data);
return 0;
}
uint32_t sceneSwitcherOptionsGetHeight(void *data)
{
UNUSED_PARAMETER(data);
return 0;
}
void sceneSwitcherOptionsSourceGetDefaults(obs_data_t *settings)
{
//load settings file
string temp = loadConfigFile("settings.txt");
//set values from file
obs_data_apply(settings, obs_data_create_from_json(temp.c_str()));
}
bool loadOldSettings(obs_properties_t *props, obs_property_t *property, void *data)
{
//read old settings
string oldPath = "F:/OBS/obs-studio/BUILD/rundir/Debug/data/obs-plugins/SceneSwitcher/settings.txt";
ifstream oldSettingsFile;
oldSettingsFile.open(oldPath);
string line;
string value;
//construct new settings format
string newFormat = "{\n\t\"WindowList\": [\n";
string valueBeginning = "\t\t{\n\t\t\t\"value\": \"";
string valueEnd = "\t\t},\n";
while (oldSettingsFile.good())
{
getline(oldSettingsFile, line);
if (!line.empty()) {
newFormat += valueBeginning + line + "\"\n" + valueEnd;
}
}
//remove trailing ',' and newline
newFormat.pop_back();
newFormat.pop_back();
newFormat += "\n\t]\n}";
//check if there are useful new settings
if (newFormat != "{\n\t\"WindowList\": \n\t]\n}") {
//write to new settings file
ofstream file;
file.open(string(configPath).append("settings.txt"));
file << newFormat;
//obs_property_list_clear(obs_properties_get(props, "WindowList"));
//obs_data_t *settings = obs_data_create_from_json(newFormat.c_str());
//obs_property_modified(obs_properties_get(props, "WindowList"), obs_data_create_from_json(newFormat.c_str()));
//obs_properties_apply_settings(props, settings);
//obs_property_list_item_remove(obs_properties_first(props),1);
//obs_properties_apply_settings(props, obs_data_create_from_json(newFormat.c_str()));
}
return true;
}
obs_properties_t *sceneSwitcherOptionsSourceGetProperties(void *data)
{
UNUSED_PARAMETER(data);
obs_properties_t *props = obs_properties_create();
obs_properties_add_editable_list(props,
"WindowList", "Window Name",
false, "",
NULL);
obs_properties_add_button(props, "LoadOldSettings", "Load settings from old version", &loadOldSettings);
return props;
}
void sceneSwitcherOptionsSourceSave(void *data, obs_data_t *settings)
{
UNUSED_PARAMETER(data);
fstream settingsFile;
settingsFile.open(string(configPath).append("settings.txt"), fstream::in | fstream::out | ofstream::trunc);
settingsFile << obs_data_get_json(settings);
}
void sceneSwitcherOptionsSourceLoad(void *data, obs_data_t *settings)
{
UNUSED_PARAMETER(data);
//load settings file
string temp = loadConfigFile("settings.txt");
//set values from file
obs_data_apply(settings, obs_data_create_from_json(temp.c_str()));
}
void sceneSwitcherOptionsSourceSetup()
{
sceneSwitcherOptionsSource.id = "Scene Switcher Options";
sceneSwitcherOptionsSource.type = OBS_SOURCE_TYPE_INPUT;
sceneSwitcherOptionsSource.get_name = sceneSwitcherOptionsGetName;
sceneSwitcherOptionsSource.create = sceneSwitcherOptionsCreate;
sceneSwitcherOptionsSource.destroy = sceneSwitcherOptionsDestroy;
sceneSwitcherOptionsSource.get_width = sceneSwitcherOptionsGetWidth;
sceneSwitcherOptionsSource.get_height = sceneSwitcherOptionsGetHeight;
sceneSwitcherOptionsSource.get_defaults = sceneSwitcherOptionsSourceGetDefaults; //create settings file if not present
sceneSwitcherOptionsSource.get_properties = sceneSwitcherOptionsSourceGetProperties;
sceneSwitcherOptionsSource.save = sceneSwitcherOptionsSourceSave; //save new settings to file
sceneSwitcherOptionsSource.load = sceneSwitcherOptionsSourceLoad; //read settings file
obs_register_source(&sceneSwitcherOptionsSource);
}
bool obs_module_load(void)
{
//set config path
//set config path and check if config dir was set up
configPath = obs_module_config_path("");
boost::filesystem::create_directories(configPath);
//register hotkey
pauseHotkeyId = obs_hotkey_register_frontend(PAUSE_HOTKEY_NAME, "Toggle automatic scene switching", SceneSwitcherPauseHotkey, switcher);
//optionsHotkeyId = obs_hotkey_register_frontend(OPTIONS_HOTKEY_NAME, "Open the Scene Switcher options menu", SceneSwitcherOptionsHotkey, switcher);
@ -95,6 +213,8 @@ bool obs_module_load(void)
//load settings file
switcher->setSettingsFilePath(configPath);
switcher->firstLoad();
//create options menu
sceneSwitcherOptionsSourceSetup();
//start the switching thread
switcher->start();
return true;

View File

@ -6,7 +6,6 @@
#include <vector>
#include "settings.h"
using namespace std;
string Settings::getSettingsFilePath()
@ -21,19 +20,21 @@ void Settings::setSettingsFilePath(string path)
void Settings::load() {
//read the settings file
std::vector<std::string> settingsElements;
int numValues = 0;
ifstream infile(settingsFilePath);
string value;
string line;
while (infile.good())
{
//read json file
getline(infile, line);
if (!line.empty()) {
stringstream lineStream = stringstream(line);
size_t pos = line.find("\"value\":");
if (!line.empty() && pos != -1) {
string temp = line.substr(pos + 10, string::npos - 1);
temp.pop_back();
stringstream lineStream = stringstream(temp);
while (lineStream.good()) {
getline(lineStream, value, ',');
settingsElements.push_back(value);
@ -41,7 +42,6 @@ void Settings::load() {
}
}
}
//create settings map containing windowname and desired scene
for (int i = 0; i < numValues; ) {
string s2 = settingsElements.back();
@ -53,7 +53,6 @@ void Settings::load() {
//window name,scene
Settings::addToMap(s1, s2);
}
}
void Settings::addToMap(string s1, string s2) {