ToolPalette.hpp
1 #pragma once
2 #include <Process/Focus/FocusDispatcher.hpp>
3 #include <Process/ProcessContext.hpp>
4 
5 #include <score/tools/Bind.hpp>
6 
7 #include <QGuiApplication>
8 #include <QPointF>
9 #include <QScreen>
10 
11 #include <chrono>
12 template <typename Tool_T, typename ToolPalette_T, typename Context_T, typename Input_T>
13 class ToolPaletteInputDispatcher : public QObject
14 {
15 public:
17  const Input_T& input, ToolPalette_T& palette, const Context_T& context)
18  : m_palette{palette}
19  , m_context{context}
20  , m_currentTool{palette.editionSettings().tool()}
21  {
22  auto screens = QGuiApplication::screens();
23  if(!screens.empty())
24  {
25  m_frameTime = 1000000. / screens.front()->refreshRate();
26  }
27  using EditionSettings_T
28  = std::remove_reference_t<decltype(palette.editionSettings())>;
29  con(palette.editionSettings(), &EditionSettings_T::toolChanged, this,
30  &ToolPaletteInputDispatcher::on_toolChanged);
31  con(input, &Input_T::pressed, this, &ToolPaletteInputDispatcher::on_pressed);
32  con(input, &Input_T::moved, this, &ToolPaletteInputDispatcher::on_moved);
33  con(input, &Input_T::released, this, &ToolPaletteInputDispatcher::on_released);
34  con(input, &Input_T::escPressed, this, &ToolPaletteInputDispatcher::on_cancel);
35  }
36 
37  void on_toolChanged(Tool_T t)
38  {
39  m_palette.desactivate(m_currentTool);
40  m_palette.activate(t);
41  m_currentTool = t;
42  if(m_running)
43  {
44  m_palette.on_cancel();
45  m_prev = std::chrono::steady_clock::now();
46  m_palette.on_pressed(m_currentPoint);
47  }
48  }
49 
50  void on_pressed(QPointF p)
51  {
52  if constexpr(std::is_same_v<decltype(m_context.presenter), Process::LayerPresenter&>)
53  {
54  m_context.context.focusDispatcher.focus(&m_context.presenter);
55  }
56  m_currentPoint = p;
57  m_prev = std::chrono::steady_clock::now();
58  m_palette.on_pressed(p);
59  m_running = true;
60  }
61 
62  void on_moved(QPointF p)
63  {
64  using namespace std::literals::chrono_literals;
65  const auto t = std::chrono::steady_clock::now();
66  if(t - m_prev < std::chrono::microseconds((int64_t)m_frameTime))
67  {
68  m_elapsedPoint = p;
69  }
70  m_currentPoint = p;
71  m_palette.on_moved(p);
72  m_prev = t;
73  }
74 
75  void on_released(QPointF p)
76  {
77  m_running = false;
78 
79  m_currentPoint = p;
80  m_palette.on_released(p);
81  }
82 
83  void on_cancel()
84  {
85  m_running = false;
86 
87  m_palette.on_cancel();
88  }
89 
90 private:
91  ToolPalette_T& m_palette;
92  const Context_T& m_context;
93  QPointF m_currentPoint;
94  Tool_T m_currentTool;
95 
96  std::chrono::steady_clock::time_point m_prev;
97  QPointF m_elapsedPoint;
98 
99  qreal m_frameTime{16666}; // In microseconds
100  bool m_running = false;
101 };
Definition: LayerPresenter.hpp:34
Definition: ToolPalette.hpp:14