NewFontAssetDialog.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. function NewFontAssetDialog::init(%this, %width, %height)
  2. {
  3. //Get the dialog contents
  4. %window = %this.getObject(0);
  5. %content = %window.getObject(0);
  6. //Create the file text box
  7. %form = new GuiGridCtrl()
  8. {
  9. class = "EditorForm";
  10. extent = %width SPC %height;
  11. cellSizeX = %width;
  12. cellSizeY = 50;
  13. };
  14. %form.addListener(%this);
  15. %item = %form.addFormItem("Font File", %width SPC 30);
  16. %this.imageFileBox = %form.createFileOpenItem(%item, "Bitmap Font (*.FNT)|*.FNT", "Open Bitmap Font File");
  17. %this.imageFileBox.Command = %this.getId() @ ".Validate();";
  18. %item = %form.addFormItem("Asset Name", %width SPC 30);
  19. %this.assetNameBox = %form.createTextEditItem(%item);
  20. %this.assetNameBox.Command = %this.getId() @ ".Validate();";
  21. %item = %form.addFormItem("Target Module", %width SPC 30);
  22. %this.moduleNameBox = %form.createTextEditItem(%item);
  23. %this.moduleNameBox.setActive(false);
  24. %content.add(%form);
  25. %this.feedback = new GuiControl()
  26. {
  27. HorizSizing = "right";
  28. VertSizing = "bottom";
  29. Position = "12 170";
  30. Extent = (%width - 24) SPC 80;
  31. text = "Select a Bimap Font File to get started!";
  32. };
  33. ThemeManager.setProfile(%this.feedback, "infoProfile");
  34. %this.cancelButton = new GuiButtonCtrl()
  35. {
  36. HorizSizing = "right";
  37. VertSizing = "bottom";
  38. Position = "478 270";
  39. Extent = "100 30";
  40. Text = "Cancel";
  41. Command = %this.getID() @ ".onClose();";
  42. };
  43. ThemeManager.setProfile(%this.cancelButton, "buttonProfile");
  44. %this.createButton = new GuiButtonCtrl()
  45. {
  46. HorizSizing = "right";
  47. VertSizing = "bottom";
  48. Position = "588 268";
  49. Extent = "100 34";
  50. Text = "Create";
  51. Command = %this.getID() @ ".onCreate();";
  52. };
  53. ThemeManager.setProfile(%this.createButton, "primaryButtonProfile");
  54. %content.add(%this.feedback);
  55. %content.add(%this.cancelButton);
  56. %content.add(%this.createButton);
  57. %this.prevFile = "";
  58. }
  59. function NewFontAssetDialog::Validate(%this)
  60. {
  61. %this.createButton.active = false;
  62. %file = %this.imageFileBox.getText();
  63. %assetName = %this.assetNameBox.getText();
  64. if(%file !$= %this.prevFile)
  65. {
  66. //remove the extention
  67. %fileNameSansExt = fileBase(fileName(%file));
  68. if(%fileNameSansExt !$= "" && %assetName $= "")
  69. {
  70. %assetName = %fileNameSansExt;
  71. %this.assetNameBox.setText(%fileNameSansExt);
  72. }
  73. %modSig = EditorCore.findModuleOfPath(%file);
  74. %this.moduleNameBox.setText(%modSig);
  75. %this.prevFile = %file;
  76. }
  77. %assetPath = filePath(%file) @ "/" @ %assetName @ ".asset.taml";
  78. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  79. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  80. %assetID = %moduleName @ ":" @ %assetName;
  81. if(!isFile(%file))
  82. {
  83. //We need a real image file!
  84. %this.feedback.setText("An existing font file must be used to create a font asset!");
  85. return false;
  86. }
  87. if(%file $= "")
  88. {
  89. %this.feedback.setText("Select a Bitmap Font File to get started!");
  90. return false;
  91. }
  92. if(%assetName $= "")
  93. {
  94. %this.feedback.setText("A font asset must have an Asset Name.");
  95. return false;
  96. }
  97. if(%moduleName $= "")
  98. {
  99. %this.feedback.setText("You can only create a font asset inside of a module.");
  100. return false;
  101. }
  102. %button = AssetAdmin.Dictionary["FontAsset"].getButton(%assetID);
  103. if(isObject(%button))
  104. {
  105. %this.feedback.setText("An asset by this name already exists in this module. Try choosing a different name.");
  106. return false;
  107. }
  108. %this.createButton.active = true;
  109. %this.feedback.setText("Press the Create button to open the new asset for editing.");
  110. return true;
  111. }
  112. function NewFontAssetDialog::onClose(%this)
  113. {
  114. Canvas.popDialog(%this);
  115. }
  116. function NewFontAssetDialog::onCreate(%this)
  117. {
  118. if(%this.validate())
  119. {
  120. %file = makeFullPath(%this.imageFileBox.getText());
  121. %assetName = %this.assetNameBox.getText();
  122. %assetPath = filePath(%file) @ "/" @ %assetName @ ".asset.taml";
  123. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  124. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  125. %assetID = %moduleName @ ":" @ %assetName;
  126. //Time to create a new file
  127. %newAsset = new FontAsset()
  128. {
  129. assetName = %assetName;
  130. fontFile = %file;
  131. };
  132. %assetImportSuccessful = TAMLWrite(%newAsset, %assetPath);
  133. %moduleDef = ModuleDatabase.findModule(%moduleName, %moduleVersion);
  134. AssetDatabase.addDeclaredAsset(%moduleDef, %assetPath);
  135. //Refresh the asset so that the loose file will be a path relative to the asset file.
  136. AssetDatabase.refreshAsset(%assetID);
  137. //Do we already have this button?
  138. %button = AssetAdmin.Dictionary["FontAsset"].getButton(%assetID);
  139. if(!isObject(%button))
  140. {
  141. //Load it into the image dictionary
  142. %button = AssetAdmin.Dictionary["FontAsset"].addButton(%assetID);
  143. }
  144. %button.onClick();
  145. %this.onClose();
  146. }
  147. }
  148. function NewFontAssetDialog::onFileOpened(%this, %textBox)
  149. {
  150. %this.Validate();
  151. }