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