Add alternative way to call AddObject on an extension using a blueprint object

Also remove dead code
This commit is contained in:
Florian Rival
2018-07-01 22:52:18 +01:00
parent 373c55ddc5
commit 97ced4145f
6 changed files with 104 additions and 182 deletions

View File

@@ -8,6 +8,7 @@
#include <iostream>
#include "GDCore/Extensions/Metadata/ExpressionMetadata.h"
#include "GDCore/Extensions/Metadata/InstructionMetadata.h"
#include "GDCore/Project/Object.h"
#if defined(GD_IDE_ONLY) && !defined(GD_NO_WX_GUI)
#include <wx/bitmap.h>
#include <wx/file.h>
@@ -16,17 +17,52 @@
namespace gd {
ObjectMetadata::ObjectMetadata(const gd::String& extensionNamespace_,
const gd::String& name_,
const gd::String& fullname_,
const gd::String& description_,
const gd::String& icon24x24,
std::shared_ptr<gd::Object> blueprintObject_)
: extensionNamespace(extensionNamespace_),
blueprintObject(blueprintObject_) {
name = name_;
#if defined(GD_IDE_ONLY)
SetFullName(gd::String(fullname_));
SetDescription(gd::String(description_));
iconFilename = icon24x24;
#if !defined(GD_NO_WX_GUI)
if (!iconFilename.empty()) {
if (gd::SkinHelper::IconExists(icon24x24, 24))
SetBitmapIcon(gd::SkinHelper::GetIcon(icon24x24, 24));
else if (wxFile::Exists(icon24x24))
SetBitmapIcon(wxBitmap(icon24x24, wxBITMAP_TYPE_ANY));
else {
std::cout << "Warning: The icon file for object \"" << name_
<< " was not found in the current skin icons"
<< " and the specified name is not an existing filename.";
SetBitmapIcon(wxBitmap(24, 24));
}
}
#endif
#endif
createFunPtr = [this](gd::String name) -> std::unique_ptr<gd::Object> {
std::unique_ptr<gd::Object> newObject = blueprintObject->Clone();
newObject->SetName(name);
return newObject;
};
}
ObjectMetadata::ObjectMetadata(const gd::String& extensionNamespace_,
const gd::String& name_,
const gd::String& fullname_,
const gd::String& informations_,
const gd::String& description_,
const gd::String& icon24x24,
CreateFunPtr createFunPtrP)
: extensionNamespace(extensionNamespace_) {
name = name_;
#if defined(GD_IDE_ONLY)
SetFullName(gd::String(fullname_));
SetDescription(gd::String(informations_));
SetDescription(gd::String(description_));
iconFilename = icon24x24;
#if !defined(GD_NO_WX_GUI)
if (!iconFilename.empty()) {

View File

@@ -7,24 +7,21 @@
#define OBJECTMETADATA_H
#include <map>
#include <memory>
#include <functional>
#include "GDCore/Extensions/Metadata/ExpressionMetadata.h"
#include "GDCore/Extensions/Metadata/InstructionMetadata.h"
#include "GDCore/Project/Object.h"
#include "GDCore/String.h"
#if defined(GD_IDE_ONLY) && !defined(GD_NO_WX_GUI)
#include <wx/bitmap.h>
#endif
namespace gd {
class Object;
}
namespace gd {
class InstructionMetadata;
}
namespace gd {
class ExpressionMetadata;
}
} // namespace gd
class wxBitmap;
typedef std::unique_ptr<gd::Object> (*CreateFunPtr)(gd::String name);
typedef std::function<std::unique_ptr<gd::Object>(gd::String name)> CreateFunPtr;
namespace gd {
@@ -36,10 +33,25 @@ namespace gd {
*/
class GD_CORE_API ObjectMetadata {
public:
/**
* \brief Construct an object metadata, using a "blueprint" object that will
* be copied when a new object is asked.
*/
ObjectMetadata(const gd::String& extensionNamespace_,
const gd::String& name_,
const gd::String& fullname_,
const gd::String& informations_,
const gd::String& description_,
const gd::String& icon24x24_,
std::shared_ptr<gd::Object> blueprintObject_);
/**
* \brief Construct an object metadata, with a function that will be called
* to instanciate a new object.
*/
ObjectMetadata(const gd::String& extensionNamespace_,
const gd::String& name_,
const gd::String& fullname_,
const gd::String& description_,
const gd::String& icon24x24_,
CreateFunPtr createFunPtrP);
ObjectMetadata() : createFunPtr(NULL) {}
@@ -157,6 +169,10 @@ class GD_CORE_API ObjectMetadata {
wxBitmap icon;
#endif
#endif
std::shared_ptr<gd::Object>
blueprintObject; ///< The "blueprint" object to be copied when a new
///< object is asked. Can be null in case a creation
///< function is passed.
};
} // namespace gd

View File

@@ -27,7 +27,7 @@ class LayoutEditorCanvas;
class ProjectExporter;
} // namespace gd
typedef std::unique_ptr<gd::Object> (*CreateFunPtr)(gd::String name);
typedef std::function<std::unique_ptr<gd::Object>(gd::String name)> CreateFunPtr;
#undef CreateEvent

View File

@@ -116,6 +116,25 @@ gd::ExpressionMetadata& PlatformExtension::AddStrExpression(
#endif
}
gd::ObjectMetadata& PlatformExtension::AddObject(
const gd::String& name,
const gd::String& fullname,
const gd::String& description,
const gd::String& icon24x24,
std::shared_ptr<gd::Object> instance) {
gd::String nameWithNamespace =
GetNameSpace().empty() ? name : GetNameSpace() + name;
objectsInfos[nameWithNamespace] =
ObjectMetadata(GetNameSpace(),
nameWithNamespace,
fullname,
description,
icon24x24,
instance);
return objectsInfos[nameWithNamespace];
}
gd::BehaviorMetadata& PlatformExtension::AddBehavior(
const gd::String& name,
const gd::String& fullname,
@@ -399,131 +418,6 @@ bool PlatformExtension::IsBuiltin() const {
}
#if defined(GD_IDE_ONLY)
void PlatformExtension::CloneExtension(
const gd::String& platformName,
const gd::String& extensionName,
bool stripFunctionsNameAndCodeGeneration) {
gd::Platform* platform =
gd::PlatformManager::Get()->GetPlatform(platformName);
if (!platform) {
std::cout << "Unable to clone extension \"" << extensionName << "\" from "
<< platformName << ": This platform doesn't exist.";
return;
}
std::shared_ptr<gd::PlatformExtension> extension =
platform->GetExtension(extensionName);
if (!extension) {
std::cout << "Unable to clone extension \"" << extensionName << "\" from "
<< platformName << ": This extension doesn't exist.";
return;
}
*this = *extension;
if (stripFunctionsNameAndCodeGeneration) {
for (std::map<gd::String, gd::InstructionMetadata>::iterator it =
GetAllActions().begin();
it != GetAllActions().end();
++it)
it->second.SetFunctionName("").SetGetter("").RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::InstructionMetadata>::iterator it =
GetAllConditions().begin();
it != GetAllConditions().end();
++it)
it->second.SetFunctionName("").SetGetter("").RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::ExpressionMetadata>::iterator it =
GetAllExpressions().begin();
it != GetAllExpressions().end();
++it)
it->second.SetFunctionName("").RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::ExpressionMetadata>::iterator it =
GetAllStrExpressions().begin();
it != GetAllStrExpressions().end();
++it)
it->second.SetFunctionName("").RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::ObjectMetadata>::iterator objIt =
objectsInfos.begin();
objIt != objectsInfos.end();
++objIt) {
gd::ObjectMetadata& obj = objIt->second;
for (std::map<gd::String, gd::InstructionMetadata>::iterator it =
obj.actionsInfos.begin();
it != obj.actionsInfos.end();
++it)
it->second.SetFunctionName("")
.SetGetter("")
.RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::InstructionMetadata>::iterator it =
obj.conditionsInfos.begin();
it != obj.conditionsInfos.end();
++it)
it->second.SetFunctionName("")
.SetGetter("")
.RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::ExpressionMetadata>::iterator it =
obj.expressionsInfos.begin();
it != obj.expressionsInfos.end();
++it)
it->second.SetFunctionName("").RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::ExpressionMetadata>::iterator it =
obj.strExpressionsInfos.begin();
it != obj.strExpressionsInfos.end();
++it)
it->second.SetFunctionName("").RemoveCustomCodeGenerator();
}
for (std::map<gd::String, gd::BehaviorMetadata>::iterator objIt =
behaviorsInfo.begin();
objIt != behaviorsInfo.end();
++objIt) {
gd::BehaviorMetadata& obj = objIt->second;
for (std::map<gd::String, gd::InstructionMetadata>::iterator it =
obj.actionsInfos.begin();
it != obj.actionsInfos.end();
++it)
it->second.SetFunctionName("")
.SetGetter("")
.RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::InstructionMetadata>::iterator it =
obj.conditionsInfos.begin();
it != obj.conditionsInfos.end();
++it)
it->second.SetFunctionName("")
.SetGetter("")
.RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::ExpressionMetadata>::iterator it =
obj.expressionsInfos.begin();
it != obj.expressionsInfos.end();
++it)
it->second.SetFunctionName("").RemoveCustomCodeGenerator();
for (std::map<gd::String, gd::ExpressionMetadata>::iterator it =
obj.strExpressionsInfos.begin();
it != obj.strExpressionsInfos.end();
++it)
it->second.SetFunctionName("").RemoveCustomCodeGenerator();
}
for (std::map<gd::String, gd::EventMetadata>::iterator it =
eventsInfos.begin();
it != eventsInfos.end();
++it)
it->second.ClearCodeGenerationAndPreprocessing();
}
}
void PlatformExtension::StripUnimplementedInstructionsAndExpressions() {
for (std::map<gd::String, gd::InstructionMetadata>::iterator it =
GetAllActions().begin();

View File

@@ -19,42 +19,20 @@
namespace gd {
class Instruction;
}
namespace gd {
class InstructionMetadata;
}
namespace gd {
class ExpressionMetadata;
}
namespace gd {
class ObjectMetadata;
}
namespace gd {
class BehaviorMetadata;
}
namespace gd {
class BaseEvent;
}
namespace gd {
class EventMetadata;
}
namespace gd {
class EventCodeGenerator;
}
namespace gd {
class ArbitraryResourceWorker;
}
namespace gd {
class BehaviorsSharedData;
}
namespace gd {
class Behavior;
}
namespace gd {
class Object;
}
typedef std::unique_ptr<gd::Object> (*CreateFunPtr)(gd::String name);
typedef std::function<std::unique_ptr<gd::Object>(gd::String name)> CreateFunPtr;
namespace gd {
@@ -171,7 +149,6 @@ class GD_CORE_API PlatformExtension {
* \param icon The 24x24 icon of the object:
res/icons_[SkinName]/[iconName]24.png will be first tried,
* and then if it does not exists, the full entered name will be tried.
\endcode
*/
template <class T>
gd::ObjectMetadata& AddObject(const gd::String& name_,
@@ -179,6 +156,23 @@ class GD_CORE_API PlatformExtension {
const gd::String& description_,
const gd::String& icon24x24_);
/**
* \brief Declare a new object as being part of the extension.
* \note This method does nothing when used for GD C++ runtime.
* \param name The name of the object
* \param fullname The user friendly name of the object
* \param description The user friendly description of the object
* \param icon The 24x24 icon of the object:
res/icons_[SkinName]/[iconName]24.png will be first tried,
* and then if it does not exists, the full entered name will be tried.
* \param instance The "blueprint" object to be copied when a new object is asked for.
*/
gd::ObjectMetadata& AddObject(const gd::String& name_,
const gd::String& fullname_,
const gd::String& description_,
const gd::String& icon24x24_,
std::shared_ptr<gd::Object> instance);
/**
* \brief Declare a new behavior as being part of the extension.
* \note This method does nothing when used for GD C++ runtime.
@@ -404,24 +398,6 @@ class GD_CORE_API PlatformExtension {
virtual void ExposeActionsResources(Instruction& action,
gd::ArbitraryResourceWorker& worker){};
/**
* \brief Clone the extension of another platform.
*
* This can be used when you want a platform to conform to another.<br>
* It is still possible to make some changes after the cloning by using getter
* methods.<br> See also
* gd::PlatformExtension::StripUnimplementedInstructionsAndExpressions.
*
* \param platformName The name of the platform in which the source extension
* must be searched for. \param extensionName The name of the source extension
* to be copied. \param stripFunctionsNameAndCodeGeneration If set to true,
* all functions names and code generator of all
* instructions/expression/events will be removed.
*/
void CloneExtension(const gd::String& platformName,
const gd::String& extensionName,
bool stripFunctionsNameAndCodeGeneration = true);
/**
* \brief Delete all instructions having no functions name or custom code
* generator.

View File

@@ -1,13 +1,13 @@
/**
* This is a declaration of an extension for GDevelop 5.
*
*
* Run `node import-GDJS-Runtime.js` (in newIDE/app/scripts) if you make any change
* to this extension file or to any other *.js file that you reference inside.
*
* The file must be named "JsExtension.js", otherwise GDevelop won't load it.
*
* The file must be named "JsExtension.js", otherwise GDevelop won't load it.
* ⚠️ If you make a change and the extension is not loaded, open the developer console
* and search for any errors.
*
*
* More information on https://github.com/4ian/GD/blob/master/newIDE/README-extensions.md
*/
module.exports = {