From 0121a1820f52d063c2d1bdc0fb51750f3d80681d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 11 Feb 2017 18:30:30 +1100 Subject: [PATCH] More fixes III --- ClassicalSharp/2D/Screens/MenuScreen.cs | 2 + ClassicalSharp/2D/Screens/WarningScreen.cs | 74 ++++++++++----------- ClassicalSharp/Entities/Entity.cs | 2 + ClassicalSharp/Entities/EntityList.cs | 10 +++ ClassicalSharp/Entities/Player.cs | 3 + ClassicalSharp/Network/IServerConnection.cs | 4 +- 6 files changed, 54 insertions(+), 41 deletions(-) diff --git a/ClassicalSharp/2D/Screens/MenuScreen.cs b/ClassicalSharp/2D/Screens/MenuScreen.cs index a59f2b280..1d0c860cf 100644 --- a/ClassicalSharp/2D/Screens/MenuScreen.cs +++ b/ClassicalSharp/2D/Screens/MenuScreen.cs @@ -46,6 +46,8 @@ namespace ClassicalSharp.Gui.Screens { } protected override void ContextLost() { + if (widgets == null) return; + for (int i = 0; i < widgets.Length; i++) { if (widgets[i] == null) continue; widgets[i].Dispose(); diff --git a/ClassicalSharp/2D/Screens/WarningScreen.cs b/ClassicalSharp/2D/Screens/WarningScreen.cs index 310a8ed2d..a160a388e 100644 --- a/ClassicalSharp/2D/Screens/WarningScreen.cs +++ b/ClassicalSharp/2D/Screens/WarningScreen.cs @@ -25,9 +25,9 @@ namespace ClassicalSharp.Gui.Screens { this.body = body; } - string title, lastTitle; - string[] body, lastBody; - bool confirmNo, confirmMode, showAlways; + string title; + string[] body; + bool confirmNo, confirmingMode, showAlways; int alwaysIndex = 100; public override void Init() { @@ -35,15 +35,14 @@ namespace ClassicalSharp.Gui.Screens { titleFont = new Font(game.FontName, 16, FontStyle.Bold); regularFont = new Font(game.FontName, 16, FontStyle.Regular); backCol.A = 210; + + if (game.Graphics.LostContext) return; InitStandardButtons(); - SetText(title, body); + RedrawText(); } - public void SetText(string title, params string[] body) { - lastTitle = title; - lastBody = body; - - if (confirmMode) { + public void RedrawText() { + if (confirmingMode) { SetTextImpl("&eYou might be missing out.", "Texture packs can play a vital role in the look and feel of maps.", "", "Sure you don't want to download the texture pack?"); @@ -57,8 +56,6 @@ namespace ClassicalSharp.Gui.Screens { for (int i = 0; i < labels.Length; i++) labels[i].Dispose(); } - this.title = title; - this.body = body; labels = new TextWidget[body.Length + 1]; labels[0] = TextWidget.Create(game, title, titleFont) @@ -96,28 +93,35 @@ namespace ClassicalSharp.Gui.Screens { for (int i = 0; i < labels.Length; i++) labels[i].CalculatePosition(); } - - public override void Dispose() { - base.Dispose(); + + protected override void ContextLost() { + base.ContextLost(); + if (labels == null) return; + for (int i = 0; i < labels.Length; i++) labels[i].Dispose(); } - protected override void ContextLost() - { - base.ContextLost(); - } - - protected override void ContextRecreated() - { - throw new NotImplementedException(); + protected override void ContextRecreated() { + InitStandardButtons(); + RedrawText(); } void InitStandardButtons() { - widgets = new ButtonWidget[showAlways ? 4 : 2]; + base.ContextLost(); alwaysIndex = 100; + if (confirmingMode) { + widgets = new ButtonWidget[2]; + widgets[0] = ButtonWidget.Create(game, 160, 35, "I'm sure", titleFont, OnNoClick) + .SetLocation(Anchor.Centre, Anchor.Centre, -110, 30); + widgets[1] = ButtonWidget.Create(game, 160, 35, "Go back", titleFont, GoBackClick) + .SetLocation(Anchor.Centre, Anchor.Centre, 110, 30); + return; + } + + widgets = new ButtonWidget[showAlways ? 4 : 2]; widgets[0] = ButtonWidget.Create(game, 160, 35, "Yes", titleFont, OnYesClick) .SetLocation(Anchor.Centre, Anchor.Centre, -110, 30); widgets[1] = ButtonWidget.Create(game, 160, 35, "No", titleFont, OnNoClick) @@ -146,8 +150,10 @@ namespace ClassicalSharp.Gui.Screens { if (btn != MouseButton.Left) return; bool always = Array.IndexOf(widgets, w) >= alwaysIndex; - if (confirmNo && !confirmMode) { - InitConfirmButtons(always); return; + if (confirmNo && !confirmingMode) { + confirmingMode = true; + ContextRecreated(); + return; } if (noClick != null) noClick(this, always); @@ -155,23 +161,11 @@ namespace ClassicalSharp.Gui.Screens { CloseScreen(); } - void InitConfirmButtons(bool always) { - alwaysIndex = always ? 0 : 100; - widgets = new ButtonWidget[] { - ButtonWidget.Create(game, 160, 35, "I'm sure", titleFont, OnNoClick) - .SetLocation(Anchor.Centre, Anchor.Centre, -110, 30), - ButtonWidget.Create(game, 160, 35, "Go back", titleFont, GoBackClick) - .SetLocation(Anchor.Centre, Anchor.Centre, 110, 30), - }; - confirmMode = true; - SetText(lastTitle, lastBody); - } - void GoBackClick(Game g, Widget w, MouseButton btn, int x, int y) { if (btn != MouseButton.Left) return; - InitStandardButtons(); - confirmMode = false; - SetText(lastTitle, lastBody); + + confirmingMode = false; + ContextRecreated(); } } } \ No newline at end of file diff --git a/ClassicalSharp/Entities/Entity.cs b/ClassicalSharp/Entities/Entity.cs index 8f754d1cf..6cbeaf4d8 100644 --- a/ClassicalSharp/Entities/Entity.cs +++ b/ClassicalSharp/Entities/Entity.cs @@ -74,6 +74,8 @@ namespace ClassicalSharp.Entities { public virtual void ContextLost() { } + public virtual void ContextRecreated() { } + /// Gets the position of the player's eye in the world. public Vector3 EyePosition { diff --git a/ClassicalSharp/Entities/EntityList.cs b/ClassicalSharp/Entities/EntityList.cs index 91fb715cd..b28d3bbb8 100644 --- a/ClassicalSharp/Entities/EntityList.cs +++ b/ClassicalSharp/Entities/EntityList.cs @@ -28,6 +28,7 @@ namespace ClassicalSharp.Entities { public EntityList(Game game) { this.game = game; game.Graphics.ContextLost += ContextLost; + game.Graphics.ContextRecreated += ContextRecreated; game.Events.ChatFontChanged += ChatFontChanged; game.Events.TextureChanged += TextureChanged; @@ -108,6 +109,13 @@ namespace ClassicalSharp.Entities { } } + void ContextRecreated() { + for (int i = 0; i < Entities.Length; i++) { + if (Entities[i] == null) continue; + Entities[i].ContextRecreated(); + } + } + void TextureChanged(object sender, TextureEventArgs e) { if (e.Name != "char.png") return; for (int i = 0; i < Entities.Length; i++) { @@ -132,8 +140,10 @@ namespace ClassicalSharp.Entities { } game.Graphics.ContextLost -= ContextLost; + game.Graphics.ContextRecreated -= ContextRecreated; game.Events.ChatFontChanged -= ChatFontChanged; game.Events.TextureChanged -= TextureChanged; + if (ShadowComponent.shadowTex > 0) game.Graphics.DeleteTexture(ref ShadowComponent.shadowTex); } diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index 23ed57b20..ee91535ca 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -33,6 +33,8 @@ namespace ClassicalSharp.Entities { game.Graphics.DeleteTexture(ref nameTex.ID); } + public override void ContextRecreated() { UpdateName(); } + protected void MakeNameTexture() { using (Font font = new Font(game.FontName, 24)) { DrawTextArgs args = new DrawTextArgs(DisplayName, font, false); @@ -58,6 +60,7 @@ namespace ClassicalSharp.Entities { public void UpdateName() { ContextLost(); + if (game.Graphics.LostContext) return; MakeNameTexture(); } diff --git a/ClassicalSharp/Network/IServerConnection.cs b/ClassicalSharp/Network/IServerConnection.cs index cef70d071..217dd14af 100644 --- a/ClassicalSharp/Network/IServerConnection.cs +++ b/ClassicalSharp/Network/IServerConnection.cs @@ -72,9 +72,11 @@ namespace ClassicalSharp { string address = url; if (url.StartsWith("https://")) address = url.Substring(8); if (url.StartsWith("http://")) address = url.Substring(7); - screen.SetText("Do you want to download the server's texture pack?", + + screen.SetTextData("Do you want to download the server's texture pack?", "Texture pack url:", address, "Download size: " + contentLengthMB.ToString("F3") + " MB"); + screen.RedrawText(); } protected internal void RetrieveTexturePack(string url) {