Fix up new Object directives handling, more directives rendering improvements, fix chat bubbles and nametags only moving every 2px

This commit is contained in:
Kae 2023-06-25 16:10:57 +10:00
parent f7d5ff8deb
commit e2424b7dcf
15 changed files with 119 additions and 61 deletions

View File

@ -1,8 +1,9 @@
{
"nametag" : {
"showMasterNames" : true,
"fontDirectives" : "?border=2;444;4440",
"inspectOpacityRate" : 0.15
"fontDirectives" : "?border=1;222;2220",
"inspectOpacityRate" : 0.15,
"movementThreshold" : 0.5
},
"font" : {
"defaultDirectives" : "",

View File

@ -1,7 +1,7 @@
{
"config" : {
"font" : {
"directives" : "?border=1;333a;3330",
"directives" : "?border=1;111a;1110",
"padding" : [1, 1] // Padding to prevent border clipping at the edges of the log canvas while keeping compat with mods that patch the canvas size
},
"colors" : {

View File

@ -0,0 +1,3 @@
{
"movementThreshold" : 0.5
}

View File

@ -301,7 +301,7 @@ StringList jsonToStringList(Json const& v) {
Json jsonFromStringList(List<String> const& v) {
JsonArray result;
for (auto e : v)
for (auto& e : v)
result.push_back(e);
return result;
}
@ -322,7 +322,7 @@ StringSet jsonToStringSet(Json const& v) {
Json jsonFromStringSet(StringSet const& v) {
JsonArray result;
for (auto e : v)
for (auto& e : v)
result.push_back(e);
return result;
}
@ -369,6 +369,20 @@ List<Color> jsonToColorList(Json const& v) {
return result;
}
List<Directives> jsonToDirectivesList(Json const& v) {
List<Directives> result;
for (auto const& entry : v.iterateArray())
result.append(move(entry.toString()));
return result;
}
Json jsonFromDirectivesList(List<Directives> const& v) {
JsonArray result;
for (auto& e : v)
result.push_back(e.toString());
return result;
}
Json weightedChoiceFromJson(Json const& source, Json const& default_) {
if (source.isNull())
return default_;

View File

@ -6,6 +6,7 @@
#include "StarColor.hpp"
#include "StarSet.hpp"
#include "StarWeightedPool.hpp"
#include "StarDirectives.hpp"
namespace Star {
@ -81,6 +82,8 @@ List<Vec2U> jsonToVec2UList(Json const& v);
List<Vec2F> jsonToVec2FList(Json const& v);
List<Vec4B> jsonToVec4BList(Json const& v);
List<Color> jsonToColorList(Json const& v);
List<Directives> jsonToDirectivesList(Json const& v);
Json jsonFromDirectivesList(List<Directives> const& v);
Json weightedChoiceFromJson(Json const& source, Json const& default_);

View File

@ -143,11 +143,10 @@ void Object::init(World* world, EntityId entityId, EntityMode mode) {
m_orientationDrawablesCache.reset();
if (isMaster()) {
// This is stupid and we should only have to deal with the new directives parameter, but blah blah backwards compatibility.
auto colorName = configValue("color", "default").toString();
auto colorEnd = colorName.find('?');
if (colorEnd != NPos) {
setImageKey("color", colorName);
m_colorDirectives = colorName.substr(colorEnd);
}
else
@ -159,6 +158,9 @@ void Object::init(World* world, EntityId entityId, EntityMode mode) {
m_directives.parse(directives.toString());
}
if (isMaster()) {
setImageKey("color", colorName);
if (m_config->lightColors.contains(colorName))
m_lightSourceColor.set(m_config->lightColors.get(colorName));
else
@ -562,7 +564,7 @@ List<Drawable> Object::cursorHintDrawables() const {
if (m_direction.get() == Direction::Left)
placementImage += "?flipx";
Drawable imageDrawable = Drawable::makeImage(AssetPath::relativeTo(m_config->path, placementImage),
1.0 / TilePixels, false, jsonToVec2F(configValue("placementImagePosition")) / TilePixels);
1.0 / TilePixels, false, jsonToVec2F(configValue("placementImagePosition", jsonFromVec2F(Vec2F()))) / TilePixels);
return {imageDrawable};
} else {
if (m_orientationIndex != NPos) {
@ -1217,11 +1219,26 @@ List<Drawable> Object::orientationDrawables(size_t orientationIndex) const {
m_orientationDrawablesCache = make_pair(orientationIndex, List<Drawable>());
for (auto const& layer : orientation->imageLayers) {
Drawable drawable = layer;
auto& imagePart = drawable.imagePart();
imagePart.image.directives.clear();
imagePart.image = AssetPath::join(imagePart.image).replaceTags(m_imageKeys, true, "default");
String imagePath = AssetPath::join(imagePart.image);
if (m_colorDirectives && m_imageKeys.contains("color")) { // We had to leave color untouched despite separating its directives for server-side compatibility reasons, temporarily substr it in the image key
String& color = m_imageKeys.find("color")->second;
String backup = move(color);
color = backup.substr(0, backup.find('?'));
imagePart.image = imagePath.replaceTags(m_imageKeys, true, "default");
color = move(backup);
imagePart.image.directives = layer.imagePart().image.directives;
imagePart.addDirectives(m_colorDirectives).addDirectives(m_directives);
imagePart.addDirectives(m_colorDirectives);
}
else {
imagePart.image = imagePath.replaceTags(m_imageKeys, true, "default");
imagePart.image.directives = layer.imagePart().image.directives;
}
imagePart.addDirectives(m_directives);
if (orientation->flipImages)
drawable.scale(Vec2F(-1, 1), drawable.boundBox(false).center() - drawable.position);

View File

@ -232,7 +232,7 @@ private:
size_t m_orientationIndex;
NetElementSize m_orientationIndexNetState;
NetElementHashMap<String, String> m_netImageKeys;
StringMap<String> m_imageKeys;
mutable StringMap<String> m_imageKeys;
void resetEmissionTimers();
List<GameTimer> m_emissionTimers;

View File

@ -39,8 +39,9 @@ ParallaxLayer::ParallaxLayer(Json const& store) : ParallaxLayer() {
}
Json ParallaxLayer::store() const {
return JsonObject{{"textures", jsonFromStringList(textures)},
{"directives", directives},
return JsonObject{
{"textures", jsonFromStringList(textures)},
{"directives", directives.toString()},
{"parallaxValue", jsonFromVec2F(parallaxValue)},
{"repeat", jsonFromVec2B(repeat)},
{"tileLimitTop", jsonFromMaybe(tileLimitTop)},
@ -55,9 +56,18 @@ Json ParallaxLayer::store() const {
{"fadePercent", fadePercent}};
}
void ParallaxLayer::addImageDirectives(String const& newDirectives) {
if (!newDirectives.empty())
directives = String::joinWith("?", directives, newDirectives);
void ParallaxLayer::addImageDirectives(Directives const& newDirectives) {
if (newDirectives) { // TODO: Move to Directives +=
if (directives) {
List<Directives::Entry> newEntries = *directives.entries;
for (auto const& entry : *newDirectives.entries)
newEntries.push_back(entry);
directives = move(newEntries);
}
else
directives = newDirectives;
}
}
void ParallaxLayer::fadeToSkyColor(Color skyColor) {
@ -225,11 +235,11 @@ void Parallax::buildLayer(Json const& layerSettings, String const& kind) {
layer.addImageDirectives(layerSettings.getString("directives", ""));
if (isFoliage)
layer.addImageDirectives(strf("hueshift=%s", m_parallaxTreeVariant->foliageHueShift));
layer.addImageDirectives(String(strf("hueshift=%s", m_parallaxTreeVariant->foliageHueShift)));
else if (isStem)
layer.addImageDirectives(strf("hueshift=%s", m_parallaxTreeVariant->stemHueShift));
layer.addImageDirectives(String(strf("hueshift=%s", m_parallaxTreeVariant->stemHueShift)));
else if (!layerSettings.getBool("nohueshift", false))
layer.addImageDirectives(strf("hueshift=%s", m_hueShift));
layer.addImageDirectives(String(strf("hueshift=%s", m_hueShift)));
layer.fadePercent = layerSettings.getFloat("fadePercent", 0);

View File

@ -4,6 +4,7 @@
#include "StarMaybe.hpp"
#include "StarColor.hpp"
#include "StarPlantDatabase.hpp"
#include "StarDirectives.hpp"
namespace Star {
@ -16,11 +17,11 @@ struct ParallaxLayer {
Json store() const;
void addImageDirectives(String const& newDirectives);
void addImageDirectives(Directives const& newDirectives);
void fadeToSkyColor(Color skyColor);
StringList textures;
String directives;
List<String> textures;
Directives directives;
float alpha;
Vec2F parallaxValue;
Vec2B repeat;

View File

@ -356,10 +356,11 @@ void WorldClient::render(WorldRenderData& renderData, unsigned bufferTiles) {
playerAimInteractive = entity->entityId();
}
Maybe<StringList> directives;
if (auto worldTemplate = m_worldTemplate) {
if(auto parameters = worldTemplate->worldParameters())
directives = m_worldTemplate->worldParameters()->globalDirectives;
const List<Directives>* directives = nullptr;
if (auto& worldTemplate = m_worldTemplate) {
if (auto& parameters = worldTemplate->worldParameters())
if (auto& globalDirectives = m_worldTemplate->worldParameters()->globalDirectives)
directives = &globalDirectives.get();
}
m_entityMap->forAllEntities([&](EntityPtr const& entity) {
if (m_startupHiddenEntities.contains(entity->entityId()))
@ -1263,10 +1264,11 @@ void WorldClient::handleDamageNotifications() {
auto hitParticles = particlesFromDefinition(damageKind.effects.get(material).get(effectHitType).particles, damageNotification.position);
Maybe<StringList> directives;
if (auto worldTemplate = m_worldTemplate) {
if(auto parameters = worldTemplate->worldParameters())
directives = m_worldTemplate->worldParameters()->globalDirectives;
const List<Directives>* directives = nullptr;
if (auto& worldTemplate = m_worldTemplate) {
if (auto& parameters = worldTemplate->worldParameters())
if (auto& globalDirectives = m_worldTemplate->worldParameters()->globalDirectives)
directives = &globalDirectives.get();
}
if (directives) {
int directiveIndex = unsigned(damageNotification.targetEntityId) % directives->size();
@ -1332,10 +1334,11 @@ void WorldClient::removeEntity(EntityId entityId, bool andDie) {
ClientRenderCallback renderCallback;
entity->destroy(&renderCallback);
Maybe<StringList> directives;
if (auto worldTemplate = m_worldTemplate) {
if(auto parameters = worldTemplate->worldParameters())
directives = m_worldTemplate->worldParameters()->globalDirectives;
const List<Directives>* directives = nullptr;
if (auto& worldTemplate = m_worldTemplate) {
if (auto& parameters = worldTemplate->worldParameters())
if (auto& globalDirectives = m_worldTemplate->worldParameters()->globalDirectives)
directives = &globalDirectives.get();
}
if (directives) {
int directiveIndex = unsigned(entity->entityId()) % directives->size();

View File

@ -47,7 +47,7 @@ VisitableWorldParameters::VisitableWorldParameters(Json const& store) {
weatherPool = jsonToWeightedPool<String>(store.getArray("weatherPool", JsonArray()));
environmentStatusEffects = store.opt("environmentStatusEffects").apply(jsonToStringList).value();
overrideTech = store.opt("overrideTech").apply(jsonToStringList);
globalDirectives = store.opt("globalDirectives").apply(jsonToStringList);
globalDirectives = store.opt("globalDirectives").apply(jsonToDirectivesList);
beamUpRule = BeamUpRuleNames.getLeft(store.getString("beamUpRule", "Surface"));
disableDeathDrops = store.getBool("disableDeathDrops", false);
terraformed = store.getBool("terraformed", false);
@ -65,7 +65,7 @@ Json VisitableWorldParameters::store() const {
{"weatherPool", jsonFromWeightedPool<String>(weatherPool)},
{"environmentStatusEffects", jsonFromStringList(environmentStatusEffects)},
{"overrideTech", jsonFromMaybe(overrideTech.apply(&jsonFromStringList))},
{"globalDirectives", jsonFromMaybe(globalDirectives.apply(&jsonFromStringList))},
{"globalDirectives", jsonFromMaybe(globalDirectives.apply(&jsonFromDirectivesList))},
{"beamUpRule", BeamUpRuleNames.getRight(beamUpRule)},
{"disableDeathDrops", disableDeathDrops},
{"terraformed", terraformed},
@ -606,7 +606,7 @@ TerrestrialWorldParametersPtr generateTerrestrialWorldParameters(String const& t
parameters->airless = biomeDatabase->biomeIsAirless(primaryBiome);
parameters->environmentStatusEffects = biomeDatabase->biomeStatusEffects(primaryBiome);
parameters->overrideTech = config.opt("overrideTech").apply(jsonToStringList);
parameters->globalDirectives = config.opt("globalDirectives").apply(jsonToStringList);
parameters->globalDirectives = config.opt("globalDirectives").apply(jsonToDirectivesList);
parameters->beamUpRule = BeamUpRuleNames.getLeft(config.getString("beamUpRule", "Surface"));
parameters->disableDeathDrops = config.getBool("disableDeathDrops", false);
parameters->worldEdgeForceRegions = WorldEdgeForceRegionTypeNames.getLeft(config.getString("worldEdgeForceRegions", "Top"));
@ -658,7 +658,7 @@ AsteroidsWorldParametersPtr generateAsteroidsWorldParameters(uint64_t seed) {
parameters->gravity = staticRandomFloatRange(gravityRange[0], gravityRange[1], seed, "WorldGravity");
parameters->environmentStatusEffects = jsonToStringList(asteroidsConfig.getArray("environmentStatusEffects", JsonArray()));
parameters->overrideTech = asteroidsConfig.opt("overrideTech").apply(jsonToStringList);
parameters->globalDirectives = asteroidsConfig.opt("globalDirectives").apply(jsonToStringList);
parameters->globalDirectives = asteroidsConfig.opt("globalDirectives").apply(jsonToDirectivesList);
parameters->beamUpRule = BeamUpRuleNames.getLeft(asteroidsConfig.getString("beamUpRule", "Surface"));
parameters->disableDeathDrops = asteroidsConfig.getBool("disableDeathDrops", false);
parameters->worldEdgeForceRegions = WorldEdgeForceRegionTypeNames.getLeft(asteroidsConfig.getString("worldEdgeForceRegions", "TopAndBottom"));
@ -687,7 +687,7 @@ FloatingDungeonWorldParametersPtr generateFloatingDungeonWorldParameters(String
parameters->airless = worldConfig.getBool("airless", false);
parameters->environmentStatusEffects = jsonToStringList(worldConfig.getArray("environmentStatusEffects", JsonArray()));
parameters->overrideTech = worldConfig.opt("overrideTech").apply(jsonToStringList);
parameters->globalDirectives = worldConfig.opt("globalDirectives").apply(jsonToStringList);
parameters->globalDirectives = worldConfig.opt("globalDirectives").apply(jsonToDirectivesList);
if (auto weatherPoolConfig = worldConfig.optArray("weatherPool"))
parameters->weatherPool = jsonToWeightedPool<String>(*weatherPoolConfig);
parameters->beamUpRule = BeamUpRuleNames.getLeft(worldConfig.getString("beamUpRule", "Surface"));

View File

@ -58,7 +58,7 @@ struct VisitableWorldParameters {
WeatherPool weatherPool;
StringList environmentStatusEffects;
Maybe<StringList> overrideTech;
Maybe<StringList> globalDirectives;
Maybe<List<Directives>> globalDirectives;
BeamUpRule beamUpRule;
bool disableDeathDrops;
bool terraformed;

View File

@ -80,10 +80,10 @@ void EnvironmentPainter::renderStars(float pixelRatio, Vec2F const& screenSize,
RectF viewRect = RectF::withSize(Vec2F(), viewSize).padded(screenBuffer);
for (auto star : stars) {
for (auto& star : stars) {
Vec2F screenPos = transform.transformVec2(star.first);
if (viewRect.contains(screenPos)) {
size_t starFrame = (int)(sky.epochTime + star.second.second) % sky.starFrames;
size_t starFrame = (size_t)(sky.epochTime + star.second.second) % sky.starFrames;
auto const& texture = m_starTextures[star.second.first * sky.starFrames + starFrame];
m_renderer->render(renderTexturedRect(texture, screenPos * pixelRatio - Vec2F(texture->size()) / 2, 1.0, color, 0.0f));
}
@ -224,7 +224,7 @@ void EnvironmentPainter::renderParallaxLayers(
// Note: the "parallax space" referenced below is a grid where the scale of each cell is the size of the parallax image
for (auto layer : layers) {
for (auto& layer : layers) {
if (layer.alpha == 0)
continue;
@ -237,8 +237,10 @@ void EnvironmentPainter::renderParallaxLayers(
Vec2F parallaxValue = layer.parallaxValue;
Vec2B parallaxRepeat = layer.repeat;
Vec2F parallaxOrigin = {0.0f, layer.verticalOrigin};
Vec2F parallaxSize =
Vec2F(m_textureGroup->loadTexture(String::joinWith("?", layer.textures.first(), layer.directives))->size());
AssetPath first = layer.textures.first();
first.directives += layer.directives;
Vec2F parallaxSize = Vec2F(m_textureGroup->loadTexture(first)->size());
Vec2F parallaxPixels = parallaxSize * camera.pixelRatio();
// texture offset in *screen pixel space*
@ -300,8 +302,10 @@ void EnvironmentPainter::renderParallaxLayers(
if (tileLimitBottom != bottom && y < tileLimitBottom)
anchorPoint[1] += fpart(tileLimitBottom) * parallaxPixels[1];
for (String const& textureImage : layer.textures) {
if (auto texture = m_textureGroup->tryTexture(String::joinWith("?", textureImage, layer.directives))) {
for (auto const& textureImage : layer.textures) {
AssetPath withDirectives = textureImage;
withDirectives.directives += layer.directives;
if (auto texture = m_textureGroup->tryTexture(withDirectives)) {
RectF drawRect = RectF::withSize(anchorPoint, subImage.size() * camera.pixelRatio());
m_renderer->render(RenderQuad{move(texture),
{{drawRect.xMin(), drawRect.yMin()}, {subImage.xMin(), subImage.yMin()}, drawColor, lightMapMultiplier},

View File

@ -203,6 +203,8 @@ void WorldPainter::renderParticles(WorldRenderData& renderData, Particle::Layer
if (size > 0) {
m_textPainter->setFontSize(size);
m_textPainter->setFontColor(particle.color.toRgba());
m_textPainter->setProcessingDirectives("");
m_textPainter->setFont("");
m_textPainter->renderText(particle.string, {position, HorizontalAnchor::HMidAnchor, VerticalAnchor::VMidAnchor});
}
}

View File

@ -44,7 +44,7 @@ private:
TilePainterPtr m_tilePainter;
Json m_highlightConfig;
Map<EntityHighlightEffectType, pair<String, String>> m_highlightDirectives;
Map<EntityHighlightEffectType, pair<Directives, Directives>> m_highlightDirectives;
Vec2F m_entityBarOffset;
Vec2F m_entityBarSpacing;