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