+11
-41
@@ -55,11 +55,7 @@ auto luma::asset_cache::texture(std::string const &path) -> std::expected<std::s
|
||||
assert(cached->second != nullptr);
|
||||
return modulus::success(cached->second);
|
||||
}
|
||||
if (!loading->result) {
|
||||
return modulus::fail<std::shared_ptr<luma::texture>>(loading->result.error());
|
||||
}
|
||||
assert(*loading->result != nullptr);
|
||||
return modulus::success(*loading->result);
|
||||
return loading->result;
|
||||
}
|
||||
loading = std::make_shared<luma::asset_cache::in_flight_load>();
|
||||
loading->owner = std::this_thread::get_id();
|
||||
@@ -75,49 +71,23 @@ auto luma::asset_cache::texture(std::string const &path) -> std::expected<std::s
|
||||
} catch (...) {
|
||||
loaded = luma::detail::fail<std::shared_ptr<luma::texture>>(luma::detail::error_internal, "texture load failed with an unknown exception");
|
||||
}
|
||||
if (loaded && !*loaded) {
|
||||
loaded = luma::detail::fail<std::shared_ptr<luma::texture>>(luma::detail::error_not_found, "texture load returned no texture: " + path);
|
||||
}
|
||||
if (!loaded) {
|
||||
modulus::log_debug("luma texture load failed: " + path);
|
||||
}
|
||||
{
|
||||
auto lock = std::scoped_lock{mutex_};
|
||||
loading->result = modulus::fail<std::shared_ptr<luma::texture>>(loaded.error());
|
||||
if (loaded && generation == generation_) {
|
||||
loaded = textures_.emplace(path, *loaded).first->second;
|
||||
}
|
||||
loading->result = loaded;
|
||||
loading->done = true;
|
||||
loading_.erase(path);
|
||||
loading->ready.notify_all();
|
||||
return loaded;
|
||||
}
|
||||
if (*loaded == nullptr) {
|
||||
modulus::log_debug("luma texture load returned null: " + path);
|
||||
auto result = luma::detail::fail<std::shared_ptr<luma::texture>>(luma::detail::error_not_found, "texture load returned no texture: " + path);
|
||||
auto lock = std::scoped_lock{mutex_};
|
||||
loading->result = modulus::fail<std::shared_ptr<luma::texture>>(result.error());
|
||||
loading->done = true;
|
||||
loading_.erase(path);
|
||||
loading->ready.notify_all();
|
||||
return result;
|
||||
}
|
||||
auto texture = *loaded;
|
||||
auto lock = std::scoped_lock{mutex_};
|
||||
if (auto found = textures_.find(path); found != textures_.end()) {
|
||||
assert(found->second != nullptr);
|
||||
loading->result = modulus::success(found->second);
|
||||
loading->done = true;
|
||||
loading_.erase(path);
|
||||
loading->ready.notify_all();
|
||||
return modulus::success(found->second);
|
||||
}
|
||||
if (generation != generation_) {
|
||||
loading->result = modulus::success(texture);
|
||||
loading->done = true;
|
||||
loading_.erase(path);
|
||||
loading->ready.notify_all();
|
||||
return modulus::success(std::move(texture));
|
||||
}
|
||||
auto inserted = textures_.emplace(path, std::move(texture));
|
||||
assert(inserted.first->second != nullptr);
|
||||
loading->result = modulus::success(inserted.first->second);
|
||||
loading->done = true;
|
||||
loading_.erase(path);
|
||||
loading->ready.notify_all();
|
||||
return modulus::success(inserted.first->second);
|
||||
return loaded;
|
||||
}
|
||||
|
||||
auto luma::asset_cache::clear() -> void {
|
||||
|
||||
+1
-1
@@ -323,7 +323,7 @@ auto luma::overlay_host::handle_touch(luma::touch_event const &event) -> std::bo
|
||||
return true;
|
||||
}
|
||||
if (has_cancel && first_point_inside(event, cancel_bounds(bounds()), luma::touch_phase::UP) != nullptr) {
|
||||
throw_if_failed(close_dialog(luma::dialog_result::CANCELLED));
|
||||
throw_if_failed(close_dialog(luma::dialog_result::CANCELED));
|
||||
return true;
|
||||
}
|
||||
if (dialog_->body && any_point_inside(event, dialog_body_bounds(bounds()))) {
|
||||
|
||||
+21
-42
@@ -4,7 +4,7 @@
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
@@ -60,7 +60,14 @@ auto apply_color(boost::json::object const &object, std::char_t const *name, lum
|
||||
return {};
|
||||
}
|
||||
|
||||
auto number_value(boost::json::value const &value) -> std::optional<std::float64_t>;
|
||||
auto apply_number(boost::json::object const &object, std::char_t const *name, std::float32_t &target) -> std::expected<void, modulus::error> {
|
||||
auto value = modulus::json_config::optional_float64_field(object, name, target, 0.0, std::numeric_limits<std::float32_t>::max(), json_options);
|
||||
if (!value) {
|
||||
return modulus::fail<void>(value.error());
|
||||
}
|
||||
target = static_cast<std::float32_t>(*value);
|
||||
return {};
|
||||
}
|
||||
|
||||
auto apply_text_style(boost::json::object const &object, std::char_t const *name, luma::text_style &target) -> std::expected<void, modulus::error> {
|
||||
assert(name != nullptr);
|
||||
@@ -78,41 +85,21 @@ auto apply_text_style(boost::json::object const &object, std::char_t const *name
|
||||
if (!unknown) {
|
||||
return modulus::fail<void>(unknown.error());
|
||||
}
|
||||
if (auto const *font = (*style)->if_contains("font")) {
|
||||
auto const *text = font->if_string();
|
||||
if (text == nullptr) {
|
||||
return luma::detail::fail<void>(luma::detail::error_invalid_argument, std::string{name} + ".font must be a string");
|
||||
}
|
||||
target.font = std::string{text->c_str()};
|
||||
auto font = modulus::json_config::optional_string_field(**style, "font", target.font, json_options);
|
||||
if (!font) {
|
||||
return modulus::fail<void>(font.error());
|
||||
}
|
||||
if (auto const *size = (*style)->if_contains("size")) {
|
||||
auto number = number_value(*size);
|
||||
if (!number || *number <= 0.0) {
|
||||
return luma::detail::fail<void>(luma::detail::error_invalid_argument, std::string{name} + ".size must be positive");
|
||||
}
|
||||
target.size = static_cast<std::float32_t>(*number);
|
||||
target.font = std::move(*font);
|
||||
if (auto status = apply_number(**style, "size", target.size); !status) {
|
||||
return status;
|
||||
}
|
||||
if (auto const *line_height = (*style)->if_contains("line_height")) {
|
||||
auto number = number_value(*line_height);
|
||||
if (!number || *number <= 0.0) {
|
||||
return luma::detail::fail<void>(luma::detail::error_invalid_argument, std::string{name} + ".line_height must be positive");
|
||||
}
|
||||
target.line_height = static_cast<std::float32_t>(*number);
|
||||
if (auto status = apply_number(**style, "line_height", target.line_height); !status) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
auto number_value(boost::json::value const &value) -> std::optional<std::float64_t> {
|
||||
if (auto const *number = value.if_double()) {
|
||||
return *number;
|
||||
}
|
||||
if (auto const *integer = value.if_int64()) {
|
||||
return static_cast<std::float64_t>(*integer);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
auto luma::default_theme() -> luma::theme {
|
||||
@@ -247,19 +234,11 @@ auto luma::parse_theme(boost::json::value const &value) -> std::expected<luma::t
|
||||
if (auto status = apply_text_style(**object, "title", parsed.title); !status) {
|
||||
return invalid_theme(std::string{status.error().message()});
|
||||
}
|
||||
if (auto const *radius = (*object)->if_contains("radius")) {
|
||||
auto number = number_value(*radius);
|
||||
if (!number || *number < 0.0) {
|
||||
return invalid_theme("theme.radius must be non-negative");
|
||||
}
|
||||
parsed.radius = static_cast<std::float32_t>(*number);
|
||||
if (auto status = apply_number(**object, "radius", parsed.radius); !status) {
|
||||
return std::unexpected(status.error());
|
||||
}
|
||||
if (auto const *spacing = (*object)->if_contains("spacing")) {
|
||||
auto number = number_value(*spacing);
|
||||
if (!number || *number < 0.0) {
|
||||
return invalid_theme("theme.spacing must be non-negative");
|
||||
}
|
||||
parsed.spacing = static_cast<std::float32_t>(*number);
|
||||
if (auto status = apply_number(**object, "spacing", parsed.spacing); !status) {
|
||||
return std::unexpected(status.error());
|
||||
}
|
||||
if (auto status = luma::validate(parsed); !status) {
|
||||
return invalid_theme(std::string{status.error().message()});
|
||||
|
||||
Reference in New Issue
Block a user