forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasterism.cpp
More file actions
174 lines (142 loc) · 4.33 KB
/
asterism.cpp
File metadata and controls
174 lines (142 loc) · 4.33 KB
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
// asterism.cpp
//
// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <celutil/gettext.h>
#include <celutil/debug.h>
#include <celutil/util.h>
#include "stardb.h"
#include "asterism.h"
#include "parser.h"
#include "tokenizer.h"
using namespace std;
Asterism::Asterism(string _name) :
name(_name)
{
#ifdef ENABLE_NLS
i18nName = dgettext("celestia_constellations", _name.c_str());
#endif
}
string Asterism::getName(bool i18n) const
{
#ifdef ENABLE_NLS
return i18n ? i18nName : name;
#else
return name;
#endif
}
int Asterism::getChainCount() const
{
return chains.size();
}
const Asterism::Chain& Asterism::getChain(int index) const
{
return *chains[index];
}
void Asterism::addChain(Asterism::Chain& chain)
{
chains.push_back(&chain);
}
/*! Return whether the constellation is visible.
*/
bool Asterism::getActive() const
{
return active;
}
/*! Set whether or not the constellation is visible.
*/
void Asterism::setActive(bool _active)
{
active = _active;
}
/*! Get the override color for this constellation.
*/
Color Asterism::getOverrideColor() const
{
return color;
}
/*! Set an override color for the constellation. If this method isn't
* called, the constellation is drawn in the render's default color
* for contellations. Calling unsetOverrideColor will remove the
* override color.
*/
void Asterism::setOverrideColor(const Color &c)
{
color = c;
useOverrideColor = true;
}
/*! Make this constellation appear in the default color (undoing any
* calls to setOverrideColor.
*/
void Asterism::unsetOverrideColor()
{
useOverrideColor = false;
}
/*! Return true if this constellation has a custom color, or false
* if it should be drawn in the default color.
*/
bool Asterism::isColorOverridden() const
{
return useOverrideColor;
}
AsterismList* ReadAsterismList(istream& in, const StarDatabase& stardb)
{
auto* asterisms = new AsterismList();
Tokenizer tokenizer(&in);
Parser parser(&tokenizer);
while (tokenizer.nextToken() != Tokenizer::TokenEnd)
{
if (tokenizer.getTokenType() != Tokenizer::TokenString)
{
DPRINTF(LOG_LEVEL_ERROR, "Error parsing asterism file.\n");
for_each(asterisms->begin(), asterisms->end(), deleteFunc<Asterism*>());
delete asterisms;
return nullptr;
}
string name = tokenizer.getStringValue();
Asterism* ast = new Asterism(name);
Value* chainsValue = parser.readValue();
if (chainsValue == nullptr || chainsValue->getType() != Value::ArrayType)
{
DPRINTF(LOG_LEVEL_ERROR, "Error parsing asterism %s\n", name.c_str());
for_each(asterisms->begin(), asterisms->end(), deleteFunc<Asterism*>());
delete ast;
delete asterisms;
delete chainsValue;
return nullptr;
}
Array* chains = chainsValue->getArray();
for (const auto chain : *chains)
{
if (chain->getType() == Value::ArrayType)
{
Array* a = chain->getArray();
// skip empty (without or only with a single star) chains
if (a->size() <= 1)
continue;
Asterism::Chain* new_chain = new Asterism::Chain();
for (const auto i : *a)
{
if (i->getType() == Value::StringType)
{
Star* star = stardb.find(i->getString(), false);
if (star == nullptr)
star = stardb.find(ReplaceGreekLetterAbbr(i->getString()), false);
if (star != nullptr)
new_chain->push_back(star->getPosition());
else
DPRINTF(LOG_LEVEL_ERROR, "Error loading star \"%s\" for asterism \"%s\".\n", name.c_str(), i->getString().c_str());
}
}
ast->addChain(*new_chain);
}
}
asterisms->push_back(ast);
delete chainsValue;
}
return asterisms;
}