|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 | + * (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 | + * |
| 5 | + * This file is part of lsp-tk-lib |
| 6 | + * Created on: 23 сент. 2025 г. |
| 7 | + * |
| 8 | + * lsp-tk-lib is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * any later version. |
| 12 | + * |
| 13 | + * lsp-tk-lib is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with lsp-tk-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef LSP_PLUG_IN_TK_PROP_MULTI_RANGE_H_ |
| 23 | +#define LSP_PLUG_IN_TK_PROP_MULTI_RANGE_H_ |
| 24 | + |
| 25 | +#ifndef LSP_PLUG_IN_TK_IMPL |
| 26 | + #error "use <lsp-plug.in/tk/tk.h>" |
| 27 | +#endif /* LSP_PLUG_IN_TK_IMPL */ |
| 28 | + |
| 29 | +namespace lsp |
| 30 | +{ |
| 31 | + namespace tk |
| 32 | + { |
| 33 | + /** |
| 34 | + * Floating-point range property |
| 35 | + */ |
| 36 | + class Range: public MultiProperty |
| 37 | + { |
| 38 | + protected: |
| 39 | + enum property_t |
| 40 | + { |
| 41 | + P_VALUE, |
| 42 | + P_MIN, |
| 43 | + P_MAX, |
| 44 | + |
| 45 | + P_COUNT |
| 46 | + }; |
| 47 | + |
| 48 | + enum flags_t |
| 49 | + { |
| 50 | + F_RANGE_LOCK = 1 << 0 |
| 51 | + }; |
| 52 | + |
| 53 | + protected: |
| 54 | + static const prop::desc_t DESC[]; |
| 55 | + |
| 56 | + protected: |
| 57 | + atom_t vAtoms[P_COUNT]; // Atom bindings |
| 58 | + float fMin; |
| 59 | + float fMax; |
| 60 | + size_t nFlags; |
| 61 | + float_transform_t pTransform; |
| 62 | + mutable void *pTransformArg; |
| 63 | + |
| 64 | + protected: |
| 65 | + virtual void push() override; |
| 66 | + virtual void commit(atom_t property) override; |
| 67 | + |
| 68 | + float do_limit(float v) const; |
| 69 | + float transform(float v) const; |
| 70 | + |
| 71 | + protected: |
| 72 | + explicit Range(prop::Listener *listener = NULL); |
| 73 | + Range(const Range &) = delete; |
| 74 | + Range(Range &&) = delete; |
| 75 | + virtual ~Range() override; |
| 76 | + |
| 77 | + Range & operator = (const Range &) = delete; |
| 78 | + Range & operator = (Range &&) = delete; |
| 79 | + |
| 80 | + public: |
| 81 | + inline void set_transform(float_transform_t func, void *arg = NULL) { pTransform = func; pTransformArg = arg; } |
| 82 | + inline void clear_transform() { set_transform(NULL, NULL); } |
| 83 | + |
| 84 | + public: |
| 85 | + inline float min() const { return fMin; } |
| 86 | + inline float max() const { return fMax; } |
| 87 | + inline float range() const { return fMax - fMin; } |
| 88 | + inline float abs_range() const { return (fMax > fMin) ? fMax - fMin : fMin - fMax; } |
| 89 | + inline bool range_locked() const { return nFlags & F_RANGE_LOCK; } |
| 90 | + inline bool inversed() const { return fMin > fMax; } |
| 91 | + |
| 92 | + float set_min(float v); |
| 93 | + float set_max(float v); |
| 94 | + void set(float min, float max); |
| 95 | + |
| 96 | + float get_normalized(float value) const { return Property::normalized(value, fMin, fMax); } |
| 97 | + static inline float limit(float value, float min, float max) { return Property::limit(value, min, max); } |
| 98 | + inline float limit(float v) const { return Property::limit(v, fMin, fMax); } |
| 99 | + static inline bool matches(float v, float min, float max) { return Property::matches(v, min, max); } |
| 100 | + inline bool matches(float v) const { return Property::matches(v, fMin, fMax); } |
| 101 | + }; |
| 102 | + |
| 103 | + namespace prop |
| 104 | + { |
| 105 | + /** |
| 106 | + * Range property implementation |
| 107 | + */ |
| 108 | + class Range: public tk::Range |
| 109 | + { |
| 110 | + public: |
| 111 | + explicit Range(prop::Listener *listener = NULL): tk::Range(listener) {}; |
| 112 | + Range(const Range &) = delete; |
| 113 | + Range(Range &&) = delete; |
| 114 | + |
| 115 | + Range & operator = (const Range &) = delete; |
| 116 | + Range & operator = (Range &&) = delete; |
| 117 | + |
| 118 | + public: |
| 119 | + bool lock_range(bool lock = true); |
| 120 | + inline bool unlock_range() { return lock_range(false); } |
| 121 | + float set_min(float v); |
| 122 | + float set_max(float v); |
| 123 | + void set(float min, float max); |
| 124 | + |
| 125 | + /** |
| 126 | + * Bind property with specified name to the style of linked widget |
| 127 | + */ |
| 128 | + inline status_t bind(atom_t property, Style *style) { return tk::Range::bind(property, style, vAtoms, DESC, &sListener); } |
| 129 | + inline status_t bind(const char *property, Style *style) { return tk::Range::bind(property, style, vAtoms, DESC, &sListener); } |
| 130 | + inline status_t bind(const LSPString *property, Style *style) { return tk::Range::bind(property, style, vAtoms, DESC, &sListener); } |
| 131 | + |
| 132 | + /** |
| 133 | + * Unbind property |
| 134 | + */ |
| 135 | + inline status_t unbind() { return tk::Range::unbind(vAtoms, DESC, &sListener); }; |
| 136 | + }; |
| 137 | + |
| 138 | + } /* namespace prop */ |
| 139 | + } /* namespace tk */ |
| 140 | +} /* namespace lsp */ |
| 141 | + |
| 142 | + |
| 143 | +#endif /* INCLUDE_LSP_PLUG_IN_TK_PROP_MULTI_RANGE_H_ */ |
0 commit comments