ClassiCube/ClassicalSharp/2D/Screens/HudScreen.cs

154 lines
4.3 KiB
C#
Raw Normal View History

using System;
using System.Drawing;
using ClassicalSharp.GraphicsAPI;
using OpenTK.Input;
2014-12-17 14:47:17 +11:00
namespace ClassicalSharp {
public class HudScreen : Screen {
2014-12-17 14:47:17 +11:00
public HudScreen( Game game ) : base( game ) {
2014-12-17 14:47:17 +11:00
}
ChatScreen chat;
BlockHotbarWidget hotbar;
PlayerListWidget playerList;
Font playerFont;
2014-12-17 14:47:17 +11:00
public override void Render( double delta ) {
2015-09-11 19:39:48 +10:00
if( game.HideGui ) return;
bool showMinimal = game.GetActiveScreen != this;
if( chat.HandlesAllInput )
chat.RenderBackground();
graphicsApi.Texturing = true;
2014-12-17 14:47:17 +11:00
chat.Render( delta );
if( !showMinimal )
RenderHotbar( delta );
//graphicsApi.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
//graphicsApi.BindTexture( game.TerrainAtlas.TexId );
//IsometricBlockDrawer.Draw( game, (byte)Block.Brick, 30, game.Width - 50, game.Height - 20 );
2014-12-17 14:47:17 +11:00
if( playerList != null ) {
playerList.Render( delta );
// NOTE: Should usually be caught by KeyUp, but just in case.
2015-10-25 20:34:47 +11:00
if( !game.IsKeyDown( KeyBinding.PlayerList ) ) {
2014-12-17 14:47:17 +11:00
playerList.Dispose();
playerList = null;
}
}
graphicsApi.Texturing = false;
if( playerList == null && !showMinimal )
DrawCrosshairs();
}
public void RenderHotbar( double delta ) { hotbar.Render( delta ); }
const int crosshairExtent = 15, crosshairWeight = 2;
void DrawCrosshairs() {
int curCol = 150 + (int)( 50 * Math.Abs( Math.Sin( game.accumulator ) ) );
FastColour col = new FastColour( curCol, curCol, curCol );
float centreX = game.Width / 2, centreY = game.Height / 2;
graphicsApi.Draw2DQuad( centreX - crosshairExtent, centreY - crosshairWeight,
crosshairExtent * 2, crosshairWeight * 2, col );
graphicsApi.Draw2DQuad( centreX - crosshairWeight, centreY - crosshairExtent,
crosshairWeight * 2, crosshairExtent * 2, col );
2014-12-17 14:47:17 +11:00
}
public override void Dispose() {
playerFont.Dispose();
2014-12-17 14:47:17 +11:00
chat.Dispose();
hotbar.Dispose();
if( playerList != null )
playerList.Dispose();
}
public void GainFocus() {
if( game.CursorVisible )
game.CursorVisible = false;
game.Camera.RegrabMouse();
}
public void LoseFocus() {
2014-12-17 14:47:17 +11:00
if( playerList != null ) {
playerList.Dispose();
}
if( !game.CursorVisible )
game.CursorVisible = true;
2014-12-17 14:47:17 +11:00
}
public override void OnResize( int oldWidth, int oldHeight, int width, int height ) {
chat.OnResize( oldWidth, oldHeight, width, height );
hotbar.OnResize( oldWidth, oldHeight, width, height );
if( playerList != null ) {
int deltaX = CalcDelta( width, oldWidth, Anchor.Centre );
playerList.MoveTo( playerList.X + deltaX, height / 4 );
2014-12-17 14:47:17 +11:00
}
}
public override void Init() {
playerFont = new Font( "Arial", 12 );
chat = new ChatScreen( game );
2014-12-17 14:47:17 +11:00
chat.Init();
hotbar = new BlockHotbarWidget( game );
2014-12-17 14:47:17 +11:00
hotbar.Init();
}
public override bool HandlesAllInput {
get { return chat.HandlesAllInput; }
}
public override bool HandlesKeyPress( char key ) {
return chat.HandlesKeyPress( key );
}
public override bool HandlesKeyDown( Key key ) {
2015-10-25 20:34:47 +11:00
if( key == game.Mapping( KeyBinding.PlayerList ) ) {
2014-12-17 14:47:17 +11:00
if( playerList == null ) {
if( game.Network.UsingExtPlayerList ) {
playerList = new ExtPlayerListWidget( game, playerFont );
2014-12-17 14:47:17 +11:00
} else {
playerList = new NormalPlayerListWidget( game, playerFont );
2014-12-17 14:47:17 +11:00
}
playerList.Init();
playerList.MoveTo( playerList.X, game.Height / 4 );
2014-12-17 14:47:17 +11:00
}
}
if( chat.HandlesKeyDown( key ) ) {
return true;
}
return hotbar.HandlesKeyDown( key );
}
public override bool HandlesKeyUp( Key key ) {
2015-10-25 20:34:47 +11:00
if( key == game.Mapping( KeyBinding.PlayerList ) ) {
2014-12-17 14:47:17 +11:00
if( playerList != null ) {
playerList.Dispose();
playerList = null;
return true;
}
}
return false;
}
2015-10-25 19:28:27 +11:00
public void OpenTextInputBar( string text ) {
chat.OpenTextInputBar( text );
}
public override bool HandlesMouseScroll( int delta ) {
return chat.HandlesMouseScroll( delta );
}
public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) {
if( button != MouseButton.Left || playerList == null || !HandlesAllInput ) return false;
string name = playerList.GetNameUnder( mouseX, mouseY );
if( name == null ) return false;
chat.AppendTextToInput( name + " " );
return true;
}
2014-12-17 14:47:17 +11:00
}
}