SayImage.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /// <reference path="../Say.ts" />
  2. /// <reference path="../../../Controls/Modules/KeyHandler.ts" />
  3. /// <reference path="../../../World/MachineBegins.ts" />
  4. class SayImage implements PrintableElement {
  5. public static imageNames : Array<SayImage> = [];
  6. public static loadTimeCheck : boolean = false;
  7. public static imageViewer : HTMLElement = <HTMLElement> document.getElementById("imageViewer");
  8. private imgName : string;
  9. private isImageDefined () {
  10. try {
  11. for(var i = 0; i < document.styleSheets.length; i++) {
  12. var rules = document.styleSheets[i]['rules'] || document.styleSheets[i]['cssRules'];
  13. for(var x in rules) {
  14. if(typeof rules[x].selectorText == 'string' && rules[x].selectorText == "." + this.imgName) {
  15. return true;
  16. }
  17. }
  18. }
  19. return false;
  20. } catch (e) {
  21. console.warn("Can't check for undefined images.");
  22. return true;
  23. }
  24. }
  25. public constructor (imgName : string) {
  26. this.imgName = imgName;
  27. if (!SayImage.loadTimeCheck && SayImage.imageNames.indexOf(this) == -1) {
  28. SayImage.imageNames.push(this);
  29. }
  30. }
  31. public getImageElement () : Element {
  32. let img = document.createElement("div");
  33. if (this.isImageDefined()) {
  34. img.classList.add(this.imgName);
  35. img.classList.add("contentImage");
  36. } else {
  37. img.classList.add("error");
  38. img.appendChild(document.createTextNode("Image \"" + this.imgName + "\" not found."));
  39. }
  40. img.addEventListener("click", () => {
  41. SayImage.showInViewer(this);
  42. });
  43. Controls.KeyHandler.applyCode(img, Controls.KeyHandler.imageKeyCode.getValue());
  44. return img;
  45. }
  46. public getPrintedElement () {
  47. return [this.getImageElement()];
  48. }
  49. public static doLoadTimeCheck() {
  50. for (let i = 0; i < SayImage.imageNames.length; i++) {
  51. let image = SayImage.imageNames[i];
  52. if (!image.isImageDefined()) {
  53. if (Settings.hardDebug) {
  54. Elements.CurrentTurnHandler.printAsError("Image \"" + image.imgName + "\" was not found.");
  55. }
  56. console.error("Image \"" + image.imgName + "\" was not found.");
  57. }
  58. }
  59. SayImage.loadTimeCheck = true;
  60. }
  61. public static showInViewer (image : SayImage) {
  62. if (!(SayImage.imageViewer.style.display == 'block')) {
  63. SayImage.imageViewer.addEventListener("click", () => {
  64. SayImage.imageViewer.style.display = "none";
  65. });
  66. SayImage.imageViewer.className = image.imgName;
  67. SayImage.imageViewer.style.display = "block";
  68. } else {
  69. SayImage.imageViewer.style.display = "none";
  70. }
  71. }
  72. }
  73. module MachineBegins {
  74. export let ImageLoadTimeCheck = MachineBegins.rulebook.createAndAddRule({
  75. firstPriority : Rule.PRIORITY_HIGHEST, // We want this to print to the top of the console
  76. name : "Check for undefined images",
  77. code : () => {
  78. try {
  79. SayImage.doLoadTimeCheck();
  80. } catch (e) {
  81. console.warn("Can't check for undefined images.");
  82. }
  83. }
  84. });
  85. }