195 lines
6.2 KiB
C#
195 lines
6.2 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using TMPro;
|
||
using UnityEngine.UI;
|
||
|
||
public class NoteBookUI : MonoBehaviour
|
||
{
|
||
[SerializeField] private TextMeshProUGUI _mainText;
|
||
[SerializeField] private TextMeshProUGUI _highlightText;
|
||
[SerializeField] private GameObject _highlightBox;
|
||
[SerializeField] private Button _leftButton;
|
||
[SerializeField] private Button _rightButton;
|
||
[SerializeField] private GameObject _canvasRoot;
|
||
|
||
private List<string> _pages = new();
|
||
private List<string> _highlights = new();
|
||
private int _currentPage = 0;
|
||
|
||
private void Start()
|
||
{
|
||
if (_leftButton != null)
|
||
_leftButton.onClick.AddListener(PrevPage);
|
||
if (_rightButton != null)
|
||
_rightButton.onClick.AddListener(NextPage);
|
||
if (_canvasRoot != null) _canvasRoot.SetActive(false);
|
||
}
|
||
|
||
public void Show()
|
||
{
|
||
BuildPages();
|
||
_currentPage = 0;
|
||
if (_canvasRoot != null) _canvasRoot.SetActive(true);
|
||
ShowPage(0);
|
||
}
|
||
|
||
public void Hide()
|
||
{
|
||
if (_canvasRoot != null) _canvasRoot.SetActive(false);
|
||
}
|
||
|
||
private void BuildPages()
|
||
{
|
||
_pages.Clear();
|
||
_highlights.Clear();
|
||
|
||
_pages.Add(
|
||
"СИЛА ТЕРТЯ\n" +
|
||
"Тертя спокою:\n" +
|
||
"F ≤ μs · N\n" +
|
||
"Тертя ковзання:\n" +
|
||
"F = μk · N\n" +
|
||
"Тертя кочення:\n" +
|
||
"F = μкоч · N\n\n" +
|
||
"N = m · g · cos α\n" +
|
||
"m - маса тіла (кг)\n" +
|
||
"g = 9.8 м/с²"
|
||
);
|
||
_highlights.Add(
|
||
"Ковзання на нахилі:\n" +
|
||
"F = μk · m · g · cos α\n" +
|
||
"Кочення на нахилі:\n" +
|
||
"F = μкоч · m · g · cos α\n" +
|
||
"Дослідження:\n" +
|
||
"1. Зважити об'єкт → m\n" +
|
||
"2. Спустити з полиці → μ\n"
|
||
);
|
||
|
||
string weighedPage = "ЗВАЖЕНІ ОБ'ЄКТИ\n\n";
|
||
if (GameData.WeighedObjects.Count == 0)
|
||
{
|
||
weighedPage += "Ще нічого не зважено.";
|
||
_pages.Add(weighedPage);
|
||
_highlights.Add("");
|
||
}
|
||
else
|
||
{
|
||
int count = 0;
|
||
foreach (var obj in GameData.WeighedObjects)
|
||
{
|
||
string massStr = obj.mass >= 1f
|
||
? obj.mass + " кг"
|
||
: (obj.mass * 1000f) + " г";
|
||
weighedPage += "• " + obj.name + ": " + massStr + "\n";
|
||
count++;
|
||
if (count >= 8)
|
||
{
|
||
_pages.Add(weighedPage);
|
||
_highlights.Add("");
|
||
weighedPage = "ЗВАЖЕНІ ОБ'ЄКТИ (прод.)\n\n";
|
||
count = 0;
|
||
}
|
||
}
|
||
if (count > 0)
|
||
{
|
||
_pages.Add(weighedPage);
|
||
_highlights.Add("");
|
||
}
|
||
}
|
||
|
||
string frictionPage = "КОЕФІЦІЄНТИ ТЕРТЯ\n\n";
|
||
if (GameData.MeasuredFrictions.Count == 0)
|
||
{
|
||
frictionPage += "Ще нічого не виміряно.\n\n" +
|
||
"Спусти об'єкт з полиці\n" +
|
||
"щоб виміряти μ.";
|
||
_pages.Add(frictionPage);
|
||
_highlights.Add("");
|
||
}
|
||
else
|
||
{
|
||
int count = 0;
|
||
foreach (var f in GameData.MeasuredFrictions)
|
||
{
|
||
string typeStr = f.frictionType switch
|
||
{
|
||
FrictionType.Статичне => "стат.",
|
||
FrictionType.Ковзання => "ковз.",
|
||
FrictionType.Кочення => "коч.",
|
||
_ => ""
|
||
};
|
||
string surf = f.surface switch
|
||
{
|
||
"Дерев'яна поверхня" => "дерево",
|
||
"Металева поверхня" => "метал",
|
||
"Крижана поверхня" => "лід",
|
||
"Гумова поверхня" => "гума",
|
||
_ => f.surface
|
||
};
|
||
frictionPage += "• " + f.objectName + " / " + surf + "\n";
|
||
frictionPage += " μ " + typeStr + " = " + f.mu.ToString("F2") + "\n";
|
||
count++;
|
||
if (count >= 6)
|
||
{
|
||
_pages.Add(frictionPage);
|
||
_highlights.Add("");
|
||
frictionPage = "КОЕФІЦІЄНТИ (прод.)\n\n";
|
||
count = 0;
|
||
}
|
||
}
|
||
if (count > 0)
|
||
{
|
||
_pages.Add(frictionPage);
|
||
_highlights.Add("");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ShowPage(int index)
|
||
{
|
||
if (_mainText != null)
|
||
_mainText.text = _pages[index];
|
||
|
||
bool hasHighlight = !string.IsNullOrEmpty(_highlights[index]);
|
||
if (_highlightBox != null)
|
||
_highlightBox.SetActive(hasHighlight);
|
||
if (_highlightText != null && hasHighlight)
|
||
_highlightText.text = _highlights[index];
|
||
|
||
if (_leftButton != null)
|
||
{
|
||
_leftButton.interactable = index > 0;
|
||
var colors = _leftButton.colors;
|
||
colors.normalColor = index > 0 ? Color.white : new Color(0.5f, 0.5f, 0.5f, 0.5f);
|
||
_leftButton.colors = colors;
|
||
}
|
||
|
||
if (_rightButton != null)
|
||
{
|
||
_rightButton.interactable = index < _pages.Count - 1;
|
||
var colors = _rightButton.colors;
|
||
colors.normalColor = index < _pages.Count - 1 ? Color.white : new Color(0.5f, 0.5f, 0.5f, 0.5f);
|
||
_rightButton.colors = colors;
|
||
}
|
||
}
|
||
|
||
private void NextPage()
|
||
{
|
||
if (_currentPage < _pages.Count - 1)
|
||
{
|
||
_currentPage++;
|
||
ShowPage(_currentPage);
|
||
}
|
||
}
|
||
|
||
private void PrevPage()
|
||
{
|
||
if (_currentPage > 0)
|
||
{
|
||
_currentPage--;
|
||
ShowPage(_currentPage);
|
||
}
|
||
}
|
||
}
|