221 lines
6.2 KiB
C#
221 lines
6.2 KiB
C#
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();
|
|
|
|
|
|
|
|
// ¹Ø±Õ°´Å¥¼àÌý
|
|
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 = "ÏÔʾ";
|
|
}
|
|
else
|
|
{
|
|
|
|
Content.GetComponent<DOTweenAnimation>().DOPlayBackwards();
|
|
HideBtn.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = "Òþ²Ø";
|
|
}
|
|
});
|
|
|
|
|
|
foreach (var toggle in Colors.GetComponentsInChildren<Toggle>())
|
|
{
|
|
toggle.onValueChanged.AddListener(isOn =>
|
|
{
|
|
if (isOn)
|
|
{
|
|
ChangeColor(toggle.transform.Find("Color").GetComponent<Image>().color);
|
|
}
|
|
});
|
|
}
|
|
|
|
BackBtn.onClick.AddListener(() =>
|
|
{
|
|
ScreenShotPainter.instance.Undo();
|
|
});
|
|
ClearBtn.onClick.AddListener(() =>
|
|
{
|
|
ScreenShotPainter.instance.Clear();
|
|
});
|
|
|
|
Pen.onValueChanged.AddListener(isOn =>
|
|
{
|
|
if (isOn)
|
|
{
|
|
ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Pen);
|
|
}
|
|
});
|
|
Eraser.onValueChanged.AddListener(isOn =>
|
|
{
|
|
if (isOn)
|
|
{
|
|
ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Eraser);
|
|
}
|
|
});
|
|
Line.onValueChanged.AddListener(isOn =>
|
|
{
|
|
if (isOn)
|
|
{
|
|
ScreenShotPainter.instance.SetStatus(ScreenShotPainter.Status.Line);
|
|
}
|
|
|
|
});
|
|
|
|
|
|
}
|
|
public void ChangeColor(Color color)
|
|
{
|
|
Icon.color = new Color(color.r, color.g, color.b, Icon.color.a);
|
|
ScreenShotPainter.instance.SetPaintColor(Icon.color);
|
|
}
|
|
|
|
public void RefreshPenSize()
|
|
{
|
|
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;//²»Ëõ·ÅyÖáÁË
|
|
Icon.transform.localScale = new Vector3(width / Icon.rectTransform.rect.width, height / Icon.rectTransform.rect.height, 1);
|
|
|
|
|
|
|
|
Penvalue.text = PenSizeSlider.value.ToString();
|
|
}
|
|
|
|
public void RefreshAlphaSet()
|
|
{
|
|
var color = Icon.color;
|
|
color.a = AlphaSlider.value / 100f;
|
|
Icon.color = color;
|
|
ScreenShotPainter.instance.SetPaintColor(Icon.color);
|
|
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();
|
|
}
|
|
|
|
}
|
|
|
|
// ±£´æ»æÖƽá¹ûµÄ·½·¨
|
|
private void SaveDrawing()
|
|
{
|
|
string filePath = Application.dataPath + "/Drawings/" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
|
|
// ´´½¨±£´æÎļþ¼Ð
|
|
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);
|
|
ScreenShotPainter.instance.Init(RawImg, captureBGImg);
|
|
ScreenShotPainter.instance.SwitchOn(true);
|
|
ScreenShotPainter.instance.SetPaintColor(Icon.color);
|
|
RefreshAlphaSet();
|
|
RefreshPenSize();
|
|
}
|
|
|
|
protected override void OnShow()
|
|
{
|
|
}
|
|
|
|
protected override void OnHide()
|
|
{
|
|
Show3DCamera.instance.lockMove = false;
|
|
DrawController.instance.gameObject.SetActive(false);
|
|
}
|
|
|
|
protected override void OnClose()
|
|
{
|
|
}
|
|
}
|
|
} |