Object2D.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package hrt.prefab;
  2. import hxd.Math;
  3. using Lambda;
  4. class Object2D extends Prefab {
  5. @:s public var x : Float = 0.;
  6. @:s public var y : Float = 0.;
  7. @:s public var scaleX : Float = 1.;
  8. @:s public var scaleY : Float = 1.;
  9. @:s public var rotation : Float = 0.;
  10. @:s public var visible : Bool = true;
  11. @:c public var blendMode : h2d.BlendMode = None;
  12. public function loadTransform(t) {
  13. x = t.x;
  14. y = t.y;
  15. scaleX = t.scaleX;
  16. scaleY = t.scaleY;
  17. rotation = t.rotation;
  18. }
  19. public function saveTransform() {
  20. return { x : x, y : y, scaleX : scaleX, scaleY : scaleY, rotation : rotation };
  21. }
  22. public function setTransform(t) {
  23. x = t.x;
  24. y = t.y;
  25. scaleX = t.scaleX;
  26. scaleY = t.scaleY;
  27. rotation = t.rotation;
  28. }
  29. override function load( obj : Dynamic ) {
  30. super.load(obj);
  31. if( obj.blendMode != null )
  32. blendMode = std.Type.createEnum(h2d.BlendMode, obj.blendMode);
  33. }
  34. override function makeInstance(ctx:Context):Context {
  35. ctx = ctx.clone(this);
  36. ctx.local2d = new h2d.Object(ctx.local2d);
  37. ctx.local2d.name = name;
  38. updateInstance(ctx);
  39. return ctx;
  40. }
  41. override function save() {
  42. var o : Dynamic = super.save();
  43. if( blendMode != None ) o.blendMode = blendMode.getName();
  44. return o;
  45. }
  46. public function getTransform() {
  47. var m = new h2d.col.Matrix();
  48. m.initScale(scaleX, scaleY);
  49. m.rotate(Math.degToRad(rotation));
  50. m.translate(x, y);
  51. return m;
  52. }
  53. public function applyTransform( o : h2d.Object ) {
  54. o.x = x;
  55. o.y = y;
  56. o.scaleX = scaleX;
  57. o.scaleY = scaleY;
  58. o.rotation = Math.degToRad(rotation);
  59. }
  60. override function updateInstance( ctx: Context, ?propName : String ) {
  61. var o = ctx.local2d;
  62. o.x = x;
  63. o.y = y;
  64. if(propName == null || propName.indexOf("scale") == 0) {
  65. o.scaleX = scaleX;
  66. o.scaleY = scaleY;
  67. }
  68. if(propName == null || propName.indexOf("rotation") == 0)
  69. o.rotation = Math.degToRad(rotation);
  70. if(propName == null || propName == "visible")
  71. o.visible = visible;
  72. if(propName == null || propName == "blendMode")
  73. if (blendMode != null) o.blendMode = blendMode;
  74. }
  75. override function removeInstance(ctx: Context):Bool {
  76. if(ctx.local2d != null)
  77. ctx.local2d.remove();
  78. return true;
  79. }
  80. #if editor
  81. override function edit( ctx : EditContext ) {
  82. var props = new hide.Element('
  83. <div class="group" name="Position">
  84. <dl>
  85. <dt>X</dt><dd><input type="range" min="-100" max="100" value="0" field="x"/></dd>
  86. <dt>Y</dt><dd><input type="range" min="-100" max="100" value="0" field="y"/></dd>
  87. <dt>Scale X</dt><dd><input type="range" min="0" max="5" value="1" field="scaleX"/></dd>
  88. <dt>Scale Y</dt><dd><input type="range" min="0" max="5" value="1" field="scaleY"/></dd>
  89. <dt>Rotation</dt><dd><input type="range" min="-180" max="180" value="0" field="rotation" /></dd>
  90. </dl>
  91. </div>
  92. <div class="group" name="Display">
  93. <dl>
  94. <dt>Visible</dt><dd><input type="checkbox" field="visible"/></dd>
  95. <dt>Blend Mode</dt><dd><select field="blendMode"/></dd></dd>
  96. </dl>
  97. </div>
  98. ');
  99. ctx.properties.add(props, this, function(pname) {
  100. ctx.onChange(this, pname);
  101. });
  102. }
  103. override function getHideProps() : HideProps {
  104. // Check children
  105. return {
  106. icon : children == null || children.length > 0 ? "folder-open" : "genderless",
  107. name : "Group 2D"
  108. };
  109. }
  110. #end
  111. override function getDefaultName() {
  112. return type == "object2D" ? "group2D" : super.getDefaultName();
  113. }
  114. static var _ = Library.register("object2D", Object2D);
  115. }