Line data Source code
1 : /** 2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #ifndef XENOLITH_RENDERER_MATERIAL2D_COMPONENTS_BUTTON_MATERIALBUTTON_H_ 24 : #define XENOLITH_RENDERER_MATERIAL2D_COMPONENTS_BUTTON_MATERIALBUTTON_H_ 25 : 26 : #include "MaterialSurface.h" 27 : #include "MaterialIconSprite.h" 28 : #include "MaterialMenuSource.h" 29 : #include "XLSubscriptionListener.h" 30 : 31 : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d { 32 : 33 : class TypescaleLabel; 34 : class MenuSource; 35 : class MenuSourceButton; 36 : 37 : class Button : public Surface { 38 : public: 39 : enum NodeMask { 40 : None = 0, 41 : LabelText = 1 << 0, 42 : LabelValue = 1 << 1, 43 : LeadingIcon = 1 << 2, 44 : TrailingIcon = 1 << 3, 45 : Labels = LabelText | LabelValue, 46 : Icons = LeadingIcon | TrailingIcon, 47 : All = LabelText | LabelValue | LeadingIcon | TrailingIcon 48 : }; 49 : 50 : static constexpr TimeInterval LongPressInterval = TimeInterval::milliseconds(350); 51 : 52 326 : virtual ~Button() { } 53 : 54 : virtual bool init(NodeStyle = NodeStyle::Filled, 55 : ColorRole = ColorRole::Primary, uint32_t schemeTag = SurfaceStyle::PrimarySchemeTag); 56 : virtual bool init(const SurfaceStyle &) override; 57 : 58 : virtual void onContentSizeDirty() override; 59 : 60 : virtual void setFollowContentSize(bool); 61 : virtual bool isFollowContentSize() const; 62 : 63 : virtual void setSwallowEvents(bool); 64 : virtual bool isSwallowEvents() const; 65 : 66 : virtual void setEnabled(bool); 67 0 : virtual bool isEnabled() const { return _enabled; } 68 : virtual bool isMenuSourceButtonEnabled() const; 69 : 70 : virtual void setNodeMask(NodeMask); 71 0 : virtual NodeMask getNodeMask() const { return _nodeMask; } 72 : 73 : virtual void setSelected(bool); 74 : virtual bool isSelected() const; 75 : 76 : virtual void setText(StringView); 77 : virtual StringView getText() const; 78 : 79 : virtual void setTextValue(StringView); 80 : virtual StringView getTextValue() const; 81 : 82 : virtual void setIconSize(float); 83 : virtual float getIconSize() const; 84 : 85 : virtual void setLeadingIconName(IconName, float progress = 0.0f); 86 : virtual IconName getLeadingIconName() const; 87 : 88 : virtual void setLeadingIconProgress(float progress, float animation = 0.0f); 89 : virtual float getLeadingIconProgress() const; 90 : 91 : virtual void setTrailingIconName(IconName); 92 : virtual IconName getTrailingIconName() const; 93 : 94 : virtual void setTrailingIconProgress(float progress, float animation = 0.0f); 95 : virtual float getTrailingIconProgress() const; 96 : 97 : virtual void setTapCallback(Function<void()> &&); 98 0 : virtual const Function<void()> &getTapCallback() const { return _callbackTap; } 99 : 100 : virtual void setLongPressCallback(Function<void()> &&); 101 0 : virtual const Function<void()> &getLongPressCallback() const { return _callbackLongPress; } 102 : 103 : virtual void setDoubleTapCallback(Function<void()> &&); 104 0 : virtual const Function<void()> &getDoubleTapCallback() const { return _callbackDoubleTap; } 105 : 106 : virtual void setMenuSourceButton(Rc<MenuSourceButton> &&); 107 : virtual MenuSourceButton *getMenuSourceButton() const; 108 : 109 : virtual void setBlendColor(ColorRole, float value); 110 : virtual void setBlendColor(const Color4F &, float value); 111 : 112 : virtual ColorRole getBlendColorRule() const; 113 : virtual const Color4F &getBlendColor() const; 114 : virtual float getBlendColorValue() const; 115 : 116 0 : virtual TypescaleLabel *getLabelTextNode() const { return _labelText; } 117 0 : virtual TypescaleLabel *getLabelValueNode() const { return _labelValue; } 118 0 : virtual IconSprite *getLeadingIconNode() const { return _leadingIcon; } 119 0 : virtual IconSprite *getTrailingIconNode() const { return _trailingIcon; } 120 : 121 0 : virtual InputListener *getInputListener() const { return _inputListener; } 122 : 123 10 : virtual DataListener<MenuSourceButton> *getMenuButtonListener() const { return _menuButtonListener; } 124 : 125 : // in input coordinates 126 : Vec2 getTouchLocation() const { return _touchLocation; } 127 : 128 : protected: 129 : virtual bool hasContent() const; 130 : virtual void updateSizeFromContent(); 131 : virtual void updateActivityState(); 132 : 133 : virtual void handleTap(); 134 : virtual void handleLongPress(); 135 : virtual void handleDoubleTap(); 136 : 137 : virtual float getWidthForContent() const; 138 : 139 : virtual void updateMenuButtonSource(); 140 : virtual void layoutContent(); 141 : 142 : InputListener *_inputListener = nullptr; 143 : TypescaleLabel *_labelText = nullptr; 144 : TypescaleLabel *_labelValue = nullptr; 145 : IconSprite *_leadingIcon = nullptr; 146 : IconSprite *_trailingIcon = nullptr; 147 : 148 : Rc<MenuSource> _floatingMenuSource; 149 : DataListener<MenuSourceButton> *_menuButtonListener = nullptr; 150 : 151 : Function<void()> _callbackTap; 152 : Function<void()> _callbackLongPress; 153 : Function<void()> _callbackDoubleTap; 154 : 155 : Vec2 _touchLocation; 156 : 157 : float _activityAnimationDuration = 0.25f; 158 : 159 : NodeMask _nodeMask = All; 160 : 161 : bool _followContentSize = true; 162 : bool _mouseOver = false; 163 : bool _enabled = true; 164 : bool _focused = false; 165 : bool _pressed = false; 166 : bool _selected = false; 167 : bool _longPressInit = false; 168 : }; 169 : 170 : SP_DEFINE_ENUM_AS_MASK(Button::NodeMask) 171 : 172 : } 173 : 174 : #endif /* XENOLITH_RENDERER_MATERIAL2D_COMPONENTS_BUTTON_MATERIALBUTTON_H_ */