Debian Codegraph @

src/Gui/Navigation/NavigationStateChart.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2025 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24
25 #ifndef GUI_NAVIGATIONSTATECHART_H
26 #define GUI_NAVIGATIONSTATECHART_H
27
28 #include <boost/statechart/event.hpp>
29 #include <Gui/Navigation/NavigationStyle.h>
30
31 // NOLINTBEGIN(cppcoreguidelines-avoid*, readability-avoid-const-params-in-decls)
32 namespace Gui
33 {
34
35 class NaviStateMachine;
36
37 class GuiExport NavigationStateChart: public UserNavigationStyle
38 {
39 using inherited = UserNavigationStyle;
40
41 TYPESYSTEM_HEADER_WITH_OVERRIDE();
42
43 public:
44 struct Event: public boost::statechart::event<Event>
45 {
46 using Button = SoMouseButtonEvent::Button;
47 using Key = SoKeyboardEvent::Key;
48
49 Event();
50 bool isMouseButtonEvent() const;
51 const SoMouseButtonEvent* asMouseButtonEvent() const;
52 bool isPress(Button button) const;
53 bool isRelease(Button button) const;
54 bool isKeyPress(Key key) const;
55 bool isKeyRelease(Key key) const;
56 bool isKeyboardEvent() const;
57 const SoKeyboardEvent* asKeyboardEvent() const;
58 bool isLocation2Event() const;
59 const SoLocation2Event* asLocation2Event() const;
60 bool isMotion3Event() const;
61 const SoMotion3Event* asMotion3Event() const;
62 bool isDownButton(unsigned int state) const;
63 bool isDownNoButton() const;
64 bool isDownButton1() const;
65 bool isDownButton2() const;
66 bool isDownButton3() const;
67 bool isDownControl() const;
68 bool isDownShift() const;
69 bool isDownAlt() const;
70
71 enum
72 {
73 // bits: 0-shift-ctrl-alt-0-lmb-mmb-rmb
74 BUTTON1DOWN = 0x00000100,
75 BUTTON2DOWN = 0x00000001,
76 BUTTON3DOWN = 0x00000010,
77 CTRLDOWN = 0x00100000,
78 SHIFTDOWN = 0x01000000,
79 ALTDOWN = 0x00010000,
80 MASKBUTTONS = BUTTON1DOWN | BUTTON2DOWN | BUTTON3DOWN,
81 MASKMODIFIERS = CTRLDOWN | SHIFTDOWN | ALTDOWN
82 };
83
84 const SoEvent* inventor_event {nullptr};
85 unsigned int modifiers {0};
86 unsigned int mbstate() const
87 {
88 return modifiers & MASKBUTTONS;
89 }
90 unsigned int kbstate() const
91 {
92 return modifiers & MASKMODIFIERS;
93 }
94
95 struct Flags
96 {
97 bool processed = false;
98 bool propagated = false;
99 };
100 std::shared_ptr<Flags> flags;
101 };
102
103 NavigationStateChart();
104 ~NavigationStateChart() override;
105
106 protected:
107 SbBool processSoEvent(const SoEvent* const ev) override;
108 std::unique_ptr<NaviStateMachine> naviMachine; // NOLINT
109 };
110
111 class GuiExport NaviStateMachine
112 {
113 public:
114 NaviStateMachine(const NaviStateMachine&) = delete;
115 NaviStateMachine(NaviStateMachine&&) = delete;
116 NaviStateMachine& operator=(const NaviStateMachine&) = delete;
117 NaviStateMachine& operator=(NaviStateMachine&&) = delete;
118
119 NaviStateMachine() = default;
120 virtual ~NaviStateMachine() = default;
121
122 virtual void process_event(const NavigationStateChart::Event&) = 0;
123 };
124
125 template<typename T>
126 class NaviStateMachineT: public NaviStateMachine
127 {
128 public:
129 explicit NaviStateMachineT(T* t)
130 : object(t)
131 {
132 object->initiate();
133 }
134
135 ~NaviStateMachineT() override
136 {
137 object.reset();
138 }
139
140 void process_event(const NavigationStateChart::Event& ev) override
141 {
142 object->process_event(ev);
143 }
144
145 private:
146 std::unique_ptr<T> object;
147 };
148
149 } // namespace Gui
150 // NOLINTEND(cppcoreguidelines-avoid*, readability-avoid-const-params-in-decls)
151
152 #endif // GUI_NAVIGATIONSTATECHART_H