SayImage.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. return Elements.getStyle("." + this.imgName) != undefined;
  11. }
  12. public constructor (imgName : string) {
  13. this.imgName = imgName;
  14. if (!SayImage.loadTimeCheck && SayImage.imageNames.indexOf(this) == -1) {
  15. SayImage.imageNames.push(this);
  16. }
  17. }
  18. public getImageElement () : Element {
  19. let img = document.createElement("div");
  20. if (this.isImageDefined()) {
  21. img.classList.add(this.imgName);
  22. img.classList.add("contentImage");
  23. } else {
  24. img.classList.add("error");
  25. img.appendChild(document.createTextNode("Image \"" + this.imgName + "\" not found."));
  26. }
  27. img.addEventListener("click", () => {
  28. SayImage.showInViewer(this);
  29. });
  30. Controls.KeyHandler.applyCode(img, Controls.KeyHandler.imageKeyCode.getValue());
  31. return img;
  32. }
  33. public getPrintedElement () {
  34. return [this.getImageElement()];
  35. }
  36. public static doLoadTimeCheck() {
  37. for (let i = 0; i < SayImage.imageNames.length; i++) {
  38. let image = SayImage.imageNames[i];
  39. if (!image.isImageDefined()) {
  40. if (Settings.hardDebug) {
  41. Elements.CurrentTurnHandler.printAsError("Image \"" + image.imgName + "\" was not found.");
  42. }
  43. console.error("Image \"" + image.imgName + "\" was not found.");
  44. }
  45. }
  46. SayImage.loadTimeCheck = true;
  47. }
  48. public static showInViewer (image : SayImage) {
  49. if (!(SayImage.imageViewer.style.display == 'block')) {
  50. SayImage.imageViewer.addEventListener("click", () => {
  51. SayImage.imageViewer.style.display = "none";
  52. });
  53. SayImage.imageViewer.className = image.imgName;
  54. SayImage.imageViewer.style.display = "block";
  55. } else {
  56. SayImage.imageViewer.style.display = "none";
  57. }
  58. }
  59. }
  60. module MachineBegins {
  61. export let ImageLoadTimeCheck = MachineBegins.rulebook.createAndAddRule({
  62. firstPriority : Rule.PRIORITY_HIGHEST, // We want this to print to the top of the console
  63. name : "Check for undefined images",
  64. code : () => {
  65. try {
  66. SayImage.doLoadTimeCheck();
  67. } catch (e) {
  68. console.warn("Can't check for undefined images.");
  69. }
  70. }
  71. });
  72. }