Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #pragma once
24 :
25 : #include "textbase.h"
26 :
27 : namespace muse::draw {
28 : class Painter;
29 : }
30 :
31 : namespace mu::engraving {
32 : class Transaction;
33 : class Measure;
34 : class Segment;
35 :
36 : struct Dyn {
37 : DynamicType type = DynamicType::OTHER;
38 : int velocity = -1; // associated midi velocity (0-127, -1 = none)
39 : int changeInVelocity = 0;
40 : bool accent = 0; // if true add velocity to current chord velocity
41 : const char* text = nullptr; // utf8 text of dynamic
42 : };
43 :
44 : //-----------------------------------------------------------------------------
45 : // @@ Dynamic
46 : /// dynamics marker; determines midi velocity
47 : //
48 : // @P range enum (Dynamic.STAFF, .PART, .SYSTEM)
49 : //-----------------------------------------------------------------------------
50 :
51 : class Dynamic final : public TextBase
52 : {
53 1701 : OBJECT_ALLOCATOR(engraving, Dynamic)
54 1868 : DECLARE_CLASSOF(ElementType::DYNAMIC)
55 :
56 : public:
57 : struct ChangeSpeedItem {
58 : DynamicSpeed speed = DynamicSpeed::NORMAL;
59 : const char* name = nullptr;
60 : };
61 :
62 : Dynamic(DummyParentOr<Segment> parent);
63 : Dynamic(const Dynamic&);
64 22 : Dynamic* clone() const override { return new Dynamic(*this); }
65 0 : void setOwnershipParent(DummyParentOr<Segment> parent) { EngravingItem::setOwnershipParent(parent); }
66 2948 : Segment* segment() const { return (Segment*)ownershipParent(); }
67 : Measure* measure() const { return (Measure*)ownershipParent()->ownershipParent(); }
68 :
69 902 : void setDynamicType(DynamicType val) { m_dynamicType = val; }
70 : void setDynamicType(const String&);
71 :
72 6580 : DynamicType dynamicType() const { return m_dynamicType; }
73 0 : int subtype() const override { return static_cast<int>(m_dynamicType); }
74 : TranslatableString subtypeUserName() const override;
75 :
76 : void reset() override;
77 :
78 295 : void setVelocity(int v) { m_velocity = v; }
79 : int velocity() const;
80 :
81 : int changeInVelocity() const;
82 : void setChangeInVelocity(int val);
83 : Fraction velocityChangeLength(BeatsPerSecond tempo) const;
84 : bool isVelocityChangeAvailable() const;
85 :
86 32 : DynamicSpeed velChangeSpeed() const { return m_velChangeSpeed; }
87 0 : void setVelChangeSpeed(DynamicSpeed val) { m_velChangeSpeed = val; }
88 :
89 : PropertyValue getProperty(Pid propertyId) const override;
90 : bool setProperty(Pid propertyId, const PropertyValue&) override;
91 : PropertyValue propertyDefault(Pid id) const override;
92 : Sid getPropertyStyle(Pid) const override;
93 :
94 : std::unique_ptr<ElementGroup> getDragGroup(std::function<bool(const EngravingItem*)> isDragged) override;
95 :
96 : String accessibleInfo() const override;
97 : String screenReaderInfo() const override;
98 :
99 : static String dynamicText(DynamicType t);
100 647 : bool hasCustomText() const { return dynamicText(m_dynamicType) != xmlText(); }
101 :
102 : Expression* snappedExpression() const;
103 : HairpinSegment* findSnapBeforeHairpinAcrossSystemBreak() const;
104 :
105 852 : bool playDynamic() const { return m_playDynamic; }
106 0 : void setPlayDynamic(bool v) { m_playDynamic = v; }
107 :
108 : bool acceptDrop(EditData& ed) const override;
109 : EngravingItem* drop(Transaction& tx, EditData& ed) override;
110 :
111 : static int dynamicVelocity(DynamicType t);
112 1 : static const std::vector<Dyn>& dynamicList() { return DYN_LIST; }
113 :
114 1453 : bool hasVoiceAssignmentProperties() const override { return true; }
115 :
116 : void startEdit(EditData&) override;
117 : void endEdit(EditData&) override;
118 : int gripsCount() const override;
119 : std::vector<PointF> gripsPositions(const EditData& = EditData()) const override;
120 : void dragGrip(EditData& editData) override;
121 : void endDragGrip(EditData&) override;
122 :
123 : bool isEditAllowed(EditData&) const override;
124 :
125 1285 : bool positionRelativeToNoteheadRest() const override { return true; }
126 6498 : bool sizeIsSpatiumDependent() const override { return true; }
127 :
128 0 : Hairpin* leftHairpin() const { return m_leftHairpin; }
129 0 : Hairpin* rightHairpin() const { return m_rightHairpin; }
130 :
131 : bool hasLeftGrip() const;
132 : bool hasRightGrip() const;
133 :
134 : void resetLeftDragOffset() { m_leftDragOffset = 0.0; }
135 0 : void resetRightDragOffset() { m_rightDragOffset = 0.0; }
136 :
137 : double leftDragOffset() const { return m_leftDragOffset; }
138 : double rightDragOffset() const { return m_rightDragOffset; }
139 :
140 : void findAdjacentHairpins();
141 :
142 : Shape symShapeWithCutouts(SymId id) const override;
143 :
144 : static Dyn dynInfo(DynamicType type);
145 :
146 : private:
147 :
148 2083 : M_PROPERTY(bool, avoidBarLines, setAvoidBarLines)
149 4793 : M_PROPERTY(bool, centerOnNotehead, setCenterOnNotehead)
150 :
151 : std::pair<DynamicType, String> parseDynamicText(const String&) const;
152 : DynamicType m_dynamicType = DynamicType::OTHER;
153 : bool m_playDynamic = true;
154 :
155 : mutable PointF m_dragOffset;
156 : int m_velocity = -1; // associated midi velocity 0-127
157 :
158 : int m_changeInVelocity = 128;
159 : DynamicSpeed m_velChangeSpeed = DynamicSpeed::NORMAL;
160 :
161 : static const std::vector<Dyn> DYN_LIST;
162 :
163 : double m_leftDragOffset = 0.0;
164 : double m_rightDragOffset = 0.0;
165 :
166 : Hairpin* m_leftHairpin = nullptr;
167 : Hairpin* m_rightHairpin = nullptr;
168 : };
169 : }
|