Skip to content

Commit

Permalink
Revert "[plugins] Remove old BFG editor"
Browse files Browse the repository at this point in the history
This reverts commit 10d9ceb.
  • Loading branch information
riidefi committed Jan 28, 2022
1 parent 53139ef commit 4c4c98a
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 1 deletion.
7 changes: 6 additions & 1 deletion source/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
18 changes: 18 additions & 0 deletions source/plugins/mk/BFG/Fog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <core/common.h>
#include <plugins/mk/BFG/FogEntry.hpp>

namespace riistudio::mk {

class BinaryFogData {
public:
bool operator==(const BinaryFogData&) const = default;
};

struct NullClass {};

} // namespace riistudio::mk

#include <core/kpi/Node2.hpp>
#include <plugins/mk/BFG/Node.h>
40 changes: 40 additions & 0 deletions source/plugins/mk/BFG/FogEntry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <core/common.h>
#include <plugins/gc/Export/Material.hpp>

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
78 changes: 78 additions & 0 deletions source/plugins/mk/BFG/FogView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <core/kpi/Plugins.hpp>
#include <core/kpi/PropertyView.hpp>
#include <core/kpi/RichNameManager.hpp>
#include <core/util/gui.hpp>
#include <llvm/Support/Casting.h>
#include <plugins/gc/Export/Material.hpp>
#include <plugins/mk/BFG/Fog.hpp>
#include <plugins/mk/BFG/io/BFG.hpp>
#include <vendor/fa5/IconsFontAwesome5.h>

namespace riistudio::mk {

using namespace llvm;

namespace ui {

auto fogEntry =
kpi::StatelessPropertyView<FogEntry>()
.setTitle("Properties")
.setIcon((const char*)ICON_FA_COGS)
.onDraw([](kpi::PropertyDelegate<FogEntry>& delegate) {
auto& fog = delegate.getActive();
int orthoOrPersp =
static_cast<int>(fog.mType) >=
static_cast<int>(FogType::OrthographicLinear)
? 0
: 1;
int fogType = static_cast<int>(fog.mType);
if (fogType >= static_cast<int>(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<FogType>(new_type));

bool enabled = fog.mEnabled;
ImGui::Checkbox("Fog Enabled", &enabled);
KPI_PROPERTY_EX(delegate, mEnabled, static_cast<u16>(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", &center);
KPI_PROPERTY_EX(delegate, mCenter, static_cast<u16>(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<BinaryFog>();
kpi::RichNameManager::getInstance().addRichName<mk::FogEntry>(
(const char*)ICON_FA_CLOUD, "Fog Entry", (const char*)ICON_FA_CLOUD,
"Fog Entries");

installer.addDeserializer<BFG>();
installer.addSerializer<BFG>();
});
} // namespace riistudio::mk
88 changes: 88 additions & 0 deletions source/plugins/mk/BFG/Node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// This is a generated file

namespace riistudio::mk {

class BinaryFog : public mk::NullClass, public kpi::TDocData<mk::BinaryFogData>, public kpi::INode {
public:
BinaryFog() = default;
virtual ~BinaryFog() = default;

kpi::MutCollectionRange<FogEntry> getFogEntries() { return { &mFogEntries }; }
kpi::ConstCollectionRange<FogEntry> getFogEntries() const { return { &mFogEntries }; }

protected:
kpi::ICollection* v_getFogEntries() const { return const_cast<kpi::ICollection*>(static_cast<const kpi::ICollection*>(&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<FogEntry> 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<kpi::TDocData<mk::BinaryFogData>*>(this); }
virtual const kpi::IDocData* getImmediateData() const { return static_cast<const kpi::TDocData<mk::BinaryFogData>*>(this); }

public:
struct _Memento : public kpi::IMemento {
kpi::ConstPersistentVec<FogEntry> mFogEntries;
template<typename M> _Memento(const M& _new, const kpi::IMemento* last=nullptr) {
const auto* old = last ? dynamic_cast<const _Memento*>(last) : nullptr;
kpi::nextFolder(this->mFogEntries, _new.getFogEntries(), old ? &old->mFogEntries : nullptr);
}
};
std::unique_ptr<kpi::IMemento> 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<const _Memento*>(&_memento);
assert(in);
kpi::fromFolder(getFogEntries(), in->mFogEntries);
}
template<typename T> void* operator=(const T& rhs) { from(rhs); return this; }
};

} // namespace riistudio::mk
90 changes: 90 additions & 0 deletions source/plugins/mk/BFG/io/BFG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "BFG.hpp"
#include <core/util/glm_io.hpp>
#include <functional>
#include <llvm/ADT/StringMap.h>
#include <numeric>
#include <oishii/writer/binary_writer.hxx>
#include <sstream>

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<BinaryFog&>(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<FogType>(reader.read<u32>());
entry.mStartZ = reader.read<f32>();
entry.mEndZ = reader.read<f32>();
entry.mColor.r = reader.read<u8>();
entry.mColor.g = reader.read<u8>();
entry.mColor.b = reader.read<u8>();
entry.mColor.a = reader.read<u8>();
entry.mEnabled = reader.read<u16>();
entry.mCenter = reader.read<u16>();
entry.mFadeSpeed = reader.read<f32>();
entry.mUnk18 = reader.read<u16>();
entry.mUnk1A = reader.read<u16>();
}
}

void BFG::write(kpi::INode& node, oishii::Writer& writer) const {
const BinaryFog& fog = *dynamic_cast<BinaryFog*>(&node);
writer.setEndian(std::endian::big);

auto entries = fog.getFogEntries();
for (auto& entry : entries) {
writer.write<u32>(static_cast<u32>(entry.mType));
writer.write<f32>(entry.mStartZ);
writer.write<f32>(entry.mEndZ);
writer.write<u8>(entry.mColor.r);
writer.write<u8>(entry.mColor.g);
writer.write<u8>(entry.mColor.b);
writer.write<u8>(entry.mColor.a);
writer.write<u16>(entry.mEnabled);
writer.write<u16>(entry.mCenter);
writer.write<f32>(entry.mFadeSpeed);
writer.write<u16>(entry.mUnk18);
writer.write<u16>(entry.mUnk1A);
}
}

} // namespace riistudio::mk
24 changes: 24 additions & 0 deletions source/plugins/mk/BFG/io/BFG.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <core/kpi/Plugins.hpp>
#include <oishii/reader/binary_reader.hxx>
#include <plugins/mk/BFG/Fog.hpp>
#include <string>

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<BinaryFog*>(&node) != nullptr;
}
void write(kpi::INode& node, oishii::Writer& writer) const;
};

} // namespace riistudio::mk

0 comments on commit 4c4c98a

Please sign in to comment.