window-basic-preview.hpp 4.0 KB

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