window-basic-preview.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include <obs.hpp>
  3. #include <graphics/vec2.h>
  4. #include <graphics/matrix4.h>
  5. #include <util/threading.h>
  6. #include <mutex>
  7. #include <vector>
  8. #include "qt-display.hpp"
  9. #include "obs-app.hpp"
  10. #include "preview-controls.hpp"
  11. class OBSBasic;
  12. class QMouseEvent;
  13. #define ITEM_LEFT (1 << 0)
  14. #define ITEM_RIGHT (1 << 1)
  15. #define ITEM_TOP (1 << 2)
  16. #define ITEM_BOTTOM (1 << 3)
  17. #define ITEM_ROT (1 << 4)
  18. #define MAX_SCALING_LEVEL 32
  19. #define MAX_SCALING_AMOUNT 8.0f
  20. #define ZOOM_SENSITIVITY pow(MAX_SCALING_AMOUNT, 1.0f / MAX_SCALING_LEVEL)
  21. #define SPACER_LABEL_MARGIN 6.0f
  22. enum class ItemHandle : uint32_t {
  23. None = 0,
  24. TopLeft = ITEM_TOP | ITEM_LEFT,
  25. TopCenter = ITEM_TOP,
  26. TopRight = ITEM_TOP | ITEM_RIGHT,
  27. CenterLeft = ITEM_LEFT,
  28. CenterRight = ITEM_RIGHT,
  29. BottomLeft = ITEM_BOTTOM | ITEM_LEFT,
  30. BottomCenter = ITEM_BOTTOM,
  31. BottomRight = ITEM_BOTTOM | ITEM_RIGHT,
  32. Rot = ITEM_ROT
  33. };
  34. class OBSBasicPreview : public OBSQTDisplay {
  35. Q_OBJECT
  36. friend class SourceTree;
  37. friend class SourceTreeItem;
  38. private:
  39. obs_sceneitem_crop startCrop;
  40. vec2 startItemPos;
  41. vec2 cropSize;
  42. OBSSceneItem stretchGroup;
  43. OBSSceneItem stretchItem;
  44. ItemHandle stretchHandle = ItemHandle::None;
  45. float rotateAngle;
  46. vec2 rotatePoint;
  47. vec2 offsetPoint;
  48. vec2 stretchItemSize;
  49. matrix4 screenToItem;
  50. matrix4 itemToScreen;
  51. matrix4 invGroupTransform;
  52. gs_texture_t *overflow = nullptr;
  53. gs_vertbuffer_t *rectFill = nullptr;
  54. gs_vertbuffer_t *circleFill = nullptr;
  55. vec2 startPos;
  56. vec2 mousePos;
  57. vec2 lastMoveOffset;
  58. vec2 scrollingFrom;
  59. vec2 scrollingOffset;
  60. bool mouseDown = false;
  61. bool mouseMoved = false;
  62. bool mouseOverItems = false;
  63. bool cropping = false;
  64. bool locked = false;
  65. bool scrollMode = false;
  66. bool fixedScaling = false;
  67. bool selectionBox = false;
  68. bool overflowHidden = false;
  69. bool overflowSelectionHidden = false;
  70. bool overflowAlwaysVisible = false;
  71. int32_t scalingLevel = 0;
  72. float scalingAmount = 1.0f;
  73. float groupRot = 0.0f;
  74. bool updatingXScrollBar = false;
  75. bool updatingYScrollBar = false;
  76. std::vector<obs_sceneitem_t *> hoveredPreviewItems;
  77. std::vector<obs_sceneitem_t *> selectedItems;
  78. std::mutex selectMutex;
  79. static vec2 GetMouseEventPos(QMouseEvent *event);
  80. static bool FindSelected(obs_scene_t *scene, obs_sceneitem_t *item, void *param);
  81. static bool DrawSelectedOverflow(obs_scene_t *scene, obs_sceneitem_t *item, void *param);
  82. static bool DrawSelectedItem(obs_scene_t *scene, obs_sceneitem_t *item, void *param);
  83. static bool DrawSelectionBox(float x1, float y1, float x2, float y2, gs_vertbuffer_t *box);
  84. static OBSSceneItem GetItemAtPos(const vec2 &pos, bool selectBelow);
  85. static bool SelectedAtPos(const vec2 &pos);
  86. static void DoSelect(const vec2 &pos);
  87. static void DoCtrlSelect(const vec2 &pos);
  88. static vec3 GetSnapOffset(const vec3 &tl, const vec3 &br);
  89. void GetStretchHandleData(const vec2 &pos, bool ignoreGroup);
  90. void UpdateCursor(uint32_t &flags);
  91. void SnapStretchingToScreen(vec3 &tl, vec3 &br);
  92. void ClampAspect(vec3 &tl, vec3 &br, vec2 &size, const vec2 &baseSize);
  93. vec3 CalculateStretchPos(const vec3 &tl, const vec3 &br);
  94. void CropItem(const vec2 &pos);
  95. void StretchItem(const vec2 &pos);
  96. void RotateItem(const vec2 &pos);
  97. static void SnapItemMovement(vec2 &offset);
  98. void MoveItems(const vec2 &pos);
  99. void BoxItems(const vec2 &startPos, const vec2 &pos);
  100. void ProcessClick(const vec2 &pos);
  101. OBSDataAutoRelease wrapper = nullptr;
  102. bool changed;
  103. private slots:
  104. void XScrollBarMoved(int value);
  105. void YScrollBarMoved(int value);
  106. public:
  107. OBSBasicPreview(QWidget *parent, Qt::WindowFlags flags = Qt::WindowFlags());
  108. ~OBSBasicPreview();
  109. void Init();
  110. static OBSBasicPreview *Get();
  111. virtual void keyPressEvent(QKeyEvent *event) override;
  112. virtual void keyReleaseEvent(QKeyEvent *event) override;
  113. virtual void wheelEvent(QWheelEvent *event) override;
  114. virtual void mousePressEvent(QMouseEvent *event) override;
  115. virtual void mouseReleaseEvent(QMouseEvent *event) override;
  116. virtual void mouseMoveEvent(QMouseEvent *event) override;
  117. virtual void leaveEvent(QEvent *event) override;
  118. void DrawOverflow();
  119. void DrawSceneEditing();
  120. inline void SetLocked(bool newLockedVal) { locked = newLockedVal; }
  121. inline void ToggleLocked() { locked = !locked; }
  122. inline bool Locked() const { return locked; }
  123. inline void SetFixedScaling(bool newFixedScalingVal)
  124. {
  125. if (fixedScaling == newFixedScalingVal)
  126. return;
  127. fixedScaling = newFixedScalingVal;
  128. emit fixedScalingChanged(fixedScaling);
  129. }
  130. inline bool IsFixedScaling() const { return fixedScaling; }
  131. void SetScalingLevel(int32_t newScalingLevelVal);
  132. void SetScalingAmount(float newScalingAmountVal);
  133. void SetScalingLevelAndAmount(int32_t newScalingLevelVal, float newScalingAmountVal);
  134. inline int32_t GetScalingLevel() const { return scalingLevel; }
  135. inline float GetScalingAmount() const { return scalingAmount; }
  136. void ResetScrollingOffset();
  137. inline void SetScrollingOffset(float x, float y) { vec2_set(&scrollingOffset, x, y); }
  138. inline float GetScrollX() const { return scrollingOffset.x; }
  139. inline float GetScrollY() const { return scrollingOffset.y; }
  140. inline void SetOverflowHidden(bool hidden) { overflowHidden = hidden; }
  141. inline void SetOverflowSelectionHidden(bool hidden) { overflowSelectionHidden = hidden; }
  142. inline void SetOverflowAlwaysVisible(bool visible) { overflowAlwaysVisible = visible; }
  143. inline bool GetOverflowSelectionHidden() const { return overflowSelectionHidden; }
  144. inline bool GetOverflowAlwaysVisible() const { return overflowAlwaysVisible; }
  145. /* use libobs allocator for alignment because the matrices itemToScreen
  146. * and screenToItem may contain SSE data, which will cause SSE
  147. * instructions to crash if the data is not aligned to at least a 16
  148. * byte boundary. */
  149. static inline void *operator new(size_t size) { return bmalloc(size); }
  150. static inline void operator delete(void *ptr) { bfree(ptr); }
  151. OBSSourceAutoRelease spacerLabel[4];
  152. int spacerPx[4] = {0};
  153. void DrawSpacingHelpers();
  154. void ClampScrollingOffsets();
  155. void UpdateXScrollBar(float cx);
  156. void UpdateYScrollBar(float cy);
  157. signals:
  158. void scalingChanged(float scalingAmount);
  159. void fixedScalingChanged(bool isFixed);
  160. };