MVVM frame work done, under unit test
This commit is contained in:
@@ -43,9 +43,9 @@ namespace StgSharp.Controls
|
||||
{
|
||||
public class BattleField : ControllingItem
|
||||
{
|
||||
public override vec4d[] TextureBox
|
||||
public override ReadOnlySpan<vec4d> TextureBox
|
||||
{
|
||||
get ;
|
||||
get => throw new NotImplementedException() ;
|
||||
}
|
||||
|
||||
public override IEnumerator<PlainGeometryMesh> GetEnumerator()
|
||||
|
||||
@@ -1,4 +1,34 @@
|
||||
using StgSharp.Geometries;
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="ButtonGroup.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Geometries;
|
||||
using StgSharp.Graphics;
|
||||
using StgSharp.Math;
|
||||
|
||||
@@ -6,19 +36,36 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Transactions;
|
||||
|
||||
namespace StgSharp.Controls
|
||||
{
|
||||
public class KeyboardButtonGroup : ControllingItem
|
||||
public abstract class KeyboardButtonGroup : ControllingItem
|
||||
{
|
||||
private List<TriggerButton> buttonList;
|
||||
|
||||
private List<TriggerButton> buttonList;
|
||||
private TriggerButton current;
|
||||
|
||||
public KeyboardButtonGroup()
|
||||
{
|
||||
buttonList = new List<TriggerButton>();
|
||||
buttonList = new List<TriggerButton>();
|
||||
}
|
||||
|
||||
public override ReadOnlySpan<vec4d> TextureBox
|
||||
{
|
||||
get => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public TriggerButton CurrentButton
|
||||
{
|
||||
get => current;
|
||||
}
|
||||
|
||||
protected List<TriggerButton> AllButton
|
||||
{
|
||||
get => buttonList;
|
||||
}
|
||||
|
||||
public void AddButton(TriggerButton button)
|
||||
@@ -26,46 +73,79 @@ namespace StgSharp.Controls
|
||||
buttonList.Add(button);
|
||||
}
|
||||
|
||||
public TriggerButton CurrentButton
|
||||
{
|
||||
get => current;
|
||||
}
|
||||
public override vec4d[] TextureBox
|
||||
{
|
||||
get => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void MoveLeft()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void MoveRight()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void MoveUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void MoveDown()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Confirm()
|
||||
{
|
||||
|
||||
}
|
||||
public abstract void Confirm();
|
||||
|
||||
public override IEnumerator<PlainGeometryMesh> GetEnumerator()
|
||||
{
|
||||
foreach (var item in buttonList)
|
||||
foreach (TriggerButton item in buttonList)
|
||||
{
|
||||
yield return item.Single();
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveDown()
|
||||
{
|
||||
TriggerButton button = current;
|
||||
while ((button.Down != null) && button.Down.IsDisabled)
|
||||
{
|
||||
button = button.Right;
|
||||
}
|
||||
if (current == button)
|
||||
{
|
||||
return;
|
||||
}
|
||||
current.DeHighlight();
|
||||
button.Enable();
|
||||
current = button;
|
||||
}
|
||||
|
||||
public void MoveLeft()
|
||||
{
|
||||
TriggerButton button = current;
|
||||
while ((button.Left != null) && button.Left.IsDisabled)
|
||||
{
|
||||
button = button.Left;
|
||||
}
|
||||
if (current == button)
|
||||
{
|
||||
return;
|
||||
}
|
||||
current.DeHighlight();
|
||||
button.Enable();
|
||||
current = button;
|
||||
}
|
||||
|
||||
public void MoveRight()
|
||||
{
|
||||
TriggerButton button = current;
|
||||
while ((button.Right != null) && button.Right.IsDisabled)
|
||||
{
|
||||
button = button.Right;
|
||||
}
|
||||
if (current == button)
|
||||
{
|
||||
return;
|
||||
}
|
||||
current.DeHighlight();
|
||||
button.Enable();
|
||||
current = button;
|
||||
}
|
||||
|
||||
public void MoveUp()
|
||||
{
|
||||
TriggerButton button = current;
|
||||
while ((button.Up != null) && button.Up.IsDisabled)
|
||||
{
|
||||
button = button.Right;
|
||||
}
|
||||
if (current == button)
|
||||
{
|
||||
return;
|
||||
}
|
||||
current.DeHighlight();
|
||||
button.Enable();
|
||||
current = button;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace StgSharp.Controls
|
||||
Dictionary<char, PlainGeometryMesh> texIndex;
|
||||
PlainGeometryMesh _tittleMesh;
|
||||
|
||||
public override vec4d[] TextureBox
|
||||
public override ReadOnlySpan<vec4d> TextureBox
|
||||
{
|
||||
get => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace StgSharp.Controls
|
||||
public vec2d Position { get; set; }
|
||||
|
||||
public Rectangle BoundingBox { get; set; }
|
||||
public abstract vec4d[] TextureBox { get; }
|
||||
public abstract ReadOnlySpan<vec4d> TextureBox { get; }
|
||||
|
||||
|
||||
public abstract IEnumerator<PlainGeometryMesh> GetEnumerator();
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace StgSharp.Controls
|
||||
{
|
||||
private PlainGeometryMesh renderMesh;
|
||||
|
||||
public override vec4d[] TextureBox
|
||||
public override ReadOnlySpan<vec4d> TextureBox
|
||||
{
|
||||
get => renderMesh.TextureCoord;
|
||||
}
|
||||
@@ -23,9 +23,9 @@ namespace StgSharp.Controls
|
||||
yield return renderMesh;
|
||||
}
|
||||
|
||||
public DecorationBox(Rectangle bound, Image texture, vec4d[] texCoord)
|
||||
public DecorationBox(Rectangle bound, Image texture, vec2d[] texCoord)
|
||||
{
|
||||
renderMesh = new PlainGeometryMesh(BoundingBox, texture, texCoord);
|
||||
renderMesh = new PlainGeometryMesh(BoundingBox, new TextureProvider(texture, texCoord));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace StgSharp.Controls
|
||||
{
|
||||
public class MarkingLabel : ControllingItem
|
||||
{
|
||||
public override vec4d[] TextureBox
|
||||
public override ReadOnlySpan<vec4d> TextureBox
|
||||
{
|
||||
get => marker.TextureCoord;
|
||||
}
|
||||
|
||||
@@ -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 --------------------------------------------
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Geometries;
|
||||
using StgSharp.Graphics;
|
||||
using StgSharp.Graphics.GL;
|
||||
using StgSharp.Graphics.OpenGL;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -103,10 +103,12 @@ namespace StgSharp
|
||||
try
|
||||
{
|
||||
IntPtr procPtr =
|
||||
#if Windows
|
||||
#if WINDOWS
|
||||
IntPtr.Zero;
|
||||
#elif Linux
|
||||
#elif LINUX
|
||||
|
||||
#else
|
||||
IntPtr.Zero;
|
||||
#endif
|
||||
return procPtr;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using StgSharp.Geometries;
|
||||
using StgSharp.Graphics;
|
||||
using StgSharp.Controls;
|
||||
using StgSharp.Controlling.UsrActivity;
|
||||
using StgSharp.Math;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
/// <summary>
|
||||
/// ViewDesigner provides descriptions of various controls in the view,
|
||||
/// binds the controls to specific user actions and behaviors,
|
||||
/// and describes the layout and interactions of each control.
|
||||
/// </summary>
|
||||
public abstract class ViewDesigner<TView> where TView:View
|
||||
{
|
||||
protected TView _binding;
|
||||
|
||||
private KeyboardButtonGroup currentButtonGroup;
|
||||
|
||||
private List<ControllingItem> _controlList;
|
||||
|
||||
public ViewDesigner()
|
||||
{
|
||||
_controlList = new List<ControllingItem>();
|
||||
}
|
||||
|
||||
protected TView Binding
|
||||
{
|
||||
get => _binding;
|
||||
}
|
||||
|
||||
public TButtonGroup CreateKeyboardButtonGroup<TButtonGroup>(
|
||||
ITrigger leftBidning,
|
||||
ITrigger rightBinding,
|
||||
ITrigger upBinding,
|
||||
ITrigger downBinding,
|
||||
ITrigger confirmBinding
|
||||
)
|
||||
where TButtonGroup : KeyboardButtonGroup,new()
|
||||
{
|
||||
var ret = new TButtonGroup();
|
||||
_controlList.Add(ret);
|
||||
currentButtonGroup = ret;
|
||||
_binding.Responder[leftBidning] = ret.MoveLeft;
|
||||
_binding.Responder[rightBinding] = ret.MoveRight;
|
||||
_binding.Responder[upBinding] = ret.MoveUp;
|
||||
_binding.Responder[downBinding] = ret.MoveDown;
|
||||
_binding.Responder[confirmBinding] = ret.Confirm;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public TriggerButton CreateKeyboardButton(
|
||||
Rectangle bonding, TextureProvider defaultAnimation, bool isEnable
|
||||
)
|
||||
{
|
||||
var ret = new TriggerButton();
|
||||
ret.BoundingBox = bonding;
|
||||
if (isEnable)
|
||||
{
|
||||
ret.IsDisabled = false;
|
||||
ret.Enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.IsDisabled = true;
|
||||
ret.Disable();
|
||||
}
|
||||
currentButtonGroup.AddButton(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public CharacterLineIndexImage CreateCharacterLine(
|
||||
int length, char blankFilling, PlainGeometryMesh tittle, IEnumerable<(char,PlainGeometryMesh)> charList
|
||||
)
|
||||
{
|
||||
var ret = new CharacterLineIndexImage(length, blankFilling,tittle,charList);
|
||||
_controlList.Add(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public abstract void CustomizedInitialize();
|
||||
|
||||
public abstract bool ValidateInitializing();
|
||||
|
||||
}//---------------------------------- End of Class -----------------------------------------
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using StgSharp.Controlling.UsrActivity;
|
||||
using StgSharp.Graphics;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public abstract class ViewResponder<TView> where TView: View
|
||||
{
|
||||
private View _viewbinding;
|
||||
private Dictionary<ITrigger, Action> keyCallbackIndex;
|
||||
private Dictionary<string, Action> nameToCallbackMap;
|
||||
private bool _isInitialized;
|
||||
|
||||
|
||||
protected ViewResponder()
|
||||
{
|
||||
keyCallbackIndex = new Dictionary<ITrigger, Action>();
|
||||
nameToCallbackMap = new Dictionary<string, Action>();
|
||||
}
|
||||
|
||||
internal void ProcessUserInput()
|
||||
{
|
||||
foreach (var pair in keyCallbackIndex)
|
||||
{
|
||||
ITrigger t = pair.Key;
|
||||
if (InternalIO.glfwGetKey(_viewbinding.ViewHandle, t.TargetKeyOrButtonID) == t.TriggeredStatus)
|
||||
{
|
||||
pair.Value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Action this[ITrigger trigger]
|
||||
{
|
||||
get => keyCallbackIndex.TryGetValue(trigger, out Action callback) ? callback : null;
|
||||
set
|
||||
{
|
||||
if (keyCallbackIndex.TryGetValue(trigger, out Action callback))
|
||||
{
|
||||
callback += value;
|
||||
}
|
||||
else
|
||||
{
|
||||
keyCallbackIndex.Add(trigger, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void Init(Func<string, Action> keyGetter)
|
||||
{
|
||||
if (_isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
CustomizedInitialize(
|
||||
(trigger, name) =>
|
||||
{
|
||||
if (!nameToCallbackMap.TryGetValue(name, out Action ret))
|
||||
{
|
||||
ret = keyGetter.Invoke(name)??
|
||||
throw new InvalidOperationException($"Cannot find method named {name} in binded view model");
|
||||
}
|
||||
this[trigger] = ret;
|
||||
return ret;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void ProcessInput()
|
||||
{
|
||||
foreach (var pair in keyCallbackIndex)
|
||||
{
|
||||
if (InternalIO.glfwGetKey(_viewbinding.ViewHandle,(int)pair.Key.TargetKeyOrButtonID) == pair.Key.TriggeredStatus)
|
||||
{
|
||||
pair.Value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void CustomizedInitialize(Func<ITrigger, string, Action> keyRegister);
|
||||
|
||||
public abstract bool ValidateInitializing();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user