221 lines
6.2 KiB
C#
Raw Normal View History

2025-02-21 11:49:10 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.IO;
using UnityEngine.Rendering.Universal;
using TMPro;
using UnityEngine.EventSystems;
using DG.Tweening;
namespace QFramework.Example
{
public class UIDrawData : UIPanelData
{
}
public partial class UIDraw : UIPanel
{
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UIDrawData ?? new UIDrawData();
// <20>رհ<D8B1>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>
CloseBtn.onClick.AddListener(() =>
{
StringEventSystem.Global.Send($"On{UIDraw.Name}Close");
Hide();
});
PenSizeSlider.onValueChanged.AddListener(value =>
{
RefreshPenSize();
});
AlphaSlider.onValueChanged.AddListener(value =>
{
RefreshAlphaSet();
});
PenHandle.OnPointerEnterEvent(data =>
{
Penvalue.gameObject.SetActive(true);
});
AlphaHandle.OnPointerEnterEvent(data =>
{
AlphaValue.gameObject.SetActive(true);
});
HideBtn.onValueChanged.AddListener(isOn =>
{
if (isOn)
{
Content.GetComponent<DOTweenAnimation>().DORestart();
HideBtn.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = "<22><>ʾ";
}
else
{
Content.GetComponent<DOTweenAnimation>().DOPlayBackwards();
HideBtn.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = "<22><><EFBFBD><EFBFBD>";
}
});
foreach (var toggle in Colors.GetComponentsInChildren<Toggle>())
{
toggle.onValueChanged.AddListener(isOn =>
{
if (isOn)
{
ChangeColor(toggle.transform.Find("Color").GetComponent<Image>().color);
}
});
}
BackBtn.onClick.AddListener(() =>
{
2025-02-21 17:26:17 +08:00
ScreenShotPainter.instance.Undo();
2025-02-21 11:49:10 +08:00
});
ClearBtn.onClick.AddListener(() =>
{
2025-02-21 17:26:17 +08:00
ScreenShotPainter.instance.Clear();
2025-02-21 11:49:10 +08:00
});
Pen.onValueChanged.AddListener(isOn =>
{
2025-02-21 17:26:17 +08:00
if (isOn)
{
ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Pen);
}
2025-02-21 11:49:10 +08:00
});
Eraser.onValueChanged.AddListener(isOn =>
{
2025-02-21 17:26:17 +08:00
if (isOn)
{
ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Eraser);
}
2025-02-21 11:49:10 +08:00
});
2025-02-21 17:26:17 +08:00
Line.onValueChanged.AddListener(isOn =>
{
if (isOn)
{
ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Line);
}
2025-02-21 11:49:10 +08:00
2025-02-21 17:26:17 +08:00
});
2025-02-21 11:49:10 +08:00
}
public void ChangeColor(Color color)
{
Icon.color = new Color(color.r, color.g, color.b, Icon.color.a);
2025-02-21 17:26:17 +08:00
ScreenShotPainter.instance.SetPaintColor(Icon.color);
2025-02-21 11:49:10 +08:00
}
public void RefreshPenSize()
{
2025-02-21 17:26:17 +08:00
var normalValue = PenSizeSlider.value / PenSizeSlider.maxValue;
float size = PaintingParams.BrushSizeMaxValue + (1 - normalValue) * PaintingParams.BrushSizeFactor;
//ScreenShotPainter.instance.SetPaintingSize(size);
float sizeY = ((float)Screen.height / (float)Screen.width) * size;
ScreenShotPainter.instance.SetPaintingSize(size, sizeY);
float width = Screen.width / size;
float height = Screen.width / size;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>y<EFBFBD><79><EFBFBD><EFBFBD>
Icon.transform.localScale = new Vector3(width / Icon.rectTransform.rect.width, height / Icon.rectTransform.rect.height, 1);
Penvalue.text = PenSizeSlider.value.ToString();
2025-02-21 11:49:10 +08:00
}
public void RefreshAlphaSet()
{
var color = Icon.color;
color.a = AlphaSlider.value / 100f;
Icon.color = color;
2025-02-21 17:50:33 +08:00
ScreenShotPainter.instance.SetPaintColor(Icon.color);
2025-02-21 11:49:10 +08:00
AlphaValue.text = AlphaSlider.value.ToString();
}
public void SetupCameraStack(Camera subCamera, Camera baseCamera)
{
UniversalAdditionalCameraData additionalCameraData = baseCamera.GetUniversalAdditionalCameraData();
if (!additionalCameraData.cameraStack.Contains(subCamera))
{
additionalCameraData.cameraStack.Add(subCamera);
}
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Penvalue.gameObject.SetActive(false);
AlphaValue.gameObject.SetActive(false);
}
}
private void HandleSaveClearColorInput()
{
if (Input.GetKeyDown(KeyCode.S))
{
SaveDrawing();
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
private void SaveDrawing()
{
string filePath = Application.dataPath + "/Drawings/" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
ScreenCapture.CaptureScreenshot(filePath);
Debug.Log("Drawing saved to: " + filePath);
}
protected override void OnOpen(IUIData uiData = null)
{
Show3DCamera.instance.lockMove = true;
DrawController.instance.gameObject.SetActive(true);
2025-02-21 17:26:17 +08:00
ScreenShotPainter.instance.Init(RawImg, captureBGImg);
ScreenShotPainter.instance.SwitchOn(true);
ScreenShotPainter.instance.SetPaintColor(Icon.color);
RefreshAlphaSet();
RefreshPenSize();
2025-02-21 11:49:10 +08:00
}
protected override void OnShow()
{
}
protected override void OnHide()
{
Show3DCamera.instance.lockMove = false;
DrawController.instance.gameObject.SetActive(false);
}
protected override void OnClose()
{
}
}
}