Skip to content

Commit

Permalink
Editor: added our source handlers, they got ripped out with a-gui mod…
Browse files Browse the repository at this point in the history
…ularization
  • Loading branch information
andreasdr committed Jan 22, 2025
1 parent ca2df33 commit 2d6f1c8
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 3 deletions.
25 changes: 23 additions & 2 deletions ext/a-gui/src/agui/gui/nodes/GUIImageNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ using agui::utilities::Exception;
using agui::utilities::StringTools;

int GUIImageNode::thumbnailTextureIdx = 0;
vector<unique_ptr<GUIImageNode::SourceHandler>> GUIImageNode::sourceHandlers;

GUIImageNode::GUIImageNode(
GUIScreenNode* screenNode,
Expand Down Expand Up @@ -115,6 +116,20 @@ GUIImageNode::GUIImageNode(
if (Math::abs(rotation) > Math::EPSILON) rotate(rotation);
}

void GUIImageNode::clearSourceHandlers() {
sourceHandlers.clear();
}

void GUIImageNode::addSourceHandler(SourceHandler* sourceHandler) {
sourceHandlers.push_back(unique_ptr<SourceHandler>(sourceHandler));
}

void GUIImageNode::assignTexture(GUITexture* texture, bool releaseTextureReference) {

this->texture = texture;
this->releaseTextureReference = texture != nullptr?releaseTextureReference:false;
}

void GUIImageNode::disposeTexture() {
if (texture != nullptr) {
GUI::getTextureManager()->removeTexture(texture->getId());
Expand Down Expand Up @@ -144,12 +159,18 @@ const string& GUIImageNode::getSource() {

void GUIImageNode::setSource(const string& source) {
disposeTexture();
// use source handlers
for (const auto& sourceHandler: sourceHandlers) {
if (sourceHandler->setSource(this, source) == true) {
// we handled the source with success
break;
}
}
this->source = source;
if (source.empty() == false) {
// load it
if (this->texture == nullptr) {
this->texture = source.empty() == true?nullptr:screenNode->getImage(source);
this->releaseTextureReference = false;
assignTexture(source.empty() == true?nullptr:screenNode->getImage(source), false);
}
}
this->textureId = texture == nullptr?0:GUI::getTextureManager()->addTexture(texture, 0);
Expand Down
50 changes: 49 additions & 1 deletion ext/a-gui/src/agui/gui/nodes/GUIImageNode.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <string>
#include <memory>
#include <vector>

#include <agui/agui.h>
#include <agui/gui/fileio/fwd-agui.h>
Expand All @@ -14,6 +16,8 @@
#include <agui/gui/renderer/fwd-agui.h>

using std::string;
using std::unique_ptr;
using std::vector;

// namespaces
namespace agui {
Expand All @@ -34,12 +38,32 @@ class agui::gui::nodes::GUIImageNode final
: public GUITextureBaseNode
{
friend class agui::gui::GUIParser;
public:
/**
* Source handler
*/
struct SourceHandler {
/**
* Destructor
*/
virtual ~SourceHandler() {
}

/**
* Set image source
* @param imageNode image node
* @param source source
* @returns success
*/
virtual bool setSource(GUIImageNode* imageNode, const string& source) = 0;
};

private:
bool releaseTextureReference { false };
string source;
GUITexture* texture { nullptr };
AGUI_STATIC_DLL_IMPEXT static int thumbnailTextureIdx;
AGUI_STATIC_DLL_IMPEXT static vector<unique_ptr<SourceHandler>> sourceHandlers;

/**
* Release texture
Expand Down Expand Up @@ -117,10 +141,34 @@ class agui::gui::nodes::GUIImageNode final
const string getNodeType() override;

public:
/**
* Clear source handlers
*/
static void clearSourceHandlers();

/**
* Add source handler
* @param sourceHandler source handler
*/
static void addSourceHandler(SourceHandler* sourceHandler);

/**
* Allocate thumbnail texture index
*/
inline static int allocateThumbnailTextureIdx() {
return thumbnailTextureIdx++;
}

/**
* Assign texture
* @param texture texture
* @param releaseTextureReference release texture reference on dispose
*/
void assignTexture(GUITexture* texture, bool releaseTextureReference);

// overridden methods
void dispose() override;


/**
* @returns texture
*/
Expand Down
Binary file modified resources/tests/asw/Mesh_Interaction_Table.fbx.tm
Binary file not shown.
117 changes: 117 additions & 0 deletions src/tdme/tools/editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

#include <agui/agui.h>
#include <agui/gui/GUI.h>
#include <agui/gui/fileio/PNGTextureReader.h>
#include <agui/gui/nodes/GUIImageNode.h>
#include <agui/gui/nodes/GUIScreenNode.h>

#include <tdme/tdme.h>
#include <tdme/engine/fileio/models/ModelReader.h>
#include <tdme/engine/fileio/prototypes/PrototypeReader.h>
#include <tdme/engine/prototype/Prototype.h>
#include <tdme/engine/prototype/Prototype_Type.h>
#include <tdme/engine/tools/FileSystemTools.h>
Expand All @@ -18,12 +23,15 @@
#include <tdme/engine/SimplePartition.h>
#include <tdme/engine/Version.h>
#include <tdme/minitscript/EngineMinitScript.h>
#include <tdme/os/filesystem/FileSystem.h>
#include <tdme/os/filesystem/FileSystemInterface.h>
#include <tdme/tools/editor/controllers/EditorScreenController.h>
#include <tdme/tools/editor/misc/PopUps.h>
#include <tdme/tools/editor/misc/Tools.h>
#include <tdme/tools/editor/views/EditorView.h>
#include <tdme/tools/editor/views/View.h>
#include <tdme/utilities/Console.h>
#include <tdme/utilities/Exception.h>
#include <tdme/utilities/Time.h>

using std::make_unique;
Expand All @@ -36,7 +44,12 @@ using tdme::tools::editor::Editor;
using tdme::utilities::Time;

using agui::gui::GUI;
using agui::gui::fileio::PNGTextureReader;
using agui::gui::nodes::GUIImageNode;
using agui::gui::nodes::GUIScreenNode;

using tdme::engine::fileio::models::ModelReader;
using tdme::engine::fileio::prototypes::PrototypeReader;
using tdme::engine::prototype::Prototype;
using tdme::engine::prototype::Prototype_Type;
using tdme::engine::tools::FileSystemTools;
Expand All @@ -46,12 +59,16 @@ using tdme::engine::Engine;
using tdme::engine::SimplePartition;
using tdme::engine::Version;
using tdme::minitscript::EngineMinitScript;
using tdme::os::filesystem::FileSystem;
using tdme::os::filesystem::FileSystemInterface;
using tdme::tools::editor::controllers::EditorScreenController;
using tdme::tools::editor::misc::PopUps;
using tdme::tools::editor::misc::Tools;
using tdme::tools::editor::views::EditorView;
using tdme::tools::editor::views::View;
using tdme::utilities::Console;
using tdme::utilities::Exception;
using tdme::utilities::Time;

Editor* Editor::instance = nullptr;

Expand Down Expand Up @@ -79,6 +96,106 @@ int Editor::main(int argc, char** argv)
Console::printLine();
//
EngineMinitScript::initialize();
// source handlers
{
// tm files
class TMSourceHandler: public GUIImageNode::SourceHandler {
bool setSource(GUIImageNode* imageNode, const string& source) {
if (StringTools::endsWith(StringTools::toLowerCase(source), ".tm") == false) return false;
//
try {
vector<uint8_t> thumbnailPNGData;
if (FileSystem::getInstance()->getThumbnailAttachment(
FileSystem::getInstance()->getPathName(source),
FileSystem::getInstance()->getFileName(source),
thumbnailPNGData
) == true) {
//
auto thumbnailTexture = PNGTextureReader::read("tdme.gui.guiimagenode." + to_string(GUIImageNode::allocateThumbnailTextureIdx()), thumbnailPNGData, true);
if (thumbnailTexture != nullptr) {
imageNode->assignTexture(thumbnailTexture, true);
} else {
imageNode->assignTexture(imageNode->getScreenNode()->getImage("resources/engine/images/mesh_big.png"), false);
}
} else {
imageNode->assignTexture(imageNode->getScreenNode()->getImage("resources/engine/images/mesh_big.png"), false);
}
} catch (Exception& exception) {
Console::printLine("TMSourceHandler::setSource(): " + string(exception.what()));
}
//
return true;
}
};
GUIImageNode::addSourceHandler(new TMSourceHandler());
}
{
// tm files
class TModelSourceHandler: public GUIImageNode::SourceHandler {
bool setSource(GUIImageNode* imageNode, const string& source) {
if (StringTools::endsWith(StringTools::toLowerCase(source), ".tmodel") == false) return false;
//
try {
vector<uint8_t> thumbnailPNGData;
if (PrototypeReader::readThumbnail(
FileSystem::getInstance()->getPathName(source),
FileSystem::getInstance()->getFileName(source),
thumbnailPNGData
) == true) {
//
auto thumbnailTexture = PNGTextureReader::read("tdme.gui.guiimagenode." + to_string(GUIImageNode::allocateThumbnailTextureIdx()), thumbnailPNGData, true);
if (thumbnailTexture != nullptr) {
imageNode->assignTexture(thumbnailTexture, true);
} else {
imageNode->assignTexture(imageNode->getScreenNode()->getImage("resources/engine/images/tdme_big.png"), false);
}
} else {
imageNode->assignTexture(imageNode->getScreenNode()->getImage("resources/engine/images/tdme_big.png"), false);
}
} catch (Exception& exception) {
Console::printLine("TModelSourceHandler::setSource(): " + string(exception.what()));
}
//
return true;
}
};
GUIImageNode::addSourceHandler(new TModelSourceHandler());
}
{
// tm files
class XMLSourceHandler: public GUIImageNode::SourceHandler {
bool setSource(GUIImageNode* imageNode, const string& source) {
if (StringTools::endsWith(StringTools::toLowerCase(source), ".xml") == false) return false;
//
try {
imageNode->assignTexture(imageNode->getScreenNode()->getImage("resources/engine/images/gui_big.png"), false);
} catch (Exception& exception) {
Console::printLine("XMLSourceHandler::setSource(): " + string(exception.what()));
}
//
return true;
}
};
GUIImageNode::addSourceHandler(new XMLSourceHandler());
}
{
// other files
class OtherSourceHandler: public GUIImageNode::SourceHandler {
bool setSource(GUIImageNode* imageNode, const string& source) {
// other model without thumbnail
for (const auto& extension: ModelReader::getModelExtensions()) {
if (StringTools::endsWith(StringTools::toLowerCase(source), "." + extension) == true) {
imageNode->assignTexture(imageNode->getScreenNode()->getImage("resources/engine/images/mesh_big.png"), false);
// done
return true;
}
}
//
return false;
}
};
GUIImageNode::addSourceHandler(new OtherSourceHandler());
}
//
auto tdmeEditor = new Editor();
return tdmeEditor->run(argc, argv, "Editor", nullptr, Application::WINDOW_HINT_MAXIMIZED);
Expand Down

0 comments on commit 2d6f1c8

Please sign in to comment.