157 lines
4.3 KiB
C#
157 lines
4.3 KiB
C#
using CharacterSystem;
|
|
using PrizeSystem;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class QrTestPanel : MonoBehaviour, IControlable
|
|
{
|
|
public event Action OnClosed;
|
|
|
|
[SerializeField] private TextMeshProUGUI _characterName;
|
|
[SerializeField] private Image _characterIcon;
|
|
[SerializeField] private Image _qrImage;
|
|
[SerializeField] private GameObject _leftArrow;
|
|
[SerializeField] private GameObject _rightArrow;
|
|
[SerializeField] private TextMeshProUGUI _counter;
|
|
|
|
private readonly List<Entry> _entries = new();
|
|
private int _currentIndex;
|
|
private Sprite _generatedQrSprite;
|
|
private Texture2D _generatedQrTexture;
|
|
private bool _waitForActionRelease;
|
|
|
|
public bool ControlsEnabled { get; set; }
|
|
|
|
private void OnDisable()
|
|
{
|
|
ClearGeneratedQr();
|
|
}
|
|
|
|
public bool Initialize()
|
|
{
|
|
CollectEntries();
|
|
|
|
if (_entries.Count == 0)
|
|
return false;
|
|
|
|
_currentIndex = 0;
|
|
_waitForActionRelease = true;
|
|
ControlsEnabled = true;
|
|
ShowCurrentEntry();
|
|
return true;
|
|
}
|
|
|
|
public void UpdateControls()
|
|
{
|
|
if (_waitForActionRelease)
|
|
{
|
|
if (In.GetAction(JoystickSide.Both, InteractMode.Continious))
|
|
return;
|
|
|
|
_waitForActionRelease = false;
|
|
}
|
|
|
|
int horizontal = In.GetAxis(JoystickSide.Both, InteractMode.Discrete).x;
|
|
|
|
if (horizontal != 0 && _entries.Count > 1)
|
|
SwitchEntry(horizontal);
|
|
|
|
if (In.GetAction(JoystickSide.Both, InteractMode.Discrete))
|
|
Close();
|
|
}
|
|
|
|
private void CollectEntries()
|
|
{
|
|
_entries.Clear();
|
|
|
|
PrizeContainer prizeContainer = PrizeSystemManager.PrizeContainer;
|
|
Character[] characters = CharactersController.GetCharactersArray();
|
|
|
|
if (prizeContainer?.Sets == null || characters == null)
|
|
return;
|
|
|
|
int setsCount = Mathf.Min(prizeContainer.Sets.Count, characters.Length);
|
|
|
|
for (int characterIndex = 0; characterIndex < setsCount; characterIndex++)
|
|
{
|
|
PrizeSet set = prizeContainer.Sets[characterIndex];
|
|
|
|
if (set?.Prizes == null)
|
|
continue;
|
|
|
|
foreach (Prize prize in set.Prizes)
|
|
{
|
|
if (prize is QrPrize qrPrize)
|
|
_entries.Add(new Entry(characters[characterIndex], qrPrize));
|
|
}
|
|
}
|
|
|
|
_entries.Sort((left, right) => left.Character.CharacterIndex.CompareTo(right.Character.CharacterIndex));
|
|
}
|
|
|
|
private void SwitchEntry(int direction)
|
|
{
|
|
_currentIndex = (_currentIndex + direction + _entries.Count) % _entries.Count;
|
|
ShowCurrentEntry();
|
|
}
|
|
|
|
private void ShowCurrentEntry()
|
|
{
|
|
Entry entry = _entries[_currentIndex];
|
|
_characterName.text = entry.Character.characterName;
|
|
_characterIcon.sprite = entry.Character.visualModule.icon;
|
|
_characterIcon.preserveAspect = true;
|
|
_counter.text = $"{_currentIndex + 1} / {_entries.Count}";
|
|
|
|
bool hasSeveralEntries = _entries.Count > 1;
|
|
_leftArrow.SetActive(hasSeveralEntries);
|
|
_rightArrow.SetActive(hasSeveralEntries);
|
|
|
|
ClearGeneratedQr();
|
|
_generatedQrTexture = QrMaker.MakePrizeTexture(entry.Prize, 5);
|
|
_generatedQrTexture.filterMode = FilterMode.Point;
|
|
_generatedQrSprite = Sprite.Create(
|
|
_generatedQrTexture,
|
|
new Rect(0, 0, _generatedQrTexture.width, _generatedQrTexture.height),
|
|
new Vector2(0.5f, 0.5f));
|
|
_qrImage.sprite = _generatedQrSprite;
|
|
_qrImage.preserveAspect = true;
|
|
}
|
|
|
|
private void ClearGeneratedQr()
|
|
{
|
|
if (_qrImage != null)
|
|
_qrImage.sprite = null;
|
|
|
|
if (_generatedQrSprite != null)
|
|
Destroy(_generatedQrSprite);
|
|
|
|
if (_generatedQrTexture != null)
|
|
Destroy(_generatedQrTexture);
|
|
|
|
_generatedQrSprite = null;
|
|
_generatedQrTexture = null;
|
|
}
|
|
|
|
private void Close()
|
|
{
|
|
ControlsEnabled = false;
|
|
OnClosed?.Invoke();
|
|
}
|
|
|
|
private readonly struct Entry
|
|
{
|
|
public Character Character { get; }
|
|
public QrPrize Prize { get; }
|
|
|
|
public Entry(Character character, QrPrize prize)
|
|
{
|
|
Character = character;
|
|
Prize = prize;
|
|
}
|
|
}
|
|
}
|