Refactor and comment gui, make tab list higher up and more solid.

This commit is contained in:
UnknownShadow200 2015-10-09 16:47:39 +11:00
parent 1f54263749
commit e9c190c28c
27 changed files with 280 additions and 314 deletions

View File

@ -0,0 +1,70 @@
using System;
using ClassicalSharp.GraphicsAPI;
using OpenTK.Input;
namespace ClassicalSharp {
public abstract class GuiElement : IDisposable {
protected Game game;
protected IGraphicsApi graphicsApi;
public GuiElement( Game game ) {
this.game = game;
graphicsApi = game.Graphics;
}
public abstract void Init();
public abstract void Render( double delta );
public abstract void Dispose();
/// <summary> Called when the game window is resized. </summary>
public abstract void OnResize( int oldWidth, int oldHeight, int width, int height );
public virtual bool HandlesKeyDown( Key key ) {
return false;
}
public virtual bool HandlesKeyPress( char key ) {
return false;
}
public virtual bool HandlesKeyUp( Key key ) {
return false;
}
public virtual bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) {
return false;
}
public virtual bool HandlesMouseMove( int mouseX, int mouseY ) {
return false;
}
public virtual bool HandlesMouseScroll( int delta ) {
return false;
}
public virtual bool HandlesMouseUp( int mouseX, int mouseY, MouseButton button ) {
return false;
}
protected static int CalcDelta( int newVal, int oldVal, Anchor mode ) {
return CalcOffset( newVal, oldVal, 0, mode );
}
protected static int CalcOffset( int axisSize, int elemSize, int offset, Anchor mode ) {
if( mode == Anchor.LeftOrTop ) return offset;
if( mode == Anchor.BottomOrRight) return axisSize - elemSize - offset;
return (axisSize - elemSize) / 2 + offset;
}
}
public enum Anchor {
LeftOrTop = 0,
Centre = 1,
BottomOrRight = 2,
}
}

View File

@ -56,19 +56,19 @@ namespace ClassicalSharp {
textInput = new TextInputWidget( game, chatFont, chatInputFont );
textInput.YOffset = ChatInputYOffset;
status = new TextGroupWidget( game, 3, chatFont );
status.VerticalDocking = Docking.LeftOrTop;
status.HorizontalDocking = Docking.BottomOrRight;
status.VerticalAnchor = Anchor.LeftOrTop;
status.HorizontalAnchor = Anchor.BottomOrRight;
status.Init();
bottomRight = new TextGroupWidget( game, 3, chatFont );
bottomRight.VerticalDocking = Docking.BottomOrRight;
bottomRight.HorizontalDocking = Docking.BottomOrRight;
bottomRight.VerticalAnchor = Anchor.BottomOrRight;
bottomRight.HorizontalAnchor = Anchor.BottomOrRight;
bottomRight.YOffset = ChatInputYOffset;
bottomRight.Init();
normalChat = new TextGroupWidget( game, chatLines, chatFont );
normalChat.XOffset = 10;
normalChat.YOffset = ChatLogYOffset;
normalChat.HorizontalDocking = Docking.LeftOrTop;
normalChat.VerticalDocking = Docking.BottomOrRight;
normalChat.HorizontalAnchor = Anchor.LeftOrTop;
normalChat.VerticalAnchor = Anchor.BottomOrRight;
normalChat.Init();
ChatLog chat = game.Chat;

View File

@ -25,8 +25,8 @@ namespace ClassicalSharp {
public override void Init() {
graphicsApi.ClearColour( new FastColour( 65, 31, 31 ) );
titleWidget = TextWidget.Create( game, 0, -30, title, Docking.Centre, Docking.Centre, titleFont );
messageWidget = TextWidget.Create( game, 0, 10, message, Docking.Centre, Docking.Centre, messageFont );
titleWidget = TextWidget.Create( game, 0, -30, title, Anchor.Centre, Anchor.Centre, titleFont );
messageWidget = TextWidget.Create( game, 0, 10, message, Anchor.Centre, Anchor.Centre, messageFont );
}
public override void Dispose() {

View File

@ -73,6 +73,9 @@ namespace ClassicalSharp {
graphicsApi.DeleteTexture( ref posTexture );
}
public override void OnResize( int oldWidth, int oldHeight, int width, int height ) {
}
void DrawPosition() {
int index = 0;
TextureRec xy = new TextureRec( 0, posTexture.Y1, baseWidth, posTexture.Height );

View File

@ -31,8 +31,8 @@ namespace ClassicalSharp {
public override void Init() {
graphicsApi.Fog = false;
titleWidget = TextWidget.Create( game, 0, 30, serverName, Docking.Centre, Docking.LeftOrTop, font );
messageWidget = TextWidget.Create( game, 0, 60, serverMotd, Docking.Centre, Docking.LeftOrTop, font );
titleWidget = TextWidget.Create( game, 0, 30, serverName, Anchor.Centre, Anchor.LeftOrTop, font );
messageWidget = TextWidget.Create( game, 0, 60, serverMotd, Anchor.Centre, Anchor.LeftOrTop, font );
progX = game.Width / 2f - progWidth / 2f;
Size size = new Size( progWidth, progHeight );

View File

@ -13,46 +13,46 @@ namespace ClassicalSharp {
base.Init();
buttons = new ButtonWidget[] {
Make( -140, -150, "Clouds colour", Docking.Centre, OnWidgetClick,
Make( -140, -150, "Clouds colour", Anchor.Centre, OnWidgetClick,
g => g.Map.CloudsCol.ToRGBHexString(),
(g, v) => g.Map.SetCloudsColour( FastColour.Parse( v ) ) ),
Make( -140, -100, "Sky colour", Docking.Centre, OnWidgetClick,
Make( -140, -100, "Sky colour", Anchor.Centre, OnWidgetClick,
g => g.Map.SkyCol.ToRGBHexString(),
(g, v) => g.Map.SetSkyColour( FastColour.Parse( v ) ) ),
Make( -140, -50, "Fog colour", Docking.Centre, OnWidgetClick,
Make( -140, -50, "Fog colour", Anchor.Centre, OnWidgetClick,
g => g.Map.FogCol.ToRGBHexString(),
(g, v) => g.Map.SetFogColour( FastColour.Parse( v ) ) ),
Make( -140, 0, "Clouds speed", Docking.Centre, OnWidgetClick,
Make( -140, 0, "Clouds speed", Anchor.Centre, OnWidgetClick,
g => { StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer;
return env == null ? "(not active)" : env.CloudsSpeed.ToString(); },
(g, v) => { StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer;
if( env != null )
env.CloudsSpeed = Single.Parse( v ); } ),
Make( -140, 50, "Clouds offset", Docking.Centre, OnWidgetClick,
Make( -140, 50, "Clouds offset", Anchor.Centre, OnWidgetClick,
g => (g.Map.CloudHeight - g.Map.Height).ToString(),
(g, v) => g.Map.SetCloudsLevel( g.Map.Height + Int32.Parse( v ) ) ),
Make( 140, -150, "Sunlight colour", Docking.Centre, OnWidgetClick,
Make( 140, -150, "Sunlight colour", Anchor.Centre, OnWidgetClick,
g => g.Map.Sunlight.ToRGBHexString(),
(g, v) => g.Map.SetSunlight( FastColour.Parse( v ) ) ),
Make( 140, -100, "Shadow colour", Docking.Centre, OnWidgetClick,
Make( 140, -100, "Shadow colour", Anchor.Centre, OnWidgetClick,
g => g.Map.Shadowlight.ToRGBHexString(),
(g, v) => g.Map.SetShadowlight( FastColour.Parse( v ) ) ),
Make( 140, -50, "Weather", Docking.Centre, OnWidgetClick,
Make( 140, -50, "Weather", Anchor.Centre, OnWidgetClick,
g => ((int)g.Map.Weather).ToString(),
(g, v) => g.Map.SetWeather( (Weather)Int32.Parse( v ) ) ),
Make( 140, 0, "Water level", Docking.Centre, OnWidgetClick,
Make( 140, 0, "Water level", Anchor.Centre, OnWidgetClick,
g => g.Map.WaterHeight.ToString(),
(g, v) => g.Map.SetWaterLevel( Int32.Parse( v ) ) ),
Make( 0, 5, "Back to menu", Docking.BottomOrRight,
Make( 0, 5, "Back to menu", Anchor.BottomOrRight,
(g, w) => g.SetNewScreen( new PauseScreen( g ) ), null, null ),
null,
};
@ -71,9 +71,9 @@ namespace ClassicalSharp {
okayIndex = buttons.Length - 1;
}
ButtonWidget Make( int x, int y, string text, Docking vDocking, Action<Game, ButtonWidget> onClick,
ButtonWidget Make( int x, int y, string text, Anchor vDocking, Action<Game, ButtonWidget> onClick,
Func<Game, string> getter, Action<Game, string> setter ) {
ButtonWidget widget = ButtonWidget.Create( game, x, y, 240, 35, text, Docking.Centre, vDocking, titleFont, onClick );
ButtonWidget widget = ButtonWidget.Create( game, x, y, 240, 35, text, Anchor.Centre, vDocking, titleFont, onClick );
widget.GetValue = getter;
widget.SetValue = setter;
return widget;

View File

@ -35,8 +35,8 @@ namespace ClassicalSharp {
MakeKeys( 0, 11, -140 );
MakeKeys( 11, 11, 140 );
buttons[index] = Make( 0, 5, "Back to menu", Docking.BottomOrRight, (g, w) => g.SetNewScreen( new PauseScreen( g ) ) );
statusWidget = TextWidget.Create( game, 0, 150, "", Docking.Centre, Docking.Centre, regularFont );
buttons[index] = Make( 0, 5, "Back to menu", Anchor.BottomOrRight, (g, w) => g.SetNewScreen( new PauseScreen( g ) ) );
statusWidget = TextWidget.Create( game, 0, 150, "", Anchor.Centre, Anchor.Centre, regularFont );
}
int index;
@ -47,7 +47,7 @@ namespace ClassicalSharp {
string text = descriptions[start + i] + ": " + keyNames[(int)game.Keys[mapping]];
buttons[index++] = ButtonWidget.Create( game, x, y, 240, 25, text,
Docking.Centre, Docking.Centre, keyFont, OnWidgetClick );
Anchor.Centre, Anchor.Centre, keyFont, OnWidgetClick );
y += 30;
}
}
@ -59,7 +59,7 @@ namespace ClassicalSharp {
statusWidget.Dispose();
string text = "Press new key binding for " + descriptions[index] + ":";
statusWidget = TextWidget.Create( game, 0, 150, text, Docking.Centre, Docking.Centre, regularFont );
statusWidget = TextWidget.Create( game, 0, 150, text, Anchor.Centre, Anchor.Centre, regularFont );
}
public override bool HandlesKeyDown( Key key ) {
@ -86,8 +86,8 @@ namespace ClassicalSharp {
return true;
}
ButtonWidget Make( int x, int y, string text, Docking vDocking, Action<Game, ButtonWidget> onClick ) {
return ButtonWidget.Create( game, x, y, 240, 35, text, Docking.Centre, vDocking, keyFont, onClick );
ButtonWidget Make( int x, int y, string text, Anchor vDocking, Action<Game, ButtonWidget> onClick ) {
return ButtonWidget.Create( game, x, y, 240, 35, text, Anchor.Centre, vDocking, keyFont, onClick );
}
public override void Dispose() {

View File

@ -89,7 +89,7 @@ namespace ClassicalSharp {
descWidget.Dispose();
string text = widget.Text + ": " + widget.GetValue( game );
descWidget = TextWidget.Create( game, 0, 100, text, Docking.Centre, Docking.Centre, regularFont );
descWidget = TextWidget.Create( game, 0, 100, text, Anchor.Centre, Anchor.Centre, regularFont );
}
protected void OnWidgetClick( Game game, ButtonWidget widget ) {
@ -112,10 +112,10 @@ namespace ClassicalSharp {
targetWidget = selectedWidget;
inputWidget = MenuInputWidget.Create( game, 0, 150, 400, 25, widget.GetValue( game ),
Docking.Centre, Docking.Centre, regularFont, titleFont,
Anchor.Centre, Anchor.Centre, regularFont, titleFont,
hintFont, validator );
buttons[okayIndex] = ButtonWidget.Create( game, 240, 150, 30, 30, "OK",
Docking.Centre, Docking.Centre, titleFont, OnWidgetClick );
Anchor.Centre, Anchor.Centre, titleFont, OnWidgetClick );
UpdateDescription( targetWidget );
}

View File

@ -47,7 +47,7 @@ namespace ClassicalSharp {
if( button != MouseButton.Left ) return false;
for( int i = 0; i < buttons.Length; i++ ) {
ButtonWidget widget = buttons[i];
if( widget != null && widget.ContainsPoint( mouseX, mouseY ) ) {
if( widget != null && widget.Bounds.Contains( mouseX, mouseY ) ) {
if( widget.OnClick != null )
widget.OnClick( game, widget );
return true;
@ -64,7 +64,7 @@ namespace ClassicalSharp {
for( int i = 0; i < buttons.Length; i++ ) {
ButtonWidget widget = buttons[i];
if( widget != null && widget.ContainsPoint( mouseX, mouseY ) ) {
if( widget != null && widget.Bounds.Contains( mouseX, mouseY ) ) {
widget.Active = true;
WidgetSelected( widget );
return true;

View File

@ -14,34 +14,34 @@ namespace ClassicalSharp {
INetworkProcessor network = game.Network;
buttons = new ButtonWidget[] {
Make( -140, -50, "Show FPS", Docking.Centre, OnWidgetClick,
Make( -140, -50, "Show FPS", Anchor.Centre, OnWidgetClick,
g => g.ShowFPS ? "yes" : "no",
(g, v) => g.ShowFPS = v == "yes" ),
Make( -140, 0, "View distance", Docking.Centre, OnWidgetClick,
Make( -140, 0, "View distance", Anchor.Centre, OnWidgetClick,
g => g.ViewDistance.ToString(),
(g, v) => g.SetViewDistance( Int32.Parse( v ) ) ),
Make( -140, 50, "VSync active", Docking.Centre, OnWidgetClick,
Make( -140, 50, "VSync active", Anchor.Centre, OnWidgetClick,
g => g.VSync ? "yes" : "no",
(g, v) => g.Graphics.SetVSync( g, v == "yes" ) ),
Make( 140, -50, "Mouse sensitivity", Docking.Centre, OnWidgetClick,
Make( 140, -50, "Mouse sensitivity", Anchor.Centre, OnWidgetClick,
g => g.MouseSensitivity.ToString(),
(g, v) => { g.MouseSensitivity = Int32.Parse( v );
Options.Set( OptionsKey.Sensitivity, v ); } ),
Make( 140, 0, "Chat font size", Docking.Centre, OnWidgetClick,
Make( 140, 0, "Chat font size", Anchor.Centre, OnWidgetClick,
g => g.Chat.FontSize.ToString(),
(g, v) => { g.Chat.FontSize = Int32.Parse( v );
Options.Set( OptionsKey.FontSize, v ); } ),
!network.IsSinglePlayer ? null :
Make( 140, 50, "Singleplayer physics", Docking.Centre, OnWidgetClick,
Make( 140, 50, "Singleplayer physics", Anchor.Centre, OnWidgetClick,
g => ((SinglePlayerServer)network).physics.Enabled ? "yes" : "no",
(g, v) => ((SinglePlayerServer)network).physics.Enabled = (v == "yes") ),
Make( 0, 5, "Back to menu", Docking.BottomOrRight,
Make( 0, 5, "Back to menu", Anchor.BottomOrRight,
(g, w) => g.SetNewScreen( new PauseScreen( g ) ), null, null ),
null,
};
@ -56,9 +56,9 @@ namespace ClassicalSharp {
okayIndex = buttons.Length - 1;
}
ButtonWidget Make( int x, int y, string text, Docking vDocking, Action<Game, ButtonWidget> onClick,
ButtonWidget Make( int x, int y, string text, Anchor vDocking, Action<Game, ButtonWidget> onClick,
Func<Game, string> getter, Action<Game, string> setter ) {
ButtonWidget widget = ButtonWidget.Create( game, x, y, 240, 35, text, Docking.Centre, vDocking, titleFont, onClick );
ButtonWidget widget = ButtonWidget.Create( game, x, y, 240, 35, text, Anchor.Centre, vDocking, titleFont, onClick );
widget.GetValue = getter;
widget.SetValue = setter;
return widget;

View File

@ -19,18 +19,18 @@ namespace ClassicalSharp {
public override void Init() {
titleFont = new Font( "Arial", 16, FontStyle.Bold );
buttons = new ButtonWidget[] {
Make( 0, -100, "Options", Docking.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ),
Make( 0, -50, "Environment settings", Docking.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ),
Make( 0, 0, "Key mappings", Docking.Centre, (g, w) => g.SetNewScreen( new KeyMappingsScreen( g ) ) ),
Make( 0, 50, "Save level", Docking.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ),
Make( 0, -100, "Options", Anchor.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ),
Make( 0, -50, "Environment settings", Anchor.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ),
Make( 0, 0, "Key mappings", Anchor.Centre, (g, w) => g.SetNewScreen( new KeyMappingsScreen( g ) ) ),
Make( 0, 50, "Save level", Anchor.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ),
// TODO: singleplayer Make( 0, 50, "Load/Save/Gen level", Docking.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ),
Make( 0, 55, "Back to game", Docking.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ),
Make( 0, 5, "Quit game", Docking.BottomOrRight, (g, w) => g.Exit() ),
Make( 0, 55, "Back to game", Anchor.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ),
Make( 0, 5, "Quit game", Anchor.BottomOrRight, (g, w) => g.Exit() ),
};
}
ButtonWidget Make( int x, int y, string text, Docking vDocking, Action<Game, ButtonWidget> onClick ) {
return ButtonWidget.Create( game, x, y, 240, 35, text, Docking.Centre, vDocking, titleFont, onClick );
ButtonWidget Make( int x, int y, string text, Anchor vDocking, Action<Game, ButtonWidget> onClick ) {
return ButtonWidget.Create( game, x, y, 240, 35, text, Anchor.Centre, vDocking, titleFont, onClick );
}
public override bool HandlesKeyDown( Key key ) {

View File

@ -52,13 +52,13 @@ namespace ClassicalSharp {
hintFont = new Font( "Arial", 14, FontStyle.Italic );
inputWidget = MenuInputWidget.Create(
game, -30, 50, 500, 25, "", Docking.Centre, Docking.Centre,
game, -30, 50, 500, 25, "", Anchor.Centre, Anchor.Centre,
regularFont, titleFont, hintFont, new PathValidator() );
buttons = new [] {
ButtonWidget.Create( game, 260, 50, 60, 30, "Save", Docking.Centre,
Docking.Centre, titleFont, OkButtonClick ),
ButtonWidget.Create( game, 0, 5, 240, 35, "Back to menu", Docking.Centre, Docking.BottomOrRight,
ButtonWidget.Create( game, 260, 50, 60, 30, "Save", Anchor.Centre,
Anchor.Centre, titleFont, OkButtonClick ),
ButtonWidget.Create( game, 0, 5, 240, 35, "Back to menu", Anchor.Centre, Anchor.BottomOrRight,
titleFont, (g, w) => g.SetNewScreen( new PauseScreen( g ) ) ),
};
}
@ -110,9 +110,9 @@ namespace ClassicalSharp {
game.SetNewScreen( new PauseScreen( game ) );
}
void MakeDescWidget( string text) {
void MakeDescWidget( string text ) {
DisposeDescWidget();
descWidget = TextWidget.Create( game, 0, 90, text, Docking.Centre, Docking.Centre, regularFont );
descWidget = TextWidget.Create( game, 0, 90, text, Anchor.Centre, Anchor.Centre, regularFont );
}
void DisposeDescWidget() {

View File

@ -67,7 +67,8 @@ namespace ClassicalSharp {
chat.OnResize( oldWidth, oldHeight, width, height );
hotbar.OnResize( oldWidth, oldHeight, width, height );
if( playerList != null ) {
playerList.OnResize( oldWidth, oldHeight, width, height );
int deltaX = CalcDelta( width, oldWidth, Anchor.Centre );
playerList.MoveTo( playerList.X + deltaX, height / 4 );
}
}
@ -102,6 +103,7 @@ namespace ClassicalSharp {
playerList = new NormalPlayerListWidget( game, playerFont );
}
playerList.Init();
playerList.MoveTo( playerList.X, game.Height / 4 );
}
}
if( chat.HandlesKeyDown( key ) ) {

View File

@ -1,61 +1,21 @@
using System;
using ClassicalSharp.GraphicsAPI;
using OpenTK.Input;
namespace ClassicalSharp {
public abstract class Screen : IDisposable {
protected Game game;
protected IGraphicsApi graphicsApi;
public Screen( Game game ) {
this.game = game;
graphicsApi = game.Graphics;
}
public virtual bool HandlesAllInput { get; protected set; }
public abstract void Init();
public abstract void Render( double delta );
public abstract void Dispose();
public virtual void OnResize( int oldWidth, int oldHeight, int width, int height ) {
}
public virtual bool HandlesKeyDown( Key key ) {
return false;
}
public virtual bool HandlesKeyPress( char key ) {
return false;
}
public virtual bool HandlesKeyUp( Key key ) {
return false;
}
public virtual bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) {
return false;
}
public virtual bool HandlesMouseMove( int mouseX, int mouseY ) {
return false;
}
public virtual bool HandlesMouseScroll( int delta ) {
return false;
}
public virtual bool HandlesMouseUp( int mouseX, int mouseY, MouseButton button ) {
return false;
}
/// <summary> Whether the screen completely covers the world behind it. </summary>
public virtual bool BlocksWorld {
get { return false; }
}
}
}
namespace ClassicalSharp {
/// <summary> Represents a container of widgets and other 2D elements. </summary>
/// <remarks> May cover the entire game window. </remarks>
public abstract class Screen : GuiElement {
public Screen( Game game ) : base( game ) {
}
/// <summary> Whether this screen handles all mouse and keyboard input. </summary>
/// <remarks> This prevents the client from interacting with the world. </remarks>
public virtual bool HandlesAllInput { get; protected set; }
/// <summary> Whether this screen completely and opaquely covers the game world behind it. </summary>
public virtual bool BlocksWorld {
get { return false; }
}
}
}

View File

@ -8,8 +8,8 @@ namespace ClassicalSharp {
public sealed class BlockHotbarWidget : Widget {
public BlockHotbarWidget( Game game ) : base( game ) {
HorizontalDocking = Docking.Centre;
VerticalDocking = Docking.BottomOrRight;
HorizontalAnchor = Anchor.Centre;
VerticalAnchor = Anchor.BottomOrRight;
hotbarCount = game.Inventory.Hotbar.Length;
}

View File

@ -10,12 +10,12 @@ namespace ClassicalSharp {
this.font = font;
}
public static ButtonWidget Create( Game game, int x, int y, int width, int height, string text, Docking horizontal,
Docking vertical, Font font, Action<Game, ButtonWidget> onClick ) {
public static ButtonWidget Create( Game game, int x, int y, int width, int height, string text, Anchor horizontal,
Anchor vertical, Font font, Action<Game, ButtonWidget> onClick ) {
ButtonWidget widget = new ButtonWidget( game, font );
widget.Init();
widget.HorizontalDocking = horizontal;
widget.VerticalDocking = vertical;
widget.HorizontalAnchor = horizontal;
widget.VerticalAnchor = vertical;
widget.XOffset = x;
widget.YOffset = y;
widget.DesiredMaxWidth = width;
@ -47,8 +47,8 @@ namespace ClassicalSharp {
Height = defaultHeight;
} else {
MakeTexture( text );
X = texture.X1 = CalcOffset( game.Width, texture.Width, XOffset, HorizontalDocking );
Y = texture.Y1 = CalcOffset( game.Height, texture.Height, YOffset, VerticalDocking );
X = texture.X1 = CalcOffset( game.Width, texture.Width, XOffset, HorizontalAnchor );
Y = texture.Y1 = CalcOffset( game.Height, texture.Height, YOffset, VerticalAnchor );
Height = texture.Height;
}
Width = texture.Width;

View File

@ -83,7 +83,7 @@ namespace ClassicalSharp {
RemoveItemAt( textures, i );
RemoveItemAt( info, i );
namesCount--;
columns = (int)Math.Ceiling( (double)namesCount / namesPerColumn );
columns = Utils.CeilDiv( namesCount, namesPerColumn );
SortPlayerInfo();
return;
}
@ -92,7 +92,7 @@ namespace ClassicalSharp {
void PlayerListInfoAdded( object sender, IdEventArgs e ) {
AddPlayerInfo( game.CpePlayersList[e.Id], -1 );
columns = (int)Math.Ceiling( (double)namesCount / namesPerColumn );
columns = Utils.CeilDiv( namesCount, namesPerColumn );
SortPlayerInfo();
}

View File

@ -7,20 +7,20 @@ namespace ClassicalSharp {
public sealed class MenuInputWidget : Widget {
public MenuInputWidget( Game game, Font font, Font boldFont, Font hintFont ) : base( game ) {
HorizontalDocking = Docking.LeftOrTop;
VerticalDocking = Docking.BottomOrRight;
HorizontalAnchor = Anchor.LeftOrTop;
VerticalAnchor = Anchor.BottomOrRight;
this.font = font;
this.boldFont = boldFont;
this.hintFont = hintFont;
chatInputText = new StringBuffer( 64 );
}
public static MenuInputWidget Create( Game game, int x, int y, int width, int height, string text, Docking horizontal,
Docking vertical, Font font, Font tildeFont, Font hintFont, MenuInputValidator validator ) {
public static MenuInputWidget Create( Game game, int x, int y, int width, int height, string text, Anchor horizontal,
Anchor vertical, Font font, Font tildeFont, Font hintFont, MenuInputValidator validator ) {
MenuInputWidget widget = new MenuInputWidget( game, font, tildeFont, hintFont );
widget.HorizontalDocking = horizontal;
widget.VerticalDocking = vertical;
widget.HorizontalAnchor = horizontal;
widget.VerticalAnchor = vertical;
widget.XOffset = x;
widget.YOffset = y;
widget.DesiredMaxWidth = width;
@ -76,8 +76,8 @@ namespace ClassicalSharp {
}
}
X = CalcOffset( game.Width, size.Width, XOffset, HorizontalDocking );
Y = CalcOffset( game.Height, size.Height, YOffset, VerticalDocking );
X = CalcOffset( game.Width, size.Width, XOffset, HorizontalAnchor );
Y = CalcOffset( game.Height, size.Height, YOffset, VerticalAnchor );
chatCaretTexture.X1 = chatInputTexture.X1 = X;
chatCaretTexture.X1 += textSize.Width;
chatCaretTexture.Y1 = chatInputTexture.Y1 = Y;

View File

@ -36,7 +36,7 @@ namespace ClassicalSharp {
void PlayerSpawned( object sender, IdEventArgs e ) {
Player player = game.Players[e.Id];
AddPlayerInfo( player );
columns = (int)Math.Ceiling( (double)namesCount / namesPerColumn );
columns = Utils.CeilDiv( namesCount, namesPerColumn );
SortPlayerInfo();
}
@ -66,7 +66,7 @@ namespace ClassicalSharp {
RemoveItemAt( info, i );
RemoveItemAt( textures, i );
namesCount--;
columns = (int)Math.Ceiling( (double)namesCount / namesPerColumn );
columns = Utils.CeilDiv( namesCount, namesPerColumn );
SortPlayerInfo();
return;
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Drawing;
namespace ClassicalSharp {
@ -8,8 +7,8 @@ namespace ClassicalSharp {
protected readonly Font font;
public PlayerListWidget( Game game, Font font ) : base( game ) {
HorizontalDocking = Docking.Centre;
VerticalDocking = Docking.Centre;
HorizontalAnchor = Anchor.Centre;
VerticalAnchor = Anchor.Centre;
this.font = font;
}
@ -19,11 +18,11 @@ namespace ClassicalSharp {
protected Texture[] textures = new Texture[256];
protected int columns;
protected int xMin, xMax, yHeight;
protected static FastColour tableCol = new FastColour( 100, 100, 100, 80 );
protected static FastColour tableCol = new FastColour( 20, 20, 20, 220 );
public override void Init() {
CreateInitialPlayerInfo();
columns = (int)Math.Ceiling( (double)namesCount / namesPerColumn );
columns = Utils.CeilDiv( namesCount, namesPerColumn );
SortPlayerInfo();
}
@ -67,9 +66,8 @@ namespace ClassicalSharp {
int maxWidth = 0;
int maxIndex = Math.Min( namesCount, i + namesPerColumn );
for( ; i < maxIndex; i++ ) {
for( ; i < maxIndex; i++ )
maxWidth = Math.Max( maxWidth, textures[i].Width );
}
return maxWidth;
}
@ -78,9 +76,8 @@ namespace ClassicalSharp {
int total = 0;
int maxIndex = Math.Min( namesCount, i + namesPerColumn );
for( ; i < maxIndex; i++ ) {
for( ; i < maxIndex; i++ )
total += textures[i].Height;
}
return total;
}
@ -90,24 +87,19 @@ namespace ClassicalSharp {
for( ; i < maxIndex; i++ ) {
Texture tex = textures[i];
tex.X1 = x;
tex.Y1 = y;
tex.X1 = x; tex.Y1 = y;
y += tex.Height;
textures[i] = tex;
}
}
public override void MoveTo( int newX, int newY ) {
int deltaX = newX - X;
int deltaY = newY - Y;
int diffX = newX - X; int diffY = newY - Y;
for( int i = 0; i < namesCount; i++ ) {
Texture tex = textures[i];
tex.X1 += deltaX;
tex.Y1 += deltaY;
textures[i] = tex;
textures[i].X1 += diffX;
textures[i].Y1 += diffY;
}
X = newX;
Y = newY;
X = newX; Y = newY;
}
protected abstract void CreateInitialPlayerInfo();
@ -131,7 +123,7 @@ namespace ClassicalSharp {
int offset = 0;
if( columns % 2 != 0 ) {
// For an odd number of columns, the middle column is centred.
offset = ( GetColumnWidth( midCol ) + 1 ) / 2; // ceiling divide by 2
offset = Utils.CeilDiv( GetColumnWidth( midCol ), 2 );
}
int x = centreX - offset;

View File

@ -31,7 +31,7 @@ namespace ClassicalSharp {
if( !String.IsNullOrEmpty( text ) ) {
DrawTextArgs args = new DrawTextArgs( text, true );
Texture tex = game.Drawer2D.MakeTextTexture( font, 0, 0, ref args );
tex.X1 = CalcOffset( game.Width, tex.Width, XOffset, HorizontalDocking );
tex.X1 = CalcOffset( game.Width, tex.Width, XOffset, HorizontalAnchor );
tex.Y1 = CalcY( index, tex.Height );
textures[index] = tex;
} else {
@ -56,7 +56,7 @@ namespace ClassicalSharp {
int y = 0;
int deltaY = newHeight - textures[index].Height;
if( VerticalDocking == Docking.LeftOrTop ) {
if( VerticalAnchor == Anchor.LeftOrTop ) {
y = Y;
for( int i = 0; i < index; i++ ) {
y += textures[i].Height;
@ -91,7 +91,7 @@ namespace ClassicalSharp {
for( int i = 0; i < textures.Length; i++ ) {
Height += textures[i].Height;
}
Y = CalcOffset( game.Height, Height, YOffset, VerticalDocking );
Y = CalcOffset( game.Height, Height, YOffset, VerticalAnchor );
Width = 0;
for( int i = 0; i < textures.Length; i++ ) {
@ -100,7 +100,7 @@ namespace ClassicalSharp {
Width = width;
}
}
X = CalcOffset( game.Width, Width, XOffset, HorizontalDocking );
X = CalcOffset( game.Width, Width, XOffset, HorizontalAnchor );
}
public override void Render( double delta ) {

View File

@ -8,8 +8,8 @@ namespace ClassicalSharp {
public sealed class TextInputWidget : Widget {
public TextInputWidget( Game game, Font font, Font boldFont ) : base( game ) {
HorizontalDocking = Docking.LeftOrTop;
VerticalDocking = Docking.BottomOrRight;
HorizontalAnchor = Anchor.LeftOrTop;
VerticalAnchor = Anchor.BottomOrRight;
typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1.
this.font = font;
this.boldFont = boldFont;

View File

@ -10,11 +10,11 @@ namespace ClassicalSharp {
this.font = font;
}
public static TextWidget Create( Game game, int x, int y, string text, Docking horizontal, Docking vertical, Font font ) {
public static TextWidget Create( Game game, int x, int y, string text, Anchor horizontal, Anchor vertical, Font font ) {
TextWidget widget = new TextWidget( game, font );
widget.Init();
widget.HorizontalDocking = horizontal;
widget.VerticalDocking = vertical;
widget.HorizontalAnchor = horizontal;
widget.VerticalAnchor = vertical;
widget.XOffset = x;
widget.YOffset = y;
widget.SetText( text );
@ -39,8 +39,8 @@ namespace ClassicalSharp {
} else {
DrawTextArgs args = new DrawTextArgs( text, true );
texture = game.Drawer2D.MakeTextTexture( font, 0, 0, ref args );
X = texture.X1 = CalcOffset( game.Width, texture.Width, XOffset, HorizontalDocking );
Y = texture.Y1 = CalcOffset( game.Height, texture.Height, YOffset, VerticalDocking );
X = texture.X1 = CalcOffset( game.Width, texture.Width, XOffset, HorizontalAnchor );
Y = texture.Y1 = CalcOffset( game.Height, texture.Height, YOffset, VerticalAnchor );
Height = texture.Height;
}
Width = texture.Width;

View File

@ -1,125 +1,63 @@
using System;
using System.Drawing;
using ClassicalSharp.GraphicsAPI;
using OpenTK.Input;
namespace ClassicalSharp {
public abstract class Widget : IDisposable {
protected internal Game game;
protected IGraphicsApi graphicsApi;
public Widget( Game game ) {
this.game = game;
graphicsApi = game.Graphics;
HorizontalDocking = Docking.LeftOrTop;
VerticalDocking = Docking.LeftOrTop;
}
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Docking HorizontalDocking { get; set; }
public Docking VerticalDocking { get; set; }
public Size Size {
get { return new Size( Width, Height ); }
}
public Point TopLeft {
get { return new Point( X, Y ); }
}
public Point BottomRight {
get { return new Point( X + Width, Y + Height ); }
}
public Rectangle Bounds {
get { return new Rectangle( X, Y, Width, Height ); }
}
public bool ContainsPoint( int x, int y ) {
return Bounds.Contains( x, y );
}
public bool ContainsPoint( Point point ) {
return Bounds.Contains( point );
}
public bool ContainsRectangle( Rectangle rect ) {
return Bounds.Contains( rect );
}
public bool IntersectsRectangle( Rectangle rect ) {
return Bounds.IntersectsWith( rect );
}
public virtual bool HandlesKeyDown( Key key ) {
return false;
}
public virtual bool HandlesKeyPress( char key ) {
return false;
}
public virtual bool HandlesKeyUp( Key key ) {
return false;
}
public virtual bool HandleMouseClick( int mouseX, int mouseY ) {
return false;
}
public virtual bool HandleMouseHover( int mouseX, int mouseY ) {
return false;
}
public virtual bool HandleMouseLeave( int mouseX, int mouseY ) {
return false;
}
public virtual bool HandleMouseDeClick( int mouseX, int mouseY ) {
return false;
}
public abstract void Init();
public abstract void Render( double delta );
public abstract void Dispose();
public virtual void MoveTo( int newX, int newY ) {
X = newX;
Y = newY;
}
public virtual void OnResize( int oldWidth, int oldHeight, int width, int height ) {
int deltaX = CalcDelta( width, oldWidth, HorizontalDocking );
int deltaY = CalcDelta( height, oldHeight, VerticalDocking );
MoveTo( X + deltaX, Y + deltaY );
}
protected static int CalcDelta( int newVal, int oldVal, Docking mode ) {
return CalcOffset( newVal, oldVal, 0, mode );
}
protected static int CalcOffset( int axisSize, int elemSize, int offset, Docking mode ) {
if( mode == Docking.LeftOrTop ) return offset;
if( mode == Docking.BottomOrRight) return axisSize - elemSize - offset;
return ( axisSize - elemSize ) / 2 + offset;
}
}
public enum Docking {
LeftOrTop = 0,
Centre = 1,
BottomOrRight = 2,
}
}
using System;
using System.Drawing;
namespace ClassicalSharp {
/// <summary> Represents an individual 2D gui component. </summary>
public abstract class Widget : GuiElement {
public Widget( Game game ) : base( game ) {
HorizontalAnchor = Anchor.LeftOrTop;
VerticalAnchor = Anchor.LeftOrTop;
}
/// <summary> Horizontal coordinate of top left corner in window space. </summary>
public int X;
/// <summary> Vertical coordinate of top left corner in window space. </summary>
public int Y;
/// <summary> Horizontal length of widget's bounds in window space. </summary>
public int Width;
/// <summary> Vertical length of widget's bounds in window space. </summary>
public int Height;
/// <summary> Specifies the horizontal reference point for when the widget is resized. </summary>
public Anchor HorizontalAnchor;
/// <summary> Specifies the vertical reference point for when the widget is resized. </summary>
public Anchor VerticalAnchor;
/// <summary> Width and height of widget in window space. </summary>
public Size Size {
get { return new Size( Width, Height ); }
}
/// <summary> Coordinate of top left corner of widget's bounds in window space. </summary>
public Point TopLeft {
get { return new Point( X, Y ); }
}
/// <summary> Coordinate of bottom right corner of widget's bounds in window space. </summary>
public Point BottomRight {
get { return new Point( X + Width, Y + Height ); }
}
/// <summary> Specifies the boundaries of the widget in window space. </summary>
public Rectangle Bounds {
get { return new Rectangle( X, Y, Width, Height ); }
}
/// <summary> Moves the widget to the specified window space coordinates. </summary>
public virtual void MoveTo( int newX, int newY ) {
X = newX; Y = newY;
}
public override void OnResize( int oldWidth, int oldHeight, int width, int height ) {
int deltaX = CalcDelta( width, oldWidth, HorizontalAnchor );
int deltaY = CalcDelta( height, oldHeight, VerticalAnchor );
MoveTo( X + deltaX, Y + deltaY );
}
}
}

View File

@ -65,6 +65,7 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="2D\GuiElement.cs" />
<Compile Include="2D\IsometricBlockDrawer.cs" />
<Compile Include="2D\Drawing\DrawTextArgs.cs" />
<Compile Include="2D\Drawing\GdiPlusDrawer2D.cs" />

View File

@ -194,7 +194,7 @@ namespace ClassicalSharp {
imageCheckAccumulator += e.Time;
ticksAccumulator += e.Time;
Vertices = 0;
if( !Focused && ( activeScreen == null || !activeScreen.HandlesAllInput ) ) {
if( !Focused && (activeScreen == null || !activeScreen.HandlesAllInput) ) {
SetNewScreen( new PauseScreen( this ) );
}

View File

@ -53,6 +53,8 @@ namespace Launcher
this.txtCCSearch = new System.Windows.Forms.TextBox();
this.lblCCSearch = new System.Windows.Forms.Label();
this.tabMinecraftNet = new System.Windows.Forms.TabPage();
this.lblMCdead2 = new System.Windows.Forms.Label();
this.lblMCdead = new System.Windows.Forms.Label();
this.tabDC = new System.Windows.Forms.TabPage();
this.lblDChint = new System.Windows.Forms.Label();
this.txtDCmppass = new System.Windows.Forms.TextBox();
@ -66,8 +68,6 @@ namespace Launcher
this.lblDCaddress = new System.Windows.Forms.Label();
this.lblDCuser = new System.Windows.Forms.Label();
this.tabs = new System.Windows.Forms.TabControl();
this.lblMCdead = new System.Windows.Forms.Label();
this.lblMCdead2 = new System.Windows.Forms.Label();
this.tabClassicubeNet.SuspendLayout();
this.tabCC.SuspendLayout();
this.tabCCSignIn.SuspendLayout();
@ -318,6 +318,24 @@ namespace Launcher
this.tabMinecraftNet.Text = "minecraft.net";
this.tabMinecraftNet.UseVisualStyleBackColor = true;
//
// lblMCdead2
//
this.lblMCdead2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblMCdead2.Location = new System.Drawing.Point(20, 62);
this.lblMCdead2.Name = "lblMCdead2";
this.lblMCdead2.Size = new System.Drawing.Size(300, 41);
this.lblMCdead2.TabIndex = 1;
this.lblMCdead2.Text = "But don\'t despair! You can sign up for an account at classicube.net.";
//
// lblMCdead
//
this.lblMCdead.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblMCdead.Location = new System.Drawing.Point(20, 20);
this.lblMCdead.Name = "lblMCdead";
this.lblMCdead.Size = new System.Drawing.Size(380, 23);
this.lblMCdead.TabIndex = 0;
this.lblMCdead.Text = "Classic has been removed from minecraft.net.";
//
// tabDC
//
this.tabDC.Controls.Add(this.lblDChint);
@ -450,24 +468,6 @@ namespace Launcher
this.tabs.Size = new System.Drawing.Size(482, 466);
this.tabs.TabIndex = 0;
//
// lblMCdead
//
this.lblMCdead.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblMCdead.Location = new System.Drawing.Point(20, 20);
this.lblMCdead.Name = "lblMCdead";
this.lblMCdead.Size = new System.Drawing.Size(380, 23);
this.lblMCdead.TabIndex = 0;
this.lblMCdead.Text = "Classic has been removed from minecraft.net.";
//
// lblMCdead2
//
this.lblMCdead2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblMCdead2.Location = new System.Drawing.Point(20, 62);
this.lblMCdead2.Name = "lblMCdead2";
this.lblMCdead2.Size = new System.Drawing.Size(300, 41);
this.lblMCdead2.TabIndex = 1;
this.lblMCdead2.Text = "But don\'t despair! You can sign up for an account at classicube.net.";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);