فهرست منبع

frontend: Add SceneCollection class as data model for Scene Collections

PatTheMav 8 ماه پیش
والد
کامیت
ca44e3037d

+ 1 - 0
frontend/CMakeLists.txt

@@ -46,6 +46,7 @@ include(cmake/ui-components.cmake)
 include(cmake/ui-dialogs.cmake)
 include(cmake/ui-docks.cmake)
 include(cmake/feature-importers.cmake)
+include(cmake/ui-models.cmake)
 include(cmake/ui-oauth.cmake)
 include(cmake/feature-browserpanels.cmake)
 include(cmake/ui-qt.cmake)

+ 1 - 0
frontend/cmake/ui-models.cmake

@@ -0,0 +1 @@
+target_sources(obs-studio PRIVATE models/SceneCollection.cpp models/SceneCollection.hpp models/Rect.cpp models/Rect.hpp)

+ 35 - 0
frontend/models/Rect.cpp

@@ -0,0 +1,35 @@
+/******************************************************************************
+    Copyright (C) 2025 by Patrick Heyer <[email protected]>
+
+    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, see <http://www.gnu.org/licenses/>.
+******************************************************************************/
+
+#include "Rect.hpp"
+
+namespace OBS {
+bool Rect::isZero() const
+{
+	return width_ == 0.0 || height_ == 0.0;
+}
+
+bool operator==(const Rect &lhs, const Rect &rhs)
+{
+	return lhs.width_ == rhs.width_ && lhs.height_ == rhs.height_;
+}
+
+bool operator!=(const Rect &lhs, const Rect &rhs)
+{
+	return lhs.width_ != rhs.width_ || lhs.height_ != rhs.height_;
+}
+} // namespace OBS

+ 98 - 0
frontend/models/Rect.hpp

@@ -0,0 +1,98 @@
+/******************************************************************************
+    Copyright (C) 2025 by Patrick Heyer <[email protected]>
+
+    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, see <http://www.gnu.org/licenses/>.
+******************************************************************************/
+
+#pragma once
+
+#include <algorithm>
+#include <limits>
+
+namespace {
+template<typename numberType> inline double safeConvertToDouble(numberType number)
+{
+	constexpr double minDoubleValue = 0.0;
+	constexpr double maxDoubleValue = std::numeric_limits<double>::max();
+
+	if (number > maxDoubleValue) {
+		return maxDoubleValue;
+	} else if (number < minDoubleValue) {
+		return minDoubleValue;
+	} else {
+		return static_cast<double>(number);
+	}
+}
+} // namespace
+
+namespace OBS {
+
+struct Rect {
+	double width_ = 0.0;
+	double height_ = 0.0;
+
+	Rect() = default;
+
+	template<typename widthType, typename heightType> Rect(widthType width, heightType height)
+	{
+		setWidth(width);
+		setHeight(height);
+	}
+
+	template<typename returnType> returnType getWidth() const
+	{
+		static_assert(std::is_convertible_v<double, returnType>,
+			      "Cannot convert <double> to requested return type");
+
+		return static_cast<returnType>(width_);
+	};
+
+	template<typename numberType> void setWidth(numberType width)
+	{
+		static_assert(std::is_same_v<numberType, double> || std::is_convertible_v<numberType, double>,
+			      "Cannot convert provided argument to <double>");
+
+		if (std::is_same<numberType, double>::value) {
+			width_ = std::max(static_cast<double>(width), 0.0);
+		} else {
+			width_ = safeConvertToDouble(width);
+		}
+	}
+
+	template<typename returnType> returnType getHeight() const
+	{
+		static_assert(std::is_convertible_v<double, returnType>,
+			      "Cannot convert <double> to requested return type");
+
+		return static_cast<returnType>(height_);
+	};
+
+	template<typename numberType> void setHeight(numberType height)
+	{
+		static_assert(std::is_same_v<numberType, double> || std::is_convertible_v<numberType, double>,
+			      "Cannot convert provided argument to <double>");
+
+		if (std::is_same<numberType, double>::value) {
+			height_ = std::max(static_cast<double>(height), 0.0);
+		} else {
+			height_ = safeConvertToDouble(height);
+		}
+	}
+
+	bool isZero() const;
+
+	friend bool operator==(const Rect &lhs, const Rect &rhs);
+	friend bool operator!=(const Rect &lhs, const Rect &rhs);
+};
+} // namespace OBS

+ 71 - 0
frontend/models/SceneCollection.cpp

@@ -0,0 +1,71 @@
+/******************************************************************************
+    Copyright (C) 2025 by Patrick Heyer <[email protected]>
+
+    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, see <http://www.gnu.org/licenses/>.
+******************************************************************************/
+
+#include "SceneCollection.hpp"
+
+static constexpr std::string_view invalidCoordinateMode = "Invalid coordinate mode provided";
+
+namespace OBS {
+std::string SceneCollection::getName() const
+{
+	return name_;
+}
+
+std::string SceneCollection::getFileName() const
+{
+	return filePath_.filename().u8string();
+}
+
+std::string SceneCollection::getFileStem() const
+{
+	return filePath_.stem().u8string();
+}
+
+std::filesystem::path SceneCollection::getFilePath() const
+{
+	return filePath_;
+}
+
+std::string SceneCollection::getFilePathString() const
+{
+	return filePath_.u8string();
+}
+
+SceneCoordinateMode SceneCollection::getCoordinateMode() const
+{
+	return coordinateMode_;
+}
+
+void SceneCollection::setCoordinateMode(SceneCoordinateMode newMode)
+{
+	if (newMode != SceneCoordinateMode::Invalid) {
+		coordinateMode_ = newMode;
+	} else {
+		throw std::invalid_argument(invalidCoordinateMode.data());
+	}
+}
+
+Rect SceneCollection::getMigrationResolution() const
+{
+	return migrationResolution_;
+}
+
+void SceneCollection::setMigrationResolution(Rect newResolution)
+{
+	migrationResolution_ = newResolution;
+}
+} // namespace OBS

+ 64 - 0
frontend/models/SceneCollection.hpp

@@ -0,0 +1,64 @@
+/******************************************************************************
+    Copyright (C) 2025 by Patrick Heyer <[email protected]>
+
+    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, see <http://www.gnu.org/licenses/>.
+******************************************************************************/
+
+#pragma once
+
+#include "Rect.hpp"
+
+#include <string>
+#include <filesystem>
+
+namespace OBS {
+
+enum class SceneCoordinateMode { Invalid, Absolute, Relative };
+
+class SceneCollection {
+private:
+	std::string name_;
+	std::filesystem::path filePath_;
+
+	SceneCoordinateMode coordinateMode_ = SceneCoordinateMode::Relative;
+	Rect migrationResolution_;
+
+public:
+	SceneCollection() = default;
+	SceneCollection(const std::string &name, const std::filesystem::path &filePath)
+		: name_(name),
+		  filePath_(filePath) {};
+
+	std::string getName() const;
+	std::string getFileName() const;
+	std::string getFileStem() const;
+	std::filesystem::path getFilePath() const;
+	std::string getFilePathString() const;
+
+	SceneCoordinateMode getCoordinateMode() const;
+	void setCoordinateMode(SceneCoordinateMode newMode);
+
+	Rect getMigrationResolution() const;
+	void setMigrationResolution(Rect newResolution);
+	template<typename widthType, typename heightType>
+	void setMigrationResolution(widthType width, heightType height)
+	{
+		this->migrationResolution_ = Rect(width, height);
+	}
+
+	inline int getVersion() const { return (coordinateMode_ == SceneCoordinateMode::Relative) ? 2 : 1; };
+
+	bool empty() const { return name_.empty() || filePath_.empty(); }
+};
+} // namespace OBS