Reduce code duplication for classic and normal player list code.

This commit is contained in:
UnknownShadow200 2016-05-29 21:47:46 +10:00
parent c4f1f69cbd
commit 6c3f72c6c8
5 changed files with 90 additions and 167 deletions

View File

@ -1,43 +1,15 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using System.Drawing;
using ClassicalSharp.Entities;
using ClassicalSharp.Events;
namespace ClassicalSharp.Gui {
public sealed class ClassicPlayerListWidget : PlayerListWidget {
public sealed class ClassicPlayerListWidget : NormalPlayerListWidget {
bool extList;
int elemHeight;
ChatTextWidget overview;
static FastColour lightTableCol = new FastColour( 20, 20, 20, 180 );
public ClassicPlayerListWidget( Game game, Font font ) : base( game, font ) {
textures = new Texture[256];
extList = game.Network.UsingExtPlayerList;
}
PlayerInfo[] info = new PlayerInfo[256];
class PlayerInfo {
public string Name, ColouredName;
public byte Id;
public PlayerInfo( TabListEntry p ) {
ColouredName = p.PlayerName;
Name = Utils.StripColours( p.PlayerName );
Id = p.NameId;
}
}
public override string GetNameUnder( int mouseX, int mouseY ) {
for( int i = 0; i < namesCount; i++ ) {
Texture texture = textures[i];
if( texture.IsValid && texture.Bounds.Contains( mouseX, mouseY ) )
return Utils.StripColours( info[i].Name );
}
return null;
}
protected override void OnSort() {
@ -82,81 +54,11 @@ namespace ClassicalSharp.Gui {
Anchor.Centre, Anchor.Centre, font );
base.Init();
game.EntityEvents.TabListEntryAdded += TabyEntryAdded;
game.EntityEvents.TabListEntryRemoved += TabEntryRemoved;
game.EntityEvents.TabListEntryChanged += TabEntryChanged;
}
public override void Dispose() {
base.Dispose();
overview.Dispose();
game.EntityEvents.TabListEntryAdded -= TabyEntryAdded;
game.EntityEvents.TabListEntryChanged -= TabEntryChanged;
game.EntityEvents.TabListEntryRemoved -= TabEntryRemoved;
}
protected override void CreateInitialPlayerInfo() {
TabListEntry[] entries = game.TabList.Entries;
for( int i = 0; i < entries.Length; i++ ) {
TabListEntry e = entries[i];
if( e != null )
AddPlayerInfo( new PlayerInfo( e ), -1 );
}
}
void AddPlayerInfo( PlayerInfo pInfo, int index ) {
DrawTextArgs args = new DrawTextArgs( pInfo.ColouredName, font, false );
Texture tex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
game.Drawer2D.ReducePadding( ref tex, Utils.Floor( font.Size ), 3 );
if( index < 0 ) {
info[namesCount] = pInfo;
textures[namesCount] = tex;
namesCount++;
} else {
info[index] = pInfo;
textures[index] = tex;
}
}
void TabyEntryAdded( object sender, IdEventArgs e ) {
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), -1 );
SortPlayerInfo();
}
void TabEntryChanged( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.Id != e.Id ) continue;
Texture tex = textures[i];
api.DeleteTexture( ref tex );
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), i );
SortPlayerInfo();
return;
}
}
void TabEntryRemoved( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.Id == e.Id ) {
RemoveInfoAt( info, i );
return;
}
}
}
PlayerInfoComparer comparer = new PlayerInfoComparer();
class PlayerInfoComparer : IComparer<PlayerInfo> {
public int Compare( PlayerInfo x, PlayerInfo y ) {
return x.Name.CompareTo( y.Name );
}
}
protected override void SortInfoList() {
Array.Sort( info, textures, 0, namesCount, comparer );
}
}
}

View File

@ -1,54 +1,28 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using System.Drawing;
using ClassicalSharp.Entities;
using ClassicalSharp.Events;
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using System.Drawing;
using ClassicalSharp.Entities;
using ClassicalSharp.Events;
namespace ClassicalSharp.Gui {
public sealed class NormalPlayerListWidget : PlayerListWidget {
public class NormalPlayerListWidget : PlayerListWidget {
public NormalPlayerListWidget( Game game, Font font ) : base( game, font ) {
textures = new Texture[256];
}
PlayerInfo[] info = new PlayerInfo[256];
PlayerInfo[] info = new PlayerInfo[256];
class PlayerInfo {
public string Name;
public byte PlayerId;
public string Name, ColouredName;
public byte Id;
public PlayerInfo( Player p ) {
Name = Utils.StripColours( p.DisplayName );
PlayerId = p.ID;
}
}
public override void Init() {
base.Init();
game.EntityEvents.Added += PlayerSpawned;
game.EntityEvents.Removed += PlayerDespawned;
}
public override void Dispose() {
base.Dispose();
game.EntityEvents.Added -= PlayerSpawned;
game.EntityEvents.Removed -= PlayerDespawned;
}
void PlayerSpawned( object sender, IdEventArgs e ) {
Player player = game.Entities[e.Id];
AddPlayerInfo( player );
SortPlayerInfo();
}
protected override void CreateInitialPlayerInfo() {
for( int i = 0; i < EntityList.MaxCount; i++ ) {
Player player = game.Entities[i];
if( player != null ) {
AddPlayerInfo( player );
}
public PlayerInfo( TabListEntry p ) {
ColouredName = p.PlayerName;
Name = Utils.StripColours( p.PlayerName );
Id = p.NameId;
}
}
@ -61,20 +35,66 @@ namespace ClassicalSharp.Gui {
return null;
}
void AddPlayerInfo( Player player ) {
DrawTextArgs args = new DrawTextArgs( player.DisplayName, font, true );
public override void Init() {
base.Init();
game.EntityEvents.TabListEntryAdded += TabEntryAdded;
game.EntityEvents.TabListEntryRemoved += TabEntryRemoved;
game.EntityEvents.TabListEntryChanged += TabEntryChanged;
}
public override void Dispose() {
base.Dispose();
game.EntityEvents.TabListEntryAdded -= TabEntryAdded;
game.EntityEvents.TabListEntryChanged -= TabEntryChanged;
game.EntityEvents.TabListEntryRemoved -= TabEntryRemoved;
}
protected override void CreateInitialPlayerInfo() {
TabListEntry[] entries = game.TabList.Entries;
for( int i = 0; i < entries.Length; i++ ) {
TabListEntry e = entries[i];
if( e != null )
AddPlayerInfo( new PlayerInfo( e ), -1 );
}
}
void AddPlayerInfo( PlayerInfo pInfo, int index ) {
DrawTextArgs args = new DrawTextArgs( pInfo.ColouredName, font, false );
Texture tex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
game.Drawer2D.ReducePadding( ref tex, Utils.Floor( font.Size ), 3 );
info[namesCount] = new PlayerInfo( player );
textures[namesCount] = tex;
namesCount++;
if( index < 0 ) {
info[namesCount] = pInfo;
textures[namesCount] = tex;
namesCount++;
} else {
info[index] = pInfo;
textures[index] = tex;
}
}
void PlayerDespawned( object sender, IdEventArgs e ) {
void TabEntryAdded( object sender, IdEventArgs e ) {
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), -1 );
SortPlayerInfo();
}
void TabEntryChanged( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.PlayerId == e.Id ) {
if( pInfo.Id != e.Id ) continue;
Texture tex = textures[i];
api.DeleteTexture( ref tex );
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), i );
SortPlayerInfo();
return;
}
}
void TabEntryRemoved( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.Id == e.Id ) {
RemoveInfoAt( info, i );
return;
}
@ -89,8 +109,8 @@ namespace ClassicalSharp.Gui {
}
}
protected override void SortInfoList() {
Array.Sort( info, textures, 0, namesCount, comparer );
protected override void SortInfoList() {
Array.Sort( info, textures, 0, namesCount, comparer );
}
}
}

View File

@ -165,6 +165,7 @@
<Compile Include="Entities\LocalPlayer.cs" />
<Compile Include="Entities\LocationUpdate.cs" />
<Compile Include="Entities\NetPlayer.cs" />
<Compile Include="Entities\TabList.cs" />
<Compile Include="Events\EntityEvents.cs" />
<Compile Include="Events\Events.cs" />
<Compile Include="Events\UserEvents.cs" />

View File

@ -311,10 +311,10 @@ namespace ClassicalSharp.Network {
}
internal void HandleExtAddEntity2() {
byte entityId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
string displayName = reader.ReadAsciiString();
string skinName = reader.ReadAsciiString();
AddEntity( entityId, displayName, skinName, true );
AddEntity( id, displayName, skinName, true );
}
const int bulkCount = 256;

View File

@ -213,7 +213,7 @@ namespace ClassicalSharp.Network {
}
internal void HandleRelPosAndOrientationUpdate() {
byte playerId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
float x = reader.ReadInt8() / 32f;
float y = reader.ReadInt8() / 32f;
float z = reader.ReadInt8() / 32f;
@ -221,31 +221,31 @@ namespace ClassicalSharp.Network {
float yaw = (float)Utils.PackedToDegrees( reader.ReadUInt8() );
float pitch = (float)Utils.PackedToDegrees( reader.ReadUInt8() );
LocationUpdate update = LocationUpdate.MakePosAndOri( x, y, z, yaw, pitch, true );
UpdateLocation( playerId, update, true );
UpdateLocation( id, update, true );
}
internal void HandleRelPositionUpdate() {
byte playerId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
float x = reader.ReadInt8() / 32f;
float y = reader.ReadInt8() / 32f;
float z = reader.ReadInt8() / 32f;
LocationUpdate update = LocationUpdate.MakePos( x, y, z, true );
UpdateLocation( playerId, update, true );
UpdateLocation( id, update, true );
}
internal void HandleOrientationUpdate() {
byte playerId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
float yaw = (float)Utils.PackedToDegrees( reader.ReadUInt8() );
float pitch = (float)Utils.PackedToDegrees( reader.ReadUInt8() );
LocationUpdate update = LocationUpdate.MakeOri( yaw, pitch );
UpdateLocation( playerId, update, true );
UpdateLocation( id, update, true );
}
internal void HandleRemoveEntity() {
byte entityId = reader.ReadUInt8();
RemoveEntity( entityId );
byte id = reader.ReadUInt8();
RemoveEntity( id );
}
internal void HandleMessage() {
@ -271,16 +271,16 @@ namespace ClassicalSharp.Network {
game.LocalPlayer.Hacks.SetUserType( reader.ReadUInt8() );
}
void AddEntity( byte entityId, string displayName, string skinName, bool readPosition ) {
void AddEntity( byte id, string displayName, string skinName, bool readPosition ) {
skinName = Utils.StripColours( skinName );
if( entityId != 0xFF ) {
Player oldPlayer = game.Entities[entityId];
if( id != 0xFF ) {
Player oldPlayer = game.Entities[id];
if( oldPlayer != null ) {
game.EntityEvents.RaiseRemoved( entityId );
game.EntityEvents.RaiseRemoved( id );
oldPlayer.Despawn();
}
game.Entities[entityId] = new NetPlayer( displayName, skinName, game, entityId );
game.EntityEvents.RaiseAdded( entityId );
game.Entities[id] = new NetPlayer( displayName, skinName, game, id );
game.EntityEvents.RaiseAdded( id );
} else {
// Server is only allowed to change our own name colours.
if( Utils.StripColours( displayName ) != game.Username )
@ -290,12 +290,12 @@ namespace ClassicalSharp.Network {
game.LocalPlayer.UpdateName();
}
string identifier = game.Entities[entityId].SkinIdentifier;
string identifier = game.Entities[id].SkinIdentifier;
game.AsyncDownloader.DownloadSkin( identifier, skinName );
if( !readPosition ) return;
ReadAbsoluteLocation( entityId, false );
if( entityId == 0xFF ) {
ReadAbsoluteLocation( id, false );
if( id == 0xFF ) {
LocalPlayer p = game.LocalPlayer;
p.Spawn = p.Position;
p.SpawnYaw = p.HeadYawDegrees;