Advertisement
Astfgl

IconCaptions

Apr 9th, 2019 (edited)
3,841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Icon captions
  3. // by Astfgl
  4. // Date: 09/06/2017
  5. // revision 09/04/2019 for public use
  6. // revision 10/04/2019: sometimes x coord of caption window would go below 0
  7. // 13/04/2019 : caught all the missing base windows
  8. // 16/04/2019 : fixed window battle status from showing icons below other windows
  9. // 17/04/2019 : added support for multiple line captions, and fixed caption window width
  10. // 22/04/2019 : Yanfly visual state and MOGBH compatibility patch
  11. // 24/04/2019  : Added individual conditions for captions, added set caption and set cond functions
  12. // Thanks to HeroicJay on the rpgmakerweb forums for pointing me to the
  13. // textWidthEx function in the base code, which I copied here.
  14. //=============================================================================
  15.  
  16. /*:
  17.  * @plugindesc Create icon captions
  18.  * @author Astfgl
  19.  *
  20.  * @param ShowCaptions
  21.  * @type Switch
  22.  * @desc This switch will be used in game to turn the captions on or off.
  23.  *
  24.  * @param DefaultText
  25.  * @default
  26.  * @desc The default text displayed when nothing is specified.
  27.  *
  28.  * @help You must setup a caption's text from within the plugin.
  29.  * Open the text file with notepad and follow the template provided.
  30.  * Icons without captions set will display what is inside the DefaultText parameter.
  31.  * If a caption is set to "" the window will not display.
  32.  *
  33.  * You can add or modifiy a caption in game with:
  34.  * IconCaptions.setCaption(iconNumber,text)
  35.  *
  36.  * You can modifiy a caption condition in game with:
  37.  * IconCaptions.setCond(iconNumber,text)
  38.  * The text must be a valid javascript statement, which will be evaled when hovering
  39.  * the caption. If it evals to true, the caption will show.
  40.  */
  41.  
  42.  var Imported = Imported || {};
  43. (function(){
  44.      var parameters = PluginManager.parameters("IconCaptions")
  45.      var showCaptions = parameters.ShowCaptions
  46.      var defText = parameters.DefaultText
  47.      
  48.      //Set the icon captions in here.
  49.      IconCaptions = {
  50.          313: "Gold \nMultiple lines",
  51.          cond313: "$gameParty.gold() > 0",
  52.          //template: iconId: "Text",
  53.          //you can use all text codes, but have to make them with two \ instead of one. \\c[1] for example
  54.          //cond[iconId] : "Text",
  55.          //The text must be a valid javascript statement. If it evals to true the caption will show.
  56.          //In the given example, the gold caption will only show if party gold is > 0.
  57.      }
  58.      
  59.      IconCaptions.setCaption = function(num,value) {
  60.         this[num] = value;
  61.      }
  62.      IconCaptions.setCond = function(num,value){
  63.          this["cond"+num] = value;
  64.      }
  65.      
  66.      var _Astfgl_newWBDI = Window_Base.prototype.drawIcon
  67.      Window_Base.prototype.drawIcon = function(index,x,y) {
  68.           _Astfgl_newWBDI.call(this,index,x,y);
  69.           this.addIconCaption(index,x,y);
  70.      }
  71.      
  72.      Window_Base.prototype.addIconCaption = function(index,x,y) {
  73.          if (!this._iconCaptions) {
  74.              this._iconCaptions = []
  75.          }
  76.          if (this._iconCaptions.some(function(caption){return caption[0][0] === x && caption[0][1] === y && caption[2] === index})) {return}
  77.          this._iconCaptions.push([[x,y],[x+Window_Base._iconWidth,y+Window_Base._iconHeight],index])
  78.      }
  79.      
  80.      Window_Base.prototype.updateCaptions = function() {
  81.          if (!$gameSwitches.value(Number(showCaptions))) {return}
  82.          if (!this.visible) {return}
  83.          if (this._iconCaptions) {
  84.              var x = this.x + this._padding;
  85.              var y = this.y + this._padding;;
  86.              var win = this;
  87.              this._iconCaptions.forEach(function(caption){
  88.                 if (IconCaptions[caption[2]] && IconCaptions["cond"+caption[2]] !== undefined && !eval(IconCaptions["cond"+caption[2]])) {return}
  89.                 if (TouchInput._mouseX >= (caption[0][0] + x) && TouchInput._mouseX <= (caption[1][0] + x) && TouchInput._mouseY >= (caption[0][1] + y) && TouchInput._mouseY <= (caption[1][1] + y)) {
  90.                     if (!SceneManager._scene._captionWindow) {
  91.                         var captionWindow = new Window_IconCaption(caption[0][0] + x, caption[1][0] + y + win.lineHeight(),800,600)
  92.                         captionWindow.height = win.lineHeight() + win._padding * 2
  93.                         captionWindow.width = 100;
  94.                         captionWindow.hide();
  95.                         SceneManager._scene._captionWindow = captionWindow
  96.                         SceneManager._scene.addChild(SceneManager._scene._captionWindow);
  97.                     } else {
  98.                         if (win instanceof Window_Gold && $gameParty.inBattle()) {return}
  99.                         if (win instanceof Window_Gold && SceneManager._scene instanceof Scene_Map) {return}
  100.                         if (!win.visible) {return}
  101.                         var captionWindow = SceneManager._scene._captionWindow;
  102.                         captionWindow._index = caption[2];
  103.                         captionWindow.isHovered = true;
  104.                         captionWindow.x = caption[0][0] + x;
  105.                         captionWindow.y = caption[0][1] + y + win.lineHeight();
  106.                         var lineNum = 1;
  107.                         if (IconCaptions[caption[2]]) {
  108.                             lineNum = IconCaptions[caption[2]].split(/\r\n|\r|\n/).length
  109.                         }
  110.                         if (captionWindow.y + captionWindow.height > Graphics.boxHeight) {
  111.                             captionWindow.y = caption[0][1] + y - win.lineHeight() * (1+ lineNum);
  112.                         }
  113.                         if (captionWindow.x + captionWindow.width > Graphics.boxWidth) {
  114.                             captionWindow.x -= captionWindow.width
  115.                         }
  116.                         if (captionWindow.x < 0) {
  117.                             captionWindow.x = 0
  118.                         }
  119.                         captionWindow.height = win.lineHeight() * lineNum + win._padding *2;
  120.                     }
  121.                 }
  122.              })
  123.          }
  124.      }
  125.      
  126.      Window_BattleStatus.prototype.updateCaptions = function() {
  127.          if (SceneManager._scene._skillWindow.visible || SceneManager._scene._itemWindow.visible) {return}
  128.          Window_Base.prototype.updateCaptions.call(this)
  129.      }
  130.      
  131.      var _Astfgl_newSBUC = Scene_Base.prototype.updateChildren
  132.      Scene_Base.prototype.updateChildren = function() {
  133.          _Astfgl_newSBUC.call(this);
  134.           if (this._captionWindow) {
  135.             this._captionWindow.isHovered = false;
  136.          }
  137.          if (this._windowLayer) {
  138.              this._windowLayer.children.forEach(function(child) {
  139.                     if (child._iconCaptions) {
  140.                         child.updateCaptions();
  141.                     }
  142.              })
  143.          }
  144.      }
  145.    
  146.     function Window_IconCaption() {
  147.         this.initialize.apply(this, arguments);
  148.     }
  149.  
  150.     Window_IconCaption.prototype = Object.create(Window_Base.prototype);
  151.     Window_IconCaption.prototype.constructor = Window_IconCaption;
  152.  
  153.     Window_IconCaption.prototype.initialize = function(x,y,width,height) {
  154.         Window_Base.prototype.initialize.call(this,x,y,width,height);
  155.         this._index = 0;
  156.         this.isHovered = false;
  157.     }
  158.    
  159.     Window_IconCaption.prototype.update = function() {
  160.         this.contents.clear();
  161.         var txt = ""
  162.         if (IconCaptions[this._index]) {
  163.             txt = IconCaptions[this._index];
  164.         } else {
  165.             if (IconCaptions[this._index] === "") {
  166.                 this.hide();
  167.                 return
  168.             }
  169.             if (defText === "") {
  170.                 this.hide();
  171.                 return
  172.             }
  173.             txt = defText
  174.         }
  175.         var lines = txt.split("\n")
  176.         var maxWidth = 0;
  177.         for (let i = 0; i < lines.length; i++) {
  178.             if (maxWidth < this.textWidthEx(lines[i])) {maxWidth = this.textWidthEx(lines[i])}
  179.         }
  180.         this.width = maxWidth + this._padding * 2
  181.         //this.width = this.textWidth(txt) + this._padding * 3;
  182.         this.drawTextEx(txt,1,0)
  183.         if (this.isHovered) {
  184.             this.show();
  185.         } else {
  186.             this.hide();
  187.         }
  188.     }
  189.    
  190.     Window_IconCaption.prototype.textWidthEx = function(text) {
  191.         return this.drawTextEx(text, 0, this.contents.height);
  192.     };
  193.    
  194.     var _Astfgl_newWSR = Window_Selectable.prototype.refresh
  195.     Window_Selectable.prototype.refresh = function() {
  196.         this._iconCaptions = [];
  197.         _Astfgl_newWSR.call(this);
  198.     }
  199.    
  200.     var _Astfgl_newWHR = Window_Help.prototype.refresh
  201.     Window_Help.prototype.refresh = function() {
  202.         this._iconCaptions = [];
  203.         _Astfgl_newWHR.call(this);
  204.     }
  205.    
  206.     var _Astfgl_newWGR = Window_Gold.prototype.refresh
  207.     Window_Gold.prototype.refresh = function() {
  208.         this._iconCaptions = [];
  209.         _Astfgl_newWGR.call(this);
  210.     }
  211.    
  212.     var _Astfgl_newWMNR = Window_MapName.prototype.refresh
  213.     Window_MapName.prototype.refresh = function() {
  214.         this._iconCaptions = [];
  215.         _Astfgl_newWMNR.call(this);
  216.     }
  217.    
  218.     var _Astfgl_newWSTR = Window_ScrollText.prototype.refresh
  219.     Window_ScrollText.prototype.refresh = function() {
  220.         this._iconCaptions = [];
  221.         _Astfgl_newWSTR.call(this);
  222.     }
  223.    
  224.     var _Astfgl_newWMNP = Window_Message.prototype.newPage
  225.     Window_Message.prototype.newPage = function(textState) {
  226.         this._iconCaptions = [];
  227.         _Astfgl_newWMNP.call(this,textState);
  228.     }
  229.    
  230.     var _Astfgl_newWSLR = Window_SkillList.prototype.refresh
  231.     Window_SkillList.prototype.refresh = function() {
  232.         this._iconCaptions = [];
  233.         _Astfgl_newWSLR.call(this);
  234.     };
  235.    
  236.     var _Astfgl_newWBSR = Window_BattleStatus.prototype.refresh
  237.     Window_BattleStatus.prototype.refresh = function() {
  238.         this._iconCaptions = [];
  239.         _Astfgl_newWBSR.call(this);
  240.     };
  241.    
  242.     var _Astfgl_newWSSR = Window_SkillStatus.prototype.refresh
  243.     Window_SkillStatus.prototype.refresh = function() {
  244.         this._iconCaptions = [];
  245.         _Astfgl_newWSSR.call(this);
  246.     }
  247.    
  248.     var _Astfgl_newWShopSR = Window_ShopStatus.prototype.refresh   
  249.     Window_ShopStatus.prototype.refresh = function() {
  250.         this._iconCaptions = [];
  251.         _Astfgl_newWShopSR.call(this);
  252.     }
  253.    
  254.     var _Astfgl_newWShopNR = Window_ShopNumber.prototype.refresh
  255.     Window_ShopNumber.prototype.refresh = function() {
  256.         this._iconCaptions = [];
  257.         _Astfgl_newWShopNR.call(this);
  258.     }
  259.    
  260.     var _Astfgl_newWShopBR = Window_ShopBuy.prototype.refresh
  261.     Window_ShopBuy.prototype.refresh = function() {
  262.         this._iconCaptions = [];
  263.         _Astfgl_newWShopBR.call(this);
  264.     }
  265.    
  266.     var _Astfgl_newWILR = Window_ItemList.prototype.refresh
  267.     Window_ItemList.prototype.refresh = function() {
  268.         this._iconCaptions = [];
  269.         _Astfgl_newWILR.call(this);
  270.     }
  271.    
  272.     var _Astfgl_newWESR = Window_EquipStatus.prototype.refresh
  273.     Window_EquipStatus.prototype.refresh = function() {
  274.         this._iconCaptions = [];
  275.         _Astfgl_newWESR.call(this);
  276.     }
  277.    
  278.     var _Astfgl_newWBLR = Window_BattleLog.prototype.refresh
  279.     Window_BattleLog.prototype.refresh = function() {
  280.         this._iconCaptions = [];
  281.         _Astfgl_newWBLR.call(this);
  282.     }
  283.    
  284.     var _Astfgl_newWinStatusR = Window_Status.prototype.refresh
  285.     Window_Status.prototype.refresh = function() {
  286.         this._iconCaptions = [];
  287.         _Astfgl_newWinStatusR.call(this);
  288.     }
  289.    
  290.     //Sprite_StateIcon
  291.     var _Astfgl_newSSIU = Sprite_StateIcon.prototype.update
  292.     Sprite_StateIcon.prototype.update = function() {
  293.         _Astfgl_newSSIU.call(this)
  294.         if (!$gameSwitches.value(Number(showCaptions)) ||this._iconIndex === 0) {return}
  295.         if (IconCaptions[this._iconIndex] && IconCaptions["cond"+this._iconIndex] !== undefined && !eval(IconCaptions["cond"+this._iconIndex])) {return}
  296.         if (!this.visible) {return}
  297.         var win = new Window_Base(800,600)
  298.         var x = this.parent.x - Window_Base._iconWidth/2;
  299.         var y = this.parent.y - Window_Base._iconHeight/2;
  300.         if (TouchInput._mouseX >= (this.x + x) && TouchInput._mouseX <= (this.x + 48 + x) && TouchInput._mouseY >= (this.y + y) && TouchInput._mouseY <= (this.y + 48 + y)) {
  301.                     if (!SceneManager._scene._captionWindow) {
  302.                         var captionWindow = new Window_IconCaption(this.x + x, this.y + y + win.lineHeight(),800,600)
  303.                         captionWindow.height = win.lineHeight() + win._padding * 2
  304.                         captionWindow.width = 100;
  305.                         captionWindow.hide();
  306.                         SceneManager._scene._captionWindow = captionWindow
  307.                         SceneManager._scene.addChild(SceneManager._scene._captionWindow);
  308.                     } else {
  309.                         var captionWindow = SceneManager._scene._captionWindow;
  310.                         captionWindow._index = this._iconIndex;
  311.                         captionWindow.isHovered = true;
  312.                         captionWindow.x = this.x + x;
  313.                         captionWindow.y = this.y + y + win.lineHeight();
  314.                         var lineNum = 1;
  315.                         if (IconCaptions[this._iconIndex]) {
  316.                             lineNum = IconCaptions[this._iconIndex].split(/\r\n|\r|\n/).length
  317.                         }
  318.                         if (captionWindow.y + captionWindow.height > Graphics.boxHeight) {
  319.                             captionWindow.y = this.y + y - win.lineHeight() * (1+ lineNum);
  320.                         }
  321.                         if (captionWindow.x + captionWindow.width > Graphics.boxWidth) {
  322.                             captionWindow.x -= captionWindow.width
  323.                         }
  324.                         if (captionWindow.x < 0) {
  325.                             captionWindow.x = 0
  326.                         }
  327.                         captionWindow.height = win.lineHeight() * lineNum + win._padding *2;
  328.                     }
  329.                 }
  330.     }
  331.    
  332.     if (Imported) {
  333.         if (Imported.MOG_BattleHud) {
  334.             var _Astfgl_MGBHRST2 = Battle_Hud.prototype.refresh_states2
  335.             Battle_Hud.prototype.refresh_states2 = function() {
  336.                 _Astfgl_MGBHRST2.call(this);
  337.                 var w = Window_Base._iconWidth;
  338.                 var icons = this._battler.allIcons().slice(0,w);
  339.                 var m = Math.min(Math.max(this._battler.allIcons().length,0),Moghunter.bhud_statesMax);
  340.                 var align = Moghunter.bhud_statesAlign;
  341.                 for (i = 0; i < m; i++){
  342.                     this._stateIcons[i]._iconIndex = icons[i]
  343.                 }
  344.             };
  345.                
  346.             var _Astfgl_newBHUS2 = Battle_Hud.prototype.update_states2
  347.             Battle_Hud.prototype.update_states2 = function() {
  348.                 _Astfgl_newBHUS2.call(this);
  349.                 if (SceneManager._scene._skillWindow.visible || SceneManager._scene._itemWindow.visible) {return}
  350.                 if (this._battler.allIcons().length == 0) {return}
  351.                 this._stateIcons.forEach(function(icon) {
  352.                     if (!$gameSwitches.value(Number(showCaptions)) ||icon._iconIndex === 0) {return}
  353.                     if (IconCaptions[icon._iconIndex] && IconCaptions["cond"+icon._iconIndex] !== undefined && !eval(IconCaptions["cond"+icon._iconIndex])) {return}
  354.                         if (!icon.visible) {return}
  355.                         var win = new Window_Base(800,600)
  356.                         var x = icon.parent.x
  357.                         var y = icon.parent.y;
  358.                         if (TouchInput._mouseX >= (icon.x + x) && TouchInput._mouseX <= (icon.x + 48 + x) && TouchInput._mouseY >= (icon.y + y) && TouchInput._mouseY <= (icon.y + 48 + y)) {
  359.                                     if (!SceneManager._scene._captionWindow) {
  360.                                         var captionWindow = new Window_IconCaption(icon.x + x, icon.y + y + win.lineHeight(),800,600)
  361.                                         captionWindow.height = win.lineHeight() + win._padding * 2
  362.                                         captionWindow.width = 100;
  363.                                         captionWindow.hide();
  364.                                         SceneManager._scene._captionWindow = captionWindow
  365.                                         SceneManager._scene.addChild(SceneManager._scene._captionWindow);
  366.                                     } else {
  367.                                         var captionWindow = SceneManager._scene._captionWindow;
  368.                                         captionWindow._index = icon._iconIndex;
  369.                                         captionWindow.isHovered = true;
  370.                                         captionWindow.x = icon.x + x;
  371.                                         captionWindow.y = icon.y + y + win.lineHeight();
  372.                                         var lineNum = 1;
  373.                                         if (IconCaptions[icon._iconIndex]) {
  374.                                             lineNum = IconCaptions[icon._iconIndex].split(/\r\n|\r|\n/).length
  375.                                         }
  376.                                         if (captionWindow.y + captionWindow.height > Graphics.boxHeight) {
  377.                                             captionWindow.y = icon.y + y - win.lineHeight() * (1+ lineNum);
  378.                                         }
  379.                                         if (captionWindow.x + captionWindow.width > Graphics.boxWidth) {
  380.                                             captionWindow.x -= captionWindow.width
  381.                                         }
  382.                                         if (captionWindow.x < 0) {
  383.                                             captionWindow.x = 0
  384.                                         }
  385.                                         captionWindow.height = win.lineHeight() * lineNum + win._padding *2;
  386.                                     }
  387.                                 }
  388.                 })
  389.             }
  390.         }
  391.     }
  392.     /*
  393.     //custom battle UI windows, remove before release to the public
  394.     if (Window_ActorActions) {
  395.         Window_ActorActions.prototype.drawIcon = function(index,x,y) {
  396.           _Astfgl_newWBDI.call(this,index,x,y);
  397.         }
  398.         Window_ActorActions.prototype.updateCaptions = function() {}
  399.     }
  400.    
  401.     if (Window_ActorMiniStatus) {
  402.         Window_ActorMiniStatus.prototype.drawIcon = function(index,x,y) {
  403.           _Astfgl_newWBDI.call(this,index,x,y);
  404.         }
  405.         Window_ActorMiniStatus.prototype.updateCaptions = function() {}
  406.     }
  407.     if (Window_optIcon) {
  408.         Window_optIcon.prototype.drawIcon = function(index,x,y) {
  409.           _Astfgl_newWBDI.call(this,index,x,y);
  410.         }
  411.         Window_optIcon.prototype.updateCaptions = function() {}
  412.     }
  413.     */
  414.    
  415.     //TI alias to get coordinate to use for captions
  416.     var TIOMM = TouchInput._onMouseMove
  417.     TouchInput._onMouseMove = function(event) {
  418.         TIOMM.call(this,event);
  419.         this._mouseX = Graphics.pageToCanvasX(event.pageX);
  420.         this._mouseY = Graphics.pageToCanvasY(event.pageY);
  421.     }
  422.  
  423. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement