private void InitializeValuesAndReferences() { _aiPlayerColorsInUse = new HashSet<GameColor>(GameColorEqualityComparer.Default); _aiPlayerColorSelectedLookup = new Dictionary<GuiPlayerColorPopupList, GameColor>(TempGameValues.MaxAIPlayers); _aiPlayerPopupLookupByColor = new Dictionary<GameColor, GuiPlayerColorPopupList>(TempGameValues.MaxAIPlayers, GameColorEqualityComparer.Default); _aiPlayerColorPopupLists = new List<GuiPlayerColorPopupList>(TempGameValues.MaxAIPlayers); var colorPopupLists = gameObject.GetSafeComponentsInChildren<GuiPlayerColorPopupList>(includeInactive: true); colorPopupLists.ForAll(cpl => { if (cpl.ElementID == GuiElementID.UserPlayerColorPopupList) { _userPlayerColorPopupList = cpl; } else { _aiPlayerColorPopupLists.Add(cpl); } cpl.userSelectedColor += ColorSelectedEventHandler; }); }
private void InitializeValuesAndReferences() { _aiPlayerColorPopupLists = new List<GuiPlayerColorPopupList>(TempGameValues.MaxAIPlayers); //_aiPlayerColorPopupLists = new Dictionary<GuiElementID, GuiPlayerColorPopupList>(TempGameValues.MaxAIPlayers); var colorPopupLists = gameObject.GetSafeMonoBehavioursInChildren<GuiPlayerColorPopupList>(includeInactive: true); colorPopupLists.ForAll(cpl => { if (cpl.ElementID == GuiElementID.UserPlayerColorPopupList) { _userPlayerColorPopupList = cpl; } else { // _aiPlayerColorPopupLists.Add(cpl.ElementID, cpl); _aiPlayerColorPopupLists.Add(cpl); } //cpl.onSelectionChanged += OnColorSelectionChanged; }); //_colorsInUse = new HashSet<GameColor>(); //_aiPlayerColorsInUse = new Dictionary<GameColor, GuiPlayerColorPopupList>(TempGameValues.MaxAIPlayers); //SubscribeToColorSelectionChanges(); _menuCancelButton = gameObject.GetSafeFirstMonoBehaviourInChildren<MenuCancelButton>(); }
private void UpdateAIPlayerCollections(GuiPlayerColorPopupList aiColorPopupList) { var colorSelected = aiColorPopupList.SelectedColor; _aiPlayerPopupLookupByColor[colorSelected] = aiColorPopupList; _aiPlayerColorSelectedLookup[aiColorPopupList] = colorSelected; }
/// <summary> /// Processes the AiPlayer's color selection change. /// </summary> /// <param name="aiPlayerColorPopupList">The AI player color popup list.</param> /// <param name="aiPlayerColorSelected">The changed AiPlayer color selected.</param> private void ProcessAIPlayerColorSelectionChange(GuiPlayerColorPopupList aiPlayerColorPopupList, GameColor changedAiPlayerColorSelected) { // the user has selected a color for an aiPlayer so refresh the colors in use RefreshAIPlayerColorsInUse(); // find the aiPlayer PopupList that is currently using this color, if any GuiPlayerColorPopupList aiPlayerPopupListAlreadyUsingColor; if (_aiPlayerPopupLookupByColor.TryGetValue(changedAiPlayerColorSelected, out aiPlayerPopupListAlreadyUsingColor)) { var allColorsInUse = new List<GameColor>(_aiPlayerColorsInUse) { _userPlayerColorSelected }.ToArray(); var unusedColors = _allPlayerColors.Except(allColorsInUse); ChangeAIPlayerColorSelection(aiPlayerPopupListAlreadyUsingColor, unusedColors); } UpdateAIPlayerCollections(aiPlayerColorPopupList); }
private void HandleAIPlayerColorSelection(GuiPlayerColorPopupList aiPlayerColorPopupList) { var aiPlayerColorSelected = aiPlayerColorPopupList.SelectedColor; if (aiPlayerColorSelected == _aiPlayerColorSelectedLookup[aiPlayerColorPopupList]) { // ignore, as the user selected the same color that was already selected return; } ProcessAIPlayerColorSelectionChange(aiPlayerColorPopupList, aiPlayerColorSelected); }
/// <summary> /// Changes the color selection for the provided aiPopupList using the provided unusedColors, then /// restores the choices available to the popupList to allPlayerColors except the color currently used by /// the UserPlayer. /// </summary> /// <param name="aiPopupList">The AIpopup list.</param> /// <param name="unusedColors">The unused colors.</param> private void ChangeAIPlayerColorSelection(GuiPlayerColorPopupList aiPopupList, IEnumerable<GameColor> unusedColors) { // pick an unused color for this aiPlayer aiPopupList.AssignColorSelectionChoices(unusedColors); aiPopupList.RefreshSelectionFromPreference(); // no default needed as all unusedColor choices are acceptable // restore the choices so the user sees the right choices going forward aiPopupList.AssignColorSelectionChoices(_allPlayerColors.Except(_userPlayerColorSelected)); RefreshAIPlayerColorsInUse(); UpdateAIPlayerCollections(aiPopupList); }