From 4c4c98a805d3cf1d4398fdd5774252b8f90265c2 Mon Sep 17 00:00:00 2001 From: riidefi <34194588+riidefi@users.noreply.github.com> Date: Thu, 27 Jan 2022 21:07:59 -0700 Subject: [PATCH] Revert "[plugins] Remove old BFG editor" This reverts commit 10d9cebe600ace146925b11904004cb14a30ba6d. --- source/plugins/CMakeLists.txt | 7 ++- source/plugins/mk/BFG/Fog.hpp | 18 ++++++ source/plugins/mk/BFG/FogEntry.hpp | 40 +++++++++++++ source/plugins/mk/BFG/FogView.cpp | 78 ++++++++++++++++++++++++++ source/plugins/mk/BFG/Node.h | 88 +++++++++++++++++++++++++++++ source/plugins/mk/BFG/io/BFG.cpp | 90 ++++++++++++++++++++++++++++++ source/plugins/mk/BFG/io/BFG.hpp | 24 ++++++++ 7 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 source/plugins/mk/BFG/Fog.hpp create mode 100644 source/plugins/mk/BFG/FogEntry.hpp create mode 100644 source/plugins/mk/BFG/FogView.cpp create mode 100644 source/plugins/mk/BFG/Node.h create mode 100644 source/plugins/mk/BFG/io/BFG.cpp create mode 100644 source/plugins/mk/BFG/io/BFG.hpp diff --git a/source/plugins/CMakeLists.txt b/source/plugins/CMakeLists.txt index ce397ba07..a33ed3d79 100644 --- a/source/plugins/CMakeLists.txt +++ b/source/plugins/CMakeLists.txt @@ -78,6 +78,11 @@ add_library(plugins "g3d/io/MDL0.cpp" "g3d/io/Common.hpp" "g3d/io/Common.cpp" - + "mk/BFG/io/BFG.hpp" + "mk/BFG/io/BFG.cpp" + "mk/BFG/Fog.hpp" + "mk/BFG/FogEntry.hpp" + "mk/BFG/FogView.cpp" + "mk/BFG/Node.h" "rhst/RHSTImporter.cpp" "g3d/ui/G3dSrtView.cpp" "g3d/G3dUi.cpp" "g3d/G3dIo.hpp" "j3d/J3dIo.hpp" "ass/Ass.hpp" "ass/SupportedFiles.hpp" "ass/SupportedFiles.cpp" "ass/InclusionMask.hpp" "ass/InclusionMask.cpp" "ass/LogScope.hpp" "ass/LogScope.cpp" "ass/ImportTexture.hpp" "ass/ImportTexture.cpp") diff --git a/source/plugins/mk/BFG/Fog.hpp b/source/plugins/mk/BFG/Fog.hpp new file mode 100644 index 000000000..7c70b2e67 --- /dev/null +++ b/source/plugins/mk/BFG/Fog.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +namespace riistudio::mk { + +class BinaryFogData { +public: + bool operator==(const BinaryFogData&) const = default; +}; + +struct NullClass {}; + +} // namespace riistudio::mk + +#include +#include diff --git a/source/plugins/mk/BFG/FogEntry.hpp b/source/plugins/mk/BFG/FogEntry.hpp new file mode 100644 index 000000000..3050ec380 --- /dev/null +++ b/source/plugins/mk/BFG/FogEntry.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +namespace riistudio::mk { + +enum class FogType { + None, + + PerspectiveLinear, + PerspectiveExponential, + PerspectiveQuadratic, + PerspectiveInverseExponential, + PerspectiveInverseQuadratic, + + OrthographicLinear, + OrthographicExponential, + OrthographicQuadratic, + OrthographicInverseExponential, + OrthographicInverseQuadratic +}; + +class FogEntry { + +public: + bool operator==(const FogEntry&) const = default; + + FogType mType; + f32 mStartZ; + f32 mEndZ; + librii::gx::Color mColor; + u16 mEnabled; + u16 mCenter; + f32 mFadeSpeed; + u16 mUnk18; + u16 mUnk1A; +}; + +} // namespace riistudio::mk diff --git a/source/plugins/mk/BFG/FogView.cpp b/source/plugins/mk/BFG/FogView.cpp new file mode 100644 index 000000000..af5206110 --- /dev/null +++ b/source/plugins/mk/BFG/FogView.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace riistudio::mk { + +using namespace llvm; + +namespace ui { + +auto fogEntry = + kpi::StatelessPropertyView() + .setTitle("Properties") + .setIcon((const char*)ICON_FA_COGS) + .onDraw([](kpi::PropertyDelegate& delegate) { + auto& fog = delegate.getActive(); + int orthoOrPersp = + static_cast(fog.mType) >= + static_cast(FogType::OrthographicLinear) + ? 0 + : 1; + int fogType = static_cast(fog.mType); + if (fogType >= static_cast(FogType::OrthographicLinear)) + fogType -= 5; + ImGui::Combo("Projection", &orthoOrPersp, + "Orthographic\0Perspective\0"); + ImGui::Combo("Function", &fogType, + "None\0Linear\0Exponential\0Quadratic\0Inverse " + "Exponential\0Quadratic\0"); + int new_type = fogType; + if (new_type != 0) + new_type += (1 - orthoOrPersp) * 5; + KPI_PROPERTY_EX(delegate, mType, static_cast(new_type)); + + bool enabled = fog.mEnabled; + ImGui::Checkbox("Fog Enabled", &enabled); + KPI_PROPERTY_EX(delegate, mEnabled, static_cast(enabled)); + { + util::ConditionalActive g(enabled); + float startZ = fog.mStartZ; + ImGui::InputFloat("Start Z", &startZ); + KPI_PROPERTY_EX(delegate, mStartZ, startZ); + + float endZ = fog.mEndZ; + ImGui::InputFloat("End Z", &endZ); + KPI_PROPERTY_EX(delegate, mEndZ, endZ); + + librii::gx::ColorF32 clr_f32 = fog.mColor; + ImGui::ColorEdit4("Fog Color", clr_f32); + KPI_PROPERTY_EX(delegate, mColor, (librii::gx::Color)clr_f32); + + int center = fog.mCenter; + ImGui::InputInt("Center", ¢er); + KPI_PROPERTY_EX(delegate, mCenter, static_cast(center)); + + float fadeSpeed = fog.mFadeSpeed; + ImGui::SliderFloat("Fade Speed", &fadeSpeed, 0.0f, 1.0f); + KPI_PROPERTY_EX(delegate, mFadeSpeed, fadeSpeed); + } + }); +} // namespace ui +kpi::DecentralizedInstaller + TypeInstaller([](kpi::ApplicationPlugins& installer) { + installer.addType(); + kpi::RichNameManager::getInstance().addRichName( + (const char*)ICON_FA_CLOUD, "Fog Entry", (const char*)ICON_FA_CLOUD, + "Fog Entries"); + + installer.addDeserializer(); + installer.addSerializer(); + }); +} // namespace riistudio::mk diff --git a/source/plugins/mk/BFG/Node.h b/source/plugins/mk/BFG/Node.h new file mode 100644 index 000000000..cafaa5326 --- /dev/null +++ b/source/plugins/mk/BFG/Node.h @@ -0,0 +1,88 @@ +// This is a generated file + +namespace riistudio::mk { + +class BinaryFog : public mk::NullClass, public kpi::TDocData, public kpi::INode { +public: + BinaryFog() = default; + virtual ~BinaryFog() = default; + + kpi::MutCollectionRange getFogEntries() { return { &mFogEntries }; } + kpi::ConstCollectionRange getFogEntries() const { return { &mFogEntries }; } + +protected: + kpi::ICollection* v_getFogEntries() const { return const_cast(static_cast(&mFogEntries)); } + void onRelocate() { + mFogEntries.onParentMoved(this); + } + BinaryFog(BinaryFog&& rhs) { + new (&mFogEntries) decltype(mFogEntries) (std::move(rhs.mFogEntries)); + + onRelocate(); + } + BinaryFog(const BinaryFog& rhs) { + new (&mFogEntries) decltype(mFogEntries) (rhs.mFogEntries); + + onRelocate(); + } + BinaryFog& operator=(BinaryFog&& rhs) { + new (this) BinaryFog (std::move(rhs)); + onRelocate(); + return *this; + } + BinaryFog& operator=(const BinaryFog& rhs) { + new (this) BinaryFog (rhs); + onRelocate(); + return *this; + } + +private: + kpi::CollectionImpl mFogEntries{this}; + + // INode implementations + std::size_t numFolders() const override { return 1; } + const kpi::ICollection* folderAt(std::size_t index) const override { + switch (index) { + case 0: return &mFogEntries; + default: return nullptr; + } + } + kpi::ICollection* folderAt(std::size_t index) override { + switch (index) { + case 0: return &mFogEntries; + default: return nullptr; + } + } + const char* idAt(std::size_t index) const override { + switch (index) { + case 0: return typeid(FogEntry).name(); + default: return nullptr; + } + } + std::size_t fromId(const char* id) const override { + if (!strcmp(id, typeid(FogEntry).name())) return 0; + return ~0; + } + virtual kpi::IDocData* getImmediateData() { return static_cast*>(this); } + virtual const kpi::IDocData* getImmediateData() const { return static_cast*>(this); } + +public: + struct _Memento : public kpi::IMemento { + kpi::ConstPersistentVec mFogEntries; + template _Memento(const M& _new, const kpi::IMemento* last=nullptr) { + const auto* old = last ? dynamic_cast(last) : nullptr; + kpi::nextFolder(this->mFogEntries, _new.getFogEntries(), old ? &old->mFogEntries : nullptr); + } + }; + std::unique_ptr next(const kpi::IMemento* last) const override { + return std::make_unique<_Memento>(*this, last); + } + void from(const kpi::IMemento& _memento) override { + auto* in = dynamic_cast(&_memento); + assert(in); + kpi::fromFolder(getFogEntries(), in->mFogEntries); + } + template void* operator=(const T& rhs) { from(rhs); return this; } +}; + +} // namespace riistudio::mk diff --git a/source/plugins/mk/BFG/io/BFG.cpp b/source/plugins/mk/BFG/io/BFG.cpp new file mode 100644 index 000000000..681a089b7 --- /dev/null +++ b/source/plugins/mk/BFG/io/BFG.cpp @@ -0,0 +1,90 @@ +#include "BFG.hpp" +#include +#include +#include +#include +#include +#include + +namespace riistudio::mk { + +struct IOContext { + std::string path; + kpi::IOTransaction& transaction; + + auto sublet(const std::string& dir) { + return IOContext(path + "/" + dir, transaction); + } + + void callback(kpi::IOMessageClass mclass, const std::string_view message) { + transaction.callback(mclass, path, message); + } + + void inform(const std::string_view message) { + callback(kpi::IOMessageClass::Information, message); + } + void warn(const std::string_view message) { + callback(kpi::IOMessageClass::Warning, message); + } + void error(const std::string_view message) { + callback(kpi::IOMessageClass::Error, message); + } + void request(bool cond, const std::string_view message) { + if (!cond) + warn(message); + } + void require(bool cond, const std::string_view message) { + if (!cond) + error(message); + } + + IOContext(std::string&& p, kpi::IOTransaction& t) + : path(std::move(p)), transaction(t) {} + IOContext(kpi::IOTransaction& t) : transaction(t) {} +}; + +void BFG::read(kpi::IOTransaction& transaction) const { + BinaryFog& fog = static_cast(transaction.node); + oishii::BinaryReader reader(std::move(transaction.data)); + IOContext ctx("bfg", transaction); + + auto entries = fog.getFogEntries(); + entries.resize(4); // Always 4 sections? + for (auto& entry : entries) { + entry.mType = static_cast(reader.read()); + entry.mStartZ = reader.read(); + entry.mEndZ = reader.read(); + entry.mColor.r = reader.read(); + entry.mColor.g = reader.read(); + entry.mColor.b = reader.read(); + entry.mColor.a = reader.read(); + entry.mEnabled = reader.read(); + entry.mCenter = reader.read(); + entry.mFadeSpeed = reader.read(); + entry.mUnk18 = reader.read(); + entry.mUnk1A = reader.read(); + } +} + +void BFG::write(kpi::INode& node, oishii::Writer& writer) const { + const BinaryFog& fog = *dynamic_cast(&node); + writer.setEndian(std::endian::big); + + auto entries = fog.getFogEntries(); + for (auto& entry : entries) { + writer.write(static_cast(entry.mType)); + writer.write(entry.mStartZ); + writer.write(entry.mEndZ); + writer.write(entry.mColor.r); + writer.write(entry.mColor.g); + writer.write(entry.mColor.b); + writer.write(entry.mColor.a); + writer.write(entry.mEnabled); + writer.write(entry.mCenter); + writer.write(entry.mFadeSpeed); + writer.write(entry.mUnk18); + writer.write(entry.mUnk1A); + } +} + +} // namespace riistudio::mk diff --git a/source/plugins/mk/BFG/io/BFG.hpp b/source/plugins/mk/BFG/io/BFG.hpp new file mode 100644 index 000000000..0df024d35 --- /dev/null +++ b/source/plugins/mk/BFG/io/BFG.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include + +namespace riistudio::mk { + +class BFG { +public: + std::string canRead(const std::string& file, + oishii::BinaryReader& reader) const { + return file.ends_with("bfg") ? typeid(BinaryFog).name() : ""; + } + + void read(kpi::IOTransaction& transaction) const; + bool canWrite(kpi::INode& node) const { + return dynamic_cast(&node) != nullptr; + } + void write(kpi::INode& node, oishii::Writer& writer) const; +}; + +} // namespace riistudio::mk