OBSBasicPreview.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  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 = reinterpret_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 = reinterpret_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 = *reinterpret_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 = reinterpret_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 = reinterpret_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 = reinterpret_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 = reinterpret_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 = reinterpret_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 = reinterpret_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 = reinterpret_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(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. while (gs_effect_loop(solid, "Draw")) {
  1468. gs_draw_sprite(prev->overflow, 0, 1, 1);
  1469. }
  1470. gs_matrix_pop();
  1471. GS_DEBUG_MARKER_END();
  1472. return true;
  1473. }
  1474. bool OBSBasicPreview::DrawSelectedItem(obs_scene_t *, obs_sceneitem_t *item, void *param)
  1475. {
  1476. if (obs_sceneitem_locked(item))
  1477. return true;
  1478. if (!SceneItemHasVideo(item))
  1479. return true;
  1480. OBSBasicPreview *prev = reinterpret_cast<OBSBasicPreview *>(param);
  1481. if (obs_sceneitem_is_group(item)) {
  1482. matrix4 mat;
  1483. obs_transform_info groupInfo;
  1484. obs_sceneitem_get_draw_transform(item, &mat);
  1485. obs_sceneitem_get_info2(item, &groupInfo);
  1486. prev->groupRot = groupInfo.rot;
  1487. gs_matrix_push();
  1488. gs_matrix_mul(&mat);
  1489. obs_sceneitem_group_enum_items(item, DrawSelectedItem, prev);
  1490. gs_matrix_pop();
  1491. prev->groupRot = 0.0f;
  1492. }
  1493. OBSBasic *main = OBSBasic::Get();
  1494. float pixelRatio = main->GetDevicePixelRatio();
  1495. bool hovered = false;
  1496. {
  1497. std::lock_guard<std::mutex> lock(prev->selectMutex);
  1498. for (size_t i = 0; i < prev->hoveredPreviewItems.size(); i++) {
  1499. if (prev->hoveredPreviewItems[i] == item) {
  1500. hovered = true;
  1501. break;
  1502. }
  1503. }
  1504. }
  1505. bool selected = obs_sceneitem_selected(item);
  1506. if (!selected && !hovered)
  1507. return true;
  1508. matrix4 boxTransform;
  1509. matrix4 invBoxTransform;
  1510. obs_sceneitem_get_box_transform(item, &boxTransform);
  1511. matrix4_inv(&invBoxTransform, &boxTransform);
  1512. vec3 bounds[] = {
  1513. {{{0.f, 0.f, 0.f}}},
  1514. {{{1.f, 0.f, 0.f}}},
  1515. {{{0.f, 1.f, 0.f}}},
  1516. {{{1.f, 1.f, 0.f}}},
  1517. };
  1518. main->GetCameraIcon();
  1519. QColor selColor = main->GetSelectionColor();
  1520. QColor cropColor = main->GetCropColor();
  1521. QColor hoverColor = main->GetHoverColor();
  1522. vec4 red;
  1523. vec4 green;
  1524. vec4 blue;
  1525. vec4_set(&red, selColor.redF(), selColor.greenF(), selColor.blueF(), 1.0f);
  1526. vec4_set(&green, cropColor.redF(), cropColor.greenF(), cropColor.blueF(), 1.0f);
  1527. vec4_set(&blue, hoverColor.redF(), hoverColor.greenF(), hoverColor.blueF(), 1.0f);
  1528. bool visible = std::all_of(std::begin(bounds), std::end(bounds), [&](const vec3 &b) {
  1529. vec3 pos;
  1530. vec3_transform(&pos, &b, &boxTransform);
  1531. vec3_transform(&pos, &pos, &invBoxTransform);
  1532. return CloseFloat(pos.x, b.x) && CloseFloat(pos.y, b.y);
  1533. });
  1534. if (!visible)
  1535. return true;
  1536. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawSelectedItem");
  1537. matrix4 curTransform;
  1538. vec2 boxScale;
  1539. gs_matrix_get(&curTransform);
  1540. obs_sceneitem_get_box_scale(item, &boxScale);
  1541. boxScale.x *= curTransform.x.x;
  1542. boxScale.y *= curTransform.y.y;
  1543. obs_transform_info info;
  1544. obs_sceneitem_get_info2(item, &info);
  1545. gs_matrix_push();
  1546. gs_matrix_mul(&boxTransform);
  1547. obs_sceneitem_crop crop;
  1548. obs_sceneitem_get_crop(item, &crop);
  1549. if (info.bounds_type == OBS_BOUNDS_NONE && crop_enabled(&crop)) {
  1550. #define DRAW_SIDE(side, x1, y1, x2, y2) \
  1551. if (hovered && !selected) { \
  1552. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color"); \
  1553. gs_effect_set_vec4(colParam, &blue); \
  1554. while (gs_effect_loop(prev->solidEffect, "Solid")) { \
  1555. DrawLine(x1, y1, x2, y2, HANDLE_RADIUS *pixelRatio / 2, boxScale); \
  1556. } \
  1557. } else if (crop.side > 0) { \
  1558. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->stripedLineEffect, "color"); \
  1559. gs_effect_set_vec4(colParam, &green); \
  1560. prev->DrawStripedLine(x1, y1, x2, y2, HANDLE_RADIUS *pixelRatio / 2, boxScale); \
  1561. } else { \
  1562. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color"); \
  1563. gs_effect_set_vec4(colParam, &red); \
  1564. while (gs_effect_loop(prev->solidEffect, "Solid")) { \
  1565. DrawLine(x1, y1, x2, y2, HANDLE_RADIUS *pixelRatio / 2, boxScale); \
  1566. } \
  1567. }
  1568. DRAW_SIDE(left, 0.0f, 0.0f, 0.0f, 1.0f);
  1569. DRAW_SIDE(top, 0.0f, 0.0f, 1.0f, 0.0f);
  1570. DRAW_SIDE(right, 1.0f, 0.0f, 1.0f, 1.0f);
  1571. DRAW_SIDE(bottom, 0.0f, 1.0f, 1.0f, 1.0f);
  1572. #undef DRAW_SIDE
  1573. } else {
  1574. if (!selected) {
  1575. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color");
  1576. gs_effect_set_vec4(colParam, &blue);
  1577. while (gs_effect_loop(prev->solidEffect, "Solid")) {
  1578. DrawRect(HANDLE_RADIUS * pixelRatio / 2, boxScale);
  1579. }
  1580. } else {
  1581. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color");
  1582. gs_effect_set_vec4(colParam, &red);
  1583. while (gs_effect_loop(prev->solidEffect, "Solid")) {
  1584. DrawRect(HANDLE_RADIUS * pixelRatio / 2, boxScale);
  1585. }
  1586. }
  1587. }
  1588. gs_eparam_t *colParam = gs_effect_get_param_by_name(prev->solidEffect, "color");
  1589. gs_technique_t *tech = gs_effect_get_technique(prev->solidEffect, "Solid");
  1590. gs_technique_begin(tech);
  1591. gs_technique_begin_pass(tech, 0);
  1592. gs_load_vertexbuffer(main->box);
  1593. gs_effect_set_vec4(colParam, &red);
  1594. if (selected) {
  1595. DrawSquareAtPos(0.0f, 0.0f, pixelRatio);
  1596. DrawSquareAtPos(0.0f, 1.0f, pixelRatio);
  1597. DrawSquareAtPos(1.0f, 0.0f, pixelRatio);
  1598. DrawSquareAtPos(1.0f, 1.0f, pixelRatio);
  1599. DrawSquareAtPos(0.5f, 0.0f, pixelRatio);
  1600. DrawSquareAtPos(0.0f, 0.5f, pixelRatio);
  1601. DrawSquareAtPos(0.5f, 1.0f, pixelRatio);
  1602. DrawSquareAtPos(1.0f, 0.5f, pixelRatio);
  1603. if (!prev->circleFill) {
  1604. gs_render_start(true);
  1605. float angle = 180;
  1606. for (int i = 0, l = 40; i < l; i++) {
  1607. gs_vertex2f(sin(RAD(angle)) / 2 + 0.5f, cos(RAD(angle)) / 2 + 0.5f);
  1608. angle += 360 / l;
  1609. gs_vertex2f(sin(RAD(angle)) / 2 + 0.5f, cos(RAD(angle)) / 2 + 0.5f);
  1610. gs_vertex2f(0.5f, 1.0f);
  1611. }
  1612. prev->circleFill = gs_render_save();
  1613. }
  1614. bool invert = info.scale.y < 0.0f && info.bounds_type == OBS_BOUNDS_NONE;
  1615. DrawRotationHandle(prev->circleFill, info.rot + prev->groupRot, pixelRatio, invert);
  1616. }
  1617. gs_matrix_pop();
  1618. gs_technique_end_pass(tech);
  1619. gs_technique_end(tech);
  1620. GS_DEBUG_MARKER_END();
  1621. return true;
  1622. }
  1623. bool OBSBasicPreview::DrawSelectionBox(float x1, float y1, float x2, float y2, gs_vertbuffer_t *rectFill)
  1624. {
  1625. OBSBasic *main = OBSBasic::Get();
  1626. float pixelRatio = main->GetDevicePixelRatio();
  1627. x1 = std::round(x1);
  1628. x2 = std::round(x2);
  1629. y1 = std::round(y1);
  1630. y2 = std::round(y2);
  1631. gs_effect_t *eff = gs_get_effect();
  1632. gs_eparam_t *colParam = gs_effect_get_param_by_name(eff, "color");
  1633. vec4 fillColor;
  1634. vec4_set(&fillColor, 0.7f, 0.7f, 0.7f, 0.5f);
  1635. vec4 borderColor;
  1636. vec4_set(&borderColor, 1.0f, 1.0f, 1.0f, 1.0f);
  1637. vec2 scale;
  1638. vec2_set(&scale, std::abs(x2 - x1), std::abs(y2 - y1));
  1639. gs_matrix_push();
  1640. gs_matrix_identity();
  1641. gs_matrix_translate3f(x1, y1, 0.0f);
  1642. gs_matrix_scale3f(x2 - x1, y2 - y1, 1.0f);
  1643. gs_effect_set_vec4(colParam, &fillColor);
  1644. gs_load_vertexbuffer(rectFill);
  1645. gs_draw(GS_TRISTRIP, 0, 0);
  1646. gs_effect_set_vec4(colParam, &borderColor);
  1647. DrawRect(HANDLE_RADIUS * pixelRatio / 2, scale);
  1648. gs_matrix_pop();
  1649. return true;
  1650. }
  1651. void OBSBasicPreview::DrawOverflow()
  1652. {
  1653. if (locked)
  1654. return;
  1655. if (overflowHidden)
  1656. return;
  1657. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawOverflow");
  1658. if (!overflow) {
  1659. std::string path;
  1660. GetDataFilePath("images/overflow.png", path);
  1661. overflow = gs_texture_create_from_file(path.c_str());
  1662. }
  1663. OBSBasic *main = OBSBasic::Get();
  1664. OBSScene scene = main->GetCurrentScene();
  1665. if (scene) {
  1666. gs_matrix_push();
  1667. gs_matrix_scale3f(main->previewScale, main->previewScale, 1.0f);
  1668. obs_scene_enum_items(scene, DrawSelectedOverflow, this);
  1669. gs_matrix_pop();
  1670. }
  1671. gs_load_vertexbuffer(nullptr);
  1672. GS_DEBUG_MARKER_END();
  1673. }
  1674. void OBSBasicPreview::DrawSceneEditing()
  1675. {
  1676. if (locked)
  1677. return;
  1678. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "DrawSceneEditing");
  1679. OBSBasic *main = OBSBasic::Get();
  1680. OBSScene scene = main->GetCurrentScene();
  1681. if (scene) {
  1682. gs_matrix_push();
  1683. gs_matrix_scale3f(main->previewScale, main->previewScale, 1.0f);
  1684. obs_scene_enum_items(scene, DrawSelectedItem, this);
  1685. gs_matrix_pop();
  1686. }
  1687. if (selectionBox) {
  1688. if (!rectFill) {
  1689. gs_render_start(true);
  1690. gs_vertex2f(0.0f, 0.0f);
  1691. gs_vertex2f(1.0f, 0.0f);
  1692. gs_vertex2f(0.0f, 1.0f);
  1693. gs_vertex2f(1.0f, 1.0f);
  1694. rectFill = gs_render_save();
  1695. }
  1696. while (gs_effect_loop(solidEffect, "Solid")) {
  1697. DrawSelectionBox(startPos.x * main->previewScale, startPos.y * main->previewScale,
  1698. mousePos.x * main->previewScale, mousePos.y * main->previewScale, rectFill);
  1699. }
  1700. }
  1701. gs_load_vertexbuffer(nullptr);
  1702. GS_DEBUG_MARKER_END();
  1703. }
  1704. void OBSBasicPreview::ResetScrollingOffset()
  1705. {
  1706. vec2_zero(&scrollingOffset);
  1707. }
  1708. void OBSBasicPreview::SetScalingLevel(int32_t newScalingLevelVal)
  1709. {
  1710. newScalingLevelVal = std::clamp(newScalingLevelVal, -MAX_SCALING_LEVEL, MAX_SCALING_LEVEL);
  1711. float newScalingAmountVal = pow(ZOOM_SENSITIVITY, float(newScalingLevelVal));
  1712. scalingLevel = newScalingLevelVal;
  1713. SetScalingAmount(newScalingAmountVal);
  1714. }
  1715. void OBSBasicPreview::SetScalingAmount(float newScalingAmountVal)
  1716. {
  1717. scrollingOffset.x *= newScalingAmountVal / scalingAmount;
  1718. scrollingOffset.y *= newScalingAmountVal / scalingAmount;
  1719. if (scalingAmount == newScalingAmountVal)
  1720. return;
  1721. scalingAmount = newScalingAmountVal;
  1722. emit scalingChanged(scalingAmount);
  1723. if (!fixedScaling) {
  1724. scalingLevel = std::round(log(scalingAmount) / log(ZOOM_SENSITIVITY));
  1725. }
  1726. }
  1727. void OBSBasicPreview::SetScalingLevelAndAmount(int32_t newScalingLevelVal, float newScalingAmountVal)
  1728. {
  1729. newScalingLevelVal = std::clamp(newScalingLevelVal, -MAX_SCALING_LEVEL, MAX_SCALING_LEVEL);
  1730. scalingLevel = newScalingLevelVal;
  1731. SetScalingAmount(newScalingAmountVal);
  1732. }
  1733. void OBSBasicPreview::increaseScalingLevel()
  1734. {
  1735. SetFixedScaling(true);
  1736. SetScalingLevel(scalingLevel + 1);
  1737. emit DisplayResized();
  1738. }
  1739. void OBSBasicPreview::decreaseScalingLevel()
  1740. {
  1741. SetFixedScaling(true);
  1742. SetScalingLevel(scalingLevel - 1);
  1743. emit DisplayResized();
  1744. }
  1745. void OBSBasicPreview::resetScalingLevel()
  1746. {
  1747. SetScalingLevel(0);
  1748. ResetScrollingOffset();
  1749. emit DisplayResized();
  1750. }
  1751. OBSBasicPreview *OBSBasicPreview::Get()
  1752. {
  1753. return OBSBasic::Get()->ui->preview;
  1754. }
  1755. static obs_source_t *CreateLabel(float pixelRatio, int i)
  1756. {
  1757. OBSDataAutoRelease settings = obs_data_create();
  1758. OBSDataAutoRelease font = obs_data_create();
  1759. #if defined(_WIN32)
  1760. obs_data_set_string(font, "face", "Arial");
  1761. #elif defined(__APPLE__)
  1762. obs_data_set_string(font, "face", "Helvetica");
  1763. #else
  1764. obs_data_set_string(font, "face", "Monospace");
  1765. #endif
  1766. obs_data_set_int(font, "flags", 1); // Bold text
  1767. obs_data_set_int(font, "size", 16 * pixelRatio);
  1768. obs_data_set_obj(settings, "font", font);
  1769. obs_data_set_bool(settings, "outline", true);
  1770. #ifdef _WIN32
  1771. obs_data_set_int(settings, "outline_color", 0x000000);
  1772. obs_data_set_int(settings, "outline_size", 3);
  1773. const char *text_source_id = "text_gdiplus";
  1774. #else
  1775. const char *text_source_id = "text_ft2_source";
  1776. #endif
  1777. DStr name;
  1778. dstr_printf(name, "Preview spacing label %d", i);
  1779. return obs_source_create_private(text_source_id, name, settings);
  1780. }
  1781. static void SetLabelText(int sourceIndex, int px)
  1782. {
  1783. OBSBasicPreview *prev = OBSBasicPreview::Get();
  1784. if (px == prev->spacerPx[sourceIndex])
  1785. return;
  1786. std::string text = std::to_string(px) + " px";
  1787. obs_source_t *source = prev->spacerLabel[sourceIndex];
  1788. OBSDataAutoRelease settings = obs_source_get_settings(source);
  1789. obs_data_set_string(settings, "text", text.c_str());
  1790. obs_source_update(source, settings);
  1791. prev->spacerPx[sourceIndex] = px;
  1792. }
  1793. static void DrawLabel(OBSSource source, vec3 &pos, vec3 &viewport)
  1794. {
  1795. if (!source)
  1796. return;
  1797. vec3_mul(&pos, &pos, &viewport);
  1798. gs_matrix_push();
  1799. gs_matrix_identity();
  1800. gs_matrix_translate(&pos);
  1801. obs_source_video_render(source);
  1802. gs_matrix_pop();
  1803. }
  1804. static void DrawSpacingLine(vec3 &start, vec3 &end, vec3 &viewport, float pixelRatio)
  1805. {
  1806. OBSBasic *main = OBSBasic::Get();
  1807. matrix4 transform;
  1808. matrix4_identity(&transform);
  1809. transform.x.x = viewport.x;
  1810. transform.y.y = viewport.y;
  1811. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  1812. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  1813. QColor selColor = main->GetSelectionColor();
  1814. vec4 color;
  1815. vec4_set(&color, selColor.redF(), selColor.greenF(), selColor.blueF(), 1.0f);
  1816. gs_effect_set_vec4(gs_effect_get_param_by_name(solid, "color"), &color);
  1817. gs_technique_begin(tech);
  1818. gs_technique_begin_pass(tech, 0);
  1819. gs_matrix_push();
  1820. gs_matrix_mul(&transform);
  1821. vec2 scale;
  1822. vec2_set(&scale, viewport.x, viewport.y);
  1823. DrawLine(start.x, start.y, end.x, end.y, pixelRatio * (HANDLE_RADIUS / 2), scale);
  1824. gs_matrix_pop();
  1825. gs_load_vertexbuffer(nullptr);
  1826. gs_technique_end_pass(tech);
  1827. gs_technique_end(tech);
  1828. }
  1829. static void RenderSpacingHelper(int sourceIndex, vec3 &start, vec3 &end, vec3 &viewport, float pixelRatio)
  1830. {
  1831. bool horizontal = (sourceIndex == 2 || sourceIndex == 3);
  1832. // If outside of preview, don't render
  1833. if (!((horizontal && (end.x >= start.x)) || (!horizontal && (end.y >= start.y))))
  1834. return;
  1835. float length = vec3_dist(&start, &end);
  1836. obs_video_info ovi;
  1837. obs_get_video_info(&ovi);
  1838. float px;
  1839. if (horizontal) {
  1840. px = length * ovi.base_width;
  1841. } else {
  1842. px = length * ovi.base_height;
  1843. }
  1844. if (px <= 0.0f)
  1845. return;
  1846. OBSBasicPreview *prev = OBSBasicPreview::Get();
  1847. obs_source_t *source = prev->spacerLabel[sourceIndex];
  1848. vec3 labelSize, labelPos;
  1849. vec3_set(&labelSize, obs_source_get_width(source), obs_source_get_height(source), 1.0f);
  1850. vec3_div(&labelSize, &labelSize, &viewport);
  1851. vec3 labelMargin;
  1852. vec3_set(&labelMargin, SPACER_LABEL_MARGIN * pixelRatio, SPACER_LABEL_MARGIN * pixelRatio, 1.0f);
  1853. vec3_div(&labelMargin, &labelMargin, &viewport);
  1854. vec3_set(&labelPos, end.x, end.y, end.z);
  1855. if (horizontal) {
  1856. labelPos.x -= (end.x - start.x) / 2;
  1857. labelPos.x -= labelSize.x / 2;
  1858. labelPos.y -= labelMargin.y + (labelSize.y / 2) + (HANDLE_RADIUS / viewport.y);
  1859. } else {
  1860. labelPos.y -= (end.y - start.y) / 2;
  1861. labelPos.y -= labelSize.y / 2;
  1862. labelPos.x += labelMargin.x;
  1863. }
  1864. DrawSpacingLine(start, end, viewport, pixelRatio);
  1865. SetLabelText(sourceIndex, (int)px);
  1866. DrawLabel(source, labelPos, viewport);
  1867. }
  1868. void OBSBasicPreview::DrawSpacingHelpers()
  1869. {
  1870. if (locked)
  1871. return;
  1872. OBSBasic *main = OBSBasic::Get();
  1873. vec2 s;
  1874. SceneFindBoxData data(s, s);
  1875. OBSScene scene = main->GetCurrentScene();
  1876. obs_scene_enum_items(scene, FindSelected, &data);
  1877. if (data.sceneItems.size() != 1)
  1878. return;
  1879. OBSSceneItem item = data.sceneItems[0];
  1880. if (!item)
  1881. return;
  1882. if (obs_sceneitem_locked(item))
  1883. return;
  1884. vec2 itemSize = GetItemSize(item);
  1885. if (itemSize.x == 0.0f || itemSize.y == 0.0f)
  1886. return;
  1887. obs_sceneitem_t *parentGroup = obs_sceneitem_get_group(scene, item);
  1888. if (parentGroup && obs_sceneitem_locked(parentGroup))
  1889. return;
  1890. matrix4 boxTransform;
  1891. obs_sceneitem_get_box_transform(item, &boxTransform);
  1892. obs_transform_info oti;
  1893. obs_sceneitem_get_info2(item, &oti);
  1894. obs_video_info ovi;
  1895. obs_get_video_info(&ovi);
  1896. vec3 size;
  1897. vec3_set(&size, ovi.base_width, ovi.base_height, 1.0f);
  1898. // Init box transform side locations
  1899. vec3 left, right, top, bottom;
  1900. vec3_set(&left, 0.0f, 0.5f, 1.0f);
  1901. vec3_set(&right, 1.0f, 0.5f, 1.0f);
  1902. vec3_set(&top, 0.5f, 0.0f, 1.0f);
  1903. vec3_set(&bottom, 0.5f, 1.0f, 1.0f);
  1904. // Decide which side to use with box transform, based on rotation
  1905. // Seems hacky, probably a better way to do it
  1906. float rot = oti.rot;
  1907. if (parentGroup) {
  1908. obs_transform_info groupOti;
  1909. obs_sceneitem_get_info2(parentGroup, &groupOti);
  1910. //Correct the scene item rotation angle
  1911. rot = oti.rot + groupOti.rot;
  1912. // Correct the scene item box transform
  1913. // Based on scale, rotation angle, position of parent's group
  1914. matrix4_scale3f(&boxTransform, &boxTransform, groupOti.scale.x, groupOti.scale.y, 1.0f);
  1915. matrix4_rotate_aa4f(&boxTransform, &boxTransform, 0.0f, 0.0f, 1.0f, RAD(groupOti.rot));
  1916. matrix4_translate3f(&boxTransform, &boxTransform, groupOti.pos.x, groupOti.pos.y, 0.0f);
  1917. }
  1918. // Switch top/bottom or right/left if scale is negative
  1919. if (oti.scale.x < 0.0f && oti.bounds_type == OBS_BOUNDS_NONE) {
  1920. vec3 l = left;
  1921. vec3 r = right;
  1922. vec3_copy(&left, &r);
  1923. vec3_copy(&right, &l);
  1924. }
  1925. if (oti.scale.y < 0.0f && oti.bounds_type == OBS_BOUNDS_NONE) {
  1926. vec3 t = top;
  1927. vec3 b = bottom;
  1928. vec3_copy(&top, &b);
  1929. vec3_copy(&bottom, &t);
  1930. }
  1931. if (rot >= HELPER_ROT_BREAKPOINT) {
  1932. for (float i = HELPER_ROT_BREAKPOINT; i <= 360.0f; i += 90.0f) {
  1933. if (rot < i)
  1934. break;
  1935. vec3 l = left;
  1936. vec3 r = right;
  1937. vec3 t = top;
  1938. vec3 b = bottom;
  1939. vec3_copy(&top, &l);
  1940. vec3_copy(&right, &t);
  1941. vec3_copy(&bottom, &r);
  1942. vec3_copy(&left, &b);
  1943. }
  1944. } else if (rot <= -HELPER_ROT_BREAKPOINT) {
  1945. for (float i = -HELPER_ROT_BREAKPOINT; i >= -360.0f; i -= 90.0f) {
  1946. if (rot > i)
  1947. break;
  1948. vec3 l = left;
  1949. vec3 r = right;
  1950. vec3 t = top;
  1951. vec3 b = bottom;
  1952. vec3_copy(&top, &r);
  1953. vec3_copy(&right, &b);
  1954. vec3_copy(&bottom, &l);
  1955. vec3_copy(&left, &t);
  1956. }
  1957. }
  1958. // Get sides of box transform
  1959. left = GetTransformedPos(left.x, left.y, boxTransform);
  1960. right = GetTransformedPos(right.x, right.y, boxTransform);
  1961. top = GetTransformedPos(top.x, top.y, boxTransform);
  1962. bottom = GetTransformedPos(bottom.x, bottom.y, boxTransform);
  1963. bottom.y = size.y - bottom.y;
  1964. right.x = size.x - right.x;
  1965. // Init viewport
  1966. vec3 viewport;
  1967. vec3_set(&viewport, main->previewCX, main->previewCY, 1.0f);
  1968. vec3_div(&left, &left, &viewport);
  1969. vec3_div(&right, &right, &viewport);
  1970. vec3_div(&top, &top, &viewport);
  1971. vec3_div(&bottom, &bottom, &viewport);
  1972. vec3_mulf(&left, &left, main->previewScale);
  1973. vec3_mulf(&right, &right, main->previewScale);
  1974. vec3_mulf(&top, &top, main->previewScale);
  1975. vec3_mulf(&bottom, &bottom, main->previewScale);
  1976. // Draw spacer lines and labels
  1977. vec3 start, end;
  1978. float pixelRatio = main->GetDevicePixelRatio();
  1979. for (int i = 0; i < 4; i++) {
  1980. if (!spacerLabel[i])
  1981. spacerLabel[i] = CreateLabel(pixelRatio, i);
  1982. }
  1983. vec3_set(&start, top.x, 0.0f, 1.0f);
  1984. vec3_set(&end, top.x, top.y, 1.0f);
  1985. RenderSpacingHelper(0, start, end, viewport, pixelRatio);
  1986. vec3_set(&start, bottom.x, 1.0f - bottom.y, 1.0f);
  1987. vec3_set(&end, bottom.x, 1.0f, 1.0f);
  1988. RenderSpacingHelper(1, start, end, viewport, pixelRatio);
  1989. vec3_set(&start, 0.0f, left.y, 1.0f);
  1990. vec3_set(&end, left.x, left.y, 1.0f);
  1991. RenderSpacingHelper(2, start, end, viewport, pixelRatio);
  1992. vec3_set(&start, 1.0f - right.x, right.y, 1.0f);
  1993. vec3_set(&end, 1.0f, right.y, 1.0f);
  1994. RenderSpacingHelper(3, start, end, viewport, pixelRatio);
  1995. }
  1996. void OBSBasicPreview::ClampScrollingOffsets()
  1997. {
  1998. obs_video_info ovi;
  1999. obs_get_video_info(&ovi);
  2000. QSize targetSize = GetPixelSize(this);
  2001. vec3 target, offset;
  2002. vec3_set(&target, (float)targetSize.width(), (float)targetSize.height(), 1.0f);
  2003. vec3_set(&offset, (float)ovi.base_width, (float)ovi.base_height, 1.0f);
  2004. vec3_mulf(&offset, &offset, scalingAmount);
  2005. vec3_sub(&offset, &offset, &target);
  2006. vec3_mulf(&offset, &offset, 0.5f);
  2007. vec3_maxf(&offset, &offset, 0.0f);
  2008. vec3_divf(&target, &target, 2.0f);
  2009. vec3_add(&offset, &offset, &target);
  2010. scrollingOffset.x = std::clamp(scrollingOffset.x, -offset.x, offset.x);
  2011. scrollingOffset.y = std::clamp(scrollingOffset.y, -offset.y, offset.y);
  2012. UpdateXScrollBar(offset.x);
  2013. UpdateYScrollBar(offset.y);
  2014. }
  2015. void OBSBasicPreview::xScrollBarChanged(int value)
  2016. {
  2017. updatingXScrollBar = true;
  2018. scrollingOffset.x = float(-value);
  2019. updatingXScrollBar = false;
  2020. emit DisplayResized();
  2021. }
  2022. void OBSBasicPreview::yScrollBarChanged(int value)
  2023. {
  2024. updatingYScrollBar = true;
  2025. scrollingOffset.y = float(-value);
  2026. updatingYScrollBar = false;
  2027. emit DisplayResized();
  2028. }
  2029. void OBSBasicPreview::UpdateXScrollBar(float cx)
  2030. {
  2031. if (updatingXScrollBar)
  2032. return;
  2033. OBSBasic *main = OBSBasic::Get();
  2034. if (!main->ui->previewXScrollBar->isVisible())
  2035. return;
  2036. main->ui->previewXScrollBar->setRange(int(-cx), int(cx));
  2037. QSize targetSize = GetPixelSize(this);
  2038. main->ui->previewXScrollBar->setPageStep(targetSize.width() / std::min(scalingAmount, 1.0f));
  2039. QSignalBlocker sig(main->ui->previewXScrollBar);
  2040. main->ui->previewXScrollBar->setValue(int(-scrollingOffset.x));
  2041. }
  2042. void OBSBasicPreview::UpdateYScrollBar(float cy)
  2043. {
  2044. if (updatingYScrollBar)
  2045. return;
  2046. OBSBasic *main = OBSBasic::Get();
  2047. if (!main->ui->previewYScrollBar->isVisible())
  2048. return;
  2049. main->ui->previewYScrollBar->setRange(int(-cy), int(cy));
  2050. QSize targetSize = GetPixelSize(this);
  2051. main->ui->previewYScrollBar->setPageStep(targetSize.height() / std::min(scalingAmount, 1.0f));
  2052. QSignalBlocker sig(main->ui->previewYScrollBar);
  2053. main->ui->previewYScrollBar->setValue(int(-scrollingOffset.y));
  2054. }