More improvements to directives

Error logging is back where it should be
This commit is contained in:
Kae 2023-06-25 14:00:20 +10:00
parent 7783fc7310
commit 25b021c0cb
10 changed files with 52 additions and 7 deletions

View File

@ -862,7 +862,10 @@ shared_ptr<Assets::AssetData> Assets::loadImage(AssetPath const& path) const {
auto newData = make_shared<ImageData>(); auto newData = make_shared<ImageData>();
Image newImage = *source->image; Image newImage = *source->image;
path.directives.forEach([&](auto const& entry) { path.directives.forEach([&](auto const& entry) {
processImageOperation(entry.operation, newImage, [&](String const& ref) { return references.get(ref).get(); }); if (auto error = entry.operation.ptr<ErrorImageOperation>())
std::rethrow_exception(error->exception);
else
processImageOperation(entry.operation, newImage, [&](String const& ref) { return references.get(ref).get(); });
}); });
newData->image = make_shared<Image>(move(newImage)); newData->image = make_shared<Image>(move(newImage));
return newData; return newData;

View File

@ -56,7 +56,7 @@ void Directives::parse(String const& directives) {
ImageOperation operation = imageOperationFromString(str); ImageOperation operation = imageOperationFromString(str);
newList.emplace_back(move(operation), move(str)); newList.emplace_back(move(operation), move(str));
} catch (StarException const& e) { } catch (StarException const& e) {
Logger::logf(LogLevel::Error, "Error parsing image operation: %s", e.what()); newList.emplace_back(ErrorImageOperation{ std::current_exception() }, move(str));
} }
} }
} }
@ -214,7 +214,10 @@ inline Image DirectivesGroup::applyNewImage(Image const& image) const {
void DirectivesGroup::applyExistingImage(Image& image) const { void DirectivesGroup::applyExistingImage(Image& image) const {
forEach([&](auto const& entry) { forEach([&](auto const& entry) {
processImageOperation(entry.operation, image); if (auto error = entry.operation.ptr<ErrorImageOperation>())
std::rethrow_exception(error->exception);
else
processImageOperation(entry.operation, image);
}); });
} }

View File

@ -18,6 +18,10 @@ Image scaleBicubic(Image const& srcImage, Vec2F const& scale);
StringList colorDirectivesFromConfig(JsonArray const& directives); StringList colorDirectivesFromConfig(JsonArray const& directives);
String paletteSwapDirectivesFromConfig(Json const& swaps); String paletteSwapDirectivesFromConfig(Json const& swaps);
struct ErrorImageOperation {
std::exception_ptr exception;
};
struct HueShiftImageOperation { struct HueShiftImageOperation {
// Specify hue shift angle as -360 to 360 rather than -1 to 1 // Specify hue shift angle as -360 to 360 rather than -1 to 1
static HueShiftImageOperation hueShiftDegrees(float degrees); static HueShiftImageOperation hueShiftDegrees(float degrees);
@ -129,7 +133,7 @@ struct FlipImageOperation {
Mode mode; Mode mode;
}; };
typedef Variant<HueShiftImageOperation, SaturationShiftImageOperation, BrightnessMultiplyImageOperation, FadeToColorImageOperation, typedef Variant<ErrorImageOperation, HueShiftImageOperation, SaturationShiftImageOperation, BrightnessMultiplyImageOperation, FadeToColorImageOperation,
ScanLinesImageOperation, SetColorImageOperation, ColorReplaceImageOperation, AlphaMaskImageOperation, BlendImageOperation, ScanLinesImageOperation, SetColorImageOperation, ColorReplaceImageOperation, AlphaMaskImageOperation, BlendImageOperation,
MultiplyImageOperation, BorderImageOperation, ScaleImageOperation, CropImageOperation, FlipImageOperation> ImageOperation; MultiplyImageOperation, BorderImageOperation, ScaleImageOperation, CropImageOperation, FlipImageOperation> ImageOperation;

View File

@ -183,6 +183,8 @@ Vec2U ImageMetadataDatabase::calculateImageSize(AssetPath const& path) const {
OperationSizeAdjust(Vec2U& size) : imageSize(size), hasError(false) {}; OperationSizeAdjust(Vec2U& size) : imageSize(size), hasError(false) {};
void operator()(ErrorImageOperation const&) {}
void operator()(HueShiftImageOperation const&) {} void operator()(HueShiftImageOperation const&) {}
void operator()(SaturationShiftImageOperation const&) {} void operator()(SaturationShiftImageOperation const&) {}

View File

@ -145,7 +145,19 @@ void Object::init(World* world, EntityId entityId, EntityMode mode) {
if (isMaster()) { if (isMaster()) {
auto colorName = configValue("color", "default").toString(); auto colorName = configValue("color", "default").toString();
setImageKey("color", colorName); auto colorEnd = colorName.find('?');
if (colorEnd != NPos) {
setImageKey("color", colorName);
m_colorDirectives = colorName.substr(colorEnd);
}
else
m_colorDirectives = "";
m_directives = "";
if (auto directives = configValue("")) {
if (directives.isType(Json::Type::String))
m_directives.parse(directives.toString());
}
if (m_config->lightColors.contains(colorName)) if (m_config->lightColors.contains(colorName))
m_lightSourceColor.set(m_config->lightColors.get(colorName)); m_lightSourceColor.set(m_config->lightColors.get(colorName));
@ -1205,10 +1217,15 @@ List<Drawable> Object::orientationDrawables(size_t orientationIndex) const {
m_orientationDrawablesCache = make_pair(orientationIndex, List<Drawable>()); m_orientationDrawablesCache = make_pair(orientationIndex, List<Drawable>());
for (auto const& layer : orientation->imageLayers) { for (auto const& layer : orientation->imageLayers) {
Drawable drawable = layer; Drawable drawable = layer;
auto& image = drawable.imagePart().image; auto& imagePart = drawable.imagePart();
image = AssetPath::join(image).replaceTags(m_imageKeys, true, "default"); imagePart.image.directives.clear();
imagePart.image = AssetPath::join(imagePart.image).replaceTags(m_imageKeys, true, "default");
imagePart.image.directives = layer.imagePart().image.directives;
imagePart.addDirectives(m_colorDirectives).addDirectives(m_directives);
if (orientation->flipImages) if (orientation->flipImages)
drawable.scale(Vec2F(-1, 1), drawable.boundBox(false).center() - drawable.position); drawable.scale(Vec2F(-1, 1), drawable.boundBox(false).center() - drawable.position);
m_orientationDrawablesCache->second.append(move(drawable)); m_orientationDrawablesCache->second.append(move(drawable));
} }
} }

View File

@ -218,6 +218,9 @@ private:
float m_animationTimer; float m_animationTimer;
int m_currentFrame; int m_currentFrame;
Directives m_directives;
Directives m_colorDirectives;
Maybe<PeriodicFunction<float>> m_lightFlickering; Maybe<PeriodicFunction<float>> m_lightFlickering;
EntityTileDamageStatusPtr m_tileDamageStatus; EntityTileDamageStatusPtr m_tileDamageStatus;

View File

@ -25,6 +25,7 @@ ButtonWidget::ButtonWidget() {
auto interfaceConfig = assets->json("/interface.config"); auto interfaceConfig = assets->json("/interface.config");
m_pressedOffset = jsonToVec2I(interfaceConfig.get("buttonPressedOffset")); m_pressedOffset = jsonToVec2I(interfaceConfig.get("buttonPressedOffset"));
m_fontSize = interfaceConfig.query("font.buttonSize").toInt(); m_fontSize = interfaceConfig.query("font.buttonSize").toInt();
m_fontDirectives = interfaceConfig.queryString("font.defaultDirectives", "");
m_font = interfaceConfig.query("font.defaultFont").toString(); m_font = interfaceConfig.query("font.defaultFont").toString();
} }
@ -89,6 +90,7 @@ void ButtonWidget::renderImpl() {
if (!m_text.empty()) { if (!m_text.empty()) {
auto& guiContext = GuiContext::singleton(); auto& guiContext = GuiContext::singleton();
guiContext.setFontProcessingDirectives(m_fontDirectives);
guiContext.setFontSize(m_fontSize); guiContext.setFontSize(m_fontSize);
guiContext.setFont(m_font); guiContext.setFont(m_font);
if (m_disabled) if (m_disabled)
@ -100,6 +102,7 @@ void ButtonWidget::renderImpl() {
guiContext.setFontMode(FontMode::Shadow); guiContext.setFontMode(FontMode::Shadow);
guiContext.renderInterfaceText(m_text, {textPosition, m_hTextAnchor, VerticalAnchor::VMidAnchor}); guiContext.renderInterfaceText(m_text, {textPosition, m_hTextAnchor, VerticalAnchor::VMidAnchor});
guiContext.setFontMode(FontMode::Normal); guiContext.setFontMode(FontMode::Normal);
guiContext.setFontProcessingDirectives("");
} }
} }
@ -302,6 +305,10 @@ void ButtonWidget::setFontSize(int size) {
m_fontSize = size; m_fontSize = size;
} }
void ButtonWidget::setFontDirectives(String directives) {
m_fontDirectives = directives;
}
void ButtonWidget::setTextOffset(Vec2I textOffset) { void ButtonWidget::setTextOffset(Vec2I textOffset) {
m_textOffset = textOffset; m_textOffset = textOffset;
} }

View File

@ -69,6 +69,7 @@ public:
virtual void setText(String const& text); virtual void setText(String const& text);
virtual void setFontSize(int size); virtual void setFontSize(int size);
virtual void setFontDirectives(String directives);
virtual void setTextOffset(Vec2I textOffset); virtual void setTextOffset(Vec2I textOffset);
void setTextAlign(HorizontalAnchor hAnchor); void setTextAlign(HorizontalAnchor hAnchor);
@ -122,6 +123,7 @@ protected:
int m_fontSize; int m_fontSize;
String m_font; String m_font;
String m_fontDirectives;
String m_text; String m_text;
Vec2I m_textOffset; Vec2I m_textOffset;

View File

@ -234,6 +234,7 @@ void CanvasWidget::renderText(Vec2F const& renderingOffset, String const& s, Tex
TextPositioning translatedPosition = position; TextPositioning translatedPosition = position;
translatedPosition.pos += renderingOffset; translatedPosition.pos += renderingOffset;
context.renderInterfaceText(s, translatedPosition); context.renderInterfaceText(s, translatedPosition);
context.setDefaultLineSpacing(); context.setDefaultLineSpacing();
context.setDefaultFont(); context.setDefaultFont();
context.setFontProcessingDirectives(""); context.setFontProcessingDirectives("");

View File

@ -183,6 +183,9 @@ WidgetConstructResult WidgetParser::buttonHandler(String const& name, Json const
if (config.contains("fontSize")) if (config.contains("fontSize"))
button->setFontSize(config.getInt("fontSize")); button->setFontSize(config.getInt("fontSize"));
if (config.contains("fontDirectives"))
button->setFontDirectives(config.getString("fontDirectives"));
if (config.contains("fontColor")) if (config.contains("fontColor"))
button->setFontColor(jsonToColor(config.get("fontColor"))); button->setFontColor(jsonToColor(config.get("fontColor")));