Skip to content

Commit

Permalink
[gradient] Added gradient prefab, and fixed gradient input type
Browse files Browse the repository at this point in the history
  • Loading branch information
EspeuteClement committed Oct 17, 2024
1 parent 972e01c commit 2050ab4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
8 changes: 6 additions & 2 deletions hide/comp/PropsEditor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,14 @@ class PropsField extends Component {

propagateValueChange = (value, isTemp) -> {
var f = resolveField();
isTempChange = isTemp;
f.gradient.value = value;
f.gradient.onChange(isTemp);
};

gradient.onChange = function(shouldUndo : Bool) {
gradient.onChange = function(temp : Bool) {
isTempChange = temp;
var shouldUndo = !temp;
if (shouldUndo) {
var setVal = function(val, undo) {
var f = resolveField();
Expand All @@ -549,7 +552,8 @@ class PropsField extends Component {
} else {
current = gradient.value;
setFieldValue(current);
onChange(false);
var f = resolveField();
f.onChange(false);
}
}
case "model":
Expand Down
73 changes: 73 additions & 0 deletions hrt/prefab/l2d/Gradient.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package hrt.prefab.l2d;

class Gradient extends Object2D {
@:s var gradient: hrt.impl.Gradient.GradientData = hrt.impl.Gradient.getDefaultGradientData();
@:s var dx: Float;
@:s var dy: Float;

override function makeObject(parent2d:h2d.Object):h2d.Object {
var bmp = new h2d.Bitmap(null, parent2d);
bmp.smooth = true;
return bmp;
}

override function updateInstance(?propName : String ) {
super.updateInstance(propName);
var bmp = (cast local2d : h2d.Bitmap);
bmp.visible = visible;
if (bmp.tile == null) {
bmp.tile = h2d.Tile.fromTexture(hrt.impl.Gradient.textureFromData(gradient));
bmp.tile.setSize(64,64);
} else {
@:privateAccess bmp.tile.setTexture(hrt.impl.Gradient.textureFromData(gradient));
}

var cRatio = Bitmap.getCenterRatio(dx,dy);
bmp.tile.setCenterRatio(cRatio[0], cRatio[1]);

#if editor
var int = Std.downcast(bmp.getChildAt(0),h2d.Interactive);
if( int != null ) {
int.width = bmp.tile.width;
int.height = bmp.tile.height;
int.x = bmp.tile.dx;
int.y = bmp.tile.dy;
}
#end
}

#if editor

override function makeInteractive():h2d.Interactive {
if(local2d == null)
return null;
var bmp = cast(local2d, h2d.Bitmap);
var int = new h2d.Interactive(bmp.tile.width, bmp.tile.height);
bmp.addChildAt(int, 0);
int.propagateEvents = true;
int.x = bmp.tile.dx;
int.y = bmp.tile.dy;
return int;
}


override function edit( ctx : hide.prefab.EditContext ) {
super.edit(ctx);
ctx.properties.add(new hide.Element('<div class="group" name="Parameters">
<dl>
<dt>Gradient</dt><dd><input type="gradient" field="gradient"/></dd>
<dt>Bg Pivot DX</dt><dd><input type="range" min="-0.5" max="0.5" field="dx"/></dd>
<dt>Bg Pivot DY</dt><dd><input type="range" min="-0.5" max="0.5" field="dy"/></dd>
</dl></div>'), this, function(pname) {
ctx.onChange(this, pname);
});
}

override function getHideProps() : hide.prefab.HideProps {
return { icon : "eyedropper", name : "Gradient" };
}

#end

static var _ = Prefab.register("gradient", Gradient);
}

0 comments on commit 2050ab4

Please sign in to comment.