window-basic-preview.hpp 3.8 KB

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