window-basic-preview.hpp 4.5 KB

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