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 : #include "hammeronpulloff.h"
24 :
25 : #include "iengravingconfiguration.h" // IWYU pragma: keep
26 :
27 : #include "dummyparent.h"
28 : #include "mscore.h"
29 : #include "note.h"
30 : #include "score.h"
31 : #include "stafftype.h"
32 : #include "style/textstyle.h"
33 : #include "system.h"
34 :
35 : namespace mu::engraving {
36 53 : HammerOnPullOff::HammerOnPullOff(DummyParent* parent)
37 53 : : Slur(ElementType::HAMMER_ON_PULL_OFF, parent)
38 : {
39 53 : }
40 :
41 6 : HammerOnPullOff::HammerOnPullOff(const HammerOnPullOff& other)
42 6 : : Slur(other)
43 : {
44 6 : }
45 :
46 58 : HammerOnPullOffSegment::HammerOnPullOffSegment(HammerOnPullOff* sp)
47 58 : : SlurSegment(sp, ElementType::HAMMER_ON_PULL_OFF_SEGMENT)
48 : {
49 58 : }
50 :
51 0 : HammerOnPullOffSegment::HammerOnPullOffSegment(const HammerOnPullOffSegment& other)
52 0 : : SlurSegment(other)
53 : {
54 0 : }
55 :
56 0 : Color HammerOnPullOffSegment::curColor(const rendering::PaintOptions& opt) const
57 : {
58 0 : if (!opt.isPrinting && MScore::warnGuitarBends && !isValid()) {
59 0 : return selected() ? configuration()->criticalSelectedColor() : configuration()->criticalColor();
60 : }
61 :
62 0 : return SlurSegment::curColor(opt);
63 : }
64 :
65 1 : void HammerOnPullOffSegment::scanElements(std::function<void(EngravingItem*)> func)
66 : {
67 2 : for (HammerOnPullOffText* hopo : m_hopoText) {
68 1 : hopo->scanElements(func);
69 : }
70 :
71 1 : EngravingItem::scanElements(func);
72 1 : }
73 :
74 122 : void HammerOnPullOffSegment::setTrack(track_idx_t idx)
75 : {
76 122 : m_track = idx;
77 136 : for (HammerOnPullOffText* hopo : m_hopoText) {
78 14 : hopo->setTrack(idx);
79 : }
80 122 : }
81 :
82 58 : void HammerOnPullOffSegment::setSelected(bool f)
83 : {
84 58 : for (HammerOnPullOffText* text : m_hopoText) {
85 0 : text->setSelected(f);
86 : }
87 :
88 58 : SlurTieSegment::setSelected(f);
89 58 : }
90 :
91 62 : void HammerOnPullOffSegment::updateHopoText()
92 : {
93 62 : Chord* startChord = nullptr;
94 62 : if (isSingleBeginType()) {
95 62 : EngravingItem* startEl = hammerOnPullOff()->startElement();
96 62 : startChord = startEl && startEl->isChord() ? toChord(startEl) : nullptr;
97 : } else {
98 0 : ChordRest* firstCR = system()->firstChordRest(track());
99 0 : startChord = firstCR && firstCR->isChord() ? toChord(firstCR) : nullptr;
100 : }
101 :
102 62 : Chord* endChord = nullptr;
103 62 : if (isSingleEndType()) {
104 62 : EngravingItem* endEl = hammerOnPullOff()->endElement();
105 62 : endChord = endEl && endEl->isChord() ? toChord(endEl) : nullptr;
106 : } else {
107 : // If the segment doesn't end in this system, the endChord is the first chord of next system
108 0 : ChordRest* lastCR = system()->lastChordRest(track());
109 0 : if (lastCR) {
110 0 : lastCR = toChordRest(lastCR->segment()->next1WithElemsOnTrack(track())->element(track()));
111 : }
112 0 : endChord = lastCR && lastCR->isChord() ? toChord(lastCR) : nullptr;
113 : }
114 :
115 62 : if (!startChord || !endChord) {
116 0 : muse::DeleteAll(m_hopoText);
117 0 : m_hopoText.clear();
118 0 : return;
119 : }
120 :
121 62 : std::vector<HopoTextRegion> hopoTextRegions = computeHopoTextRegions(startChord, endChord);
122 62 : size_t regionCount = hopoTextRegions.size();
123 :
124 62 : size_t curRegionIdx = 0;
125 138 : for (; curRegionIdx < regionCount; ++curRegionIdx) {
126 76 : HopoTextRegion curRegion = hopoTextRegions[curRegionIdx];
127 :
128 : HammerOnPullOffText* curHopoText;
129 76 : if (curRegionIdx < m_hopoText.size()) {
130 : // Reuse existing, if available
131 10 : curHopoText = m_hopoText[curRegionIdx];
132 : } else {
133 : // Create new
134 66 : curHopoText = new HammerOnPullOffText(this);
135 66 : m_hopoText.push_back(curHopoText);
136 : }
137 :
138 76 : curHopoText->setTrack(track());
139 76 : curHopoText->setIsValid(curRegion.isValid);
140 76 : curHopoText->setIsHammerOn(curRegion.isHammerOn);
141 76 : curHopoText->setXmlText(style().styleB(Sid::hopoUpperCase) ? (curRegion.isHammerOn ? "H" : "P") : (curRegion.isHammerOn ? "h" : "p"));
142 76 : curHopoText->setStartChord(curRegion.startChord);
143 76 : curHopoText->setEndChord(curRegion.endChord);
144 : }
145 :
146 : // Delete unused
147 62 : if (curRegionIdx < m_hopoText.size()) {
148 0 : size_t unusedHopoCount = m_hopoText.size() - curRegionIdx;
149 0 : for (size_t i = 0; i < unusedHopoCount; ++i) {
150 0 : delete m_hopoText.back();
151 0 : m_hopoText.pop_back();
152 : }
153 : }
154 62 : }
155 :
156 62 : std::vector<HammerOnPullOffSegment::HopoTextRegion> HammerOnPullOffSegment::computeHopoTextRegions(Chord* startChord, Chord* endChord)
157 : {
158 62 : std::vector<HopoTextRegion> result;
159 62 : bool isTabStaff = staffType()->isTabStaff();
160 62 : if ((isTabStaff && !style().styleB(Sid::hopoShowOnTabStaves)) || (!isTabStaff && !style().styleB(Sid::hopoShowOnStandardStaves))) {
161 0 : return result;
162 : }
163 :
164 138 : for (Chord* curChord = startChord; curChord != endChord;) {
165 76 : IF_ASSERT_FAILED(curChord->tick() <= endChord->tick()) {
166 0 : break;
167 : }
168 :
169 76 : Chord* nextChord = curChord->next();
170 76 : if (!nextChord) {
171 0 : break;
172 : }
173 :
174 76 : Note* curNote = nullptr;
175 76 : Note* nextNote = nullptr;
176 76 : resolveStartEndNotes(&curNote, &nextNote, curChord, nextChord, isTabStaff);
177 :
178 76 : bool isValid = isTabStaff ? nextNote->string() == curNote->string() : nextNote->pitch() != curNote->pitch();
179 76 : bool isHammerOn = isTabStaff ? nextNote->fret() > curNote->fret() : nextNote->pitch() > curNote->pitch();
180 :
181 76 : bool startNewRegion = result.empty() || style().styleB(Sid::hopoShowAll) || result.back().isHammerOn != isHammerOn;
182 :
183 76 : if (startNewRegion) {
184 76 : HopoTextRegion region;
185 76 : region.startChord = curChord;
186 76 : region.endChord = nextChord;
187 76 : region.isValid = isValid;
188 76 : region.isHammerOn = isHammerOn;
189 76 : result.push_back(region);
190 : } else {
191 0 : HopoTextRegion& curRegion = result.back();
192 0 : curRegion.endChord = nextChord;
193 0 : curRegion.isValid = curRegion.isValid && isValid;
194 : }
195 :
196 76 : curChord = nextChord;
197 : }
198 :
199 62 : return result;
200 0 : }
201 :
202 76 : void HammerOnPullOffSegment::resolveStartEndNotes(Note** startNote, Note** endNote, Chord* startChord, Chord* endChord, bool isTabStaff)
203 : {
204 : // In future we need the ability to draw slurs between individual notes. For now, slurs are
205 : // attached to chords so we just try to guess which notes to consider for this HOPO
206 :
207 76 : const std::vector<Note*>& startNotes = startChord->notes();
208 76 : const std::vector<Note*>& endNotes = endChord->notes();
209 76 : int startNotesSize = static_cast<int>(startNotes.size());
210 76 : int endNotesSize = static_cast<int>(endNotes.size());
211 76 : int largerNoteCount = startNotesSize > endNotesSize ? startNotesSize : endNotesSize;
212 :
213 76 : for (int i = largerNoteCount - 1; i >= 0; --i) {
214 76 : int startNoteIdx = std::min(i, startNotesSize - 1);
215 76 : int endNoteIdx = std::min(i, endNotesSize - 1);
216 76 : Note* sNote = startNotes[startNoteIdx];
217 76 : Note* eNote = endNotes[endNoteIdx];
218 76 : if (sNote->tieFor() && sNote->tieFor() == eNote->tieBack()) {
219 0 : continue;
220 : }
221 :
222 76 : bool usablePair = sNote->pitch() != eNote->pitch();
223 76 : if (isTabStaff) {
224 12 : usablePair &= sNote->string() == eNote->string();
225 : }
226 :
227 76 : if (usablePair) {
228 76 : *startNote = sNote;
229 76 : *endNote = eNote;
230 76 : return;
231 : }
232 : }
233 :
234 : // Couldn't find a usable pair
235 0 : *startNote = startChord->upNote();
236 0 : *endNote = endChord->upNote();
237 : }
238 :
239 48 : bool HammerOnPullOffSegment::isUserModified() const
240 : {
241 48 : for (const HammerOnPullOffText* hopoText : m_hopoText) {
242 48 : if (hopoText->isUserModified()) {
243 48 : return true;
244 : }
245 : }
246 :
247 0 : return SlurSegment::isUserModified();
248 : }
249 :
250 0 : bool HammerOnPullOffSegment::isValid() const
251 : {
252 0 : for (const HammerOnPullOffText* hopoText : m_hopoText) {
253 0 : if (!hopoText->isValid()) {
254 0 : return false;
255 : }
256 : }
257 :
258 0 : return true;
259 : }
260 :
261 0 : void HammerOnPullOffSegment::reset()
262 : {
263 0 : for (HammerOnPullOffText* hopoText : m_hopoText) {
264 0 : hopoText->reset();
265 : }
266 :
267 0 : SlurTieSegment::reset();
268 0 : }
269 :
270 : static ElementStyle hopoStyle;
271 :
272 70 : HammerOnPullOffText::HammerOnPullOffText(HammerOnPullOffSegment* parent)
273 0 : : TextBase(ElementType::HAMMER_ON_PULL_OFF_TEXT, parent, TextStyleType::HAMMER_ON_PULL_OFF,
274 70 : ElementFlag::MOVABLE | ElementFlag::GENERATED | ElementFlag::ON_STAFF)
275 : {
276 70 : resetProperty(Pid::PLACEMENT);
277 70 : initElementStyle(&hopoStyle);
278 70 : }
279 :
280 0 : HammerOnPullOffText::HammerOnPullOffText(const HammerOnPullOffText& h)
281 0 : : TextBase(h)
282 : {
283 0 : }
284 :
285 0 : std::vector<LineF> HammerOnPullOffText::dragAnchorLines() const
286 : {
287 0 : std::vector<LineF> result;
288 :
289 0 : PointF p1 = canvasPos();
290 :
291 0 : HammerOnPullOffSegment* hopoSeg = hopoSegment();
292 0 : const Shape& hopoSegShape = hopoSeg->ldata()->shape();
293 0 : double x = ldata()->pos().x();// + hopoSegShape.bbox().x();
294 0 : double y = hopoSeg->hammerOnPullOff()->up() ? hopoSegShape.topAtX(x) : hopoSegShape.bottomAtX(x);
295 :
296 0 : PointF p2 = PointF(x, y) + hopoSeg->canvasPos();
297 :
298 0 : result.push_back(LineF(p1, p2));
299 :
300 0 : return result;
301 0 : }
302 :
303 103 : bool HammerOnPullOffText::isUserModified() const
304 : {
305 721 : for (const TextStyleProperty& p : *textStyle(textStyleType())) {
306 721 : if (getProperty(p.pid) != propertyDefault(p.pid)) {
307 103 : return true;
308 : }
309 : }
310 :
311 0 : return TextBase::isUserModified();
312 : }
313 :
314 0 : Color HammerOnPullOffText::curColor(const rendering::PaintOptions& opt) const
315 : {
316 0 : if (!isValid() && MScore::warnGuitarBends && !opt.isPrinting) {
317 0 : return selected() ? configuration()->criticalSelectedColor() : configuration()->criticalColor();
318 : }
319 :
320 0 : return TextBase::curColor(opt);
321 : }
322 :
323 1341 : PropertyValue HammerOnPullOffText::propertyDefault(Pid id) const
324 : {
325 1341 : switch (id) {
326 125 : case Pid::PLACEMENT:
327 125 : return PlacementV::ABOVE;
328 1216 : default:
329 1216 : return TextBase::propertyDefault(id);
330 : }
331 : }
332 : } // namespace mu::engraving
|