Skip to content

Commit d07d321

Browse files
authored
New add-on menus, descriptions and screenshots (#2222)
* Addon description + addon preview menu * Fix "No language packs installed" not showing up * Fix clang compiler errors * Add a seperate addon browse menu to list addons * Fix clang compiler errors * Change color of addon description * List updates with installed addons, seperately * Preview installed addons + allow uninstalling * Sorted addon listing + fixes * Fixes to uninstall dialog * Code improvements * Download screenshot previews for addons * Various fixes * Visualize addon screenshot previews * Confirm uninstalling addon + miscellaneous fixes * No passive dialogs for auto installs + final fixes
1 parent 442a0e2 commit d07d321

23 files changed

+1159
-305
lines changed

src/addon/addon.cpp

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717

1818
#include "addon/addon.hpp"
1919

20+
#include <boost/optional.hpp>
21+
#include <fmt/format.h>
2022
#include <sstream>
2123

24+
#include "util/gettext.hpp"
2225
#include "util/reader.hpp"
2326
#include "util/reader_document.hpp"
27+
#include "util/reader_collection.hpp"
2428
#include "util/reader_mapping.hpp"
2529

26-
namespace {
27-
2830
static const char* s_allowed_characters = "-_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2931

32+
namespace addon_string_util {
33+
3034
Addon::Type addon_type_from_string(const std::string& type)
3135
{
3236
if (type == "world")
@@ -55,7 +59,50 @@ Addon::Type addon_type_from_string(const std::string& type)
5559
}
5660
}
5761

58-
} // namespace
62+
std::string addon_type_to_translated_string(Addon::Type type)
63+
{
64+
switch (type)
65+
{
66+
case Addon::LEVELSET:
67+
return _("Levelset");
68+
69+
case Addon::WORLDMAP:
70+
return _("Worldmap");
71+
72+
case Addon::WORLD:
73+
return _("World");
74+
75+
case Addon::ADDON:
76+
return _("Add-on");
77+
78+
case Addon::LANGUAGEPACK:
79+
return _("Language Pack");
80+
81+
default:
82+
return _("Unknown");
83+
}
84+
}
85+
86+
std::string generate_menu_item_text(const Addon& addon)
87+
{
88+
std::string text;
89+
std::string type = addon_type_to_translated_string(addon.get_type());
90+
91+
if (!addon.get_author().empty())
92+
{
93+
text = fmt::format(fmt::runtime(_("{} \"{}\" by \"{}\"")),
94+
type, addon.get_title(), addon.get_author());
95+
}
96+
else
97+
{
98+
// Only addon type and name, no need for translation.
99+
text = fmt::format("{} \"{}\"", type, addon.get_title());
100+
}
101+
102+
return text;
103+
}
104+
105+
} // namespace addon_string_util
59106

60107
std::unique_ptr<Addon>
61108
Addon::parse(const ReaderMapping& mapping)
@@ -83,14 +130,26 @@ Addon::parse(const ReaderMapping& mapping)
83130

84131
std::string type;
85132
mapping.get("type", type);
86-
addon->m_type = addon_type_from_string(type);
133+
addon->m_type = addon_string_util::addon_type_from_string(type);
87134

88135
mapping.get("title", addon->m_title);
89136
mapping.get("author", addon->m_author);
90137
mapping.get("license", addon->m_license);
138+
mapping.get("description", addon->m_description);
91139
mapping.get("url", addon->m_url);
92140
mapping.get("md5", addon->m_md5);
93141
mapping.get("format", addon->m_format);
142+
boost::optional<ReaderCollection> reader;
143+
if (mapping.get("screenshots", reader))
144+
{
145+
for (auto& obj : reader->get_objects())
146+
{
147+
std::string url;
148+
auto data = obj.get_mapping();
149+
data.get("url", url);
150+
addon->m_screenshots.push_back(url);
151+
}
152+
}
94153

95154
return addon;
96155
}
@@ -135,8 +194,10 @@ Addon::Addon() :
135194
m_author(),
136195
m_license(),
137196
m_format(0),
197+
m_description(),
138198
m_url(),
139199
m_md5(),
200+
m_screenshots(),
140201
m_install_filename(),
141202
m_enabled(false)
142203
{}
@@ -165,6 +226,12 @@ Addon::is_enabled() const
165226
return m_enabled;
166227
}
167228

229+
bool
230+
Addon::is_visible() const
231+
{
232+
return true;
233+
}
234+
168235
void
169236
Addon::set_install_filename(const std::string& absolute_filename, const std::string& md5)
170237
{

src/addon/addon.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define HEADER_SUPERTUX_ADDON_ADDON_HPP
2020

2121
#include <memory>
22+
#include <vector>
2223
#include <string>
2324

2425
class ReaderMapping;
@@ -47,8 +48,10 @@ class Addon final
4748
int m_format;
4849

4950
// additional fields provided for addons from an addon repository
51+
std::string m_description;
5052
std::string m_url;
5153
std::string m_md5;
54+
std::vector<std::string> m_screenshots;
5255

5356
// fields filled by the AddonManager
5457
std::string m_install_filename;
@@ -67,14 +70,17 @@ class Addon final
6770
std::string get_author() const { return m_author; }
6871
std::string get_license() const { return m_license; }
6972

73+
std::string get_description() const { return m_description; }
7074
std::string get_url() const { return m_url; }
7175
std::string get_md5() const { return m_md5; }
76+
std::vector<std::string> get_screenshots() const { return m_screenshots; }
7277

7378
std::string get_filename() const;
7479
std::string get_install_filename() const;
7580

7681
bool is_installed() const;
7782
bool is_enabled() const;
83+
bool is_visible() const;
7884

7985
void set_install_filename(const std::string& absolute_filename, const std::string& md5);
8086
void set_enabled(bool v);
@@ -84,6 +90,12 @@ class Addon final
8490
Addon& operator=(const Addon&) = delete;
8591
};
8692

93+
namespace addon_string_util {
94+
Addon::Type addon_type_from_string(const std::string& type);
95+
std::string addon_type_to_translated_string(Addon::Type type);
96+
std::string generate_menu_item_text(const Addon& addon);
97+
}
98+
8799
#endif
88100

89101
/* EOF */

0 commit comments

Comments
 (0)