Interactive2D.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import h2d.col.RoundRect;
  2. import h2d.col.Circle;
  3. import h2d.col.Triangle;
  4. import h2d.col.Point;
  5. import h2d.col.Polygon;
  6. import h2d.col.PolygonCollider;
  7. class Interactive2D extends SampleApp {
  8. var hover : Bool;
  9. var shouldRotate : Bool;
  10. var interactive : h2d.Interactive;
  11. var graphics : h2d.Graphics;
  12. var polygonShape : PolygonCollider;
  13. var triangleShape : Triangle;
  14. var circleShape : Circle;
  15. var rectShape : RoundRect;
  16. static inline var rectY : Int = 32;
  17. static inline var rectWidth : Int = 64; // *2
  18. static inline var rectHeight : Int = 32;
  19. public function new() {
  20. super();
  21. }
  22. override private function init()
  23. {
  24. super.init();
  25. var poly : Polygon = new Polygon([
  26. new Point(64, 16),
  27. new Point(96, 0),
  28. new Point(127, 0),
  29. new Point(127, 32),
  30. new Point(111, 63),
  31. new Point(127, 95),
  32. new Point(127, 127),
  33. new Point(96, 127),
  34. new Point(64, 111),
  35. new Point(32, 127),
  36. new Point(1, 127),
  37. new Point(1, 95),
  38. new Point(17, 63),
  39. new Point(1, 32),
  40. new Point(1, 0),
  41. new Point(32, 0),
  42. new Point(64, 16),
  43. ]);
  44. // Polygon collider can be used both for single polygon or for multiple polygons at once.
  45. polygonShape = poly.getCollider();
  46. triangleShape = new Triangle(new Point(64, 0), new Point(128, 128), new Point(0, 128));
  47. circleShape = new Circle(64, 64, 64);
  48. rectShape = new RoundRect(rectWidth, rectY+rectHeight, rectWidth*2, rectHeight*2, 0);
  49. interactive = new h2d.Interactive(128, 128, s2d);
  50. graphics = new h2d.Graphics(interactive);
  51. interactive.onOver = function( e : hxd.Event ) {
  52. hover = true;
  53. redrawGraphics();
  54. }
  55. interactive.onOut = function( e : hxd.Event ) {
  56. hover = false;
  57. redrawGraphics();
  58. }
  59. addCheck("isEllipse", function() return interactive.isEllipse, function(v) interactive.isEllipse = v);
  60. addCheck("Rotate", function() return shouldRotate, function(v) {
  61. shouldRotate = v;
  62. if (!v) setRotation(0);
  63. });
  64. addChoice("Shape", ["Polygon", "Triangle", "Circle", "RoundRect", "None"], setShape, 0);
  65. setShape(0);
  66. }
  67. function setRotation( v : Float ) {
  68. interactive.rotation = v;
  69. var cos = Math.cos(v);
  70. var sin = Math.sin(v);
  71. var hw = -interactive.width * .5;
  72. var hh = -interactive.height * .5;
  73. interactive.x = s2d.width * .5 + (cos * hw - sin * hh);
  74. interactive.y = s2d.height * .5 + (sin * hw + cos * hh);
  75. }
  76. function redrawGraphics() {
  77. graphics.clear();
  78. graphics.beginFill(hover ? 0xff0000 : 0xf8931f);
  79. if (interactive.shape == polygonShape) {
  80. for (polygon in polygonShape.polygons) {
  81. var it = polygon.iterator();
  82. var pt = it.next();
  83. graphics.moveTo(pt.x, pt.y);
  84. while (it.hasNext()) {
  85. pt = it.next();
  86. graphics.lineTo(pt.x, pt.y);
  87. }
  88. }
  89. } else if (interactive.shape == triangleShape) {
  90. graphics.moveTo(triangleShape.a.x, triangleShape.a.y);
  91. graphics.lineTo(triangleShape.b.x, triangleShape.b.y);
  92. graphics.lineTo(triangleShape.c.x, triangleShape.c.y);
  93. } else if (interactive.shape == circleShape) {
  94. graphics.drawCircle(circleShape.x, circleShape.y, circleShape.ray);
  95. } else if (interactive.shape == rectShape) {
  96. var size = rectWidth - rectHeight;
  97. var k = 10;
  98. for( i in 0...k+1 ) {
  99. var a = Math.PI * i / k - Math.PI / 2;
  100. graphics.lineTo(size + rectWidth + rectHeight * Math.cos(a), rectY + rectHeight + rectHeight * Math.sin(a));
  101. }
  102. for( i in 0...k+1 ) {
  103. var a = Math.PI * i / k + Math.PI / 2;
  104. graphics.lineTo(-size + rectWidth + rectHeight * Math.cos(a), rectY + rectHeight + rectHeight * Math.sin(a));
  105. }
  106. } else {
  107. graphics.drawRect(0, 0, interactive.width, interactive.height);
  108. }
  109. graphics.endFill();
  110. }
  111. function setShape( index : Int ) {
  112. switch (index) {
  113. case 0: interactive.shape = polygonShape;
  114. case 1: interactive.shape = triangleShape;
  115. case 2: interactive.shape = circleShape;
  116. case 3: interactive.shape = rectShape;
  117. case 4: interactive.shape = null;
  118. }
  119. redrawGraphics();
  120. setRotation(interactive.rotation);
  121. }
  122. override private function update(dt:Float)
  123. {
  124. super.update(dt);
  125. if (shouldRotate) setRotation(interactive.rotation + dt * .1);
  126. }
  127. static function main() {
  128. hxd.Res.initEmbed();
  129. new Interactive2D();
  130. }
  131. }