/** * Copyright 2019 The Knights Of Unity, created by Pawel Stolarczyk * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using DemoGame.Scripts.Session; using Nakama; using UnityEngine; using UnityEngine.UI; namespace DemoGame.Scripts.Clans { /// /// Single user entry in 's member list. /// Contains UI needed to display basic user information aswell as allow for /// multiple clan related operations, such as kicking and promoting users. /// public class ClanUserEntry : MonoBehaviour { #region Fields /// /// Text field containing username. /// [SerializeField] private Text _usernameText = null; /// /// Image representing user's rank. /// [SerializeField] private Image _rankImage = null; #region Interaction Panel [Space] /// /// Animator used to hide and show interaction panel. /// [SerializeField] private Animator _animator = null; /// /// Button used to open and close user interaction panel. /// [SerializeField] private Button _panelButton = null; /// /// Button responsible for kicking current user. /// [SerializeField] private Button _kickButton = null; /// /// Button responsible for promoting current user. /// [SerializeField] private Button _promoteButton = null; /// /// Button responsible for showing of current user. /// [SerializeField] private Button _profileButton = null; /// /// If true, interaction panel is currently showm. /// private bool _isShown; #endregion #region Rank Sprites [Space] /// /// Sprite representing admin rank. /// [SerializeField] private Sprite _adminRankSprite = null; /// /// Sprite representing superadmin rank. /// [SerializeField] private Sprite _superadminRankSprite = null; #endregion #endregion #region Methods /// /// Initializes this object. /// /// The user all associated with this object. /// The state of in currently shown Clan. /// The state of local user in currently shown Clan. /// Method called after clicking . /// Method called after clicking . /// Method called after clicking . public void SetUser(IApiUser user, ClanUserState userState, ClanUserState localState, Action onSelected, Action onKick, Action onPromote, Action onShowProfile) { // Setting basic information based on supplied parameters. _usernameText.text = user.Username; _rankImage.sprite = GetRankSprite(userState); _rankImage.gameObject.SetActive(_rankImage.sprite != null); // If local user is the same as supplied user, show their name in green color // and disable kick and promote options. if (user.Id == NakamaSessionManager.Instance.Account.User.Id) { _usernameText.color = Color.green; _kickButton.gameObject.SetActive(false); _promoteButton.gameObject.SetActive(false); } // If local user cannot manager supplied user (because local user's ClanUserState is lower // or local user does not belong to this clan), disable kick and promote options. else if (CanManageUser(localState, userState) == false) { _kickButton.gameObject.SetActive(false); _promoteButton.gameObject.SetActive(false); } // Add listeners to buttons else { _kickButton.onClick.AddListener(() => onKick?.Invoke(user)); _promoteButton.onClick.AddListener(() => onPromote?.Invoke(user)); } _profileButton.onClick.AddListener(() => onShowProfile?.Invoke(user)); _panelButton.onClick.AddListener(() => onSelected(this)); } /// /// Opens interaction panel. /// public void ShowInteractionPanel() { if (_isShown == false) { _isShown = true; _animator.SetTrigger("Open"); } } /// /// Hides interaction panel. /// public void HideInteractionPanel() { if (_isShown == true) { _isShown = false; _animator.SetTrigger("Close"); } } /// /// Returns a sprite associated with supplied rank. /// Only Admin and Superadmin have rank sprites. /// private Sprite GetRankSprite(ClanUserState userState) { switch (userState) { case ClanUserState.Superadmin: return _superadminRankSprite; case ClanUserState.Admin: return _adminRankSprite; default: return null; } } /// /// Returns true if user can kick and promote user with . /// private bool CanManageUser(ClanUserState localState, ClanUserState managedUser) { switch (localState) { case ClanUserState.Superadmin: return true; case ClanUserState.Admin: return managedUser == ClanUserState.Member || managedUser == ClanUserState.JoinRequest; case ClanUserState.Member: return false; case ClanUserState.JoinRequest: return false; default: return false; } } #endregion } }