Skip to content

Commit c62b376

Browse files
committed
Exposed Color class
1 parent 5090186 commit c62b376

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

src/core/modules/keyvalues/keyvalues_wrap_python.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,22 @@ class KeyValuesExt
7171
{
7272
return pKeyValues->SaveToFile(filesystem, szFile);
7373
}
74+
75+
static Color GetColor(KeyValues* pKeyValues, const char* szKeyName, const Color &defaultColor = Color())
76+
{
77+
if (!pKeyValues->FindKey(szKeyName))
78+
return defaultColor;
79+
80+
return pKeyValues->GetColor(szKeyName);
81+
}
7482
};
7583

7684
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(find_key_overload, FindKey, 1, 2);
7785
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_int_overload, GetInt, 0, 2);
7886
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_uint64_overload, GetUint64, 0, 2);
7987
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_float_overload, GetFloat, 0, 2);
8088
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_string_overload, GetString, 0, 2);
89+
BOOST_PYTHON_FUNCTION_OVERLOADS(get_color_overload, KeyValuesExt::GetColor, 2, 3);
8190
BOOST_PYTHON_FUNCTION_OVERLOADS(get_bool_overload, KeyValuesExt::GetBool, 2, 3);
8291
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(is_empty_overload, IsEmpty, 0, 1);
8392

@@ -228,6 +237,14 @@ void export_keyvalues()
228237
)
229238
)
230239

240+
.def("get_color",
241+
&KeyValuesExt::GetColor,
242+
get_color_overload(
243+
"Returns the color value of the given key name.",
244+
args("key_name", "default_value")
245+
)
246+
)
247+
231248
.def("get_bool",
232249
&KeyValuesExt::GetBool,
233250
get_bool_overload(
@@ -268,6 +285,12 @@ void export_keyvalues()
268285
args("key_name", "value")
269286
)
270287

288+
.def("set_color",
289+
&KeyValues::SetColor,
290+
"Sets the given key's color value.",
291+
args("key_name", "value")
292+
)
293+
271294
.def("set_bool",
272295
&KeyValuesExt::SetBool,
273296
"Sets the given key's boolean value.",

src/core/modules/usermessage/usermessage_wrap_python.cpp

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
// Includes.
2929
//-----------------------------------------------------------------------------
3030
#include "modules/export_main.h"
31+
#include "utility/sp_util.h"
3132
#include "usermessage.h"
33+
#include "Color.h"
3234

3335
//-----------------------------------------------------------------------------
3436
// Namespaces to use
@@ -41,6 +43,7 @@ using namespace boost::python;
4143
void export_usermessage_interface();
4244
void export_message_functions();
4345
void export_dialog_enum();
46+
void export_color();
4447

4548
//-----------------------------------------------------------------------------
4649
// Method overloads
@@ -62,6 +65,7 @@ DECLARE_SP_MODULE(usermessage_c)
6265
export_usermessage_interface();
6366
export_message_functions();
6467
export_dialog_enum();
68+
export_color();
6569
}
6670

6771
void export_usermessage_interface()
@@ -144,8 +148,7 @@ void export_usermessage_interface()
144148
"Sets a field parameter to the specified value.",
145149
args("field_name", "field_value", "index")
146150
)
147-
148-
BOOST_END_CLASS()
151+
;
149152
}
150153

151154
void export_message_functions()
@@ -159,10 +162,65 @@ void export_message_functions()
159162
void export_dialog_enum()
160163
{
161164
enum_<DIALOG_TYPE>("DialogType")
162-
ENUM_VALUE("MSG", DIALOG_MSG)
163-
ENUM_VALUE("MENU", DIALOG_MENU)
164-
ENUM_VALUE("TEXT", DIALOG_TEXT)
165-
ENUM_VALUE("ENTRY", DIALOG_ENTRY)
166-
ENUM_VALUE("ASKCONNECT", DIALOG_ASKCONNECT)
167-
BOOST_END_CLASS()
165+
.value("MSG", DIALOG_MSG)
166+
.value("MENU", DIALOG_MENU)
167+
.value("TEXT", DIALOG_TEXT)
168+
.value("ENTRY", DIALOG_ENTRY)
169+
.value("ASKCONNECT", DIALOG_ASKCONNECT)
170+
;
168171
}
172+
173+
//---------------------------------------------------------------------------------
174+
// Exposes the Color class
175+
//---------------------------------------------------------------------------------
176+
class ColorExt
177+
{
178+
public:
179+
static tuple GetColor(Color* pColor)
180+
{
181+
list color;
182+
int r, g, b, a;
183+
pColor->GetColor(r, g, b, a);
184+
color.append(r);
185+
color.append(g);
186+
color.append(b);
187+
color.append(a);
188+
return tuple(color);
189+
}
190+
};
191+
192+
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(set_color_overload, SetColor, 3, 4);
193+
194+
void export_color()
195+
{
196+
class_<Color>("Color")
197+
.def(init<int, int, int>())
198+
.def(init<int, int, int, int>())
199+
200+
.def("set_color",
201+
&Color::SetColor,
202+
set_color_overload(
203+
"Sets the color in a RGB(A) format (0-255).",
204+
args("r", "g", "b", "a")
205+
)
206+
)
207+
208+
.def("get_color",
209+
&ColorExt::GetColor,
210+
"Returns the color as a tuple containing the RGBA values."
211+
)
212+
213+
.def("__getitem__",
214+
&GetItemIndexer<Color, unsigned char>,
215+
"Returns the color at the given index (0-3)."
216+
)
217+
218+
.def("__setitem__",
219+
&SetItemIndexer<Color, unsigned char>,
220+
"Sets the color at the given index (0-3)."
221+
)
222+
223+
.def(self == self)
224+
.def(self != self)
225+
;
226+
}

0 commit comments

Comments
 (0)