window-basic-preview.hpp 5.4 KB

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