Ticket #1580: 1580.12.diff

File 1580.12.diff, 8.3 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

     
    88const g_MapTypes = prepareForDropdown(g_Settings ? g_Settings.MapTypes : undefined);
    99const g_PopulationCapacities = prepareForDropdown(g_Settings ? g_Settings.PopulationCapacities : undefined);
    1010const g_StartingResources = prepareForDropdown(g_Settings ? g_Settings.StartingResources : undefined);
    1111const g_VictoryConditions = prepareForDropdown(g_Settings ? g_Settings.VictoryConditions : undefined);
    1212
     13// All colors except gaia
     14const g_PlayerColors = initPlayerDefaults().slice(1).map(pData => pData.Color);
     15
    1316////////////////////////////////////////////////////////////////////////////////////////////////
    1417
    1518// Is this is a networked game, or offline
    1619var g_IsNetworked;
    1720
     
    374377                g_GameAttributes.settings.PlayerData[playerSlot].Team = this.selected - 1;
    375378
    376379            updateGameAttributes();
    377380        };
    378381
     382        // Populate color drop-down lists.
     383        var colorPicker = Engine.GetGUIObjectByName("playerColorPicker["+i+"]");
     384        colorPicker.list = g_PlayerColors.map(color => '[color="' + color.r + ' ' + color.g + ' ' + color.b + '"] ■[/color]');
     385        colorPicker.list_data = g_PlayerColors.map((color, index) => index);
     386        colorPicker.selected = -1;
     387        colorPicker.onSelectionChange = function() { selectPlayerColor(playerSlot, this.selected); };
     388
    379389        // Set events
    380390        var civ = Engine.GetGUIObjectByName("playerCiv["+i+"]");
    381391        civ.onSelectionChange = function() {
    382392            if ((this.selected != -1)&&(g_GameAttributes.mapType !== "scenario"))
    383393                g_GameAttributes.settings.PlayerData[playerSlot].Civ = this.list_data[this.selected];
     
    886896    }
    887897
    888898    updateGameAttributes();
    889899}
    890900
     901/**
     902 *  Assigns the given color to that player.
     903 */
     904function selectPlayerColor(playerSlot, colorIndex)
     905{
     906    if (colorIndex == -1)
     907        return;
     908
     909    var playerData = g_GameAttributes.settings.PlayerData;
     910
     911    // Add missing properties
     912    playerData.forEach((pData, index) => {
     913        if (!pData.Color)
     914            pData.Color = g_PlayerColors[index];
     915    });
     916
     917    // If someone else has that color, give that player the old color
     918    var pData = playerData.find(pData => sameColor(g_PlayerColors[colorIndex], pData.Color));
     919    if (pData)
     920        pData.Color = playerData[playerSlot].Color;
     921
     922    // Assign the new color
     923    playerData[playerSlot].Color = g_PlayerColors[colorIndex];
     924
     925    // Ensure colors are not used twice after increasing the number of players
     926    for (let i = playerData.length - 1; i >= 0; --i)
     927    {
     928        // If someone else has that color, assign an unused color
     929        if (playerData.some((pData, j) => i != j && sameColor(playerData[i].Color, pData.Color)))
     930            playerData[i].Color = g_PlayerColors.find(color => playerData.every(pData => !sameColor(color, pData.Color)));
     931    }
     932
     933    if (!g_IsInGuiUpdate)
     934        updateGameAttributes();
     935}
     936
    891937// Called when the user selects a map type from the list
    892938function selectMapType(type)
    893939{
    894940    // Avoid recursion
    895941    if (g_IsInGuiUpdate)
     
    14201466        var pCiv = Engine.GetGUIObjectByName("playerCiv["+i+"]");
    14211467        var pCivText = Engine.GetGUIObjectByName("playerCivText["+i+"]");
    14221468        var pTeam = Engine.GetGUIObjectByName("playerTeam["+i+"]");
    14231469        var pTeamText = Engine.GetGUIObjectByName("playerTeamText["+i+"]");
    14241470        var pColor = Engine.GetGUIObjectByName("playerColor["+i+"]");
     1471        var pColorPicker = Engine.GetGUIObjectByName("playerColorPicker["+i+"]");
     1472        var pColorPickerHeading = Engine.GetGUIObjectByName("playerColorHeading");
    14251473
    14261474        // Player data / defaults
    14271475        var pData = mapSettings.PlayerData ? mapSettings.PlayerData[i] : {};
    14281476        var pDefs = g_DefaultPlayerData ? g_DefaultPlayerData[i] : {};
    14291477
    14301478        // Common to all game types
    1431         var color = rgbToGuiColor(getSetting(pData, pDefs, "Color"));
    1432         pColor.sprite = "color:" + color + " 100";
     1479        var color = getSetting(pData, pDefs, "Color");
     1480        pColor.sprite = "color:" + rgbToGuiColor(color) + " 100";
    14331481        pName.caption = translate(getSetting(pData, pDefs, "Name"));
    14341482
    14351483        var team = getSetting(pData, pDefs, "Team");
    14361484        var civ = getSetting(pData, pDefs, "Civ");
    14371485
     
    14661514            pTeam.hidden = false;
    14671515            // Set dropdown values
    14681516            pCiv.selected = (civ ? pCiv.list_data.indexOf(civ) : 0);
    14691517            pTeam.selected = (team !== undefined && team >= 0) ? team+1 : 0;
    14701518        }
     1519
     1520        // Allow host to chose player colors random maps, since they don't define custom colors currently.
     1521        // TODO: Random maps might define custom colors too at some point. We should make sure that
     1522        // we remove colors that are too similar to others, so that players can't select seemingly identical colors.
     1523        const canChangeColors = g_IsController && g_GameAttributes.mapType == "random"
     1524        pColorPicker.hidden = !canChangeColors;
     1525        pColorPickerHeading.hidden = !canChangeColors;
     1526        if (canChangeColors)
     1527            pColorPicker.selected = g_PlayerColors.findIndex(col => sameColor(col, color));
    14711528    }
    14721529
    14731530    Engine.GetGUIObjectByName("mapInfoDescription").caption = playerString;
    14741531
    14751532    g_IsInGuiUpdate = false;
  • 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%+33 30" sprite="" scrollbar="false" button_width="22" font="sans-stroke-14" tooltip_style="onscreenToolTip">
     76                                <translatableAttribute id="tooltip">Pick a color.</translatableAttribute>
     77                            </object>
     78                            <object name="playerAssignment[n]" type="dropdown" style="ModernDropDown" size="22%+37 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"