SplineCtrl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_EDITOR_CONTROLS_SPLINECTRL_H
  9. #define CRYINCLUDE_EDITOR_CONTROLS_SPLINECTRL_H
  10. #pragma once
  11. #if !defined(Q_MOC_RUN)
  12. #include <QWidget>
  13. #include <ISplines.h>
  14. #endif
  15. // Custom styles for this control.
  16. #define SPLINE_STYLE_NOGRID 0x0001
  17. #define SPLINE_STYLE_NO_TIME_MARKER 0x0002
  18. // Notify event sent when spline is being modified.
  19. #define SPLN_CHANGE (0x0001)
  20. // Notify event sent just before when spline is modified.
  21. #define SPLN_BEFORE_CHANGE (0x0002)
  22. class TimelineWidget;
  23. //////////////////////////////////////////////////////////////////////////
  24. // Spline control.
  25. //////////////////////////////////////////////////////////////////////////
  26. class CSplineCtrl
  27. : public QWidget
  28. {
  29. Q_OBJECT
  30. public:
  31. CSplineCtrl(QWidget* parent = nullptr);
  32. virtual ~CSplineCtrl();
  33. //Key functions
  34. int GetActiveKey() { return m_nActiveKey; };
  35. void SetActiveKey(int nIndex);
  36. int InsertKey(const QPoint& point);
  37. void ToggleKeySlope(int nIndex, int nDist);
  38. void SetGrid(int numX, int numY) { m_gridX = numX; m_gridY = numY; };
  39. void SetTimeRange(float tmin, float tmax) { m_fMinTime = tmin; m_fMaxTime = tmax; }
  40. void SetValueRange(float tmin, float tmax)
  41. {
  42. m_fMinValue = tmin;
  43. m_fMaxValue = tmax;
  44. if (m_fMinValue == m_fMaxValue)
  45. {
  46. m_fMaxValue = m_fMinValue + 0.001f;
  47. }
  48. }
  49. void SetTooltipValueScale(float x, float y) { m_fTooltipScaleX = x; m_fTooltipScaleY = y; };
  50. // Lock value of first and last key to be the same.
  51. void LockFirstAndLastKeys(bool bLock) { m_bLockFirstLastKey = bLock; }
  52. void SetSpline(ISplineInterpolator* pSpline, bool bRedraw = false);
  53. ISplineInterpolator* GetSpline();
  54. void SetTimeMarker(float fTime);
  55. void SetTimelineCtrl(TimelineWidget* pTimelineCtrl);
  56. void UpdateToolTip();
  57. typedef AZStd::function<void(CSplineCtrl*)> UpdateCallback;
  58. void SetUpdateCallback(UpdateCallback cb) { m_updateCallback = cb; };
  59. Q_SIGNALS:
  60. void beforeChange();
  61. void change();
  62. protected:
  63. enum EHitCode
  64. {
  65. HIT_NOTHING,
  66. HIT_KEY,
  67. HIT_SPLINE,
  68. };
  69. void mousePressEvent(QMouseEvent* event) override;
  70. void mouseReleaseEvent(QMouseEvent* event) override;
  71. void paintEvent(QPaintEvent* event) override;
  72. void resizeEvent(QResizeEvent* event) override;
  73. void OnLButtonDown(const QPoint& point, Qt::KeyboardModifiers modifiers);
  74. void mouseMoveEvent(QMouseEvent* event) override;
  75. void OnLButtonUp(const QPoint& point, Qt::KeyboardModifiers modifiers);
  76. void OnRButtonDown(const QPoint& point, Qt::KeyboardModifiers modifiers);
  77. void OnSetCursor();
  78. void mouseDoubleClickEvent(QMouseEvent* event) override;
  79. void keyPressEvent(QKeyEvent* event) override;
  80. // Drawing functions
  81. void DrawGrid(QPainter* pDC);
  82. void DrawSpline(QPainter* pDC);
  83. void DrawKeys(QPainter* pDC);
  84. void DrawTimeMarker(QPainter* pDC);
  85. EHitCode HitTest(const QPoint& point);
  86. //Tracking support helper functions
  87. void StartTracking();
  88. void TrackKey(const QPoint& point);
  89. void StopTracking();
  90. void RemoveKey(int nKey);
  91. QPoint KeyToPoint(int nKey);
  92. QPoint TimeToPoint(float time);
  93. void PointToTimeValue(const QPoint& point, float& time, float& value);
  94. float XOfsToTime(int x);
  95. QPoint XOfsToPoint(int x);
  96. void ClearSelection();
  97. void ValidateSpline();
  98. private:
  99. ISplineInterpolator* m_pSpline;
  100. QRect m_rcClipRect;
  101. QRect m_rcSpline;
  102. QPoint m_hitPoint;
  103. EHitCode m_hitCode;
  104. int m_nHitKeyIndex;
  105. int m_nHitKeyDist;
  106. float m_fTimeMarker;
  107. int m_nActiveKey;
  108. int m_nKeyDrawRadius;
  109. bool m_bTracking;
  110. int m_gridX;
  111. int m_gridY;
  112. float m_fMinTime, m_fMaxTime;
  113. float m_fMinValue, m_fMaxValue;
  114. float m_fTooltipScaleX, m_fTooltipScaleY;
  115. bool m_bLockFirstLastKey;
  116. std::vector<int> m_bSelectedKeys;
  117. TimelineWidget* m_pTimelineCtrl;
  118. QRect m_TimeUpdateRect;
  119. UpdateCallback m_updateCallback;
  120. };
  121. #endif // CRYINCLUDE_EDITOR_CONTROLS_SPLINECTRL_H