window-basic-preview.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. #include <QGuiApplication>
  2. #include <QMouseEvent>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <graphics/vec4.h>
  6. #include <graphics/matrix4.h>
  7. #include "window-basic-preview.hpp"
  8. #include "window-basic-main.hpp"
  9. #include "obs-app.hpp"
  10. #define HANDLE_RADIUS 4.0f
  11. #define HANDLE_SEL_RADIUS (HANDLE_RADIUS * 1.5f)
  12. /* TODO: make C++ math classes and clean up code here later */
  13. OBSBasicPreview::OBSBasicPreview(QWidget *parent, Qt::WindowFlags flags)
  14. : OBSQTDisplay(parent, flags)
  15. {
  16. ResetScrollingOffset();
  17. setMouseTracking(true);
  18. }
  19. vec2 OBSBasicPreview::GetMouseEventPos(QMouseEvent *event)
  20. {
  21. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  22. float pixelRatio = main->devicePixelRatio();
  23. float scale = pixelRatio / main->previewScale;
  24. vec2 pos;
  25. vec2_set(&pos,
  26. (float(event->x()) - main->previewX / pixelRatio) * scale,
  27. (float(event->y()) - main->previewY / pixelRatio) * scale);
  28. return pos;
  29. }
  30. struct SceneFindData {
  31. const vec2 &pos;
  32. OBSSceneItem item;
  33. bool selectBelow;
  34. obs_sceneitem_t *group = nullptr;
  35. SceneFindData(const SceneFindData &) = delete;
  36. SceneFindData(SceneFindData &&) = delete;
  37. SceneFindData& operator=(const SceneFindData &) = delete;
  38. SceneFindData& operator=(SceneFindData &&) = delete;
  39. inline SceneFindData(const vec2 &pos_, bool selectBelow_)
  40. : pos (pos_),
  41. selectBelow (selectBelow_)
  42. {}
  43. };
  44. static bool SceneItemHasVideo(obs_sceneitem_t *item)
  45. {
  46. obs_source_t *source = obs_sceneitem_get_source(item);
  47. uint32_t flags = obs_source_get_output_flags(source);
  48. return (flags & OBS_SOURCE_VIDEO) != 0;
  49. }
  50. static bool CloseFloat(float a, float b, float epsilon=0.01)
  51. {
  52. using std::abs;
  53. return abs(a-b) <= epsilon;
  54. }
  55. static bool FindItemAtPos(obs_scene_t *scene, obs_sceneitem_t *item,
  56. void *param)
  57. {
  58. SceneFindData *data = reinterpret_cast<SceneFindData*>(param);
  59. matrix4 transform;
  60. matrix4 invTransform;
  61. vec3 transformedPos;
  62. vec3 pos3;
  63. vec3 pos3_;
  64. if (!SceneItemHasVideo(item))
  65. return true;
  66. if (obs_sceneitem_locked(item))
  67. return true;
  68. vec3_set(&pos3, data->pos.x, data->pos.y, 0.0f);
  69. obs_sceneitem_get_box_transform(item, &transform);
  70. matrix4_inv(&invTransform, &transform);
  71. vec3_transform(&transformedPos, &pos3, &invTransform);
  72. vec3_transform(&pos3_, &transformedPos, &transform);
  73. if (CloseFloat(pos3.x, pos3_.x) && CloseFloat(pos3.y, pos3_.y) &&
  74. transformedPos.x >= 0.0f && transformedPos.x <= 1.0f &&
  75. transformedPos.y >= 0.0f && transformedPos.y <= 1.0f) {
  76. if (data->selectBelow && obs_sceneitem_selected(item)) {
  77. if (data->item)
  78. return false;
  79. else
  80. data->selectBelow = false;
  81. }
  82. data->item = item;
  83. }
  84. UNUSED_PARAMETER(scene);
  85. return true;
  86. }
  87. static vec3 GetTransformedPos(float x, float y, const matrix4 &mat)
  88. {
  89. vec3 result;
  90. vec3_set(&result, x, y, 0.0f);
  91. vec3_transform(&result, &result, &mat);
  92. return result;
  93. }
  94. static vec3 GetTransformedPosScaled(float x, float y, const matrix4 &mat,
  95. float scale)
  96. {
  97. vec3 result;
  98. vec3_set(&result, x, y, 0.0f);
  99. vec3_transform(&result, &result, &mat);
  100. vec3_mulf(&result, &result, scale);
  101. return result;
  102. }
  103. static inline vec2 GetOBSScreenSize()
  104. {
  105. obs_video_info ovi;
  106. vec2 size;
  107. vec2_zero(&size);
  108. if (obs_get_video_info(&ovi)) {
  109. size.x = float(ovi.base_width);
  110. size.y = float(ovi.base_height);
  111. }
  112. return size;
  113. }
  114. vec3 OBSBasicPreview::GetSnapOffset(const vec3 &tl, const vec3 &br)
  115. {
  116. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  117. vec2 screenSize = GetOBSScreenSize();
  118. vec3 clampOffset;
  119. vec3_zero(&clampOffset);
  120. const bool snap = config_get_bool(GetGlobalConfig(),
  121. "BasicWindow", "SnappingEnabled");
  122. if (snap == false)
  123. return clampOffset;
  124. const bool screenSnap = config_get_bool(GetGlobalConfig(),
  125. "BasicWindow", "ScreenSnapping");
  126. const bool centerSnap = config_get_bool(GetGlobalConfig(),
  127. "BasicWindow", "CenterSnapping");
  128. const float clampDist = config_get_double(GetGlobalConfig(),
  129. "BasicWindow", "SnapDistance") / main->previewScale;
  130. const float centerX = br.x - (br.x - tl.x) / 2.0f;
  131. const float centerY = br.y - (br.y - tl.y) / 2.0f;
  132. // Left screen edge.
  133. if (screenSnap &&
  134. fabsf(tl.x) < clampDist)
  135. clampOffset.x = -tl.x;
  136. // Right screen edge.
  137. if (screenSnap &&
  138. fabsf(clampOffset.x) < EPSILON &&
  139. fabsf(screenSize.x - br.x) < clampDist)
  140. clampOffset.x = screenSize.x - br.x;
  141. // Horizontal center.
  142. if (centerSnap &&
  143. fabsf(screenSize.x - (br.x - tl.x)) > clampDist &&
  144. fabsf(screenSize.x / 2.0f - centerX) < clampDist)
  145. clampOffset.x = screenSize.x / 2.0f - centerX;
  146. // Top screen edge.
  147. if (screenSnap &&
  148. fabsf(tl.y) < clampDist)
  149. clampOffset.y = -tl.y;
  150. // Bottom screen edge.
  151. if (screenSnap &&
  152. fabsf(clampOffset.y) < EPSILON &&
  153. fabsf(screenSize.y - br.y) < clampDist)
  154. clampOffset.y = screenSize.y - br.y;
  155. // Vertical center.
  156. if (centerSnap &&
  157. fabsf(screenSize.y - (br.y - tl.y)) > clampDist &&
  158. fabsf(screenSize.y / 2.0f - centerY) < clampDist)
  159. clampOffset.y = screenSize.y / 2.0f - centerY;
  160. return clampOffset;
  161. }
  162. OBSSceneItem OBSBasicPreview::GetItemAtPos(const vec2 &pos, bool selectBelow)
  163. {
  164. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  165. OBSScene scene = main->GetCurrentScene();
  166. if (!scene)
  167. return OBSSceneItem();
  168. SceneFindData data(pos, selectBelow);
  169. obs_scene_enum_items(scene, FindItemAtPos, &data);
  170. return data.item;
  171. }
  172. static bool CheckItemSelected(obs_scene_t *scene, obs_sceneitem_t *item,
  173. void *param)
  174. {
  175. SceneFindData *data = reinterpret_cast<SceneFindData*>(param);
  176. matrix4 transform;
  177. vec3 transformedPos;
  178. vec3 pos3;
  179. if (!SceneItemHasVideo(item))
  180. return true;
  181. if (obs_sceneitem_is_group(item)) {
  182. data->group = item;
  183. obs_sceneitem_group_enum_items(item, CheckItemSelected, param);
  184. data->group = nullptr;
  185. if (data->item) {
  186. return false;
  187. }
  188. }
  189. vec3_set(&pos3, data->pos.x, data->pos.y, 0.0f);
  190. obs_sceneitem_get_box_transform(item, &transform);
  191. if (data->group) {
  192. matrix4 parent_transform;
  193. obs_sceneitem_get_draw_transform(data->group, &parent_transform);
  194. matrix4_mul(&transform, &transform, &parent_transform);
  195. }
  196. matrix4_inv(&transform, &transform);
  197. vec3_transform(&transformedPos, &pos3, &transform);
  198. if (transformedPos.x >= 0.0f && transformedPos.x <= 1.0f &&
  199. transformedPos.y >= 0.0f && transformedPos.y <= 1.0f) {
  200. if (obs_sceneitem_selected(item)) {
  201. data->item = item;
  202. return false;
  203. }
  204. }
  205. UNUSED_PARAMETER(scene);
  206. return true;
  207. }
  208. bool OBSBasicPreview::SelectedAtPos(const vec2 &pos)
  209. {
  210. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  211. OBSScene scene = main->GetCurrentScene();
  212. if (!scene)
  213. return false;
  214. SceneFindData data(pos, false);
  215. obs_scene_enum_items(scene, CheckItemSelected, &data);
  216. return !!data.item;
  217. }
  218. struct HandleFindData {
  219. const vec2 &pos;
  220. const float scale;
  221. OBSSceneItem item;
  222. ItemHandle handle = ItemHandle::None;
  223. HandleFindData(const HandleFindData &) = delete;
  224. HandleFindData(HandleFindData &&) = delete;
  225. HandleFindData& operator=(const HandleFindData &) = delete;
  226. HandleFindData& operator=(HandleFindData &&) = delete;
  227. inline HandleFindData(const vec2 &pos_, float scale_)
  228. : pos (pos_),
  229. scale (scale_)
  230. {}
  231. };
  232. static bool FindHandleAtPos(obs_scene_t *scene, obs_sceneitem_t *item,
  233. void *param)
  234. {
  235. HandleFindData *data = reinterpret_cast<HandleFindData*>(param);
  236. if (!obs_sceneitem_selected(item)) {
  237. if (obs_sceneitem_is_group(item)) {
  238. matrix4 transform;
  239. vec3 new_pos3;
  240. vec3_set(&new_pos3, data->pos.x, data->pos.y, 0.0f);
  241. vec3_divf(&new_pos3, &new_pos3, data->scale);
  242. obs_sceneitem_get_draw_transform(item, &transform);
  243. matrix4_inv(&transform, &transform);
  244. vec3_transform(&new_pos3, &new_pos3, &transform);
  245. vec2 new_pos;
  246. vec2_set(&new_pos, new_pos3.x, new_pos3.y);
  247. HandleFindData findData(new_pos, 1.0f);
  248. findData.item = data->item;
  249. findData.handle = data->handle;
  250. obs_sceneitem_group_enum_items(item, FindHandleAtPos,
  251. &findData);
  252. data->item = findData.item;
  253. data->handle = findData.handle;
  254. }
  255. return true;
  256. }
  257. matrix4 transform;
  258. vec3 pos3;
  259. float closestHandle = HANDLE_SEL_RADIUS;
  260. vec3_set(&pos3, data->pos.x, data->pos.y, 0.0f);
  261. obs_sceneitem_get_box_transform(item, &transform);
  262. auto TestHandle = [&] (float x, float y, ItemHandle handle)
  263. {
  264. vec3 handlePos = GetTransformedPosScaled(x, y, transform,
  265. data->scale);
  266. float dist = vec3_dist(&handlePos, &pos3);
  267. if (dist < HANDLE_SEL_RADIUS) {
  268. if (dist < closestHandle) {
  269. closestHandle = dist;
  270. data->handle = handle;
  271. data->item = item;
  272. }
  273. }
  274. };
  275. TestHandle(0.0f, 0.0f, ItemHandle::TopLeft);
  276. TestHandle(0.5f, 0.0f, ItemHandle::TopCenter);
  277. TestHandle(1.0f, 0.0f, ItemHandle::TopRight);
  278. TestHandle(0.0f, 0.5f, ItemHandle::CenterLeft);
  279. TestHandle(1.0f, 0.5f, ItemHandle::CenterRight);
  280. TestHandle(0.0f, 1.0f, ItemHandle::BottomLeft);
  281. TestHandle(0.5f, 1.0f, ItemHandle::BottomCenter);
  282. TestHandle(1.0f, 1.0f, ItemHandle::BottomRight);
  283. UNUSED_PARAMETER(scene);
  284. return true;
  285. }
  286. static vec2 GetItemSize(obs_sceneitem_t *item)
  287. {
  288. obs_bounds_type boundsType = obs_sceneitem_get_bounds_type(item);
  289. vec2 size;
  290. if (boundsType != OBS_BOUNDS_NONE) {
  291. obs_sceneitem_get_bounds(item, &size);
  292. } else {
  293. obs_source_t *source = obs_sceneitem_get_source(item);
  294. obs_sceneitem_crop crop;
  295. vec2 scale;
  296. obs_sceneitem_get_scale(item, &scale);
  297. obs_sceneitem_get_crop(item, &crop);
  298. size.x = float(obs_source_get_width(source) -
  299. crop.left - crop.right) * scale.x;
  300. size.y = float(obs_source_get_height(source) -
  301. crop.top - crop.bottom) * scale.y;
  302. }
  303. return size;
  304. }
  305. void OBSBasicPreview::GetStretchHandleData(const vec2 &pos)
  306. {
  307. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  308. OBSScene scene = main->GetCurrentScene();
  309. if (!scene)
  310. return;
  311. HandleFindData data(pos, main->previewScale / main->devicePixelRatio());
  312. obs_scene_enum_items(scene, FindHandleAtPos, &data);
  313. stretchItem = std::move(data.item);
  314. stretchHandle = data.handle;
  315. if (stretchHandle != ItemHandle::None) {
  316. matrix4 boxTransform;
  317. vec3 itemUL;
  318. float itemRot;
  319. stretchItemSize = GetItemSize(stretchItem);
  320. obs_sceneitem_get_box_transform(stretchItem, &boxTransform);
  321. itemRot = obs_sceneitem_get_rot(stretchItem);
  322. vec3_from_vec4(&itemUL, &boxTransform.t);
  323. /* build the item space conversion matrices */
  324. matrix4_identity(&itemToScreen);
  325. matrix4_rotate_aa4f(&itemToScreen, &itemToScreen,
  326. 0.0f, 0.0f, 1.0f, RAD(itemRot));
  327. matrix4_translate3f(&itemToScreen, &itemToScreen,
  328. itemUL.x, itemUL.y, 0.0f);
  329. matrix4_identity(&screenToItem);
  330. matrix4_translate3f(&screenToItem, &screenToItem,
  331. -itemUL.x, -itemUL.y, 0.0f);
  332. matrix4_rotate_aa4f(&screenToItem, &screenToItem,
  333. 0.0f, 0.0f, 1.0f, RAD(-itemRot));
  334. obs_sceneitem_get_crop(stretchItem, &startCrop);
  335. obs_sceneitem_get_pos(stretchItem, &startItemPos);
  336. obs_source_t *source = obs_sceneitem_get_source(stretchItem);
  337. cropSize.x = float(obs_source_get_width(source) -
  338. startCrop.left - startCrop.right);
  339. cropSize.y = float(obs_source_get_height(source) -
  340. startCrop.top - startCrop.bottom);
  341. stretchGroup = obs_sceneitem_get_group(scene, stretchItem);
  342. if (stretchGroup) {
  343. obs_sceneitem_get_draw_transform(stretchGroup,
  344. &invGroupTransform);
  345. matrix4_inv(&invGroupTransform,
  346. &invGroupTransform);
  347. obs_sceneitem_defer_group_resize_begin(stretchGroup);
  348. }
  349. }
  350. }
  351. void OBSBasicPreview::keyPressEvent(QKeyEvent *event)
  352. {
  353. if (!IsFixedScaling() || event->isAutoRepeat()) {
  354. OBSQTDisplay::keyPressEvent(event);
  355. return;
  356. }
  357. switch (event->key()) {
  358. case Qt::Key_Space:
  359. setCursor(Qt::OpenHandCursor);
  360. scrollMode = true;
  361. break;
  362. }
  363. OBSQTDisplay::keyPressEvent(event);
  364. }
  365. void OBSBasicPreview::keyReleaseEvent(QKeyEvent *event)
  366. {
  367. if (event->isAutoRepeat()) {
  368. OBSQTDisplay::keyReleaseEvent(event);
  369. return;
  370. }
  371. switch (event->key()) {
  372. case Qt::Key_Space:
  373. scrollMode = false;
  374. setCursor(Qt::ArrowCursor);
  375. break;
  376. }
  377. OBSQTDisplay::keyReleaseEvent(event);
  378. }
  379. void OBSBasicPreview::wheelEvent(QWheelEvent *event)
  380. {
  381. if (scrollMode && IsFixedScaling()
  382. && event->orientation() == Qt::Vertical) {
  383. if (event->delta() > 0)
  384. SetScalingLevel(scalingLevel + 1);
  385. else if (event->delta() < 0)
  386. SetScalingLevel(scalingLevel - 1);
  387. emit DisplayResized();
  388. }
  389. OBSQTDisplay::wheelEvent(event);
  390. }
  391. void OBSBasicPreview::mousePressEvent(QMouseEvent *event)
  392. {
  393. if (scrollMode && IsFixedScaling() &&
  394. event->button() == Qt::LeftButton) {
  395. setCursor(Qt::ClosedHandCursor);
  396. scrollingFrom.x = event->x();
  397. scrollingFrom.y = event->y();
  398. return;
  399. }
  400. if (event->button() == Qt::RightButton) {
  401. scrollMode = false;
  402. setCursor(Qt::ArrowCursor);
  403. }
  404. if (locked) {
  405. OBSQTDisplay::mousePressEvent(event);
  406. return;
  407. }
  408. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  409. float pixelRatio = main->devicePixelRatio();
  410. float x = float(event->x()) - main->previewX / pixelRatio;
  411. float y = float(event->y()) - main->previewY / pixelRatio;
  412. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  413. bool altDown = (modifiers & Qt::AltModifier);
  414. OBSQTDisplay::mousePressEvent(event);
  415. if (event->button() != Qt::LeftButton &&
  416. event->button() != Qt::RightButton)
  417. return;
  418. if (event->button() == Qt::LeftButton)
  419. mouseDown = true;
  420. if (altDown)
  421. cropping = true;
  422. vec2_set(&startPos, x, y);
  423. GetStretchHandleData(startPos);
  424. vec2_divf(&startPos, &startPos, main->previewScale / pixelRatio);
  425. startPos.x = std::round(startPos.x);
  426. startPos.y = std::round(startPos.y);
  427. mouseOverItems = SelectedAtPos(startPos);
  428. vec2_zero(&lastMoveOffset);
  429. }
  430. static bool select_one(obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  431. {
  432. obs_sceneitem_t *selectedItem =
  433. reinterpret_cast<obs_sceneitem_t*>(param);
  434. if (obs_sceneitem_is_group(item))
  435. obs_sceneitem_group_enum_items(item, select_one, param);
  436. obs_sceneitem_select(item, (selectedItem == item));
  437. UNUSED_PARAMETER(scene);
  438. return true;
  439. }
  440. void OBSBasicPreview::DoSelect(const vec2 &pos)
  441. {
  442. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  443. OBSScene scene = main->GetCurrentScene();
  444. OBSSceneItem item = GetItemAtPos(pos, true);
  445. obs_scene_enum_items(scene, select_one, (obs_sceneitem_t*)item);
  446. }
  447. void OBSBasicPreview::DoCtrlSelect(const vec2 &pos)
  448. {
  449. OBSSceneItem item = GetItemAtPos(pos, false);
  450. if (!item)
  451. return;
  452. bool selected = obs_sceneitem_selected(item);
  453. obs_sceneitem_select(item, !selected);
  454. }
  455. void OBSBasicPreview::ProcessClick(const vec2 &pos)
  456. {
  457. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  458. if (modifiers & Qt::ControlModifier)
  459. DoCtrlSelect(pos);
  460. else
  461. DoSelect(pos);
  462. }
  463. void OBSBasicPreview::mouseReleaseEvent(QMouseEvent *event)
  464. {
  465. if (scrollMode)
  466. setCursor(Qt::OpenHandCursor);
  467. if (locked) {
  468. OBSQTDisplay::mouseReleaseEvent(event);
  469. return;
  470. }
  471. if (mouseDown) {
  472. vec2 pos = GetMouseEventPos(event);
  473. if (!mouseMoved)
  474. ProcessClick(pos);
  475. if (stretchGroup) {
  476. obs_sceneitem_defer_group_resize_end(stretchGroup);
  477. }
  478. stretchItem = nullptr;
  479. stretchGroup = nullptr;
  480. mouseDown = false;
  481. mouseMoved = false;
  482. cropping = false;
  483. }
  484. }
  485. struct SelectedItemBounds {
  486. bool first = true;
  487. vec3 tl, br;
  488. };
  489. static bool AddItemBounds(obs_scene_t *scene, obs_sceneitem_t *item,
  490. void *param)
  491. {
  492. SelectedItemBounds *data = reinterpret_cast<SelectedItemBounds*>(param);
  493. if (!obs_sceneitem_selected(item))
  494. return true;
  495. matrix4 boxTransform;
  496. obs_sceneitem_get_box_transform(item, &boxTransform);
  497. vec3 t[4] = {
  498. GetTransformedPos(0.0f, 0.0f, boxTransform),
  499. GetTransformedPos(1.0f, 0.0f, boxTransform),
  500. GetTransformedPos(0.0f, 1.0f, boxTransform),
  501. GetTransformedPos(1.0f, 1.0f, boxTransform)
  502. };
  503. for (const vec3 &v : t) {
  504. if (data->first) {
  505. vec3_copy(&data->tl, &v);
  506. vec3_copy(&data->br, &v);
  507. data->first = false;
  508. } else {
  509. vec3_min(&data->tl, &data->tl, &v);
  510. vec3_max(&data->br, &data->br, &v);
  511. }
  512. }
  513. UNUSED_PARAMETER(scene);
  514. return true;
  515. }
  516. struct OffsetData {
  517. float clampDist;
  518. vec3 tl, br, offset;
  519. };
  520. static bool GetSourceSnapOffset(obs_scene_t *scene, obs_sceneitem_t *item,
  521. void *param)
  522. {
  523. OffsetData *data = reinterpret_cast<OffsetData*>(param);
  524. if (obs_sceneitem_selected(item))
  525. return true;
  526. matrix4 boxTransform;
  527. obs_sceneitem_get_box_transform(item, &boxTransform);
  528. vec3 t[4] = {
  529. GetTransformedPos(0.0f, 0.0f, boxTransform),
  530. GetTransformedPos(1.0f, 0.0f, boxTransform),
  531. GetTransformedPos(0.0f, 1.0f, boxTransform),
  532. GetTransformedPos(1.0f, 1.0f, boxTransform)
  533. };
  534. bool first = true;
  535. vec3 tl, br;
  536. vec3_zero(&tl);
  537. vec3_zero(&br);
  538. for (const vec3 &v : t) {
  539. if (first) {
  540. vec3_copy(&tl, &v);
  541. vec3_copy(&br, &v);
  542. first = false;
  543. } else {
  544. vec3_min(&tl, &tl, &v);
  545. vec3_max(&br, &br, &v);
  546. }
  547. }
  548. // Snap to other source edges
  549. #define EDGE_SNAP(l, r, x, y) \
  550. do { \
  551. double dist = fabsf(l.x - data->r.x); \
  552. if (dist < data->clampDist && \
  553. fabsf(data->offset.x) < EPSILON && \
  554. data->tl.y < br.y && \
  555. data->br.y > tl.y && \
  556. (fabsf(data->offset.x) > dist || data->offset.x < EPSILON)) \
  557. data->offset.x = l.x - data->r.x; \
  558. } while (false)
  559. EDGE_SNAP(tl, br, x, y);
  560. EDGE_SNAP(tl, br, y, x);
  561. EDGE_SNAP(br, tl, x, y);
  562. EDGE_SNAP(br, tl, y, x);
  563. #undef EDGE_SNAP
  564. UNUSED_PARAMETER(scene);
  565. return true;
  566. }
  567. void OBSBasicPreview::SnapItemMovement(vec2 &offset)
  568. {
  569. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  570. OBSScene scene = main->GetCurrentScene();
  571. SelectedItemBounds data;
  572. obs_scene_enum_items(scene, AddItemBounds, &data);
  573. data.tl.x += offset.x;
  574. data.tl.y += offset.y;
  575. data.br.x += offset.x;
  576. data.br.y += offset.y;
  577. vec3 snapOffset = GetSnapOffset(data.tl, data.br);
  578. const bool snap = config_get_bool(GetGlobalConfig(),
  579. "BasicWindow", "SnappingEnabled");
  580. const bool sourcesSnap = config_get_bool(GetGlobalConfig(),
  581. "BasicWindow", "SourceSnapping");
  582. if (snap == false)
  583. return;
  584. if (sourcesSnap == false) {
  585. offset.x += snapOffset.x;
  586. offset.y += snapOffset.y;
  587. return;
  588. }
  589. const float clampDist = config_get_double(GetGlobalConfig(),
  590. "BasicWindow", "SnapDistance") / main->previewScale;
  591. OffsetData offsetData;
  592. offsetData.clampDist = clampDist;
  593. offsetData.tl = data.tl;
  594. offsetData.br = data.br;
  595. vec3_copy(&offsetData.offset, &snapOffset);
  596. obs_scene_enum_items(scene, GetSourceSnapOffset, &offsetData);
  597. if (fabsf(offsetData.offset.x) > EPSILON ||
  598. fabsf(offsetData.offset.y) > EPSILON) {
  599. offset.x += offsetData.offset.x;
  600. offset.y += offsetData.offset.y;
  601. } else {
  602. offset.x += snapOffset.x;
  603. offset.y += snapOffset.y;
  604. }
  605. }
  606. static bool move_items(obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  607. {
  608. if (obs_sceneitem_locked(item))
  609. return true;
  610. bool selected = obs_sceneitem_selected(item);
  611. vec2 *offset = reinterpret_cast<vec2*>(param);
  612. if (obs_sceneitem_is_group(item) && !selected) {
  613. matrix4 transform;
  614. vec3 new_offset;
  615. vec3_set(&new_offset, offset->x, offset->y, 0.0f);
  616. obs_sceneitem_get_draw_transform(item, &transform);
  617. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  618. matrix4_inv(&transform, &transform);
  619. vec3_transform(&new_offset, &new_offset, &transform);
  620. obs_sceneitem_group_enum_items(item, move_items, &new_offset);
  621. }
  622. if (selected) {
  623. vec2 pos;
  624. obs_sceneitem_get_pos(item, &pos);
  625. vec2_add(&pos, &pos, offset);
  626. obs_sceneitem_set_pos(item, &pos);
  627. }
  628. UNUSED_PARAMETER(scene);
  629. return true;
  630. }
  631. void OBSBasicPreview::MoveItems(const vec2 &pos)
  632. {
  633. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  634. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  635. OBSScene scene = main->GetCurrentScene();
  636. vec2 offset, moveOffset;
  637. vec2_sub(&offset, &pos, &startPos);
  638. vec2_sub(&moveOffset, &offset, &lastMoveOffset);
  639. if (!(modifiers & Qt::ControlModifier))
  640. SnapItemMovement(moveOffset);
  641. vec2_add(&lastMoveOffset, &lastMoveOffset, &moveOffset);
  642. obs_scene_enum_items(scene, move_items, &moveOffset);
  643. }
  644. vec3 OBSBasicPreview::CalculateStretchPos(const vec3 &tl, const vec3 &br)
  645. {
  646. uint32_t alignment = obs_sceneitem_get_alignment(stretchItem);
  647. vec3 pos;
  648. vec3_zero(&pos);
  649. if (alignment & OBS_ALIGN_LEFT)
  650. pos.x = tl.x;
  651. else if (alignment & OBS_ALIGN_RIGHT)
  652. pos.x = br.x;
  653. else
  654. pos.x = (br.x - tl.x) * 0.5f + tl.x;
  655. if (alignment & OBS_ALIGN_TOP)
  656. pos.y = tl.y;
  657. else if (alignment & OBS_ALIGN_BOTTOM)
  658. pos.y = br.y;
  659. else
  660. pos.y = (br.y - tl.y) * 0.5f + tl.y;
  661. return pos;
  662. }
  663. void OBSBasicPreview::ClampAspect(vec3 &tl, vec3 &br, vec2 &size,
  664. const vec2 &baseSize)
  665. {
  666. float baseAspect = baseSize.x / baseSize.y;
  667. float aspect = size.x / size.y;
  668. uint32_t stretchFlags = (uint32_t)stretchHandle;
  669. if (stretchHandle == ItemHandle::TopLeft ||
  670. stretchHandle == ItemHandle::TopRight ||
  671. stretchHandle == ItemHandle::BottomLeft ||
  672. stretchHandle == ItemHandle::BottomRight) {
  673. if (aspect < baseAspect) {
  674. if ((size.y >= 0.0f && size.x >= 0.0f) ||
  675. (size.y <= 0.0f && size.x <= 0.0f))
  676. size.x = size.y * baseAspect;
  677. else
  678. size.x = size.y * baseAspect * -1.0f;
  679. } else {
  680. if ((size.y >= 0.0f && size.x >= 0.0f) ||
  681. (size.y <= 0.0f && size.x <= 0.0f))
  682. size.y = size.x / baseAspect;
  683. else
  684. size.y = size.x / baseAspect * -1.0f;
  685. }
  686. } else if (stretchHandle == ItemHandle::TopCenter ||
  687. stretchHandle == ItemHandle::BottomCenter) {
  688. if ((size.y >= 0.0f && size.x >= 0.0f) ||
  689. (size.y <= 0.0f && size.x <= 0.0f))
  690. size.x = size.y * baseAspect;
  691. else
  692. size.x = size.y * baseAspect * -1.0f;
  693. } else if (stretchHandle == ItemHandle::CenterLeft ||
  694. stretchHandle == ItemHandle::CenterRight) {
  695. if ((size.y >= 0.0f && size.x >= 0.0f) ||
  696. (size.y <= 0.0f && size.x <= 0.0f))
  697. size.y = size.x / baseAspect;
  698. else
  699. size.y = size.x / baseAspect * -1.0f;
  700. }
  701. size.x = std::round(size.x);
  702. size.y = std::round(size.y);
  703. if (stretchFlags & ITEM_LEFT)
  704. tl.x = br.x - size.x;
  705. else if (stretchFlags & ITEM_RIGHT)
  706. br.x = tl.x + size.x;
  707. if (stretchFlags & ITEM_TOP)
  708. tl.y = br.y - size.y;
  709. else if (stretchFlags & ITEM_BOTTOM)
  710. br.y = tl.y + size.y;
  711. }
  712. void OBSBasicPreview::SnapStretchingToScreen(vec3 &tl, vec3 &br)
  713. {
  714. uint32_t stretchFlags = (uint32_t)stretchHandle;
  715. vec3 newTL = GetTransformedPos(tl.x, tl.y, itemToScreen);
  716. vec3 newTR = GetTransformedPos(br.x, tl.y, itemToScreen);
  717. vec3 newBL = GetTransformedPos(tl.x, br.y, itemToScreen);
  718. vec3 newBR = GetTransformedPos(br.x, br.y, itemToScreen);
  719. vec3 boundingTL;
  720. vec3 boundingBR;
  721. vec3_copy(&boundingTL, &newTL);
  722. vec3_min(&boundingTL, &boundingTL, &newTR);
  723. vec3_min(&boundingTL, &boundingTL, &newBL);
  724. vec3_min(&boundingTL, &boundingTL, &newBR);
  725. vec3_copy(&boundingBR, &newTL);
  726. vec3_max(&boundingBR, &boundingBR, &newTR);
  727. vec3_max(&boundingBR, &boundingBR, &newBL);
  728. vec3_max(&boundingBR, &boundingBR, &newBR);
  729. vec3 offset = GetSnapOffset(boundingTL, boundingBR);
  730. vec3_add(&offset, &offset, &newTL);
  731. vec3_transform(&offset, &offset, &screenToItem);
  732. vec3_sub(&offset, &offset, &tl);
  733. if (stretchFlags & ITEM_LEFT)
  734. tl.x += offset.x;
  735. else if (stretchFlags & ITEM_RIGHT)
  736. br.x += offset.x;
  737. if (stretchFlags & ITEM_TOP)
  738. tl.y += offset.y;
  739. else if (stretchFlags & ITEM_BOTTOM)
  740. br.y += offset.y;
  741. }
  742. static float maxfunc(float x, float y)
  743. {
  744. return x > y ? x : y;
  745. }
  746. static float minfunc(float x, float y)
  747. {
  748. return x < y ? x : y;
  749. }
  750. void OBSBasicPreview::CropItem(const vec2 &pos)
  751. {
  752. obs_bounds_type boundsType = obs_sceneitem_get_bounds_type(stretchItem);
  753. uint32_t stretchFlags = (uint32_t)stretchHandle;
  754. uint32_t align = obs_sceneitem_get_alignment(stretchItem);
  755. vec3 tl, br, pos3;
  756. vec3_zero(&tl);
  757. vec3_set(&br, stretchItemSize.x, stretchItemSize.y, 0.0f);
  758. vec3_set(&pos3, pos.x, pos.y, 0.0f);
  759. vec3_transform(&pos3, &pos3, &screenToItem);
  760. obs_sceneitem_crop crop = startCrop;
  761. vec2 scale;
  762. obs_sceneitem_get_scale(stretchItem, &scale);
  763. vec2 max_tl;
  764. vec2 max_br;
  765. vec2_set(&max_tl,
  766. float(-crop.left) * scale.x,
  767. float(-crop.top) * scale.y);
  768. vec2_set(&max_br,
  769. stretchItemSize.x + crop.right * scale.x,
  770. stretchItemSize.y + crop.bottom * scale.y);
  771. typedef std::function<float (float, float)> minmax_func_t;
  772. minmax_func_t min_x = scale.x < 0.0f ? maxfunc : minfunc;
  773. minmax_func_t min_y = scale.y < 0.0f ? maxfunc : minfunc;
  774. minmax_func_t max_x = scale.x < 0.0f ? minfunc : maxfunc;
  775. minmax_func_t max_y = scale.y < 0.0f ? minfunc : maxfunc;
  776. pos3.x = min_x(pos3.x, max_br.x);
  777. pos3.x = max_x(pos3.x, max_tl.x);
  778. pos3.y = min_y(pos3.y, max_br.y);
  779. pos3.y = max_y(pos3.y, max_tl.y);
  780. if (stretchFlags & ITEM_LEFT) {
  781. float maxX = stretchItemSize.x - (2.0 * scale.x);
  782. pos3.x = tl.x = min_x(pos3.x, maxX);
  783. } else if (stretchFlags & ITEM_RIGHT) {
  784. float minX = (2.0 * scale.x);
  785. pos3.x = br.x = max_x(pos3.x, minX);
  786. }
  787. if (stretchFlags & ITEM_TOP) {
  788. float maxY = stretchItemSize.y - (2.0 * scale.y);
  789. pos3.y = tl.y = min_y(pos3.y, maxY);
  790. } else if (stretchFlags & ITEM_BOTTOM) {
  791. float minY = (2.0 * scale.y);
  792. pos3.y = br.y = max_y(pos3.y, minY);
  793. }
  794. #define ALIGN_X (ITEM_LEFT|ITEM_RIGHT)
  795. #define ALIGN_Y (ITEM_TOP|ITEM_BOTTOM)
  796. vec3 newPos;
  797. vec3_zero(&newPos);
  798. uint32_t align_x = (align & ALIGN_X);
  799. uint32_t align_y = (align & ALIGN_Y);
  800. if (align_x == (stretchFlags & ALIGN_X) && align_x != 0)
  801. newPos.x = pos3.x;
  802. else if (align & ITEM_RIGHT)
  803. newPos.x = stretchItemSize.x;
  804. else if (!(align & ITEM_LEFT))
  805. newPos.x = stretchItemSize.x * 0.5f;
  806. if (align_y == (stretchFlags & ALIGN_Y) && align_y != 0)
  807. newPos.y = pos3.y;
  808. else if (align & ITEM_BOTTOM)
  809. newPos.y = stretchItemSize.y;
  810. else if (!(align & ITEM_TOP))
  811. newPos.y = stretchItemSize.y * 0.5f;
  812. #undef ALIGN_X
  813. #undef ALIGN_Y
  814. crop = startCrop;
  815. if (stretchFlags & ITEM_LEFT)
  816. crop.left += int(std::round(tl.x / scale.x));
  817. else if (stretchFlags & ITEM_RIGHT)
  818. crop.right += int(std::round((stretchItemSize.x - br.x) / scale.x));
  819. if (stretchFlags & ITEM_TOP)
  820. crop.top += int(std::round(tl.y / scale.y));
  821. else if (stretchFlags & ITEM_BOTTOM)
  822. crop.bottom += int(std::round((stretchItemSize.y - br.y) / scale.y));
  823. vec3_transform(&newPos, &newPos, &itemToScreen);
  824. newPos.x = std::round(newPos.x);
  825. newPos.y = std::round(newPos.y);
  826. #if 0
  827. vec3 curPos;
  828. vec3_zero(&curPos);
  829. obs_sceneitem_get_pos(stretchItem, (vec2*)&curPos);
  830. blog(LOG_DEBUG, "curPos {%d, %d} - newPos {%d, %d}",
  831. int(curPos.x), int(curPos.y),
  832. int(newPos.x), int(newPos.y));
  833. blog(LOG_DEBUG, "crop {%d, %d, %d, %d}",
  834. crop.left, crop.top,
  835. crop.right, crop.bottom);
  836. #endif
  837. obs_sceneitem_defer_update_begin(stretchItem);
  838. obs_sceneitem_set_crop(stretchItem, &crop);
  839. if (boundsType == OBS_BOUNDS_NONE)
  840. obs_sceneitem_set_pos(stretchItem, (vec2*)&newPos);
  841. obs_sceneitem_defer_update_end(stretchItem);
  842. }
  843. void OBSBasicPreview::StretchItem(const vec2 &pos)
  844. {
  845. Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
  846. obs_bounds_type boundsType = obs_sceneitem_get_bounds_type(stretchItem);
  847. uint32_t stretchFlags = (uint32_t)stretchHandle;
  848. bool shiftDown = (modifiers & Qt::ShiftModifier);
  849. vec3 tl, br, pos3;
  850. vec3_zero(&tl);
  851. vec3_set(&br, stretchItemSize.x, stretchItemSize.y, 0.0f);
  852. vec3_set(&pos3, pos.x, pos.y, 0.0f);
  853. vec3_transform(&pos3, &pos3, &screenToItem);
  854. if (stretchFlags & ITEM_LEFT)
  855. tl.x = pos3.x;
  856. else if (stretchFlags & ITEM_RIGHT)
  857. br.x = pos3.x;
  858. if (stretchFlags & ITEM_TOP)
  859. tl.y = pos3.y;
  860. else if (stretchFlags & ITEM_BOTTOM)
  861. br.y = pos3.y;
  862. if (!(modifiers & Qt::ControlModifier))
  863. SnapStretchingToScreen(tl, br);
  864. obs_source_t *source = obs_sceneitem_get_source(stretchItem);
  865. vec2 baseSize;
  866. vec2_set(&baseSize,
  867. float(obs_source_get_width(source)),
  868. float(obs_source_get_height(source)));
  869. vec2 size;
  870. vec2_set(&size,br. x - tl.x, br.y - tl.y);
  871. if (boundsType != OBS_BOUNDS_NONE) {
  872. if (shiftDown)
  873. ClampAspect(tl, br, size, baseSize);
  874. if (tl.x > br.x) std::swap(tl.x, br.x);
  875. if (tl.y > br.y) std::swap(tl.y, br.y);
  876. vec2_abs(&size, &size);
  877. obs_sceneitem_set_bounds(stretchItem, &size);
  878. } else {
  879. obs_sceneitem_crop crop;
  880. obs_sceneitem_get_crop(stretchItem, &crop);
  881. baseSize.x -= float(crop.left + crop.right);
  882. baseSize.y -= float(crop.top + crop.bottom);
  883. if (!shiftDown)
  884. ClampAspect(tl, br, size, baseSize);
  885. vec2_div(&size, &size, &baseSize);
  886. obs_sceneitem_set_scale(stretchItem, &size);
  887. }
  888. pos3 = CalculateStretchPos(tl, br);
  889. vec3_transform(&pos3, &pos3, &itemToScreen);
  890. vec2 newPos;
  891. vec2_set(&newPos, std::round(pos3.x), std::round(pos3.y));
  892. obs_sceneitem_set_pos(stretchItem, &newPos);
  893. }
  894. void OBSBasicPreview::mouseMoveEvent(QMouseEvent *event)
  895. {
  896. if (scrollMode && event->buttons() == Qt::LeftButton) {
  897. scrollingOffset.x += event->x() - scrollingFrom.x;
  898. scrollingOffset.y += event->y() - scrollingFrom.y;
  899. scrollingFrom.x = event->x();
  900. scrollingFrom.y = event->y();
  901. emit DisplayResized();
  902. return;
  903. }
  904. if (locked)
  905. return;
  906. if (mouseDown) {
  907. vec2 pos = GetMouseEventPos(event);
  908. if (!mouseMoved && !mouseOverItems &&
  909. stretchHandle == ItemHandle::None) {
  910. ProcessClick(startPos);
  911. mouseOverItems = SelectedAtPos(startPos);
  912. }
  913. pos.x = std::round(pos.x);
  914. pos.y = std::round(pos.y);
  915. if (stretchHandle != ItemHandle::None) {
  916. OBSBasic *main = reinterpret_cast<OBSBasic*>(
  917. App()->GetMainWindow());
  918. OBSScene scene = main->GetCurrentScene();
  919. obs_sceneitem_t *group = obs_sceneitem_get_group(
  920. scene, stretchItem);
  921. if (group) {
  922. vec3 group_pos;
  923. vec3_set(&group_pos, pos.x, pos.y, 0.0f);
  924. vec3_transform(&group_pos, &group_pos,
  925. &invGroupTransform);
  926. pos.x = group_pos.x;
  927. pos.y = group_pos.y;
  928. }
  929. if (cropping)
  930. CropItem(pos);
  931. else
  932. StretchItem(pos);
  933. } else if (mouseOverItems) {
  934. MoveItems(pos);
  935. }
  936. mouseMoved = true;
  937. }
  938. }
  939. static void DrawCircleAtPos(float x, float y)
  940. {
  941. struct vec3 pos;
  942. vec3_set(&pos, x, y, 0.0f);
  943. struct matrix4 matrix;
  944. gs_matrix_get(&matrix);
  945. vec3_transform(&pos, &pos, &matrix);
  946. gs_matrix_push();
  947. gs_matrix_identity();
  948. gs_matrix_translate(&pos);
  949. gs_matrix_scale3f(HANDLE_RADIUS, HANDLE_RADIUS, 1.0f);
  950. gs_draw(GS_LINESTRIP, 0, 0);
  951. gs_matrix_pop();
  952. }
  953. static inline bool crop_enabled(const obs_sceneitem_crop *crop)
  954. {
  955. return crop->left > 0 ||
  956. crop->top > 0 ||
  957. crop->right > 0 ||
  958. crop->bottom > 0;
  959. }
  960. bool OBSBasicPreview::DrawSelectedItem(obs_scene_t *scene,
  961. obs_sceneitem_t *item, void *param)
  962. {
  963. if (obs_sceneitem_locked(item))
  964. return true;
  965. if (!SceneItemHasVideo(item))
  966. return true;
  967. if (obs_sceneitem_is_group(item)) {
  968. matrix4 mat;
  969. obs_sceneitem_get_draw_transform(item, &mat);
  970. gs_matrix_push();
  971. gs_matrix_mul(&mat);
  972. obs_sceneitem_group_enum_items(item, DrawSelectedItem, param);
  973. gs_matrix_pop();
  974. }
  975. if (!obs_sceneitem_selected(item))
  976. return true;
  977. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  978. matrix4 boxTransform;
  979. matrix4 invBoxTransform;
  980. obs_sceneitem_get_box_transform(item, &boxTransform);
  981. matrix4_inv(&invBoxTransform, &boxTransform);
  982. vec3 bounds[] = {
  983. {{{0.f, 0.f, 0.f}}},
  984. {{{1.f, 0.f, 0.f}}},
  985. {{{0.f, 1.f, 0.f}}},
  986. {{{1.f, 1.f, 0.f}}},
  987. };
  988. bool visible = std::all_of(std::begin(bounds), std::end(bounds),
  989. [&](const vec3 &b)
  990. {
  991. vec3 pos;
  992. vec3_transform(&pos, &b, &boxTransform);
  993. vec3_transform(&pos, &pos, &invBoxTransform);
  994. return CloseFloat(pos.x, b.x) && CloseFloat(pos.y, b.y);
  995. });
  996. if (!visible)
  997. return true;
  998. obs_transform_info info;
  999. obs_sceneitem_get_info(item, &info);
  1000. gs_load_vertexbuffer(main->circle);
  1001. gs_matrix_push();
  1002. gs_matrix_mul(&boxTransform);
  1003. DrawCircleAtPos(0.0f, 0.0f);
  1004. DrawCircleAtPos(0.0f, 1.0f);
  1005. DrawCircleAtPos(1.0f, 0.0f);
  1006. DrawCircleAtPos(1.0f, 1.0f);
  1007. DrawCircleAtPos(0.5f, 0.0f);
  1008. DrawCircleAtPos(0.0f, 0.5f);
  1009. DrawCircleAtPos(0.5f, 1.0f);
  1010. DrawCircleAtPos(1.0f, 0.5f);
  1011. obs_sceneitem_crop crop;
  1012. obs_sceneitem_get_crop(item, &crop);
  1013. if (info.bounds_type == OBS_BOUNDS_NONE && crop_enabled(&crop)) {
  1014. vec4 color;
  1015. gs_effect_t *eff = gs_get_effect();
  1016. gs_eparam_t *param = gs_effect_get_param_by_name(eff, "color");
  1017. #define DRAW_SIDE(side, vb) \
  1018. if (crop.side > 0) \
  1019. vec4_set(&color, 0.0f, 1.0f, 0.0f, 1.0f); \
  1020. else \
  1021. vec4_set(&color, 1.0f, 0.0f, 0.0f, 1.0f); \
  1022. gs_effect_set_vec4(param, &color); \
  1023. gs_load_vertexbuffer(main->vb); \
  1024. gs_draw(GS_LINESTRIP, 0, 0);
  1025. DRAW_SIDE(left, boxLeft);
  1026. DRAW_SIDE(top, boxTop);
  1027. DRAW_SIDE(right, boxRight);
  1028. DRAW_SIDE(bottom, boxBottom);
  1029. #undef DRAW_SIDE
  1030. } else {
  1031. gs_load_vertexbuffer(main->box);
  1032. gs_draw(GS_LINESTRIP, 0, 0);
  1033. }
  1034. gs_matrix_pop();
  1035. UNUSED_PARAMETER(scene);
  1036. UNUSED_PARAMETER(param);
  1037. return true;
  1038. }
  1039. void OBSBasicPreview::DrawSceneEditing()
  1040. {
  1041. if (locked)
  1042. return;
  1043. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  1044. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  1045. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  1046. vec4 color;
  1047. vec4_set(&color, 1.0f, 0.0f, 0.0f, 1.0f);
  1048. gs_effect_set_vec4(gs_effect_get_param_by_name(solid, "color"), &color);
  1049. gs_technique_begin(tech);
  1050. gs_technique_begin_pass(tech, 0);
  1051. OBSScene scene = main->GetCurrentScene();
  1052. if (scene) {
  1053. gs_matrix_push();
  1054. gs_matrix_scale3f(main->previewScale, main->previewScale, 1.0f);
  1055. obs_scene_enum_items(scene, DrawSelectedItem, this);
  1056. gs_matrix_pop();
  1057. }
  1058. gs_load_vertexbuffer(nullptr);
  1059. gs_technique_end_pass(tech);
  1060. gs_technique_end(tech);
  1061. }
  1062. void OBSBasicPreview::ResetScrollingOffset()
  1063. {
  1064. vec2_zero(&scrollingOffset);
  1065. }
  1066. void OBSBasicPreview::SetScalingLevel(int32_t newScalingLevelVal) {
  1067. float newScalingAmountVal = pow(ZOOM_SENSITIVITY, float(newScalingLevelVal));
  1068. scalingLevel = newScalingLevelVal;
  1069. SetScalingAmount(newScalingAmountVal);
  1070. }
  1071. void OBSBasicPreview::SetScalingAmount(float newScalingAmountVal) {
  1072. scrollingOffset.x *= newScalingAmountVal / scalingAmount;
  1073. scrollingOffset.y *= newScalingAmountVal / scalingAmount;
  1074. scalingAmount = newScalingAmountVal;
  1075. }