Bitmap.hx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package hrt.prefab.l2d;
  2. class Bitmap extends Object2D {
  3. // parameters
  4. @:s var color : Int = 0xFFFFFF;
  5. @:s var src : String;
  6. @:s var dx : Float = 0;
  7. @:s var dy : Float = 0;
  8. var tex : h3d.mat.Texture;
  9. override function updateInstance( ctx: Context, ?propName : String ) {
  10. super.updateInstance(ctx, propName);
  11. var bmp = (cast ctx.local2d : h2d.Bitmap);
  12. bmp.visible = visible;
  13. if (propName == null || propName == "src") {
  14. if (tex != null) {
  15. tex = null;
  16. }
  17. if (src != null) {
  18. tex = ctx.loadTexture(src);
  19. bmp.tile = h2d.Tile.fromTexture(this.tex);
  20. } else {
  21. bmp.tile = h2d.Tile.fromColor(0xFF00FF,32,32,0.5);
  22. }
  23. }
  24. bmp.color = h3d.Vector.fromColor(color);
  25. bmp.color.w = 1;
  26. var cRatio = getCenterRatio(dx, dy);
  27. bmp.tile.setCenterRatio(cRatio[0], cRatio[1]);
  28. bmp.blendMode = blendMode;
  29. #if editor
  30. var int = Std.downcast(bmp.getChildAt(0),h2d.Interactive);
  31. if( int != null ) {
  32. int.width = bmp.tile.width;
  33. int.height = bmp.tile.height;
  34. int.x = bmp.tile.dx;
  35. int.y = bmp.tile.dy;
  36. }
  37. #end
  38. }
  39. override function makeInstance(ctx:Context):Context {
  40. ctx = ctx.clone(this);
  41. var bmp = new h2d.Bitmap(null, ctx.local2d);
  42. bmp.smooth = true;
  43. ctx.local2d = bmp;
  44. ctx.local2d.name = name;
  45. updateInstance(ctx);
  46. return ctx;
  47. }
  48. static public function getCenterRatio(dx : Float, dy : Float) {
  49. return [0.5 + hxd.Math.clamp(dx, -0.5, 0.5), 0.5 + hxd.Math.clamp(dy, -0.5, 0.5)];
  50. }
  51. #if editor
  52. override function makeInteractive(ctx:Context):h2d.Interactive {
  53. var local2d = ctx.local2d;
  54. if(local2d == null)
  55. return null;
  56. var bmp = cast(local2d, h2d.Bitmap);
  57. var int = new h2d.Interactive(bmp.tile.width, bmp.tile.height);
  58. bmp.addChildAt(int, 0);
  59. int.propagateEvents = true;
  60. int.x = bmp.tile.dx;
  61. int.y = bmp.tile.dy;
  62. return int;
  63. }
  64. override function edit( ctx : EditContext ) {
  65. super.edit(ctx);
  66. ctx.properties.add(new hide.Element('<div class="group" name="Parameters">
  67. <dl>
  68. <dt>Color</dt><dd><input type="color" field="color" /></dd>
  69. <dt>Background</dt><dd><input type="texturepath" field="src" style="width:165px"/></dd>
  70. <dt>Bg Pivot DX</dt><dd><input type="range" min="-0.5" max="0.5" field="dx"/></dd>
  71. <dt>Bg Pivot DY</dt><dd><input type="range" min="-0.5" max="0.5" field="dy"/></dd>
  72. </dl></div>'), this, function(pname) {
  73. ctx.onChange(this, pname);
  74. });
  75. }
  76. override function getHideProps() : HideProps {
  77. return { icon : "square", name : "Bitmap" };
  78. }
  79. #end
  80. static var _ = Library.register("bitmap", Bitmap);
  81. }