refactor(drawtypes): Cleanup and fixes
This commit is contained in:
parent
a0f0fc8723
commit
d2187f44e0
17 changed files with 257 additions and 189 deletions
include/drawtypes
|
@ -3,14 +3,12 @@
|
|||
#include "common.hpp"
|
||||
#include "components/config.hpp"
|
||||
#include "drawtypes/label.hpp"
|
||||
#include "utils/math.hpp"
|
||||
#include "utils/mixins.hpp"
|
||||
|
||||
LEMONBUDDY_NS
|
||||
|
||||
namespace drawtypes {
|
||||
class ramp;
|
||||
using ramp_t = shared_ptr<ramp>;
|
||||
|
||||
class ramp : public non_copyable_mixin<ramp> {
|
||||
public:
|
||||
explicit ramp() = default;
|
||||
|
@ -20,28 +18,40 @@ namespace drawtypes {
|
|||
m_icons.emplace_back(forward<decltype(icon)>(icon));
|
||||
}
|
||||
|
||||
icon_t get(int index) {
|
||||
icon_t get(size_t index) {
|
||||
return m_icons[index];
|
||||
}
|
||||
|
||||
icon_t get_by_percentage(float percentage) {
|
||||
return m_icons[static_cast<int>(percentage * (m_icons.size() - 1) / 100.0f + 0.5f)];
|
||||
size_t index = percentage * (m_icons.size() - 1) / 100.0f + 0.5f;
|
||||
return m_icons[math_util::cap<size_t>(index, 0, m_icons.size() - 1)];
|
||||
}
|
||||
|
||||
operator bool() {
|
||||
return m_icons.size() > 0;
|
||||
return !m_icons.empty();
|
||||
}
|
||||
|
||||
protected:
|
||||
vector<icon_t> m_icons;
|
||||
};
|
||||
|
||||
inline auto get_config_ramp(
|
||||
const config& conf, string section, string name = "ramp", bool required = true) {
|
||||
vector<icon_t> vec;
|
||||
using ramp_t = shared_ptr<ramp>;
|
||||
|
||||
/**
|
||||
* Create a ramp by loading values
|
||||
* from the configuration
|
||||
*/
|
||||
inline auto load_ramp(const config& conf, string section, string name, bool required = true) {
|
||||
name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
|
||||
|
||||
icon_t ramp_defaults;
|
||||
|
||||
try {
|
||||
ramp_defaults = load_icon(conf, section, name);
|
||||
} catch (const key_error&) {
|
||||
}
|
||||
|
||||
vector<icon_t> vec;
|
||||
vector<string> icons;
|
||||
|
||||
if (required)
|
||||
|
@ -49,16 +59,16 @@ namespace drawtypes {
|
|||
else
|
||||
icons = conf.get_list<string>(section, name, {});
|
||||
|
||||
auto foreground = conf.get<string>(section, name + "-foreground", "");
|
||||
for (int i = 0; i < (int)icons.size(); i++) {
|
||||
auto ramp = name + "-" + to_string(i);
|
||||
auto icon = get_optional_config_icon(conf, section, ramp, icons[i]);
|
||||
if (icon->m_foreground.empty() && !foreground.empty())
|
||||
icon->m_foreground = foreground;
|
||||
vec.emplace_back(std::move(icon));
|
||||
for (size_t i = 0; i < icons.size(); i++) {
|
||||
auto icon = load_optional_icon(conf, section, name + "-" + to_string(i), icons[i]);
|
||||
|
||||
if (ramp_defaults)
|
||||
icon->copy_undefined(ramp_defaults);
|
||||
|
||||
vec.emplace_back(move(icon));
|
||||
}
|
||||
|
||||
return ramp_t{new ramp(std::move(vec))};
|
||||
return ramp_t{new ramp_t::element_type(move(vec))};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue