mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-24 05:10:42 -04:00
Get rid of some widget code duplication.
This commit is contained in:
parent
dfc5380757
commit
82646f5cdb
@ -194,10 +194,10 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
int height = InputUsedHeight;
|
||||
if (force || height != inputOldHeight) {
|
||||
clientStatus.YOffset = Math.Max(hud.BottomOffset + 15, height);
|
||||
clientStatus.CalculatePosition();
|
||||
clientStatus.Reposition();
|
||||
|
||||
normalChat.YOffset = clientStatus.YOffset + clientStatus.GetUsedHeight();
|
||||
normalChat.CalculatePosition();
|
||||
normalChat.Reposition();
|
||||
inputOldHeight = height;
|
||||
}
|
||||
}
|
||||
|
@ -57,8 +57,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
}
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
for (int i = 0; i < widgets.Length; i++)
|
||||
widgets[i].CalculatePosition();
|
||||
RepositionWidgets(widgets);
|
||||
clearTime = DateTime.UtcNow.AddSeconds(0.5);
|
||||
}
|
||||
|
||||
@ -104,8 +103,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
void Redraw(double delta) {
|
||||
gfx.Draw2DQuad(0, 0, game.Width, game.Height, top, bottom);
|
||||
gfx.Texturing = true;
|
||||
for (int i = 0; i < widgets.Length; i++)
|
||||
widgets[i].Render(delta);
|
||||
RenderWidgets(widgets, delta);
|
||||
gfx.Texturing = false;
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
|
||||
playerList.Init();
|
||||
playerList.RecalcYOffset();
|
||||
playerList.CalculatePosition();
|
||||
playerList.Reposition();
|
||||
}
|
||||
|
||||
public override void Dispose() {
|
||||
@ -114,11 +114,11 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
chat.OnResize(width, height);
|
||||
hotbar.CalculatePosition();
|
||||
hotbar.Reposition();
|
||||
|
||||
if (playerList != null) {
|
||||
playerList.RecalcYOffset();
|
||||
playerList.CalculatePosition();
|
||||
playerList.Reposition();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,8 +72,8 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
}
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
messageWidget.CalculatePosition();
|
||||
titleWidget.CalculatePosition();
|
||||
messageWidget.Reposition();
|
||||
titleWidget.Reposition();
|
||||
}
|
||||
|
||||
protected override void ContextLost() {
|
||||
|
@ -125,17 +125,15 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
}
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
for (int i = 0; i < buttons.Length; i++)
|
||||
buttons[i].CalculatePosition();
|
||||
title.CalculatePosition();
|
||||
RepositionWidgets(buttons);
|
||||
title.Reposition();
|
||||
}
|
||||
|
||||
public override void Render(double delta) {
|
||||
gfx.Draw2DQuad(0, 0, game.Width, game.Height, new FastColour(60, 60, 60, 160));
|
||||
gfx.Texturing = true;
|
||||
title.Render(delta);
|
||||
for (int i = 0; i < buttons.Length; i++)
|
||||
buttons[i].Render(delta);
|
||||
RenderWidgets(buttons, delta);
|
||||
gfx.Texturing = false;
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
.SetLocation(Anchor.Centre, Anchor.Centre, x, y);
|
||||
|
||||
label.XOffset = -110 - label.Width / 2;
|
||||
label.CalculatePosition();
|
||||
label.Reposition();
|
||||
label.Colour = new FastColour(224, 224, 224);
|
||||
return label;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
}
|
||||
|
||||
gfx.Texturing = true;
|
||||
RenderMenuWidgets(delta);
|
||||
RenderWidgets(widgets, delta);
|
||||
|
||||
if (extendedHelp != null && extEndY <= extClipY)
|
||||
extendedHelp.Render(delta);
|
||||
@ -72,7 +72,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
|
||||
extendedHelp.XOffset = game.Width / 2 - extendedHelp.Width / 2;
|
||||
extendedHelp.YOffset = game.Height / 2 + extHelpY;
|
||||
extendedHelp.CalculatePosition();
|
||||
extendedHelp.Reposition();
|
||||
}
|
||||
|
||||
protected override void ContextLost() {
|
||||
@ -167,7 +167,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
|
||||
extendedHelp.XOffset = game.Width / 2 - extendedHelp.Width / 2;
|
||||
extendedHelp.YOffset = game.Height / 2 + extHelpY;
|
||||
extendedHelp.CalculatePosition();
|
||||
extendedHelp.Reposition();
|
||||
}
|
||||
|
||||
void DisposeExtendedHelp() {
|
||||
|
@ -17,7 +17,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
public override void Render(double delta) {
|
||||
RenderMenuBounds();
|
||||
gfx.Texturing = true;
|
||||
RenderMenuWidgets(delta);
|
||||
RenderWidgets(widgets, delta);
|
||||
if (descWidget != null)
|
||||
descWidget.Render(delta);
|
||||
gfx.Texturing = false;
|
||||
@ -94,7 +94,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
if (descWidget != null)
|
||||
descWidget.CalculatePosition();
|
||||
descWidget.Reposition();
|
||||
base.OnResize(width, height);
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
public override void Render(double delta) {
|
||||
RenderMenuBounds();
|
||||
gfx.Texturing = true;
|
||||
RenderMenuWidgets(delta);
|
||||
RenderWidgets(widgets, delta);
|
||||
input.Render(delta);
|
||||
if (desc != null) desc.Render(delta);
|
||||
gfx.Texturing = false;
|
||||
@ -84,7 +84,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
input.CalculatePosition();
|
||||
input.Reposition();
|
||||
base.OnResize(width, height);
|
||||
}
|
||||
|
||||
|
@ -18,17 +18,10 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
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) {
|
||||
RenderMenuBounds();
|
||||
gfx.Texturing = true;
|
||||
RenderMenuWidgets(delta);
|
||||
RenderWidgets(widgets, delta);
|
||||
gfx.Texturing = false;
|
||||
}
|
||||
|
||||
@ -49,10 +42,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
protected override void ContextLost() { DisposeWidgets(widgets); }
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
for (int i = 0; i < widgets.Length; i++) {
|
||||
if (widgets[i] == null) continue;
|
||||
widgets[i].CalculatePosition();
|
||||
}
|
||||
RepositionWidgets(widgets);
|
||||
}
|
||||
|
||||
|
||||
|
@ -28,9 +28,8 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
public override void Render(double delta) {
|
||||
RenderMenuBounds();
|
||||
gfx.Texturing = true;
|
||||
RenderMenuWidgets(delta);
|
||||
for (int i = 0; i < labels.Length; i++)
|
||||
labels[i].Render(delta);
|
||||
RenderWidgets(widgets, delta);
|
||||
RenderWidgets(labels, delta);
|
||||
gfx.Texturing = false;
|
||||
|
||||
if (OnRenderFrame != null) OnRenderFrame(this);
|
||||
@ -38,8 +37,7 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
|
||||
public override void OnResize(int width, int height) {
|
||||
base.OnResize(width, height);
|
||||
for (int i = 0; i < labels.Length; i++)
|
||||
labels[i].CalculatePosition();
|
||||
RepositionWidgets(labels);
|
||||
}
|
||||
|
||||
protected override void ContextLost() {
|
||||
|
@ -43,7 +43,23 @@ namespace ClassicalSharp.Gui.Screens {
|
||||
if (widgets == null) return;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
public ButtonWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
|
||||
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
|
||||
XOffset = xOffset; YOffset = yOffset;
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
Width = Math.Max(texture.Width, MinWidth);
|
||||
Height = Math.Max(texture.Height, MinHeight);
|
||||
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
texture.X1 = X + (Width / 2 - texture.Width / 2);
|
||||
texture.Y1 = Y + (Height / 2 - texture.Height / 2);
|
||||
}
|
||||
@ -115,9 +115,9 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
gfx.DeleteTexture(ref texture);
|
||||
}
|
||||
|
||||
public override void CalculatePosition() {
|
||||
public override void Reposition() {
|
||||
int oldX = X, oldY = Y;
|
||||
base.CalculatePosition();
|
||||
base.Reposition();
|
||||
|
||||
texture.X1 += X - oldX;
|
||||
texture.Y1 += Y - oldY;
|
||||
|
@ -15,7 +15,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
public TextGroupWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
|
||||
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
|
||||
XOffset = xOffset; YOffset = yOffset;
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -107,7 +107,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
Width = Math.Max(Width, Textures[i].Width);
|
||||
Height += Textures[i].Height;
|
||||
}
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
}
|
||||
|
||||
public override void Render(double delta) {
|
||||
@ -123,9 +123,9 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
gfx.DeleteTexture(ref Textures[i]);
|
||||
}
|
||||
|
||||
public override void CalculatePosition() {
|
||||
public override void Reposition() {
|
||||
int oldY = Y;
|
||||
base.CalculatePosition();
|
||||
base.Reposition();
|
||||
if (Textures == null) return;
|
||||
|
||||
for (int i = 0; i < Textures.Length; i++) {
|
||||
|
@ -48,8 +48,8 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
|
||||
public override void Dispose() { }
|
||||
|
||||
public override void CalculatePosition() {
|
||||
base.CalculatePosition();
|
||||
public override void Reposition() {
|
||||
base.Reposition();
|
||||
Recreate();
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
public InputWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
|
||||
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
|
||||
XOffset = xOffset; YOffset = yOffset;
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -89,9 +89,9 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
Init();
|
||||
}
|
||||
|
||||
public override void CalculatePosition() {
|
||||
public override void Reposition() {
|
||||
int oldX = X, oldY = Y;
|
||||
base.CalculatePosition();
|
||||
base.Reposition();
|
||||
|
||||
caretTex.X1 += X - oldX; caretTex.Y1 += Y - oldY;
|
||||
inputTex.X1 += X - oldX; inputTex.Y1 += Y - oldY;
|
||||
@ -194,7 +194,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
|
||||
Width = size.Width;
|
||||
Height = realHeight == 0 ? prefixHeight : realHeight;
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
inputTex.X1 = X + Padding; inputTex.Y1 = Y;
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
inputTex = drawer.Make2DTexture(bmp, adjSize, 0, 0);
|
||||
}
|
||||
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
inputTex.X1 = X; inputTex.Y1 = Y;
|
||||
if (size.Height < MinHeight)
|
||||
inputTex.Y1 += MinHeight / 2 - size.Height / 2;
|
||||
|
@ -35,7 +35,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
|
||||
gfx.Texturing = true;
|
||||
overview.YOffset = Y - offset + 5;
|
||||
overview.CalculatePosition();
|
||||
overview.Reposition();
|
||||
overview.Render(delta);
|
||||
|
||||
for (int i = 0; i < namesCount; i++) {
|
||||
|
@ -105,9 +105,9 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
YOffset = -Math.Max(0, game.Height / 4 - Height / 2);
|
||||
}
|
||||
|
||||
public override void CalculatePosition() {
|
||||
public override void Reposition() {
|
||||
int oldX = X, oldY = Y;
|
||||
base.CalculatePosition();
|
||||
base.Reposition();
|
||||
|
||||
for (int i = 0; i < namesCount; i++) {
|
||||
textures[i].X1 += X - oldX;
|
||||
@ -164,7 +164,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
OnSort();
|
||||
UpdateTableDimensions();
|
||||
RecalcYOffset();
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
}
|
||||
|
||||
protected virtual void OnSort() { }
|
||||
|
@ -19,7 +19,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
public TextWidget SetLocation(Anchor horAnchor, Anchor verAnchor, int xOffset, int yOffset) {
|
||||
HorizontalAnchor = horAnchor; VerticalAnchor = verAnchor;
|
||||
XOffset = xOffset; YOffset = yOffset;
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
game.Drawer2D.ReducePadding(ref texture, Utils.Floor(font.Size));
|
||||
Width = texture.Width; Height = texture.Height;
|
||||
|
||||
CalculatePosition();
|
||||
Reposition();
|
||||
texture.X1 = X; texture.Y1 = Y;
|
||||
}
|
||||
}
|
||||
@ -70,9 +70,9 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
gfx.DeleteTexture(ref texture);
|
||||
}
|
||||
|
||||
public override void CalculatePosition() {
|
||||
public override void Reposition() {
|
||||
int oldX = X, oldY = Y;
|
||||
base.CalculatePosition();
|
||||
base.Reposition();
|
||||
|
||||
texture.X1 += X - oldX;
|
||||
texture.Y1 += Y - oldY;
|
||||
|
@ -48,7 +48,7 @@ namespace ClassicalSharp.Gui.Widgets {
|
||||
/// <summary> Specifies the boundaries of the widget in pixels. </summary>
|
||||
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);
|
||||
Y = CalcPos(VerticalAnchor, YOffset, Height, game.Height);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user