Skip to content

Commit 7b42827

Browse files
committed
color helper function
1 parent 07984d6 commit 7b42827

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

src/CustomAlert.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,7 @@ void customizeText(CCLabelBMFont* text, matjson::Value data) {
213213
}
214214

215215
if (data.contains("color")) {
216-
ccColor3B col = text->getColor();
217-
auto arr = data["color"].asArray();
218-
if (arr) {
219-
auto x = arr.unwrap();
220-
col = ccColor3B(x[0].asInt().unwrapOr(0), x[1].asInt().unwrapOr(0), x[2].asInt().unwrapOr(0));
221-
}
222-
text->setColor(col);
216+
text->setColor(getColor(data, "color", text->getColor()));
223217
}
224218

225219
if (data.contains("scale")) {
@@ -240,13 +234,7 @@ void customizeText(TextArea* text, matjson::Value data) {
240234
}
241235

242236
if (data.contains("color")) {
243-
ccColor3B col = ccWHITE;
244-
auto arr = data["color"].asArray();
245-
if (arr) {
246-
auto x = arr.unwrap();
247-
col = ccColor3B(x[0].asInt().unwrapOr(0), x[1].asInt().unwrapOr(0), x[2].asInt().unwrapOr(0));
248-
}
249-
text->colorAllLabels(col);
237+
text->setColor(getColor(data));
250238
}
251239

252240
if (data.contains("scale")) {

src/CustomTextbox.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,7 @@ void CustomTextbox::showTextbox(std::string id) {
153153
matjson::Value messageList = data["messages"].asArray().unwrap();
154154

155155
for (auto m : messageList) {
156-
ccColor3B col = ccWHITE;
157-
if (m.contains("nameColor")) {
158-
auto arr = m["nameColor"].asArray();
159-
if (arr) {
160-
auto x = arr.unwrap();
161-
col = ccColor3B(x[0].asInt().unwrapOr(0), x[1].asInt().unwrapOr(0), x[2].asInt().unwrapOr(0));
162-
}
163-
}
156+
ccColor3B col = getColor(m, "nameColor");
164157

165158
std::string content = getStr(m, "content", "");
166159
if (!content.empty()) {

src/utils.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ inline std::string getKey(matjson::Value data, char const* key, std::map<std::st
160160
else return fallback;
161161
}
162162

163+
inline ccColor3B getColor(matjson::Value data, char const* key = "color", ccColor3B fallback = ccWHITE) {
164+
ccColor3B col = fallback;
165+
auto arr = data[key].asArray();
166+
if (arr) {
167+
auto x = arr.unwrap();
168+
col.r = x[0].asInt().unwrapOr(0);
169+
col.g = x[1].asInt().unwrapOr(0);
170+
col.b = x[2].asInt().unwrapOr(0);
171+
}
172+
return col;
173+
}
174+
163175
// Gets a custom texture from both spritesheets and the resources folder
164176
inline CCSprite* getCustomTexture(char const* frame, char const* fallback, bool fallbackIsResource = false) {
165177
// Search spritesheets

0 commit comments

Comments
 (0)