Requests: Add GetSceneItemId

I realized that it was not entirely fair to expect users to fetch a
scene's item list, then search the list on the client in order to find
an item ID, so this is a compromise. This will also help developers
move from the 4.x scene item functionality to 5.x's

Operating on scene items by relying on source name can be dangerous
and in some cases exhibit what would be considered undefinied behavior.
Operating on scene items using IDs is best practice.
This commit is contained in:
tt2468 2021-09-25 17:21:06 -07:00
parent 4e0b8c5f11
commit b91ad0f790
3 changed files with 24 additions and 0 deletions

View File

@ -69,6 +69,7 @@ const std::map<std::string, RequestMethodHandler> RequestHandler::_handlerMap
// Scene Items
{"GetSceneItemList", &RequestHandler::GetSceneItemList},
{"GetGroupSceneItemList", &RequestHandler::GetGroupSceneItemList},
{"GetSceneItemId", &RequestHandler::GetSceneItemId},
{"CreateSceneItem", &RequestHandler::CreateSceneItem},
{"RemoveSceneItem", &RequestHandler::RemoveSceneItem},
{"GetSceneItemTransform", &RequestHandler::GetSceneItemTransform},

View File

@ -90,6 +90,7 @@ class RequestHandler {
// Scene Items
RequestResult GetSceneItemList(const Request&);
RequestResult GetGroupSceneItemList(const Request&);
RequestResult GetSceneItemId(const Request&);
RequestResult CreateSceneItem(const Request&);
RequestResult RemoveSceneItem(const Request&);
RequestResult GetSceneItemTransform(const Request&);

View File

@ -28,6 +28,28 @@ RequestResult RequestHandler::GetGroupSceneItemList(const Request& request)
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::GetSceneItemId(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSourceAutoRelease sceneSource = request.ValidateScene("sceneName", statusCode, comment);
if (!(sceneSource && request.ValidateString("sourceName", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
OBSScene scene = obs_scene_from_source(sceneSource);
std::string sourceName = request.RequestData["sourceName"];
obs_sceneitem_t *item = Utils::Obs::SearchHelper::GetSceneItemByName(scene, sourceName);
if (!item)
return RequestResult::Error(RequestStatus::ResourceNotFound, "No scene items were found in the specified scene by that name.");
json responseData;
responseData["sceneItemId"] = obs_sceneitem_get_id(item);
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::CreateSceneItem(const Request& request)
{
RequestStatus::RequestStatus statusCode;