197 lines
5.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using Kill.Bluetooth.Protocol;
using Kill.UI.Components;
using UnityEngine;
using UnityEngine.UI;
namespace Kill.UI.Pages
{
public class OneSchedule : MonoBehaviour
{
/// <summary>
/// 状态颜色 0: 关闭(灰色) 1: 开启(白色/正常)
/// </summary>
public Color[] statusColors;
/// <summary>
/// 时间文本 开始时间 - 结束时间00:00 - 01:00
/// </summary>
public Text timeText;
public Image modeImg;
public Sprite[] modeSprites;
/// <summary>
/// 模式和重复时间 模式|重复时间(扫描|周日,周一)
/// </summary>
public Text modeAndRepeatText;
public Switch isOnSwitch;
// 当前任务索引
private int _taskIndex = -1;
// 开关状态变化回调
private Action<int, bool> _onSwitchChanged;
// 点击回调
private Action<int> _onClick;
// 删除回调
private Action<int> _onDelete;
/// <summary>
/// 初始化任务项
/// </summary>
/// <param name="taskIndex">任务索引</param>
/// <param name="task">任务数据</param>
/// <param name="modeText">模式文本</param>
/// <param name="repeatText">重复文本</param>
/// <param name="onSwitchChanged">开关变化回调</param>
/// <param name="onClick">点击回调</param>
/// <param name="onDelete">删除回调</param>
public void Init(int taskIndex, ScheduleTask task, string modeText, string repeatText,
Action<int, bool> onSwitchChanged = null, Action<int> onClick = null, Action<int> onDelete = null)
{
_taskIndex = taskIndex;
_onSwitchChanged = onSwitchChanged;
_onClick = onClick;
_onDelete = onDelete;
// 更新时间文本
UpdateTimeText(task.StartHour, task.StartMinute, task.EndHour, task.EndMinute);
// 更新模式图标
UpdateModeIcon(task.Mode);
// 更新模式和重复文本
UpdateModeAndRepeatText(modeText, repeatText);
// 更新开关状态
UpdateSwitchState(task.Enabled);
// 根据开关状态更新颜色
UpdateColors(task.Enabled);
}
/// <summary>
/// 更新时间文本
/// </summary>
public void UpdateTimeText(byte startHour, byte startMinute, byte endHour, byte endMinute)
{
if (timeText != null)
{
timeText.text = $"{startHour:D2}:{startMinute:D2} - {endHour:D2}:{endMinute:D2}";
}
}
/// <summary>
/// 更新模式图标
/// </summary>
public void UpdateModeIcon(ScheduleTaskMode mode)
{
if (modeImg != null && modeSprites != null && modeSprites.Length > 0)
{
int modeIndex = (int)mode;
if (modeIndex >= 0 && modeIndex < modeSprites.Length)
{
modeImg.sprite = modeSprites[modeIndex];
}
}
}
/// <summary>
/// 更新模式和重复文本
/// </summary>
public void UpdateModeAndRepeatText(string modeText, string repeatText)
{
if (modeAndRepeatText != null)
{
// 如果有重复时间,显示模式和重复时间;否则只显示模式
if (!string.IsNullOrEmpty(repeatText))
{
modeAndRepeatText.text = $"{modeText} | {repeatText}";
}
else
{
modeAndRepeatText.text = modeText;
}
}
}
/// <summary>
/// 更新开关状态
/// </summary>
public void UpdateSwitchState(bool isOn)
{
if (isOnSwitch != null)
{
isOnSwitch.Init(isOn, OnSwitchClicked);
}
}
/// <summary>
/// 根据开关状态更新颜色
/// </summary>
public void UpdateColors(bool isOn)
{
if (statusColors == null || statusColors.Length < 2)
{
return;
}
Color targetColor = isOn ? statusColors[1] : statusColors[0];
// 更新时间文本颜色
if (timeText != null)
{
timeText.color = targetColor;
}
// 更新模式图标颜色
if (modeImg != null)
{
modeImg.color = targetColor;
}
// 更新模式和重复文本颜色
if (modeAndRepeatText != null)
{
modeAndRepeatText.color = targetColor;
}
}
/// <summary>
/// 开关点击回调
/// </summary>
private void OnSwitchClicked(bool isOn)
{
// 更新颜色
UpdateColors(isOn);
// 触发回调
if (_taskIndex >= 0)
{
_onSwitchChanged?.Invoke(_taskIndex, isOn);
}
}
/// <summary>
/// 获取当前任务索引
/// </summary>
public int GetTaskIndex()
{
return _taskIndex;
}
/// <summary>
/// 设置任务索引
/// </summary>
public void SetTaskIndex(int index)
{
_taskIndex = index;
}
}
}