Skip to content

Commit

Permalink
Allow changing Opacity & Flip directly in Instance Properties Panel (#…
Browse files Browse the repository at this point in the history
…6935)

* This allows playing around with instances directly on the canvas instead of relying on the actions to flip or change opacity and needing to start a preview
  • Loading branch information
ClementPasteau authored Sep 13, 2024
1 parent 0ca26a8 commit dc45f3d
Show file tree
Hide file tree
Showing 47 changed files with 969 additions and 134 deletions.
23 changes: 18 additions & 5 deletions Core/GDCore/Project/InitialInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ InitialInstance::InitialInstance()
rotationX(0),
rotationY(0),
zOrder(0),
opacity(255),
layer(""),
flippedX(false),
flippedY(false),
flippedZ(false),
customSize(false),
customDepth(false),
width(0),
Expand Down Expand Up @@ -57,7 +61,11 @@ void InitialInstance::UnserializeFrom(const SerializerElement& element) {
SetHasCustomDepth(false);
}
SetZOrder(element.GetIntAttribute("zOrder", 0, "plan"));
SetOpacity(element.GetIntAttribute("opacity", 255));
SetLayer(element.GetStringAttribute("layer"));
SetFlippedX(element.GetBoolAttribute("flippedX", false));
SetFlippedY(element.GetBoolAttribute("flippedY", false));
SetFlippedZ(element.GetBoolAttribute("flippedZ", false));
SetLocked(element.GetBoolAttribute("locked", false));
SetSealed(element.GetBoolAttribute("sealed", false));
SetShouldKeepRatio(element.GetBoolAttribute("keepRatio", false));
Expand Down Expand Up @@ -113,6 +121,10 @@ void InitialInstance::SerializeTo(SerializerElement& element) const {
element.SetAttribute("y", GetY());
if (GetZ() != 0) element.SetAttribute("z", GetZ());
element.SetAttribute("zOrder", GetZOrder());
if (GetOpacity() != 255) element.SetAttribute("opacity", GetOpacity());
if (IsFlippedX()) element.SetAttribute("flippedX", IsFlippedX());
if (IsFlippedY()) element.SetAttribute("flippedY", IsFlippedY());
if (IsFlippedZ()) element.SetAttribute("flippedZ", IsFlippedZ());
element.SetAttribute("layer", GetLayer());
element.SetAttribute("angle", GetAngle());
if (GetRotationX() != 0) element.SetAttribute("rotationX", GetRotationX());
Expand Down Expand Up @@ -155,8 +167,8 @@ InitialInstance& InitialInstance::ResetPersistentUuid() {

std::map<gd::String, gd::PropertyDescriptor>
InitialInstance::GetCustomProperties(
gd::ObjectsContainer &globalObjectsContainer,
gd::ObjectsContainer &objectsContainer) {
gd::ObjectsContainer& globalObjectsContainer,
gd::ObjectsContainer& objectsContainer) {
// Find an object
if (objectsContainer.HasObjectNamed(GetObjectName()))
return objectsContainer.GetObject(GetObjectName())
Expand All @@ -172,9 +184,10 @@ InitialInstance::GetCustomProperties(
}

bool InitialInstance::UpdateCustomProperty(
const gd::String &name, const gd::String &value,
gd::ObjectsContainer &globalObjectsContainer,
gd::ObjectsContainer &objectsContainer) {
const gd::String& name,
const gd::String& value,
gd::ObjectsContainer& globalObjectsContainer,
gd::ObjectsContainer& objectsContainer) {
if (objectsContainer.HasObjectNamed(GetObjectName()))
return objectsContainer.GetObject(GetObjectName())
.GetConfiguration()
Expand Down
82 changes: 63 additions & 19 deletions Core/GDCore/Project/InitialInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GD_CORE_API InitialInstance {
* \brief Create an initial instance pointing to no object, at position (0,0).
*/
InitialInstance();
virtual ~InitialInstance(){};
virtual ~InitialInstance() {};

/**
* Must return a pointer to a copy of the object. A such method is needed to
Expand Down Expand Up @@ -123,6 +123,46 @@ class GD_CORE_API InitialInstance {
*/
void SetZOrder(int zOrder_) { zOrder = zOrder_; }

/**
* \brief Get Opacity.
*/
int GetOpacity() const { return opacity; }

/**
* \brief Set the opacity of the instance.
*/
void SetOpacity(int opacity_) { opacity = opacity_; }

/**
* \brief Return true if the instance is flipped on X axis.
*/
bool IsFlippedX() const { return flippedX; }

/**
* \brief Set whether the instance is flipped on X axis.
*/
void SetFlippedX(bool flippedX_) { flippedX = flippedX_; }

/**
* \brief Return true if the instance is flipped on Y axis.
*/
bool IsFlippedY() const { return flippedY; }

/**
* \brief Set whether the instance is flipped on Y axis.
*/
void SetFlippedY(bool flippedY_) { flippedY = flippedY_; }

/**
* \brief Return true if the instance is flipped on Z axis.
*/
bool IsFlippedZ() const { return flippedZ; }

/**
* \brief Set whether the instance is flipped on Z axis.
*/
void SetFlippedZ(bool flippedZ_) { flippedZ = flippedZ_; }

/**
* \brief Get the layer the instance belongs to.
*/
Expand All @@ -134,8 +174,9 @@ class GD_CORE_API InitialInstance {
void SetLayer(const gd::String& layer_) { layer = layer_; }

/**
* \brief Return true if the instance has a width/height which is different from its
* object default width/height. This is independent from `HasCustomDepth`.
* \brief Return true if the instance has a width/height which is different
* from its object default width/height. This is independent from
* `HasCustomDepth`.
*
* \see gd::Object
*/
Expand All @@ -150,15 +191,13 @@ class GD_CORE_API InitialInstance {
bool HasCustomDepth() const { return customDepth; }

/**
* \brief Set whether the instance has a width/height which is different from its
* object default width/height or not.
* This is independent from `SetHasCustomDepth`.
* \brief Set whether the instance has a width/height which is different from
* its object default width/height or not. This is independent from
* `SetHasCustomDepth`.
*
* \see gd::Object
*/
void SetHasCustomSize(bool hasCustomSize_) {
customSize = hasCustomSize_;
}
void SetHasCustomSize(bool hasCustomSize_) { customSize = hasCustomSize_; }

/**
* \brief Set whether the instance has a depth which is different from its
Expand Down Expand Up @@ -264,18 +303,19 @@ class GD_CORE_API InitialInstance {
* \note Common properties ( name, position... ) do not need to be
* inserted in this map
*/
std::map<gd::String, gd::PropertyDescriptor>
GetCustomProperties(gd::ObjectsContainer &globalObjectsContainer,
gd::ObjectsContainer &objectsContainer);
std::map<gd::String, gd::PropertyDescriptor> GetCustomProperties(
gd::ObjectsContainer& globalObjectsContainer,
gd::ObjectsContainer& objectsContainer);

/**
* \brief Update the property called \a name with the new \a value.
*
* \return false if the property could not be updated.
*/
bool UpdateCustomProperty(const gd::String &name, const gd::String &value,
gd::ObjectsContainer &globalObjectsContainer,
gd::ObjectsContainer &objectsContainer);
bool UpdateCustomProperty(const gd::String& name,
const gd::String& value,
gd::ObjectsContainer& globalObjectsContainer,
gd::ObjectsContainer& objectsContainer);

/**
* \brief Get the value of a double property stored in the instance.
Expand Down Expand Up @@ -343,6 +383,10 @@ class GD_CORE_API InitialInstance {
double rotationX; ///< Instance angle on X axis (for a 3D object)
double rotationY; ///< Instance angle on Y axis (for a 3D object)
int zOrder; ///< Instance Z order (for a 2D object)
int opacity; ///< Instance opacity
bool flippedX; ///< True if the instance is flipped on X axis
bool flippedY; ///< True if the instance is flipped on Y axis
bool flippedZ; ///< True if the instance is flipped on Z axis
gd::String layer; ///< Instance layer
bool customSize; ///< True if object has a custom width and height
bool customDepth; ///< True if object has a custom depth
Expand All @@ -352,13 +396,13 @@ class GD_CORE_API InitialInstance {
gd::VariablesContainer initialVariables; ///< Instance specific variables
bool locked; ///< True if the instance is locked
bool sealed; ///< True if the instance is sealed
bool keepRatio; ///< True if the instance's dimensions
/// should keep the same ratio.
bool keepRatio; ///< True if the instance's dimensions
/// should keep the same ratio.
mutable gd::String persistentUuid; ///< A persistent random version 4 UUID,
/// useful for hot reloading.

static gd::String*
badStringPropertyValue; ///< Empty string returned by GetRawStringProperty
static gd::String* badStringPropertyValue; ///< Empty string returned by
///< GetRawStringProperty
};

} // namespace gd
12 changes: 11 additions & 1 deletion Extensions/3D/A_RuntimeObject3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,18 @@ namespace gdjs {
this.setWidth(initialInstanceData.width);
this.setHeight(initialInstanceData.height);
}
if (initialInstanceData.depth !== undefined)
if (initialInstanceData.depth !== undefined) {
this.setDepth(initialInstanceData.depth);
}
if (initialInstanceData.flippedX) {
this.flipX(initialInstanceData.flippedX);
}
if (initialInstanceData.flippedY) {
this.flipY(initialInstanceData.flippedY);
}
if (initialInstanceData.flippedZ) {
this.flipZ(initialInstanceData.flippedZ);
}
}

setX(x: float): void {
Expand Down
12 changes: 11 additions & 1 deletion Extensions/3D/CustomRuntimeObject3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,18 @@ namespace gdjs {

extraInitializationFromInitialInstance(initialInstanceData: InstanceData) {
super.extraInitializationFromInitialInstance(initialInstanceData);
if (initialInstanceData.depth !== undefined)
if (initialInstanceData.depth !== undefined) {
this.setDepth(initialInstanceData.depth);
}
if (initialInstanceData.flippedX) {
this.flipX(initialInstanceData.flippedX);
}
if (initialInstanceData.flippedY) {
this.flipY(initialInstanceData.flippedY);
}
if (initialInstanceData.flippedZ) {
this.flipZ(initialInstanceData.flippedZ);
}
}

/**
Expand Down
37 changes: 27 additions & 10 deletions Extensions/3D/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2216,8 +2216,14 @@ module.exports = {
this._centerY / objectTextureFrame.height;

this._pixiTexturedObject.angle = this._instance.getAngle();
this._pixiTexturedObject.scale.x = width / objectTextureFrame.width;
this._pixiTexturedObject.scale.y = height / objectTextureFrame.height;
const scaleX =
(width / objectTextureFrame.width) *
(this._instance.isFlippedX() ? -1 : 1);
const scaleY =
(height / objectTextureFrame.height) *
(this._instance.isFlippedY() ? -1 : 1);
this._pixiTexturedObject.scale.x = scaleX;
this._pixiTexturedObject.scale.y = scaleY;

this._pixiTexturedObject.position.x =
this._instance.getX() +
Expand All @@ -2244,6 +2250,9 @@ module.exports = {
this._pixiFallbackObject.position.y =
this._instance.getY() + height / 2;
this._pixiFallbackObject.angle = this._instance.getAngle();

if (this._instance.isFlippedX()) this._pixiFallbackObject.scale.x = -1;
if (this._instance.isFlippedY()) this._pixiFallbackObject.scale.y = -1;
}

update() {
Expand Down Expand Up @@ -2393,12 +2402,16 @@ module.exports = {
RenderedInstance.toRad(this._instance.getAngle())
);

const scaleX = width * (this._instance.isFlippedX() ? -1 : 1);
const scaleY = height * (this._instance.isFlippedY() ? -1 : 1);
const scaleZ = depth * (this._instance.isFlippedZ() ? -1 : 1);

if (
width !== this._threeObject.scale.width ||
height !== this._threeObject.scale.height ||
depth !== this._threeObject.scale.depth
scaleX !== this._threeObject.scale.width ||
scaleY !== this._threeObject.scale.height ||
scaleZ !== this._threeObject.scale.depth
) {
this._threeObject.scale.set(width, height, depth);
this._threeObject.scale.set(scaleX, scaleY, scaleZ);
this.updateTextureUvMapping();
}
}
Expand Down Expand Up @@ -3186,12 +3199,16 @@ module.exports = {
RenderedInstance.toRad(this._instance.getAngle())
);

const scaleX = width * (this._instance.isFlippedX() ? -1 : 1);
const scaleY = height * (this._instance.isFlippedY() ? -1 : 1);
const scaleZ = depth * (this._instance.isFlippedZ() ? -1 : 1);

if (
width !== this._threeObject.scale.width ||
height !== this._threeObject.scale.height ||
depth !== this._threeObject.scale.depth
scaleX !== this._threeObject.scale.width ||
scaleY !== this._threeObject.scale.height ||
scaleZ !== this._threeObject.scale.depth
) {
this._threeObject.scale.set(width, height, depth);
this._threeObject.scale.set(scaleX, scaleY, scaleZ);
}
}

Expand Down
17 changes: 7 additions & 10 deletions Extensions/BBText/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ module.exports = {
.setLabel(_('Base color'))
.setGroup(_('Appearance'));

objectProperties
.getOrCreate('opacity')
.setValue(objectContent.opacity.toString())
.setType('number')
.setLabel(_('Opacity (0-255)'))
.setGroup(_('Appearance'));

objectProperties
.getOrCreate('fontSize')
.setValue(objectContent.fontSize.toString())
Expand Down Expand Up @@ -545,9 +538,6 @@ module.exports = {
this._pixiObject.text = rawText;
}

const opacity = +properties.get('opacity').getValue();
this._pixiObject.alpha = opacity / 255;

const color = properties.get('color').getValue();
this._pixiObject.textStyles.default.fill = objectsRenderingService.rgbOrHexToHexNumber(
color
Expand Down Expand Up @@ -607,6 +597,13 @@ module.exports = {
this._pixiObject.dirty = true;
}
}

// Do not hide completely an object so it can still be manipulated
const alphaForDisplay = Math.max(
this._instance.getOpacity() / 255,
0.5
);
this._pixiObject.alpha = alphaForDisplay;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Extensions/BBText/bbtextruntimeobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ namespace gdjs {
250
);
}
if (initialInstanceData.opacity !== undefined) {
this.setOpacity(initialInstanceData.opacity);
}
}

onDestroyed(): void {
Expand Down
17 changes: 7 additions & 10 deletions Extensions/BitmapText/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ module.exports = {
.setType('textarea')
.setLabel(_('Text'));

objectProperties
.getOrCreate('opacity')
.setValue(objectContent.opacity.toString())
.setType('number')
.setLabel(_('Opacity (0-255)'))
.setGroup(_('Appearance'));

objectProperties
.getOrCreate('align')
.setValue(objectContent.align)
Expand Down Expand Up @@ -673,9 +666,6 @@ module.exports = {
const rawText = properties.get('text').getValue();
this._pixiObject.text = rawText;

const opacity = +properties.get('opacity').getValue();
this._pixiObject.alpha = opacity / 255;

const align = properties.get('align').getValue();
this._pixiObject.align = align;

Expand Down Expand Up @@ -739,6 +729,13 @@ module.exports = {
this._pixiObject.rotation = RenderedInstance.toRad(
this._instance.getAngle()
);

// Do not hide completely an object so it can still be manipulated
const alphaForDisplay = Math.max(
this._instance.getOpacity() / 255,
0.5
);
this._pixiObject.alpha = alphaForDisplay;
}

onRemovedFromScene() {
Expand Down
Loading

0 comments on commit dc45f3d

Please sign in to comment.