Ticket #1580: 1580.11.diff

File 1580.11.diff, 8.7 KB (added by elexis, 9 years ago)
  • binaries/data/mods/public/gui/common/functions_utility.js

     
    146146    return ret;
    147147}
    148148
    149149// ====================================================================
    150150
     151function sameColor(color1, color2)
     152{
     153    return color1.r === color2.r && color1.g === color2.g && color1.b === color2.b;
     154}
     155
     156// ====================================================================
     157
    151158/**
    152159 * Convert time in milliseconds to [hh:]mm:ss string representation.
    153160 * @param time Time period in milliseconds (integer)
    154161 * @return String representing time period
    155162 */
  • binaries/data/mods/public/gui/gamesetup/gamesetup.js

     
    77const g_GameSpeeds = prepareForDropdown(g_Settings ? g_Settings.GameSpeeds.filter(speed => !speed.ReplayOnly) : undefined);
    88const g_VictoryConditions = prepareForDropdown(g_Settings ? g_Settings.VictoryConditions : undefined);
    99const g_PopulationCapacities = prepareForDropdown(g_Settings ? g_Settings.PopulationCapacities : undefined);
    1010const g_StartingResources = prepareForDropdown(g_Settings ? g_Settings.StartingResources : undefined);
    1111
     12// All colors except gaia
     13const g_PlayerColors = initPlayerDefaults().slice(1).map(playerData => playerData.Color);
     14
    1215////////////////////////////////////////////////////////////////////////////////////////////////
    1316
    1417// Is this is a networked game, or offline
    1518var g_IsNetworked;
    1619
     
    373376                g_GameAttributes.settings.PlayerData[playerSlot].Team = this.selected - 1;
    374377
    375378            updateGameAttributes();
    376379        };
    377380
     381        // Populate color drop-down lists.
     382        var colorPicker = Engine.GetGUIObjectByName("playerColorPicker["+i+"]");
     383        colorPicker.list = g_PlayerColors.map(color => '[font="sans-stroke-14"][color="' + color.r + ' ' + color.g + ' ' + color.b + '"] ■[/color][/font]');
     384        colorPicker.list_data = g_PlayerColors.map((color, index) => index);
     385        colorPicker.onSelectionChange = function() { selectPlayerColor(playerSlot, colorPicker.selected); };
     386        colorPicker.selected = -1;
     387
    378388        // Set events
    379389        var civ = Engine.GetGUIObjectByName("playerCiv["+i+"]");
    380390        civ.onSelectionChange = function() {
    381391            if ((this.selected != -1)&&(g_GameAttributes.mapType !== "scenario"))
    382392                g_GameAttributes.settings.PlayerData[playerSlot].Civ = this.list_data[this.selected];
     
    886896    }
    887897
    888898    updateGameAttributes();
    889899}
    890900
     901function selectPlayerColor(playerSlot, selectedColorIdx)
     902{
     903    if (selectedColorIdx == -1)
     904        return;
     905
     906    // Add missing properties
     907    g_GameAttributes.settings.PlayerData.forEach((pData, index) => {
     908        if (!pData.Color)
     909            pData.Color = g_PlayerColors[index];
     910    });
     911
     912    // If someone else has that color, give him/her the old one
     913    var playerID = g_GameAttributes.settings.PlayerData.findIndex(pData => sameColor(g_PlayerColors[selectedColorIdx], pData.Color));
     914    if (playerID != -1)
     915        g_GameAttributes.settings.PlayerData[playerID].Color = g_GameAttributes.settings.PlayerData[playerSlot].Color;
     916
     917    // Assign the new player color
     918    g_GameAttributes.settings.PlayerData[playerSlot].Color = g_PlayerColors[selectedColorIdx];
     919
     920    // Ensure colors are not used twice (would be possible by selecting any unused color and then increasing the number of players)
     921    do
     922    {
     923        // Find a player with a color that is used twice
     924        playerID = g_GameAttributes.settings.PlayerData.findIndex((pData, index) =>
     925            g_GameAttributes.settings.PlayerData.some((pData2, index2) => index != index2 && sameColor(pData.Color, pData2.Color)));
     926
     927        // Assign an unused color
     928        if (playerID != -1)
     929            g_GameAttributes.settings.PlayerData[playerID].Color =
     930                g_PlayerColors.find(color => g_GameAttributes.settings.PlayerData.every(pData => !sameColor(color, pData.Color)));
     931
     932    } while (playerID != -1);
     933
     934    if (!g_IsInGuiUpdate)
     935        updateGameAttributes();
     936}
     937
    891938// Called when the user selects a map type from the list
    892939function selectMapType(type)
    893940{
    894941    // Avoid recursion
    895942    if (g_IsInGuiUpdate)
     
    14221469        var pCiv = Engine.GetGUIObjectByName("playerCiv["+i+"]");
    14231470        var pCivText = Engine.GetGUIObjectByName("playerCivText["+i+"]");
    14241471        var pTeam = Engine.GetGUIObjectByName("playerTeam["+i+"]");
    14251472        var pTeamText = Engine.GetGUIObjectByName("playerTeamText["+i+"]");
    14261473        var pColor = Engine.GetGUIObjectByName("playerColor["+i+"]");
     1474        var pColorPicker = Engine.GetGUIObjectByName("playerColorPicker["+i+"]");
     1475        var pColorPickerHeading = Engine.GetGUIObjectByName("playerColorHeading");
    14271476
    14281477        // Player data / defaults
    14291478        var pData = mapSettings.PlayerData ? mapSettings.PlayerData[i] : {};
    14301479        var pDefs = g_DefaultPlayerData ? g_DefaultPlayerData[i] : {};
    14311480
    14321481        // Common to all game types
    1433         var color = rgbToGuiColor(getSetting(pData, pDefs, "Color"));
    1434         pColor.sprite = "color:" + color + " 100";
     1482        var color = getSetting(pData, pDefs, "Color");
     1483        pColor.sprite = "color:" + rgbToGuiColor(color) + " 100";
    14351484        pName.caption = translate(getSetting(pData, pDefs, "Name"));
    14361485
    14371486        var team = getSetting(pData, pDefs, "Team");
    14381487        var civ = getSetting(pData, pDefs, "Civ");
    14391488
     
    14491498        // TODO: Allow clients to choose their own civ and team
    14501499        if (!g_IsController || g_GameAttributes.mapType == "scenario")
    14511500        {
    14521501            pCivText.hidden = false;
    14531502            pCiv.hidden = true;
     1503            pColorPicker.hidden = true;
     1504            pColorPickerHeading.hidden = true;
    14541505            pTeamText.hidden = false;
    14551506            pTeam.hidden = true;
    14561507            // Set text values
    14571508            if (civ == "random")
    14581509                pCivText.caption = "[color=\"orange\"]" + translateWithContext("civilization", "Random");
     
    14641515        {
    14651516            pCivText.hidden = true;
    14661517            pCiv.hidden = false;
    14671518            pTeamText.hidden = true;
    14681519            pTeam.hidden = false;
     1520            pColorPicker.hidden = false;
     1521            pColorPickerHeading.hidden = false;
     1522            pColorPicker.selected = g_PlayerColors.findIndex(col => sameColor(col, color));
    14691523            // Set dropdown values
    14701524            pCiv.selected = (civ ? pCiv.list_data.indexOf(civ) : 0);
    14711525            pTeam.selected = (team !== undefined && team >= 0) ? team+1 : 0;
    14721526        }
    14731527    }
  • binaries/data/mods/public/gui/gamesetup/gamesetup.xml

     
    4040            <object size="24 49 100%-440 345" type="image" sprite="ModernDarkBoxGold" name="playerAssignmentsPanel">
    4141                <object size="0 6 100% 30">
    4242                    <object name="playerNameHeading" type="text" style="ModernLabelText" size="0 0 22% 100%">
    4343                        <translatableAttribute id="caption">Player Name</translatableAttribute>
    4444                    </object>
    45                     <object name="playerPlacementHeading" type="text" style="ModernLabelText" size="22%+5 0 50%+35 100%">
     45                    <object name="playerColorHeading" type="text" style="ModernLabelText" size="22%+5 0 22%+40 100%">
     46                        <translatableAttribute id="caption">Color</translatableAttribute>
     47                    </object>
     48                    <object name="playerPlacementHeading" type="text" style="ModernLabelText" size="22%+45 0 50%+35 100%">
    4649                        <translatableAttribute id="caption">Player Placement</translatableAttribute>
    4750                    </object>
    4851                    <object name="playerCivHeading" type="text" style="ModernLabelText" size="50%+65 0 85%-26 100%">
    4952                        <translatableAttribute id="caption">Civilization</translatableAttribute>
    5053                    </object>
     
    6770                <object size="1 36 100%-1 100%">
    6871                    <repeat count="8">
    6972                        <object name="playerBox[n]" size="0 0 100% 32" hidden="true">
    7073                            <object name="playerColor[n]" type="image" size="0 0 100% 100%"/>
    7174                            <object name="playerName[n]" type="text" style="ModernLabelText" size="0 2 22% 30"/>
    72                             <object name="playerAssignment[n]" type="dropdown" style="ModernDropDown" size="22%+5 2 50%+35 30" tooltip_style="onscreenToolTip">
     75                            <object name="playerColorPicker[n]" type="dropdown" style="ModernDropDown" size="22%+5 2 22%+40 30" scrollbar="false" tooltip_style="onscreenToolTip">
     76                                <translatableAttribute id="tooltip">Pick a color.</translatableAttribute>
     77                            </object>
     78                            <object name="playerAssignment[n]" type="dropdown" style="ModernDropDown" size="22%+45 2 50%+35 30" tooltip_style="onscreenToolTip">
    7379                                <translatableAttribute id="tooltip">Select player.</translatableAttribute>
    7480                            </object>
    7581                            <object name="playerAssignmentText[n]" type="text" style="ModernLabelText" size="22%+5 0 50%+35 30"/>
    7682                            <object name="playerConfig[n]" type="button" style="StoneButton" size="50%+40 4 50%+64 28"
    7783                                tooltip_style="onscreenToolTip"