-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirection.cpp
328 lines (277 loc) · 10.3 KB
/
direction.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/////////////////////////////////////////////////////////////////////////////
// Name: direction.cpp
// Purpose: Stores and renders directions
// Author: Brad Larsen
// Modified by:
// Created: Jan 12, 2005
// RCS-ID:
// Copyright: (c) Brad Larsen
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
#include "stdwx.h"
#include "direction.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Default constants
const wxByte Direction::DEFAULT_POSITION = 0;
// Position Constants
const wxUint32 Direction::MIN_POSITION = 0;
const wxUint32 Direction::MAX_POSITION = 255;
// Symbol Constants
const wxByte Direction::MAX_SYMBOLS = 3;
const wxByte Direction::NUM_SYMBOL_TYPES = 19;
// Repeat Number Constants
const wxByte Direction::MIN_REPEAT_NUMBER = 0;
const wxByte Direction::MAX_REPEAT_NUMBER = 24;
static wxChar* directionText[Direction::NUM_SYMBOL_TYPES] =
{
wxT("Coda"), wxT("Double Coda"), wxT("Segno"), wxT("Segno Segno"),
wxT("Fine"), wxT("D.C."), wxT("D.S."), wxT("D.S.S."), wxT("To Coda"),
wxT("To Dbl. Coda"), wxT("D.C. al Coda"), wxT("D.C. al Dbl. Coda"),
wxT("D.S. al Coda"), wxT("D.S. al Dbl. Coda"), wxT("D.S.S. al Coda"),
wxT("D.S.S. al Dbl. Coda"), wxT("D.C. al Fine"), wxT("D.S. al Fine"),
wxT("D.S.S. al Fine")
};
// Constructor/Destructor
/// Default Constructor
Direction::Direction() :
m_position(DEFAULT_POSITION)
{
//------Last Checked------//
// - Jan 11, 2005
}
/// Primary Constructor
/// @param position Zero-based index of the position within the system where the
/// barline is anchored
/// @param symbolType Type of symbol to add (see symbolTypes enum for values)
/// @param activeSymbol Symbol that must be active for the symbol to be
/// triggered (see activeSymbols enum for values)
/// @param repeatNumber Repeat number that must be active for the symbol to be
/// triggered (0 = none)
Direction::Direction(wxUint32 position, wxByte symbolType, wxByte activeSymbol,
wxByte repeatNumber) : m_position(position)
{
//------Last Checked------//
// - Jan 11, 2005
wxASSERT(IsValidPosition(position));
AddSymbol(symbolType, activeSymbol, repeatNumber);
}
/// Copy constructor
Direction::Direction(const Direction& direction) :
m_position(DEFAULT_POSITION)
{
//------Last Checked------//
// - Jan 11, 2005
*this = direction;
}
/// Destructor
Direction::~Direction()
{
//------Last Checked------//
// - Jan 11, 2005
DeleteSymbolArrayContents();
}
/// Assignment Operator
const Direction& Direction::operator=(const Direction& direction)
{
//------Last Checked------//
// - Jan 11, 2005
// Check for assignment to self
if (this != &direction)
{
m_position = direction.m_position;
DeleteSymbolArrayContents();
size_t i = 0;
size_t count = direction.m_symbolArray.GetCount();
for (; i < count; i++)
m_symbolArray.Add(direction.m_symbolArray[i]);
}
return (*this);
}
/// Equality Operator
bool Direction::operator==(const Direction& direction) const
{
//------Last Checked------//
// - Jan 11, 2005
size_t thisSymbolCount = GetSymbolCount();
size_t otherSymbolCount = direction.GetSymbolCount();
// Directions have differing number of symbols
if (thisSymbolCount != otherSymbolCount)
return (false);
// All symbols must match
size_t i = 0;
for (; i < thisSymbolCount; i++)
{
if (m_symbolArray[i] != direction.m_symbolArray[i])
return (false);
}
return (m_position == direction.m_position);
}
/// Inequality Operator
bool Direction::operator!=(const Direction& direction) const
{
//------Last Checked------//
// - Jan 5, 2005
return (!operator==(direction));
}
// 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 Direction::DoSerialize(PowerTabOutputStream& stream)
{
//------Last Checked------//
// - Jan 11, 2005
stream << m_position;
wxCHECK(stream.CheckState(), false);
size_t symbolCount = GetSymbolCount();
stream << (wxByte)symbolCount;
wxCHECK(stream.CheckState(), false);
size_t i = 0;
for (; i < symbolCount; i++)
{
stream << m_symbolArray[i];
wxCHECK(stream.CheckState(), false);
}
return (stream.CheckState());
}
/// 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 Direction::DoDeserialize(PowerTabInputStream& stream,
wxWord WXUNUSED(version))
{
//------Last Checked------//
// - Apr 22, 2007
stream >> m_position;
wxCHECK(stream.CheckState(), false);
wxByte symbolCount;
stream >> symbolCount;
wxCHECK(stream.CheckState(), false);
size_t i = 0;
for (i = 0; i < symbolCount; i++)
{
wxWord symbol = 0;
stream >> symbol;
wxCHECK(stream.CheckState(), false);
m_symbolArray.Add(symbol);
}
return (stream.CheckState());
}
// Symbol Functions
/// Adds a symbol to the symbol array
/// @param symbolType Type of symbol to add (see symbolTypes enum for values)
/// @param activeSymbol Symbol that must be active for the symbol to be
/// triggered (see activeSymbols enum for values)
/// @param repeatNumber Repeat number that must be active for the symbol to be
/// triggered (0 = none)
/// @return True if the symbol was added, false if not
bool Direction::AddSymbol(wxByte symbolType, wxByte activeSymbol,
wxByte repeatNumber)
{
//------Last Checked------//
// - Jan 11, 2005
wxCHECK(IsValidSymbolType(symbolType), false);
wxCHECK(IsValidActiveSymbol(activeSymbol), false);
wxCHECK(IsValidRepeatNumber(repeatNumber), false);
// Can't add anymore symbols
if (GetSymbolCount() == MAX_SYMBOLS)
return (false);
// Add a symbol to the end of the array, then set the data
m_symbolArray.Add(0);
return (SetSymbol(GetSymbolCount() - 1, symbolType, activeSymbol,
repeatNumber));
}
/// Sets the data for an existing symbol in the symbol array
/// @param index Index of the symbol to set the data for
/// @param symbolType Type of symbol (see symbolTypes enum for values)
/// @param activeSymbol Symbol that must be active for the symbol to be
/// triggered (see activeSymbols enum for values)
/// @param repeatNumber Repeat number that must be active for the symbol to be
/// triggered (0 = none)
/// @return True if the symbol data was set, false if not
bool Direction::SetSymbol(wxUint32 index, wxByte symbolType,
wxByte activeSymbol, wxByte repeatNumber)
{
//------Last Checked------//
// - Jan 11, 2005
wxCHECK(IsValidSymbolIndex(index), false);
wxCHECK(IsValidSymbolType(symbolType), false);
wxCHECK(IsValidActiveSymbol(activeSymbol), false);
wxCHECK(IsValidRepeatNumber(repeatNumber), false);
wxWord symbol = (wxWord)(symbolType << 8);
symbol |= (wxWord)(activeSymbol << 6);
symbol |= (wxWord)repeatNumber;
m_symbolArray[index] = symbol;
return (true);
}
/// Gets the symbol stored in the nth index of the symbol array
/// @param index Index of the symbol to get
/// @param symbolType Holds the symbol type return value
/// @param activeSymbol Holds the active symbol return value
/// @param repeatNumber Holds the repeat number return value
/// @return True if the direction data was retrieved, false if not
bool Direction::GetSymbol(wxUint32 index, wxByte& symbolType,
wxByte& activeSymbol, wxByte& repeatNumber) const
{
//------Last Checked------//
// - Jan 11, 2005
wxCHECK(IsValidSymbolIndex(index), false);
symbolType = activeSymbol = repeatNumber = 0;
symbolType = (wxByte)((m_symbolArray[index] & symbolTypeMask) >> 8);
activeSymbol = (wxByte)((m_symbolArray[index] & activeSymbolMask) >> 6);
repeatNumber = (wxByte)(m_symbolArray[index] & repeatNumberMask);
return (true);
}
/// Determines if a symbol in the symbol array is a given type
/// @param index Index of the symbol
/// @param symbolType Type of symbol to test against
/// @return True if the symbol is of the type, false if not
bool Direction::IsSymbolType(wxUint32 index, wxByte symbolType) const
{
//------Last Checked------//
// - Jan 11, 2005
wxCHECK(IsValidSymbolIndex(index), false);
wxCHECK(IsValidSymbolType(symbolType), false);
wxByte type = 0;
wxByte activeSymbol = 0;
wxByte repeatNumber = 0;
if (!GetSymbol(index, type, activeSymbol, repeatNumber))
return (false);
return (type == symbolType);
}
/// Removes a symbol from the symbol array
/// @param index Index of the symbol to remove
/// @return True if the symbol was removed, false if not
bool Direction::RemoveSymbolAtIndex(wxUint32 index)
{
//------Last Checked------//
// - Jan 11, 2005
wxCHECK(IsValidSymbolIndex(index), false);
m_symbolArray.RemoveAt(index);
return (true);
}
/// Deletes the contents (and frees the memory) of the symbol array
void Direction::DeleteSymbolArrayContents()
{
//------Last Checked------//
// - Jan 11, 2005
m_symbolArray.Clear();
}
/// Gets a text representation of a symbol
/// @param index Index of the symbol to get the text for
/// @return Text representation of the symbol
wxString Direction::GetText(wxUint32 index) const
{
//------Last Checked------//
// - Jan 11, 2005
wxByte symbolType = 0;
wxByte activeSymbol = 0;
wxByte repeatNumber = 0;
if (!GetSymbol(index, symbolType, activeSymbol, repeatNumber))
return (wxT(""));
wxCHECK(IsValidSymbolType(symbolType), wxT(""));
return (directionText[symbolType]);
}