Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using System.Threading;
- using System.Threading.Tasks;
- public class Scale
- {
- public double x;
- public double y;
- public String filter;
- }
- public class Crop
- {
- public int top;
- public int bottom;
- public int left;
- public int right;
- }
- public class Item
- {
- public int id;
- public string name;
- }
- public class Position
- {
- public double x;
- public double y;
- }
- public class Transform
- {
- public Position position;
- public double rotation;
- public Item item;
- public Scale scale;
- public Crop crop;
- }
- public class VideoInfo
- {
- public int baseWidth;
- public int baseHeight;
- public int outputWidth;
- public int outputHeight;
- public String scaleType;
- public double fps;
- public String videoFormat;
- public String colorSpace;
- public String colorRange;
- }
- public class SourceSettings
- {
- public String souceName;
- public String sourceType;
- public Object sourceSettings;
- }
- public class Scene
- {
- public String name;
- }
- public class SceneItem
- {
- public int itemId;
- public String sourceKind;
- public String sourceName;
- public String sourceType;
- }
- public class SceneItemList
- {
- public String sourceName;
- public List<SceneItem> sceneItems;
- }
- public class DuplicatedSceneItemResponse
- {
- public String scene;
- public Item item;
- }
- public class CPHInline
- {
- int tweenCount;
- public void Init()
- {
- tweenCount = 0;
- }
- public bool Execute()
- {
- int dadScale = 10;
- int shakeLength = 36*dadScale;
- int spinTimes = 3*dadScale;
- int xSpacing = 500;
- int ySpacing = 300;
- String sourceName = "Lumix Greenscreen";
- List<int> ids = new List<int>();
- int listLength = 9;
- double scale = 0.75;
- Transform t = getTransform(sourceName);
- t.visible = false;
- t.scale.x *= scale;
- t.scale.y *= scale;
- setTransform(t, sourceName);
- int tWidth = (int)Math.Abs((t.sourceWidth * t.scale.x)) - (int)Math.Abs((t.crop.left * t.scale.x));
- int tHeight = (int)Math.Abs((t.sourceHeight * t.scale.y));
- VideoInfo info = getVideoInfo();
- int cX = info.baseWidth/2;
- int cY = info.baseHeight/2;
- SceneItem cam1 = getSceneItemNamed(sourceName);
- for(int i = 0; i < listLength; i++)
- {
- for(int j = 0; j < 4; j++) {
- ids.Add(copySource(sourceName, -3000,3000));
- Transform nt = getTransform("",ids[i*4+j]);
- nt.visible = true;
- setTransform(nt, "",ids[i*4+j]);
- }
- }
- int id1 = cam1.itemId;
- t.visible = true;
- setTransform(t, "", id1);
- //phase 1:
- // 4 corners
- q.setPosition(tWidth/2, tHeight/2, "", id1);
- int id2 = copySource(sourceName, tWidth/2, info.baseHeight - tHeight/2);
- int id3 = copySource(sourceName, info.baseWidth - tWidth/2, tHeight/2);
- int id4 = copySource(sourceName, info.baseWidth - tWidth/2, info.baseHeight - tHeight/2);
- setRotation(45, "", id2);
- setRotation(135, "", id1);
- setRotation(225, "", id3);
- setRotation(315, "", id4);
- Thread.Sleep(3000);
- //phase 2:
- // 4 corner pass through
- tween(info.baseWidth - tWidth, info.baseHeight - tHeight, 30, "", 180, id1);
- tween(-info.baseWidth + tWidth, -info.baseHeight + tHeight, 30, "", 180, id4);
- tween(info.baseWidth - tWidth, -info.baseHeight + tHeight, 30, "", 180, id2);
- tween(-info.baseWidth + tWidth, info.baseHeight - tHeight, 30, "", 180, id3);
- Thread.Sleep(3000);
- //phase 3:
- // Straighten and Cascade
- tween(0, 0, 5, "", 45, id1);
- tween(0, tHeight, 5, "", -45, id2);
- tween(0, -tHeight, 5, "", 135, id3);
- tween(0, 0, 5, "", -135, id4);
- Thread.Sleep(500);
- int index = 0;
- int spacing = 200;
- for(int i = 0; i < listLength; i++)
- {
- setRotation(-180, "", ids[index+1]);
- setRotation(180, "", ids[index+2]);
- setPosition(info.baseWidth - tWidth/2 - i*spacing, info.baseHeight - tHeight/2, "", ids[index]);
- setPosition(info.baseWidth - tWidth/2 - i*spacing, (int)(tHeight*1.5), "", ids[index+1]);
- setPosition(tWidth/2 + i*spacing, info.baseHeight - (int)(tHeight*1.5), "", ids[index+2]);
- setPosition(tWidth/2 + i*spacing, tHeight/2, "", ids[index+3]);
- index += 4;
- Thread.Sleep(50);
- }
- for(int i = 0; i < listLength; i++)
- {
- deleteSource("", ids[i*4]);
- deleteSource("", ids[i*4+1]);
- deleteSource("", ids[i*4+2]);
- deleteSource("", ids[i*4+3]);
- Thread.Sleep(50);
- }
- Thread.Sleep(2000);
- //phase 4:
- // Spin away
- tween(-3000, 0, 60, "", 9000, id2);
- tween(-1000, 0, 80, "", 9000, id3);
- tween(-1000, 0, 80, "", 9000, id4);
- Thread.Sleep(3000);
- deleteSource("", id2);
- deleteSource("", id3);
- deleteSource("", id4);
- foreach(int i in ids) deleteSource("", i);
- t.scale.x /= scale;
- t.scale.y /= scale;
- setTransform(t, "", id1);
- return true;
- }
- SourceSettings getSourceSettings(String sourceName)
- {
- Dictionary<String, object> getRequest = new Dictionary<String, object>();
- getRequest.Add("sourceName", sourceName);
- String requestResponse = CPH.ObsSendRaw("GetSourceSettings", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
- SourceSettings responseJson = JsonConvert.DeserializeObject<SourceSettings>(requestResponse);
- return responseJson;
- }
- Scene getCurrentScene()
- {
- String requestResponse = CPH.ObsSendRaw("GetCurrentScene", "");
- Scene sceneObj = JsonConvert.DeserializeObject<Scene>(requestResponse);
- return sceneObj;
- }
- SceneItemList getSceneItemList()
- {
- Dictionary<String, object> getRequest = new Dictionary<String, object>();
- getRequest.Add("sourceName", "garbage");
- String requestResponse = CPH.ObsSendRaw("GetSceneItemList", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
- SceneItemList responseJson = JsonConvert.DeserializeObject<SceneItemList>(requestResponse);
- return responseJson;
- }
- SceneItem getSceneItemNamed(String name)
- {
- SceneItemList list = getSceneItemList();
- foreach (SceneItem i in list.sceneItems)
- {
- if (i.sourceName.Equals(name))
- {
- return i;
- }
- }
- return null;
- }
- SceneItem getSceneItemById(int id)
- {
- SceneItemList list = getSceneItemList();
- foreach (SceneItem i in list.sceneItems)
- {
- if (i.itemId == id)
- {
- return i;
- }
- }
- return null;
- }
- void deleteSource(String name, int id = -1)
- {
- Dictionary<String, object> updateRequest = new Dictionary<String, object>();
- Dictionary<String, object> itemRequest = new Dictionary<String, object>();
- if (!name.Equals(""))
- itemRequest.Add("name", name);
- if (id != -1)
- itemRequest.Add("id", id);
- updateRequest.Add("item", itemRequest);
- CPH.ObsSendRaw("DeleteSceneItem", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
- }
- int copySource(String name, int x = 0, int y = 0, int rotation = 0)
- {
- SceneItem item = getSceneItemNamed(name);
- Dictionary<String, object> updateRequest = new Dictionary<String, object>();
- Dictionary<String, object> itemRequest = new Dictionary<String, object>();
- itemRequest.Add("id", item.itemId);
- updateRequest.Add("item", itemRequest);
- String response = CPH.ObsSendRaw("DuplicateSceneItem", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
- DuplicatedSceneItemResponse newItemResponse = JsonConvert.DeserializeObject<DuplicatedSceneItemResponse>(response);
- Transform t = getTransform("", item.itemId);
- t.item = new Item();
- t.item.id = newItemResponse.item.id;
- t.item.name = newItemResponse.item.name;
- setTransform(t, "", newItemResponse.item.id);
- setPosition(x, y, "", newItemResponse.item.id);
- setRotation(rotation, "", newItemResponse.item.id);
- return newItemResponse.item.id;
- }
- void setPosition(int x, int y, String item, int id = -1)
- {
- Dictionary<String, object> updateRequest = new Dictionary<String, object>();
- if (id == -1)
- updateRequest.Add("item", item);
- else
- {
- Dictionary<String, object> itemDict = new Dictionary<String, object>();
- itemDict.Add("id", id);
- updateRequest.Add("item", itemDict);
- }
- Dictionary<String, object> position = new Dictionary<String, object>();
- position.Add("x", x);
- position.Add("y", y);
- updateRequest.Add("position", position);
- CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
- }
- void setRotation(int angle, String item, int id = -1)
- {
- Dictionary<String, object> updateRequest = new Dictionary<String, object>();
- if (id == -1)
- updateRequest.Add("item", item);
- else
- {
- Dictionary<String, object> itemDict = new Dictionary<String, object>();
- itemDict.Add("id", id);
- updateRequest.Add("item", itemDict);
- }
- updateRequest.Add("rotation", angle);
- CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
- }
- Transform getTransform(String item, int id = -1)
- {
- Dictionary<String, object> getRequest = new Dictionary<String, object>();
- if (id == -1)
- getRequest.Add("item", item);
- else
- {
- Dictionary<String, object> itemDict = new Dictionary<String, object>();
- itemDict.Add("id", id);
- getRequest.Add("item", itemDict);
- }
- String requestResponse = CPH.ObsSendRaw("GetSceneItemProperties", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
- Transform t = JsonConvert.DeserializeObject<Transform>(requestResponse);
- return t;
- }
- void setTransform(Transform t, String itemName, int id = -1)
- {
- SceneItem item;
- if (id == -1)
- item = getSceneItemNamed(itemName);
- else
- item = getSceneItemById(id);
- if (item == null)
- return;
- CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(t, Formatting.Indented));
- }
- void TweenThread(double x, double y, int frames, String item, double rotation = 0, int id = -1)
- {
- Transform t;
- if (id == -1)
- t = getTransform(item);
- else
- t = getTransform("", id);
- double sX = t.position.x;
- double sY = t.position.y;
- double sR = t.rotation;
- for (int i = 1; i - 1 < frames; i++)
- {
- Thread.Sleep((int)(1.0 / 60.0 * 1000.0));
- Dictionary<String, object> updateRequest = new Dictionary<String, object>();
- if (id == -1)
- updateRequest.Add("item", item);
- else
- {
- Dictionary<String, object> itemDict = new Dictionary<String, object>();
- itemDict.Add("id", id);
- updateRequest.Add("item", itemDict);
- }
- Dictionary<String, object> position = new Dictionary<String, object>();
- position.Add("x", sX + (x * ((double)i / (double)frames)));
- position.Add("y", sY + (y * ((double)i / (double)frames)));
- updateRequest.Add("position", position);
- updateRequest.Add("rotation", sR + (rotation * ((double)i / (double)frames)));
- CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
- }
- tweenCount--;
- }
- public Task tween(double x, double y, int frames, String item, double rotation = 0, int id = -1)
- {
- tweenCount++;
- Task t = new Task(() => TweenThread(x, y, frames, item, rotation, id));
- t.Start();
- return t;
- }
- VideoInfo getVideoInfo()
- {
- Dictionary<String, object> getRequest = new Dictionary<String, object>();
- String requestResponse = CPH.ObsSendRaw("GetVideoInfo", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
- VideoInfo responseJson = JsonConvert.DeserializeObject<VideoInfo>(requestResponse);
- return responseJson;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement