Get rid of some widget code duplication.

This commit is contained in:
UnknownShadow200 2017-08-20 17:22:10 +10:00
parent dfc5380757
commit 82646f5cdb
21 changed files with 68 additions and 68 deletions

View File

@ -194,10 +194,10 @@ namespace ClassicalSharp.Gui.Screens {
int height = InputUsedHeight; int height = InputUsedHeight;
if (force || height != inputOldHeight) { if (force || height != inputOldHeight) {
clientStatus.YOffset = Math.Max(hud.BottomOffset + 15, height); clientStatus.YOffset = Math.Max(hud.BottomOffset + 15, height);
clientStatus.CalculatePosition(); clientStatus.Reposition();
normalChat.YOffset = clientStatus.YOffset + clientStatus.GetUsedHeight(); normalChat.YOffset = clientStatus.YOffset + clientStatus.GetUsedHeight();
normalChat.CalculatePosition(); normalChat.Reposition();
inputOldHeight = height; inputOldHeight = height;
} }
} }

View File

@ -57,8 +57,7 @@ namespace ClassicalSharp.Gui.Screens {
} }
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
for (int i = 0; i < widgets.Length; i++) RepositionWidgets(widgets);
widgets[i].CalculatePosition();
clearTime = DateTime.UtcNow.AddSeconds(0.5); clearTime = DateTime.UtcNow.AddSeconds(0.5);
} }
@ -104,8 +103,7 @@ namespace ClassicalSharp.Gui.Screens {
void Redraw(double delta) { void Redraw(double delta) {
gfx.Draw2DQuad(0, 0, game.Width, game.Height, top, bottom); gfx.Draw2DQuad(0, 0, game.Width, game.Height, top, bottom);
gfx.Texturing = true; gfx.Texturing = true;
for (int i = 0; i < widgets.Length; i++) RenderWidgets(widgets, delta);
widgets[i].Render(delta);
gfx.Texturing = false; gfx.Texturing = false;
} }

View File

@ -89,7 +89,7 @@ namespace ClassicalSharp.Gui.Screens {
playerList.Init(); playerList.Init();
playerList.RecalcYOffset(); playerList.RecalcYOffset();
playerList.CalculatePosition(); playerList.Reposition();
} }
public override void Dispose() { public override void Dispose() {
@ -114,11 +114,11 @@ namespace ClassicalSharp.Gui.Screens {
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
chat.OnResize(width, height); chat.OnResize(width, height);
hotbar.CalculatePosition(); hotbar.Reposition();
if (playerList != null) { if (playerList != null) {
playerList.RecalcYOffset(); playerList.RecalcYOffset();
playerList.CalculatePosition(); playerList.Reposition();
} }
} }

View File

@ -72,8 +72,8 @@ namespace ClassicalSharp.Gui.Screens {
} }
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
messageWidget.CalculatePosition(); messageWidget.Reposition();
titleWidget.CalculatePosition(); titleWidget.Reposition();
} }
protected override void ContextLost() { protected override void ContextLost() {

View File

@ -125,17 +125,15 @@ namespace ClassicalSharp.Gui.Screens {
} }
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
for (int i = 0; i < buttons.Length; i++) RepositionWidgets(buttons);
buttons[i].CalculatePosition(); title.Reposition();
title.CalculatePosition();
} }
public override void Render(double delta) { public override void Render(double delta) {
gfx.Draw2DQuad(0, 0, game.Width, game.Height, new FastColour(60, 60, 60, 160)); gfx.Draw2DQuad(0, 0, game.Width, game.Height, new FastColour(60, 60, 60, 160));
gfx.Texturing = true; gfx.Texturing = true;
title.Render(delta); title.Render(delta);
for (int i = 0; i < buttons.Length; i++) RenderWidgets(buttons, delta);
buttons[i].Render(delta);
gfx.Texturing = false; gfx.Texturing = false;
} }
} }

View File

@ -83,7 +83,7 @@ namespace ClassicalSharp.Gui.Screens {
.SetLocation(Anchor.Centre, Anchor.Centre, x, y); .SetLocation(Anchor.Centre, Anchor.Centre, x, y);
label.XOffset = -110 - label.Width / 2; label.XOffset = -110 - label.Width / 2;
label.CalculatePosition(); label.Reposition();
label.Colour = new FastColour(224, 224, 224); label.Colour = new FastColour(224, 224, 224);
return label; return label;
} }

View File

@ -28,7 +28,7 @@ namespace ClassicalSharp.Gui.Screens {
} }
gfx.Texturing = true; gfx.Texturing = true;
RenderMenuWidgets(delta); RenderWidgets(widgets, delta);
if (extendedHelp != null && extEndY <= extClipY) if (extendedHelp != null && extEndY <= extClipY)
extendedHelp.Render(delta); extendedHelp.Render(delta);
@ -72,7 +72,7 @@ namespace ClassicalSharp.Gui.Screens {
extendedHelp.XOffset = game.Width / 2 - extendedHelp.Width / 2; extendedHelp.XOffset = game.Width / 2 - extendedHelp.Width / 2;
extendedHelp.YOffset = game.Height / 2 + extHelpY; extendedHelp.YOffset = game.Height / 2 + extHelpY;
extendedHelp.CalculatePosition(); extendedHelp.Reposition();
} }
protected override void ContextLost() { protected override void ContextLost() {
@ -167,7 +167,7 @@ namespace ClassicalSharp.Gui.Screens {
extendedHelp.XOffset = game.Width / 2 - extendedHelp.Width / 2; extendedHelp.XOffset = game.Width / 2 - extendedHelp.Width / 2;
extendedHelp.YOffset = game.Height / 2 + extHelpY; extendedHelp.YOffset = game.Height / 2 + extHelpY;
extendedHelp.CalculatePosition(); extendedHelp.Reposition();
} }
void DisposeExtendedHelp() { void DisposeExtendedHelp() {

View File

@ -17,7 +17,7 @@ namespace ClassicalSharp.Gui.Screens {
public override void Render(double delta) { public override void Render(double delta) {
RenderMenuBounds(); RenderMenuBounds();
gfx.Texturing = true; gfx.Texturing = true;
RenderMenuWidgets(delta); RenderWidgets(widgets, delta);
if (descWidget != null) if (descWidget != null)
descWidget.Render(delta); descWidget.Render(delta);
gfx.Texturing = false; gfx.Texturing = false;
@ -94,7 +94,7 @@ namespace ClassicalSharp.Gui.Screens {
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
if (descWidget != null) if (descWidget != null)
descWidget.CalculatePosition(); descWidget.Reposition();
base.OnResize(width, height); base.OnResize(width, height);
} }

View File

@ -20,7 +20,7 @@ namespace ClassicalSharp.Gui.Screens {
public override void Render(double delta) { public override void Render(double delta) {
RenderMenuBounds(); RenderMenuBounds();
gfx.Texturing = true; gfx.Texturing = true;
RenderMenuWidgets(delta); RenderWidgets(widgets, delta);
input.Render(delta); input.Render(delta);
if (desc != null) desc.Render(delta); if (desc != null) desc.Render(delta);
gfx.Texturing = false; gfx.Texturing = false;
@ -84,7 +84,7 @@ namespace ClassicalSharp.Gui.Screens {
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
input.CalculatePosition(); input.Reposition();
base.OnResize(width, height); base.OnResize(width, height);
} }

View File

@ -18,17 +18,10 @@ namespace ClassicalSharp.Gui.Screens {
gfx.Draw2DQuad(0, 0, game.Width, game.Height, backCol); gfx.Draw2DQuad(0, 0, game.Width, game.Height, backCol);
} }
protected void RenderMenuWidgets(double delta) {
for (int i = 0; i < widgets.Length; i++) {
if (widgets[i] == null) continue;
widgets[i].Render(delta);
}
}
public override void Render(double delta) { public override void Render(double delta) {
RenderMenuBounds(); RenderMenuBounds();
gfx.Texturing = true; gfx.Texturing = true;
RenderMenuWidgets(delta); RenderWidgets(widgets, delta);
gfx.Texturing = false; gfx.Texturing = false;
} }
@ -49,10 +42,7 @@ namespace ClassicalSharp.Gui.Screens {
protected override void ContextLost() { DisposeWidgets(widgets); } protected override void ContextLost() { DisposeWidgets(widgets); }
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
for (int i = 0; i < widgets.Length; i++) { RepositionWidgets(widgets);
if (widgets[i] == null) continue;
widgets[i].CalculatePosition();
}
} }

View File

@ -28,9 +28,8 @@ namespace ClassicalSharp.Gui.Screens {
public override void Render(double delta) { public override void Render(double delta) {
RenderMenuBounds(); RenderMenuBounds();
gfx.Texturing = true; gfx.Texturing = true;
RenderMenuWidgets(delta); RenderWidgets(widgets, delta);
for (int i = 0; i < labels.Length; i++) RenderWidgets(labels, delta);
labels[i].Render(delta);
gfx.Texturing = false; gfx.Texturing = false;
if (OnRenderFrame != null) OnRenderFrame(this); if (OnRenderFrame != null) OnRenderFrame(this);
@ -38,8 +37,7 @@ namespace ClassicalSharp.Gui.Screens {
public override void OnResize(int width, int height) { public override void OnResize(int width, int height) {
base.OnResize(width, height); base.OnResize(width, height);
for (int i = 0; i < labels.Length; i++) RepositionWidgets(labels);
labels[i].CalculatePosition();
} }
protected override void ContextLost() { protected override void ContextLost() {

View File

@ -43,7 +43,23 @@ namespace ClassicalSharp.Gui.Screens {
if (widgets == null) return; if (widgets == null) return;
for (int i = 0; i < widgets.Length; i++) { for (int i = 0; i < widgets.Length; i++) {
widgets[i].Dispose(); if (widgets[i] != null) widgets[i].Dispose();
}
}
protected static void RepositionWidgets<T>(T[] widgets) where T : Widget {
if (widgets == null) return;
for (int i = 0; i < widgets.Length; i++) {
if (widgets[i] != null) widgets[i].Reposition();
}
}
protected static void RenderWidgets<T>(T[] widgets, double delta) where T : Widget {
if (widgets == null) return;
for (int i = 0; i < widgets.Length; i++) {
if (widgets[i] != null) widgets[i].Render(delta);
} }
} }
} }

View File

@ -35,7 +35,7 @@ namespace ClassicalSharp.Gui.Widgets {
public ButtonWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) { public ButtonWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor; HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
XOffset = xOffset; YOffset = yOffset; XOffset = xOffset; YOffset = yOffset;
CalculatePosition(); Reposition();
return this; return this;
} }
@ -70,7 +70,7 @@ namespace ClassicalSharp.Gui.Widgets {
Width = Math.Max(texture.Width, MinWidth); Width = Math.Max(texture.Width, MinWidth);
Height = Math.Max(texture.Height, MinHeight); Height = Math.Max(texture.Height, MinHeight);
CalculatePosition(); Reposition();
texture.X1 = X + (Width / 2 - texture.Width / 2); texture.X1 = X + (Width / 2 - texture.Width / 2);
texture.Y1 = Y + (Height / 2 - texture.Height / 2); texture.Y1 = Y + (Height / 2 - texture.Height / 2);
} }
@ -115,9 +115,9 @@ namespace ClassicalSharp.Gui.Widgets {
gfx.DeleteTexture(ref texture); gfx.DeleteTexture(ref texture);
} }
public override void CalculatePosition() { public override void Reposition() {
int oldX = X, oldY = Y; int oldX = X, oldY = Y;
base.CalculatePosition(); base.Reposition();
texture.X1 += X - oldX; texture.X1 += X - oldX;
texture.Y1 += Y - oldY; texture.Y1 += Y - oldY;

View File

@ -15,7 +15,7 @@ namespace ClassicalSharp.Gui.Widgets {
public TextGroupWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) { public TextGroupWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor; HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
XOffset = xOffset; YOffset = yOffset; XOffset = xOffset; YOffset = yOffset;
CalculatePosition(); Reposition();
return this; return this;
} }
@ -107,7 +107,7 @@ namespace ClassicalSharp.Gui.Widgets {
Width = Math.Max(Width, Textures[i].Width); Width = Math.Max(Width, Textures[i].Width);
Height += Textures[i].Height; Height += Textures[i].Height;
} }
CalculatePosition(); Reposition();
} }
public override void Render(double delta) { public override void Render(double delta) {
@ -123,9 +123,9 @@ namespace ClassicalSharp.Gui.Widgets {
gfx.DeleteTexture(ref Textures[i]); gfx.DeleteTexture(ref Textures[i]);
} }
public override void CalculatePosition() { public override void Reposition() {
int oldY = Y; int oldY = Y;
base.CalculatePosition(); base.Reposition();
if (Textures == null) return; if (Textures == null) return;
for (int i = 0; i < Textures.Length; i++) { for (int i = 0; i < Textures.Length; i++) {

View File

@ -48,8 +48,8 @@ namespace ClassicalSharp.Gui.Widgets {
public override void Dispose() { } public override void Dispose() { }
public override void CalculatePosition() { public override void Reposition() {
base.CalculatePosition(); base.Reposition();
Recreate(); Recreate();
} }

View File

@ -30,7 +30,7 @@ namespace ClassicalSharp.Gui.Widgets {
public InputWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) { public InputWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor; HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
XOffset = xOffset; YOffset = yOffset; XOffset = xOffset; YOffset = yOffset;
CalculatePosition(); Reposition();
return this; return this;
} }
@ -89,9 +89,9 @@ namespace ClassicalSharp.Gui.Widgets {
Init(); Init();
} }
public override void CalculatePosition() { public override void Reposition() {
int oldX = X, oldY = Y; int oldX = X, oldY = Y;
base.CalculatePosition(); base.Reposition();
caretTex.X1 += X - oldX; caretTex.Y1 += Y - oldY; caretTex.X1 += X - oldX; caretTex.Y1 += Y - oldY;
inputTex.X1 += X - oldX; inputTex.Y1 += Y - oldY; inputTex.X1 += X - oldX; inputTex.Y1 += Y - oldY;
@ -194,7 +194,7 @@ namespace ClassicalSharp.Gui.Widgets {
Width = size.Width; Width = size.Width;
Height = realHeight == 0 ? prefixHeight : realHeight; Height = realHeight == 0 ? prefixHeight : realHeight;
CalculatePosition(); Reposition();
inputTex.X1 = X + Padding; inputTex.Y1 = Y; inputTex.X1 = X + Padding; inputTex.Y1 = Y;
} }

View File

@ -76,7 +76,7 @@ namespace ClassicalSharp.Gui.Widgets {
inputTex = drawer.Make2DTexture(bmp, adjSize, 0, 0); inputTex = drawer.Make2DTexture(bmp, adjSize, 0, 0);
} }
CalculatePosition(); Reposition();
inputTex.X1 = X; inputTex.Y1 = Y; inputTex.X1 = X; inputTex.Y1 = Y;
if (size.Height < MinHeight) if (size.Height < MinHeight)
inputTex.Y1 += MinHeight / 2 - size.Height / 2; inputTex.Y1 += MinHeight / 2 - size.Height / 2;

View File

@ -35,7 +35,7 @@ namespace ClassicalSharp.Gui.Widgets {
gfx.Texturing = true; gfx.Texturing = true;
overview.YOffset = Y - offset + 5; overview.YOffset = Y - offset + 5;
overview.CalculatePosition(); overview.Reposition();
overview.Render(delta); overview.Render(delta);
for (int i = 0; i < namesCount; i++) { for (int i = 0; i < namesCount; i++) {

View File

@ -105,9 +105,9 @@ namespace ClassicalSharp.Gui.Widgets {
YOffset = -Math.Max(0, game.Height / 4 - Height / 2); YOffset = -Math.Max(0, game.Height / 4 - Height / 2);
} }
public override void CalculatePosition() { public override void Reposition() {
int oldX = X, oldY = Y; int oldX = X, oldY = Y;
base.CalculatePosition(); base.Reposition();
for (int i = 0; i < namesCount; i++) { for (int i = 0; i < namesCount; i++) {
textures[i].X1 += X - oldX; textures[i].X1 += X - oldX;
@ -164,7 +164,7 @@ namespace ClassicalSharp.Gui.Widgets {
OnSort(); OnSort();
UpdateTableDimensions(); UpdateTableDimensions();
RecalcYOffset(); RecalcYOffset();
CalculatePosition(); Reposition();
} }
protected virtual void OnSort() { } protected virtual void OnSort() { }

View File

@ -19,7 +19,7 @@ namespace ClassicalSharp.Gui.Widgets {
public TextWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) { public TextWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor; HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
XOffset = xOffset; YOffset = yOffset; XOffset = xOffset; YOffset = yOffset;
CalculatePosition(); Reposition();
return this; return this;
} }
@ -56,7 +56,7 @@ namespace ClassicalSharp.Gui.Widgets {
game.Drawer2D.ReducePadding(ref texture, Utils.Floor(font.Size)); game.Drawer2D.ReducePadding(ref texture, Utils.Floor(font.Size));
Width = texture.Width; Height = texture.Height; Width = texture.Width; Height = texture.Height;
CalculatePosition(); Reposition();
texture.X1 = X; texture.Y1 = Y; texture.X1 = X; texture.Y1 = Y;
} }
} }
@ -70,9 +70,9 @@ namespace ClassicalSharp.Gui.Widgets {
gfx.DeleteTexture(ref texture); gfx.DeleteTexture(ref texture);
} }
public override void CalculatePosition() { public override void Reposition() {
int oldX = X, oldY = Y; int oldX = X, oldY = Y;
base.CalculatePosition(); base.Reposition();
texture.X1 += X - oldX; texture.X1 += X - oldX;
texture.Y1 += Y - oldY; texture.Y1 += Y - oldY;

View File

@ -48,7 +48,7 @@ namespace ClassicalSharp.Gui.Widgets {
/// <summary> Specifies the boundaries of the widget in pixels. </summary> /// <summary> Specifies the boundaries of the widget in pixels. </summary>
public Rectangle Bounds { get { return new Rectangle(X, Y, Width, Height); } } public Rectangle Bounds { get { return new Rectangle(X, Y, Width, Height); } }
public virtual void CalculatePosition() { public virtual void Reposition() {
X = CalcPos(HorizontalAnchor, XOffset, Width, game.Width); X = CalcPos(HorizontalAnchor, XOffset, Width, game.Width);
Y = CalcPos(VerticalAnchor, YOffset, Height, game.Height); Y = CalcPos(VerticalAnchor, YOffset, Height, game.Height);
} }