PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tutos/example17a/FireConeHedra.hx

http://github.com/sandy3d/sandy-hx
Haxe | 253 lines | 194 code | 51 blank | 8 comment | 4 complexity | 7255693e1ce863a60dbc4641fa0f086f MD5 | raw file
  1. import com.gskinner.effects.FireFX;
  2. import flash.display.Sprite;
  3. import flash.events.Event;
  4. import flash.events.MouseEvent;
  5. import flash.text.TextField;
  6. import flash.Lib;
  7. import sandy.core.Scene3D;
  8. import sandy.core.scenegraph.Camera3D;
  9. import sandy.core.scenegraph.Group;
  10. import sandy.core.scenegraph.Shape3D;
  11. import sandy.materials.Appearance;
  12. import sandy.materials.Material;
  13. import sandy.materials.ColorMaterial;
  14. import sandy.materials.attributes.LightAttributes;
  15. import sandy.materials.attributes.LineAttributes;
  16. import sandy.materials.attributes.MaterialAttributes;
  17. import sandy.primitive.Hedra;
  18. import sandy.primitive.Cone;
  19. import org.aswing.JPanel;
  20. import org.aswing.JSlider;
  21. import org.aswing.JLabel;
  22. import org.aswing.JCheckBox;
  23. import org.aswing.geom.IntDimension;
  24. import org.aswing.geom.IntPoint;
  25. enum FireParams {
  26. DISTORTION;
  27. DISTORTIONSCALE;
  28. FADERATE;
  29. FLAMEHEIGHT;
  30. FLAMESPREAD;
  31. SMOKE;
  32. }
  33. class FireConeHedra extends Sprite {
  34. private var scene:Scene3D;
  35. private var camera:Camera3D;
  36. private var myCone:Cone;
  37. private var myHedra:Hedra;
  38. private var myText:TextField;
  39. private var needRemove:Bool;
  40. private var myPanel:JPanel;
  41. private var fireFX:FireFX;
  42. private var blueFlameCheckBox:JCheckBox;
  43. private var hide3DCheckBox:JCheckBox;
  44. private var distortionSlider:JSlider;
  45. private var distortionScaleSlider:JSlider;
  46. private var params:Array<FireParams>;
  47. public function new () {
  48. myText = new TextField();
  49. needRemove = false;
  50. params = [DISTORTION, DISTORTIONSCALE,
  51. FADERATE, FLAMEHEIGHT, FLAMESPREAD, SMOKE];
  52. super();
  53. fireFX = new FX();
  54. myPanel = new JPanel();
  55. myPanel.setSize(new IntDimension(300, 600));
  56. myPanel.setLocation(new IntPoint(350, 0));
  57. // Blue flame
  58. var blueFlame:JCheckBox = new JCheckBox();
  59. blueFlame.setLocation(new IntPoint(0, 0));
  60. blueFlame.setSize(new IntDimension(100, 20));
  61. blueFlame.setHorizontalAlignment( 2 );
  62. blueFlame.setText("Blue flame");
  63. // Hide 3D
  64. var hideText:JCheckBox = new JCheckBox();
  65. hideText.setLocation(new IntPoint(100, 0));
  66. hideText.setSize(new IntDimension(100, 20));
  67. hideText.setHorizontalAlignment( 2 );
  68. hideText.setText("Hide 3D");
  69. // top-level sprite contains the hedra and cone
  70. var obj:Sprite = new Sprite();
  71. // set clickhandlers on checkboxes
  72. var fx:FireFX = fireFX;
  73. var handleCheckBox = function () {
  74. fx.blueFlame = blueFlame.isSelected();
  75. obj.visible = !hideText.isSelected();
  76. }
  77. hideText.addStateListener(handleCheckBox);
  78. blueFlame.addStateListener(handleCheckBox);
  79. myPanel.append(blueFlame);
  80. myPanel.append(hideText);
  81. // generate labels and sliders
  82. var i:Int, l:UInt = params.length;
  83. for (i in 0...l) {
  84. var name:String = switch ( params[i] ) {
  85. case DISTORTION: "distortion";
  86. case DISTORTIONSCALE: "distortionScale";
  87. case FADERATE: "fadeRate";
  88. case FLAMEHEIGHT: "flameHeight";
  89. case FLAMESPREAD: "flameSpread";
  90. case SMOKE: "smoke";
  91. }
  92. var label:JLabel = new JLabel();
  93. label.setLocation(new IntPoint(0, 25+50*i));
  94. label.setSize(new IntDimension(140, 20));
  95. label.setHorizontalAlignment( 2 );
  96. label.setText(name);
  97. myPanel.append( label );
  98. label = new JLabel();
  99. var label:JLabel = new JLabel();
  100. label.setLocation(new IntPoint(110, 25+50*i));
  101. label.setSize(new IntDimension(50, 20));
  102. label.setHorizontalAlignment( 4 );
  103. label.setText("0.35");
  104. myPanel.append( label );
  105. var slider:JSlider = new JSlider();
  106. slider.setLocation(new IntPoint(0, 55+50*i));
  107. slider.setSize(new IntDimension(150, 20));
  108. slider.setValue(35);
  109. slider.setExtent(400);
  110. slider.setMinimum(-200);
  111. slider.setMaximum(200);
  112. var param:FireParams = params[i];
  113. slider.addStateListener(function () {
  114. var value:Float = slider.getValue() / 100.0;
  115. switch ( param ) {
  116. case DISTORTION: fx.distortion = value;
  117. case DISTORTIONSCALE: fx.distortionScale = value;
  118. case FADERATE: fx.fadeRate = value;
  119. case FLAMEHEIGHT: fx.flameHeight = value;
  120. case FLAMESPREAD: fx.flameSpread = value;
  121. case SMOKE: fx.smoke = value;
  122. }
  123. label.setText(Std.string( value ));
  124. });
  125. myPanel.append( slider );
  126. }
  127. addChild( myPanel );
  128. var root:Group = createScene();
  129. camera = new Camera3D( Std.int( fireFX.width ), Std.int( fireFX.height ) );
  130. camera.x = 100;
  131. camera.y = 100;
  132. camera.z = -300;
  133. camera.lookAt(0,0,0);
  134. scene = new Scene3D( "scene", obj, camera, root );
  135. obj.blendMode = flash.display.BlendMode.MULTIPLY;
  136. addEventListener( Event.ENTER_FRAME, enterFrameHandler );
  137. // preserve ordering of layers.
  138. Lib.current.stage.addChild(fireFX);
  139. Lib.current.stage.addChild(obj);
  140. Lib.current.stage.addChild(this);
  141. }
  142. function createScene():Group {
  143. var g:Group = new Group();
  144. myCone = new Cone("Cone",50, 100);
  145. myHedra = new Hedra( "Hedra", 80, 60, 60 );
  146. myCone.x = -160;
  147. myCone.z = 150;
  148. myCone.y -= 50;
  149. myHedra.x = 50;
  150. myHedra.y -= 50;
  151. myCone.container.buttonMode = true;
  152. myHedra.container.buttonMode = true;
  153. var materialAttr:MaterialAttributes = new MaterialAttributes(
  154. [new LineAttributes( 0.5, 0x2111BB, 0.4 ),
  155. new LightAttributes( true, 0.1)]
  156. );
  157. var material:Material = new ColorMaterial( 0xFFCC33, 1, materialAttr );
  158. material.lightingEnable = true;
  159. var app:Appearance = new Appearance( material );
  160. myCone.appearance = app;
  161. myHedra.appearance = app;
  162. myCone.enableEvents = true;
  163. myCone.addEventListener(MouseEvent.CLICK, clickHandler);
  164. myHedra.enableEvents = true;
  165. myHedra.addEventListener(MouseEvent.CLICK, clickHandler);
  166. myCone.container.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
  167. myHedra.container.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
  168. g.addChild(myCone);
  169. g.addChild(myHedra);
  170. return g;
  171. }
  172. function enterFrameHandler( event:Event ):Void {
  173. myHedra.pan +=4;
  174. myCone.pan +=4;
  175. scene.render();
  176. }
  177. private function clickHandler(event:sandy.events.BubbleEvent):Void {
  178. var shape:Shape3D = event.object;
  179. trace( "You have hit the " + shape.name );
  180. needRemove = true;
  181. fireFX.target = shape.container;
  182. }
  183. private function outHandler(event:MouseEvent):Void {
  184. if(needRemove)
  185. {
  186. //this.removeChild(myText);
  187. needRemove = false;
  188. }
  189. }
  190. static function main() {
  191. new FireConeHedra();
  192. }
  193. }
  194. class FX extends FireFX {
  195. public function new () {
  196. // sets bounds on the fire
  197. var circle1:Sprite = new Sprite();
  198. circle1.graphics.drawCircle(200, 200, 200);
  199. this.addChild( circle1 );
  200. super();
  201. }
  202. }