| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- [DisallowMultipleComponent]
- public class HelpScreenView : MonoBehaviour
- {
- public event Action CloseRequested;
- public event Action ReplayIntroRequested;
- struct Entry
- {
- public readonly string title;
- public readonly string body;
- public Entry(string title, string body)
- {
- this.title = title;
- this.body = body;
- }
- }
- static readonly Entry[] Basics =
- {
- new Entry(
- "TAP A FRUIT",
- "Tap any fruit on the front of a shelf to pick it up. It lifts up and starts glowing so you know it is held."),
- new Entry(
- "DROP IT ON A SHELF",
- "Tap a shelf \u2014 or drag the fruit onto it. A shelf with room takes the fruit; a full shelf swaps it with the fruit you dropped on. Each action costs one move."),
- new Entry(
- "SERVE A SET OF 3",
- "Three identical fruits on the same shelf pop instantly and are served. Serving is free \u2014 it never costs a move."),
- new Entry(
- "EMPTY THE STAND",
- "Clear every fruit from every shelf before your moves run out. That clears the level."),
- new Entry(
- "DIG THROUGH LAYERS",
- "Some shelves hide a second layer behind the front row \u2014 you can see its shadow through the gaps. Clear the front row and the layer behind slides forward."),
- new Entry(
- "USE POWER-UPS",
- "Hammer, Shuffle and Master Key are free actions. +5 Moves buys you breathing room. Buy more in the shop.")
- };
- static readonly Entry[] Faqs =
- {
- new Entry(
- "How do I clear a level?",
- "Empty every shelf on the stand before you run out of moves. Each tap that moves a fruit counts as one move; serving a set of three is free."),
- new Entry(
- "Do I choose where the fruit goes?",
- "Yes. Tap a fruit to pick it up, then tap any shelf \u2014 or drag the fruit onto it. You can put it anywhere that still has a free slot."),
- new Entry(
- "What if the shelf is already full?",
- "Drop on a full shelf and the two fruits trade places \u2014 the one you are holding goes in, the one you dropped on comes back to the shelf you took it from. That also costs one move."),
- new Entry(
- "What happens when a shelf holds three of a kind?",
- "They are served immediately and the shelf is empty again. You cannot hold a set of three and save it for later."),
- new Entry(
- "What are the hidden layers?",
- "Some shelves carry a second row of fruit behind the front row. You can see their shadow through the gaps. Once the front row is gone the hidden row slides forward and becomes tappable."),
- new Entry(
- "What do the chains mean?",
- "Chained shelves cannot be used at all. Every set you serve removes one chain from every chained shelf. Master Key snaps one shelf open instantly."),
- new Entry(
- "Do power-ups cost a move?",
- "Hammer, Shuffle and Master Key do not. +5 Moves simply adds five moves to the counter."),
- new Entry(
- "Why can I not drop it there?",
- "That shelf is chained shut, or it already holds three fruits. Pick another highlighted shelf."),
- new Entry(
- "How do I earn coins?",
- "Every served set pays 2 coins. Clearing a level pays a bonus, and leftover moves pay 8 coins each."),
- new Entry(
- "How do I get draw tickets?",
- "The first clear of level 1 always drops one ticket. Every other clear has a 35% chance."),
- new Entry(
- "Does progress save?",
- "Yes \u2014 coins, power-ups, level progress, name and daily counters are stored in this browser."),
- new Entry(
- "Is there anything to buy with real money?",
- "No. This is a free single-player prototype with no purchases, ads or cash value.")
- };
- const string ContentPath = "IMG_BG/GRP_Content/Viewport/Content";
- const string BasicTemplatePath = "Templates/ITEM_BasicTemplate";
- const string FaqTemplatePath = "Templates/ITEM_FaqTemplate";
- [Header("Prefab References")]
- [SerializeField] Transform contentRoot;
- [SerializeField] GameObject basicTemplate;
- [SerializeField] GameObject faqTemplate;
- [SerializeField] Transform basicsTitle;
- [SerializeField] Transform faqTitle;
- [SerializeField] Button closeButton;
- [SerializeField] Button replayButton;
- [SerializeField] ScrollRect scrollRect;
- readonly List<UIHelpItem> faqItems = new List<UIHelpItem>();
- readonly List<GameObject> runtimeItems = new List<GameObject>();
- Sprite[] numberSprites;
- bool built;
- bool closeWired;
- bool replayWired;
- bool showing;
- void Awake()
- {
- ResolveReferences();
- WireButtons();
- if (!showing)
- gameObject.SetActive(false);
- }
- void OnEnable()
- {
- LocaleService.Changed += HandleLocaleChanged;
- }
- void OnDisable()
- {
- LocaleService.Changed -= HandleLocaleChanged;
- }
- void OnDestroy()
- {
- LocaleService.Changed -= HandleLocaleChanged;
- if (closeButton != null)
- closeButton.onClick.RemoveListener(RequestClose);
- if (replayButton != null)
- replayButton.onClick.RemoveListener(RequestReplay);
- }
- void HandleLocaleChanged()
- {
- if (isActiveAndEnabled)
- {
- UILocaleScreens.ApplyHelp(transform);
- RefreshLocalizedItems();
- }
- }
- public void Show()
- {
- showing = true;
- ResolveReferences();
- ConfigureContent();
- WireButtons();
- transform.SetAsLastSibling();
- gameObject.SetActive(true);
- UILocaleScreens.ApplyHelp(transform);
- BuildItems();
- RefreshLocalizedItems();
- RefreshLayout();
- StartCoroutine(ExpandDefaultsNextFrame());
- ResetScroll();
- }
- public void Hide()
- {
- showing = false;
- StopAllCoroutines();
- gameObject.SetActive(false);
- }
- void ResolveReferences()
- {
- if (contentRoot == null)
- contentRoot = UIViewUtility.Find(transform, ContentPath);
- if (contentRoot == null)
- contentRoot = UIViewUtility.Find(transform, "Content");
- if (basicTemplate == null)
- {
- Transform template = UIViewUtility.Find(transform, BasicTemplatePath);
- if (template != null)
- basicTemplate = template.gameObject;
- }
- if (faqTemplate == null)
- {
- Transform template = UIViewUtility.Find(transform, FaqTemplatePath);
- if (template != null)
- faqTemplate = template.gameObject;
- }
- if (basicsTitle == null && contentRoot != null)
- basicsTitle = contentRoot.Find("TMP_SectionBasics");
- if (faqTitle == null && contentRoot != null)
- faqTitle = contentRoot.Find("TMP_SectionFaq");
- if (closeButton == null)
- closeButton = UIViewUtility.Get<Button>(transform, "IMG_BG/BTN_ClosePause");
- if (replayButton == null)
- replayButton = UIViewUtility.Get<Button>(transform, ContentPath + "/GRP_BtnReplayIntro/BTN_ReplayIntro");
- if (scrollRect == null)
- scrollRect = UIViewUtility.Get<ScrollRect>(transform, "IMG_BG/GRP_Content");
- if (numberSprites == null)
- numberSprites = Resources.LoadAll<Sprite>("UI/06_help/spr_help_num");
- }
- void ConfigureContent()
- {
- if (contentRoot == null)
- return;
- VerticalLayoutGroup layout = contentRoot.GetComponent<VerticalLayoutGroup>();
- if (layout == null)
- layout = contentRoot.gameObject.AddComponent<VerticalLayoutGroup>();
- layout.spacing = 10f;
- layout.childAlignment = TextAnchor.UpperCenter;
- layout.childControlWidth = true;
- layout.childControlHeight = true;
- layout.childForceExpandWidth = true;
- layout.childForceExpandHeight = false;
- ContentSizeFitter fitter = contentRoot.GetComponent<ContentSizeFitter>();
- if (fitter == null)
- fitter = contentRoot.gameObject.AddComponent<ContentSizeFitter>();
- fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
- fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
- if (scrollRect != null)
- {
- scrollRect.horizontal = false;
- scrollRect.vertical = true;
- scrollRect.movementType = ScrollRect.MovementType.Clamped;
- scrollRect.scrollSensitivity = 30f;
- }
- }
- void EnsureSectionOrder()
- {
- if (basicsTitle != null)
- basicsTitle.SetSiblingIndex(0);
- Transform replayGroup = contentRoot.Find("GRP_BtnReplayIntro");
- if (replayGroup != null)
- replayGroup.SetSiblingIndex(1);
- if (faqTitle != null)
- faqTitle.SetSiblingIndex(2);
- }
- void BuildItems()
- {
- if (built)
- return;
- if (contentRoot == null || basicTemplate == null || faqTemplate == null)
- {
- Debug.LogWarning("[HelpScreenView] Help templates are not available.");
- return;
- }
- EnsureSectionOrder();
- built = true;
- int basicIndex = basicsTitle != null ? basicsTitle.GetSiblingIndex() + 1 : 0;
- for (int i = 0; i < Basics.Length; i++)
- {
- GameObject instance = Instantiate(basicTemplate, contentRoot, false);
- instance.name = "ITEM_Basic_" + (i + 1);
- instance.transform.SetSiblingIndex(basicIndex++);
- instance.SetActive(true);
- UIHelpItem item = instance.GetComponent<UIHelpItem>();
- if (item == null)
- item = instance.AddComponent<UIHelpItem>();
- item.Configure((RectTransform)contentRoot, false, true, 48f, 48f, 14f);
- item.SetData(L.Get("help.basic" + (i + 1) + ".title"), L.Get("help.basic" + (i + 1) + ".body"));
- item.SetNumber(GetNumberSprite(i));
- runtimeItems.Add(instance);
- }
- int faqIndex = faqTitle != null ? faqTitle.GetSiblingIndex() + 1 : contentRoot.childCount;
- for (int i = 0; i < Faqs.Length; i++)
- {
- GameObject instance = Instantiate(faqTemplate, contentRoot, false);
- instance.name = "ITEM_Faq_" + (i + 1);
- instance.transform.SetSiblingIndex(faqIndex++);
- instance.SetActive(true);
- UIHelpItem item = instance.GetComponent<UIHelpItem>();
- if (item == null)
- item = instance.AddComponent<UIHelpItem>();
- item.Configure((RectTransform)contentRoot, true, false, 60f, 68f, 16f);
- item.SetData(L.Get("help.faq" + (i + 1) + ".title"), L.Get("help.faq" + (i + 1) + ".body"));
- if (item.HeaderButton != null)
- UIViewUtility.Bind(item.HeaderButton, item.Toggle);
- faqItems.Add(item);
- runtimeItems.Add(instance);
- }
- }
- void RefreshLayout()
- {
- if (contentRoot == null)
- return;
- Canvas.ForceUpdateCanvases();
- RectTransform contentRect = contentRoot as RectTransform;
- if (contentRect != null)
- LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
- for (int i = 0; i < runtimeItems.Count; i++)
- {
- UIHelpItem item = runtimeItems[i].GetComponent<UIHelpItem>();
- if (item != null)
- item.RefreshLayout();
- }
- if (contentRect != null)
- LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
- ResetScroll();
- }
- IEnumerator ExpandDefaultsNextFrame()
- {
- yield return null;
- Canvas.ForceUpdateCanvases();
- RectTransform contentRect = contentRoot as RectTransform;
- if (contentRect != null)
- LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
- for (int i = 0; i < faqItems.Count; i++)
- faqItems[i].SetExpanded(i < 2, i == 0);
- if (contentRect != null)
- LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
- ResetScroll();
- }
- void ResetScroll()
- {
- if (scrollRect == null)
- return;
- scrollRect.verticalNormalizedPosition = 1f;
- }
- Sprite GetNumberSprite(int index)
- {
- if (numberSprites == null || numberSprites.Length == 0)
- return null;
- string wanted = (index + 1).ToString();
- for (int i = 0; i < numberSprites.Length; i++)
- {
- if (numberSprites[i] != null && numberSprites[i].name == wanted)
- return numberSprites[i];
- }
- for (int i = 0; i < numberSprites.Length; i++)
- {
- if (numberSprites[i] != null && numberSprites[i].name == "spr_help_num_" + index)
- return numberSprites[i];
- }
- return index < numberSprites.Length ? numberSprites[index] : null;
- }
- void WireButtons()
- {
- if (!closeWired && closeButton != null)
- {
- UIViewUtility.Bind(closeButton, RequestClose);
- closeWired = true;
- }
- if (!replayWired && replayButton != null)
- {
- UIViewUtility.Bind(replayButton, RequestReplay);
- replayWired = true;
- }
- }
- void RequestClose()
- {
- CloseRequested?.Invoke();
- }
- void RequestReplay()
- {
- ReplayIntroRequested?.Invoke();
- }
- void RefreshLocalizedItems()
- {
- if (!built)
- return;
- int basicsCount = Basics.Length;
- for (int i = 0; i < basicsCount && i < runtimeItems.Count; i++)
- {
- UIHelpItem item = runtimeItems[i].GetComponent<UIHelpItem>();
- if (item != null)
- item.SetData(L.Get("help.basic" + (i + 1) + ".title"), L.Get("help.basic" + (i + 1) + ".body"));
- }
- for (int i = 0; i < Faqs.Length; i++)
- {
- int index = basicsCount + i;
- if (index >= runtimeItems.Count)
- break;
- UIHelpItem item = runtimeItems[index].GetComponent<UIHelpItem>();
- if (item != null)
- item.SetData(L.Get("help.faq" + (i + 1) + ".title"), L.Get("help.faq" + (i + 1) + ".body"));
- }
- }
- }
|