William JCM
9ec88fa521
Now property names are behind defines, so if a name changes, I don't have to hunt it down in up to six source files.
134 lines
5.3 KiB
C++
134 lines
5.3 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 "PropertyNames.h"
|
|
#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>(MASS_UNIT_DATA);
|
|
if(!unit_data) {
|
|
Utility::Error{} << "Can't find unit data in" << _filename;
|
|
_state = State::Invalid;
|
|
return;
|
|
}
|
|
|
|
auto global_styles = unit_data->at<ArrayProperty>(MASS_GLOBAL_STYLES);
|
|
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>(MASS_UNIT_DATA);
|
|
if(!unit_data) {
|
|
_state = State::Invalid;
|
|
_lastError = "No unit data found in "_s + _filename;
|
|
return false;
|
|
}
|
|
|
|
auto global_styles = unit_data->at<ArrayProperty>(MASS_GLOBAL_STYLES);
|
|
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>(MASS_STYLE_NAME)->value;
|
|
auto colour_prop = style_prop->at<ColourStructProperty>(MASS_STYLE_COLOUR);
|
|
style.colour = Color4{colour_prop->r, colour_prop->g, colour_prop->b, colour_prop->a};
|
|
style.glow = colour_prop->a == 0.0f ? false : true;
|
|
style.metallic = style_prop->at<FloatProperty>(MASS_STYLE_METALLIC)->value;
|
|
style.gloss = style_prop->at<FloatProperty>(MASS_STYLE_GLOSS)->value;
|
|
|
|
style.patternId = style_prop->at<IntProperty>(MASS_STYLE_PATTERN_ID)->value;
|
|
style.opacity = style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_OPACITY)->value;
|
|
style.offset = Vector2{
|
|
style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_OFFSETX)->value,
|
|
style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_OFFSETY)->value
|
|
};
|
|
style.rotation = style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_ROTATION)->value;
|
|
style.scale = style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_SCALE)->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>(MASS_STYLE_NAME)->value = style.name;
|
|
auto colour_prop = style_prop->at<ColourStructProperty>(MASS_STYLE_COLOUR);
|
|
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>(MASS_STYLE_METALLIC)->value = style.metallic;
|
|
style_prop->at<FloatProperty>(MASS_STYLE_GLOSS)->value = style.gloss;
|
|
|
|
style_prop->at<IntProperty>(MASS_STYLE_PATTERN_ID)->value = style.patternId;
|
|
style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_OPACITY)->value = style.opacity;
|
|
style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_OFFSETX)->value = style.offset.x();
|
|
style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_OFFSETY)->value = style.offset.y();
|
|
style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_ROTATION)->value = style.rotation;
|
|
style_prop->at<FloatProperty>(MASS_STYLE_PATTERN_SCALE)->value = style.scale;
|
|
|
|
if(!_mass->saveToFile()) {
|
|
_lastError = _mass->lastError();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|