2019-08-27 22:38:06 +02:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Drawing ;
using System.Data ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
2019-08-28 01:03:01 +02:00
using OpenTK.Graphics.OpenGL ;
2019-08-29 21:45:32 +02:00
using OpenTK ;
2019-08-28 01:03:01 +02:00
using Toolbox.Library ;
using Toolbox.Library.Rendering ;
2019-08-29 21:45:32 +02:00
using Toolbox.Library.IO ;
2019-09-01 19:02:48 +02:00
using LayoutBXLYT.Cafe ;
2019-08-27 22:38:06 +02:00
2019-08-29 22:33:23 +02:00
namespace LayoutBXLYT
2019-08-27 22:38:06 +02:00
{
2019-08-30 01:01:47 +02:00
public partial class LayoutViewer : LayoutDocked
2019-08-27 22:38:06 +02:00
{
2019-08-29 22:33:23 +02:00
public List < BasePane > SelectedPanes = new List < BasePane > ( ) ;
2019-08-29 21:45:32 +02:00
public Camera2D Camera = new Camera2D ( ) ;
public class Camera2D
{
public float Zoom = 1 ;
public Vector2 Position ;
}
2019-08-28 01:03:01 +02:00
private RenderableTex backgroundTex ;
2019-08-29 23:17:24 +02:00
public BxlytHeader LayoutFile ;
2019-08-31 00:53:45 +02:00
private Dictionary < string , STGenericTexture > Textures ;
2019-08-29 21:45:32 +02:00
2019-09-08 21:15:42 +02:00
private void glControl1_Load ( object sender , EventArgs e )
{
}
2019-09-07 01:12:39 +02:00
public void ResetCamera ( )
{
Camera = new Camera2D ( ) ;
}
2019-09-04 20:10:17 +02:00
public void ResetLayout ( BxlytHeader bxlyt )
{
LayoutFile = bxlyt ;
UpdateViewport ( ) ;
}
2019-08-31 00:53:45 +02:00
public LayoutViewer ( BxlytHeader bxlyt , Dictionary < string , STGenericTexture > textures )
2019-08-27 22:38:06 +02:00
{
InitializeComponent ( ) ;
2019-08-31 00:53:45 +02:00
LayoutFile = bxlyt ;
Text = bxlyt . FileName ;
2019-08-29 21:45:32 +02:00
2019-08-31 00:53:45 +02:00
Textures = textures ;
if ( bxlyt . Textures . Count > 0 )
{
if ( bxlyt . FileInfo is BFLYT )
Textures = ( ( BFLYT ) bxlyt . FileInfo ) . GetTextures ( ) ;
else if ( bxlyt . FileInfo is BCLYT )
Textures = ( ( BCLYT ) bxlyt . FileInfo ) . GetTextures ( ) ;
}
2019-08-30 01:01:47 +02:00
}
protected override void OnFormClosing ( FormClosingEventArgs e )
{
2019-09-01 19:29:24 +02:00
if ( LayoutEditor . IsSaving )
{
base . OnFormClosing ( e ) ;
return ;
}
2019-08-30 01:01:47 +02:00
var result = MessageBox . Show ( "Are you sure you want to close this file? You will lose any unsaved progress!" , "Layout Editor" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
if ( result ! = DialogResult . Yes )
e . Cancel = true ;
else
LayoutFile . Dispose ( ) ;
base . OnFormClosing ( e ) ;
2019-08-29 21:45:32 +02:00
}
public void UpdateViewport ( )
{
glControl1 . Invalidate ( ) ;
2019-08-27 22:38:06 +02:00
}
2019-08-28 01:03:01 +02:00
private void glControl1_Paint ( object sender , PaintEventArgs e )
{
if ( ! Runtime . OpenTKInitialized )
return ;
glControl1 . Context . MakeCurrent ( glControl1 . WindowInfo ) ;
OnRender ( ) ;
}
2019-09-07 01:12:39 +02:00
public bool UseOrtho = true ;
2019-08-30 02:51:41 +02:00
private Color BackgroundColor = > Runtime . LayoutEditor . BackgroundColor ;
2019-08-28 01:03:01 +02:00
private void OnRender ( )
{
2019-08-29 21:45:32 +02:00
if ( LayoutFile = = null ) return ;
2019-08-28 01:03:01 +02:00
GL . Viewport ( 0 , 0 , glControl1 . Width , glControl1 . Height ) ;
GL . MatrixMode ( MatrixMode . Projection ) ;
GL . LoadIdentity ( ) ;
2019-09-07 01:12:39 +02:00
if ( UseOrtho )
{
GL . Ortho ( - ( glControl1 . Width / 2.0f ) , glControl1 . Width / 2.0f , glControl1 . Height / 2.0f , - ( glControl1 . Height / 2.0f ) , - 10000 , 10000 ) ;
GL . MatrixMode ( MatrixMode . Modelview ) ;
GL . LoadIdentity ( ) ;
}
else
{
var cameraPosition = new Vector3 ( Camera . Position . X , Camera . Position . Y , - ( Camera . Zoom * 500 ) ) ;
var perspectiveMatrix = Matrix4 . CreateTranslation ( cameraPosition ) * Matrix4 . CreatePerspectiveFieldOfView ( 1.3f , glControl1 . Width / ( float ) glControl1 . Height , 0.01f , 100000 ) ;
GL . LoadMatrix ( ref perspectiveMatrix ) ;
GL . MatrixMode ( MatrixMode . Modelview ) ;
}
2019-08-28 01:03:01 +02:00
2019-08-29 21:45:32 +02:00
GL . ClearColor ( BackgroundColor ) ;
2019-08-28 01:03:01 +02:00
GL . Clear ( ClearBufferMask . ColorBufferBit | ClearBufferMask . DepthBufferBit ) ;
2019-09-08 21:15:42 +02:00
// GL.Disable(EnableCap.CullFace);
2019-08-29 21:45:32 +02:00
GL . Enable ( EnableCap . Blend ) ;
2019-09-08 21:15:42 +02:00
GL . Enable ( EnableCap . AlphaTest ) ;
GL . AlphaFunc ( AlphaFunction . Always , 0f ) ;
2019-08-29 21:45:32 +02:00
GL . BlendFunc ( BlendingFactor . SrcAlpha , BlendingFactor . OneMinusSrcAlpha ) ;
2019-09-03 01:48:47 +02:00
GL . Enable ( EnableCap . ColorMaterial ) ;
2019-08-31 03:53:00 +02:00
GL . Enable ( EnableCap . Texture2D ) ;
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
2019-09-07 01:12:39 +02:00
if ( UseOrtho )
{
GL . PushMatrix ( ) ;
GL . Scale ( 1 * Camera . Zoom , - 1 * Camera . Zoom , 1 ) ;
GL . Translate ( Camera . Position . X , Camera . Position . Y , 0 ) ;
}
2019-08-29 21:45:32 +02:00
DrawRootPane ( LayoutFile . RootPane ) ;
DrawGrid ( ) ;
DrawXyLines ( ) ;
2019-09-08 21:15:42 +02:00
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
RenderPanes ( LayoutFile . RootPane , true , 255 , false , null , 0 ) ;
2019-08-29 21:45:32 +02:00
2019-09-07 01:12:39 +02:00
if ( UseOrtho )
GL . PopMatrix ( ) ;
2019-08-28 01:03:01 +02:00
2019-09-08 21:15:42 +02:00
GL . UseProgram ( 0 ) ;
2019-08-28 01:03:01 +02:00
glControl1 . SwapBuffers ( ) ;
}
2019-09-08 21:15:42 +02:00
private void RenderPanes ( BasePane pane , bool isRoot , byte parentAlpha , bool parentAlphaInfluence , BasePane partPane = null , int stage = 0 )
2019-08-28 01:03:01 +02:00
{
2019-08-29 21:45:32 +02:00
if ( ! pane . DisplayInEditor )
return ;
2019-08-28 01:03:01 +02:00
2019-08-29 21:45:32 +02:00
GL . PushMatrix ( ) ;
2019-08-28 01:03:01 +02:00
2019-09-07 01:12:39 +02:00
//Check XY rotation and draw the pane before it was rotated
bool isRotatedXY = pane . Rotate . X ! = 0 | | pane . Rotate . Y ! = 0 ;
if ( isRotatedXY & & SelectedPanes . Contains ( pane ) )
{
GL . PushMatrix ( ) ;
GL . Translate ( pane . Translate . X , pane . Translate . Y , 0 ) ;
GL . Rotate ( pane . Rotate . Z , 0 , 0 , 1 ) ;
GL . Scale ( pane . Scale . X , pane . Scale . Y , 1 ) ;
if ( pane is BFLYT . PAN1 )
DrawDefaultPane ( ( BFLYT . PAN1 ) pane ) ;
else if ( pane is BCLYT . PAN1 )
DrawDefaultPane ( ( BCLYT . PAN1 ) pane ) ;
else if ( pane is BRLYT . PAN1 )
DrawDefaultPane ( ( BRLYT . PAN1 ) pane ) ;
GL . PopMatrix ( ) ;
}
2019-09-05 22:24:03 +02:00
if ( partPane ! = null )
{
var translate = partPane . Translate + pane . Translate ;
var scale = partPane . Scale * pane . Scale ;
var rotate = partPane . Rotate + pane . Rotate ;
GL . Translate ( translate . X + translate . X , translate . Y , 0 ) ;
2019-09-07 01:12:39 +02:00
GL . Rotate ( rotate . X , 1 , 0 , 0 ) ;
GL . Rotate ( rotate . Y , 0 , 1 , 0 ) ;
GL . Rotate ( rotate . Z , 0 , 0 , 1 ) ;
2019-09-05 22:24:03 +02:00
GL . Scale ( scale . X , scale . Y , 1 ) ;
}
else
{
GL . Translate ( pane . Translate . X , pane . Translate . Y , 0 ) ;
2019-09-07 01:12:39 +02:00
GL . Rotate ( pane . Rotate . X , 1 , 0 , 0 ) ;
GL . Rotate ( pane . Rotate . Y , 0 , 1 , 0 ) ;
GL . Rotate ( pane . Rotate . Z , 0 , 0 , 1 ) ;
2019-09-05 22:24:03 +02:00
GL . Scale ( pane . Scale . X , pane . Scale . Y , 1 ) ;
}
byte effectiveAlpha = ( byte ) ( parentAlpha = = 255 ? pane . Alpha : ( pane . Alpha * parentAlpha ) / 255 ) ;
2019-09-08 21:15:42 +02:00
if ( ! parentAlphaInfluence )
effectiveAlpha = pane . Alpha ;
2019-09-07 01:12:39 +02:00
parentAlphaInfluence = parentAlphaInfluence | | pane . InfluenceAlpha ;
2019-09-02 23:10:24 +02:00
2019-08-29 21:45:32 +02:00
if ( ! isRoot )
{
if ( pane is BFLYT . PIC1 )
2019-09-08 21:15:42 +02:00
DrawPicturePane ( ( BFLYT . PIC1 ) pane , effectiveAlpha , stage ) ;
2019-08-31 00:53:45 +02:00
else if ( pane is BCLYT . PIC1 )
2019-09-02 23:10:24 +02:00
DrawPicturePane ( ( BCLYT . PIC1 ) pane , effectiveAlpha ) ;
2019-08-31 23:24:05 +02:00
else if ( pane is BRLYT . PIC1 )
2019-09-02 23:10:24 +02:00
DrawPicturePane ( ( BRLYT . PIC1 ) pane , effectiveAlpha ) ;
2019-09-08 21:15:42 +02:00
else if ( pane is BFLYT . WND1 )
DrawWindowPane ( ( BFLYT . WND1 ) pane , effectiveAlpha ) ;
else if ( pane is BFLYT . BND1 )
DrawBoundryPane ( ( BFLYT . BND1 ) pane , effectiveAlpha ) ;
2019-09-05 22:24:03 +02:00
else if ( pane is BFLYT . PRT1 )
2019-09-07 01:12:39 +02:00
DrawPartsPane ( ( BFLYT . PRT1 ) pane , effectiveAlpha , parentAlphaInfluence ) ;
2019-08-29 21:45:32 +02:00
else if ( pane is BFLYT . PAN1 )
2019-08-29 22:33:23 +02:00
DrawDefaultPane ( ( BFLYT . PAN1 ) pane ) ;
2019-08-31 00:53:45 +02:00
else if ( pane is BCLYT . PAN1 )
DrawDefaultPane ( ( BCLYT . PAN1 ) pane ) ;
2019-08-31 23:24:05 +02:00
else if ( pane is BRLYT . PAN1 )
DrawDefaultPane ( ( BRLYT . PAN1 ) pane ) ;
2019-08-29 21:45:32 +02:00
}
else
isRoot = false ;
2019-08-28 01:03:01 +02:00
2019-09-07 01:12:39 +02:00
byte childAlpha = pane . InfluenceAlpha | | parentAlphaInfluence ? effectiveAlpha : byte . MaxValue ;
2019-08-29 21:45:32 +02:00
foreach ( var childPane in pane . Childern )
2019-09-07 01:12:39 +02:00
RenderPanes ( childPane , isRoot , childAlpha , parentAlphaInfluence , partPane ) ;
2019-08-29 21:45:32 +02:00
GL . PopMatrix ( ) ;
}
2019-08-29 22:33:23 +02:00
private void DrawRootPane ( BasePane pane )
2019-08-29 21:45:32 +02:00
{
Color color = Color . Black ;
if ( SelectedPanes . Contains ( pane ) )
color = Color . Red ;
2019-08-28 01:03:01 +02:00
2019-08-29 22:33:23 +02:00
CustomRectangle rect = pane . CreateRectangle ( ) ;
2019-08-29 21:45:32 +02:00
//Draw a quad which is the backcolor but lighter
2019-08-28 01:03:01 +02:00
GL . Begin ( PrimitiveType . Quads ) ;
2019-08-29 21:45:32 +02:00
GL . Color3 ( BackgroundColor . Lighten ( 10 ) ) ;
GL . Vertex2 ( rect . LeftPoint , rect . TopPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . TopPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . BottomPoint ) ;
GL . Vertex2 ( rect . LeftPoint , rect . BottomPoint ) ;
GL . End ( ) ;
//Draw outline of root pane
GL . Begin ( PrimitiveType . LineLoop ) ;
GL . PolygonOffset ( 0.5f , 2 ) ;
GL . LineWidth ( 33 ) ;
GL . Color3 ( color ) ;
GL . Vertex2 ( rect . LeftPoint , rect . TopPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . TopPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . BottomPoint ) ;
GL . Vertex2 ( rect . LeftPoint , rect . BottomPoint ) ;
2019-08-28 01:03:01 +02:00
GL . End ( ) ;
2019-08-29 21:45:32 +02:00
}
2019-09-08 21:15:42 +02:00
private void DrawBoundryPane ( BFLYT . BND1 pane , byte effectiveAlpha )
{
Vector2 [ ] TexCoords = new Vector2 [ ] {
new Vector2 ( 1 , 1 ) ,
new Vector2 ( 0 , 1 ) ,
new Vector2 ( 0 , 0 ) ,
new Vector2 ( 1 , 0 )
} ;
Color color = Color . DarkGreen ;
if ( SelectedPanes . Contains ( pane ) )
color = Color . Red ;
color = Color . FromArgb ( 70 , color ) ;
Color [ ] Colors = new Color [ ] {
color ,
color ,
color ,
color ,
} ;
DrawRectangle ( pane . Rectangle , TexCoords , Colors , false , effectiveAlpha ) ;
}
2019-08-31 00:53:45 +02:00
private void DrawDefaultPane ( BasePane pane )
2019-08-29 21:45:32 +02:00
{
Vector2 [ ] TexCoords = new Vector2 [ ] {
new Vector2 ( 1 , 1 ) ,
new Vector2 ( 0 , 1 ) ,
new Vector2 ( 0 , 0 ) ,
new Vector2 ( 1 , 0 )
} ;
Color color = Color . White ;
if ( SelectedPanes . Contains ( pane ) )
color = Color . Red ;
Color [ ] Colors = new Color [ ] {
color ,
color ,
color ,
color ,
} ;
2019-09-07 01:12:39 +02:00
DrawRectangle ( pane . Rectangle , TexCoords , Colors ) ;
2019-08-29 21:45:32 +02:00
}
2019-09-07 01:12:39 +02:00
private void DrawPartsPane ( BFLYT . PRT1 pane , byte effectiveAlpha , bool parentInfluenceAlpha )
2019-09-05 22:24:03 +02:00
{
pane . UpdateTextureData ( this . Textures ) ;
var partPane = pane . GetExternalPane ( ) ;
if ( partPane ! = null )
2019-09-08 21:15:42 +02:00
RenderPanes ( partPane , true , effectiveAlpha , parentInfluenceAlpha ) ;
2019-09-05 22:24:03 +02:00
else
DrawDefaultPane ( pane ) ;
if ( pane . Properties ! = null )
{
foreach ( var prop in pane . Properties )
{
if ( prop . Property ! = null )
2019-09-08 21:15:42 +02:00
{
2019-09-07 01:12:39 +02:00
RenderPanes ( prop . Property , false , effectiveAlpha , parentInfluenceAlpha | | pane . InfluenceAlpha ) ;
2019-09-08 21:15:42 +02:00
}
2019-09-05 22:24:03 +02:00
}
}
}
private void DrawWindowPane ( BFLYT . WND1 pane , byte effectiveAlpha )
{
2019-09-08 21:15:42 +02:00
Vector2 [ ] TexCoords = new Vector2 [ ] {
new Vector2 ( 1 , 1 ) ,
new Vector2 ( 0 , 1 ) ,
new Vector2 ( 0 , 0 ) ,
new Vector2 ( 1 , 0 )
} ;
Color [ ] Colors = new Color [ ] {
pane . Content . ColorTopLeft . Color ,
pane . Content . ColorTopRight . Color ,
pane . Content . ColorBottomRight . Color ,
pane . Content . ColorBottomLeft . Color ,
} ;
2019-09-05 22:24:03 +02:00
float frameLeft = 0 ;
float frameTop = 0 ;
float frameRight = 0 ;
float frameBottom = 0 ;
if ( pane . FrameCount = = 1 )
{
}
else if ( pane . FrameCount = = 4 )
{
}
else if ( pane . FrameCount = = 8 )
{
}
2019-09-08 21:15:42 +02:00
var mat = pane . Content . Material ;
if ( mat . Shader = = null )
{
mat . Shader = new BflytShader ( mat ) ;
mat . Shader . Compile ( ) ;
}
mat . Shader . Enable ( ) ;
( ( BflytShader ) mat . Shader ) . SetMaterials ( Textures ) ;
if ( pane . Content . TexCoords . Count > 0 )
{
TexCoords = new Vector2 [ ] {
pane . Content . TexCoords [ 0 ] . TopLeft . ToTKVector2 ( ) ,
pane . Content . TexCoords [ 0 ] . TopRight . ToTKVector2 ( ) ,
pane . Content . TexCoords [ 0 ] . BottomRight . ToTKVector2 ( ) ,
pane . Content . TexCoords [ 0 ] . BottomLeft . ToTKVector2 ( ) ,
} ;
}
DrawRectangle ( pane . Rectangle , TexCoords , Colors , false , effectiveAlpha ) ;
mat . Shader . Disable ( ) ;
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
GL . PopAttrib ( ) ;
2019-09-05 22:24:03 +02:00
}
2019-09-02 23:10:24 +02:00
private void DrawPicturePane ( BCLYT . PIC1 pane , byte effectiveAlpha )
2019-08-31 00:53:45 +02:00
{
2019-09-08 21:15:42 +02:00
Vector2 [ ] TexCoords = new Vector2 [ ] {
2019-08-31 00:53:45 +02:00
new Vector2 ( 1 , 1 ) ,
new Vector2 ( 0 , 1 ) ,
new Vector2 ( 0 , 0 ) ,
new Vector2 ( 1 , 0 )
} ;
Color [ ] Colors = new Color [ ] {
pane . ColorTopLeft . Color ,
pane . ColorTopRight . Color ,
pane . ColorBottomRight . Color ,
pane . ColorBottomLeft . Color ,
} ;
2019-09-02 23:10:24 +02:00
var mat = pane . Material ;
2019-08-31 00:53:45 +02:00
if ( pane . TexCoords . Length > 0 )
{
string textureMap0 = "" ;
if ( mat . TextureMaps . Count > 0 )
textureMap0 = mat . GetTexture ( 0 ) ;
// if (Textures.ContainsKey(textureMap0))
// BindGLTexture(mat.TextureMaps[0], Textures[textureMap0]);
TexCoords = new Vector2 [ ] {
pane . TexCoords [ 0 ] . TopLeft . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . TopRight . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . BottomRight . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . BottomLeft . ToTKVector2 ( ) ,
} ;
}
2019-09-07 01:12:39 +02:00
DrawRectangle ( pane . Rectangle , TexCoords , Colors , false , effectiveAlpha ) ;
2019-08-31 00:53:45 +02:00
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
}
2019-09-02 23:10:24 +02:00
private void DrawPicturePane ( BRLYT . PIC1 pane , byte effectiveAlpha )
2019-08-31 23:24:05 +02:00
{
Vector2 [ ] TexCoords = new Vector2 [ ] {
new Vector2 ( 1 , 1 ) ,
new Vector2 ( 0 , 1 ) ,
new Vector2 ( 0 , 0 ) ,
new Vector2 ( 1 , 0 )
} ;
Color [ ] Colors = new Color [ ] {
pane . ColorTopLeft . Color ,
pane . ColorTopRight . Color ,
pane . ColorBottomRight . Color ,
pane . ColorBottomLeft . Color ,
} ;
if ( pane . TexCoords . Length > 0 )
{
var mat = pane . GetMaterial ( ) ;
string textureMap0 = "" ;
if ( mat . TextureMaps . Count > 0 )
textureMap0 = mat . GetTexture ( 0 ) ;
// if (Textures.ContainsKey(textureMap0))
// BindGLTexture(mat.TextureMaps[0], Textures[textureMap0]);
2019-09-06 00:14:15 +02:00
if ( Runtime . LayoutEditor . Shading = = Runtime . LayoutEditor . DebugShading . UVTestPattern )
GL . BindTexture ( TextureTarget . Texture2D , RenderTools . uvTestPattern . RenderableTex . TexID ) ;
2019-08-31 23:24:05 +02:00
TexCoords = new Vector2 [ ] {
pane . TexCoords [ 0 ] . TopLeft . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . TopRight . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . BottomRight . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . BottomLeft . ToTKVector2 ( ) ,
} ;
}
2019-09-07 01:12:39 +02:00
DrawRectangle ( pane . Rectangle , TexCoords , Colors , false , effectiveAlpha ) ;
2019-08-31 23:24:05 +02:00
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
}
2019-09-08 21:15:42 +02:00
private void DrawPicturePane ( BFLYT . PIC1 pane , byte effectiveAlpha , int stage = 0 )
2019-08-29 21:45:32 +02:00
{
Vector2 [ ] TexCoords = new Vector2 [ ] {
new Vector2 ( 1 , 1 ) ,
new Vector2 ( 0 , 1 ) ,
new Vector2 ( 0 , 0 ) ,
new Vector2 ( 1 , 0 )
} ;
Color [ ] Colors = new Color [ ] {
pane . ColorTopLeft . Color ,
pane . ColorTopRight . Color ,
pane . ColorBottomRight . Color ,
pane . ColorBottomLeft . Color ,
} ;
2019-09-02 21:26:26 +02:00
var mat = pane . Material ;
2019-09-08 21:15:42 +02:00
if ( mat . Shader = = null )
2019-08-29 21:45:32 +02:00
{
2019-09-08 21:15:42 +02:00
mat . Shader = new BflytShader ( mat ) ;
mat . Shader . Compile ( ) ;
}
2019-09-02 23:10:24 +02:00
2019-09-08 21:15:42 +02:00
mat . Shader . Enable ( ) ;
( ( BflytShader ) mat . Shader ) . SetMaterials ( Textures ) ;
2019-08-29 21:45:32 +02:00
2019-09-08 21:15:42 +02:00
if ( pane . TexCoords . Length > 0 )
{
2019-08-29 21:45:32 +02:00
TexCoords = new Vector2 [ ] {
pane . TexCoords [ 0 ] . TopLeft . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . TopRight . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . BottomRight . ToTKVector2 ( ) ,
pane . TexCoords [ 0 ] . BottomLeft . ToTKVector2 ( ) ,
} ;
}
2019-09-08 21:15:42 +02:00
DrawRectangle ( pane . Rectangle , TexCoords , Colors , false , effectiveAlpha ) ;
2019-09-06 00:14:15 +02:00
2019-09-08 21:15:42 +02:00
mat . Shader . Disable ( ) ;
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
GL . PopAttrib ( ) ;
}
2019-09-06 00:14:15 +02:00
2019-09-08 21:15:42 +02:00
private static BlendingFactor ConvertBlendFactor ( BlendMode . GX2BlendFactor blendFactor )
{
switch ( blendFactor )
2019-09-06 00:14:15 +02:00
{
2019-09-08 21:15:42 +02:00
case BlendMode . GX2BlendFactor . DestAlpha : return BlendingFactor . DstAlpha ;
case BlendMode . GX2BlendFactor . DestColor : return BlendingFactor . DstColor ;
case BlendMode . GX2BlendFactor . DestInvAlpha : return BlendingFactor . OneMinusDstAlpha ;
case BlendMode . GX2BlendFactor . DestInvColor : return BlendingFactor . OneMinusDstColor ;
case BlendMode . GX2BlendFactor . Factor0 : return BlendingFactor . Zero ;
case BlendMode . GX2BlendFactor . Factor1 : return BlendingFactor . One ;
case BlendMode . GX2BlendFactor . SourceAlpha : return BlendingFactor . SrcAlpha ;
case BlendMode . GX2BlendFactor . SourceColor : return BlendingFactor . SrcColor ;
case BlendMode . GX2BlendFactor . SourceInvAlpha : return BlendingFactor . OneMinusSrcAlpha ;
case BlendMode . GX2BlendFactor . SourceInvColor : return BlendingFactor . OneMinusSrcColor ;
default : return BlendingFactor . Zero ;
2019-09-06 00:14:15 +02:00
}
2019-09-08 21:15:42 +02:00
}
2019-09-06 00:14:15 +02:00
2019-09-08 21:15:42 +02:00
private static LogicOp ConvertLogicOperation ( BlendMode . GX2LogicOp blendOp )
{
switch ( blendOp )
{
case BlendMode . GX2LogicOp . And : return LogicOp . And ;
case BlendMode . GX2LogicOp . Clear : return LogicOp . Clear ;
case BlendMode . GX2LogicOp . Copy : return LogicOp . Copy ;
case BlendMode . GX2LogicOp . Equiv : return LogicOp . Equiv ;
case BlendMode . GX2LogicOp . Inv : return LogicOp . Invert ;
case BlendMode . GX2LogicOp . Nand : return LogicOp . Nand ;
case BlendMode . GX2LogicOp . NoOp : return LogicOp . Noop ;
case BlendMode . GX2LogicOp . Nor : return LogicOp . Nor ;
case BlendMode . GX2LogicOp . Or : return LogicOp . Or ;
case BlendMode . GX2LogicOp . RevAnd : return LogicOp . AndReverse ;
case BlendMode . GX2LogicOp . RevOr : return LogicOp . OrReverse ;
case BlendMode . GX2LogicOp . Set : return LogicOp . Set ;
case BlendMode . GX2LogicOp . Xor : return LogicOp . Xor ;
case BlendMode . GX2LogicOp . Disable :
GL . Disable ( EnableCap . ColorLogicOp ) ;
return LogicOp . Noop ;
default : return LogicOp . Noop ;
2019-09-07 01:12:39 +02:00
2019-09-08 21:15:42 +02:00
}
2019-08-29 21:45:32 +02:00
}
2019-09-08 21:15:42 +02:00
private static BlendEquationMode ConvertBlendOperation ( BlendMode . GX2BlendOp blendOp )
2019-09-07 01:12:39 +02:00
{
2019-09-08 21:15:42 +02:00
switch ( blendOp )
{
case BlendMode . GX2BlendOp . Add : return BlendEquationMode . FuncAdd ;
case BlendMode . GX2BlendOp . ReverseSubtract : return BlendEquationMode . FuncReverseSubtract ;
case BlendMode . GX2BlendOp . SelectMax : return BlendEquationMode . Max ;
case BlendMode . GX2BlendOp . SelectMin : return BlendEquationMode . Min ;
case BlendMode . GX2BlendOp . Subtract : return BlendEquationMode . FuncSubtract ;
case BlendMode . GX2BlendOp . Disable :
GL . Disable ( EnableCap . Blend ) ;
return BlendEquationMode . FuncAdd ;
default : return BlendEquationMode . FuncAdd ;
}
2019-09-07 01:12:39 +02:00
}
public void DrawRectangle ( CustomRectangle rect , Vector2 [ ] texCoords ,
Color [ ] colors , bool useLines = true , byte alpha = 255 )
2019-08-29 21:45:32 +02:00
{
2019-09-02 23:10:24 +02:00
for ( int i = 0 ; i < colors . Length ; i + + )
2019-09-07 01:12:39 +02:00
{
uint setalpha = ( uint ) ( ( colors [ i ] . A * alpha ) / 255 ) ;
colors [ i ] = Color . FromArgb ( ( int ) setalpha , colors [ i ] ) ;
}
2019-09-02 23:10:24 +02:00
2019-08-29 21:45:32 +02:00
if ( useLines )
{
GL . Begin ( PrimitiveType . LineLoop ) ;
2019-09-02 21:26:26 +02:00
GL . Color4 ( colors [ 0 ] ) ;
2019-08-29 21:45:32 +02:00
GL . Vertex2 ( rect . LeftPoint , rect . BottomPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . BottomPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . TopPoint ) ;
GL . Vertex2 ( rect . LeftPoint , rect . TopPoint ) ;
GL . End ( ) ;
}
else
{
GL . Begin ( PrimitiveType . Quads ) ;
2019-09-02 21:26:26 +02:00
GL . Color4 ( colors [ 0 ] ) ;
2019-09-08 21:15:42 +02:00
GL . MultiTexCoord2 ( TextureUnit . Texture0 , texCoords [ 0 ] . X , texCoords [ 0 ] . Y ) ;
2019-08-29 21:45:32 +02:00
GL . Vertex2 ( rect . LeftPoint , rect . BottomPoint ) ;
2019-09-02 21:26:26 +02:00
GL . Color4 ( colors [ 1 ] ) ;
2019-09-08 21:15:42 +02:00
GL . MultiTexCoord2 ( TextureUnit . Texture0 , texCoords [ 1 ] . X , texCoords [ 1 ] . Y ) ;
2019-08-29 21:45:32 +02:00
GL . Vertex2 ( rect . RightPoint , rect . BottomPoint ) ;
2019-09-02 21:26:26 +02:00
GL . Color4 ( colors [ 2 ] ) ;
2019-09-08 21:15:42 +02:00
GL . MultiTexCoord2 ( TextureUnit . Texture0 , texCoords [ 2 ] . X , texCoords [ 2 ] . Y ) ;
2019-08-29 21:45:32 +02:00
GL . Vertex2 ( rect . RightPoint , rect . TopPoint ) ;
2019-09-02 21:26:26 +02:00
GL . Color4 ( colors [ 3 ] ) ;
2019-09-08 21:15:42 +02:00
GL . MultiTexCoord2 ( TextureUnit . Texture0 , texCoords [ 3 ] . X , texCoords [ 3 ] . Y ) ;
2019-08-29 21:45:32 +02:00
GL . Vertex2 ( rect . LeftPoint , rect . TopPoint ) ;
GL . End ( ) ;
2019-09-05 22:24:03 +02:00
//Draw outline
GL . Begin ( PrimitiveType . LineLoop ) ;
GL . LineWidth ( 3 ) ;
GL . Color4 ( colors [ 0 ] ) ;
GL . Vertex2 ( rect . LeftPoint , rect . BottomPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . BottomPoint ) ;
GL . Vertex2 ( rect . RightPoint , rect . TopPoint ) ;
GL . Vertex2 ( rect . LeftPoint , rect . TopPoint ) ;
GL . End ( ) ;
2019-08-29 21:45:32 +02:00
}
}
2019-09-04 00:58:58 +02:00
enum TextureSwizzle
{
Zero = All . Zero ,
One = All . One ,
Red = All . Red ,
Green = All . Green ,
Blue = All . Blue ,
Alpha = All . Alpha ,
}
2019-09-08 21:15:42 +02:00
public static bool BindGLTexture ( TextureRef tex , STGenericTexture texture )
2019-08-29 21:45:32 +02:00
{
if ( texture . RenderableTex = = null | | ! texture . RenderableTex . GLInitialized )
texture . LoadOpenGLTexture ( ) ;
//If the texture is still not initialized then return
if ( ! texture . RenderableTex . GLInitialized )
2019-09-08 21:15:42 +02:00
return false ;
2019-08-29 21:45:32 +02:00
GL . BindTexture ( TextureTarget . Texture2D , texture . RenderableTex . TexID ) ;
2019-09-04 00:58:58 +02:00
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureSwizzleR , ConvertChannel ( texture . RedChannel ) ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureSwizzleG , ConvertChannel ( texture . GreenChannel ) ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureSwizzleB , ConvertChannel ( texture . BlueChannel ) ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureSwizzleA , ConvertChannel ( texture . AlphaChannel ) ) ;
2019-08-29 21:45:32 +02:00
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureWrapS , ConvertTextureWrap ( tex . WrapModeU ) ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureWrapT , ConvertTextureWrap ( tex . WrapModeV ) ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMagFilter , ConvertMagFilterMode ( tex . MaxFilterMode ) ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMinFilter , ConvertMinFilterMode ( tex . MinFilterMode ) ) ;
2019-09-08 21:15:42 +02:00
return true ;
2019-08-29 21:45:32 +02:00
}
2019-09-04 00:58:58 +02:00
private static int ConvertChannel ( STChannelType type )
{
switch ( type )
{
case STChannelType . Red : return ( int ) TextureSwizzle . Red ;
case STChannelType . Green : return ( int ) TextureSwizzle . Green ;
case STChannelType . Blue : return ( int ) TextureSwizzle . Blue ;
case STChannelType . Alpha : return ( int ) TextureSwizzle . Alpha ;
case STChannelType . Zero : return ( int ) TextureSwizzle . Zero ;
case STChannelType . One : return ( int ) TextureSwizzle . One ;
default : return 0 ;
}
}
private static int ConvertTextureWrap ( TextureRef . WrapMode wrapMode )
2019-08-29 21:45:32 +02:00
{
switch ( wrapMode )
{
2019-09-01 19:02:48 +02:00
case TextureRef . WrapMode . Clamp : return ( int ) TextureWrapMode . Clamp ;
case TextureRef . WrapMode . Mirror : return ( int ) TextureWrapMode . MirroredRepeat ;
case TextureRef . WrapMode . Repeat : return ( int ) TextureWrapMode . Repeat ;
2019-08-29 21:45:32 +02:00
default : return ( int ) TextureWrapMode . Clamp ;
}
}
2019-09-01 19:02:48 +02:00
private static int ConvertMagFilterMode ( TextureRef . FilterMode filterMode )
2019-08-29 21:45:32 +02:00
{
switch ( filterMode )
{
2019-09-01 19:02:48 +02:00
case TextureRef . FilterMode . Linear : return ( int ) TextureMagFilter . Linear ;
case TextureRef . FilterMode . Near : return ( int ) TextureMagFilter . Nearest ;
default : return ( int ) TextureRef . FilterMode . Linear ;
2019-08-29 21:45:32 +02:00
}
}
2019-09-01 19:02:48 +02:00
private static int ConvertMinFilterMode ( TextureRef . FilterMode filterMode )
2019-08-29 21:45:32 +02:00
{
switch ( filterMode )
{
2019-09-01 19:02:48 +02:00
case TextureRef . FilterMode . Linear : return ( int ) TextureMinFilter . Linear ;
case TextureRef . FilterMode . Near : return ( int ) TextureMinFilter . Nearest ;
default : return ( int ) TextureRef . FilterMode . Linear ;
2019-08-29 21:45:32 +02:00
}
}
private void DrawBackground ( )
{
if ( backgroundTex = = null )
{
/ * backgroundTex = RenderableTex . FromBitmap ( Properties . Resources . GridBackground ) ;
backgroundTex . TextureWrapR = TextureWrapMode . Repeat ;
backgroundTex . TextureWrapT = TextureWrapMode . Repeat ;
GL . Enable ( EnableCap . Texture2D ) ;
GL . BindTexture ( TextureTarget . Texture2D , backgroundTex . TexID ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureWrapS , ( float ) backgroundTex . TextureWrapR ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureWrapT , ( float ) backgroundTex . TextureWrapT ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMagFilter , ( float ) backgroundTex . TextureMagFilter ) ;
GL . TexParameter ( TextureTarget . Texture2D , TextureParameterName . TextureMinFilter , ( float ) backgroundTex . TextureMinFilter ) ;
float UVscale = 15 ;
int PanelWidth = 9000 ;
int PanelWHeight = 9000 ;
Vector2 scaleCenter = new Vector2 ( 0.5f , 0.5f ) ;
Vector2 [ ] TexCoords = new Vector2 [ ] {
new Vector2 ( 1 , 1 ) ,
new Vector2 ( 0 , 1 ) ,
new Vector2 ( 0 , 0 ) ,
new Vector2 ( 1 , 0 ) ,
} ;
for ( int i = 0 ; i < TexCoords . Length ; i + + )
TexCoords [ i ] = ( TexCoords [ i ] - scaleCenter ) * 20 + scaleCenter ;
GL . MatrixMode ( MatrixMode . Modelview ) ;
GL . LoadIdentity ( ) ;
GL . PushMatrix ( ) ;
GL . Scale ( 1 , 1 , 1 ) ;
GL . Translate ( 0 , 0 , 0 ) ;
GL . Color4 ( Color . White ) ;
GL . Begin ( PrimitiveType . Quads ) ;
GL . TexCoord2 ( TexCoords [ 0 ] ) ;
GL . Vertex3 ( PanelWidth , PanelWHeight , 0 ) ;
GL . TexCoord2 ( TexCoords [ 1 ] ) ;
GL . Vertex3 ( - PanelWidth , PanelWHeight , 0 ) ;
GL . TexCoord2 ( TexCoords [ 2 ] ) ;
GL . Vertex3 ( - PanelWidth , - PanelWHeight , 0 ) ;
GL . TexCoord2 ( TexCoords [ 3 ] ) ;
GL . Vertex3 ( PanelWidth , - PanelWHeight , 0 ) ;
GL . End ( ) ;
GL . BindTexture ( TextureTarget . Texture2D , 0 ) ;
GL . PopMatrix ( ) ; * /
}
}
public void UpdateBackgroundColor ( Color color )
{
2019-08-30 02:51:41 +02:00
Runtime . LayoutEditor . BackgroundColor = color ;
2019-08-29 21:45:32 +02:00
glControl1 . Invalidate ( ) ;
2019-08-30 02:51:41 +02:00
Config . Save ( ) ;
2019-08-29 21:45:32 +02:00
}
private void DrawXyLines ( )
{
int lineLength = 20 ;
GL . Color3 ( Color . Green ) ;
GL . Begin ( PrimitiveType . Lines ) ;
GL . Vertex2 ( 0 , 0 ) ;
GL . Vertex2 ( 0 , lineLength ) ;
GL . End ( ) ;
GL . Color3 ( Color . Red ) ;
GL . Begin ( PrimitiveType . Lines ) ;
GL . Vertex2 ( 0 , 0 ) ;
GL . Vertex2 ( lineLength , 0 ) ;
GL . End ( ) ;
2019-08-28 01:03:01 +02:00
}
2019-08-29 21:45:32 +02:00
private void DrawGrid ( )
2019-08-28 01:03:01 +02:00
{
2019-08-29 21:45:32 +02:00
var size = 40 ;
var amount = 300 ;
GL . LineWidth ( 0.001f ) ;
GL . Color3 ( BackgroundColor . Darken ( 20 ) ) ;
GL . Begin ( PrimitiveType . Lines ) ;
int squareGridCounter = 0 ;
for ( var i = - amount ; i < = amount ; i + + )
{
if ( squareGridCounter > 5 )
{
squareGridCounter = 0 ;
GL . LineWidth ( 33f ) ;
}
else
{
GL . LineWidth ( 0.001f ) ;
}
2019-09-07 01:12:39 +02:00
GL . Vertex2 ( new Vector2 ( - amount * size , i * size ) ) ;
GL . Vertex2 ( new Vector2 ( amount * size , i * size ) ) ;
GL . Vertex2 ( new Vector2 ( i * size , - amount * size ) ) ;
GL . Vertex2 ( new Vector2 ( i * size , amount * size ) ) ;
2019-08-28 01:03:01 +02:00
2019-08-29 21:45:32 +02:00
squareGridCounter + + ;
}
GL . End ( ) ;
GL . Color3 ( Color . Transparent ) ;
GL . PopAttrib ( ) ;
2019-08-28 01:03:01 +02:00
}
2019-08-29 21:45:32 +02:00
private bool mouseHeldDown = false ;
2019-09-03 01:48:47 +02:00
private bool isPicked = false ;
2019-08-29 21:45:32 +02:00
private Point originMouse ;
private void glControl1_MouseDown ( object sender , MouseEventArgs e )
2019-08-28 01:03:01 +02:00
{
2019-09-04 00:58:58 +02:00
SelectedPanes . Clear ( ) ;
2019-09-03 01:48:47 +02:00
//Pick an object for moving
if ( Control . ModifierKeys = = Keys . Alt & & e . Button = = MouseButtons . Left )
{
2019-09-04 00:58:58 +02:00
BasePane hitPane = null ;
SearchHit ( LayoutFile . RootPane , e . X , e . Y , ref hitPane ) ;
2019-09-03 01:48:47 +02:00
if ( hitPane ! = null )
{
SelectedPanes . Add ( hitPane ) ;
UpdateViewport ( ) ;
isPicked = true ;
}
}
else if ( e . Button = = MouseButtons . Left )
2019-08-29 21:45:32 +02:00
{
mouseHeldDown = true ;
originMouse = e . Location ;
2019-09-03 01:48:47 +02:00
2019-09-04 00:58:58 +02:00
BasePane hitPane = null ;
foreach ( var child in LayoutFile . RootPane . Childern )
SearchHit ( child , e . X , e . Y , ref hitPane ) ;
2019-09-03 01:48:47 +02:00
Console . WriteLine ( $"Has Hit " + hitPane ! = null ) ;
if ( hitPane ! = null )
{
SelectedPanes . Add ( hitPane ) ;
UpdateViewport ( ) ;
}
2019-08-29 21:45:32 +02:00
glControl1 . Invalidate ( ) ;
}
2019-09-04 00:58:58 +02:00
Console . WriteLine ( "SelectedPanes " + SelectedPanes . Count ) ;
2019-08-29 21:45:32 +02:00
}
2019-08-28 01:03:01 +02:00
2019-09-04 00:58:58 +02:00
private void SearchHit ( BasePane pane , int X , int Y , ref BasePane SelectedPane )
2019-09-03 01:48:47 +02:00
{
2019-09-04 00:58:58 +02:00
if ( pane . IsHit ( X , Y ) ) {
SelectedPane = pane ;
return ;
}
2019-09-03 01:48:47 +02:00
foreach ( var childPane in pane . Childern )
2019-09-04 00:58:58 +02:00
SearchHit ( childPane , X , Y , ref SelectedPane ) ;
2019-09-03 01:48:47 +02:00
}
2019-08-29 21:45:32 +02:00
private void glControl1_MouseUp ( object sender , MouseEventArgs e )
{
if ( e . Button = = MouseButtons . Left )
{
mouseHeldDown = false ;
2019-09-03 01:48:47 +02:00
isPicked = false ;
2019-08-29 21:45:32 +02:00
glControl1 . Invalidate ( ) ;
}
2019-08-28 01:03:01 +02:00
}
private void glControl1_MouseMove ( object sender , MouseEventArgs e )
{
2019-08-29 21:45:32 +02:00
if ( mouseHeldDown )
{
var pos = new Vector2 ( e . Location . X - originMouse . X , e . Location . Y - originMouse . Y ) ;
Camera . Position . X + = pos . X ;
Camera . Position . Y - = pos . Y ;
originMouse = e . Location ;
glControl1 . Invalidate ( ) ;
}
}
2019-08-28 01:03:01 +02:00
2019-08-31 00:53:45 +02:00
protected override void OnClosed ( EventArgs e )
{
foreach ( var tex in LayoutFile . Textures )
{
if ( Textures . ContainsKey ( tex ) )
{
Textures [ tex ] . DisposeRenderable ( ) ;
Textures . Remove ( tex ) ;
}
}
}
2019-08-29 21:45:32 +02:00
protected override void OnMouseWheel ( MouseEventArgs e )
{
base . OnMouseWheel ( e ) ;
2019-09-07 01:12:39 +02:00
if ( UseOrtho )
{
if ( e . Delta > 0 & & Camera . Zoom > 0 )
Camera . Zoom + = 0.1f ;
if ( e . Delta < 0 & & Camera . Zoom < 100 & & Camera . Zoom > 0.1 )
Camera . Zoom - = 0.1f ;
}
else
{
if ( e . Delta > 0 & & Camera . Zoom > 0.1 )
Camera . Zoom - = 0.1f ;
if ( e . Delta < 0 & & Camera . Zoom < 100 & & Camera . Zoom > 0 )
Camera . Zoom + = 0.1f ;
}
2019-08-29 21:45:32 +02:00
glControl1 . Invalidate ( ) ;
2019-08-28 01:03:01 +02:00
}
private void glControl1_Resize ( object sender , EventArgs e )
{
glControl1 . Invalidate ( ) ;
}
2019-08-27 22:38:06 +02:00
}
}