Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.85 KB | None | 0 0
  1. var changeAnimationDuration = 500;
  2. var resizeAnimationDuration = 1000;
  3.  
  4. function Marker(poiData) {
  5.  
  6. this.poiData = poiData;
  7. this.isSelected = false;
  8.  
  9. /*
  10. With AR.PropertyAnimations you are able to animate almost any property of ARchitect objects. This sample
  11. will animate the opacity of both background drawables so that one will fade out while the other one fades
  12. in. The scaling is animated too. The marker size changes over time so the labels need to be animated too in
  13. order to keep them relative to the background drawable. AR.AnimationGroups are used to synchronize all
  14. animations in parallel or sequentially.
  15. */
  16. this.animationGroupIdle = null;
  17. this.animationGroupSelected = null;
  18.  
  19. /* Create the AR.GeoLocation from the poi data. */
  20. var markerLocation = new AR.GeoLocation(poiData.latitude, poiData.longitude, poiData.altitude);
  21.  
  22. /* Create an AR.ImageDrawable for the marker in idle state. */
  23. this.markerDrawableIdle = new AR.ImageDrawable(World.markerDrawableIdle, 2.5, {
  24. zOrder: 0,
  25. opacity: 1.0,
  26. /*
  27. To react on user interaction, an onClick property can be set for each AR.Drawable. The property is a
  28. function which will be called each time the user taps on the drawable. The function called on each tap
  29. is returned from the following helper function defined in marker.js. The function returns a function
  30. which checks the selected state with the help of the variable isSelected and executes the appropriate
  31. function. The clicked marker is passed as an argument.
  32. */
  33. onClick: Marker.prototype.getOnClickTrigger(this)
  34. });
  35.  
  36. /* Create an AR.ImageDrawable for the marker in selected state. */
  37. this.markerDrawableSelected = new AR.ImageDrawable(World.markerDrawableSelected, 2.5, {
  38. zOrder: 0,
  39. opacity: 0.0,
  40. onClick: null
  41. });
  42.  
  43. /* Create an AR.Label for the marker's title . */
  44. this.titleLabel = new AR.Label(poiData.title.trunc(10), 1, {
  45. zOrder: 1,
  46. translate: {
  47. y: 0.55
  48. },
  49. style: {
  50. textColor: '#FFFFFF',
  51. fontStyle: AR.CONST.FONT_STYLE.BOLD
  52. }
  53. });
  54.  
  55. this.descriptionLabel = new AR.Label(poiData.description.trunc(15), 0.8, {
  56. zOrder: 1,
  57. translate: {
  58. y: -0.55
  59. },
  60. style: {
  61. textColor: '#FFFFFF'
  62. }
  63. });
  64.  
  65. /*
  66. Create an AR.ImageDrawable using the AR.ImageResource for the direction indicator which was created in the
  67. World. Set options regarding the offset and anchor of the image so that it will be displayed correctly on
  68. the edge of the screen.
  69. */
  70. this.directionIndicatorDrawable = new AR.ImageDrawable(World.markerDrawableDirectionIndicator, 0.1, {
  71. enabled: false,
  72. verticalAnchor: AR.CONST.VERTICAL_ANCHOR.TOP
  73. });
  74.  
  75. /*
  76. Create the AR.GeoObject with the drawable objects and define the AR.ImageDrawable as an indicator target on
  77. the marker AR.GeoObject. The direction indicator is displayed automatically when necessary. AR.Drawable
  78. subclasses (e.g. AR.Circle) can be used as direction indicators.
  79. */
  80. this.markerObject = new AR.GeoObject(markerLocation, {
  81. drawables: {
  82. cam: [this.markerDrawableIdle, this.markerDrawableSelected, this.titleLabel, this.descriptionLabel],
  83. indicator: this.directionIndicatorDrawable
  84. }
  85. });
  86.  
  87. return this;
  88. }
  89.  
  90. Marker.prototype.getOnClickTrigger = function(marker) {
  91.  
  92. /*
  93. The setSelected and setDeselected functions are prototype Marker functions.
  94.  
  95. Both functions perform the same steps but inverted, hence only one function (setSelected) is covered in
  96. detail. Three steps are necessary to select the marker. First the state will be set appropriately. Second
  97. the background drawable will be enabled and the standard background disabled. This is done by setting the
  98. opacity property to 1.0 for the visible state and to 0.0 for an invisible state. Third the onClick function
  99. is set only for the background drawable of the selected marker.
  100. */
  101.  
  102. return function() {
  103.  
  104. if (!Marker.prototype.isAnyAnimationRunning(marker)) {
  105. if (marker.isSelected) {
  106.  
  107. Marker.prototype.setDeselected(marker);
  108.  
  109. } else {
  110. Marker.prototype.setSelected(marker);
  111. try {
  112. World.onMarkerSelected(marker);
  113. } catch (err) {
  114. alert(err);
  115. }
  116.  
  117. }
  118. } else {
  119. AR.logger.debug('a animation is already running');
  120. }
  121.  
  122. return true;
  123. };
  124. };
  125.  
  126. /*
  127. Property Animations allow constant changes to a numeric value/property of an object, dependent on start-value,
  128. end-value and the duration of the animation. Animations can be seen as functions defining the progress of the
  129. change on the value. The Animation can be parametrized via easing curves.
  130. */
  131. Marker.prototype.setSelected = function(marker) {
  132.  
  133. marker.isSelected = true;
  134.  
  135. /* New: . */
  136. if (marker.animationGroupSelected === null) {
  137. var easingCurve = new AR.EasingCurve(AR.CONST.EASING_CURVE_TYPE.EASE_OUT_ELASTIC, {
  138. amplitude: 2.0
  139. });
  140.  
  141. /* Create AR.PropertyAnimation that animates the opacity to 0.0 in order to hide the idle-state-drawable. */
  142. var hideIdleDrawableAnimation = new AR.PropertyAnimation(
  143. marker.markerDrawableIdle, "opacity", null, 0.0, changeAnimationDuration);
  144. /* Create AR.PropertyAnimation that animates the opacity to 1.0 in order to show the selected-state-drawable. */
  145. var showSelectedDrawableAnimation = new AR.PropertyAnimation(
  146. marker.markerDrawableSelected, "opacity", null, 1.0, changeAnimationDuration);
  147.  
  148. /* Create AR.PropertyAnimation that animates the scaling of the idle-state-drawable to 1.2. */
  149. var idleDrawableResizeAnimationX = new AR.PropertyAnimation(
  150. marker.markerDrawableIdle, 'scale.x', null, 1.2, resizeAnimationDuration, easingCurve);
  151. /* Create AR.PropertyAnimation that animates the scaling of the selected-state-drawable to 1.2. */
  152. var selectedDrawableResizeAnimationX = new AR.PropertyAnimation(
  153. marker.markerDrawableSelected, 'scale.x', null, 1.2, resizeAnimationDuration, easingCurve);
  154. /* Create AR.PropertyAnimation that animates the scaling of the title label to 1.2. */
  155. var titleLabelResizeAnimationX = new AR.PropertyAnimation(
  156. marker.titleLabel, 'scale.x', null, 1.2, resizeAnimationDuration, easingCurve);
  157. /* Create AR.PropertyAnimation that animates the scaling of the description label to 1.2. */
  158. var descriptionLabelResizeAnimationX = new AR.PropertyAnimation(
  159. marker.descriptionLabel, 'scale.x', null, 1.2, resizeAnimationDuration, easingCurve);
  160.  
  161. /* Create AR.PropertyAnimation that animates the scaling of the idle-state-drawable to 1.2. */
  162. var idleDrawableResizeAnimationY = new AR.PropertyAnimation(
  163. marker.markerDrawableIdle, 'scale.y', null, 1.2, resizeAnimationDuration, easingCurve);
  164. /* Create AR.PropertyAnimation that animates the scaling of the selected-state-drawable to 1.2. */
  165. var selectedDrawableResizeAnimationY = new AR.PropertyAnimation(
  166. marker.markerDrawableSelected, 'scale.y', null, 1.2, resizeAnimationDuration, easingCurve);
  167. /* Create AR.PropertyAnimation that animates the scaling of the title label to 1.2. */
  168. var titleLabelResizeAnimationY = new AR.PropertyAnimation(
  169. marker.titleLabel, 'scale.y', null, 1.2, resizeAnimationDuration, easingCurve);
  170. /* Create AR.PropertyAnimation that animates the scaling of the description label to 1.2. */
  171. var descriptionLabelResizeAnimationY = new AR.PropertyAnimation(
  172. marker.descriptionLabel, 'scale.y', null, 1.2, resizeAnimationDuration, easingCurve);
  173.  
  174. /*
  175. There are two types of AR.AnimationGroups. Parallel animations are running at the same time,
  176. sequentials are played one after another. This example uses a parallel AR.AnimationGroup.
  177. */
  178. marker.animationGroupSelected = new AR.AnimationGroup(AR.CONST.ANIMATION_GROUP_TYPE.PARALLEL, [
  179. hideIdleDrawableAnimation,
  180. showSelectedDrawableAnimation,
  181. idleDrawableResizeAnimationX,
  182. selectedDrawableResizeAnimationX,
  183. titleLabelResizeAnimationX,
  184. descriptionLabelResizeAnimationX,
  185. idleDrawableResizeAnimationY,
  186. selectedDrawableResizeAnimationY,
  187. titleLabelResizeAnimationY,
  188. descriptionLabelResizeAnimationY
  189. ]);
  190. }
  191.  
  192. /* Removes function that is set on the onClick trigger of the idle-state marker. */
  193. marker.markerDrawableIdle.onClick = null;
  194. /* Sets the click trigger function for the selected state marker. */
  195. marker.markerDrawableSelected.onClick = Marker.prototype.getOnClickTrigger(marker);
  196.  
  197. /* Enables the direction indicator drawable for the current marker. */
  198. marker.directionIndicatorDrawable.enabled = true;
  199. /* Starts the selected-state animation. */
  200. marker.animationGroupSelected.start();
  201. };
  202.  
  203. Marker.prototype.setDeselected = function(marker) {
  204.  
  205. marker.isSelected = false;
  206.  
  207. if (marker.animationGroupIdle === null) {
  208. var easingCurve = new AR.EasingCurve(AR.CONST.EASING_CURVE_TYPE.EASE_OUT_ELASTIC, {
  209. amplitude: 2.0
  210. });
  211.  
  212. /* Create AR.PropertyAnimation that animates the opacity to 1.0 in order to show the idle-state-drawable. */
  213. var showIdleDrawableAnimation = new AR.PropertyAnimation(
  214. marker.markerDrawableIdle, "opacity", null, 1.0, changeAnimationDuration);
  215. /* Create AR.PropertyAnimation that animates the opacity to 0.0 in order to hide the selected-state-drawable. */
  216. var hideSelectedDrawableAnimation = new AR.PropertyAnimation(
  217. marker.markerDrawableSelected, "opacity", null, 0, changeAnimationDuration);
  218. /* Create AR.PropertyAnimation that animates the scaling of the idle-state-drawable to 1.0. */
  219. var idleDrawableResizeAnimationX = new AR.PropertyAnimation(
  220. marker.markerDrawableIdle, 'scale.x', null, 1.0, resizeAnimationDuration, easingCurve);
  221. /* Create AR.PropertyAnimation that animates the scaling of the selected-state-drawable to 1.0. */
  222. var selectedDrawableResizeAnimationX = new AR.PropertyAnimation(
  223. marker.markerDrawableSelected, 'scale.x', null, 1.0, resizeAnimationDuration, easingCurve);
  224. /* Create AR.PropertyAnimation that animates the scaling of the title label to 1.0. */
  225. var titleLabelResizeAnimationX = new AR.PropertyAnimation(
  226. marker.titleLabel, 'scale.x', null, 1.0, resizeAnimationDuration, easingCurve);
  227. /* Create AR.PropertyAnimation that animates the scaling of the description label to 1.0. */
  228. var descriptionLabelResizeAnimationX = new AR.PropertyAnimation(
  229. marker.descriptionLabel, 'scale.x', null, 1.0, resizeAnimationDuration, easingCurve);
  230. /* Create AR.PropertyAnimation that animates the scaling of the idle-state-drawable to 1.0. */
  231. var idleDrawableResizeAnimationY = new AR.PropertyAnimation(
  232. marker.markerDrawableIdle, 'scale.y', null, 1.0, resizeAnimationDuration, easingCurve);
  233. /* Create AR.PropertyAnimation that animates the scaling of the selected-state-drawable to 1.0. */
  234. var selectedDrawableResizeAnimationY = new AR.PropertyAnimation(
  235. marker.markerDrawableSelected, 'scale.y', null, 1.0, resizeAnimationDuration, easingCurve);
  236. /* Create AR.PropertyAnimation that animates the scaling of the title label to 1.0. */
  237. var titleLabelResizeAnimationY = new AR.PropertyAnimation(
  238. marker.titleLabel, 'scale.y', null, 1.0, resizeAnimationDuration, easingCurve);
  239. /* Create AR.PropertyAnimation that animates the scaling of the description label to 1.0. */
  240. var descriptionLabelResizeAnimationY = new AR.PropertyAnimation(
  241. marker.descriptionLabel, 'scale.y', null, 1.0, resizeAnimationDuration, easingCurve);
  242.  
  243. /*
  244. There are two types of AR.AnimationGroups. Parallel animations are running at the same time,
  245. sequentials are played one after another. This example uses a parallel AR.AnimationGroup.
  246. */
  247. marker.animationGroupIdle = new AR.AnimationGroup(AR.CONST.ANIMATION_GROUP_TYPE.PARALLEL, [
  248. showIdleDrawableAnimation,
  249. hideSelectedDrawableAnimation,
  250. idleDrawableResizeAnimationX,
  251. selectedDrawableResizeAnimationX,
  252. titleLabelResizeAnimationX,
  253. descriptionLabelResizeAnimationX,
  254. idleDrawableResizeAnimationY,
  255. selectedDrawableResizeAnimationY,
  256. titleLabelResizeAnimationY,
  257. descriptionLabelResizeAnimationY
  258. ]);
  259. }
  260.  
  261. /* Sets the click trigger function for the idle state marker. */
  262. marker.markerDrawableIdle.onClick = Marker.prototype.getOnClickTrigger(marker);
  263. /* Removes function that is set on the onClick trigger of the selected-state marker. */
  264. marker.markerDrawableSelected.onClick = null;
  265.  
  266. /* Disables the direction indicator drawable for the current marker. */
  267. marker.directionIndicatorDrawable.enabled = false;
  268. /* Starts the idle-state animation. */
  269. marker.animationGroupIdle.start();
  270. };
  271.  
  272. Marker.prototype.isAnyAnimationRunning = function(marker) {
  273.  
  274. if (marker.animationGroupIdle === null || marker.animationGroupSelected === null) {
  275. return false;
  276. } else {
  277. return marker.animationGroupIdle.isRunning() === true || marker.animationGroupSelected.isRunning() === true;
  278. }
  279. };
  280.  
  281. /* Will truncate all strings longer than given max-length "n". e.g. "foobar".trunc(3) -> "foo...". */
  282. String.prototype.trunc = function(n) {
  283. return this.substr(0, n - 1) + (this.length > n ? '...' : '');
  284. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement