Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
* Added 'server busy' popup
* Fixed possible crashes and error popups
* Enabled multi-line motd messages for servers
* Set mod list popups to not clip beneath the screen
* Minor UI tweaks
  • Loading branch information
hamstar0 committed Apr 8, 2018
1 parent dac998d commit 3b29543
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 29 deletions.
12 changes: 11 additions & 1 deletion ServerBrowserMod.cs
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ServerBrowser.UI;
using System;
using Terraria;
using Terraria.ModLoader;

Expand Down Expand Up @@ -68,12 +69,21 @@ class ServerBrowserMod : Mod {

////////////////

private bool HasErrored = false;

private void DrawBrowser( GameTime game_time ) {
if( !Main.gameMenu ) { return; }

Main.spriteBatch.Begin( SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Main.UIScaleMatrix );

this.Dialog.Draw( Main.spriteBatch );
try {
this.Dialog.Draw( Main.spriteBatch );
} catch( Exception e ) {
if( !this.HasErrored ) {
this.HasErrored = true;
LogHelpers.Log( e.ToString() );
}
}

Vector2 bonus = Main.DrawThickCursor( false );
Main.DrawCursor( bonus, false );
Expand Down
18 changes: 16 additions & 2 deletions UI/UIServerBrowserDialog.cs
Expand Up @@ -19,6 +19,7 @@ partial class UIServerBrowserDialog : UIDialog {
private UITextPanelButton SortByPlayersButton;
private UITextField FilterByModInput;
private UITextField FilterByNameInput;
private UIText ServerListErr;

private UIServerBrowserList ServerList;

Expand Down Expand Up @@ -92,7 +93,7 @@ partial class UIServerBrowserDialog : UIDialog {

this.SortByPingButton = new UITextPanelButton( this.Theme, "Sort by ping", 1.2f );
this.SortByPingButton.Top.Set( 12f, 0f );
this.SortByPingButton.Left.Set( 128f + 8f, 0f );
this.SortByPingButton.Left.Set( 128f + 12f, 0f );
this.SortByPingButton.Width.Set( 128f, 0f );
this.SortByPingButton.Height.Set( 32f, 0f );
this.SortByPingButton.OnClick += delegate ( UIMouseEvent evt, UIElement listening_element ) {
Expand Down Expand Up @@ -171,6 +172,11 @@ partial class UIServerBrowserDialog : UIDialog {
this.ServerList.Initialize();
this.InnerContainer.Append( (UIElement)this.ServerList );

this.ServerListErr = new UIText( "", 1f );
this.ServerListErr.Top.Set( 60f, 0f );
this.ServerListErr.Left.Set( -128, 0.5f );
this.InnerContainer.Append( (UIElement)this.ServerListErr );

////

var modrecommend_url = new UIWebUrl( this.Theme, "Trouble choosing mods?", "https://sites.google.com/site/terrariamodsuggestions/", true, 0.86f );
Expand All @@ -192,8 +198,16 @@ partial class UIServerBrowserDialog : UIDialog {
this.Close();
};

Action on_success = () => {
this.ServerListErr.SetText( "" );
};

Action on_err = () => {
this.ServerListErr.SetText( "Server busy. Try again later." );
};

base.OnActivate();
this.ServerList.RefreshServerList( pre_join );
this.ServerList.RefreshServerList( pre_join, on_success, on_err );
}


Expand Down
34 changes: 15 additions & 19 deletions UI/UIServerBrowserList.cs
Expand Up @@ -6,7 +6,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;

Expand All @@ -20,23 +19,25 @@ class UIServerBrowserList : UIPanel {

public static UIServerDataElement[] GetListFromJsonStr( UITheme theme, string json_str,
Func<UIServerDataElement, UIServerDataElement, int> comparator,
Action<string, int> pre_join ) {
Action<string, int> pre_join, Action on_err ) {
UIServerDataElement[] list = new UIServerDataElement[0];

try {
var data = JsonConfig<IDictionary<string, ServerBrowserEntry>>.Deserialize( json_str );
UIServerDataElement[] list = new UIServerDataElement[data.Count];
list = new UIServerDataElement[data.Count];

int i = 0;
foreach( var kv in data ) {
list[i++] = new UIServerDataElement( theme, kv.Value, comparator, pre_join );
}

return list;
} catch( Exception e ) {
int len = json_str.Length > 64 ? 64 : json_str.Length;
LogHelpers.Log( "GetListFromJsonStr - " + e.ToString() + " - " + json_str.Substring( 0, len ) );

on_err();
}

return new UIServerDataElement[0];
return list;
}


Expand Down Expand Up @@ -96,14 +97,13 @@ class UIServerBrowserList : UIPanel {
if( this.MyList.Count > 0 ) {
this.MyList.Clear();
}

this.MyList.AddRange( list );
this.MyList.Recalculate();
}
this.MyList.Recalculate();
}


public void RefreshServerList( Action<string, int> pre_join ) {
public void RefreshServerList( Action<string, int> pre_join, Action on_success, Action on_err ) {
lock( UIServerBrowserList.MyLock ) {
if( this.MyList.Count > 0 ) {
this.MyList.Clear();
Expand All @@ -112,14 +112,15 @@ class UIServerBrowserList : UIPanel {
}

Action<string> list_ready = delegate ( string output ) {
this.FullServerList = UIServerBrowserList.GetListFromJsonStr( this.Theme, output, this.Comparator, pre_join );
this.FullServerList = UIServerBrowserList.GetListFromJsonStr( this.Theme, output, this.Comparator, pre_join, on_err );

if( this.FullServerList.Count > 0 ) {
lock( UIServerBrowserList.MyLock ) {
this.MyList.AddRange( this.FullServerList );
this.Recalculate();
}
}
on_success();
};

Action<Exception, string> list_error = delegate ( Exception e, string output ) {
Expand All @@ -135,7 +136,9 @@ class UIServerBrowserList : UIPanel {
////////////////

internal void UpdateOrder() {
this.MyList.UpdateOrder();
lock( UIServerBrowserList.MyLock ) {
this.MyList.UpdateOrder();
}
}

private int Comparator( UIServerDataElement prev, UIServerDataElement next ) {
Expand Down Expand Up @@ -187,14 +190,7 @@ class UIServerBrowserList : UIPanel {
var server_data_elem = (UIServerDataElement)item;

this.ModListPopup.SetServer( server_data_elem.Data );

CalculatedStyle dim = this.ModListPopup.GetDimensions();
float top = Main.mouseY + 16f;
float left = Main.mouseX - ( dim.Width * 0.5f );

this.ModListPopup.Top.Set( top, 0f );
this.ModListPopup.Left.Set( left, 0f );
this.ModListPopup.Recalculate();
break;
}

if( is_hovering_server ) {
Expand Down
36 changes: 31 additions & 5 deletions UI/UIServerDataElement.cs
Expand Up @@ -61,6 +61,26 @@ class UIServerDataElement : UIPanel {
return way;
}

////////////////

public static string[] GetMotdLines( string motd, int line_width ) {
string[] motd_chunks;

if( motd.Length > line_width ) {
int chunks = (int)Math.Ceiling( (float)motd.Length / (float)line_width );

motd_chunks = new string[chunks];
for( int i = 0; i < motd_chunks.Length; i++ ) {
int width = ( i + 1 ) * line_width <= motd.Length ? line_width : motd.Length - ( i * line_width );
motd_chunks[i] = motd.Substring( i * line_width, width );
}
} else {
motd_chunks = new string[] { motd };
}

return motd_chunks;
}



////////////////
Expand Down Expand Up @@ -161,12 +181,18 @@ class UIServerDataElement : UIPanel {
////

if( this.Data.Motd != "" ) {
var motd_label = new UIText( this.Data.Motd, 0.8f );
motd_label.Left.Set( UIServerDataElement.MotdLabelLeft, 0f );
motd_label.Top.Set( UIServerDataElement.MotdLabelTop, 0f );
this.Append( (UIElement)motd_label );
int line_height = 24;
string[] motd_chunks = UIServerDataElement.GetMotdLines( this.Data.Motd, 96 );

for( int i=0; i<motd_chunks.Length; i++ ) {
//LogHelpers.Log( "motd "+i+" "+motd_chunks[i]+", "+ ( i * 24f )+" - "+(line_height * motd_chunks.Length) );
var motd_label = new UIText( motd_chunks[i], 0.8f );
motd_label.Left.Set( UIServerDataElement.MotdLabelLeft, 0f );
motd_label.Top.Set( UIServerDataElement.MotdLabelTop + (i * line_height ), 0f );
this.Append( (UIElement)motd_label );
}

this.Height.Set( this.Height.Pixels + 24f, 0f );
this.Height.Set( this.Height.Pixels + (line_height * motd_chunks.Length), 0f );
}

////
Expand Down
20 changes: 19 additions & 1 deletion UI/UIServerModListPopup.cs
Expand Up @@ -4,7 +4,7 @@
using ReLogic.Graphics;
using Terraria;
using Terraria.GameContent.UI.Elements;

using Terraria.UI;

namespace ServerBrowser.UI {
class UIServerModListPopup : UIPanel {
Expand Down Expand Up @@ -50,9 +50,27 @@ class UIServerModListPopup : UIPanel {

////////////////

public void UpdatePosition() {
CalculatedStyle dim = this.GetDimensions();
float top = Main.mouseY + 16f;
float left = Main.mouseX - ( dim.Width * 0.5f );

if( ( top + this.Height.Pixels ) > Main.screenHeight ) {
top = Main.screenHeight - dim.Height;
left = (Main.mouseX - 16f) - dim.Width;
}

this.Top.Set( top, 0f );
this.Left.Set( left, 0f );
this.Recalculate();
}


public override void Draw( SpriteBatch sb ) {
if( this.CurrentEntry == null ) { return; }

this.UpdatePosition();

base.Draw( sb );

float x = this.Left.Pixels + 8f;
Expand Down
2 changes: 1 addition & 1 deletion build.txt
@@ -1,5 +1,5 @@
author = hamstar
version = 1.0.0.1
version = 1.0.1
displayName = Server Browser
modReferences = HamstarHelpers@1.4.3
buildIgnore = *.csproj, *.user, *.bat, obj\*, bin\*, .vs\*, .git\*
Expand Down
Binary file added v1.0.0 UI (blue).png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added v1.0.0 UI (red).png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3b29543

Please sign in to comment.