Files
stgsharp/StgSharp.Common/MVVM/ViewPort.cs
T

142 lines
4.7 KiB
C#

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ViewPort.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 System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace StgSharp.MVVM
{
public class ViewPort
{
private static ConcurrentDictionary<IntPtr, ViewPort> _handleToViewPortIndex = new ConcurrentDictionary<IntPtr, ViewPort>(
);
private int _shouldCloseMask, _closeRequestedMask;
private int _width, _height, _newWidth, _newHeight;
internal ViewPort()
{
_newHeight = 600;
_newWidth = 800;
FlushSize();
Name = new StackFrame( 1, false ).
GetMethod()!.DeclaringType!.Name;
Monitor = IntPtr.Zero;
ViewPortID = InternalIO.glfwCreateWindow(
_newWidth, _newHeight, Encoding.UTF8.GetBytes( Name ), Monitor,
IntPtr.Zero );
if( ViewPortID == IntPtr.Zero ) {
throw new InvalidOperationException(
"Unable to create viewport handle." );
}
_handleToViewPortIndex.TryAdd( ViewPortID, this );
}
public ViewPort(int width, int height, string name, IntPtr monitor )
{
_newWidth = width;
_newHeight = height;
FlushSize();
Name = name;
Monitor = monitor;
ViewPortID = InternalIO.glfwCreateWindow(
_newWidth, _newHeight, Encoding.UTF8.GetBytes( Name ), Monitor,
IntPtr.Zero );
if( ViewPortID == IntPtr.Zero ) {
throw new InvalidOperationException(
"Unable to create viewport handle." );
}
_handleToViewPortIndex.TryAdd( ViewPortID, this );
}
public unsafe int Height
{
get { return _height; }
internal set { Interlocked.Exchange( ref _height, value ); }
}
public unsafe int Width
{
get { return _width; }
internal set { Interlocked .Exchange( ref _width, value ); }
}
public IntPtr GraphicHandle
{
get;
internal set;
}
public IntPtr Monitor
{
get;
internal set;
}
public IntPtr ViewPortID
{
get;
private set;
}
public string Name
{
get;
set;
}
//[BlueprintNodeExecution]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static bool GetExistedViewPort(
IntPtr handle,
out ViewPort port )
{
return _handleToViewPortIndex.TryGetValue( handle, out port );
}
internal unsafe void FlushSize()
{
Interlocked.Exchange( ref _height, _newHeight );
Interlocked.Exchange( ref _width, _newWidth );
}
internal void RequestFlushSizeInNextFrame( int width, int height )
{
Interlocked.Exchange( ref _newHeight, height );
Interlocked .Exchange( ref _newWidth, width );
}
}
}