Ticket #1580: 1580.14.patch
File 1580.14.patch, 9.9 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/gui/common/functions_utility.js
144 144 if (alpha) 145 145 ret += " " + alpha; 146 146 return ret; 147 147 } 148 148 149 function sameColor(color1, color2) 150 { 151 return color1.r === color2.r && color1.g === color2.g && color1.b === color2.b; 152 } 153 154 /** 155 * Computes the euclidian distance between the two colors. 156 * The smaller the return value, the close the colors. Zero if identical. 157 */ 158 function colorDistance(color1, color2) 159 { 160 return Math.sqrt(Math.pow(color2.r - color1.r, 2) + Math.pow(color2.g - color1.g, 2) + Math.pow(color2.b - color1.b, 2)); 161 } 162 149 163 // ==================================================================== 150 164 151 165 /** 152 166 * Convert time in milliseconds to [hh:]mm:ss string representation. 153 167 * @param time Time period in milliseconds (integer) -
binaries/data/mods/public/gui/gamesetup/gamesetup.js
8 8 const g_MapTypes = prepareForDropdown(g_Settings ? g_Settings.MapTypes : undefined); 9 9 const g_PopulationCapacities = prepareForDropdown(g_Settings ? g_Settings.PopulationCapacities : undefined); 10 10 const g_StartingResources = prepareForDropdown(g_Settings ? g_Settings.StartingResources : undefined); 11 11 const g_VictoryConditions = prepareForDropdown(g_Settings ? g_Settings.VictoryConditions : undefined); 12 12 13 // All colors except gaia 14 const g_PlayerColors = initPlayerDefaults().slice(1).map(pData => pData.Color); 15 13 16 //////////////////////////////////////////////////////////////////////////////////////////////// 14 17 15 18 // Is this is a networked game, or offline 16 19 var g_IsNetworked; 17 20 … … 364 367 g_GameAttributes.settings.PlayerData[playerSlot].Team = this.selected - 1; 365 368 366 369 updateGameAttributes(); 367 370 }; 368 371 372 // Populate color drop-down lists. 373 var colorPicker = Engine.GetGUIObjectByName("playerColorPicker["+i+"]"); 374 colorPicker.list = g_PlayerColors.map(color => '[color="' + color.r + ' ' + color.g + ' ' + color.b + '"] ■[/color]'); 375 colorPicker.list_data = g_PlayerColors.map((color, index) => index); 376 colorPicker.selected = -1; 377 colorPicker.onSelectionChange = function() { selectPlayerColor(playerSlot, this.selected); }; 378 369 379 // Set events 370 380 var civ = Engine.GetGUIObjectByName("playerCiv["+i+"]"); 371 381 civ.onSelectionChange = function() { 372 382 if ((this.selected != -1)&&(g_GameAttributes.mapType !== "scenario")) 373 383 g_GameAttributes.settings.PlayerData[playerSlot].Civ = this.list_data[this.selected]; … … 875 885 } 876 886 877 887 updateGameAttributes(); 878 888 } 879 889 890 /** 891 * Assigns the given color to that player. 892 */ 893 function selectPlayerColor(playerSlot, colorIndex) 894 { 895 if (colorIndex == -1) 896 return; 897 898 var playerData = g_GameAttributes.settings.PlayerData; 899 900 // If someone else has that color, give that player the old color 901 var pData = playerData.find(pData => sameColor(g_PlayerColors[colorIndex], pData.Color)); 902 if (pData) 903 pData.Color = playerData[playerSlot].Color; 904 905 // Assign the new color 906 playerData[playerSlot].Color = g_PlayerColors[colorIndex]; 907 908 // Ensure colors are not used twice after increasing the number of players 909 ensureUniquePlayerColors(playerData); 910 911 if (!g_IsInGuiUpdate) 912 updateGameAttributes(); 913 } 914 915 function ensureUniquePlayerColors(playerData) 916 { 917 for (let i = playerData.length - 1; i >= 0; --i) 918 { 919 // If someone else has that color, assign an unused color 920 if (playerData.some((pData, j) => i != j && sameColor(playerData[i].Color, pData.Color))) 921 playerData[i].Color = g_PlayerColors.find(color => playerData.every(pData => !sameColor(color, pData.Color))); 922 } 923 } 924 880 925 // Called when the user selects a map type from the list 881 926 function selectMapType(type) 882 927 { 883 928 // Avoid recursion 884 929 if (g_IsInGuiUpdate) … … 981 1026 let victoryIdx = mapSettings.GameType !== undefined && g_VictoryConditions.Name.indexOf(mapSettings.GameType) != -1 ? g_VictoryConditions.Name.indexOf(mapSettings.GameType) : g_VictoryConditions.Default; 982 1027 g_GameAttributes.settings.GameType = g_VictoryConditions.Name[victoryIdx]; 983 1028 g_GameAttributes.settings.VictoryScripts = g_VictoryConditions.Scripts[victoryIdx]; 984 1029 } 985 1030 1031 // Sanitize player data 1032 if (mapSettings.PlayerData) 1033 { 1034 // Ignore gaia 1035 if (mapSettings.PlayerData.length && !mapSettings.PlayerData[0]) 1036 mapSettings.PlayerData.shift(); 1037 1038 // Set default color if no color present 1039 mapSettings.PlayerData.forEach((pData, index) => { 1040 if (pData && !pData.Color) 1041 pData.Color = g_PlayerColors[index]; 1042 }); 1043 1044 // Replace colors with the best matching color of PlayerDefaults 1045 mapSettings.PlayerData.forEach((pData, index) => { 1046 let colorDistances = g_PlayerColors.map(color => colorDistance(color, pData.Color)); 1047 let smallestDistance = colorDistances.find(distance => colorDistances.every(distance2 => (distance2 >= distance))); 1048 pData.Color = g_PlayerColors.find(color => colorDistance(color, pData.Color) == smallestDistance); 1049 }); 1050 1051 ensureUniquePlayerColors(mapSettings.PlayerData); 1052 } 1053 986 1054 // Copy any new settings 987 1055 g_GameAttributes.map = name; 988 1056 g_GameAttributes.script = mapSettings.Script; 989 1057 if (g_GameAttributes.map !== "random") 990 1058 for (var prop in mapSettings) 991 1059 g_GameAttributes.settings[prop] = mapSettings[prop]; 992 1060 993 // Ignore gaia994 if (g_GameAttributes.settings.PlayerData.length && !g_GameAttributes.settings.PlayerData[0])995 g_GameAttributes.settings.PlayerData.shift();996 997 1061 // Use default AI if the map doesn't specify any explicitly 998 1062 for (var i = 0; i < g_GameAttributes.settings.PlayerData.length; ++i) 999 1063 { 1000 1064 if (!('AI' in g_GameAttributes.settings.PlayerData[i])) 1001 1065 g_GameAttributes.settings.PlayerData[i].AI = g_DefaultPlayerData[i].AI; … … 1415 1479 // Player data / defaults 1416 1480 var pData = mapSettings.PlayerData ? mapSettings.PlayerData[i] : {}; 1417 1481 var pDefs = g_DefaultPlayerData ? g_DefaultPlayerData[i] : {}; 1418 1482 1419 1483 // Common to all game types 1420 var color = rgbToGuiColor(getSetting(pData, pDefs, "Color"));1421 pColor.sprite = "color:" + color+ " 100";1484 var color = getSetting(pData, pDefs, "Color"); 1485 pColor.sprite = "color:" + rgbToGuiColor(color) + " 100"; 1422 1486 pName.caption = translate(getSetting(pData, pDefs, "Name")); 1423 1487 1424 1488 var team = getSetting(pData, pDefs, "Team"); 1425 1489 var civ = getSetting(pData, pDefs, "Civ"); 1426 1490 … … 1455 1519 pTeam.hidden = false; 1456 1520 // Set dropdown values 1457 1521 pCiv.selected = (civ ? pCiv.list_data.indexOf(civ) : 0); 1458 1522 pTeam.selected = (team !== undefined && team >= 0) ? team+1 : 0; 1459 1523 } 1524 1525 // Allow host to chose player colors on non-scenario maps 1526 const pColorPicker = Engine.GetGUIObjectByName("playerColorPicker["+i+"]"); 1527 const pColorPickerHeading = Engine.GetGUIObjectByName("playerColorHeading"); 1528 const canChangeColors = g_IsController && g_GameAttributes.mapType != "scenario"; 1529 pColorPicker.hidden = !canChangeColors; 1530 pColorPickerHeading.hidden = !canChangeColors; 1531 if (canChangeColors) 1532 pColorPicker.selected = g_PlayerColors.findIndex(col => sameColor(col, color)); 1460 1533 } 1461 1534 1462 1535 Engine.GetGUIObjectByName("mapInfoDescription").caption = playerString; 1463 1536 1464 1537 g_IsInGuiUpdate = false; -
binaries/data/mods/public/gui/gamesetup/gamesetup.xml
40 40 <object size="24 49 100%-440 345" type="image" sprite="ModernDarkBoxGold" name="playerAssignmentsPanel"> 41 41 <object size="0 6 100% 30"> 42 42 <object name="playerNameHeading" type="text" style="ModernLabelText" size="0 0 22% 100%"> 43 43 <translatableAttribute id="caption">Player Name</translatableAttribute> 44 44 </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%"> 46 49 <translatableAttribute id="caption">Player Placement</translatableAttribute> 47 50 </object> 48 51 <object name="playerCivHeading" type="text" style="ModernLabelText" size="50%+65 0 85%-26 100%"> 49 52 <translatableAttribute id="caption">Civilization</translatableAttribute> 50 53 </object> … … 67 70 <object size="1 36 100%-1 100%"> 68 71 <repeat count="8"> 69 72 <object name="playerBox[n]" size="0 0 100% 32" hidden="true"> 70 73 <object name="playerColor[n]" type="image" size="0 0 100% 100%"/> 71 74 <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"> 73 79 <translatableAttribute id="tooltip">Select player.</translatableAttribute> 74 80 </object> 75 81 <object name="playerAssignmentText[n]" type="text" style="ModernLabelText" size="22%+5 0 50%+35 30"/> 76 82 <object name="playerConfig[n]" type="button" style="StoneButton" size="50%+40 4 50%+64 28" 77 83 tooltip_style="onscreenToolTip"