OBSBasicPreview.hpp 6.1 KB

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