diff --git a/src/requesthandler/RequestHandler.cpp b/src/requesthandler/RequestHandler.cpp index b200fd9c..26818a4c 100644 --- a/src/requesthandler/RequestHandler.cpp +++ b/src/requesthandler/RequestHandler.cpp @@ -118,6 +118,8 @@ const std::unordered_map RequestHandler::_han {"SetSceneItemLocked", &RequestHandler::SetSceneItemLocked}, {"GetSceneItemIndex", &RequestHandler::GetSceneItemIndex}, {"SetSceneItemIndex", &RequestHandler::SetSceneItemIndex}, + {"GetSceneItemBlendMode", &RequestHandler::GetSceneItemBlendMode}, + {"SetSceneItemBlendMode", &RequestHandler::SetSceneItemBlendMode}, // Stream {"GetStreamStatus", &RequestHandler::GetStreamStatus}, diff --git a/src/requesthandler/RequestHandler.h b/src/requesthandler/RequestHandler.h index d108feb0..d411f59b 100644 --- a/src/requesthandler/RequestHandler.h +++ b/src/requesthandler/RequestHandler.h @@ -136,6 +136,8 @@ class RequestHandler { RequestResult SetSceneItemLocked(const Request&); RequestResult GetSceneItemIndex(const Request&); RequestResult SetSceneItemIndex(const Request&); + RequestResult GetSceneItemBlendMode(const Request&); + RequestResult SetSceneItemBlendMode(const Request&); // Stream RequestResult GetStreamStatus(const Request&); diff --git a/src/requesthandler/RequestHandler_SceneItems.cpp b/src/requesthandler/RequestHandler_SceneItems.cpp index 50ea294a..c47621f9 100644 --- a/src/requesthandler/RequestHandler_SceneItems.cpp +++ b/src/requesthandler/RequestHandler_SceneItems.cpp @@ -631,3 +631,83 @@ RequestResult RequestHandler::SetSceneItemIndex(const Request& request) return RequestResult::Success(); } + +/** + * Gets the blend mode of a scene item. + * + * Blend modes: + * + * - `OBS_BLEND_NORMAL` + * - `OBS_BLEND_ADDITIVE` + * - `OBS_BLEND_SUBTRACT` + * - `OBS_BLEND_SCREEN` + * - `OBS_BLEND_MULTIPLY` + * - `OBS_BLEND_LIGHTEN` + * - `OBS_BLEND_DARKEN` + * + * Scenes and Groups + * + * @requestField sceneName | String | Name of the scene the item is in + * @requestField sceneItemId | Number | Numeric ID of the scene item | >= 0 + * + * @responseField sceneItemBlendMode | String | Current blend mode + * + * @requestType GetSceneItemBlendMode + * @complexity 2 + * @rpcVersion -1 + * @initialVersion 5.0.0 + * @api requests + * @category scene items + */ +RequestResult RequestHandler::GetSceneItemBlendMode(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment, OBS_WEBSOCKET_SCENE_FILTER_SCENE_OR_GROUP); + if (!sceneItem) + return RequestResult::Error(statusCode, comment); + + auto blendMode = obs_sceneitem_get_blending_mode(sceneItem); + + json responseData; + responseData["sceneItemBlendMode"] = Utils::Obs::StringHelper::GetSceneItemBlendMode(blendMode); + + return RequestResult::Success(responseData); +} + + + +/** + * Sets the blend mode of a scene item. + * + * Scenes and Groups + * + * @requestField sceneName | String | Name of the scene the item is in + * @requestField sceneItemId | Number | Numeric ID of the scene item | >= 0 + * @requestField sceneItemBlendMode | String | New blend mode + * + * @requestType SetSceneItemBlendMode + * @complexity 2 + * @rpcVersion -1 + * @initialVersion 5.0.0 + * @api requests + * @category scene items + */ +RequestResult RequestHandler::SetSceneItemBlendMode(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment, OBS_WEBSOCKET_SCENE_FILTER_SCENE_OR_GROUP); + if (!(sceneItem && request.ValidateString("sceneItemBlendMode", statusCode, comment))) + return RequestResult::Error(statusCode, comment); + + std::string blendModeString = request.RequestData["sceneItemBlendMode"]; + + auto blendMode = Utils::Obs::EnumHelper::GetSceneItemBlendMode(blendModeString); + if (blendMode == OBS_BLEND_NORMAL && blendModeString != "OBS_BLEND_NORMAL") + return RequestResult::Error(RequestStatus::InvalidRequestField, "The field sceneItemBlendMode has an invalid value."); + + obs_sceneitem_set_blending_mode(sceneItem, blendMode); + + return RequestResult::Success(); +} diff --git a/src/utils/Obs.h b/src/utils/Obs.h index 460d3113..fb5e0899 100644 --- a/src/utils/Obs.h +++ b/src/utils/Obs.h @@ -163,6 +163,7 @@ namespace Utils { std::string GetMediaInputState(obs_source_t *input); std::string GetLastReplayBufferFilePath(); std::string GetSceneItemBoundsType(enum obs_bounds_type type); + std::string GetSceneItemBlendMode(enum obs_blending_type mode); std::string DurationToTimecode(uint64_t); std::string GetOutputState(ObsOutputState state); } @@ -170,6 +171,7 @@ namespace Utils { namespace EnumHelper { enum obs_bounds_type GetSceneItemBoundsType(std::string boundsType); enum ObsMediaInputAction GetMediaInputAction(std::string mediaAction); + enum obs_blending_type GetSceneItemBlendMode(std::string mode); } namespace NumberHelper { diff --git a/src/utils/Obs_EnumHelper.cpp b/src/utils/Obs_EnumHelper.cpp index b0c21aa4..887c33d1 100644 --- a/src/utils/Obs_EnumHelper.cpp +++ b/src/utils/Obs_EnumHelper.cpp @@ -23,25 +23,38 @@ with this program. If not, see enum obs_bounds_type Utils::Obs::EnumHelper::GetSceneItemBoundsType(std::string boundsType) { - RET_COMPARE(boundsType, OBS_BOUNDS_NONE); - RET_COMPARE(boundsType, OBS_BOUNDS_STRETCH); - RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_INNER); - RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_OUTER); - RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_TO_WIDTH); - RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_TO_HEIGHT); - RET_COMPARE(boundsType, OBS_BOUNDS_MAX_ONLY); + RET_COMPARE(boundsType, OBS_BOUNDS_NONE) + RET_COMPARE(boundsType, OBS_BOUNDS_STRETCH) + RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_INNER) + RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_OUTER) + RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_TO_WIDTH) + RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_TO_HEIGHT) + RET_COMPARE(boundsType, OBS_BOUNDS_MAX_ONLY) return OBS_BOUNDS_NONE; } enum ObsMediaInputAction Utils::Obs::EnumHelper::GetMediaInputAction(std::string mediaAction) { - RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PLAY); - RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE); - RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_STOP); - RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART); - RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NEXT); - RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PREVIOUS); + RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PLAY) + RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE) + RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_STOP) + RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART) + RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NEXT) + RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PREVIOUS) return OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NONE; } + +enum obs_blending_type Utils::Obs::EnumHelper::GetSceneItemBlendMode(std::string mode) +{ + RET_COMPARE(mode, OBS_BLEND_NORMAL) + RET_COMPARE(mode, OBS_BLEND_ADDITIVE) + RET_COMPARE(mode, OBS_BLEND_SUBTRACT) + RET_COMPARE(mode, OBS_BLEND_SCREEN) + RET_COMPARE(mode, OBS_BLEND_MULTIPLY) + RET_COMPARE(mode, OBS_BLEND_LIGHTEN) + RET_COMPARE(mode, OBS_BLEND_DARKEN) + + return OBS_BLEND_NORMAL; +} diff --git a/src/utils/Obs_StringHelper.cpp b/src/utils/Obs_StringHelper.cpp index 8b552abb..ef040bf1 100644 --- a/src/utils/Obs_StringHelper.cpp +++ b/src/utils/Obs_StringHelper.cpp @@ -142,6 +142,20 @@ std::string Utils::Obs::StringHelper::GetSceneItemBoundsType(enum obs_bounds_typ } } +std::string Utils::Obs::StringHelper::GetSceneItemBlendMode(enum obs_blending_type mode) +{ + switch (mode) { + default: + CASE(OBS_BLEND_NORMAL) + CASE(OBS_BLEND_ADDITIVE) + CASE(OBS_BLEND_SUBTRACT) + CASE(OBS_BLEND_SCREEN) + CASE(OBS_BLEND_MULTIPLY) + CASE(OBS_BLEND_LIGHTEN) + CASE(OBS_BLEND_DARKEN) + } +} + std::string Utils::Obs::StringHelper::DurationToTimecode(uint64_t ms) { uint64_t secs = ms / 1000ULL;