forked from RaptDept/ptparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarline.cpp
285 lines (242 loc) · 8 KB
/
barline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/////////////////////////////////////////////////////////////////////////////
// Name: barline.cpp
// Purpose: Stores and renders barlines
// Author: Brad Larsen
// Modified by:
// Created: Jan 4, 2005
// RCS-ID:
// Copyright: (c) Brad Larsen
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
#include "stdwx.h"
#include "barline.h"
#include "powertabfileheader.h" // Needed for FILE_VERSION constants
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Default Constants
const wxByte Barline::DEFAULT_POSITION = 0;
const wxByte Barline::DEFAULT_DATA = (wxByte)(bar << 5);
// Repeat Count Constants
const wxByte Barline::MIN_REPEAT_COUNT = 2;
const wxByte Barline::MAX_REPEAT_COUNT = 24;
// Constructor/Destructor
/// Default Constructor
Barline::Barline() :
m_position(DEFAULT_POSITION), m_data(DEFAULT_DATA)
{
//------Last Checked------//
// - Jan 4, 2005
}
/// Primary Constructor
/// @param position Zero-based index of the position within the system where the
/// barline is anchored
/// @param type The type of barline (see barTypes enum)
/// @param repeatCount The repeat count to set (if type is a repeat end, use 0
/// for other types)
Barline::Barline(wxUint32 position, wxByte type, wxByte repeatCount) :
m_position(DEFAULT_POSITION), m_data(DEFAULT_DATA)
{
//------Last Checked------//
// - Jan 4, 2005
SetPosition(position);
SetType(type);
SetRepeatCount(repeatCount);
}
/// Copy Constructor
Barline::Barline(const Barline& barline) :
m_position(DEFAULT_POSITION), m_data(DEFAULT_DATA)
{
//------Last Checked------//
// - Jan 4, 2005
*this = barline;
}
/// Destructor
Barline::~Barline()
{
//------Last Checked------//
// - Jan 4, 2005
}
/// Assignment Operator
const Barline& Barline::operator=(const Barline& barline)
{
//------Last Checked------//
// - Jan 4, 2005
// Check for assignment to self
if (this != &barline)
{
m_position = barline.m_position;
m_data = barline.m_data;
m_keySignature = barline.m_keySignature;
m_timeSignature = barline.m_timeSignature;
m_rehearsalSign = barline.m_rehearsalSign;
}
return (*this);
}
/// Equality Operator
bool Barline::operator==(const Barline& barline) const
{
//------Last Checked------//
// - Jan 4, 2005
return (
(m_position == barline.m_position) &&
(m_data == barline.m_data) &&
(m_keySignature == barline.m_keySignature) &&
(m_timeSignature == barline.m_timeSignature) &&
(m_rehearsalSign == barline.m_rehearsalSign)
);
}
/// Inequality Operator
bool Barline::operator!=(const Barline& barline) const
{
//------Last Checked------//
// - Jan 4, 2005
return (!operator==(barline));
}
// Serialize Functions
/// Performs serialization for the class
/// @param stream Power Tab output stream to serialize to
/// @return True if the object was serialized, false if not
bool Barline::DoSerialize(PowerTabOutputStream& stream)
{
//------Last Checked------//
// - Jan 4, 2005
stream << m_position << m_data;
wxCHECK(stream.CheckState(), false);
m_keySignature.Serialize(stream);
wxCHECK(stream.CheckState(), false);
m_timeSignature.Serialize(stream);
wxCHECK(stream.CheckState(), false);
m_rehearsalSign.Serialize(stream);
wxCHECK(stream.CheckState(), false);
return (true);
}
/// Performs deserialization for the class
/// @param stream Power Tab input stream to load from
/// @param version File version
/// @return True if the object was deserialized, false if not
bool Barline::DoDeserialize(PowerTabInputStream& stream, wxWord version)
{
//------Last Checked------//
// - Jan 4, 2005
// Version 1.0/1.0.2 (key was stored in word)
if (version == PowerTabFileHeader::FILEVERSION_1_0 ||
version == PowerTabFileHeader::FILEVERSION_1_0_2)
{
wxWord symbol;
stream >> m_position >> symbol;
wxCHECK(stream.CheckState(), false);
wxByte temp = HIBYTE(symbol);
wxByte keyType = (wxByte)((temp >> 4) & 0xf);
wxByte keyAccidentals = (wxByte)(temp & 0xf);
// Key signature was set
if (keyType > 0)
{
// Set the key to be shown
m_keySignature.Show();
// Cancellation
if (keyType > 2)
m_keySignature.SetCancellation();
keyType = (wxByte)(((keyType % 2) == 1) ? KeySignature::majorKey :
KeySignature::minorKey);
m_keySignature.SetKey(keyType, keyAccidentals);
}
// Update the bar data (stored in low byte of symbol)
m_data = LOBYTE(symbol);
}
// CASE: Version 1.5 and up
else
{
stream >> m_position >> m_data;
wxCHECK(stream.CheckState(), false);
m_keySignature.Deserialize(stream, version);
wxCHECK(stream.CheckState(), false);
m_timeSignature.Deserialize(stream, version);
wxCHECK(stream.CheckState(), false);
m_rehearsalSign.Deserialize(stream, version);
wxCHECK(stream.CheckState(), false);
}
return (true);
}
// Barline Data Functions
/// Sets the barline data
/// @param type The type to set
/// @param repeatCount The repeat count to set
/// @return True if the data was set, false if not
bool Barline::SetBarlineData(wxByte type, wxByte repeatCount)
{
//------Last Checked------//
// - Jan 4, 2005
if (!SetType(type))
return (false);
if (!SetRepeatCount(repeatCount))
return (false);
return (true);
}
/// Gets the barline data
/// @param type Holds the type return value
/// @param repeatCount Holds the repeat count return value
void Barline::GetBarlineData(wxByte& type, wxByte& repeatCount) const
{
//------Last Checked------//
// - Jan 4, 2005
type = GetType();
repeatCount = (wxByte)GetRepeatCount();
}
// Type Functions
/// Sets the type of bar
/// @param type Type of bar to set
/// @return True if the bar type was set, false if not
bool Barline::SetType(wxByte type)
{
//------Last Checked------//
// - Jan 4, 2005
wxCHECK(IsValidType(type), false);
m_data &= ~typeMask;
m_data |= (wxByte)(type << 5);
return (true);
}
// Repeat Count Functions
/// Sets the repeat count for a repeat ending bar
/// @param repeatCount Repeat count to set
/// @return True if the repeat count was set, false if not
bool Barline::SetRepeatCount(wxUint32 repeatCount)
{
//------Last Checked------//
// - Jan 4, 2005
wxCHECK(IsValidRepeatCount(repeatCount), false);
m_data &= ~repeatCountMask;
m_data |= (wxByte)repeatCount;
return (true);
}
// Operations
/// Gets the width of the key and time signature on the barline
/// @return The width of the key and time signature on the barline
int Barline::GetKeyAndTimeSignatureWidth() const
{
//------Last Checked------//
// - Sep 01, 2007
int returnValue = 0;
// Add the width of the key signature
returnValue += m_keySignature.GetWidth();
// If the key signature has width, we need to adjust to account the right
// side of the barline
if (returnValue > 0)
{
// Some bars are thicker than others
if (IsDoubleBar())
returnValue += 2;
else if (IsRepeatStart())
returnValue += 5;
else if (GetType() >= repeatEnd)
returnValue += 6;
}
// Add the width of the time signature
int timeSignatureWidth = m_timeSignature.GetWidth();
if (timeSignatureWidth > 0)
{
// 3 units of space from barline or key signature
returnValue += (3 + timeSignatureWidth);
}
return (returnValue);
}