Ticket #1571: redIcons.4.patch

File redIcons.4.patch, 12.1 KB (added by Deiz, 12 years ago)
  • binaries/data/mods/public/simulation/components/Player.js

     
    160160    }
    161161};
    162162
    163 Player.prototype.TrySubtractResources = function(amounts)
     163Player.prototype.GetNeededResources = function(amounts)
    164164{
    165165    // Check if we can afford it all
    166166    var amountsNeeded = {};
    167167    for (var type in amounts)
    168         if (amounts[type] > this.resourceCount[type])
     168        if (this.resourceCount[type] != undefined && amounts[type] > this.resourceCount[type])
    169169            amountsNeeded[type] = amounts[type] - this.resourceCount[type];
    170    
    171     var formattedAmountsNeeded = [];
    172     for (var type in amountsNeeded)
    173         formattedAmountsNeeded.push(type + ": " + amountsNeeded[type]);
     170
     171    if (Object.keys(amountsNeeded).length == 0)
     172        return undefined;
     173    return amountsNeeded;
     174};
     175
     176Player.prototype.TrySubtractResources = function(amounts)
     177{
     178    var amountsNeeded = this.GetNeededResources(amounts);
    174179           
    175180    // If we don't have enough resources, send a notification to the player
    176     if (formattedAmountsNeeded.length)
     181    if (amountsNeeded)
    177182    {
    178         var notification = {"player": this.playerID, "message": "Insufficient resources - " + formattedAmountsNeeded.join(", ")};
     183        var formatted = [];
     184        for (var type in amountsNeeded)
     185            formatted.push(amountsNeeded[type] + " " + type[0].toUpperCase() + type.substr(1) );
     186        var notification = {"player": this.playerID, "message": "Insufficient resources - " + formatted.join(", ")};
    179187        var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    180188        cmpGUIInterface.PushNotification(notification);
    181189        return false;
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    567567    return ret;
    568568}
    569569
     570// Used to show a red square over GUI elements you can't yet afford.
     571GuiInterface.prototype.GetNeededResources = function(player, amounts)
     572{
     573    var cmpPlayer = QueryPlayerIDInterface(player, IID_Player);
     574    return cmpPlayer.GetNeededResources(amounts);
     575};
     576
     577
    570578GuiInterface.prototype.PushNotification = function(notification)
    571579{
    572580    this.notifications.push(notification);
     
    16171625    "IsTechnologyResearched": 1,
    16181626    "CheckTechnologyRequirements": 1,
    16191627    "GetStartedResearch": 1,
     1628    "GetNeededResources": 1,
    16201629    "GetNextNotification": 1,
    16211630
    16221631    "GetAvailableFormations": 1,
  • binaries/data/mods/public/gui/session/utility_functions.js

     
    318318    return popBonus;
    319319}
    320320
     321/**
     322 * Returns a message with the amount of each resource needed to create an entity.
     323 */
     324function getNeededResourcesTooltip(resources)
     325{
     326    var formatted = [];
     327    for (var resource in resources)
     328        formatted.push(resources[resource] + " [font=\"serif-12\"]" + getCostComponentDisplayName(resource) + "[/font]");
     329
     330    return "\n\n[font=\"serif-bold-13\"]Insufficient resources:[/font]\n" + formatted.join(", ");
     331}
     332
    321333function getEntitySpeed(template)
    322334{
    323335    var speed = "";
  • binaries/data/mods/public/gui/session/unit_commands.js

     
    332332            case GATE:
    333333                var tooltip = item.tooltip;
    334334                if (item.template)
    335                     tooltip += "\n" + getEntityCostTooltip(GetTemplateData(item.template));
     335                {
     336                    var template = GetTemplateData(item.template);
     337                    tooltip += "\n" + getEntityCostTooltip(template);
     338
     339                    var affordableMask = getGUIObjectByName("unitGateUnaffordable["+i+"]");
     340                    affordableMask.hidden = true;
     341
     342                    var neededResources = Engine.GuiInterfaceCall("GetNeededResources", template.cost);
     343                    if (neededResources)
     344                    {
     345                        affordableMask.hidden = false;
     346                        tooltip += getNeededResourcesTooltip(neededResources);
     347                    }
     348                }
    336349                break;
    337350
    338351            case STANCE:
     
    416429        // Button
    417430        var button = getGUIObjectByName("unit"+guiName+"Button["+i+"]");
    418431        var button1 = getGUIObjectByName("unit"+guiName+"Button["+(i+rowLength)+"]");
     432        var affordableMask = getGUIObjectByName("unit"+guiName+"Unaffordable["+i+"]");
     433        var affordableMask1 = getGUIObjectByName("unit"+guiName+"Unaffordable["+(i+rowLength)+"]");
    419434        var icon = getGUIObjectByName("unit"+guiName+"Icon["+i+"]");
    420435        var selection = getGUIObjectByName("unit"+guiName+"Selection["+i+"]");
    421436        var pair = getGUIObjectByName("unit"+guiName+"Pair["+i+"]");
     
    553568           
    554569            if (guiName == RESEARCH)
    555570            {
     571                // Check resource requirements for first button
     572                affordableMask.hidden = true;
     573                var neededResources = Engine.GuiInterfaceCall("GetNeededResources", template.cost);
     574                if (neededResources)
     575                {
     576                    if (button.enabled !== false)
     577                    {
     578                        button.enabled = false;
     579                        affordableMask.hidden = false;
     580                    }
     581                    button.tooltip += getNeededResourcesTooltip(neededResources);
     582                }
     583
    556584                if (item.pair)
    557585                {
    558586                    grayscale = "";
    559587                    button1.enabled = true;
    560                     if (guiName == RESEARCH && !Engine.GuiInterfaceCall("CheckTechnologyRequirements", entType1))
     588
     589                    if (!Engine.GuiInterfaceCall("CheckTechnologyRequirements", entType1))
    561590                    {
    562591                        button1.enabled = false;
    563592                        button1.tooltip += "\n" + GetTechnologyData(entType1).requirementsTooltip;
    564593                        grayscale = "grayscale:";
    565594                    }
    566595                    icon1.sprite = "stretched:" + grayscale + "session/portraits/" +template1.icon;
     596
     597                    // Check resource requirements for second button
     598                    affordableMask1.hidden = true;
     599                    neededResources = Engine.GuiInterfaceCall("GetNeededResources", template.cost);
     600                    if (neededResources)
     601                    {
     602                        if (button1.enabled !== false)
     603                        {
     604                            button1.enabled = false;
     605                            affordableMask1.hidden = false;
     606                        }
     607                        button1.tooltip += getNeededResourcesTooltip(neededResources);
     608                    }
    567609                }
    568610                else
    569611                {
    570612                    pair.hidden = true;
    571613                    button1.hidden = true;
     614                    affordableMask1.hidden = true;
     615                }
     616            }
     617            else if (guiName == CONSTRUCTION || guiName == TRAINING)
     618            {
     619                affordableMask.hidden = true;
     620                var totalCosts = {};
     621                var trainNum = 1;
     622                if (Engine.HotkeyIsPressed("session.batchtrain") && guiName == TRAINING)
     623                {
     624                    var [batchSize, batchIncrement] = getTrainingBatchStatus(unitEntState.id, entType);
     625                    trainNum = batchSize + batchIncrement;
    572626                }
     627
     628                // Walls have no cost defined.
     629                if (template.cost !== undefined)
     630                    for (var r in template.cost)
     631                        totalCosts[r] = Math.floor(template.cost[r] * trainNum);
     632
     633                var neededResources = Engine.GuiInterfaceCall("GetNeededResources", totalCosts);
     634                if (neededResources)
     635                {
     636                    var totalCost = 0;
     637                    if (button.enabled !== false)
     638                    {
     639                        for each (var resource in neededResources)
     640                            totalCost += resource;
     641
     642                        button.enabled = false;
     643                        affordableMask.hidden = false;
     644                        var alpha = 75 + totalCost/6;
     645                        alpha = alpha > 150 ? 150 : alpha;
     646                        affordableMask.sprite = "colour: 255 0 0 " + (alpha);
     647                    }
     648                    button.tooltip += getNeededResourcesTooltip(neededResources);
     649                }
    573650            }
    574651        }
    575652        else
     
    718795                var buyPrice = unitEntState.barterMarket.prices["buy"][resource];
    719796                amountToBuy = "+" + Math.round(sellPrice / buyPrice * amountToSell);
    720797            }
     798
    721799            var amount;
    722800            if (j == 0)
    723801            {
    724802                button.onpress = (function(i){ return function() { g_barterSell = i; } })(i);
    725                 amount = (i == g_barterSell) ? "-" + amountToSell : "";
    726             }
     803                if (i == g_barterSell)
     804                {
     805                    amount = "-" + amountToSell;
     806                   
     807                    var neededRes = {};
     808                    neededRes[resource] = amountToSell;
     809                    var neededResources = Engine.GuiInterfaceCall("GetNeededResources", neededRes);
     810                    var hidden = neededResources ? false : true;
     811                    for (var ii = 0; ii < BARTER_RESOURCES.length; ii++)
     812                    {
     813                        var affordableMask = getGUIObjectByName("unitBarterBuyUnaffordable["+ii+"]");
     814                        affordableMask.hidden = hidden;
     815                    }
     816                }
     817                else
     818                    amount = "";
     819            }
    727820            else
    728821            {
    729822                var exchangeResourcesParameters = { "sell": BARTER_RESOURCES[g_barterSell], "buy": BARTER_RESOURCES[i], "amount": amountToSell };
  • binaries/data/mods/public/gui/session/session.xml

     
    603603            <repeat count="4">
    604604                <object name="unitBarterSellButton[n]" style="iconButton" type="button" size="0 0 46 46" tooltip_style="sessionToolTipBottomBold">
    605605                <object name="unitBarterSellIcon[n]" type="image" ghost="true" size="3 3 43 43"/>
     606                <object name="unitBarterSellUnaffordable[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 60"/>
    606607                <object name="unitBarterSellAmount[n]" ghost="true" style="resourceText" type="text" size="0 0 100% 50%"/>
    607608                <object name="unitBarterSellSelection[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="stretched:session/icons/corners.png"/>
    608609                </object>
     
    612613            <repeat count="4">
    613614                <object name="unitBarterBuyButton[n]" style="iconButton" type="button" size="0 0 46 46" tooltip_style="sessionToolTipBottomBold">
    614615                <object name="unitBarterBuyIcon[n]" type="image" ghost="true" size="3 3 43 43"/>
     616                <object name="unitBarterBuyUnaffordable[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 60"/>
    615617                <object name="unitBarterBuyAmount[n]" ghost="true" style="resourceText" type="text" size="0 0 100% 50%"/>
    616618                </object>
    617619            </repeat>
     
    822824            <repeat count="24">
    823825                <object name="unitConstructionButton[n]" hidden="true" style="iconButton" type="button" size="0 0 46 46" tooltip_style="sessionToolTipBottom">
    824826                <object name="unitConstructionIcon[n]" type="image" ghost="true" size="3 3 43 43"/>
     827                <object name="unitConstructionUnaffordable[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 127"/>
    825828                </object>
    826829            </repeat>
    827830            </object>
     
    835838                <object name="unitResearchButton[n]" hidden="true" style="iconButton" type="button" size="0 0 46 46" tooltip_style="sessionToolTipBottom">
    836839                <object name="unitResearchIcon[n]" type="image" ghost="true" size="3 3 43 43"/>
    837840                <object name="unitResearchSelection[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 127"/>
     841                <object name="unitResearchUnaffordable[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 60"/>
    838842                </object>
    839843            </repeat>
    840844            <repeat count="8">
     
    852856            <repeat count="24">
    853857                <object name="unitTrainingButton[n]" hidden="true" style="iconButton" type="button" size="0 0 46 46" tooltip_style="sessionToolTipBottom">
    854858                <object name="unitTrainingIcon[n]" type="image" ghost="true" size="3 3 43 43"/>
     859                <object name="unitTrainingUnaffordable[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 127"/>
    855860                </object>
    856861            </repeat>
    857862            </object>
     
    898903                <object name="unitGateButton[n]" hidden="true" style="iconButton" type="button" size="0 0 46 46" tooltip_style="sessionToolTipBottom">
    899904                <object name="unitGateIcon[n]" type="image" ghost="true" size="3 3 43 43"/>
    900905                <object name="unitGateSelection[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="stretched:session/icons/corners.png"/>
     906                <object name="unitGateUnaffordable[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 127"/>
    901907                </object>
    902908            </repeat>
    903909            </object>