/demo/clock/Clock.hx

http://github.com/silexlabs/Cocktail · Haxe · 203 lines · 104 code · 40 blank · 59 comment · 2 complexity · 6433a4b4e2c7d07ab7b53704b9f2bee7 MD5 · raw file

  1. package ;
  2. import cocktail.domElement.ImageDOMElement;
  3. import haxe.Log;
  4. import haxe.Timer;
  5. import cocktail.domElement.GraphicDOMElement;
  6. import cocktail.domElement.TextDOMElement;
  7. import cocktail.domElement.abstract.AbstractDOMElement;
  8. import cocktail.domElement.DOMElementData;
  9. import cocktail.geom.GeomData;
  10. import cocktail.domElement.DOMElement;
  11. import cocktail.nativeElement.NativeElementManager;
  12. import cocktail.resource.ResourceLoaderManager;
  13. /**
  14. * Display an "analogue" and numeric clock updated each seconds
  15. * @author Yannick DOMINGUEZ
  16. */
  17. class Clock
  18. {
  19. /**
  20. * the background of the analogue clock
  21. */
  22. private var _clockBackground:ImageDOMElement;
  23. /**
  24. * the foreground of the analogue clock
  25. */
  26. private var _clockForeGround:ImageDOMElement;
  27. /**
  28. * the second needle of the clock
  29. */
  30. private var _secondNeedle:ImageDOMElement;
  31. /**
  32. * the minute needle of the clock
  33. */
  34. private var _minuteNeedle:ImageDOMElement;
  35. /**
  36. * the hour needle of the clock
  37. */
  38. private var _hourNeedle:ImageDOMElement;
  39. /**
  40. * the numeric clock
  41. */
  42. private var _clockDisplay:TextDOMElement;
  43. /**
  44. * count the loading of assets
  45. */
  46. private var _loadedAssetsCounter:Int;
  47. /**
  48. * the center of the rotation for the second nedle
  49. * as this needle goes further than the clock half
  50. */
  51. private var _secondNeedleRotationCenter:Point;
  52. /**
  53. * The root of the dom (stage in flash, body in html)
  54. */
  55. private static var rootDOMElement:DOMElement;
  56. /**
  57. * init the root dom element of the publication
  58. */
  59. public static function main()
  60. {
  61. rootDOMElement = new DOMElement(NativeElementManager.getRoot());
  62. //ibnstantiate the clock class
  63. var cl:Clock = new Clock();
  64. }
  65. /**
  66. * starts the loading of all the clocks parts
  67. */
  68. public function new()
  69. {
  70. _loadedAssetsCounter = 0;
  71. ResourceLoaderManager.loadImage("assets/second_needle.png", onSecondNeedleLoaded, function(err){});
  72. ResourceLoaderManager.loadImage("assets/hour_needle.png", onHourNeedleLoaded, function(err){});
  73. ResourceLoaderManager.loadImage("assets/minute_needle.png", onMinuteNeedleLoaded, function(err){});
  74. ResourceLoaderManager.loadImage("assets/clock_background.png", onClockBackgroundLoaded, function(err){});
  75. ResourceLoaderManager.loadImage("assets/clock_foreground.png", onClockForegroundLoaded, function(err){});
  76. }
  77. /**
  78. * Called once all the clock's assets
  79. * are loaded
  80. */
  81. private function init()
  82. {
  83. //init the numeric hour display
  84. _clockDisplay = new TextDOMElement();
  85. //ad all dom elements to the DOM
  86. rootDOMElement.addChild(_clockBackground);
  87. rootDOMElement.addChild(_secondNeedle);
  88. rootDOMElement.addChild(_minuteNeedle);
  89. rootDOMElement.addChild(_hourNeedle);
  90. rootDOMElement.addChild(_clockForeGround);
  91. rootDOMElement.addChild(_clockDisplay);
  92. //prepare the rotation center of the second needle
  93. var secondNeedleOffset:Float = 15.0;
  94. _secondNeedleRotationCenter = {
  95. x:_secondNeedle.width / 2,
  96. y:_secondNeedle.height - secondNeedleOffset
  97. }
  98. //set the pivot point for each needle
  99. _secondNeedle.registrationPoint = point(_secondNeedleRotationCenter);
  100. _minuteNeedle.registrationPoint = constant(center, bottom);
  101. _hourNeedle.registrationPoint = constant(center, bottom);
  102. //move all the needles and the foreground to the center
  103. _secondNeedle.translate(_clockBackground.width / 2 - _secondNeedle.width /2, (_clockBackground.height / 2 - _secondNeedle.height + secondNeedleOffset));
  104. _minuteNeedle.translate(_clockBackground.width / 2 - _minuteNeedle.width / 2, (_clockBackground.height / 2 - _minuteNeedle.height ));
  105. _hourNeedle.translate(_clockBackground.width / 2 - _hourNeedle.width / 2, (_clockBackground.height / 2 - _hourNeedle.height));
  106. _clockForeGround.translate(_clockBackground.width / 2 - _clockForeGround.width / 2, (_clockBackground.height / 2 - _clockForeGround.height / 2));
  107. //set up the text display
  108. _clockDisplay.width = 300;
  109. _clockDisplay.y = _clockBackground.height;
  110. //update the needle position and the text display with the current time
  111. //and enter a loop, refreshing every second
  112. updateTime();
  113. }
  114. /**
  115. * Update the clock display and set another delay
  116. * re-calling this function every second
  117. */
  118. private function updateTime():Void
  119. {
  120. _clockDisplay.text = Date.now().toString();
  121. _secondNeedle.rotation = Math.round(Date.now().getSeconds() * 6);
  122. _minuteNeedle.rotation = Math.round(Date.now().getMinutes() * 6);
  123. _hourNeedle.rotation = Math.round(Date.now().getHours() * (360/12));
  124. Timer.delay(updateTime, 1000);
  125. }
  126. /////////////////////////////////////////////
  127. // ASSET LOADING CALLBACK
  128. // Call the init method once all assets are loaded
  129. ////////////////////////////////////////////
  130. private function onHourNeedleLoaded(imageDOMElement:ImageDOMElement):Void
  131. {
  132. _hourNeedle = imageDOMElement;
  133. checkLoadedAssets();
  134. }
  135. private function onMinuteNeedleLoaded(imageDOMElement:ImageDOMElement):Void
  136. {
  137. _minuteNeedle = imageDOMElement;
  138. checkLoadedAssets();
  139. }
  140. private function onSecondNeedleLoaded(imageDOMElement:ImageDOMElement):Void
  141. {
  142. _secondNeedle = imageDOMElement;
  143. checkLoadedAssets();
  144. }
  145. private function onClockBackgroundLoaded(imageDOMElement:ImageDOMElement):Void
  146. {
  147. _clockBackground = imageDOMElement;
  148. checkLoadedAssets();
  149. }
  150. private function onClockForegroundLoaded(imageDOMElement:ImageDOMElement):Void
  151. {
  152. _clockForeGround = imageDOMElement;
  153. checkLoadedAssets();
  154. }
  155. private function checkLoadedAssets()
  156. {
  157. _loadedAssetsCounter++;
  158. if (_loadedAssetsCounter == 5)
  159. {
  160. init();
  161. }
  162. }
  163. }