Skip to content

Commit f89505b

Browse files
committed
v1.0.3 - misc changes
1 parent c7a1806 commit f89505b

File tree

10 files changed

+61
-35
lines changed

10 files changed

+61
-35
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Default keybind is HOME
103103
**Button properties** can be a string, OR an object with any of:
104104
- `content` (string or Text) - The message on the button
105105
- `background` (string) - Button color
106-
- green, cyan, pink, grey, darkgrey, red, geodeblue, geodepink, geodepurple, or trans
106+
- green, cyan, pink, grey, darkgrey, red, geodeblue, geodepink, geodepurple, geodeblack or trans
107107
- Uses GJ_button_0#.png texture in resources folder
108108
- `texture` ([filename](#texture-files-can-be-read-from)) - Custom button texture, overrides background
109109
- Use a name in the resources folder, e.g. "GJ_button_03.png"
@@ -417,11 +417,13 @@ Here's an example of a popup that plays a looping, rising tone:
417417
## Extra resources
418418

419419
### Sound object
420-
The "sound" and "playSound" properties support a string filename such as "explode_11.ogg", or an object with:
420+
The "sound", "playSound", and other sound-related properties support a string filename such as "explode_11.ogg", or an object with:
421421
- `name` ([filename](#sound-files-can-be-read-from)) - Sound filename, must be in resources or a texture pack folder
422422
- `volume` (float) - Volume to play at, default is 1
423423
- `speed` (float) - Speed adjustment, in semitones. Default is 0 and a full octave (200% speed) is 12
424424

425+
Additionally, you can provide an array with multiple sound objects and it will choose one randomly.
426+
425427
### Dialogue Portrait Strings
426428
If you don't want to find portraits by their ID, you can enter these strings instead. I don't recommend it, but it works if you're lazy or just want to make something quick.
427429

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 1.0.3
2+
- `Sound` object now supports an array of sounds to randomly choose from
3+
- Improved error popup (now shows location of JSON errors)
4+
- Added "geodeblack" button color
5+
- SHIFT+ESC keybind now also cancels delayed callbacks
6+
- Fixed crash when `width` property of popups is too low
7+
18
# 1.0.2
29
- Added links, tags, and previews to mod
310

mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"id": "colon.customtextboxes",
88
"name": "Custom Textboxes",
9-
"version": "v1.0.2",
9+
"version": "v1.0.3",
1010
"developer": "Colon",
1111
"description": "Display custom popups, chests, and more",
1212
"resources": {

src/Callback.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class CallerBackerInator {
5454
if (delay <= 0) return handleCallback(data, forced);
5555

5656
auto timer = CCNode::create();
57+
timer->setID("custom_callback_timer"_spr);
5758
CCScene::get()->addChild(timer);
5859

5960
timer->runAction(

src/CustomAlert.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@
55

66
void CustomAlert::showPopup(std::string id)
77
{
8-
auto rawJSON = readJSON(CustomAlert::jsonFilePath);
9-
if (rawJSON == nullptr) return FLAlertLayer::create("Custom Textboxes", "Error loading <cy>custom alert</c> JSON! Maybe it has errors?", "OK")->show();
10-
11-
if (!rawJSON.contains(id)) {
12-
if (Mod::get()->getSettingValue<bool>("missingIDWarn")) FLAlertLayer::create("Custom Textboxes", fmt::format("Popup ID not found!\nID: <cy>{}</c> ", id), "OK")->show();
13-
return;
14-
};
15-
16-
matjson::Value data = rawJSON[id];
8+
auto data = getTextboxByID(CustomAlert::jsonFilePath, CustomAlert::jsonFileName, id, "Popup");
9+
if (data == nullptr) return;
1710

1811
std::string alertType = getStr(data, "type", "alert");
1912

@@ -160,13 +153,16 @@ void CustomAlert::showPopup(std::string id)
160153

161154
// Normal popup
162155
else {
156+
auto width = getNum(data, "width", 350.0f);
157+
if (width < 100) width = 100.0f;
158+
163159
FLAlertLayer* popup = FLAlertLayer::create(
164160
nullptr,
165161
title.c_str(),
166162
hasContent ? getText(content, "").c_str() : "",
167163
hasButton ? getText(button1, "OK").c_str() : "OK",
168164
data.contains("button2") ? getText(data["button2"], "Cancel").c_str() : nullptr,
169-
getNum(data, "width", 350.0f)
165+
width
170166
);
171167
popup->setID("custom_alertpopup"_spr);
172168
popup->show();

src/CustomAlert.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@ inline std::map<std::string, std::string> EXTRA_BUTTON_COLORS = {
5858
{ "geodeblue", "geode.loader/GE_button_01.png" },
5959
{ "geodepink", "geode.loader/GE_button_02.png" },
6060
{ "geodepurple", "geode.loader/GE_button_03.png" },
61+
{ "geodeblack", "geode.loader/GE_button_05.png" },
6162
{ "trans", "geode.loader/GE_button_04.png" }
6263
};

src/CustomChest.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,8 @@ class $modify(MyRUL, RewardUnlockLayer) {
2323

2424
void CustomChest::showChest(std::string id) {
2525

26-
auto rawJSON = readJSON(CustomChest::jsonFilePath);
27-
if (rawJSON == nullptr) return FLAlertLayer::create("Custom Textboxes", "Error loading <cy>custom chest</c> JSON! Maybe it has errors?", "OK")->show();
28-
29-
if (!rawJSON.contains(id)) {
30-
if (Mod::get()->getSettingValue<bool>("missingIDWarn")) FLAlertLayer::create("Custom Textboxes", fmt::format("Chest ID not found!\nID: <cy>{}</c> ", id), "OK")->show();
31-
return;
32-
};
33-
34-
matjson::Value data = rawJSON[id];
26+
auto data = getTextboxByID(CustomChest::jsonFilePath, CustomChest::jsonFileName, id, "Chest");
27+
if (data == nullptr) return;
3528

3629
int chestType = getKey(data, "type", CHEST_TYPES, 1);
3730

src/CustomTextbox.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct CustomDialogData {
1919
};
2020
std::map<int, CustomDialogData> storedCustomData;
2121

22-
SoundData blipSound;
22+
matjson::Value blipSound;
2323
bool activeBlipSound = false;
2424

2525
void dialogCallback(matjson::Value data, bool forced = false) {
@@ -58,7 +58,7 @@ class $modify(CustomDialogLayer, DialogLayer) {
5858

5959
// Blip sound
6060
if (data.blipSound != nullptr) {
61-
blipSound = getCustomSound(data.blipSound);
61+
blipSound = data.blipSound;
6262
activeBlipSound = true;
6363
}
6464

@@ -130,15 +130,9 @@ class $modify(CCFadeIn) {
130130
};
131131

132132
void CustomTextbox::showTextbox(std::string id) {
133-
auto rawJSON = readJSON(CustomTextbox::jsonFilePath);
134-
if (rawJSON == nullptr) return FLAlertLayer::create("Custom Textboxes", "Error loading <cy>custom textbox</c> JSON! Maybe it has errors?", "OK")->show();
133+
auto data = getTextboxByID(CustomTextbox::jsonFilePath, CustomTextbox::jsonFileName, id, "Dialogue");
134+
if (data == nullptr) return;
135135

136-
if (!rawJSON.contains(id)) {
137-
if (Mod::get()->getSettingValue<bool>("missingIDWarn")) FLAlertLayer::create("Custom Textboxes", fmt::format("Dialogue ID not found!\nID: <cy>{}</c> ", id), "OK")->show();
138-
return;
139-
};
140-
141-
matjson::Value data = rawJSON[id];
142136
bool pausesMusic = false;
143137

144138
auto dialogLines = CCArray::create();

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ std::string getTargetID(std::string path, bool alt) {
4848
checkMissingFiles();
4949
}
5050

51-
auto rawJSON = readJSON(path);
51+
std::string err = "";
52+
auto rawJSON = readJSON(path, err);
5253
if (rawJSON == nullptr) return "";
5354

5455
auto k = alt ? altShowKey : showKey;

src/utils.hpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <Geode/Geode.hpp>
33
#include <matjson.hpp>
4+
#include <regex>
45

56
#include "CustomAlert.hpp"
67
#include "CustomChest.hpp"
@@ -102,16 +103,37 @@ inline std::map<std::string, int> ITEM_TYPES = {
102103
{ "spiderAnimationFast", 19 },
103104
};
104105

105-
inline matjson::Value readJSON(std::filesystem::path path) {
106+
inline std::regex errorPattern(R"(>:(\d+):(\d+)$)");
107+
108+
inline matjson::Value readJSON(std::filesystem::path path, std::string& err) {
106109
std::ifstream jsonFile(path);
107110
auto parsed = matjson::parse(jsonFile);
108111
if (!parsed) {
109-
geode::log::error("{}", parsed.unwrapErr());
112+
err = parsed.unwrapErr();
113+
geode::log::error("{}", err);
110114
return nullptr;
111115
}
112116
else return parsed.unwrap();
113117
}
114118

119+
// Read JSON or display an error popup
120+
inline matjson::Value getTextboxByID(std::filesystem::path path, std::filesystem::path shortPath, std::string id, std::string typeStr) {
121+
std::string err;
122+
auto rawJSON = readJSON(path, err);
123+
if (rawJSON == nullptr) {
124+
err = std::regex_replace(err, errorPattern, "line $1, position $2");
125+
FLAlertLayer::create("Custom Textboxes", fmt::format("Failed to load <cy>{}</c>\n\nError: <cr>{}</c>", shortPath, err), "OK")->show();
126+
return nullptr;
127+
}
128+
129+
if (!rawJSON.contains(id)) {
130+
if (Mod::get()->getSettingValue<bool>("missingIDWarn")) FLAlertLayer::create("Custom Textboxes", fmt::format("{} ID not found!\nID: <cy>{}</c> ", typeStr, id), "OK")->show();
131+
return nullptr;
132+
};
133+
134+
return rawJSON[id];
135+
}
136+
115137
inline std::string getStr(matjson::Value data, char const* key, char const* fallback) {
116138
if (!data.contains(key)) return fallback;
117139
else return data[key].asString().unwrapOr(fallback);
@@ -247,6 +269,15 @@ inline IconData getIcon(matjson::Value data, char const* key) {
247269
}
248270

249271
inline SoundData getCustomSound(matjson::Value data) {
272+
if (data.isArray()) {
273+
auto arr = data.asArray().unwrap();
274+
if (arr.size() == 1) return getCustomSound(arr[0]);
275+
else if (arr.size() > 1) {
276+
auto val = arr[rand() % arr.size()];
277+
if (val != nullptr && !val.isArray()) return getCustomSound(val);
278+
}
279+
}
280+
250281
float vol = 1.0f;
251282
float speed = 0.0f;
252283
std::string sound;

0 commit comments

Comments
 (0)