MVVM frame work done, under unit test

This commit is contained in:
Nitload
2024-07-25 04:41:24 +08:00
parent 924fe6eef7
commit da75200347
101 changed files with 8435 additions and 847 deletions
@@ -35,81 +35,148 @@ using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Controls
{
public enum ButtonStatus
{
Error = 0b0,
Disable = 0b1,
Enable = 0b10,
Highlight = 0b100,
Confirm = 0b1000,
DisableToEnable = Disable | Enable,
EnableToDisable = -(Enable | DisableToEnable),
EnableToHighlight = Enable | Highlight,
HighlightToEnable = -(Enable | Highlight),
HighlightToDisable = -(Highlight | Disable),
}
public delegate void RenderButtonTextureCallback(ButtonStatus status, float beginTime, in TextureProvider texture);
public class TriggerButton : ControllingItem
{
private AnimationBuilder switchAnimationBuilder;
private TextureProvider _texture;
private float _statusBeginTime;
public TriggerButton Left { get; internal set; }
public TriggerButton Right { get; internal set; }
public TriggerButton Up { get; internal set; }
public TriggerButton Down { get; internal set; }
#nullable enable
private Action? enableAction;
private Action? disableAction;
private Action? confirmAction;
private Action? enableCallback;
private Action? disableCallback;
private Action? confirmCallback;
#nullable restore
public override vec4d[] TextureBox
protected ButtonStatus CurrentStatus { get; set; }
private RenderButtonTextureCallback _renderButton;
public event RenderButtonTextureCallback RenderButtonTexture
{
get { return switchAnimationBuilder.TextureFrame.VertexStream; }
add
{
if (_renderButton == null)
{
_renderButton = value;
}
else
{
_renderButton += value;
}
}
remove
{
if (_renderButton != null)
{
_renderButton -= value;
}
}
}
public override ReadOnlySpan<vec4d> TextureBox
{
get => MemoryMarshal.Cast<vec2d, vec4d>(_texture.TextureCoordinate);
}
public bool IsHighLighted { get; internal set; }
public bool IsDisabled { get; internal set; }
public event Action EnableEvent
{
add { enableAction += value; }
remove { enableAction -= value; }
add { enableCallback += value; }
remove { enableCallback -= value; }
}
public event Action DisableEvent
{
add { disableAction += value; }
remove { disableAction -= value; }
add { disableCallback += value; }
remove { disableCallback -= value; }
}
public event Action ConfirmEvent
{
add { confirmAction += value; }
remove { confirmAction -= value; }
add { confirmCallback += value; }
remove { confirmCallback -= value; }
}
public void Enable(AnimationBuilder animation)
public void Enable()
{
if (!IsDisabled)
if (CurrentStatus == ButtonStatus.Disable)
{
CurrentStatus = ButtonStatus.DisableToEnable;
}
else
{
return;
}
if (switchAnimationBuilder == null || switchAnimationBuilder.IsComplete)
{
switchAnimationBuilder = animation;
}
if( enableAction != null)enableAction();
if ( enableCallback != null)enableCallback();
}
public void Disable(AnimationBuilder animation)
public void Disable()
{
if (switchAnimationBuilder == null || switchAnimationBuilder.IsComplete)
CurrentStatus = CurrentStatus switch
{
switchAnimationBuilder = animation;
}
if (disableAction != null) disableAction();
ButtonStatus.Enable => ButtonStatus.EnableToDisable,
ButtonStatus.Highlight => ButtonStatus.HighlightToDisable,
_ => CurrentStatus,
};
if (disableCallback != null) disableCallback();
}
public void Highlight(AnimationBuilder animation)
public void Highlight()
{
if (switchAnimationBuilder == null || switchAnimationBuilder.IsComplete)
if (CurrentStatus == ButtonStatus.Enable)
{
switchAnimationBuilder = animation;
CurrentStatus |= ButtonStatus.EnableToHighlight;
}
else
{
return;
}
if (confirmCallback != null) confirmCallback();
}
public void DeHighlight()
{
if (CurrentStatus == ButtonStatus.Highlight)
{
CurrentStatus = ButtonStatus.HighlightToEnable;
}
if (confirmAction != null) confirmAction();
}
public override IEnumerator<PlainGeometryMesh> GetEnumerator()
{
yield return new PlainGeometryMesh(this.BoundingBox, switchAnimationBuilder.GetAnimationFrame(),
this.TextureBox);
_renderButton(CurrentStatus,_statusBeginTime, in _texture);
yield return new PlainGeometryMesh(this.BoundingBox, _texture);
}
}//----------------------------------- End of class --------------------------------------------