AssetWindow.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function AssetWindow::onAdd(%this)
  23. {
  24. }
  25. function AssetWindow::resetCamera(%this)
  26. {
  27. %this.setCameraPosition("0 0");
  28. %this.setCameraZoom(1);
  29. }
  30. function AssetWindow::displayImageAsset(%this, %imageAsset, %assetID)
  31. {
  32. AssetAdmin.AssetScene.clear(true);
  33. %size = %this.getWorldSize(%imageAsset.getImageSize());
  34. %sizeX = getWord(%size, 0);
  35. %sizeY = getWord(%size, 1);
  36. if(!%imageAsset.getExplicitMode() && %imageAsset.getFrameCount() == 1)
  37. {
  38. new Sprite()
  39. {
  40. Scene = AssetAdmin.AssetScene;
  41. Image = %assetID;
  42. size = %size;
  43. BlandColor = "1 1 1 1";
  44. SceneLayer = 1;
  45. Position = "0 0";
  46. BodyType = static;
  47. };
  48. }
  49. else if(!%imageAsset.getExplicitMode() && %imageAsset.getFrameCount() > 1)
  50. {
  51. %cellCountX = %imageAsset.getCellCountX();
  52. %cellCountY = %imageAsset.getCellCountY();
  53. %worldCellWidth = %sizeX / %cellCountX;
  54. %worldCellHeight = %sizeY / %cellCountY;
  55. for(%j = 0; %j < %cellCountY; %j++)
  56. {
  57. for(%i = 0; %i < %cellCountX; %i++)
  58. {
  59. %sprite = new Sprite()
  60. {
  61. Scene = AssetAdmin.AssetScene;
  62. Image = %assetID;
  63. Frame = (%j * %cellCountX) + %i;
  64. size = Vector2Scale(%worldCellWidth SPC %worldCellHeight, 2.8);
  65. position = ((%worldCellWidth * %i) + (%worldCellWidth/2) - (%sizeX/2)) SPC ((-%worldCellHeight * %j) - (%worldCellHeight/2) + (%sizeY/2));
  66. BlendColor = "1 1 1 0";
  67. SceneLayer = 1;
  68. BodyType = static;
  69. };
  70. %sprite.growToTime(Vector2Scale(%worldCellWidth SPC %worldCellHeight, 0.94), 225 + (%i * 20));
  71. %sprite.fadetoTime("1 1 1 1", 225 + (%i * 20));
  72. }
  73. }
  74. }
  75. else if(%imageAsset.getExplicitMode())
  76. {
  77. for(%i = 0; %i < %imageAsset.getExplicitCellCount(); %i++)
  78. {
  79. %worldCellWidth = %sizeX * (%imageAsset.getExplicitCellWidth(%i) / %imageAsset.getImageWidth());
  80. %worldCellHeight = %sizeY * (%imageAsset.getExplicitCellHeight(%i) / %imageAsset.getImageHeight());
  81. %offset = %imageAsset.getExplicitCellOffset(%i);
  82. %offsetX = getWord(%offset, 0);
  83. %offsetY = getWord(%offset, 1);
  84. %worldCellOffsetX = (%sizeX * (%offsetX / %imageAsset.getImageWidth())) - (%sizeX / 2) + (%worldCellWidth / 2);
  85. %worldCellOffsetY = (%sizeY - (%sizeY * (%offsetY / %imageAsset.getImageHeight()))) - (%sizeY / 2) - (%worldCellHeight / 2);
  86. %sprite = new Sprite()
  87. {
  88. Scene = AssetAdmin.AssetScene;
  89. Image = %assetID;
  90. Frame = %i;
  91. size = Vector2Scale(%worldCellWidth SPC %worldCellHeight, 2.8);
  92. position = %worldCellOffsetX SPC %worldCellOffsetY;
  93. BlendColor = "1 1 1 0";
  94. SceneLayer = 1;
  95. BodyType = static;
  96. };
  97. %sprite.growToTime(Vector2Scale(%worldCellWidth SPC %worldCellHeight, 0.94), 225 + (%i * 20));
  98. %sprite.fadetoTime("1 1 1 1", 225 + (%i * 20));
  99. }
  100. }
  101. }
  102. function AssetWindow::displayAnimationAsset(%this, %imageAsset, %animationAsset, %assetID)
  103. {
  104. AssetAdmin.AssetScene.clear(true);
  105. %size = %this.getWorldSize(%imageAsset.getFrameSize(0));
  106. new Sprite()
  107. {
  108. Scene = AssetAdmin.AssetScene;
  109. Animation = %assetID;
  110. size = %size;
  111. BlandColor = "1 1 1 1";
  112. SceneLayer = 1;
  113. Position = "0 0";
  114. BodyType = static;
  115. };
  116. }
  117. function AssetWindow::displayParticleAsset(%this, %particleAsset, %assetID)
  118. {
  119. AssetAdmin.AssetScene.clear(true);
  120. new ParticlePlayer()
  121. {
  122. Scene = AssetAdmin.AssetScene;
  123. Particle = %assetID;
  124. size = "10 10";
  125. BlandColor = "1 1 1 1";
  126. SceneLayer = 1;
  127. Position = "0 0";
  128. BodyType = static;
  129. };
  130. }
  131. function AssetWindow::displayFontAsset(%this, %fontAsset, %assetID)
  132. {
  133. AssetAdmin.AssetScene.clear(true);
  134. %size = %this.getWorldSize("10 10");
  135. new TextSprite()
  136. {
  137. Scene = AssetAdmin.AssetScene;
  138. Font = %assetID;
  139. fontSize = 4;
  140. size = %size;
  141. BlandColor = "1 1 1 1";
  142. SceneLayer = 1;
  143. Position = "0 0";
  144. BodyType = static;
  145. Text = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9";
  146. OverflowModeY = "Visible";
  147. OverflowModeX = "Wrap";
  148. TextAlignment = "center";
  149. TextVAlignment = "middle";
  150. };
  151. }
  152. function AssetWindow::displayAudioAsset(%this, %audioAsset, %assetID)
  153. {
  154. AssetAdmin.AssetScene.clear(true);
  155. AssetAdmin.audioPlayButtonContainer.setVisible(true);
  156. AssetAdmin.AssetWindow.setVisible(false);
  157. AssetAdmin.audioPlayButton.resetSound();
  158. AssetAdmin.audioPlayButton.assetID = %assetID;
  159. AssetAdmin.audioPlayButton.asset = %audioAsset;
  160. AssetAdmin.audioPlayButton.onClick();
  161. }
  162. function AssetWindow::displaySpineAsset(%this, %spineAsset, %assetID)
  163. {
  164. AssetAdmin.AssetScene.clear(true);
  165. //good luck!
  166. }
  167. function AssetWindow::getWorldSize(%this, %size)
  168. {
  169. %cameraSize = %this.getCameraSize();
  170. %cameraX = getWord(%cameraSize, 0);
  171. %cameraY = getWord(%cameraSize, 1);
  172. %x = getWord(%size, 0);
  173. %y = getWord(%size, 1);
  174. %inset = 10;
  175. //calculate the finished rect 2 ways
  176. %finX1 = %cameraX - %inset;
  177. %finY1 = (%y * %finX1) / %x;
  178. %finY2 = %cameraY - %inset;
  179. %finX2 = (%x * %finY2) / %y;
  180. //Return 1 if it's bigger and fits or if it's smaller and 2 doesn't fit
  181. if((%finX1 > %finX2 && %finX1 <= (%cameraX - %inset) && %finY1 <= (%cameraY - %inset)) || (%finX1 < %finX2 && (%finX2 > (%cameraX - %inset) || %finY2 > (%cameraY - %inset))))
  182. {
  183. return %finX1 SPC %finY1;
  184. }
  185. else
  186. {
  187. //otherwise use the second finished rect
  188. return %finX2 SPC %finY2;
  189. }
  190. }
  191. function AssetWindow::onExtentChange(%this, %d)
  192. {
  193. %x = getWord(%d, 2);
  194. %y = getWord(%d, 3);
  195. if(%x >= %y)
  196. {
  197. %mult = 100/%x;
  198. %areaY = (%y * %mult);
  199. %topLeft = "-50" SPC (%areaY / 2);
  200. %bottomRight = "50" SPC -(%areaY / 2);
  201. }
  202. else
  203. {
  204. %mult = 100/%y;
  205. %areaX = (%x * %mult);
  206. %topLeft = (%areaX / 2) SPC "-50";
  207. %bottomRight = -(%areaX / 2) SPC "50";
  208. }
  209. %area = %topLeft SPC %bottomRight;
  210. %this.setCameraArea(%area);
  211. %this.setViewLimitOn(%area);
  212. if(isObject(AssetAdmin.chosenButton))
  213. {
  214. AssetAdmin.chosenButton.onClick();
  215. }
  216. }
  217. function AssetWindow::onMouseWheelUp(%this)
  218. {
  219. %zoom = %this.getTargetCameraZoom();
  220. if(%zoom > 1)
  221. {
  222. //prevent gradual rounding errors
  223. %zoom = mRound(%zoom);
  224. }
  225. if(%zoom == 1)
  226. {
  227. %this.setTargetCameraPosition("0 0");
  228. }
  229. %this.setTargetCameraZoom(mGetMin(16, %zoom * 2));
  230. %this.startCameraMove(0.2);
  231. }
  232. function AssetWindow::onMouseWheelDown(%this)
  233. {
  234. %zoom = %this.getTargetCameraZoom();
  235. if(%zoom > 1)
  236. {
  237. //prevent gradual rounding errors
  238. %zoom = mRound(%zoom);
  239. }
  240. if(%zoom == 1)
  241. {
  242. %this.setTargetCameraPosition("0 0");
  243. }
  244. %this.setTargetCameraZoom(mGetMax(0.0625, %zoom / 2));
  245. %this.startCameraMove(0.2);
  246. }