Skip to content

Commit

Permalink
Handle custom objects at runtime (#4294)
Browse files Browse the repository at this point in the history
* Don't show in changelog
  • Loading branch information
D8H authored Oct 18, 2022
1 parent 6b08fec commit 80cb6d6
Show file tree
Hide file tree
Showing 116 changed files with 5,688 additions and 1,902 deletions.
29 changes: 26 additions & 3 deletions Core/GDCore/IDE/ProjectStripper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
*/
#include "ProjectStripper.h"

#include "GDCore/Project/EventsFunctionsContainer.h"
#include "GDCore/Project/EventsFunctionsExtension.h"
#include "GDCore/Project/ExternalEvents.h"
#include "GDCore/Project/ExternalLayout.h"
#include "GDCore/Project/Layout.h"
#include "GDCore/Project/Project.h"

namespace gd {

void GD_CORE_API ProjectStripper::StripProjectForExport(gd::Project& project) {
void GD_CORE_API ProjectStripper::StripProjectForExport(gd::Project &project) {
project.GetObjectGroups().Clear();
while (project.GetExternalEventsCount() > 0)
project.RemoveExternalEvents(project.GetExternalEvents(0).GetName());
Expand All @@ -22,7 +24,28 @@ void GD_CORE_API ProjectStripper::StripProjectForExport(gd::Project& project) {
project.GetLayout(i).GetEvents().Clear();
}

project.ClearEventsFunctionsExtensions();
// Keep the EventsBasedObject object list because it's useful for the Runtime
// to create the child-object.
for (unsigned int extensionIndex = 0;
extensionIndex < project.GetEventsFunctionsExtensionsCount();
++extensionIndex) {
auto &extension = project.GetEventsFunctionsExtension(extensionIndex);
auto &eventsBasedObjects = extension.GetEventsBasedObjects();
if (eventsBasedObjects.size() == 0) {
project.RemoveEventsFunctionsExtension(extension.GetName());
extensionIndex--;
continue;
}
for (unsigned int objectIndex = 0; objectIndex < eventsBasedObjects.size();
++objectIndex) {
auto &eventsBasedObject = eventsBasedObjects.at(objectIndex);
eventsBasedObject.SetFullName("");
eventsBasedObject.SetDescription("");
eventsBasedObject.GetEventsFunctions().GetInternalVector().clear();
eventsBasedObject.GetPropertyDescriptors().GetInternalVector().clear();
}
extension.GetEventsBasedBehaviors().Clear();
}
}

} // namespace gd
} // namespace gd
2 changes: 1 addition & 1 deletion Core/GDCore/Project/CustomObjectConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gd::ObjectConfiguration &CustomObjectConfiguration::GetChildObjectConfiguration(
if (configurationPosition == childObjectConfigurations.end()) {
childObjectConfigurations.insert(std::make_pair(
objectName,
project->CreateObjectConfiguration(childObject.GetType())));
childObject.GetConfiguration().Clone()));
return *(childObjectConfigurations[objectName]);
}
else {
Expand Down
2 changes: 2 additions & 0 deletions Core/tests/WholeProjectRefactorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "GDCore/Project/Variable.h"
#include "catch.hpp"

// TODO EBO Add a test where a child is removed form the EventsBasedObject
// and check the configuration still gives access to other child configuration.
namespace {

const gd::StandardEvent &EnsureStandardEvent(const gd::BaseEvent &baseEvent) {
Expand Down
70 changes: 48 additions & 22 deletions Extensions/AnchorBehavior/anchorruntimebehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ namespace gdjs {
_bottomEdgeDistance: number = 0;
_useLegacyBottomAndRightAnchors: boolean = false;

constructor(runtimeScene, behaviorData, owner) {
super(runtimeScene, behaviorData, owner);
constructor(
instanceContainer: gdjs.RuntimeInstanceContainer,
behaviorData,
owner: gdjs.RuntimeObject
) {
super(instanceContainer, behaviorData, owner);
this._relativeToOriginalWindowSize = !!behaviorData.relativeToOriginalWindowSize;
this._leftEdgeAnchor = behaviorData.leftEdgeAnchor;
this._rightEdgeAnchor = behaviorData.rightEdgeAnchor;
Expand Down Expand Up @@ -65,11 +69,15 @@ namespace gdjs {
this._invalidDistances = true;
}

doStepPreEvents(runtimeScene) {
const game = runtimeScene.getGame();
doStepPreEvents(instanceContainer: gdjs.RuntimeInstanceContainer) {
const workingPoint: FloatPoint = gdjs.staticArray(
gdjs.AnchorRuntimeBehavior.prototype.doStepPreEvents
) as FloatPoint;
// TODO EBO Make it work with event based objects or hide this behavior for them.
const game = instanceContainer.getGame();
let rendererWidth = game.getGameResolutionWidth();
let rendererHeight = game.getGameResolutionHeight();
const layer = runtimeScene.getLayer(this.owner.getLayer());
const layer = instanceContainer.getLayer(this.owner.getLayer());
if (this._invalidDistances) {
if (this._relativeToOriginalWindowSize) {
rendererWidth = game.getOriginalWidth();
Expand All @@ -79,7 +87,9 @@ namespace gdjs {
//Calculate the distances from the window's bounds.
const topLeftPixel = layer.convertCoords(
this.owner.getDrawableX(),
this.owner.getDrawableY()
this.owner.getDrawableY(),
0,
workingPoint
);

//Left edge
Expand Down Expand Up @@ -125,9 +135,12 @@ namespace gdjs {
}
}
}
// It's fine to reuse workingPoint as topLeftPixel is no longer used.
const bottomRightPixel = layer.convertCoords(
this.owner.getDrawableX() + this.owner.getWidth(),
this.owner.getDrawableY() + this.owner.getHeight()
this.owner.getDrawableY() + this.owner.getHeight(),
0,
workingPoint
);

//Right edge
Expand Down Expand Up @@ -268,11 +281,24 @@ namespace gdjs {
}
}
}
const topLeftCoord = layer.convertInverseCoords(leftPixel, topPixel);
// It's fine to reuse workingPoint as topLeftPixel is no longer used.
const topLeftCoord = layer.convertInverseCoords(
leftPixel,
topPixel,
0,
workingPoint
);
const left = topLeftCoord[0];
const top = topLeftCoord[1];

const bottomRightCoord = layer.convertInverseCoords(
rightPixel,
bottomPixel
bottomPixel,
0,
workingPoint
);
const right = bottomRightCoord[0];
const bottom = bottomRightCoord[1];

// Compatibility with GD <= 5.0.133
if (this._useLegacyBottomAndRightAnchors) {
Expand All @@ -281,25 +307,25 @@ namespace gdjs {
this._rightEdgeAnchor !==
AnchorRuntimeBehavior.HorizontalAnchor.NONE
) {
this.owner.setWidth(bottomRightCoord[0] - topLeftCoord[0]);
this.owner.setWidth(right - left);
}
if (
this._bottomEdgeAnchor !== AnchorRuntimeBehavior.VerticalAnchor.NONE
) {
this.owner.setHeight(bottomRightCoord[1] - topLeftCoord[1]);
this.owner.setHeight(bottom - top);
}
if (
this._leftEdgeAnchor !== AnchorRuntimeBehavior.HorizontalAnchor.NONE
) {
this.owner.setX(
topLeftCoord[0] + this.owner.getX() - this.owner.getDrawableX()
left + this.owner.getX() - this.owner.getDrawableX()
);
}
if (
this._topEdgeAnchor !== AnchorRuntimeBehavior.VerticalAnchor.NONE
) {
this.owner.setY(
topLeftCoord[1] + this.owner.getY() - this.owner.getDrawableY()
top + this.owner.getY() - this.owner.getDrawableY()
);
}
}
Expand All @@ -311,23 +337,23 @@ namespace gdjs {
AnchorRuntimeBehavior.HorizontalAnchor.NONE &&
this._leftEdgeAnchor !== AnchorRuntimeBehavior.HorizontalAnchor.NONE
) {
this.owner.setWidth(bottomRightCoord[0] - topLeftCoord[0]);
this.owner.setX(topLeftCoord[0]);
this.owner.setWidth(right - left);
this.owner.setX(left);
} else {
if (
this._leftEdgeAnchor !==
AnchorRuntimeBehavior.HorizontalAnchor.NONE
) {
this.owner.setX(
topLeftCoord[0] + this.owner.getX() - this.owner.getDrawableX()
left + this.owner.getX() - this.owner.getDrawableX()
);
}
if (
this._rightEdgeAnchor !==
AnchorRuntimeBehavior.HorizontalAnchor.NONE
) {
this.owner.setX(
bottomRightCoord[0] +
right +
this.owner.getX() -
this.owner.getDrawableX() -
this.owner.getWidth()
Expand All @@ -340,22 +366,22 @@ namespace gdjs {
AnchorRuntimeBehavior.VerticalAnchor.NONE &&
this._topEdgeAnchor !== AnchorRuntimeBehavior.VerticalAnchor.NONE
) {
this.owner.setHeight(bottomRightCoord[1] - topLeftCoord[1]);
this.owner.setY(topLeftCoord[1]);
this.owner.setHeight(bottom - top);
this.owner.setY(top);
} else {
if (
this._topEdgeAnchor !== AnchorRuntimeBehavior.VerticalAnchor.NONE
) {
this.owner.setY(
topLeftCoord[1] + this.owner.getY() - this.owner.getDrawableY()
top + this.owner.getY() - this.owner.getDrawableY()
);
}
if (
this._bottomEdgeAnchor !==
AnchorRuntimeBehavior.VerticalAnchor.NONE
) {
this.owner.setY(
bottomRightCoord[1] +
bottom +
this.owner.getY() -
this.owner.getDrawableY() -
this.owner.getHeight()
Expand All @@ -366,7 +392,7 @@ namespace gdjs {
}
}

doStepPostEvents(runtimeScene) {}
doStepPostEvents(instanceContainer: gdjs.RuntimeInstanceContainer) {}

static HorizontalAnchor = {
NONE: 0,
Expand Down
11 changes: 6 additions & 5 deletions Extensions/BBText/bbtextruntimeobject-pixi-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ namespace gdjs {

/**
* @param runtimeObject The object to render
* @param runtimeScene The gdjs.RuntimeScene in which the object is
* @param instanceContainer The gdjs.RuntimeInstanceContainer in which the object is
*/
constructor(
runtimeObject: gdjs.BBTextRuntimeObject,
runtimeScene: gdjs.RuntimeScene
instanceContainer: gdjs.RuntimeInstanceContainer
) {
this._object = runtimeObject;

// Load (or reset) the text
if (this._pixiObject === undefined) {
this._pixiObject = new MultiStyleText(runtimeObject._text, {
default: {
fontFamily: runtimeScene
fontFamily: instanceContainer
.getGame()
.getFontManager()
.getFontFamily(runtimeObject._fontFamily),
Expand All @@ -44,7 +44,7 @@ namespace gdjs {
this.updateFontFamily();
this.updateFontSize();
}
runtimeScene
instanceContainer
.getLayer('')
.getRenderer()
.addRendererObject(this._pixiObject, runtimeObject.getZOrder());
Expand Down Expand Up @@ -95,7 +95,8 @@ namespace gdjs {
}

updateFontFamily(): void {
this._pixiObject.textStyles.default.fontFamily = this._object._runtimeScene
this._pixiObject.textStyles.default.fontFamily = this._object
.getInstanceContainer()
.getGame()
.getFontManager()
.getFontFamily(this._object._fontFamily);
Expand Down
22 changes: 14 additions & 8 deletions Extensions/BBText/bbtextruntimeobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ namespace gdjs {
hidden: boolean;

/**
* @param runtimeScene The scene the object belongs to.
* @param instanceContainer The container the object belongs to.
* @param objectData The object data used to initialize the object
*/
constructor(runtimeScene: gdjs.RuntimeScene, objectData: BBTextObjectData) {
super(runtimeScene, objectData);
constructor(
instanceContainer: gdjs.RuntimeInstanceContainer,
objectData: BBTextObjectData
) {
super(instanceContainer, objectData);
// @ts-ignore - parseFloat should not be required, but GDevelop 5.0 beta 92 and below were storing it as a string.
this._opacity = parseFloat(objectData.content.opacity);
this._text = objectData.content.text;
Expand All @@ -62,7 +65,10 @@ namespace gdjs {
this._fontSize = parseFloat(objectData.content.fontSize);
this._wordWrap = objectData.content.wordWrap;
this._align = objectData.content.align;
this._renderer = new gdjs.BBTextRuntimeObjectRenderer(this, runtimeScene);
this._renderer = new gdjs.BBTextRuntimeObjectRenderer(
this,
instanceContainer
);
this.hidden = !objectData.content.visible;

// *ALWAYS* call `this.onCreated()` at the very end of your object constructor.
Expand Down Expand Up @@ -122,8 +128,8 @@ namespace gdjs {
}
}

onDestroyFromScene(runtimeScene): void {
super.onDestroyFromScene(runtimeScene);
onDestroyFromScene(instanceContainer: gdjs.RuntimeInstanceContainer): void {
super.onDestroyFromScene(instanceContainer);
}

/**
Expand Down Expand Up @@ -239,7 +245,7 @@ namespace gdjs {

this._wrappingWidth = width;
this._renderer.updateWrappingWidth();
this.hitBoxesDirty = true;
this.invalidateHitboxes();
}

/**
Expand All @@ -254,7 +260,7 @@ namespace gdjs {

this._wordWrap = wordWrap;
this._renderer.updateWordWrap();
this.hitBoxesDirty = true;
this.invalidateHitboxes();
}

getWordWrap() {
Expand Down
Loading

0 comments on commit 80cb6d6

Please sign in to comment.