window-basic-preview.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #include <obs.hpp>
  3. #include <graphics/vec2.h>
  4. #include <graphics/matrix4.h>
  5. #include "qt-display.hpp"
  6. #include "obs-app.hpp"
  7. class OBSBasic;
  8. class QMouseEvent;
  9. #define ITEM_LEFT (1 << 0)
  10. #define ITEM_RIGHT (1 << 1)
  11. #define ITEM_TOP (1 << 2)
  12. #define ITEM_BOTTOM (1 << 3)
  13. #define ZOOM_SENSITIVITY 1.125f
  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. };
  25. class OBSBasicPreview : public OBSQTDisplay {
  26. Q_OBJECT
  27. friend class SourceTree;
  28. private:
  29. obs_sceneitem_crop startCrop;
  30. vec2 startItemPos;
  31. vec2 cropSize;
  32. OBSSceneItem stretchGroup;
  33. OBSSceneItem stretchItem;
  34. ItemHandle stretchHandle = ItemHandle::None;
  35. vec2 stretchItemSize;
  36. matrix4 screenToItem;
  37. matrix4 itemToScreen;
  38. matrix4 invGroupTransform;
  39. gs_texture_t *overflow = nullptr;
  40. vec2 startPos;
  41. vec2 lastMoveOffset;
  42. vec2 scrollingFrom;
  43. vec2 scrollingOffset;
  44. bool mouseDown = false;
  45. bool mouseMoved = false;
  46. bool mouseOverItems = false;
  47. bool cropping = false;
  48. bool locked = false;
  49. bool scrollMode = false;
  50. bool fixedScaling = false;
  51. int32_t scalingLevel = 0;
  52. float scalingAmount = 1.0f;
  53. obs_sceneitem_t *hoveredPreviewItem = nullptr;
  54. obs_sceneitem_t *hoveredListItem = nullptr;
  55. static vec2 GetMouseEventPos(QMouseEvent *event);
  56. static bool DrawSelectedOverflow(obs_scene_t *scene,
  57. obs_sceneitem_t *item, void *param);
  58. static bool DrawSelectedItem(obs_scene_t *scene, obs_sceneitem_t *item,
  59. void *param);
  60. static OBSSceneItem GetItemAtPos(const vec2 &pos, bool selectBelow);
  61. static bool SelectedAtPos(const vec2 &pos);
  62. static void DoSelect(const vec2 &pos);
  63. static void DoCtrlSelect(const vec2 &pos);
  64. static vec3 GetSnapOffset(const vec3 &tl, const vec3 &br);
  65. void GetStretchHandleData(const vec2 &pos);
  66. void SnapStretchingToScreen(vec3 &tl, vec3 &br);
  67. void ClampAspect(vec3 &tl, vec3 &br, vec2 &size, const vec2 &baseSize);
  68. vec3 CalculateStretchPos(const vec3 &tl, const vec3 &br);
  69. void CropItem(const vec2 &pos);
  70. void StretchItem(const vec2 &pos);
  71. static void SnapItemMovement(vec2 &offset);
  72. void MoveItems(const vec2 &pos);
  73. void ProcessClick(const vec2 &pos);
  74. public:
  75. OBSBasicPreview(QWidget *parent, Qt::WindowFlags flags = 0);
  76. ~OBSBasicPreview();
  77. static OBSBasicPreview *Get();
  78. virtual void keyPressEvent(QKeyEvent *event) override;
  79. virtual void keyReleaseEvent(QKeyEvent *event) override;
  80. virtual void wheelEvent(QWheelEvent *event) override;
  81. virtual void mousePressEvent(QMouseEvent *event) override;
  82. virtual void mouseReleaseEvent(QMouseEvent *event) override;
  83. virtual void mouseMoveEvent(QMouseEvent *event) override;
  84. virtual void leaveEvent(QEvent *event) override;
  85. void DrawOverflow();
  86. void DrawSceneEditing();
  87. inline void SetLocked(bool newLockedVal) { locked = newLockedVal; }
  88. inline void ToggleLocked() { locked = !locked; }
  89. inline bool Locked() const { return locked; }
  90. inline void SetFixedScaling(bool newFixedScalingVal)
  91. {
  92. fixedScaling = newFixedScalingVal;
  93. }
  94. inline bool IsFixedScaling() const { return fixedScaling; }
  95. void SetScalingLevel(int32_t newScalingLevelVal);
  96. void SetScalingAmount(float newScalingAmountVal);
  97. inline int32_t GetScalingLevel() const { return scalingLevel; }
  98. inline float GetScalingAmount() const { return scalingAmount; }
  99. void ResetScrollingOffset();
  100. inline void SetScrollingOffset(float x, float y)
  101. {
  102. vec2_set(&scrollingOffset, x, y);
  103. }
  104. inline float GetScrollX() const { return scrollingOffset.x; }
  105. inline float GetScrollY() const { return scrollingOffset.y; }
  106. /* use libobs allocator for alignment because the matrices itemToScreen
  107. * and screenToItem may contain SSE data, which will cause SSE
  108. * instructions to crash if the data is not aligned to at least a 16
  109. * byte boundary. */
  110. static inline void *operator new(size_t size) { return bmalloc(size); }
  111. static inline void operator delete(void *ptr) { bfree(ptr); }
  112. };