AssetDictionaryButton.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 AssetDictionaryButton::onAdd(%this)
  23. {
  24. %this.call("load" @ %this.type, %this.assetID);
  25. %this.assetID = "";
  26. }
  27. function AssetDictionaryButton::loadImageAsset(%this, %assetID)
  28. {
  29. %imageAsset = AssetDatabase.acquireAsset(%assetID);
  30. %extent = %this.getIconExtent(%imageAsset.getFrameSize(0));
  31. %texture = %this.buildIcon(%extent);
  32. %texture.setImage(%assetID);
  33. %this.ImageAsset = %imageAsset;
  34. %this.ImageAssetID = %assetID;
  35. %this.add(%texture);
  36. }
  37. function AssetDictionaryButton::loadAnimationAsset(%this, %assetID)
  38. {
  39. %animationAsset = AssetDatabase.acquireAsset(%assetID);
  40. %imageAssetID = %animationAsset.getImage();
  41. %imageAsset = AssetDatabase.acquireAsset(%imageAssetID);
  42. %extent = %this.getIconExtent(%imageAsset.getFrameSize(0));
  43. %texture = %this.buildIcon(%extent);
  44. %texture.setAnimation(%assetID);
  45. %this.ImageAsset = %imageAsset;
  46. %this.imageAssetID = %imageAssetID;
  47. %this.AnimationAsset = %animationAsset;
  48. %this.AnimationAssetID = %assetID;
  49. %this.add(%texture);
  50. }
  51. function AssetDictionaryButton::getIconExtent(%this, %size)
  52. {
  53. %x = getWord(%size, 0);
  54. %y = getWord(%size, 1);
  55. %ratio = %x / %y;
  56. %extent = "50 50";
  57. if(%x > %y)
  58. {
  59. %extent = "50" SPC (50/%ratio);
  60. }
  61. else if(%x < %y)
  62. {
  63. %extent = (50*%ratio) SPC "50";
  64. }
  65. return %extent;
  66. }
  67. function AssetDictionaryButton::buildIcon(%this, %extent)
  68. {
  69. %texture = new GuiSpriteCtrl()
  70. {
  71. class = "AssetDictionarySprite";
  72. HorizSizing="center";
  73. VertSizing="center";
  74. Extent = %extent;
  75. minExtent=%extent;
  76. Position = "0 0";
  77. };
  78. ThemeManager.setProfile(%texture, "spriteProfile");
  79. return %texture;
  80. }
  81. function AssetDictionaryButton::onClick(%this)
  82. {
  83. if(isObject(%this.AnimationAsset) && %this.AnimationAssetID !$= "")
  84. {
  85. AssetAdmin.AssetWindow.displayAnimationAsset(%this.imageAsset, %this.AnimationAsset, %this.AnimationAssetID);
  86. }
  87. else if(isObject(%this.ImageAsset) && %this.ImageAssetID !$= "")
  88. {
  89. AssetAdmin.AssetWindow.displayImageAsset(%this.ImageAsset, %this.ImageAssetID);
  90. }
  91. }
  92. function AssetDictionaryButton::onRemove(%this)
  93. {
  94. if(isObject(%this.ImageAsset))
  95. {
  96. AssetDatabase.releaseAsset(%this.ImageAssetID);
  97. }
  98. if(isObject(%this.AnimationAsset))
  99. {
  100. AssetDatabase.releaseAsset(%this.AnimationAssetID);
  101. }
  102. }