Line data Source code
1 : /** 2 : Copyright (c) 2022 Roman Katuntsev <sbkarr@stappler.org> 3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 4 : 5 : Permission is hereby granted, free of charge, to any person obtaining a copy 6 : of this software and associated documentation files (the "Software"), to deal 7 : in the Software without restriction, including without limitation the rights 8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 : copies of the Software, and to permit persons to whom the Software is 10 : furnished to do so, subject to the following conditions: 11 : 12 : The above copyright notice and this permission notice shall be included in 13 : all copies or substantial portions of the Software. 14 : 15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 : THE SOFTWARE. 22 : **/ 23 : 24 : #ifndef XENOLITH_SCENE_INPUT_XLINPUTLISTENER_H_ 25 : #define XENOLITH_SCENE_INPUT_XLINPUTLISTENER_H_ 26 : 27 : #include "XLCommon.h" 28 : #include "XLNodeInfo.h" 29 : #include "XLGestureRecognizer.h" 30 : #include <bitset> 31 : 32 : namespace STAPPLER_VERSIONIZED stappler::xenolith { 33 : 34 : class Node; 35 : class Scene; 36 : class GestureRecognizer; 37 : 38 : class InputListener : public Ref { 39 : public: 40 : using EventMask = std::bitset<toInt(InputEventName::Max)>; 41 : using ButtonMask = std::bitset<toInt(InputMouseButton::Max)>; 42 : using KeyMask = std::bitset<toInt(InputKeyCode::Max)>; 43 : 44 : template<typename T> 45 : using InputCallback = Function<bool(const T &)>; 46 : 47 : using DefaultEventFilter = std::function<bool(const InputEvent &)>; 48 : using EventFilter = Function<bool(const InputEvent &, const DefaultEventFilter &)>; 49 : 50 : static EventMask EventMaskTouch; 51 : static EventMask EventMaskKeyboard; 52 : 53 : static ButtonMask makeButtonMask(std::initializer_list<InputMouseButton> &&); 54 : static EventMask makeEventMask(std::initializer_list<InputEventName> &&); 55 : static KeyMask makeKeyMask(std::initializer_list<InputKeyCode> &&); 56 : 57 : InputListener(); 58 : virtual ~InputListener(); 59 : 60 : bool init(int32_t priority = 0); 61 : 62 : void onEnter(Scene *); 63 : void onExit(); 64 : 65 : void update(UpdateTime); 66 : 67 : bool isRunning() const { return _running; } 68 : 69 : void setEnabled(bool b); 70 21098 : bool isEnabled() const { return _enabled; } 71 : 72 : void setOwner(Node *pOwner); 73 14458 : Node* getOwner() const { return _owner; } 74 : 75 : void setPriority(int32_t); 76 33958 : int32_t getPriority() const { return _priority; } 77 : 78 : void setDedicatedFocus(uint32_t); 79 33580 : uint32_t getDedicatedFocus() const { return _dedicatedFocus; } 80 : 81 : void setOpacityFilter(float value) { _opacityFilter = value; } 82 : float getOpacityFilter() const { return _opacityFilter; } 83 : 84 : void setTouchPadding(float value) { _touchPadding = value; } 85 2458 : float getTouchPadding() const { return _touchPadding; } 86 : 87 126 : void setDensity(float value) { _density = value; } 88 : float getDensity() const { return _density; } 89 : 90 : void setExclusive(); 91 : void setExclusiveForTouch(uint32_t eventId); 92 : 93 : void setSwallowEvents(EventMask &&); 94 : void setSwallowEvents(const EventMask &); 95 : void setSwallowAllEvents(); 96 : void setSwallowEvent(InputEventName); 97 : void clearSwallowAllEvents(); 98 : void clearSwallowEvent(InputEventName); 99 : void clearSwallowEvents(const EventMask &); 100 : 101 : bool isSwallowAllEvents() const; 102 : bool isSwallowAllEvents(const EventMask &) const; 103 : bool isSwallowAnyEvents(const EventMask &) const; 104 : bool isSwallowEvent(InputEventName) const; 105 : 106 : void setTouchFilter(const EventFilter &); 107 : 108 : bool shouldSwallowEvent(const InputEvent &) const; 109 : bool canHandleEvent(const InputEvent &event) const; 110 : InputEventState handleEvent(const InputEvent &event); 111 : 112 20 : GestureRecognizer *addTouchRecognizer(InputCallback<GestureData> &&, ButtonMask && = makeButtonMask({InputMouseButton::Touch})); 113 186 : GestureRecognizer *addTapRecognizer(InputCallback<GestureTap> &&, ButtonMask && = makeButtonMask({InputMouseButton::Touch}), 114 : uint32_t maxTapCount = 2); 115 : GestureRecognizer *addPressRecognizer(InputCallback<GesturePress> &&, TimeInterval interval = TapIntervalAllowed, 116 272 : bool continuous = false, ButtonMask && = makeButtonMask({InputMouseButton::Touch})); 117 : GestureRecognizer *addSwipeRecognizer(InputCallback<GestureSwipe> &&, float threshold = TapDistanceAllowed, bool sendThreshold = false, 118 68 : ButtonMask && = makeButtonMask({InputMouseButton::Touch})); 119 10 : GestureRecognizer *addPinchRecognizer(InputCallback<GesturePinch> &&, ButtonMask && = makeButtonMask({InputMouseButton::Touch})); 120 : GestureRecognizer *addScrollRecognizer(InputCallback<GestureScroll> &&); 121 : GestureRecognizer *addMoveRecognizer(InputCallback<GestureData> &&, bool withinNode = true); 122 : GestureRecognizer *addMouseOverRecognizer(InputCallback<GestureData> &&, float padding = 0.0f); 123 : 124 : GestureKeyRecognizer *addKeyRecognizer(InputCallback<GestureData> &&, KeyMask && = KeyMask()); 125 : 126 : void setPointerEnterCallback(Function<bool(bool)> &&); 127 : void setBackgroudCallback(Function<bool(bool)> &&); 128 : void setFocusCallback(Function<bool(bool)> &&); 129 : 130 : void clear(); 131 : 132 : protected: 133 : bool shouldProcessEvent(const InputEvent &) const; 134 : bool _shouldProcessEvent(const InputEvent &) const; // default realization 135 : 136 : void addEventMask(const EventMask &); 137 : 138 : GestureRecognizer *addRecognizer(GestureRecognizer *); 139 : 140 : int32_t _priority = 0; // 0 - scene graph 141 : uint32_t _dedicatedFocus = 0; // 0 - unused 142 : bool _enabled = true; 143 : bool _running = false; 144 : Node *_owner = nullptr; 145 : EventMask _eventMask; 146 : EventMask _swallowEvents; 147 : 148 : float _touchPadding = 0.0f; 149 : float _opacityFilter = 0.0f; 150 : float _density = 1.0f; 151 : 152 : Scene *_scene = nullptr; 153 : 154 : EventFilter _eventFilter; 155 : Vector<Rc<GestureRecognizer>> _recognizers; 156 : Map<InputEventName, Function<bool(bool)>> _callbacks; 157 : }; 158 : 159 : } 160 : 161 : #endif /* XENOLITH_SCENE_INPUT_XLINPUTLISTENER_H_ */