window-basic-preview.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. int32_t scalingLevel = 0;
  66. float scalingAmount = 1.0f;
  67. float groupRot = 0.0f;
  68. std::vector<obs_sceneitem_t *> hoveredPreviewItems;
  69. std::vector<obs_sceneitem_t *> selectedItems;
  70. std::mutex selectMutex;
  71. static vec2 GetMouseEventPos(QMouseEvent *event);
  72. static bool FindSelected(obs_scene_t *scene, obs_sceneitem_t *item,
  73. void *param);
  74. static bool DrawSelectedOverflow(obs_scene_t *scene,
  75. obs_sceneitem_t *item, void *param);
  76. static bool DrawSelectedItem(obs_scene_t *scene, obs_sceneitem_t *item,
  77. void *param);
  78. static bool DrawSelectionBox(float x1, float y1, float x2, float y2,
  79. gs_vertbuffer_t *box);
  80. static OBSSceneItem GetItemAtPos(const vec2 &pos, bool selectBelow);
  81. static bool SelectedAtPos(const vec2 &pos);
  82. static void DoSelect(const vec2 &pos);
  83. static void DoCtrlSelect(const vec2 &pos);
  84. static vec3 GetSnapOffset(const vec3 &tl, const vec3 &br);
  85. void GetStretchHandleData(const vec2 &pos, bool ignoreGroup);
  86. void UpdateCursor(uint32_t &flags);
  87. void SnapStretchingToScreen(vec3 &tl, vec3 &br);
  88. void ClampAspect(vec3 &tl, vec3 &br, vec2 &size, const vec2 &baseSize);
  89. vec3 CalculateStretchPos(const vec3 &tl, const vec3 &br);
  90. void CropItem(const vec2 &pos);
  91. void StretchItem(const vec2 &pos);
  92. void RotateItem(const vec2 &pos);
  93. static void SnapItemMovement(vec2 &offset);
  94. void MoveItems(const vec2 &pos);
  95. void BoxItems(const vec2 &startPos, const vec2 &pos);
  96. void ProcessClick(const vec2 &pos);
  97. OBSDataAutoRelease wrapper = nullptr;
  98. bool changed;
  99. public:
  100. OBSBasicPreview(QWidget *parent,
  101. Qt::WindowFlags flags = Qt::WindowFlags());
  102. ~OBSBasicPreview();
  103. static OBSBasicPreview *Get();
  104. virtual void keyPressEvent(QKeyEvent *event) override;
  105. virtual void keyReleaseEvent(QKeyEvent *event) override;
  106. virtual void wheelEvent(QWheelEvent *event) override;
  107. virtual void mousePressEvent(QMouseEvent *event) override;
  108. virtual void mouseReleaseEvent(QMouseEvent *event) override;
  109. virtual void mouseMoveEvent(QMouseEvent *event) override;
  110. virtual void leaveEvent(QEvent *event) override;
  111. void DrawOverflow();
  112. void DrawSceneEditing();
  113. inline void SetLocked(bool newLockedVal) { locked = newLockedVal; }
  114. inline void ToggleLocked() { locked = !locked; }
  115. inline bool Locked() const { return locked; }
  116. inline void SetFixedScaling(bool newFixedScalingVal)
  117. {
  118. fixedScaling = newFixedScalingVal;
  119. }
  120. inline bool IsFixedScaling() const { return fixedScaling; }
  121. void SetScalingLevel(int32_t newScalingLevelVal);
  122. void SetScalingAmount(float newScalingAmountVal);
  123. inline int32_t GetScalingLevel() const { return scalingLevel; }
  124. inline float GetScalingAmount() const { return scalingAmount; }
  125. void ResetScrollingOffset();
  126. inline void SetScrollingOffset(float x, float y)
  127. {
  128. vec2_set(&scrollingOffset, x, y);
  129. }
  130. inline float GetScrollX() const { return scrollingOffset.x; }
  131. inline float GetScrollY() const { return scrollingOffset.y; }
  132. /* use libobs allocator for alignment because the matrices itemToScreen
  133. * and screenToItem may contain SSE data, which will cause SSE
  134. * instructions to crash if the data is not aligned to at least a 16
  135. * byte boundary. */
  136. static inline void *operator new(size_t size) { return bmalloc(size); }
  137. static inline void operator delete(void *ptr) { bfree(ptr); }
  138. OBSSourceAutoRelease spacerLabel[4];
  139. int spacerPx[4] = {0};
  140. void DrawSpacingHelpers();
  141. };