mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-11 08:36:38 -04:00
Can configure most keys now. (excluding pause and function keys)
This commit is contained in:
parent
908c3c4ade
commit
1ed45db594
@ -9,14 +9,16 @@ namespace ClassicalSharp {
|
||||
public PauseScreen( Game window ) : base( window ) {
|
||||
}
|
||||
|
||||
TextWidget controlsWidget, gameWidget, exitWidget;
|
||||
TextWidget[] keysLeft, keysRight;
|
||||
TextWidget controlsWidget, gameWidget, exitWidget, keyStatusWidget;
|
||||
KeyMapWidget[] keysLeft, keysRight;
|
||||
KeyMapWidget widgetToChange;
|
||||
|
||||
public override void Render( double delta ) {
|
||||
GraphicsApi.Draw2DQuad( 0, 0, Window.Width, Window.Height, new FastColour( 255, 255, 255, 100 ) );
|
||||
controlsWidget.Render( delta );
|
||||
gameWidget.Render( delta );
|
||||
exitWidget.Render( delta );
|
||||
keyStatusWidget.Render( delta );
|
||||
for( int i = 0; i < keysLeft.Length; i++ ) {
|
||||
keysLeft[i].Render( delta );
|
||||
}
|
||||
@ -26,9 +28,10 @@ namespace ClassicalSharp {
|
||||
}
|
||||
|
||||
public override void Init() {
|
||||
controlsWidget = CreateTextWidget( 0, 30, "&eControls list", Docking.LeftOrTop, 16, FontStyle.Bold );
|
||||
gameWidget = CreateTextWidget( 0, -60, "&eBack to game", Docking.BottomOrRight, 16, FontStyle.Bold );
|
||||
exitWidget = CreateTextWidget( 0, -10, "&eExit", Docking.BottomOrRight, 16, FontStyle.Bold );
|
||||
controlsWidget = CreateTextWidget( 0, 30, "&eControls list", Docking.Centre, Docking.LeftOrTop, 16, FontStyle.Bold );
|
||||
keyStatusWidget = CreateTextWidget( 0, -80, "", Docking.Centre, Docking.BottomOrRight, 13, FontStyle.Italic );
|
||||
gameWidget = CreateTextWidget( 0, -50, "&eBack to game", Docking.Centre, Docking.BottomOrRight, 16, FontStyle.Bold );
|
||||
exitWidget = CreateTextWidget( 0, -10, "&eExit", Docking.Centre, Docking.BottomOrRight, 16, FontStyle.Bold );
|
||||
|
||||
KeyMapping[] mappingsLeft = { KeyMapping.Forward, KeyMapping.Back, KeyMapping.Left, KeyMapping.Right,
|
||||
KeyMapping.Jump, KeyMapping.Respawn, KeyMapping.SetSpawn, KeyMapping.OpenChat, KeyMapping.SendChat,
|
||||
@ -45,28 +48,30 @@ namespace ClassicalSharp {
|
||||
MakeKeysRight( mappingsRight, descriptionsRight );
|
||||
}
|
||||
|
||||
int leftEnd;
|
||||
void MakeKeysLeft( KeyMapping[] mappings, string[] descriptions ) {
|
||||
int startY = controlsWidget.BottomRight.Y + 10;
|
||||
keysLeft = new TextWidget[mappings.Length];
|
||||
keysLeft = new KeyMapWidget[mappings.Length];
|
||||
for( int i = 0; i < keysLeft.Length; i++ ) {
|
||||
string text = descriptions[i] + ": " + Window.Keys[mappings[i]];
|
||||
TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, 14, FontStyle.Bold );
|
||||
widget.XOffset = -widget.Width / 2 - 20;
|
||||
TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, Docking.LeftOrTop, 14, FontStyle.Bold );
|
||||
widget.XOffset = 10;
|
||||
widget.MoveTo( widget.X + widget.XOffset, widget.Y );
|
||||
keysLeft[i] = widget;
|
||||
keysLeft[i] = new KeyMapWidget( widget, mappings[i], descriptions[i] );
|
||||
startY += widget.Height + 5;
|
||||
}
|
||||
leftEnd = CalculateMaxWidth( keysLeft );
|
||||
}
|
||||
|
||||
void MakeKeysRight( KeyMapping[] mappings, string[] descriptions ) {
|
||||
int startY = controlsWidget.BottomRight.Y + 10;
|
||||
keysRight = new TextWidget[mappings.Length];
|
||||
keysRight = new KeyMapWidget[mappings.Length];
|
||||
for( int i = 0; i < keysRight.Length; i++ ) {
|
||||
string text = descriptions[i] + ": " + Window.Keys[mappings[i]];
|
||||
TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, 14, FontStyle.Bold );
|
||||
widget.XOffset = widget.Width / 2 + 20;
|
||||
TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, Docking.LeftOrTop, 14, FontStyle.Bold );
|
||||
widget.XOffset = leftEnd + 30;
|
||||
widget.MoveTo( widget.X + widget.XOffset, widget.Y );
|
||||
keysRight[i] = widget;
|
||||
keysRight[i] = new KeyMapWidget( widget, mappings[i], descriptions[i] );
|
||||
startY += widget.Height + 5;
|
||||
}
|
||||
}
|
||||
@ -83,11 +88,11 @@ namespace ClassicalSharp {
|
||||
}
|
||||
}
|
||||
|
||||
TextWidget CreateTextWidget( int x, int y, string text, Docking vertical, int fontSize, FontStyle style ) {
|
||||
TextWidget CreateTextWidget( int x, int y, string text, Docking horizontal, Docking vertical, int fontSize, FontStyle style ) {
|
||||
TextWidget widget = new TextWidget( Window, fontSize );
|
||||
widget.Style = style;
|
||||
widget.Init();
|
||||
widget.HorizontalDocking = Docking.Centre;
|
||||
widget.HorizontalDocking = horizontal;
|
||||
widget.VerticalDocking = vertical;
|
||||
widget.XOffset = x;
|
||||
widget.YOffset = y;
|
||||
@ -99,6 +104,7 @@ namespace ClassicalSharp {
|
||||
gameWidget.OnResize( oldWidth, oldHeight, width, height );
|
||||
controlsWidget.OnResize( oldWidth, oldHeight, width, height );
|
||||
exitWidget.OnResize( oldWidth, oldHeight, width, height );
|
||||
keyStatusWidget.OnResize( oldWidth, oldHeight, width, height );
|
||||
for( int i = 0; i < keysLeft.Length; i++ ) {
|
||||
keysLeft[i].OnResize( oldWidth, oldHeight, width, height );
|
||||
}
|
||||
@ -111,6 +117,49 @@ namespace ClassicalSharp {
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool HandlesKeyDown( Key key ) {
|
||||
if( widgetToChange != null ) {
|
||||
KeyMapWidget widget = widgetToChange;
|
||||
widgetToChange = null;
|
||||
string reason;
|
||||
if( !Window.Keys.IsKeyOkay( key, out reason ) ) {
|
||||
const string format = "&eFailed to change mapping \"{0}\". &c({1})";
|
||||
keyStatusWidget.SetText( String.Format( format, widget.Description, reason ) );
|
||||
} else {
|
||||
Key oldKey = Window.Keys[widget.Mapping];
|
||||
const string format = "&eChanged mapping \"{0}\" from &7{1} &eto &7{2}&e.";
|
||||
keyStatusWidget.SetText( String.Format( format, widget.Description, oldKey, key ) );
|
||||
Window.Keys[widget.Mapping] = key;
|
||||
widget.Widget.SetText( widget.Description + ": " + key );
|
||||
if( Array.IndexOf( keysLeft, widget ) >= 0 ) {
|
||||
ResizeKeysRight();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ResizeKeysRight() {
|
||||
int newLeftEnd = CalculateMaxWidth( keysLeft );
|
||||
if( newLeftEnd != leftEnd ) {
|
||||
int diff = newLeftEnd - leftEnd;
|
||||
for( int i = 0; i < keysRight.Length; i++ ) {
|
||||
TextWidget textWidget = keysRight[i].Widget;
|
||||
textWidget.XOffset = newLeftEnd + 30;
|
||||
textWidget.MoveTo( textWidget.X + diff, textWidget.Y );
|
||||
}
|
||||
}
|
||||
leftEnd = newLeftEnd;
|
||||
}
|
||||
|
||||
int CalculateMaxWidth( KeyMapWidget[] widgets ) {
|
||||
int maxWidth = 0;
|
||||
for( int i = 0; i < widgets.Length; i++ ) {
|
||||
maxWidth = Math.Max( widgets[i].Widget.Width, maxWidth );
|
||||
}
|
||||
return maxWidth;
|
||||
}
|
||||
|
||||
public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) {
|
||||
if( button != MouseButton.Left ) return false;
|
||||
if( exitWidget.ContainsPoint( mouseX, mouseY ) ) {
|
||||
@ -119,8 +168,59 @@ namespace ClassicalSharp {
|
||||
} else if( gameWidget.ContainsPoint( mouseX, mouseY ) ) {
|
||||
Window.SetNewScreen( new NormalScreen( Window ) );
|
||||
return true;
|
||||
} else if( widgetToChange == null ) {
|
||||
for( int i = 0; i < keysLeft.Length; i++ ) {
|
||||
KeyMapWidget widget = keysLeft[i];
|
||||
if( widget.Widget.ContainsPoint( mouseX, mouseY ) ) {
|
||||
SetWidgetToChange( widget );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for( int i = 0; i < keysRight.Length; i++ ) {
|
||||
KeyMapWidget widget = keysRight[i];
|
||||
if( widget.Widget.ContainsPoint( mouseX, mouseY ) ) {
|
||||
SetWidgetToChange( widget );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SetWidgetToChange( KeyMapWidget widget ) {
|
||||
Key oldKey = Window.Keys[widget.Mapping];
|
||||
if( !Window.Keys.IsLockedKey( oldKey ) ) {
|
||||
const string format = "&ePress new key for \"{0}\".";
|
||||
keyStatusWidget.SetText( String.Format( format, widget.Description ) );
|
||||
widgetToChange = widget;
|
||||
} else {
|
||||
const string format = "&cCannot change mapping of &e\"{0}\".";
|
||||
keyStatusWidget.SetText( String.Format( format, widget.Description ) );
|
||||
}
|
||||
}
|
||||
|
||||
class KeyMapWidget {
|
||||
public TextWidget Widget;
|
||||
public KeyMapping Mapping;
|
||||
public string Description;
|
||||
|
||||
public KeyMapWidget( TextWidget widget, KeyMapping mapping, string desc ) {
|
||||
Widget = widget;
|
||||
Mapping = mapping;
|
||||
Description = desc;
|
||||
}
|
||||
|
||||
public void Render( double delta ) {
|
||||
Widget.Render( delta );
|
||||
}
|
||||
|
||||
public void OnResize( int oldWidth, int oldHeight, int width, int height ) {
|
||||
Widget.OnResize( oldWidth, oldHeight, width, height );
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Widget.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -164,10 +164,6 @@ namespace ClassicalSharp {
|
||||
Key.Tab, Key.H,
|
||||
};
|
||||
|
||||
bool IsLockedKey( Key key ) {
|
||||
return key == Key.Escape || ( key >= Key.F1 && key <= Key.F35 );
|
||||
}
|
||||
|
||||
bool IsReservedKey( Key key ) {
|
||||
return key == Key.Slash || key == Key.BackSpace ||
|
||||
( key >= Key.Insert && key <= Key.End ) ||
|
||||
@ -175,18 +171,18 @@ namespace ClassicalSharp {
|
||||
( key >= Key.Number0 && key <= Key.Number9 ); // block hotbar
|
||||
}
|
||||
|
||||
public bool IsKeyOkay( Key key, out string reason ) {
|
||||
if( IsLockedKey( key ) ) {
|
||||
reason = "Given key mapping cannot be changed.";
|
||||
return false;
|
||||
public bool IsLockedKey( Key key ) {
|
||||
return key == Key.Escape || ( key >= Key.F1 && key <= Key.F35 );
|
||||
}
|
||||
|
||||
public bool IsKeyOkay( Key key, out string reason ) {
|
||||
if( IsReservedKey( key ) ) {
|
||||
reason = "Given key is reserved for gui.";
|
||||
reason = "Given key is reserved for gui";
|
||||
return false;
|
||||
}
|
||||
for( int i = 0; i < Keys.Length; i++ ) {
|
||||
if( Keys[i] == key ) {
|
||||
reason = "Key is already assigned.";
|
||||
reason = "Key is already assigned";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user