MVVM rebuild complete

This commit is contained in:
Nitload
2024-07-30 09:37:25 +08:00
parent da75200347
commit d082071f2d
68 changed files with 2690 additions and 762 deletions
+6 -1
View File
@@ -4,6 +4,7 @@ using StgSharp.Geometries;
using StgSharp.Graphics;
using StgSharp.Logic;
using StgSharp.Math;
using StgSharp.MVVM;
using StgSharpDebug.Logic;
@@ -27,8 +28,12 @@ namespace StgSharp
StgSharp.InitGL(4, 6);
StgSharp.GlobalSetting.VSyncActivated = false;
ViewPort vp = new ViewPort();
ViewModelMain mainModel = new ViewModelMain(vp);
mainModel.CreateAndBindView<ViewMain>("MainView", (1, 1, 1));
mainModel.UseViewModel("MainView");
StgSharp.Terminate();
/**/
@@ -36,10 +41,10 @@ namespace StgSharp
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
InternalIO.InternalWriteLog(
ex.Message + "\n" + ex.StackTrace,
LogType.Error);
Environment.FailFast(ex.Message + "\n" + ex.StackTrace);
}
}
}
@@ -1,4 +1,34 @@
using StgSharp.Controls;
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Partical.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.Controls;
using StgSharp.Geometries;
using StgSharp.Graphics;
using StgSharp.Graphics.OpenGL;
@@ -17,40 +47,91 @@ namespace StgSharp
{
ElementBuffer ebo;
ElementBuffer f_ebo;
FrameBuffer fbo;
Image i;
PlainInstancingBuffer<ParticleOnPlain> buffer;
RenderBuffer rbo;
ShaderProgram frameMainShader;
ShaderProgram s;
TextureGL t;
Uniform<int> texUniform;
Uniform<float> time;
VertexArray f_vao;
VertexArray vao;
VertexBuffer f_vbo;
VertexBuffer vbo;
Uniform<float> time;
public ParticleRenderStream()
{
}
public override bool IsContextSharable => true;
public Rectangle ButtonBounding
{
get;
private set;
}
public TextureProvider ButtonTextureProvider { get; private set; }
//render a controlling item
public void AnotherMain(IEnumerable<PlainGeometryMesh> items)
{
//Console.WriteLine("Render Button Itself");
GL.BindFrameBuffer(FrameBufferTarget.All, GlHandle.Zero); //GL.Assert(true);
GL.ClearColor(0.4f, TimePassed % 1.0f, 0f, 1.0f); //GL.Assert(true);
GL.Clear(MaskBufferBit.COLOR /**/| MaskBufferBit.DEPTH/**/); //GL.Assert(true);
f_vao.Bind(0); //GL.Assert(true);
foreach (PlainGeometryMesh mesh in items)
{
f_t.LoadTexture(0, mesh.TextureImage);
}
//GL.Assert(true);
frameMainShader.Use(); //GL.Assert(true);
GL.DrawElements(GeometryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero);
//GL.Assert(true);
//GL.DrawElements(GeometryType.LINES, 6, TypeCode.UInt32, IntPtr.Zero);
//BlueprintPipeline.SkipAll(output);
SwapBuffers();
}
public void RenderButton(ButtonStatus status, float statusBegin, in TextureProvider provider)
{
provider.UpdateFromImage(i);
}
internal static vec3d DefaultRotation(TimeSpanProvider provider, out float rotation)
{
rotation = provider.SpanLengthLowPrecession;
float angle = provider.CurrentSecond * 2;
return (new vec3d(
Scaler.Cos(angle),
Scaler.Sin(angle),
0
)) * provider.SpanLengthLowPrecession;
}
protected override void CustomizeDeinit()
{
}
internal static vec3d DefaultRotation(TimeSpanProvider provider, out float rotation)
{
rotation = provider.SpanLengthLowPrecession * 2 * Scaler.Pi;
float angle = provider.CurrentSecond * 2 * Scaler.Pi;
return new vec3d(
Scaler.Cos(rotation),
Scaler.Sin(rotation),
0
) * provider.SpanLengthLowPrecession;
}
ShaderProgram frameMainShader;
protected override void CustomizeInit()
{
//GL.ClearError();
//GL.Assert(true);
buffer = new PlainInstancingBuffer<ParticleOnPlain>();
for (int i = 0; i < 10; i++)
{
@@ -65,12 +146,14 @@ namespace StgSharp
((IInstancing)p).Scale =
Scaler.MinOf(i, t) * 0.15f;
((IInstancing)p).Motion = new GeometryMotion(
true, this.timeProvider,
true, this._timeProvider,
DefaultRotation)
;
}
}
//GL.Assert(true);
//SetSizeLimit(800, 800, 800, 800);
s = CreateShaderProgram();
@@ -78,11 +161,15 @@ namespace StgSharp
Shader pvs = CreateShaderSegment(ShaderType.Vertex, 2);
Shader pfs = CreateShaderSegment(ShaderType.Fragment, 2);
//GL.Assert(true);
pvs.Compile(0, Shader.FromCodeFile("Shader\\particle.vert"));
//GL.Assert(true);
pvs.AttachTo(0, s);
//GL.Assert(true);
pfs.Compile(0, Shader.FromCodeFile("Shader\\particle.frag"));
pfs.AttachTo(0, s);
s.Link();
//GL.Assert(true);
pvs.Compile(1, Shader.FromCodeFile("Shader\\fbo.vert"));
pvs.AttachTo(1, frameMainShader);
@@ -90,6 +177,7 @@ namespace StgSharp
pfs.AttachTo(1, frameMainShader);
frameMainShader.Link();
//GL.Assert(true);
vbo = CreateVertexBuffer(3);
vao = CreateVertexArray(1);
ebo = CreateElementBuffer(1);
@@ -103,6 +191,7 @@ namespace StgSharp
-0.05f, 0.05f
);
//GL.Assert(true);
vao.Bind(0);
vbo.WriteVectorData(0, rec.VertexStream, BufferUsage.StaticDraw);
ebo.SetValue(0, rec.VertexIndices, BufferUsage.StaticDraw);
@@ -141,97 +230,111 @@ namespace StgSharp
GL.SetRenderBufferStorage(
RenderBufferInternalFormat.Depth24_Stencil8, this.Size);
//GL.Assert(true);
t = CreateTexture(1);
t.Bind2D(0);
/**/
GL.TextureImage2d<byte>(Texture2DTarget.Texture2D, 0, (int)ImageChannel.WithAlphaChannel,
(uint)Width, (uint)Height, (int)ImageChannel.WithAlphaChannel, PixelChannelLayout.Byte, null);
/**/
t.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat);
t.Set2dFilterProperty(Filter.Nearest, Filter.Nearest);
//GL.Assert(true);
GL.FrameBufferTexture2d(FrameBufferTarget.All, glAttachment.Color(0),
Texture2DTarget.Texture2D, t[0], 0);
GL.CombineFrameBufferRenderBuffer(fbo[0], rbo[0]);
//GL.Assert(true);
GL.CombineFrameBufferRenderBuffer(FrameBufferAttachment.Stencil|FrameBufferAttachment.Depth, rbo[0]);
Console.WriteLine(GL.CheckFrameBufferStatus(FrameBufferTarget.All));
FrameBuffer.BindNull();
//GL.Assert(true);
int streamLength = this.Width * this.Height * 4;
byte[] imageStream = new byte[streamLength];
i = Image.FromMemory(this.Size, ImageChannel.WithAlphaChannel, imageStream);
time = s.GetUniform<float>("time");
vec2d[] coordArray = {
vec2d[] buttonTextureCoordArray = {
(0f, 1f),
(1f, 1f),
(1f, 0f),
(0f, 0f)
};
ButtonBounding = new Rectangle(
-0.9f, 0.9f,
0.9f, 0.9f,
0.9f, -0.9f,
-0.9f, -0.9f
);
buttonTextureProvider = new TextureProvider(i,coordArray);
//GL.Assert(true);
ButtonTextureProvider = new TextureProvider(i, buttonTextureCoordArray);
f_vao = CreateVertexArray(1);
f_vbo = CreateVertexBuffer(2);
f_ebo = CreateElementBuffer(1);
//GL.Assert(true);
f_vao.Bind(0);
f_ebo.Bind(0);
f_ebo.SetValue(0, rec.VertexIndices, BufferUsage.StaticDraw);
f_ebo.SetValue(0, ButtonBounding.VertexIndices, BufferUsage.StaticDraw);
//GL.Assert(true);
f_vbo.Bind(0);
f_vbo.WriteVectorData(0, rec.VertexStream, BufferUsage.StaticDraw);
f_vbo.WriteVectorData(0, ButtonBounding.VertexStream, BufferUsage.StaticDraw);
f_vao.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
//GL.Assert(true);
f_vbo.Bind(1);
f_vbo.WriteVectorData(1, coordArray, BufferUsage.StaticDraw);
f_vbo.WriteVectorData(1, buttonTextureCoordArray, BufferUsage.StaticDraw);
f_vao.SetVertexAttribute(1, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
//GL.Assert(true);
f_t = CreateTexture(1);
f_t.Bind2D(0);
f_t.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat);
f_t.Set2dFilterProperty(Filter.Nearest, Filter.Nearest);
t = CreateTexture(1);
t.Bind2D(0);
t.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat);
t.Set2dFilterProperty(Filter.Nearest, Filter.Nearest);
t.ActivateAs(TextureUnit.Unit0);
//GL.Assert(true);
f_t.ActivateAs(TextureUnit.Unit1);
//GL.Assert(true);
frameMainShader.Use();
texUniform = frameMainShader.GetUniform<int>("tex");
//GL.Assert(true);
GL.SetUniformValue(texUniform, 1);
//GL.Assert(true);
}
VertexBuffer f_vbo;
VertexArray f_vao;
ElementBuffer f_ebo;
Uniform<int> texUniform;
public void RenderButton(ButtonStatus status, float statusBegin, in TextureProvider provider)
{
provider.ReadImage(i);
}
TextureGL f_t;
protected override void Main()
{
//GL.ClearError();
//Console.Clear();
//Console.WriteLine($"{Width} {Height}");
buffer.UpdateAllParticles();
//Console.WriteLine(GL.CheckFrameBufferStatus(FrameBufferTarget.All));
fbo.Bind(0); // Console.Write($"{GL.GetError()}\t");
vao.Bind(0); // Console.Write($"{GL.GetError()}\t");
t.Bind2D(0); // Console.Write($"{GL.GetError()}\t");
fbo.Bind(0); //GL.Assert(true);
vao.Bind(0); //GL.Assert(true);
t.Bind2D(0); //GL.Assert(true);
/**/
GL.TextureImage2d(Texture2DTarget.Texture2D, 0, (int)ImageChannel.WithAlphaChannel,
(uint)Width, (uint)Height, (int)ImageChannel.WithAlphaChannel, PixelChannelLayout.Byte, new byte[1]);
/**/
//Console.Write($"{GL.GetError()}\t");
//GL.Assert(true);
GL.ClearColor((0.6f + TimePassed) % 1.0f, 0.1f, 0.3f, 1);
GL.Clear(MaskBufferBit.COLOR | MaskBufferBit.DEPTH);
//Console.Write($"{GL.GetError()}\t");
//GL.Assert(true);
vbo.Bind(1);
vbo.WriteVectorData<vec4d>(1, buffer.CoordAndRotationSpan, BufferUsage.StreamDraw);
@@ -245,14 +348,14 @@ namespace StgSharp
GeometryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero, 100
);
//Console.WriteLine(TimePassed);
//Console.Write($"{GL.GetError()}\t");
//GL.Assert(true);
GL.WaitAccomplishment();
GL.BindVertexArray(GlHandle.Zero);
/**/
//Console.Write($"{GL.GetError()}\t");
//GL.Assert(true);
ImageHelper.Resize(i, this.Width, this.Height, ImageMovement.Discard);
ImageHelper.Resize(i, Width, Height, ImageMovement.Discard);
//GL.ClearError();
@@ -264,12 +367,12 @@ namespace StgSharp
Console.Write($"{i.PixelBuffer[num]}\t");
}
/**/
//Console.Write($"{GL.GetError()}\t");
//GL.Assert(true);
//Array.Fill<byte>(i.PixelBuffer, 24);
GL.BindFrameBuffer(FrameBufferTarget.All, GlHandle.Zero);
//Console.Write($"{GL.GetError()}\t");
//GL.Assert(true);
/**/
/*
@@ -279,34 +382,5 @@ namespace StgSharp
/**/
}
public void AnotherMain(IEnumerable<PlainGeometryMesh> items)
{
GL.BindFrameBuffer(FrameBufferTarget.All, GlHandle.Zero); //Console.Write($"{GL.GetError()}\t");
GL.ClearColor(0.4f, TimePassed % 1.0f, 0f, 1.0f); //Console.Write($"{GL.GetError()}\t");
GL.Clear(MaskBufferBit.COLOR /**/| MaskBufferBit.DEPTH/**/); //Console.Write($"{GL.GetError()}\t");
vao.Bind(0); //Console.Write($"{GL.GetError()}\t");
foreach (PlainGeometryMesh mesh in items)
{
t.LoadTexture(0, mesh.TextureImage);
}
//Console.Write($"{GL.GetError()}\t");
s.Use(); //Console.Write($"{GL.GetError()}\t");
GL.DrawElements(GeometryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero);
//Console.Write($"{GL.GetError()}\t");
//GL.DrawElements(GeometryType.LINES, 6, TypeCode.UInt32, IntPtr.Zero);
//BlueprintPipeline.SkipAll(output);
SwapBuffers();
}
TextureProvider buttonTextureProvider;
public override bool IsContextSharable => true;
}
}
+17 -10
View File
@@ -1,4 +1,5 @@
using StgSharp.Controlling.UsrActivity;
using StgSharp.MVVM.View;
using StgSharp.Controlling.UsrActivity;
using System;
using System.Collections.Generic;
@@ -7,19 +8,25 @@ using System.Text;
using System.Threading.Tasks;
namespace StgSharp
{
public class Control : ViewResponder<ViewMain>
{
public partial class ViewMain
{
public override void CustomizedInitialize(Func<ITrigger, string, Action> keyRegister)
protected class Control : ViewResponder<ViewMain>
{
public Control(ViewMain view):base(view)
{
}
}
public override void CustomizedInitialize(Func<ITrigger, string, Action> keyRegister)
{
}
public override bool ValidateInitializing()
{
throw new NotImplementedException();
public override bool ValidateInitializing()
{
return true;
}
}
}
}
+33 -9
View File
@@ -1,5 +1,6 @@
using StgSharp.Graphics;
using StgSharp.Controlling;
using StgSharp.Controlling.UsrActivity;
using System;
using System.Collections.Generic;
@@ -7,21 +8,45 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StgSharp.Controls;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;
using StgSharp.Geometries;
namespace StgSharp
{
internal class Design : ViewDesigner<ViewMain>
public partial class ViewMain
{
DemoButtonGroup group;
internal DemoButtonGroup Group { get; set; }
internal TriggerButton OnlyButton { get; set; }
public override sealed void CustomizedInitialize()
internal class Design : ViewDesigner<ViewMain>
{
group = new DemoButtonGroup();
}
public Design(ViewMain view):base(view)
{
}
public override bool ValidateInitializing()
{
return true;
public override sealed void CustomizedInitialize()
{
Binding.Group = CreateKeyboardButtonGroup<DemoButtonGroup>(
new KeyTrigger(KeyboardKey.LEFT,KeyStatus.PRESS),
new KeyTrigger(KeyboardKey.RIGHT, KeyStatus.PRESS),
new KeyTrigger(KeyboardKey.UP, KeyStatus.PRESS),
new KeyTrigger(KeyboardKey.DOWN, KeyStatus.PRESS),
new KeyTrigger(KeyboardKey.ENTER, KeyStatus.PRESS)
);
Binding.OnlyButton = CreateKeyboardButton(
Binding.Stream.ButtonBounding,
Binding.Stream.ButtonTextureProvider,
true
);
Binding.OnlyButton.RenderButtonTexture += Binding.Stream.RenderButton;
}
public override bool ValidateInitializing()
{
return true;
}
}
}
@@ -31,6 +56,5 @@ namespace StgSharp
{
return;
}
}
}
@@ -0,0 +1,34 @@
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp
{
public partial class ViewMain
{
public ParticleRenderStream Stream { get; set; }
private class NewRender : ViewRender<ViewMain>
{
public NewRender(ViewMain view):base(view)
{
}
public override void CustomizedInitialize()
{
Binding.Stream = CreateRenderStream<ParticleRenderStream>((1,1,1));
}
public override bool ValidateInitializing()
{
return Binding.Stream != null;
}
}
}
}
+34 -12
View File
@@ -1,7 +1,8 @@
using StgSharp.Graphics;
using StgSharp.Logic;
using StgSharpDebug.Logic;
using StgSharp.MVVM;
using StgSharp.MVVM.View;
using StgSharp.MVVM.ViewModel;
using System;
using System.Collections.Generic;
@@ -11,30 +12,51 @@ using System.Threading.Tasks;
namespace StgSharp
{
public class ViewMain : View
public partial class ViewMain : ViewBase
{
public DemoButtonGroup DemoGroup
public ViewMain()
{
get; set;
_databindingIndexMap = new Dictionary<string, DataBindingEntry>();
}
Dictionary<string, DataBindingEntry> _databindingIndexMap;
public override void CustomizedInitialize(ViewModelBase viewModelBinding)
{
Designer = new Design();
Responder = new Control();
Render = new Render();
Designer = new Design(this);
Responder = new Control(this);
Render = new NewRender(this);
DemoGroup = new DemoButtonGroup();
Render.Initialize();
Designer.Initialize();
Responder.Initialize(viewModelBinding.MethodLookup);
}
public override bool TryGetObjectReference(string name, out DataBindingEntry entry)
{
throw new NotImplementedException();
if (_databindingIndexMap.TryGetValue(name,out entry))
{
return true;
}
try
{
entry = name switch
{
_ => default(DataBindingEntry),
};
return !entry.ExactEqual(default(DataBindingEntry));
}
catch (Exception)
{
entry = default(DataBindingEntry);
return false;
}
}
internal protected override void Show()
protected internal override void Show()
{
throw new NotImplementedException();
Stream.Draw();
Stream.AnotherMain(OnlyButton);
}
}
}
@@ -1,5 +1,7 @@
using StgSharp.Graphics;
using StgSharp.Logic;
using StgSharp.MVVM.ViewModel;
using StgSharp.MVVM;
using StgSharpDebug.Logic;