Panda3D

Loading...
Searching...
No Matches
configVariableColor.I
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file configVariableColor.I
10 * @author rdb
11 * @date 2014-02-02
12 */
13
14/**
15 *
16 */
17INLINE ConfigVariableColor::
18ConfigVariableColor(const std::string &name) :
19 ConfigVariable(name, VT_color),
20 _local_modified(initial_invalid_cache()),
21 _cache(0, 0, 0, 1)
22{
23 _core->set_used();
24}
25
26/**
27 *
28 */
29INLINE ConfigVariableColor::
30ConfigVariableColor(const std::string &name, const LColor &default_value,
31 const std::string &description, int flags) :
32#ifdef PRC_SAVE_DESCRIPTIONS
33 ConfigVariable(name, ConfigVariableCore::VT_color, description, flags),
34#else
35 ConfigVariable(name, ConfigVariableCore::VT_color, std::string(), flags),
36#endif
37 _local_modified(initial_invalid_cache()),
38 _cache(0, 0, 0, 1)
39{
40 set_default_value(default_value);
41 _core->set_used();
42}
43
44/**
45 *
46 */
47INLINE ConfigVariableColor::
48ConfigVariableColor(const std::string &name, const std::string &default_value,
49 const std::string &description, int flags) :
50#ifdef PRC_SAVE_DESCRIPTIONS
51 ConfigVariable(name, ConfigVariableCore::VT_color, description, flags),
52#else
53 ConfigVariable(name, ConfigVariableCore::VT_color, std::string(), flags),
54#endif
55 _local_modified(initial_invalid_cache()),
56 _cache(0, 0, 0, 1)
57{
58 _core->set_default_value(default_value);
59 _core->set_used();
60}
61
62/**
63 * Reassigns the variable's local value.
64 */
65INLINE void ConfigVariableColor::
66 operator = (const LColor &value) {
67 set_value(value);
68}
69
70/**
71 * Returns the variable's value.
72 */
73INLINE ConfigVariableColor::
74operator const LColor & () const {
75 return get_value();
76}
77
78/**
79 * Returns the value of the color's nth component (which is not necessarily
80 * the same thing as the variable's nth word).
81 */
82INLINE PN_stdfloat ConfigVariableColor::
83 operator [] (int n) const {
84 return get_value()[n];
85}
86
87/**
88 * Reassigns the variable's local value.
89 */
90INLINE void ConfigVariableColor::
91 set_value(const LColor &color) {
92 set_string_value("");
93 set_double_word(0, color[0]);
94 set_double_word(1, color[1]);
95 set_double_word(2, color[2]);
96 set_double_word(3, color[3]);
97}
98
99/**
100 * Returns the variable's value.
101 */
102INLINE const LColor &ConfigVariableColor::
103 get_value() const {
104 TAU_PROFILE("const LColor &ConfigVariableColor::get_value() const", " ", TAU_USER);
105 if (!is_cache_valid(_local_modified)) {
106 mark_cache_valid(_local_modified);
107
108 switch (get_num_words()) {
109 case 1:
110 _cache.set((PN_stdfloat)get_double_word(0), (PN_stdfloat)get_double_word(0),
111 (PN_stdfloat)get_double_word(0), 1);
112 break;
113
114 case 2:
115 _cache.set((PN_stdfloat)get_double_word(0), (PN_stdfloat)get_double_word(0),
116 (PN_stdfloat)get_double_word(0), (PN_stdfloat)get_double_word(1));
117 break;
118
119 case 3:
120 _cache.set((PN_stdfloat)get_double_word(0), (PN_stdfloat)get_double_word(1),
121 (PN_stdfloat)get_double_word(2), 1);
122 break;
123
124 case 4:
125 _cache.set((PN_stdfloat)get_double_word(0), (PN_stdfloat)get_double_word(1),
126 (PN_stdfloat)get_double_word(2), (PN_stdfloat)get_double_word(3));
127 break;
128
129 default:
130 prc_cat->warning()
131 << "Invalid color value for ConfigVariable "
132 << get_name() << ": " << get_string_value() << "\n";
133 }
134 }
135 return _cache;
136}
137
138/**
139 * Returns the variable's default value.
140 */
141INLINE LColor ConfigVariableColor::
142 get_default_value() const {
143 const ConfigDeclaration *decl = ConfigVariable::get_default_value();
144 if (decl != nullptr) {
145 switch (decl->get_num_words()) {
146 case 1:
147 return LColor((PN_stdfloat)decl->get_double_word(0), (PN_stdfloat)decl->get_double_word(0),
148 (PN_stdfloat)decl->get_double_word(0), 1);
149 case 2:
150 return LColor((PN_stdfloat)decl->get_double_word(0), (PN_stdfloat)decl->get_double_word(0),
151 (PN_stdfloat)decl->get_double_word(0), (PN_stdfloat)decl->get_double_word(1));
152 case 3:
153 return LColor((PN_stdfloat)decl->get_double_word(0), (PN_stdfloat)decl->get_double_word(1),
154 (PN_stdfloat)decl->get_double_word(2), 1);
155 case 4:
156 return LColor((PN_stdfloat)decl->get_double_word(0), (PN_stdfloat)decl->get_double_word(1),
157 (PN_stdfloat)decl->get_double_word(2), (PN_stdfloat)decl->get_double_word(3));
158 default:
159 prc_cat->warning()
160 << "Invalid default color value for ConfigVariable "
161 << get_name() << ": " << decl->get_string_value() << "\n";
162 }
163 }
164 return LColor(0, 0, 0, 1);
165}
ConfigDeclaration
A single declaration of a config variable, typically defined as one line in a .prc file,...
Definition configDeclaration.h:34
ConfigDeclaration::get_double_word
double get_double_word(size_t n) const
Returns the integer value of the nth word of the declaration's value, or 0 if there is no nth value.
Definition configDeclaration.I:206
ConfigDeclaration::get_num_words
size_t get_num_words() const
Returns the number of words in the declaration's value.
Definition configDeclaration.I:72
ConfigDeclaration::get_string_value
const std::string & get_string_value() const
Returns the value assigned to this variable.
Definition configDeclaration.I:53
ConfigVariableBase::get_name
get_name
Returns the name of the variable.
Definition configVariableBase.h:62
ConfigVariableColor::operator=
void operator=(const LColor &value)
Reassigns the variable's local value.
Definition configVariableColor.I:66
ConfigVariableColor::set_value
void set_value(const LColor &value)
Reassigns the variable's local value.
Definition configVariableColor.I:91
ConfigVariableColor::get_default_value
LColor get_default_value() const
Returns the variable's default value.
Definition configVariableColor.I:142
ConfigVariableColor::operator[]
PN_stdfloat operator[](int n) const
Returns the value of the color's nth component (which is not necessarily the same thing as the variab...
Definition configVariableColor.I:83
ConfigVariableColor::get_value
const LColor & get_value() const
Returns the variable's value.
Definition configVariableColor.I:103
ConfigVariableCore
The internal definition of a ConfigVariable.
Definition configVariableCore.h:34
ConfigVariable
This is a generic, untyped ConfigVariable.
Definition configVariable.h:31
ConfigVariable::get_num_words
size_t get_num_words() const
Returns the number of words in the variable's value.
Definition configVariable.I:100
ConfigVariable::get_string_value
const std::string & get_string_value() const
Returns the toplevel value of the variable, formatted as a string.
Definition configVariable.I:68
ConfigVariable::set_string_value
void set_string_value(const std::string &value)
Changes the value assigned to this variable.
Definition configVariable.I:80
string
STL class.
std
STL namespace.