MassBuilderSaveTool/src/Mass/Mass_Styles.cpp

155 lines
5.9 KiB
C++
Raw Normal View History

// MassBuilderSaveTool
// Copyright (C) 2021-2023 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 "../Logger/Logger.h"
#include "../Gvas/Types/ArrayProperty.h"
#include "../Gvas/Types/ColourStructProperty.h"
#include "../Gvas/Types/FloatProperty.h"
#include "../Gvas/Types/GenericStructProperty.h"
#include "../Gvas/Types/IntProperty.h"
#include "../Gvas/Types/StringProperty.h"
#include "Mass.h"
using namespace Containers::Literals;
namespace mbst { namespace GameObjects {
2022-12-03 16:49:39 +01:00
Containers::ArrayView<CustomStyle>
Mass::globalStyles() {
return _globalStyles;
}
2022-12-03 16:49:39 +01:00
void
Mass::getGlobalStyles() {
LOG_INFO("Getting global styles.");
auto unit_data = _mass->at<Gvas::Types::GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
LOG_ERROR_FORMAT("Couldn't find {} in {}.", MASS_UNIT_DATA, _filename);
_state = State::Invalid;
return;
}
auto global_styles = unit_data->at<Gvas::Types::ArrayProperty>(MASS_GLOBAL_STYLES);
if(!global_styles) {
LOG_WARNING_FORMAT("Couldn't find global styles in {}.", _filename);
_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);
}
2022-12-03 16:49:39 +01:00
bool
Mass::writeGlobalStyle(std::size_t index) {
LOG_INFO_FORMAT("Writing global style number {}.", index);
if(index > _globalStyles.size()) {
_lastError = "Global style index out of range"_s;
LOG_ERROR(_lastError);
return false;
}
auto unit_data = _mass->at<Gvas::Types::GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
_lastError = "No unit data found in "_s + _filename;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
auto global_styles = unit_data->at<Gvas::Types::ArrayProperty>(MASS_GLOBAL_STYLES);
if(!global_styles) {
_lastError = "No global styles found in "_s + _filename;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
return writeCustomStyle(_globalStyles[index], index, global_styles);
}
2022-12-03 16:49:39 +01:00
void
Mass::getCustomStyles(Containers::ArrayView<CustomStyle> styles, Gvas::Types::ArrayProperty* style_array) {
for(std::uint32_t i = 0; i < style_array->items.size(); i++) {
auto style_prop = style_array->at<Gvas::Types::GenericStructProperty>(i);
auto& style = styles[i];
style.name = style_prop->at<Gvas::Types::StringProperty>(MASS_STYLE_NAME)->value;
auto colour_prop = style_prop->at<Gvas::Types::ColourStructProperty>(MASS_STYLE_COLOUR);
style.colour = Color4{colour_prop->r, colour_prop->g, colour_prop->b, colour_prop->a};
style.metallic = style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_METALLIC)->value;
style.gloss = style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_GLOSS)->value;
style.glow = colour_prop->a != 0.0f;
style.patternId = style_prop->at<Gvas::Types::IntProperty>(MASS_STYLE_PATTERN_ID)->value;
style.opacity = style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_OPACITY)->value;
style.offset = Vector2{
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_OFFSETX)->value,
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_OFFSETY)->value
};
style.rotation = style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_ROTATION)->value;
style.scale = style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_SCALE)->value;
}
}
2022-12-03 16:49:39 +01:00
bool
Mass::writeCustomStyle(const CustomStyle& style, std::size_t index, Gvas::Types::ArrayProperty* style_array) {
if(!style_array) {
_lastError = "style_array is null."_s;
LOG_ERROR(_lastError);
return false;
}
auto style_prop = style_array->at<Gvas::Types::GenericStructProperty>(index);
if(!style_prop) {
_lastError = "Style index is out of range in "_s + _filename;
LOG_ERROR(_lastError);
return false;
}
style_prop->at<Gvas::Types::StringProperty>(MASS_STYLE_NAME)->value = style.name;
auto colour_prop = style_prop->at<Gvas::Types::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<Gvas::Types::FloatProperty>(MASS_STYLE_METALLIC)->value = style.metallic;
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_GLOSS)->value = style.gloss;
style_prop->at<Gvas::Types::IntProperty>(MASS_STYLE_PATTERN_ID)->value = style.patternId;
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_OPACITY)->value = style.opacity;
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_OFFSETX)->value = style.offset.x();
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_OFFSETY)->value = style.offset.y();
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_ROTATION)->value = style.rotation;
style_prop->at<Gvas::Types::FloatProperty>(MASS_STYLE_PATTERN_SCALE)->value = style.scale;
if(!_mass->saveToFile()) {
_lastError = _mass->lastError();
return false;
}
return true;
}
}}