Ticket #3114: health_garrisoned_v1.patch

File health_garrisoned_v1.patch, 4.7 KB (added by Imarok, 8 years ago)

Show a health bar for garrisoned units. Also replaced some var with let

  • binaries/data/mods/public/gui/session/selection_panels.js

     
    468468            return false;
    469469        data.name = getEntityNames(data.template);
    470470        data.count = data.item.ents.length;
     471
     472        data.health = 0;
     473        data.maxHealth = 0;
     474        let ents = data.item.ents;
     475        for (let i = 0; i < ents.length; i++)
     476        {
     477            let state = GetEntityState(ents[i]);
     478            data.health += state.hitpoints;
     479            data.maxHealth += state.maxHitpoints;
     480        }
    471481        return true;
    472482    },
    473483    "setAction": function(data)
  • binaries/data/mods/public/gui/session/selection_panels_left/garrison_panel.xml

     
    88            </object>
    99        </repeat>
    1010    </object>
     11        <!-- Health bar of garrisoned units -->
     12    <object size="-14 1 -7 100%-49" type="image" name="healthGarrison" tooltip_style="sessionToolTip">
     13        <translatableAttribute id="tooltip">Hitpoints</translatableAttribute>
     14        <object type="image" sprite="barBorder" ghost="true" size="-1 -1 100%+1 100%+1"/>
     15        <object type="image" sprite="healthBackground" ghost="true"/>
     16        <object type="image" sprite="healthForeground" ghost="true" name="healthBarGarrison"/>
     17        <object type="image" sprite="statsBarShaderVertical" ghost="true"/>
     18    </object>
    1119</object>
  • binaries/data/mods/public/gui/session/unit_commands.js

     
    4040    }
    4141    let selection = g_Selection.toList();
    4242
    43     var items = g_SelectionPanels[guiName].getItems(unitEntState, selection);
     43    let items = g_SelectionPanels[guiName].getItems(unitEntState, selection);
    4444
    4545    if (!items || !items.length)
    4646        return;
    4747
    48     var numberOfItems = items.length;
    49     var garrisonGroups = new EntityGroups();
     48    let numberOfItems = items.length;
     49    let garrisonGroups = new EntityGroups();
    5050
    5151    // Determine how many buttons there should be
    52     var maxNumberOfItems = g_SelectionPanels[guiName].getMaxNumberOfItems();
     52    let maxNumberOfItems = g_SelectionPanels[guiName].getMaxNumberOfItems();
    5353    if (maxNumberOfItems < numberOfItems)
    5454        numberOfItems = maxNumberOfItems;
    5555
     
    5858    if (g_SelectionPanels[guiName].resizePanel)
    5959        g_SelectionPanels[guiName].resizePanel(numberOfItems, rowLength);
    6060
     61    let totalGarrisionHealth = 0;
     62    let maxGarrisionHealth = 0;
     63
    6164    // Make buttons
    6265    for (let i = 0; i < numberOfItems; ++i)
    6366    {
    64         var item = items[i];
     67        let item = items[i];
    6568
    6669        // If a tech has been researched it leaves an empty slot
    6770        if (!item)
     
    7376
    7477        // STANDARD DATA
    7578        // add standard data
    76         var data = {
     79        let data = {
    7780            "i": i,
    7881            "item": item,
    7982            "selection": selection,
     
    104107        // add general data, and a chance to abort on faulty data
    105108        if (g_SelectionPanels[guiName].addData)
    106109        {
    107             var success = g_SelectionPanels[guiName].addData(data);
     110            let success = g_SelectionPanels[guiName].addData(data);
    108111            if (!success)
    109112                continue; // ignore faulty data
    110113        }
     114        if (guiName == "Garrison")
     115        {
     116            totalGarrisionHealth += data.health;
     117            maxGarrisionHealth += data.maxHealth;
     118        }
    111119
    112120        // SET CONTENT
    113121        // run all content setters
    114         for (var f in g_SelectionPanels[guiName])
     122        for (let f in g_SelectionPanels[guiName])
    115123        {
    116124            if (f.match(/^set/))
    117125                g_SelectionPanels[guiName][f](data);
     
    137145    // remember the number of items
    138146    g_unitPanelButtons[guiName] = numberOfItems;
    139147    g_SelectionPanels[guiName].used = true;
     148
     149    // Show health of garrisoned units
     150    if (guiName == "Garrison")
     151    {
     152        let healthGarrison = Engine.GetGUIObjectByName("healthGarrison");
     153        healthGarrison.hidden = totalGarrisionHealth <= 0;
     154        if (totalGarrisionHealth > 0)
     155        {
     156            let garrisonedUnitsHealthBar = Engine.GetGUIObjectByName("healthBarGarrison");
     157            let healthSize = garrisonedUnitsHealthBar.size;
     158            healthSize.rtop = 100-100*Math.max(0, Math.min(1, totalGarrisionHealth / maxGarrisionHealth));
     159            garrisonedUnitsHealthBar.size = healthSize;
     160            healthGarrison.tooltip = sprintf(translate("%(label)s %(current)s / %(max)s"), {
     161                "label": "[font=\"sans-bold-13\"]" + translate("Hitpoints:") + "[/font]",
     162                "current": Math.ceil(totalGarrisionHealth),
     163                "max": Math.ceil(maxGarrisionHealth)
     164            });
     165        }
     166    }
    140167}
    141168
    142169/**