Skip to content

Commit

Permalink
Extract item_process_music_help into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
James De Ricco committed Jul 19, 2024
1 parent 212d0a9 commit abecc37
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 42 deletions.
44 changes: 2 additions & 42 deletions src/editor/object_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
#include <assert.h>
#include <sexp/value.hpp>

#include "audio/sound_file.hpp"
#include "fmt/format.h"
#include "util/file_system.hpp"
#include "gui/menu_filesystem_item_processor_music_help.hpp"
#include "util/gettext.hpp"
#include "video/color.hpp"

Expand Down Expand Up @@ -298,45 +296,7 @@ ObjectSettings::add_music(const std::string& text, std::string* value_ptr,
std::optional<std::string> default_value,
unsigned int flags)
{
auto item_processor = [](MenuItem& item, const std::string& file_path, bool in_basedir)
{
std::unique_ptr<SoundFile> sound_file;
try {
sound_file = load_sound_file(file_path);
} catch (...) {
item.set_help("");
return;
}

const std::vector<std::string>& authors = sound_file->m_authors;
const std::string& license = sound_file->m_license;
const std::string& title = sound_file->m_title;

if (title.empty() && authors.empty() && license.empty()) {
item.set_help("");
return;
}

const std::string filename = FileSystem::basename(file_path);
const std::string title_or_filename_line = title.empty() ? filename : "\"" + title + "\""; // assumes path is just a filename

std::string author_lines = "";

for (const std::string& author : authors) {
author_lines.append("\n" + fmt::format(fmt::runtime(_("Author") +": {}"), author));
}

const std::string license_line = fmt::format(fmt::runtime(_("License") + ": {}"), license);

const std::string help_text =
title_or_filename_line
+ author_lines
+ (license.empty() ? "" : "\n" + license_line);

item.set_help(help_text);
};

add_file(text, value_ptr, key, std::move(default_value), {".music"}, {"/music"}, false, flags, item_processor);
add_file(text, value_ptr, key, std::move(default_value), {".music"}, {"/music"}, false, flags, item_processor_music_help);
}

void
Expand Down
44 changes: 44 additions & 0 deletions src/gui/menu_filesystem_item_processor_music_help.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "gui/menu_filesystem_item_processor_music_help.hpp"

#include "fmt/format.h"
#include "util/file_system.hpp"
#include "util/gettext.hpp"

void item_processor_music_help(MenuItem& menu_item, const std::string& file_path, bool in_basedir) {
std::unique_ptr<SoundFile> sound_file;
try {
sound_file = load_sound_file(file_path);
} catch (...) {
menu_item.set_help("");
return;
}

const std::vector<std::string>& authors = sound_file->m_authors;
const std::string& license = sound_file->m_license;
const std::string& title = sound_file->m_title;

if (title.empty() && authors.empty() && license.empty()) {
menu_item.set_help("");
return;
}

const std::string filename = FileSystem::basename(file_path);
const std::string title_or_filename_line = title.empty() ? filename : "\"" + title + "\""; // assumes path is just a filename

std::string author_lines = "";

for (const std::string& author : authors) {
author_lines.append("\n" + fmt::format(fmt::runtime(_("Author") +": {}"), author));
}

const std::string license_line = fmt::format(fmt::runtime(_("License") + ": {}"), license);

const std::string help_text =
title_or_filename_line
+ author_lines
+ (license.empty() ? "" : "\n" + license_line);

menu_item.set_help(help_text);
}

/* EOF */
11 changes: 11 additions & 0 deletions src/gui/menu_filesystem_item_processor_music_help.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef HEADER_SUPERTUX_GUI_MENU_FILESYSTEM_ITEM_PROCESSOR_MUSIC_HELP_HPP
#define HEADER_SUPERTUX_GUI_MENU_FILESYSTEM_ITEM_PROCESSOR_MUSIC_HELP_HPP

#include "audio/sound_file.hpp"
#include "gui/menu_item.hpp"

void item_processor_music_help(MenuItem& menu_item, const std::string& file_path, bool in_basedir);

#endif

/* EOF */

0 comments on commit abecc37

Please sign in to comment.