Skip to content

Commit

Permalink
Text boxes for entering an internal name now check if the name is good.
Browse files Browse the repository at this point in the history
  • Loading branch information
Espyo committed Dec 15, 2024
1 parent d735d74 commit 280b06e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
3 changes: 1 addition & 2 deletions source/documents/todo.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Point I left on (so I can continue the next day without losing my train of thought)
Point I left on (so I can continue the next day without losing my train of thought)


Current tasks (tasks being worked on, but not yet committed)
Packs
Internal names should be sanitized wherever the player can insert them
When it's all done
Organize the performance monitor start/end points
Run a memory leak check
Expand Down
4 changes: 4 additions & 0 deletions source/source/game_states/animation_editor/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ void animation_editor::process_gui_new_dialog() {
if(new_dialog.type == 0) {
if(new_dialog.internal_name.empty()) {
new_dialog.problem = "You have to type an internal name first!";
} else if(!is_internal_name_good(new_dialog.internal_name)) {
new_dialog.problem =
"The internal name should only have lowercase letters,\n"
"numbers, and underscores!";
} else {
content_manifest temp_man;
temp_man.internal_name = new_dialog.internal_name;
Expand Down
4 changes: 4 additions & 0 deletions source/source/game_states/area_editor/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ void area_editor::process_gui_new_dialog() {
new_dialog.problem.clear();
if(new_dialog.internal_name.empty()) {
new_dialog.problem = "You have to type an internal name first!";
} else if(!is_internal_name_good(new_dialog.internal_name)) {
new_dialog.problem =
"The internal name should only have lowercase letters,\n"
"numbers, and underscores!";
} else {
content_manifest temp_man;
temp_man.pack = new_dialog.pack;
Expand Down
22 changes: 22 additions & 0 deletions source/source/game_states/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,24 @@ bool editor::input_popup(
}


/**
* @brief Returns whether a given internal name is good or not.
*
* @param name The internal name to check.
* @return Whether it's good.
*/
bool editor::is_internal_name_good(const string &name) const {
for(size_t c = 0; c < name.size(); c++) {
char ch = name[c];
const bool is_lowercase = ch >= 'a' && ch <= 'z';
const bool is_digit = ch >= '0' && ch <= '9';
const bool is_underscore = ch == '_';
if(!is_lowercase && !is_digit && !is_underscore) return false;
}
return true;
}


/**
* @brief Returns whether or not the pressed key corresponds to the specified
* key combination. Used for keyboard shortcuts.
Expand Down Expand Up @@ -1754,6 +1772,10 @@ void editor::process_gui_new_pack_dialog() {
string problem;
if(internal_name.empty()) {
problem = "You have to type an internal name first!";
} else if(!is_internal_name_good(internal_name)) {
problem =
"The internal name should only have lowercase letters,\n"
"numbers, and underscores!";
} else {
for(const auto &p : game.content.packs.manifests_with_base) {
if(internal_name == p) {
Expand Down
19 changes: 10 additions & 9 deletions source/source/game_states/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,22 +537,22 @@ class editor : public game_state {
//Callback for when the item is really meant to be picked, in the base
//content warning dialog.
std::function<void()> base_content_warning_do_pick_callback = nullptr;

//Currently-chosen bitmap name in the bitmap dialog.
string bitmap_dialog_cur_bmp_name;

//Currently-chosen bitmap pointer in the bitmap dialog.
ALLEGRO_BITMAP* bitmap_dialog_cur_bmp_ptr = nullptr;

//Name of the newly-chosen bitmap in the bitmap dialog.
string bitmap_dialog_new_bmp_name;

//Callback for when the user chooses a bitmap in the bitmap dialog.
std::function<void(const string&)> bitmap_dialog_ok_callback = nullptr;

std::function<void(const string &)> bitmap_dialog_ok_callback = nullptr;
//Picker for the bitmap dialog.
picker_info bitmap_dialog_picker = picker_info(this);

//Recommended folder in the bitmap dialog, if any. "." for graphics root.
string bitmap_dialog_recommended_folder;

Expand Down Expand Up @@ -681,6 +681,7 @@ class editor : public game_state {
bool input_popup(
const char* label, const char* prompt, string* text
);
bool is_internal_name_good(const string &name) const;
bool list_popup(
const char* label, const vector<string> &items, string* picked_item
);
Expand All @@ -690,8 +691,8 @@ class editor : public game_state {
const std::function<void()> &do_pick_callback
);
void open_bitmap_dialog(
std::function<void(const string&)> ok_callback,
const string& recommended_folder
std::function<void(const string &)> ok_callback,
const string &recommended_folder
);
void open_dialog(
const string &title,
Expand Down Expand Up @@ -724,7 +725,7 @@ class editor : public game_state {
);
void process_gui_message_dialog();
bool process_gui_mob_type_widgets(
string* custom_cat_name, mob_type** type, const string& pack_filter = ""
string* custom_cat_name, mob_type** type, const string &pack_filter = ""
);
bool process_gui_new_dialog_pack_widgets(string* pack);
void process_gui_new_pack_dialog();
Expand Down
8 changes: 6 additions & 2 deletions source/source/game_states/gui_editor/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void gui_editor::process_gui_new_dialog() {
//Pack widgets.
new_dialog.must_update |=
process_gui_new_dialog_pack_widgets(&new_dialog.pack);

//GUI definition combo.
vector<string> gui_files;
for(const auto &g : game.content.gui_defs.manifests) {
Expand All @@ -407,13 +407,17 @@ void gui_editor::process_gui_new_dialog() {
ImGui::Spacer();
new_dialog.must_update |=
ImGui::Combo("File", &new_dialog.internal_name, gui_files);

//Check if everything's ok.
if(new_dialog.must_update) {
new_dialog.problem.clear();
if(new_dialog.internal_name.empty()) {
new_dialog.problem =
"You have to select a file!";
} else if(!is_internal_name_good(new_dialog.internal_name)) {
new_dialog.problem =
"The internal name should only have lowercase letters,\n"
"numbers, and underscores!";
} else if(new_dialog.pack == FOLDER_NAMES::BASE_PACK) {
new_dialog.problem =
"All the GUI definition files already live in the\n"
Expand Down

0 comments on commit 280b06e

Please sign in to comment.