OBSBasicPreview.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634
  1. #include "OBSBasicPreview.hpp"
  2. #include <utility/display-helpers.hpp>
  3. #include <widgets/OBSBasic.hpp>
  4. #include "moc_OBSBasicPreview.cpp"
  5. #define HANDLE_RADIUS 4.0f
  6. #define HANDLE_SEL_RADIUS (HANDLE_RADIUS * 1.5f)
  7. #define HELPER_ROT_BREAKPOINT 45.0f
  8. /* TODO: make C++ math classes and clean up code here later */
  9. OBSBasicPreview::OBSBasicPreview(QWidget *parent, Qt::WindowFlags flags) : OBSQTDisplay(parent, flags)
  10. {
  11. ResetScrollingOffset();
  12. setMouseTracking(true);
  13. }
  14. OBSBasicPreview::~OBSBasicPreview()
  15. {
  16. obs_enter_graphics();
  17. if (overflow)
  18. gs_texture_destroy(overflow);
  19. if (rectFill)
  20. gs_vertexbuffer_destroy(rectFill);
  21. if (circleFill)
  22. gs_vertexbuffer_destroy(circleFill);
  23. if (stripedLineEffect)
  24. gs_effect_destroy(stripedLineEffect);
  25. obs_leave_graphics();
  26. }
  27. void OBSBasicPreview::Init()
  28. {
  29. std::string effect_path;
  30. GetDataFilePath("striped_line.effect", effect_path);
  31. obs_enter_graphics();
  32. stripedLineEffect = gs_effect_create_from_file(effect_path.c_str(), nullptr);
  33. solidEffect = obs_get_base_effect(OBS_EFFECT_SOLID);
  34. obs_leave_graphics();
  35. }
  36. vec2 OBSBasicPreview::GetMouseEventPos(QMouseEvent *event)
  37. {
  38. OBSBasic *main = OBSBasic::Get();
  39. float pixelRatio = main->GetDevicePixelRatio();
  40. float scale = pixelRatio / main->previewScale;
  41. QPoint qtPos = event->pos();
  42. vec2 pos;
  43. vec2_set(&pos, (qtPos.x() - main->previewX / pixelRatio) * scale,
  44. (qtPos.y() - main->previewY / pixelRatio) * scale);
  45. return pos;
  46. }
  47. static void RotatePos(vec2 *pos, float rot)
  48. {
  49. float cosR = cos(rot);
  50. float sinR = sin(rot);
  51. vec2 newPos;
  52. newPos.x = cosR * pos->x - sinR * pos->y;
  53. newPos.y = sinR * pos->x + cosR * pos->y;
  54. vec2_copy(pos, &newPos);
  55. }
  56. struct SceneFindData {
  57. const vec2 &pos;
  58. OBSSceneItem item;
  59. bool selectBelow;
  60. obs_sceneitem_t *group = nullptr;
  61. SceneFindData(const SceneFindData &) = delete;
  62. SceneFindData(SceneFindData &&) = delete;
  63. SceneFindData &operator=(const SceneFindData &) = delete;
  64. SceneFindData &operator=(SceneFindData &&) = delete;
  65. inline SceneFindData(const vec2 &pos_, bool selectBelow_) : pos(pos_), selectBelow(selectBelow_) {}
  66. };
  67. struct SceneFindBoxData {
  68. const vec2 &startPos;
  69. const vec2 &pos;
  70. std::vector<obs_sceneitem_t *> sceneItems;
  71. SceneFindBoxData(const SceneFindData &) = delete;
  72. SceneFindBoxData(SceneFindData &&) = delete;
  73. SceneFindBoxData &operator=(const SceneFindData &) = delete;
  74. SceneFindBoxData &operator=(SceneFindData &&) = delete;
  75. inline SceneFindBoxData(const vec2 &startPos_, const vec2 &pos_) : startPos(startPos_), pos(pos_) {}
  76. };
  77. static bool SceneItemHasVideo(obs_sceneitem_t *item)
  78. {
  79. obs_source_t *source = obs_sceneitem_get_source(item);
  80. uint32_t flags = obs_source_get_output_flags(source);
  81. return (flags & OBS_SOURCE_VIDEO) != 0;
  82. }
  83. static bool CloseFloat(float a, float b, float epsilon = 0.01)
  84. {
  85. using std::abs;
  86. return abs(a - b) <= epsilon;
  87. }
  88. static bool FindItemAtPos(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  89. {
  90. SceneFindData *data = static_cast<SceneFindData *>(param);
  91. matrix4 transform;
  92. matrix4 invTransform;
  93. vec3 transformedPos;
  94. vec3 pos3;
  95. vec3 pos3_;
  96. if (!SceneItemHasVideo(item))
  97. return true;
  98. if (obs_sceneitem_locked(item))
  99. return true;
  100. vec3_set(&pos3, data->pos.x, data->pos.y, 0.0f);
  101. obs_sceneitem_get_box_transform(item, &transform);
  102. matrix4_inv(&invTransform, &transform);
  103. vec3_transform(&transformedPos, &pos3, &invTransform);
  104. vec3_transform(&pos3_, &transformedPos, &transform);
  105. if (CloseFloat(pos3.x, pos3_.x) && CloseFloat(pos3.y, pos3_.y) && transformedPos.x >= 0.0f &&
  106. transformedPos.x <= 1.0f && transformedPos.y >= 0.0f && transformedPos.y <= 1.0f) {
  107. if (data->selectBelow && obs_sceneitem_selected(item)) {
  108. if (data->item)
  109. return false;
  110. else
  111. data->selectBelow = false;
  112. }
  113. data->item = item;
  114. }
  115. return true;
  116. }
  117. static vec3 GetTransformedPos(float x, float y, const matrix4 &mat)
  118. {
  119. vec3 result;
  120. vec3_set(&result, x, y, 0.0f);
  121. vec3_transform(&result, &result, &mat);
  122. return result;
  123. }
  124. static inline vec2 GetOBSScreenSize()
  125. {
  126. obs_video_info ovi;
  127. vec2 size;
  128. vec2_zero(&size);
  129. if (obs_get_video_info(&ovi)) {
  130. size.x = float(ovi.base_width);
  131. size.y = float(ovi.base_height);
  132. }
  133. return size;
  134. }
  135. vec3 OBSBasicPreview::GetSnapOffset(const vec3 &tl, const vec3 &br)
  136. {
  137. OBSBasic *main = OBSBasic::Get();
  138. vec2 screenSize = GetOBSScreenSize();
  139. vec3 clampOffset;
  140. vec3_zero(&clampOffset);
  141. const bool snap = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SnappingEnabled");
  142. if (snap == false)
  143. return clampOffset;
  144. const bool screenSnap = config_get_bool(App()->GetUserConfig(), "BasicWindow", "ScreenSnapping");
  145. const bool centerSnap = config_get_bool(App()->GetUserConfig(), "BasicWindow", "CenterSnapping");
  146. const float clampDist =
  147. config_get_double(App()->GetUserConfig(), "BasicWindow", "SnapDistance") / main->previewScale;
  148. const float centerX = br.x - (br.x - tl.x) / 2.0f;
  149. const float centerY = br.y - (br.y - tl.y) / 2.0f;
  150. // Left screen edge.
  151. if (screenSnap && fabsf(tl.x) < clampDist)
  152. clampOffset.x = -tl.x;
  153. // Right screen edge.
  154. if (screenSnap && fabsf(clampOffset.x) < EPSILON && fabsf(screenSize.x - br.x) < clampDist)
  155. clampOffset.x = screenSize.x - br.x;
  156. // Horizontal center.
  157. if (centerSnap && fabsf(screenSize.x - (br.x - tl.x)) > clampDist &&
  158. fabsf(screenSize.x / 2.0f - centerX) < clampDist)
  159. clampOffset.x = screenSize.x / 2.0f - centerX;
  160. // Top screen edge.
  161. if (screenSnap && fabsf(tl.y) < clampDist)
  162. clampOffset.y = -tl.y;
  163. // Bottom screen edge.
  164. if (screenSnap && fabsf(clampOffset.y) < EPSILON && fabsf(screenSize.y - br.y) < clampDist)
  165. clampOffset.y = screenSize.y - br.y;
  166. // Vertical center.
  167. if (centerSnap && fabsf(screenSize.y - (br.y - tl.y)) > clampDist &&
  168. fabsf(screenSize.y / 2.0f - centerY) < clampDist)
  169. clampOffset.y = screenSize.y / 2.0f - centerY;
  170. return clampOffset;
  171. }
  172. OBSSceneItem OBSBasicPreview::GetItemAtPos(const vec2 &pos, bool selectBelow)
  173. {
  174. OBSBasic *main = OBSBasic::Get();
  175. OBSScene scene = main->GetCurrentScene();
  176. if (!scene)
  177. return OBSSceneItem();
  178. SceneFindData data(pos, selectBelow);
  179. obs_scene_enum_items(scene, FindItemAtPos, &data);
  180. return data.item;
  181. }
  182. static bool CheckItemSelected(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  183. {
  184. SceneFindData *data = static_cast<SceneFindData *>(param);
  185. matrix4 transform;
  186. vec3 transformedPos;
  187. vec3 pos3;
  188. if (!SceneItemHasVideo(item))
  189. return true;
  190. if (obs_sceneitem_is_group(item)) {
  191. data->group = item;
  192. obs_sceneitem_group_enum_items(item, CheckItemSelected, param);
  193. data->group = nullptr;
  194. if (data->item) {
  195. return false;
  196. }
  197. }
  198. vec3_set(&pos3, data->pos.x, data->pos.y, 0.0f);
  199. obs_sceneitem_get_box_transform(item, &transform);
  200. if (data->group) {
  201. matrix4 parent_transform;
  202. obs_sceneitem_get_draw_transform(data->group, &parent_transform);
  203. matrix4_mul(&transform, &transform, &parent_transform);
  204. }
  205. matrix4_inv(&transform, &transform);
  206. vec3_transform(&transformedPos, &pos3, &transform);
  207. if (transformedPos.x >= 0.0f && transformedPos.x <= 1.0f && transformedPos.y >= 0.0f &&
  208. transformedPos.y <= 1.0f) {
  209. if (obs_sceneitem_selected(item)) {
  210. data->item = item;
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. bool OBSBasicPreview::SelectedAtPos(const vec2 &pos)
  217. {
  218. OBSBasic *main = OBSBasic::Get();
  219. OBSScene scene = main->GetCurrentScene();
  220. if (!scene)
  221. return false;
  222. SceneFindData data(pos, false);
  223. obs_scene_enum_items(scene, CheckItemSelected, &data);
  224. return !!data.item;
  225. }
  226. struct HandleFindData {
  227. const vec2 &pos;
  228. const float radius;
  229. matrix4 parent_xform;
  230. OBSSceneItem item;
  231. ItemHandle handle = ItemHandle::None;
  232. float angle = 0.0f;
  233. vec2 rotatePoint;
  234. vec2 offsetPoint;
  235. float angleOffset = 0.0f;
  236. HandleFindData(const HandleFindData &) = delete;
  237. HandleFindData(HandleFindData &&) = delete;
  238. HandleFindData &operator=(const HandleFindData &) = delete;
  239. HandleFindData &operator=(HandleFindData &&) = delete;
  240. inline HandleFindData(const vec2 &pos_, float scale) : pos(pos_), radius(HANDLE_SEL_RADIUS / scale)
  241. {
  242. matrix4_identity(&parent_xform);
  243. }
  244. inline HandleFindData(const HandleFindData &hfd, obs_sceneitem_t *parent)
  245. : pos(hfd.pos),
  246. radius(hfd.radius),
  247. item(hfd.item),
  248. handle(hfd.handle),
  249. angle(hfd.angle),
  250. rotatePoint(hfd.rotatePoint),
  251. offsetPoint(hfd.offsetPoint)
  252. {
  253. obs_sceneitem_get_draw_transform(parent, &parent_xform);
  254. }
  255. };
  256. static bool FindHandleAtPos(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  257. {
  258. HandleFindData &data = *static_cast<HandleFindData *>(param);
  259. if (!obs_sceneitem_selected(item)) {
  260. if (obs_sceneitem_is_group(item)) {
  261. HandleFindData newData(data, item);
  262. newData.angleOffset = obs_sceneitem_get_rot(item);
  263. obs_sceneitem_group_enum_items(item, FindHandleAtPos, &newData);
  264. data.item = newData.item;
  265. data.handle = newData.handle;
  266. data.angle = newData.angle;
  267. data.rotatePoint = newData.rotatePoint;
  268. data.offsetPoint = newData.offsetPoint;
  269. }
  270. return true;
  271. }
  272. matrix4 transform;
  273. vec3 pos3;
  274. float closestHandle = data.radius;
  275. vec3_set(&pos3, data.pos.x, data.pos.y, 0.0f);
  276. obs_sceneitem_get_box_transform(item, &transform);
  277. auto TestHandle = [&](float x, float y, ItemHandle handle) {
  278. vec3 handlePos = GetTransformedPos(x, y, transform);
  279. vec3_transform(&handlePos, &handlePos, &data.parent_xform);
  280. float dist = vec3_dist(&handlePos, &pos3);
  281. if (dist < data.radius) {
  282. if (dist < closestHandle) {
  283. closestHandle = dist;
  284. data.handle = handle;
  285. data.item = item;
  286. }
  287. }
  288. };
  289. TestHandle(0.0f, 0.0f, ItemHandle::TopLeft);
  290. TestHandle(0.5f, 0.0f, ItemHandle::TopCenter);
  291. TestHandle(1.0f, 0.0f, ItemHandle::TopRight);
  292. TestHandle(0.0f, 0.5f, ItemHandle::CenterLeft);
  293. TestHandle(1.0f, 0.5f, ItemHandle::CenterRight);
  294. TestHandle(0.0f, 1.0f, ItemHandle::BottomLeft);
  295. TestHandle(0.5f, 1.0f, ItemHandle::BottomCenter);
  296. TestHandle(1.0f, 1.0f, ItemHandle::BottomRight);
  297. vec2 scale;
  298. obs_sceneitem_get_scale(item, &scale);
  299. obs_bounds_type boundsType = obs_sceneitem_get_bounds_type(item);
  300. vec2 rotHandleOffset;
  301. vec2_set(&rotHandleOffset, 0.0f, HANDLE_RADIUS * data.radius * 1.5 - data.radius);
  302. bool invertx = scale.x < 0.0f && boundsType == OBS_BOUNDS_NONE;
  303. float angle =
  304. atan2(invertx ? transform.x.y * -1.0f : transform.x.y, invertx ? transform.x.x * -1.0f : transform.x.x);
  305. RotatePos(&rotHandleOffset, angle);
  306. RotatePos(&rotHandleOffset, RAD(data.angleOffset));
  307. bool inverty = scale.y < 0.0f && boundsType == OBS_BOUNDS_NONE;
  308. vec3 handlePos = GetTransformedPos(0.5f, inverty ? 1.0f : 0.0f, transform);
  309. vec3_transform(&handlePos, &handlePos, &data.parent_xform);
  310. handlePos.x -= rotHandleOffset.x;
  311. handlePos.y -= rotHandleOffset.y;
  312. float dist = vec3_dist(&handlePos, &pos3);
  313. if (dist < data.radius) {
  314. if (dist < closestHandle) {
  315. closestHandle = dist;
  316. data.item = item;
  317. data.angle = obs_sceneitem_get_rot(item);
  318. data.handle = ItemHandle::Rot;
  319. vec2_set(&data.rotatePoint, transform.t.x + transform.x.x / 2 + transform.y.x / 2,
  320. transform.t.y + transform.x.y / 2 + transform.y.y / 2);
  321. obs_sceneitem_get_pos(item, &data.offsetPoint);
  322. data.offsetPoint.x -= data.rotatePoint.x;
  323. data.offsetPoint.y -= data.rotatePoint.y;
  324. RotatePos(&data.offsetPoint, -RAD(obs_sceneitem_get_rot(item)));
  325. }
  326. }
  327. return true;
  328. }
  329. static vec2 GetItemSize(obs_sceneitem_t *item)
  330. {
  331. obs_bounds_type boundsType = obs_sceneitem_get_bounds_type(item);
  332. vec2 size;
  333. if (boundsType != OBS_BOUNDS_NONE) {
  334. obs_sceneitem_get_bounds(item, &size);
  335. } else {
  336. obs_source_t *source = obs_sceneitem_get_source(item);
  337. obs_sceneitem_crop crop;
  338. vec2 scale;
  339. obs_sceneitem_get_scale(item, &scale);
  340. obs_sceneitem_get_crop(item, &crop);
  341. size.x = fmaxf(float((int)obs_source_get_width(source) - crop.left - crop.right), 0.0f);
  342. size.y = fmaxf(float((int)obs_source_get_height(source) - crop.top - crop.bottom), 0.0f);
  343. vec2_mul(&size, &size, &scale);
  344. }
  345. return size;
  346. }
  347. void OBSBasicPreview::GetStretchHandleData(const vec2 &pos, bool ignoreGroup)
  348. {
  349. OBSBasic *main = OBSBasic::Get();
  350. OBSScene scene = main->GetCurrentScene();
  351. if (!scene)
  352. return;
  353. float scale = main->previewScale / main->GetDevicePixelRatio();
  354. vec2 scaled_pos = pos;
  355. vec2_divf(&scaled_pos, &scaled_pos, scale);
  356. HandleFindData data(scaled_pos, scale);
  357. obs_scene_enum_items(scene, FindHandleAtPos, &data);
  358. stretchItem = std::move(data.item);
  359. stretchHandle = data.handle;
  360. rotateAngle = data.angle;
  361. rotatePoint = data.rotatePoint;
  362. offsetPoint = data.offsetPoint;
  363. if (stretchHandle != ItemHandle::None) {
  364. matrix4 boxTransform;
  365. vec3 itemUL;
  366. float itemRot;
  367. stretchItemSize = GetItemSize(stretchItem);
  368. obs_sceneitem_get_box_transform(stretchItem, &boxTransform);
  369. itemRot = obs_sceneitem_get_rot(stretchItem);
  370. vec3_from_vec4(&itemUL, &boxTransform.t);
  371. /* build the item space conversion matrices */
  372. matrix4_identity(&itemToScreen);
  373. matrix4_rotate_aa4f(&itemToScreen, &itemToScreen, 0.0f, 0.0f, 1.0f, RAD(itemRot));
  374. matrix4_translate3f(&itemToScreen, &itemToScreen, itemUL.x, itemUL.y, 0.0f);
  375. matrix4_identity(&screenToItem);
  376. matrix4_translate3f(&screenToItem, &screenToItem, -itemUL.x, -itemUL.y, 0.0f);
  377. matrix4_rotate_aa4f(&screenToItem, &screenToItem, 0.0f, 0.0f, 1.0f, RAD(-itemRot));
  378. obs_sceneitem_get_crop(stretchItem, &startCrop);
  379. obs_sceneitem_get_pos(stretchItem, &startItemPos);
  380. obs_source_t *source = obs_sceneitem_get_source(stretchItem);
  381. cropSize.x = float(obs_source_get_width(source) - startCrop.left - startCrop.right);
  382. cropSize.y = float(obs_source_get_height(source) - startCrop.top - startCrop.bottom);
  383. stretchGroup = obs_sceneitem_get_group(scene, stretchItem);
  384. if (stretchGroup && !ignoreGroup) {
  385. obs_sceneitem_get_draw_transform(stretchGroup, &invGroupTransform);
  386. matrix4_inv(&invGroupTransform, &invGroupTransform);
  387. obs_sceneitem_defer_group_resize_begin(stretchGroup);
  388. } else {
  389. stretchGroup = nullptr;
  390. }
  391. }
  392. }
  393. void OBSBasicPreview::keyPressEvent(QKeyEvent *event)
  394. {
  395. if (!IsFixedScaling() || event->isAutoRepeat()) {
  396. OBSQTDisplay::keyPressEvent(event);
  397. return;
  398. }
  399. switch (event->key()) {
  400. case Qt::Key_Space:
  401. setCursor(Qt::OpenHandCursor);
  402. scrollMode = true;
  403. break;
  404. }
  405. OBSQTDisplay::keyPressEvent(event);
  406. }
  407. void OBSBasicPreview::keyReleaseEvent(QKeyEvent *event)
  408. {
  409. if (event->isAutoRepeat()) {
  410. OBSQTDisplay::keyReleaseEvent(event);
  411. return;
  412. }
  413. switch (event->key()) {
  414. case Qt::Key_Space:
  415. scrollMode = false;
  416. setCursor(Qt::ArrowCursor);
  417. break;
  418. }
  419. OBSQTDisplay::keyReleaseEvent(event);
  420. }
  421. void OBSBasicPreview::wheelEvent(QWheelEvent *event)
  422. {
  423. if (scrollMode && IsFixedScaling()) {
  424. const int delta = event->angleDelta().y();
  425. if (delta != 0) {
  426. if (delta > 0)
  427. increaseScalingLevel();
  428. else
  429. decreaseScalingLevel();
  430. }
  431. }
  432. OBSQTDisplay::wheelEvent(event);
  433. }
  434. void OBSBasicPreview::mousePressEvent(QMouseEvent *event)
  435. {
  436. QPointF pos = event->position();
  437. if (scrollMode && IsFixedScaling() && event->button() == Qt::LeftButton) {
  438. setCursor(Qt::ClosedHandCursor);
  439. scrollingFrom.x = pos.x();
  440. scrollingFrom.y = pos.y();
  441. return;
  442. }
  443. if (event->button() == Qt::RightButton) {
  444. scrollMode = false;
  445. setCursor(Qt::ArrowCursor);
  446. }
  447. if (locked) {
  448. OBSQTDisplay::mousePressEvent(event);
  449. return;
  450. }
  451. OBSBasic *main = OBSBasic::Get();
  452. float pixelRatio = main->GetDevicePixelRatio();
  453. float x = pos.x() - main->previewX / pixelRatio;
  454. float y = pos.y() - main->previewY / pixelRatio;
  455. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  456. bool altDown = (modifiers & Qt::AltModifier);
  457. bool shiftDown = (modifiers & Qt::ShiftModifier);
  458. bool ctrlDown = (modifiers & Qt::ControlModifier);
  459. OBSQTDisplay::mousePressEvent(event);
  460. if (event->button() != Qt::LeftButton && event->button() != Qt::RightButton)
  461. return;
  462. if (event->button() == Qt::LeftButton)
  463. mouseDown = true;
  464. {
  465. std::lock_guard<std::mutex> lock(selectMutex);
  466. selectedItems.clear();
  467. }
  468. if (altDown)
  469. cropping = true;
  470. if (altDown || shiftDown || ctrlDown) {
  471. vec2 s;
  472. SceneFindBoxData data(s, s);
  473. obs_scene_enum_items(main->GetCurrentScene(), FindSelected, &data);
  474. std::lock_guard<std::mutex> lock(selectMutex);
  475. selectedItems = data.sceneItems;
  476. }
  477. vec2_set(&startPos, x, y);
  478. GetStretchHandleData(startPos, false);
  479. vec2_divf(&startPos, &startPos, main->previewScale / pixelRatio);
  480. startPos.x = std::round(startPos.x);
  481. startPos.y = std::round(startPos.y);
  482. mouseOverItems = SelectedAtPos(startPos);
  483. vec2_zero(&lastMoveOffset);
  484. mousePos = startPos;
  485. wrapper = obs_scene_save_transform_states(main->GetCurrentScene(), true);
  486. changed = false;
  487. }
  488. void OBSBasicPreview::UpdateCursor(uint32_t &flags)
  489. {
  490. if (!stretchItem || obs_sceneitem_locked(stretchItem)) {
  491. unsetCursor();
  492. return;
  493. }
  494. if (!flags && (cursor().shape() != Qt::OpenHandCursor || !scrollMode))
  495. unsetCursor();
  496. if ((cursor().shape() != Qt::ArrowCursor) || flags == 0)
  497. return;
  498. if (flags & ITEM_ROT) {
  499. setCursor(Qt::OpenHandCursor);
  500. return;
  501. }
  502. float rotation = obs_sceneitem_get_rot(stretchItem);
  503. vec2 scale;
  504. obs_sceneitem_get_scale(stretchItem, &scale);
  505. if (rotation < 0.0f)
  506. rotation = 360.0f + rotation;
  507. int octant = int(std::round(rotation / 45.0f));
  508. bool isCorner = (flags & (flags - 1)) != 0;
  509. if ((scale.x < 0.0f) && isCorner)
  510. flags ^= ITEM_LEFT | ITEM_RIGHT;
  511. if ((scale.y < 0.0f) && isCorner)
  512. flags ^= ITEM_TOP | ITEM_BOTTOM;
  513. if (octant % 4 >= 2) {
  514. if (isCorner) {
  515. flags ^= ITEM_TOP | ITEM_BOTTOM;
  516. } else {
  517. flags = (flags >> 2) | (flags << 2);
  518. }
  519. }
  520. if (octant % 2 == 1) {
  521. if (isCorner) {
  522. flags &= (flags % 3 == 0) ? ~ITEM_TOP & ~ITEM_BOTTOM : ~ITEM_LEFT & ~ITEM_RIGHT;
  523. } else {
  524. flags = (flags % 4 == 0) ? flags | flags >> ((flags / 2) - 1)
  525. : flags | ((flags >> 2) | (flags << 2));
  526. }
  527. }
  528. if ((flags & ITEM_LEFT && flags & ITEM_TOP) || (flags & ITEM_RIGHT && flags & ITEM_BOTTOM))
  529. setCursor(Qt::SizeFDiagCursor);
  530. else if ((flags & ITEM_LEFT && flags & ITEM_BOTTOM) || (flags & ITEM_RIGHT && flags & ITEM_TOP))
  531. setCursor(Qt::SizeBDiagCursor);
  532. else if (flags & ITEM_LEFT || flags & ITEM_RIGHT)
  533. setCursor(Qt::SizeHorCursor);
  534. else if (flags & ITEM_TOP || flags & ITEM_BOTTOM)
  535. setCursor(Qt::SizeVerCursor);
  536. }
  537. static bool select_one(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  538. {
  539. obs_sceneitem_t *selectedItem = static_cast<obs_sceneitem_t *>(param);
  540. if (obs_sceneitem_is_group(item))
  541. obs_sceneitem_group_enum_items(item, select_one, param);
  542. obs_sceneitem_select(item, (selectedItem == item));
  543. return true;
  544. }
  545. void OBSBasicPreview::DoSelect(const vec2 &pos)
  546. {
  547. OBSBasic *main = OBSBasic::Get();
  548. OBSScene scene = main->GetCurrentScene();
  549. OBSSceneItem item = GetItemAtPos(pos, true);
  550. obs_scene_enum_items(scene, select_one, (obs_sceneitem_t *)item);
  551. }
  552. void OBSBasicPreview::DoCtrlSelect(const vec2 &pos)
  553. {
  554. OBSSceneItem item = GetItemAtPos(pos, false);
  555. if (!item)
  556. return;
  557. bool selected = obs_sceneitem_selected(item);
  558. obs_sceneitem_select(item, !selected);
  559. }
  560. void OBSBasicPreview::ProcessClick(const vec2 &pos)
  561. {
  562. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  563. if (modifiers & Qt::ControlModifier)
  564. DoCtrlSelect(pos);
  565. else
  566. DoSelect(pos);
  567. }
  568. void OBSBasicPreview::mouseReleaseEvent(QMouseEvent *event)
  569. {
  570. if (scrollMode)
  571. setCursor(Qt::OpenHandCursor);
  572. if (locked) {
  573. OBSQTDisplay::mouseReleaseEvent(event);
  574. return;
  575. }
  576. if (mouseDown) {
  577. vec2 pos = GetMouseEventPos(event);
  578. if (!mouseMoved)
  579. ProcessClick(pos);
  580. if (selectionBox) {
  581. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  582. bool altDown = modifiers & Qt::AltModifier;
  583. bool shiftDown = modifiers & Qt::ShiftModifier;
  584. bool ctrlDown = modifiers & Qt::ControlModifier;
  585. std::lock_guard<std::mutex> lock(selectMutex);
  586. if (altDown || ctrlDown || shiftDown) {
  587. for (size_t i = 0; i < selectedItems.size(); i++) {
  588. obs_sceneitem_select(selectedItems[i], true);
  589. }
  590. }
  591. for (size_t i = 0; i < hoveredPreviewItems.size(); i++) {
  592. bool select = true;
  593. obs_sceneitem_t *item = hoveredPreviewItems[i];
  594. if (altDown) {
  595. select = false;
  596. } else if (ctrlDown) {
  597. select = !obs_sceneitem_selected(item);
  598. }
  599. obs_sceneitem_select(hoveredPreviewItems[i], select);
  600. }
  601. }
  602. if (stretchGroup) {
  603. obs_sceneitem_defer_group_resize_end(stretchGroup);
  604. }
  605. stretchItem = nullptr;
  606. stretchGroup = nullptr;
  607. mouseDown = false;
  608. mouseMoved = false;
  609. cropping = false;
  610. selectionBox = false;
  611. unsetCursor();
  612. OBSSceneItem item = GetItemAtPos(pos, true);
  613. std::lock_guard<std::mutex> lock(selectMutex);
  614. hoveredPreviewItems.clear();
  615. hoveredPreviewItems.push_back(item);
  616. selectedItems.clear();
  617. }
  618. OBSBasic *main = OBSBasic::Get();
  619. OBSDataAutoRelease rwrapper = obs_scene_save_transform_states(main->GetCurrentScene(), true);
  620. auto undo_redo = [](const std::string &data) {
  621. OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str());
  622. OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(dat, "scene_uuid"));
  623. OBSBasic::Get()->SetCurrentScene(source.Get(), true);
  624. obs_scene_load_transform_states(data.c_str());
  625. };
  626. if (wrapper && rwrapper) {
  627. std::string undo_data(obs_data_get_json(wrapper));
  628. std::string redo_data(obs_data_get_json(rwrapper));
  629. if (changed && undo_data.compare(redo_data) != 0)
  630. main->undo_s.add_action(
  631. QTStr("Undo.Transform").arg(obs_source_get_name(main->GetCurrentSceneSource())),
  632. undo_redo, undo_redo, undo_data, redo_data);
  633. }
  634. wrapper = nullptr;
  635. }
  636. struct SelectedItemBounds {
  637. bool first = true;
  638. vec3 tl, br;
  639. };
  640. static bool AddItemBounds(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  641. {
  642. SelectedItemBounds *data = static_cast<SelectedItemBounds *>(param);
  643. vec3 t[4];
  644. auto add_bounds = [data, &t]() {
  645. for (const vec3 &v : t) {
  646. if (data->first) {
  647. vec3_copy(&data->tl, &v);
  648. vec3_copy(&data->br, &v);
  649. data->first = false;
  650. } else {
  651. vec3_min(&data->tl, &data->tl, &v);
  652. vec3_max(&data->br, &data->br, &v);
  653. }
  654. }
  655. };
  656. if (obs_sceneitem_is_group(item)) {
  657. SelectedItemBounds sib;
  658. obs_sceneitem_group_enum_items(item, AddItemBounds, &sib);
  659. if (!sib.first) {
  660. matrix4 xform;
  661. obs_sceneitem_get_draw_transform(item, &xform);
  662. vec3_set(&t[0], sib.tl.x, sib.tl.y, 0.0f);
  663. vec3_set(&t[1], sib.tl.x, sib.br.y, 0.0f);
  664. vec3_set(&t[2], sib.br.x, sib.tl.y, 0.0f);
  665. vec3_set(&t[3], sib.br.x, sib.br.y, 0.0f);
  666. vec3_transform(&t[0], &t[0], &xform);
  667. vec3_transform(&t[1], &t[1], &xform);
  668. vec3_transform(&t[2], &t[2], &xform);
  669. vec3_transform(&t[3], &t[3], &xform);
  670. add_bounds();
  671. }
  672. }
  673. if (!obs_sceneitem_selected(item))
  674. return true;
  675. matrix4 boxTransform;
  676. obs_sceneitem_get_box_transform(item, &boxTransform);
  677. t[0] = GetTransformedPos(0.0f, 0.0f, boxTransform);
  678. t[1] = GetTransformedPos(1.0f, 0.0f, boxTransform);
  679. t[2] = GetTransformedPos(0.0f, 1.0f, boxTransform);
  680. t[3] = GetTransformedPos(1.0f, 1.0f, boxTransform);
  681. add_bounds();
  682. return true;
  683. }
  684. struct OffsetData {
  685. float clampDist;
  686. vec3 tl, br, offset;
  687. };
  688. static bool GetSourceSnapOffset(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  689. {
  690. OffsetData *data = static_cast<OffsetData *>(param);
  691. if (obs_sceneitem_selected(item))
  692. return true;
  693. matrix4 boxTransform;
  694. obs_sceneitem_get_box_transform(item, &boxTransform);
  695. vec3 t[4] = {GetTransformedPos(0.0f, 0.0f, boxTransform), GetTransformedPos(1.0f, 0.0f, boxTransform),
  696. GetTransformedPos(0.0f, 1.0f, boxTransform), GetTransformedPos(1.0f, 1.0f, boxTransform)};
  697. bool first = true;
  698. vec3 tl, br;
  699. vec3_zero(&tl);
  700. vec3_zero(&br);
  701. for (const vec3 &v : t) {
  702. if (first) {
  703. vec3_copy(&tl, &v);
  704. vec3_copy(&br, &v);
  705. first = false;
  706. } else {
  707. vec3_min(&tl, &tl, &v);
  708. vec3_max(&br, &br, &v);
  709. }
  710. }
  711. // Snap to other source edges
  712. #define EDGE_SNAP(l, r, x, y) \
  713. do { \
  714. double dist = fabsf(l.x - data->r.x); \
  715. if (dist < data->clampDist && fabsf(data->offset.x) < EPSILON && data->tl.y < br.y && \
  716. data->br.y > tl.y && (fabsf(data->offset.x) > dist || data->offset.x < EPSILON)) \
  717. data->offset.x = l.x - data->r.x; \
  718. } while (false)
  719. EDGE_SNAP(tl, br, x, y);
  720. EDGE_SNAP(tl, br, y, x);
  721. EDGE_SNAP(br, tl, x, y);
  722. EDGE_SNAP(br, tl, y, x);
  723. #undef EDGE_SNAP
  724. return true;
  725. }
  726. void OBSBasicPreview::SnapItemMovement(vec2 &offset)
  727. {
  728. OBSBasic *main = OBSBasic::Get();
  729. OBSScene scene = main->GetCurrentScene();
  730. SelectedItemBounds data;
  731. obs_scene_enum_items(scene, AddItemBounds, &data);
  732. data.tl.x += offset.x;
  733. data.tl.y += offset.y;
  734. data.br.x += offset.x;
  735. data.br.y += offset.y;
  736. vec3 snapOffset = GetSnapOffset(data.tl, data.br);
  737. const bool snap = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SnappingEnabled");
  738. const bool sourcesSnap = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SourceSnapping");
  739. if (snap == false)
  740. return;
  741. if (sourcesSnap == false) {
  742. offset.x += snapOffset.x;
  743. offset.y += snapOffset.y;
  744. return;
  745. }
  746. const float clampDist =
  747. config_get_double(App()->GetUserConfig(), "BasicWindow", "SnapDistance") / main->previewScale;
  748. OffsetData offsetData;
  749. offsetData.clampDist = clampDist;
  750. offsetData.tl = data.tl;
  751. offsetData.br = data.br;
  752. vec3_copy(&offsetData.offset, &snapOffset);
  753. obs_scene_enum_items(scene, GetSourceSnapOffset, &offsetData);
  754. if (fabsf(offsetData.offset.x) > EPSILON || fabsf(offsetData.offset.y) > EPSILON) {
  755. offset.x += offsetData.offset.x;
  756. offset.y += offsetData.offset.y;
  757. } else {
  758. offset.x += snapOffset.x;
  759. offset.y += snapOffset.y;
  760. }
  761. }
  762. static bool move_items(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  763. {
  764. if (obs_sceneitem_locked(item))
  765. return true;
  766. bool selected = obs_sceneitem_selected(item);
  767. vec2 *offset = static_cast<vec2 *>(param);
  768. if (obs_sceneitem_is_group(item) && !selected) {
  769. matrix4 transform;
  770. vec3 new_offset;
  771. vec3_set(&new_offset, offset->x, offset->y, 0.0f);
  772. obs_sceneitem_get_draw_transform(item, &transform);
  773. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  774. matrix4_inv(&transform, &transform);
  775. vec3_transform(&new_offset, &new_offset, &transform);
  776. obs_sceneitem_group_enum_items(item, move_items, &new_offset);
  777. }
  778. if (selected) {
  779. vec2 pos;
  780. obs_sceneitem_get_pos(item, &pos);
  781. vec2_add(&pos, &pos, offset);
  782. obs_sceneitem_set_pos(item, &pos);
  783. }
  784. return true;
  785. }
  786. void OBSBasicPreview::MoveItems(const vec2 &pos)
  787. {
  788. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  789. OBSBasic *main = OBSBasic::Get();
  790. OBSScene scene = main->GetCurrentScene();
  791. vec2 offset, moveOffset;
  792. vec2_sub(&offset, &pos, &startPos);
  793. vec2_sub(&moveOffset, &offset, &lastMoveOffset);
  794. if (!(modifiers & Qt::ControlModifier))
  795. SnapItemMovement(moveOffset);
  796. vec2_add(&lastMoveOffset, &lastMoveOffset, &moveOffset);
  797. obs_scene_enum_items(scene, move_items, &moveOffset);
  798. }
  799. static bool CounterClockwise(float x1, float x2, float x3, float y1, float y2, float y3)
  800. {
  801. return (y3 - y1) * (x2 - x1) > (y2 - y1) * (x3 - x1);
  802. }
  803. static bool IntersectLine(float x1, float x2, float x3, float x4, float y1, float y2, float y3, float y4)
  804. {
  805. bool a = CounterClockwise(x1, x2, x3, y1, y2, y3);
  806. bool b = CounterClockwise(x1, x2, x4, y1, y2, y4);
  807. bool c = CounterClockwise(x3, x4, x1, y3, y4, y1);
  808. bool d = CounterClockwise(x3, x4, x2, y3, y4, y2);
  809. return (a != b) && (c != d);
  810. }
  811. static bool IntersectBox(matrix4 transform, float x1, float x2, float y1, float y2)
  812. {
  813. float x3, x4, y3, y4;
  814. x3 = transform.t.x;
  815. y3 = transform.t.y;
  816. x4 = x3 + transform.x.x;
  817. y4 = y3 + transform.x.y;
  818. if (IntersectLine(x1, x1, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y1, y1, y3, y4) ||
  819. IntersectLine(x2, x2, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y2, y2, y3, y4))
  820. return true;
  821. x4 = x3 + transform.y.x;
  822. y4 = y3 + transform.y.y;
  823. if (IntersectLine(x1, x1, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y1, y1, y3, y4) ||
  824. IntersectLine(x2, x2, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y2, y2, y3, y4))
  825. return true;
  826. x3 = transform.t.x + transform.x.x;
  827. y3 = transform.t.y + transform.x.y;
  828. x4 = x3 + transform.y.x;
  829. y4 = y3 + transform.y.y;
  830. if (IntersectLine(x1, x1, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y1, y1, y3, y4) ||
  831. IntersectLine(x2, x2, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y2, y2, y3, y4))
  832. return true;
  833. x3 = transform.t.x + transform.y.x;
  834. y3 = transform.t.y + transform.y.y;
  835. x4 = x3 + transform.x.x;
  836. y4 = y3 + transform.x.y;
  837. if (IntersectLine(x1, x1, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y1, y1, y3, y4) ||
  838. IntersectLine(x2, x2, x3, x4, y1, y2, y3, y4) || IntersectLine(x1, x2, x3, x4, y2, y2, y3, y4))
  839. return true;
  840. return false;
  841. }
  842. #undef PI
  843. bool OBSBasicPreview::FindSelected(obs_scene_t *, obs_sceneitem_t *item, void *param)
  844. {
  845. SceneFindBoxData *data = static_cast<SceneFindBoxData *>(param);
  846. if (obs_sceneitem_selected(item))
  847. data->sceneItems.push_back(item);
  848. return true;
  849. }
  850. static bool FindItemsInBox(obs_scene_t * /* scene */, obs_sceneitem_t *item, void *param)
  851. {
  852. SceneFindBoxData *data = static_cast<SceneFindBoxData *>(param);
  853. matrix4 transform;
  854. matrix4 invTransform;
  855. vec3 transformedPos;
  856. vec3 pos3;
  857. vec3 pos3_;
  858. vec2 pos_min, pos_max;
  859. vec2_min(&pos_min, &data->startPos, &data->pos);
  860. vec2_max(&pos_max, &data->startPos, &data->pos);
  861. const float x1 = pos_min.x;
  862. const float x2 = pos_max.x;
  863. const float y1 = pos_min.y;
  864. const float y2 = pos_max.y;
  865. if (!SceneItemHasVideo(item))
  866. return true;
  867. if (obs_sceneitem_locked(item))
  868. return true;
  869. if (!obs_sceneitem_visible(item))
  870. return true;
  871. vec3_set(&pos3, data->pos.x, data->pos.y, 0.0f);
  872. obs_sceneitem_get_box_transform(item, &transform);
  873. matrix4_inv(&invTransform, &transform);
  874. vec3_transform(&transformedPos, &pos3, &invTransform);
  875. vec3_transform(&pos3_, &transformedPos, &transform);
  876. if (CloseFloat(pos3.x, pos3_.x) && CloseFloat(pos3.y, pos3_.y) && transformedPos.x >= 0.0f &&
  877. transformedPos.x <= 1.0f && transformedPos.y >= 0.0f && transformedPos.y <= 1.0f) {
  878. data->sceneItems.push_back(item);
  879. return true;
  880. }
  881. if (transform.t.x > x1 && transform.t.x < x2 && transform.t.y > y1 && transform.t.y < y2) {
  882. data->sceneItems.push_back(item);
  883. return true;
  884. }
  885. if (transform.t.x + transform.x.x > x1 && transform.t.x + transform.x.x < x2 &&
  886. transform.t.y + transform.x.y > y1 && transform.t.y + transform.x.y < y2) {
  887. data->sceneItems.push_back(item);
  888. return true;
  889. }
  890. if (transform.t.x + transform.y.x > x1 && transform.t.x + transform.y.x < x2 &&
  891. transform.t.y + transform.y.y > y1 && transform.t.y + transform.y.y < y2) {
  892. data->sceneItems.push_back(item);
  893. return true;
  894. }
  895. if (transform.t.x + transform.x.x + transform.y.x > x1 && transform.t.x + transform.x.x + transform.y.x < x2 &&
  896. transform.t.y + transform.x.y + transform.y.y > y1 && transform.t.y + transform.x.y + transform.y.y < y2) {
  897. data->sceneItems.push_back(item);
  898. return true;
  899. }
  900. if (transform.t.x + 0.5 * (transform.x.x + transform.y.x) > x1 &&
  901. transform.t.x + 0.5 * (transform.x.x + transform.y.x) < x2 &&
  902. transform.t.y + 0.5 * (transform.x.y + transform.y.y) > y1 &&
  903. transform.t.y + 0.5 * (transform.x.y + transform.y.y) < y2) {
  904. data->sceneItems.push_back(item);
  905. return true;
  906. }
  907. if (IntersectBox(transform, x1, x2, y1, y2)) {
  908. data->sceneItems.push_back(item);
  909. return true;
  910. }
  911. return true;
  912. }
  913. void OBSBasicPreview::BoxItems(const vec2 &startPos, const vec2 &pos)
  914. {
  915. OBSBasic *main = OBSBasic::Get();
  916. OBSScene scene = main->GetCurrentScene();
  917. if (!scene)
  918. return;
  919. if (cursor().shape() != Qt::CrossCursor)
  920. setCursor(Qt::CrossCursor);
  921. SceneFindBoxData data(startPos, pos);
  922. obs_scene_enum_items(scene, FindItemsInBox, &data);
  923. std::lock_guard<std::mutex> lock(selectMutex);
  924. hoveredPreviewItems = data.sceneItems;
  925. }
  926. vec3 OBSBasicPreview::CalculateStretchPos(const vec3 &tl, const vec3 &br)
  927. {
  928. uint32_t alignment = obs_sceneitem_get_alignment(stretchItem);
  929. vec3 pos;
  930. vec3_zero(&pos);
  931. if (alignment & OBS_ALIGN_LEFT)
  932. pos.x = tl.x;
  933. else if (alignment & OBS_ALIGN_RIGHT)
  934. pos.x = br.x;
  935. else
  936. pos.x = (br.x - tl.x) * 0.5f + tl.x;
  937. if (alignment & OBS_ALIGN_TOP)
  938. pos.y = tl.y;
  939. else if (alignment & OBS_ALIGN_BOTTOM)
  940. pos.y = br.y;
  941. else
  942. pos.y = (br.y - tl.y) * 0.5f + tl.y;
  943. return pos;
  944. }
  945. void OBSBasicPreview::ClampAspect(vec3 &tl, vec3 &br, vec2 &size, const vec2 &baseSize)
  946. {
  947. float baseAspect = baseSize.x / baseSize.y;
  948. float aspect = size.x / size.y;
  949. uint32_t stretchFlags = (uint32_t)stretchHandle;
  950. if (stretchHandle == ItemHandle::TopLeft || stretchHandle == ItemHandle::TopRight ||
  951. stretchHandle == ItemHandle::BottomLeft || stretchHandle == ItemHandle::BottomRight) {
  952. if (aspect < baseAspect) {
  953. if ((size.y >= 0.0f && size.x >= 0.0f) || (size.y <= 0.0f && size.x <= 0.0f))
  954. size.x = size.y * baseAspect;
  955. else
  956. size.x = size.y * baseAspect * -1.0f;
  957. } else {
  958. if ((size.y >= 0.0f && size.x >= 0.0f) || (size.y <= 0.0f && size.x <= 0.0f))
  959. size.y = size.x / baseAspect;
  960. else
  961. size.y = size.x / baseAspect * -1.0f;
  962. }
  963. } else if (stretchHandle == ItemHandle::TopCenter || stretchHandle == ItemHandle::BottomCenter) {
  964. if ((size.y >= 0.0f && size.x >= 0.0f) || (size.y <= 0.0f && size.x <= 0.0f))
  965. size.x = size.y * baseAspect;
  966. else
  967. size.x = size.y * baseAspect * -1.0f;
  968. } else if (stretchHandle == ItemHandle::CenterLeft || stretchHandle == ItemHandle::CenterRight) {
  969. if ((size.y >= 0.0f && size.x >= 0.0f) || (size.y <= 0.0f && size.x <= 0.0f))
  970. size.y = size.x / baseAspect;
  971. else
  972. size.y = size.x / baseAspect * -1.0f;
  973. }
  974. size.x = std::round(size.x);
  975. size.y = std::round(size.y);
  976. if (stretchFlags & ITEM_LEFT)
  977. tl.x = br.x - size.x;
  978. else if (stretchFlags & ITEM_RIGHT)
  979. br.x = tl.x + size.x;
  980. if (stretchFlags & ITEM_TOP)
  981. tl.y = br.y - size.y;
  982. else if (stretchFlags & ITEM_BOTTOM)
  983. br.y = tl.y + size.y;
  984. }
  985. void OBSBasicPreview::SnapStretchingToScreen(vec3 &tl, vec3 &br)
  986. {
  987. uint32_t stretchFlags = (uint32_t)stretchHandle;
  988. vec3 newTL = GetTransformedPos(tl.x, tl.y, itemToScreen);
  989. vec3 newTR = GetTransformedPos(br.x, tl.y, itemToScreen);
  990. vec3 newBL = GetTransformedPos(tl.x, br.y, itemToScreen);
  991. vec3 newBR = GetTransformedPos(br.x, br.y, itemToScreen);
  992. vec3 boundingTL;
  993. vec3 boundingBR;
  994. vec3_copy(&boundingTL, &newTL);
  995. vec3_min(&boundingTL, &boundingTL, &newTR);
  996. vec3_min(&boundingTL, &boundingTL, &newBL);
  997. vec3_min(&boundingTL, &boundingTL, &newBR);
  998. vec3_copy(&boundingBR, &newTL);
  999. vec3_max(&boundingBR, &boundingBR, &newTR);
  1000. vec3_max(&boundingBR, &boundingBR, &newBL);
  1001. vec3_max(&boundingBR, &boundingBR, &newBR);
  1002. vec3 offset = GetSnapOffset(boundingTL, boundingBR);
  1003. vec3_add(&offset, &offset, &newTL);
  1004. vec3_transform(&offset, &offset, &screenToItem);
  1005. vec3_sub(&offset, &offset, &tl);
  1006. if (stretchFlags & ITEM_LEFT)
  1007. tl.x += offset.x;
  1008. else if (stretchFlags & ITEM_RIGHT)
  1009. br.x += offset.x;
  1010. if (stretchFlags & ITEM_TOP)
  1011. tl.y += offset.y;
  1012. else if (stretchFlags & ITEM_BOTTOM)
  1013. br.y += offset.y;
  1014. }
  1015. static float maxfunc(float x, float y)
  1016. {
  1017. return x > y ? x : y;
  1018. }
  1019. static float minfunc(float x, float y)
  1020. {
  1021. return x < y ? x : y;
  1022. }
  1023. void OBSBasicPreview::CropItem(const vec2 &pos)
  1024. {
  1025. obs_bounds_type boundsType = obs_sceneitem_get_bounds_type(stretchItem);
  1026. uint32_t stretchFlags = (uint32_t)stretchHandle;
  1027. uint32_t align = obs_sceneitem_get_alignment(stretchItem);
  1028. vec3 tl, br, pos3;
  1029. vec3_zero(&tl);
  1030. vec3_set(&br, stretchItemSize.x, stretchItemSize.y, 0.0f);
  1031. vec3_set(&pos3, pos.x, pos.y, 0.0f);
  1032. vec3_transform(&pos3, &pos3, &screenToItem);
  1033. obs_sceneitem_crop crop = startCrop;
  1034. vec2 scale, rawscale;
  1035. obs_sceneitem_get_scale(stretchItem, &rawscale);
  1036. vec2_set(&scale, boundsType == OBS_BOUNDS_NONE ? rawscale.x : fabsf(rawscale.x),
  1037. boundsType == OBS_BOUNDS_NONE ? rawscale.y : fabsf(rawscale.y));
  1038. vec2 max_tl;
  1039. vec2 max_br;
  1040. vec2_set(&max_tl, float(-crop.left) * scale.x, float(-crop.top) * scale.y);
  1041. vec2_set(&max_br, stretchItemSize.x + crop.right * scale.x, stretchItemSize.y + crop.bottom * scale.y);
  1042. typedef std::function<float(float, float)> minmax_func_t;
  1043. minmax_func_t min_x = scale.x < 0.0f && boundsType == OBS_BOUNDS_NONE ? maxfunc : minfunc;
  1044. minmax_func_t min_y = scale.y < 0.0f && boundsType == OBS_BOUNDS_NONE ? maxfunc : minfunc;
  1045. minmax_func_t max_x = scale.x < 0.0f && boundsType == OBS_BOUNDS_NONE ? minfunc : maxfunc;
  1046. minmax_func_t max_y = scale.y < 0.0f && boundsType == OBS_BOUNDS_NONE ? minfunc : maxfunc;
  1047. pos3.x = min_x(pos3.x, max_br.x);
  1048. pos3.x = max_x(pos3.x, max_tl.x);
  1049. pos3.y = min_y(pos3.y, max_br.y);
  1050. pos3.y = max_y(pos3.y, max_tl.y);
  1051. if (stretchFlags & ITEM_LEFT) {
  1052. float maxX = stretchItemSize.x - (2.0 * scale.x);
  1053. pos3.x = tl.x = min_x(pos3.x, maxX);
  1054. } else if (stretchFlags & ITEM_RIGHT) {
  1055. float minX = (2.0 * scale.x);
  1056. pos3.x = br.x = max_x(pos3.x, minX);
  1057. }
  1058. if (stretchFlags & ITEM_TOP) {
  1059. float maxY = stretchItemSize.y - (2.0 * scale.y);
  1060. pos3.y = tl.y = min_y(pos3.y, maxY);
  1061. } else if (stretchFlags & ITEM_BOTTOM) {
  1062. float minY = (2.0 * scale.y);
  1063. pos3.y = br.y = max_y(pos3.y, minY);
  1064. }
  1065. #define ALIGN_X (ITEM_LEFT | ITEM_RIGHT)
  1066. #define ALIGN_Y (ITEM_TOP | ITEM_BOTTOM)
  1067. vec3 newPos;
  1068. vec3_zero(&newPos);
  1069. uint32_t align_x = (align & ALIGN_X);
  1070. uint32_t align_y = (align & ALIGN_Y);
  1071. if (align_x == (stretchFlags & ALIGN_X) && align_x != 0)
  1072. newPos.x = pos3.x;
  1073. else if (align & ITEM_RIGHT)
  1074. newPos.x = stretchItemSize.x;
  1075. else if (!(align & ITEM_LEFT))
  1076. newPos.x = stretchItemSize.x * 0.5f;
  1077. if (align_y == (stretchFlags & ALIGN_Y) && align_y != 0)
  1078. newPos.y = pos3.y;
  1079. else if (align & ITEM_BOTTOM)
  1080. newPos.y = stretchItemSize.y;
  1081. else if (!(align & ITEM_TOP))
  1082. newPos.y = stretchItemSize.y * 0.5f;
  1083. #undef ALIGN_X
  1084. #undef ALIGN_Y
  1085. crop = startCrop;
  1086. if (stretchFlags & ITEM_LEFT)
  1087. crop.left += int(std::round(tl.x / scale.x));
  1088. else if (stretchFlags & ITEM_RIGHT)
  1089. crop.right += int(std::round((stretchItemSize.x - br.x) / scale.x));
  1090. if (stretchFlags & ITEM_TOP)
  1091. crop.top += int(std::round(tl.y / scale.y));
  1092. else if (stretchFlags & ITEM_BOTTOM)
  1093. crop.bottom += int(std::round((stretchItemSize.y - br.y) / scale.y));
  1094. vec3_transform(&newPos, &newPos, &itemToScreen);
  1095. newPos.x = std::round(newPos.x);
  1096. newPos.y = std::round(newPos.y);
  1097. #if 0
  1098. vec3 curPos;
  1099. vec3_zero(&curPos);
  1100. obs_sceneitem_get_pos(stretchItem, (vec2*)&curPos);
  1101. blog(LOG_DEBUG, "curPos {%d, %d} - newPos {%d, %d}",
  1102. int(curPos.x), int(curPos.y),
  1103. int(newPos.x), int(newPos.y));
  1104. blog(LOG_DEBUG, "crop {%d, %d, %d, %d}",
  1105. crop.left, crop.top,
  1106. crop.right, crop.bottom);
  1107. #endif
  1108. obs_sceneitem_defer_update_begin(stretchItem);
  1109. obs_sceneitem_set_crop(stretchItem, &crop);
  1110. if (boundsType == OBS_BOUNDS_NONE)
  1111. obs_sceneitem_set_pos(stretchItem, (vec2 *)&newPos);
  1112. obs_sceneitem_defer_update_end(stretchItem);
  1113. }
  1114. void OBSBasicPreview::StretchItem(const vec2 &pos)
  1115. {
  1116. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  1117. obs_bounds_type boundsType = obs_sceneitem_get_bounds_type(stretchItem);
  1118. uint32_t stretchFlags = (uint32_t)stretchHandle;
  1119. bool shiftDown = (modifiers & Qt::ShiftModifier);
  1120. vec3 tl, br, pos3;
  1121. vec3_zero(&tl);
  1122. vec3_set(&br, stretchItemSize.x, stretchItemSize.y, 0.0f);
  1123. vec3_set(&pos3, pos.x, pos.y, 0.0f);
  1124. vec3_transform(&pos3, &pos3, &screenToItem);
  1125. if (stretchFlags & ITEM_LEFT)
  1126. tl.x = pos3.x;
  1127. else if (stretchFlags & ITEM_RIGHT)
  1128. br.x = pos3.x;
  1129. if (stretchFlags & ITEM_TOP)
  1130. tl.y = pos3.y;
  1131. else if (stretchFlags & ITEM_BOTTOM)
  1132. br.y = pos3.y;
  1133. if (!(modifiers & Qt::ControlModifier))
  1134. SnapStretchingToScreen(tl, br);
  1135. obs_source_t *source = obs_sceneitem_get_source(stretchItem);
  1136. uint32_t source_cx = obs_source_get_width(source);
  1137. uint32_t source_cy = obs_source_get_height(source);
  1138. /* if the source's internal size has been set to 0 for whatever reason
  1139. * while resizing, do not update transform, otherwise source will be
  1140. * stuck invisible until a complete transform reset */
  1141. if (!source_cx || !source_cy)
  1142. return;
  1143. vec2 baseSize;
  1144. vec2_set(&baseSize, float(source_cx), float(source_cy));
  1145. vec2 size;
  1146. vec2_set(&size, br.x - tl.x, br.y - tl.y);
  1147. if (boundsType != OBS_BOUNDS_NONE) {
  1148. if (shiftDown)
  1149. ClampAspect(tl, br, size, baseSize);
  1150. if (tl.x > br.x)
  1151. std::swap(tl.x, br.x);
  1152. if (tl.y > br.y)
  1153. std::swap(tl.y, br.y);
  1154. vec2_abs(&size, &size);
  1155. obs_sceneitem_set_bounds(stretchItem, &size);
  1156. } else {
  1157. obs_sceneitem_crop crop;
  1158. obs_sceneitem_get_crop(stretchItem, &crop);
  1159. baseSize.x -= float(crop.left + crop.right);
  1160. baseSize.y -= float(crop.top + crop.bottom);
  1161. if (!shiftDown)
  1162. ClampAspect(tl, br, size, baseSize);
  1163. vec2_div(&size, &size, &baseSize);
  1164. obs_sceneitem_set_scale(stretchItem, &size);
  1165. }
  1166. pos3 = CalculateStretchPos(tl, br);
  1167. vec3_transform(&pos3, &pos3, &itemToScreen);
  1168. vec2 newPos;
  1169. vec2_set(&newPos, std::round(pos3.x), std::round(pos3.y));
  1170. obs_sceneitem_set_pos(stretchItem, &newPos);
  1171. }
  1172. void OBSBasicPreview::RotateItem(const vec2 &pos)
  1173. {
  1174. OBSBasic *main = OBSBasic::Get();
  1175. OBSScene scene = main->GetCurrentScene();
  1176. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  1177. bool shiftDown = (modifiers & Qt::ShiftModifier);
  1178. bool ctrlDown = (modifiers & Qt::ControlModifier);
  1179. vec2 pos2;
  1180. vec2_copy(&pos2, &pos);
  1181. float angle = atan2(pos2.y - rotatePoint.y, pos2.x - rotatePoint.x) + RAD(90);
  1182. #define ROT_SNAP(rot, thresh) \
  1183. if (abs(angle - RAD(rot)) < RAD(thresh)) { \
  1184. angle = RAD(rot); \
  1185. }
  1186. if (shiftDown) {
  1187. for (int i = 0; i <= 360 / 15; i++) {
  1188. ROT_SNAP(i * 15 - 90, 7.5);
  1189. }
  1190. } else if (!ctrlDown) {
  1191. ROT_SNAP(rotateAngle, 5)
  1192. ROT_SNAP(-90, 5)
  1193. ROT_SNAP(-45, 5)
  1194. ROT_SNAP(0, 5)
  1195. ROT_SNAP(45, 5)
  1196. ROT_SNAP(90, 5)
  1197. ROT_SNAP(135, 5)
  1198. ROT_SNAP(180, 5)
  1199. ROT_SNAP(225, 5)
  1200. ROT_SNAP(270, 5)
  1201. ROT_SNAP(315, 5)
  1202. }
  1203. #undef ROT_SNAP
  1204. vec2 pos3;
  1205. vec2_copy(&pos3, &offsetPoint);
  1206. RotatePos(&pos3, angle);
  1207. pos3.x += rotatePoint.x;
  1208. pos3.y += rotatePoint.y;
  1209. obs_sceneitem_set_rot(stretchItem, DEG(angle));
  1210. obs_sceneitem_set_pos(stretchItem, &pos3);
  1211. }
  1212. void OBSBasicPreview::mouseMoveEvent(QMouseEvent *event)
  1213. {
  1214. OBSBasic *main = OBSBasic::Get();
  1215. changed = true;
  1216. QPointF qtPos = event->position();
  1217. float pixelRatio = main->GetDevicePixelRatio();
  1218. if (scrollMode && event->buttons() == Qt::LeftButton) {
  1219. scrollingOffset.x += pixelRatio * (qtPos.x() - scrollingFrom.x);
  1220. scrollingOffset.y += pixelRatio * (qtPos.y() - scrollingFrom.y);
  1221. scrollingFrom.x = qtPos.x();
  1222. scrollingFrom.y = qtPos.y();
  1223. emit DisplayResized();
  1224. return;
  1225. }
  1226. if (locked)
  1227. return;
  1228. bool updateCursor = false;
  1229. if (mouseDown) {
  1230. vec2 pos = GetMouseEventPos(event);
  1231. if (!mouseMoved && !mouseOverItems && stretchHandle == ItemHandle::None) {
  1232. ProcessClick(startPos);
  1233. mouseOverItems = SelectedAtPos(startPos);
  1234. }
  1235. pos.x = std::round(pos.x);
  1236. pos.y = std::round(pos.y);
  1237. if (stretchHandle != ItemHandle::None) {
  1238. if (obs_sceneitem_locked(stretchItem))
  1239. return;
  1240. selectionBox = false;
  1241. OBSScene scene = main->GetCurrentScene();
  1242. obs_sceneitem_t *group = obs_sceneitem_get_group(scene, stretchItem);
  1243. if (group) {
  1244. vec3 group_pos;
  1245. vec3_set(&group_pos, pos.x, pos.y, 0.0f);
  1246. vec3_transform(&group_pos, &group_pos, &invGroupTransform);
  1247. pos.x = group_pos.x;
  1248. pos.y = group_pos.y;
  1249. }
  1250. if (stretchHandle == ItemHandle::Rot) {
  1251. RotateItem(pos);
  1252. setCursor(Qt::ClosedHandCursor);
  1253. } else if (cropping)
  1254. CropItem(pos);
  1255. else
  1256. StretchItem(pos);
  1257. } else if (mouseOverItems) {
  1258. if (cursor().shape() != Qt::SizeAllCursor)
  1259. setCursor(Qt::SizeAllCursor);
  1260. selectionBox = false;
  1261. MoveItems(pos);
  1262. } else {
  1263. selectionBox = true;
  1264. if (!mouseMoved)
  1265. DoSelect(startPos);
  1266. BoxItems(startPos, pos);
  1267. }
  1268. mouseMoved = true;
  1269. mousePos = pos;
  1270. } else {
  1271. vec2 pos = GetMouseEventPos(event);
  1272. OBSSceneItem item = GetItemAtPos(pos, true);
  1273. std::lock_guard<std::mutex> lock(selectMutex);
  1274. hoveredPreviewItems.clear();
  1275. hoveredPreviewItems.push_back(item);
  1276. if (!mouseMoved && hoveredPreviewItems.size() > 0) {
  1277. mousePos = pos;
  1278. OBSBasic *main = OBSBasic::Get();
  1279. float scale = main->GetDevicePixelRatio();
  1280. float x = qtPos.x() - main->previewX / scale;
  1281. float y = qtPos.y() - main->previewY / scale;
  1282. vec2_set(&startPos, x, y);
  1283. updateCursor = true;
  1284. }
  1285. }
  1286. if (updateCursor) {
  1287. GetStretchHandleData(startPos, true);
  1288. uint32_t stretchFlags = (uint32_t)stretchHandle;
  1289. UpdateCursor(stretchFlags);
  1290. }
  1291. }
  1292. void OBSBasicPreview::leaveEvent(QEvent *)
  1293. {
  1294. std::lock_guard<std::mutex> lock(selectMutex);
  1295. if (!selectionBox)
  1296. hoveredPreviewItems.clear();
  1297. }
  1298. static void DrawLine(float x1, float y1, float x2, float y2, float thickness, vec2 scale)
  1299. {
  1300. float cx;
  1301. float cy;
  1302. vec2 thickness_relative;
  1303. bool is_x_axis = !close_float(x1, x2, TINY_EPSILON) || close_float(y1, y2, TINY_EPSILON);
  1304. vec2_abs(&scale, &scale);
  1305. thickness_relative.x = thickness / scale.x;
  1306. thickness_relative.y = thickness / scale.y;
  1307. if (is_x_axis) {
  1308. cx = fabsf(x2 - x1) + thickness_relative.x;
  1309. cy = thickness_relative.y;
  1310. } else {
  1311. cy = fabsf(y2 - y1) + thickness_relative.y;
  1312. cx = thickness_relative.x;
  1313. }
  1314. x1 -= thickness_relative.x * 0.5f;
  1315. y1 -= thickness_relative.y * 0.5f;
  1316. gs_matrix_push();
  1317. gs_matrix_translate3f(x1, y1, 0.0f);
  1318. gs_draw_quadf(NULL, 0, cx, cy);
  1319. gs_matrix_pop();
  1320. }
  1321. static void DrawSquareAtPos(float x, float y, float pixelRatio)
  1322. {
  1323. struct vec3 pos;
  1324. vec3_set(&pos, x, y, 0.0f);
  1325. struct matrix4 matrix;
  1326. gs_matrix_get(&matrix);
  1327. vec3_transform(&pos, &pos, &matrix);
  1328. gs_matrix_push();
  1329. gs_matrix_identity();
  1330. gs_matrix_translate(&pos);
  1331. gs_matrix_translate3f(-HANDLE_RADIUS * pixelRatio, -HANDLE_RADIUS * pixelRatio, 0.0f);
  1332. gs_matrix_scale3f(HANDLE_RADIUS * pixelRatio * 2, HANDLE_RADIUS * pixelRatio * 2, 1.0f);
  1333. gs_draw(GS_TRISTRIP, 0, 0);
  1334. gs_matrix_pop();
  1335. }
  1336. static void DrawRotationHandle(gs_vertbuffer_t *circle, float rot, float pixelRatio, bool invert)
  1337. {
  1338. struct vec3 pos;
  1339. vec3_set(&pos, 0.5f, invert ? 1.0f : 0.0f, 0.0f);
  1340. struct matrix4 matrix;
  1341. gs_matrix_get(&matrix);
  1342. vec3_transform(&pos, &pos, &matrix);
  1343. constexpr float thickness = 0.68f;
  1344. gs_matrix_push();
  1345. gs_matrix_identity();
  1346. gs_matrix_translate(&pos);
  1347. gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(rot));
  1348. gs_matrix_translate3f(-HANDLE_RADIUS * 1.5 * pixelRatio, -HANDLE_RADIUS * 1.5 * pixelRatio, 0.0f);
  1349. gs_matrix_scale3f(HANDLE_RADIUS * 3 * pixelRatio, HANDLE_RADIUS * 3 * pixelRatio, 1.0f);
  1350. gs_matrix_push();
  1351. gs_matrix_translate3f(0.5f - thickness / 2.0f / HANDLE_RADIUS, -2.0f, 0.0f);
  1352. gs_draw_quadf(NULL, 0, thickness / HANDLE_RADIUS, 2.5f);
  1353. gs_matrix_pop();
  1354. gs_matrix_translate3f(0.0f, -HANDLE_RADIUS * 2 / 3, 0.0f);
  1355. gs_load_vertexbuffer(circle);
  1356. gs_draw(GS_TRISTRIP, 0, 0);
  1357. gs_matrix_pop();
  1358. }
  1359. void OBSBasicPreview::DrawStripedLine(float x1, float y1, float x2, float y2, float thickness, vec2 scale)
  1360. {
  1361. float cx;
  1362. float cy;
  1363. float dist;
  1364. float dist_scaled;
  1365. vec2 thickness_relative;
  1366. bool is_x_axis = !close_float(x1, x2, TINY_EPSILON) || close_float(y1, y2, TINY_EPSILON);
  1367. vec2_abs(&scale, &scale);
  1368. thickness_relative.x = thickness / scale.x;
  1369. thickness_relative.y = thickness / scale.y;
  1370. if (is_x_axis) {
  1371. dist = fabsf(x2 - x1);
  1372. dist_scaled = dist * scale.x;
  1373. cx = dist + thickness_relative.x;
  1374. cy = thickness_relative.y;
  1375. } else {
  1376. dist = fabsf(y2 - y1);
  1377. dist_scaled = dist * scale.y;
  1378. cy = dist + thickness_relative.y;
  1379. cx = thickness_relative.x;
  1380. }
  1381. x1 -= thickness_relative.x * 0.5f;
  1382. y1 -= thickness_relative.y * 0.5f;
  1383. float stripe_length = dist_scaled / 15.0f;
  1384. float f_stripes_inv = 1.0f / (dist_scaled / stripe_length);
  1385. struct vec2 size;
  1386. struct vec2 count_inv;
  1387. if (is_x_axis) {
  1388. vec2_set(&size, dist_scaled, 0.0f);
  1389. vec2_set(&count_inv, f_stripes_inv, 0.0f);
  1390. } else {
  1391. vec2_set(&size, 0.0f, dist_scaled);
  1392. vec2_set(&count_inv, 0.0f, f_stripes_inv);
  1393. }
  1394. gs_eparam_t *size_param = gs_effect_get_param_by_name(stripedLineEffect, "size");
  1395. gs_eparam_t *count_inv_param = gs_effect_get_param_by_name(stripedLineEffect, "count_inv");
  1396. gs_effect_set_vec2(size_param, &size);
  1397. gs_effect_set_vec2(count_inv_param, &count_inv);
  1398. gs_matrix_push();
  1399. gs_matrix_translate3f(x1, y1, 0.0f);
  1400. while (gs_effect_loop(stripedLineEffect, "StripedLine")) {
  1401. gs_draw_quadf(nullptr, 0, cx, cy);
  1402. }
  1403. gs_matrix_pop();
  1404. }
  1405. static void DrawRect(float thickness, vec2 scale)
  1406. {
  1407. DrawLine(0.0f, 0.0f, 0.0f, 1.0f, thickness, scale);
  1408. DrawLine(0.0f, 0.0f, 1.0f, 0.0f, thickness, scale);
  1409. DrawLine(1.0f, 0.0f, 1.0f, 1.0f, thickness, scale);
  1410. DrawLine(0.0f, 1.0f, 1.0f, 1.0f, thickness, scale);
  1411. }
  1412. static inline bool crop_enabled(const obs_sceneitem_crop *crop)
  1413. {
  1414. return crop->left > 0 || crop->top > 0 || crop->right > 0 || crop->bottom > 0;
  1415. }
  1416. bool OBSBasicPreview::DrawSelectedOverflow(obs_scene_t *, obs_sceneitem_t *item, void *param)
  1417. {
  1418. if (obs_sceneitem_locked(item))
  1419. return true;
  1420. if (!SceneItemHasVideo(item))
  1421. return true;
  1422. OBSBasicPreview *prev = static_cast<OBSBasicPreview *>(param);
  1423. if (!prev->GetOverflowSelectionHidden() && !obs_sceneitem_visible(item))
  1424. return true;
  1425. if (obs_sceneitem_is_group(item)) {
  1426. matrix4 mat;
  1427. obs_sceneitem_get_draw_transform(item, &mat);
  1428. gs_matrix_push();
  1429. gs_matrix_mul(&mat);
  1430. obs_sceneitem_group_enum_items(item, DrawSelectedOverflow, param);
  1431. gs_matrix_pop();
  1432. }
  1433. if (!prev->GetOverflowAlwaysVisible() && !obs_sceneitem_selected(item))
  1434. return true;
  1435. matrix4 boxTransform;
  1436. matrix4 invBoxTransform;
  1437. obs_sceneitem_get_box_transform(item, &boxTransform);
  1438. matrix4_inv(&invBoxTransform, &boxTransform);
  1439. vec3 bounds[] = {
  1440. {{{0.f, 0.f, 0.f}}},
  1441. {{{1.f, 0.f, 0.f}}},
  1442. {{{0.f, 1.f, 0.f}}},
  1443. {{{1.f, 1.f, 0.f}}},
  1444. };
  1445. bool visible = std::all_of(std::begin(bounds), std::end(bounds), [&](const vec3 &b) {
  1446. vec3 pos;
  1447. vec3_transform(&pos, &b, &boxTransform);
  1448. vec3_transform(&pos, &pos, &invBoxTransform);
  1449. return CloseFloat(pos.x, b.x) && CloseFloat(pos.y, b.y);
  1450. });
  1451. if (!visible)
  1452. return true;
  1453. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawSelectedOverflow");
  1454. obs_transform_info info;
  1455. obs_sceneitem_get_info2(item, &info);
  1456. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_REPEAT);
  1457. gs_eparam_t *image = gs_effect_get_param_by_name(solid, "image");
  1458. gs_eparam_t *scale = gs_effect_get_param_by_name(solid, "scale");
  1459. vec2 s;
  1460. vec2_set(&s, boxTransform.x.x / 96, boxTransform.y.y / 96);
  1461. gs_effect_set_vec2(scale, &s);
  1462. gs_effect_set_texture_srgb(image, prev->overflow);
  1463. gs_matrix_push();
  1464. gs_matrix_mul(&boxTransform);
  1465. obs_sceneitem_crop crop;
  1466. obs_sceneitem_get_crop(item, &crop);
  1467. const bool currentFramebufferSrgbState = gs_framebuffer_srgb_enabled();
  1468. gs_enable_framebuffer_srgb(true);
  1469. while (gs_effect_loop(solid, "Draw")) {
  1470. gs_draw_sprite(prev->overflow, 0, 1, 1);
  1471. }
  1472. gs_enable_framebuffer_srgb(currentFramebufferSrgbState);
  1473. gs_matrix_pop();
  1474. GS_DEBUG_MARKER_END();
  1475. return true;
  1476. }
  1477. bool OBSBasicPreview::DrawSelectedItem(obs_scene_t *, obs_sceneitem_t *item, void *param)
  1478. {
  1479. if (obs_sceneitem_locked(item))
  1480. return true;
  1481. if (!SceneItemHasVideo(item))
  1482. return true;
  1483. OBSBasicPreview *prev = static_cast<OBSBasicPreview *>(param);
  1484. if (obs_sceneitem_is_group(item)) {
  1485. matrix4 mat;
  1486. obs_transform_info groupInfo;
  1487. obs_sceneitem_get_draw_transform(item, &mat);
  1488. obs_sceneitem_get_info2(item, &groupInfo);
  1489. prev->groupRot = groupInfo.rot;
  1490. gs_matrix_push();
  1491. gs_matrix_mul(&mat);
  1492. obs_sceneitem_group_enum_items(item, DrawSelectedItem, prev);
  1493. gs_matrix_pop();
  1494. prev->groupRot = 0.0f;
  1495. }
  1496. OBSBasic *main = OBSBasic::Get();
  1497. float pixelRatio = main->GetDevicePixelRatio();
  1498. bool hovered = false;
  1499. {
  1500. std::lock_guard<std::mutex> lock(prev->selectMutex);
  1501. for (size_t i = 0; i < prev->hoveredPreviewItems.size(); i++) {
  1502. if (prev->hoveredPreviewItems[i] == item) {
  1503. hovered = true;
  1504. break;
  1505. }
  1506. }
  1507. }
  1508. bool selected = obs_sceneitem_selected(item);
  1509. if (!selected && !hovered)
  1510. return true;
  1511. matrix4 boxTransform;
  1512. matrix4 invBoxTransform;
  1513. obs_sceneitem_get_box_transform(item, &boxTransform);
  1514. matrix4_inv(&invBoxTransform, &boxTransform);
  1515. vec3 bounds[] = {
  1516. {{{0.f, 0.f, 0.f}}},
  1517. {{{1.f, 0.f, 0.f}}},
  1518. {{{0.f, 1.f, 0.f}}},
  1519. {{{1.f, 1.f, 0.f}}},
  1520. };
  1521. main->GetCameraIcon();
  1522. QColor selColor = main->GetSelectionColor();
  1523. QColor cropColor = main->GetCropColor();
  1524. QColor hoverColor = main->GetHoverColor();
  1525. vec4 red;
  1526. vec4 green;
  1527. vec4 blue;
  1528. vec4_set(&red, selColor.redF(), selColor.greenF(), selColor.blueF(), 1.0f);
  1529. vec4_set(&green, cropColor.redF(), cropColor.greenF(), cropColor.blueF(), 1.0f);
  1530. vec4_set(&blue, hoverColor.redF(), hoverColor.greenF(), hoverColor.blueF(), 1.0f);
  1531. bool visible = std::all_of(std::begin(bounds), std::end(bounds), [&](const vec3 &b) {
  1532. vec3 pos;
  1533. vec3_transform(&pos, &b, &boxTransform);
  1534. vec3_transform(&pos, &pos, &invBoxTransform);
  1535. return CloseFloat(pos.x, b.x) && CloseFloat(pos.y, b.y);
  1536. });
  1537. if (!visible)
  1538. return true;
  1539. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawSelectedItem");
  1540. matrix4 curTransform;
  1541. vec2 boxScale;
  1542. gs_matrix_get(&curTransform);
  1543. obs_sceneitem_get_box_scale(item, &boxScale);
  1544. boxScale.x *= curTransform.x.x;
  1545. boxScale.y *= curTransform.y.y;
  1546. obs_transform_info info;
  1547. obs_sceneitem_get_info2(item, &info);
  1548. gs_matrix_push();
  1549. gs_matrix_mul(&boxTransform);
  1550. obs_sceneitem_crop crop;
  1551. obs_sceneitem_get_crop(item, &crop);
  1552. if (info.bounds_type == OBS_BOUNDS_NONE && crop_enabled(&crop)) {
  1553. #define DRAW_SIDE(side, x1, y1, x2, y2) \
  1554. if (hovered && !selected) { \
  1555. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color"); \
  1556. gs_effect_set_vec4(colParam, &blue); \
  1557. while (gs_effect_loop(prev->solidEffect, "Solid")) { \
  1558. DrawLine(x1, y1, x2, y2, HANDLE_RADIUS *pixelRatio / 2, boxScale); \
  1559. } \
  1560. } else if (crop.side > 0) { \
  1561. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->stripedLineEffect, "color"); \
  1562. gs_effect_set_vec4(colParam, &green); \
  1563. prev->DrawStripedLine(x1, y1, x2, y2, HANDLE_RADIUS *pixelRatio / 2, boxScale); \
  1564. } else { \
  1565. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color"); \
  1566. gs_effect_set_vec4(colParam, &red); \
  1567. while (gs_effect_loop(prev->solidEffect, "Solid")) { \
  1568. DrawLine(x1, y1, x2, y2, HANDLE_RADIUS *pixelRatio / 2, boxScale); \
  1569. } \
  1570. }
  1571. DRAW_SIDE(left, 0.0f, 0.0f, 0.0f, 1.0f);
  1572. DRAW_SIDE(top, 0.0f, 0.0f, 1.0f, 0.0f);
  1573. DRAW_SIDE(right, 1.0f, 0.0f, 1.0f, 1.0f);
  1574. DRAW_SIDE(bottom, 0.0f, 1.0f, 1.0f, 1.0f);
  1575. #undef DRAW_SIDE
  1576. } else {
  1577. if (!selected) {
  1578. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color");
  1579. gs_effect_set_vec4(colParam, &blue);
  1580. while (gs_effect_loop(prev->solidEffect, "Solid")) {
  1581. DrawRect(HANDLE_RADIUS * pixelRatio / 2, boxScale);
  1582. }
  1583. } else {
  1584. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color");
  1585. gs_effect_set_vec4(colParam, &red);
  1586. while (gs_effect_loop(prev->solidEffect, "Solid")) {
  1587. DrawRect(HANDLE_RADIUS * pixelRatio / 2, boxScale);
  1588. }
  1589. }
  1590. }
  1591. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color");
  1592. gs_technique_t *tech = gs_effect_get_technique(prev->solidEffect, "Solid");
  1593. gs_technique_begin(tech);
  1594. gs_technique_begin_pass(tech, 0);
  1595. gs_load_vertexbuffer(main->box);
  1596. gs_effect_set_vec4(colParam, &red);
  1597. if (selected) {
  1598. DrawSquareAtPos(0.0f, 0.0f, pixelRatio);
  1599. DrawSquareAtPos(0.0f, 1.0f, pixelRatio);
  1600. DrawSquareAtPos(1.0f, 0.0f, pixelRatio);
  1601. DrawSquareAtPos(1.0f, 1.0f, pixelRatio);
  1602. DrawSquareAtPos(0.5f, 0.0f, pixelRatio);
  1603. DrawSquareAtPos(0.0f, 0.5f, pixelRatio);
  1604. DrawSquareAtPos(0.5f, 1.0f, pixelRatio);
  1605. DrawSquareAtPos(1.0f, 0.5f, pixelRatio);
  1606. if (!prev->circleFill) {
  1607. gs_render_start(true);
  1608. float angle = 180;
  1609. for (int i = 0, l = 40; i < l; i++) {
  1610. gs_vertex2f(sin(RAD(angle)) / 2 + 0.5f, cos(RAD(angle)) / 2 + 0.5f);
  1611. angle += 360 / l;
  1612. gs_vertex2f(sin(RAD(angle)) / 2 + 0.5f, cos(RAD(angle)) / 2 + 0.5f);
  1613. gs_vertex2f(0.5f, 1.0f);
  1614. }
  1615. prev->circleFill = gs_render_save();
  1616. }
  1617. bool invert = info.scale.y < 0.0f && info.bounds_type == OBS_BOUNDS_NONE;
  1618. DrawRotationHandle(prev->circleFill, info.rot + prev->groupRot, pixelRatio, invert);
  1619. }
  1620. gs_matrix_pop();
  1621. gs_technique_end_pass(tech);
  1622. gs_technique_end(tech);
  1623. GS_DEBUG_MARKER_END();
  1624. return true;
  1625. }
  1626. bool OBSBasicPreview::DrawSelectionBox(float x1, float y1, float x2, float y2, gs_vertbuffer_t *rectFill)
  1627. {
  1628. OBSBasic *main = OBSBasic::Get();
  1629. float pixelRatio = main->GetDevicePixelRatio();
  1630. x1 = std::round(x1);
  1631. x2 = std::round(x2);
  1632. y1 = std::round(y1);
  1633. y2 = std::round(y2);
  1634. gs_effect_t *eff = gs_get_effect();
  1635. gs_eparam_t *colParam = gs_effect_get_param_by_name(eff, "color");
  1636. vec4 fillColor;
  1637. vec4_set(&fillColor, 0.7f, 0.7f, 0.7f, 0.5f);
  1638. vec4 borderColor;
  1639. vec4_set(&borderColor, 1.0f, 1.0f, 1.0f, 1.0f);
  1640. vec2 scale;
  1641. vec2_set(&scale, std::abs(x2 - x1), std::abs(y2 - y1));
  1642. gs_matrix_push();
  1643. gs_matrix_identity();
  1644. gs_matrix_translate3f(x1, y1, 0.0f);
  1645. gs_matrix_scale3f(x2 - x1, y2 - y1, 1.0f);
  1646. gs_effect_set_vec4(colParam, &fillColor);
  1647. gs_load_vertexbuffer(rectFill);
  1648. gs_draw(GS_TRISTRIP, 0, 0);
  1649. gs_effect_set_vec4(colParam, &borderColor);
  1650. DrawRect(HANDLE_RADIUS * pixelRatio / 2, scale);
  1651. gs_matrix_pop();
  1652. return true;
  1653. }
  1654. void OBSBasicPreview::DrawOverflow()
  1655. {
  1656. if (locked)
  1657. return;
  1658. if (overflowHidden)
  1659. return;
  1660. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawOverflow");
  1661. if (!overflow) {
  1662. std::string path;
  1663. GetDataFilePath("images/overflow.png", path);
  1664. overflow = gs_texture_create_from_file(path.c_str());
  1665. }
  1666. OBSBasic *main = OBSBasic::Get();
  1667. OBSScene scene = main->GetCurrentScene();
  1668. if (scene) {
  1669. gs_matrix_push();
  1670. gs_matrix_scale3f(main->previewScale, main->previewScale, 1.0f);
  1671. obs_scene_enum_items(scene, DrawSelectedOverflow, this);
  1672. gs_matrix_pop();
  1673. }
  1674. gs_load_vertexbuffer(nullptr);
  1675. GS_DEBUG_MARKER_END();
  1676. }
  1677. void OBSBasicPreview::DrawSceneEditing()
  1678. {
  1679. if (locked)
  1680. return;
  1681. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawSceneEditing");
  1682. OBSBasic *main = OBSBasic::Get();
  1683. OBSScene scene = main->GetCurrentScene();
  1684. if (scene) {
  1685. gs_matrix_push();
  1686. gs_matrix_scale3f(main->previewScale, main->previewScale, 1.0f);
  1687. obs_scene_enum_items(scene, DrawSelectedItem, this);
  1688. gs_matrix_pop();
  1689. }
  1690. if (selectionBox) {
  1691. if (!rectFill) {
  1692. gs_render_start(true);
  1693. gs_vertex2f(0.0f, 0.0f);
  1694. gs_vertex2f(1.0f, 0.0f);
  1695. gs_vertex2f(0.0f, 1.0f);
  1696. gs_vertex2f(1.0f, 1.0f);
  1697. rectFill = gs_render_save();
  1698. }
  1699. while (gs_effect_loop(solidEffect, "Solid")) {
  1700. DrawSelectionBox(startPos.x * main->previewScale, startPos.y * main->previewScale,
  1701. mousePos.x * main->previewScale, mousePos.y * main->previewScale, rectFill);
  1702. }
  1703. }
  1704. gs_load_vertexbuffer(nullptr);
  1705. GS_DEBUG_MARKER_END();
  1706. }
  1707. void OBSBasicPreview::ResetScrollingOffset()
  1708. {
  1709. vec2_zero(&scrollingOffset);
  1710. }
  1711. void OBSBasicPreview::SetScalingLevel(int32_t newScalingLevelVal)
  1712. {
  1713. newScalingLevelVal = std::clamp(newScalingLevelVal, -MAX_SCALING_LEVEL, MAX_SCALING_LEVEL);
  1714. float newScalingAmountVal = pow(ZOOM_SENSITIVITY, float(newScalingLevelVal));
  1715. scalingLevel = newScalingLevelVal;
  1716. SetScalingAmount(newScalingAmountVal);
  1717. }
  1718. void OBSBasicPreview::SetScalingAmount(float newScalingAmountVal)
  1719. {
  1720. scrollingOffset.x *= newScalingAmountVal / scalingAmount;
  1721. scrollingOffset.y *= newScalingAmountVal / scalingAmount;
  1722. if (scalingAmount == newScalingAmountVal)
  1723. return;
  1724. scalingAmount = newScalingAmountVal;
  1725. emit scalingChanged(scalingAmount);
  1726. if (!fixedScaling) {
  1727. scalingLevel = std::round(log(scalingAmount) / log(ZOOM_SENSITIVITY));
  1728. }
  1729. }
  1730. void OBSBasicPreview::SetScalingLevelAndAmount(int32_t newScalingLevelVal, float newScalingAmountVal)
  1731. {
  1732. newScalingLevelVal = std::clamp(newScalingLevelVal, -MAX_SCALING_LEVEL, MAX_SCALING_LEVEL);
  1733. scalingLevel = newScalingLevelVal;
  1734. SetScalingAmount(newScalingAmountVal);
  1735. }
  1736. void OBSBasicPreview::increaseScalingLevel()
  1737. {
  1738. SetFixedScaling(true);
  1739. SetScalingLevel(scalingLevel + 1);
  1740. emit DisplayResized();
  1741. }
  1742. void OBSBasicPreview::decreaseScalingLevel()
  1743. {
  1744. SetFixedScaling(true);
  1745. SetScalingLevel(scalingLevel - 1);
  1746. emit DisplayResized();
  1747. }
  1748. void OBSBasicPreview::resetScalingLevel()
  1749. {
  1750. SetScalingLevel(0);
  1751. ResetScrollingOffset();
  1752. emit DisplayResized();
  1753. }
  1754. OBSBasicPreview *OBSBasicPreview::Get()
  1755. {
  1756. return OBSBasic::Get()->ui->preview;
  1757. }
  1758. static obs_source_t *CreateLabel(float pixelRatio, int i)
  1759. {
  1760. OBSDataAutoRelease settings = obs_data_create();
  1761. OBSDataAutoRelease font = obs_data_create();
  1762. #if defined(_WIN32)
  1763. obs_data_set_string(font, "face", "Arial");
  1764. #elif defined(__APPLE__)
  1765. obs_data_set_string(font, "face", "Helvetica");
  1766. #else
  1767. obs_data_set_string(font, "face", "Monospace");
  1768. #endif
  1769. obs_data_set_int(font, "flags", 1); // Bold text
  1770. obs_data_set_int(font, "size", 16 * pixelRatio);
  1771. obs_data_set_obj(settings, "font", font);
  1772. obs_data_set_bool(settings, "outline", true);
  1773. #ifdef _WIN32
  1774. obs_data_set_int(settings, "outline_color", 0x000000);
  1775. obs_data_set_int(settings, "outline_size", 3);
  1776. const char *text_source_id = "text_gdiplus";
  1777. #else
  1778. const char *text_source_id = "text_ft2_source";
  1779. #endif
  1780. DStr name;
  1781. dstr_printf(name, "Preview spacing label %d", i);
  1782. return obs_source_create_private(text_source_id, name, settings);
  1783. }
  1784. static void SetLabelText(int sourceIndex, int px)
  1785. {
  1786. OBSBasicPreview *prev = OBSBasicPreview::Get();
  1787. if (px == prev->spacerPx[sourceIndex])
  1788. return;
  1789. std::string text = std::to_string(px) + " px";
  1790. obs_source_t *source = prev->spacerLabel[sourceIndex];
  1791. OBSDataAutoRelease settings = obs_source_get_settings(source);
  1792. obs_data_set_string(settings, "text", text.c_str());
  1793. obs_source_update(source, settings);
  1794. prev->spacerPx[sourceIndex] = px;
  1795. }
  1796. static void DrawLabel(OBSSource source, vec3 &pos, vec3 &viewport)
  1797. {
  1798. if (!source)
  1799. return;
  1800. vec3_mul(&pos, &pos, &viewport);
  1801. gs_matrix_push();
  1802. gs_matrix_identity();
  1803. gs_matrix_translate(&pos);
  1804. obs_source_video_render(source);
  1805. gs_matrix_pop();
  1806. }
  1807. static void DrawSpacingLine(vec3 &start, vec3 &end, vec3 &viewport, float pixelRatio)
  1808. {
  1809. OBSBasic *main = OBSBasic::Get();
  1810. matrix4 transform;
  1811. matrix4_identity(&transform);
  1812. transform.x.x = viewport.x;
  1813. transform.y.y = viewport.y;
  1814. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  1815. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  1816. QColor selColor = main->GetSelectionColor();
  1817. vec4 color;
  1818. vec4_set(&color, selColor.redF(), selColor.greenF(), selColor.blueF(), 1.0f);
  1819. gs_effect_set_vec4(gs_effect_get_param_by_name(solid, "color"), &color);
  1820. gs_technique_begin(tech);
  1821. gs_technique_begin_pass(tech, 0);
  1822. gs_matrix_push();
  1823. gs_matrix_mul(&transform);
  1824. vec2 scale;
  1825. vec2_set(&scale, viewport.x, viewport.y);
  1826. DrawLine(start.x, start.y, end.x, end.y, pixelRatio * (HANDLE_RADIUS / 2), scale);
  1827. gs_matrix_pop();
  1828. gs_load_vertexbuffer(nullptr);
  1829. gs_technique_end_pass(tech);
  1830. gs_technique_end(tech);
  1831. }
  1832. static void RenderSpacingHelper(int sourceIndex, vec3 &start, vec3 &end, vec3 &viewport, float pixelRatio)
  1833. {
  1834. bool horizontal = (sourceIndex == 2 || sourceIndex == 3);
  1835. // If outside of preview, don't render
  1836. if (!((horizontal && (end.x >= start.x)) || (!horizontal && (end.y >= start.y))))
  1837. return;
  1838. float length = vec3_dist(&start, &end);
  1839. obs_video_info ovi;
  1840. obs_get_video_info(&ovi);
  1841. float px;
  1842. if (horizontal) {
  1843. px = length * ovi.base_width;
  1844. } else {
  1845. px = length * ovi.base_height;
  1846. }
  1847. if (px <= 0.0f)
  1848. return;
  1849. OBSBasicPreview *prev = OBSBasicPreview::Get();
  1850. obs_source_t *source = prev->spacerLabel[sourceIndex];
  1851. vec3 labelSize, labelPos;
  1852. vec3_set(&labelSize, obs_source_get_width(source), obs_source_get_height(source), 1.0f);
  1853. vec3_div(&labelSize, &labelSize, &viewport);
  1854. vec3 labelMargin;
  1855. vec3_set(&labelMargin, SPACER_LABEL_MARGIN * pixelRatio, SPACER_LABEL_MARGIN * pixelRatio, 1.0f);
  1856. vec3_div(&labelMargin, &labelMargin, &viewport);
  1857. vec3_set(&labelPos, end.x, end.y, end.z);
  1858. if (horizontal) {
  1859. labelPos.x -= (end.x - start.x) / 2;
  1860. labelPos.x -= labelSize.x / 2;
  1861. labelPos.y -= labelMargin.y + (labelSize.y / 2) + (HANDLE_RADIUS / viewport.y);
  1862. } else {
  1863. labelPos.y -= (end.y - start.y) / 2;
  1864. labelPos.y -= labelSize.y / 2;
  1865. labelPos.x += labelMargin.x;
  1866. }
  1867. DrawSpacingLine(start, end, viewport, pixelRatio);
  1868. SetLabelText(sourceIndex, (int)px);
  1869. DrawLabel(source, labelPos, viewport);
  1870. }
  1871. void OBSBasicPreview::DrawSpacingHelpers()
  1872. {
  1873. if (locked)
  1874. return;
  1875. OBSBasic *main = OBSBasic::Get();
  1876. vec2 s;
  1877. SceneFindBoxData data(s, s);
  1878. OBSScene scene = main->GetCurrentScene();
  1879. obs_scene_enum_items(scene, FindSelected, &data);
  1880. if (data.sceneItems.size() != 1)
  1881. return;
  1882. OBSSceneItem item = data.sceneItems[0];
  1883. if (!item)
  1884. return;
  1885. if (obs_sceneitem_locked(item))
  1886. return;
  1887. vec2 itemSize = GetItemSize(item);
  1888. if (itemSize.x == 0.0f || itemSize.y == 0.0f)
  1889. return;
  1890. obs_sceneitem_t *parentGroup = obs_sceneitem_get_group(scene, item);
  1891. if (parentGroup && obs_sceneitem_locked(parentGroup))
  1892. return;
  1893. matrix4 boxTransform;
  1894. obs_sceneitem_get_box_transform(item, &boxTransform);
  1895. obs_transform_info oti;
  1896. obs_sceneitem_get_info2(item, &oti);
  1897. obs_video_info ovi;
  1898. obs_get_video_info(&ovi);
  1899. vec3 size;
  1900. vec3_set(&size, ovi.base_width, ovi.base_height, 1.0f);
  1901. // Init box transform side locations
  1902. vec3 left, right, top, bottom;
  1903. vec3_set(&left, 0.0f, 0.5f, 1.0f);
  1904. vec3_set(&right, 1.0f, 0.5f, 1.0f);
  1905. vec3_set(&top, 0.5f, 0.0f, 1.0f);
  1906. vec3_set(&bottom, 0.5f, 1.0f, 1.0f);
  1907. // Decide which side to use with box transform, based on rotation
  1908. // Seems hacky, probably a better way to do it
  1909. float rot = oti.rot;
  1910. if (parentGroup) {
  1911. obs_transform_info groupOti;
  1912. obs_sceneitem_get_info2(parentGroup, &groupOti);
  1913. //Correct the scene item rotation angle
  1914. rot = oti.rot + groupOti.rot;
  1915. // Correct the scene item box transform
  1916. // Based on scale, rotation angle, position of parent's group
  1917. matrix4_scale3f(&boxTransform, &boxTransform, groupOti.scale.x, groupOti.scale.y, 1.0f);
  1918. matrix4_rotate_aa4f(&boxTransform, &boxTransform, 0.0f, 0.0f, 1.0f, RAD(groupOti.rot));
  1919. matrix4_translate3f(&boxTransform, &boxTransform, groupOti.pos.x, groupOti.pos.y, 0.0f);
  1920. }
  1921. // Switch top/bottom or right/left if scale is negative
  1922. if (oti.scale.x < 0.0f && oti.bounds_type == OBS_BOUNDS_NONE) {
  1923. vec3 l = left;
  1924. vec3 r = right;
  1925. vec3_copy(&left, &r);
  1926. vec3_copy(&right, &l);
  1927. }
  1928. if (oti.scale.y < 0.0f && oti.bounds_type == OBS_BOUNDS_NONE) {
  1929. vec3 t = top;
  1930. vec3 b = bottom;
  1931. vec3_copy(&top, &b);
  1932. vec3_copy(&bottom, &t);
  1933. }
  1934. if (rot >= HELPER_ROT_BREAKPOINT) {
  1935. for (float i = HELPER_ROT_BREAKPOINT; i <= 360.0f; i += 90.0f) {
  1936. if (rot < i)
  1937. break;
  1938. vec3 l = left;
  1939. vec3 r = right;
  1940. vec3 t = top;
  1941. vec3 b = bottom;
  1942. vec3_copy(&top, &l);
  1943. vec3_copy(&right, &t);
  1944. vec3_copy(&bottom, &r);
  1945. vec3_copy(&left, &b);
  1946. }
  1947. } else if (rot <= -HELPER_ROT_BREAKPOINT) {
  1948. for (float i = -HELPER_ROT_BREAKPOINT; i >= -360.0f; i -= 90.0f) {
  1949. if (rot > i)
  1950. break;
  1951. vec3 l = left;
  1952. vec3 r = right;
  1953. vec3 t = top;
  1954. vec3 b = bottom;
  1955. vec3_copy(&top, &r);
  1956. vec3_copy(&right, &b);
  1957. vec3_copy(&bottom, &l);
  1958. vec3_copy(&left, &t);
  1959. }
  1960. }
  1961. // Get sides of box transform
  1962. left = GetTransformedPos(left.x, left.y, boxTransform);
  1963. right = GetTransformedPos(right.x, right.y, boxTransform);
  1964. top = GetTransformedPos(top.x, top.y, boxTransform);
  1965. bottom = GetTransformedPos(bottom.x, bottom.y, boxTransform);
  1966. bottom.y = size.y - bottom.y;
  1967. right.x = size.x - right.x;
  1968. // Init viewport
  1969. vec3 viewport;
  1970. vec3_set(&viewport, main->previewCX, main->previewCY, 1.0f);
  1971. vec3_div(&left, &left, &viewport);
  1972. vec3_div(&right, &right, &viewport);
  1973. vec3_div(&top, &top, &viewport);
  1974. vec3_div(&bottom, &bottom, &viewport);
  1975. vec3_mulf(&left, &left, main->previewScale);
  1976. vec3_mulf(&right, &right, main->previewScale);
  1977. vec3_mulf(&top, &top, main->previewScale);
  1978. vec3_mulf(&bottom, &bottom, main->previewScale);
  1979. // Draw spacer lines and labels
  1980. vec3 start, end;
  1981. float pixelRatio = main->GetDevicePixelRatio();
  1982. for (int i = 0; i < 4; i++) {
  1983. if (!spacerLabel[i])
  1984. spacerLabel[i] = CreateLabel(pixelRatio, i);
  1985. }
  1986. vec3_set(&start, top.x, 0.0f, 1.0f);
  1987. vec3_set(&end, top.x, top.y, 1.0f);
  1988. RenderSpacingHelper(0, start, end, viewport, pixelRatio);
  1989. vec3_set(&start, bottom.x, 1.0f - bottom.y, 1.0f);
  1990. vec3_set(&end, bottom.x, 1.0f, 1.0f);
  1991. RenderSpacingHelper(1, start, end, viewport, pixelRatio);
  1992. vec3_set(&start, 0.0f, left.y, 1.0f);
  1993. vec3_set(&end, left.x, left.y, 1.0f);
  1994. RenderSpacingHelper(2, start, end, viewport, pixelRatio);
  1995. vec3_set(&start, 1.0f - right.x, right.y, 1.0f);
  1996. vec3_set(&end, 1.0f, right.y, 1.0f);
  1997. RenderSpacingHelper(3, start, end, viewport, pixelRatio);
  1998. }
  1999. void OBSBasicPreview::ClampScrollingOffsets()
  2000. {
  2001. obs_video_info ovi;
  2002. obs_get_video_info(&ovi);
  2003. QSize targetSize = GetPixelSize(this);
  2004. vec3 target, offset;
  2005. vec3_set(&target, (float)targetSize.width(), (float)targetSize.height(), 1.0f);
  2006. vec3_set(&offset, (float)ovi.base_width, (float)ovi.base_height, 1.0f);
  2007. vec3_mulf(&offset, &offset, scalingAmount);
  2008. vec3_sub(&offset, &offset, &target);
  2009. vec3_mulf(&offset, &offset, 0.5f);
  2010. vec3_maxf(&offset, &offset, 0.0f);
  2011. vec3_divf(&target, &target, 2.0f);
  2012. vec3_add(&offset, &offset, &target);
  2013. scrollingOffset.x = std::clamp(scrollingOffset.x, -offset.x, offset.x);
  2014. scrollingOffset.y = std::clamp(scrollingOffset.y, -offset.y, offset.y);
  2015. UpdateXScrollBar(offset.x);
  2016. UpdateYScrollBar(offset.y);
  2017. }
  2018. void OBSBasicPreview::xScrollBarChanged(int value)
  2019. {
  2020. updatingXScrollBar = true;
  2021. scrollingOffset.x = float(-value);
  2022. updatingXScrollBar = false;
  2023. emit DisplayResized();
  2024. }
  2025. void OBSBasicPreview::yScrollBarChanged(int value)
  2026. {
  2027. updatingYScrollBar = true;
  2028. scrollingOffset.y = float(-value);
  2029. updatingYScrollBar = false;
  2030. emit DisplayResized();
  2031. }
  2032. void OBSBasicPreview::UpdateXScrollBar(float cx)
  2033. {
  2034. if (updatingXScrollBar)
  2035. return;
  2036. OBSBasic *main = OBSBasic::Get();
  2037. if (!main->ui->previewXScrollBar->isVisible())
  2038. return;
  2039. main->ui->previewXScrollBar->setRange(int(-cx), int(cx));
  2040. QSize targetSize = GetPixelSize(this);
  2041. main->ui->previewXScrollBar->setPageStep(targetSize.width() / std::min(scalingAmount, 1.0f));
  2042. QSignalBlocker sig(main->ui->previewXScrollBar);
  2043. main->ui->previewXScrollBar->setValue(int(-scrollingOffset.x));
  2044. }
  2045. void OBSBasicPreview::UpdateYScrollBar(float cy)
  2046. {
  2047. if (updatingYScrollBar)
  2048. return;
  2049. OBSBasic *main = OBSBasic::Get();
  2050. if (!main->ui->previewYScrollBar->isVisible())
  2051. return;
  2052. main->ui->previewYScrollBar->setRange(int(-cy), int(cy));
  2053. QSize targetSize = GetPixelSize(this);
  2054. main->ui->previewYScrollBar->setPageStep(targetSize.height() / std::min(scalingAmount, 1.0f));
  2055. QSignalBlocker sig(main->ui->previewYScrollBar);
  2056. main->ui->previewYScrollBar->setValue(int(-scrollingOffset.y));
  2057. }