MassBuilderSaveTool/src/Mass/Mass_Styles.cpp

134 lines
5.7 KiB
C++

// MassBuilderSaveTool
// Copyright (C) 2021-2022 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "../UESaveFile/Types/ArrayProperty.h"
#include "../UESaveFile/Types/ColourStructProperty.h"
#include "../UESaveFile/Types/FloatProperty.h"
#include "../UESaveFile/Types/GenericStructProperty.h"
#include "../UESaveFile/Types/IntProperty.h"
#include "../UESaveFile/Types/StringProperty.h"
#include "Mass.h"
using namespace Containers::Literals;
auto Mass::globalStyles() -> Containers::ArrayView<CustomStyle> {
return _globalStyles;
}
void Mass::getGlobalStyles() {
auto unit_data = _mass->at<GenericStructProperty>("UnitData"_s);
if(!unit_data) {
_state = State::Invalid;
return;
}
auto global_styles = unit_data->at<ArrayProperty>("GlobalStyles_57_6A681C114035241F7BDAAE9B43A8BF1B"_s);
if(!global_styles) {
_globalStyles = Containers::Array<CustomStyle>{0};
return;
}
if(global_styles->items.size() != _globalStyles.size()) {
_globalStyles = Containers::Array<CustomStyle>{global_styles->items.size()};
}
getCustomStyles(_globalStyles, global_styles);
}
auto Mass::writeGlobalStyle(UnsignedLong index) -> bool {
if(index > _globalStyles.size()) {
_lastError = "Global style index out of range"_s;
return false;
}
auto unit_data = _mass->at<GenericStructProperty>("UnitData"_s);
if(!unit_data) {
_state = State::Invalid;
_lastError = "No unit data found in "_s + _filename;
return false;
}
auto global_styles = unit_data->at<ArrayProperty>("GlobalStyles_57_6A681C114035241F7BDAAE9B43A8BF1B"_s);
if(!global_styles) {
_state = State::Invalid;
_lastError = "No global styles found in "_s + _filename;
return false;
}
return writeCustomStyle(_globalStyles[index], index, global_styles);
}
void Mass::getCustomStyles(Containers::ArrayView<CustomStyle> styles, ArrayProperty* style_array) {
for(UnsignedInt i = 0; i < style_array->items.size(); i++) {
auto style_prop = style_array->at<GenericStructProperty>(i);
auto& style = styles[i];
style.name = style_prop->at<StringProperty>("Name_27_1532115A46EF2B2FA283908DF561A86B"_s)->value;
auto colour_prop = style_prop->at<ColourStructProperty>("Color_5_F0D383DF40474C9464AE48A0984A212E"_s);
style.colour = Color4{colour_prop->r, colour_prop->g, colour_prop->b, colour_prop->a};
style.metallic = style_prop->at<FloatProperty>("Metallic_10_0A4CD1E4482CBF41CA61D0A856DE90B9"_s)->value;
style.gloss = style_prop->at<FloatProperty>("Gloss_11_9769599842CC275A401C4282A236E240"_s)->value;
style.glow = colour_prop->a == 0.0f ? false : true;
style.patternId = style_prop->at<IntProperty>("PatternID_14_516DB85641DAF8ECFD2920BE2BDF1311"_s)->value;
style.opacity = style_prop->at<FloatProperty>("Opacity_30_53BD060B4DFCA1C92302D6A0F7831131"_s)->value;
style.offset = Vector2{
style_prop->at<FloatProperty>("OffsetX_23_70FC2E814C64BBB82452748D2AF9CD48"_s)->value,
style_prop->at<FloatProperty>("OffsetY_24_5E1F866C4C054D9B2EE337ADC180C17F"_s)->value
};
style.rotation = style_prop->at<FloatProperty>("Rotation_25_EC2DFAD84AD0A6BD3FA841ACD52EDD6D"_s)->value;
style.scale = style_prop->at<FloatProperty>("Scale_26_19DF0708409262183E1247B317137671"_s)->value;
}
}
auto Mass::writeCustomStyle(const CustomStyle& style, UnsignedLong index, ArrayProperty* style_array) -> bool {
if(!style_array) {
_lastError = "Mass::setCustomStyle(): style_array is null."_s;
return false;
}
auto style_prop = style_array->at<GenericStructProperty>(index);
if(!style_prop) {
_lastError = "Style index is out of range in "_s + _filename;
return false;
}
style_prop->at<StringProperty>("Name_27_1532115A46EF2B2FA283908DF561A86B"_s)->value = style.name;
auto colour_prop = style_prop->at<ColourStructProperty>("Color_5_F0D383DF40474C9464AE48A0984A212E"_s);
colour_prop->r = style.colour.r();
colour_prop->g = style.colour.g();
colour_prop->b = style.colour.b();
colour_prop->a = style.glow ? 1.0f : 0.0f;
style_prop->at<FloatProperty>("Metallic_10_0A4CD1E4482CBF41CA61D0A856DE90B9"_s)->value = style.metallic;
style_prop->at<FloatProperty>("Gloss_11_9769599842CC275A401C4282A236E240"_s)->value = style.gloss;
style_prop->at<IntProperty>("PatternID_14_516DB85641DAF8ECFD2920BE2BDF1311"_s)->value = style.patternId;
style_prop->at<FloatProperty>("Opacity_30_53BD060B4DFCA1C92302D6A0F7831131"_s)->value = style.opacity;
style_prop->at<FloatProperty>("OffsetX_23_70FC2E814C64BBB82452748D2AF9CD48"_s)->value = style.offset.x();
style_prop->at<FloatProperty>("OffsetY_24_5E1F866C4C054D9B2EE337ADC180C17F"_s)->value = style.offset.y();
style_prop->at<FloatProperty>("Rotation_25_EC2DFAD84AD0A6BD3FA841ACD52EDD6D"_s)->value = style.rotation;
style_prop->at<FloatProperty>("Scale_26_19DF0708409262183E1247B317137671"_s)->value = style.scale;
if(!_mass->saveToFile()) {
_lastError = _mass->lastError();
return false;
}
return true;
}