Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor credits window #2349

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions Intersect.Client/Interface/Menu/CreditsParser.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
using Intersect.Client.Framework.Gwen;
using Intersect.Client.Framework.Gwen;

namespace Intersect.Client.Interface.Menu;


partial class Credits
{

public List<CreditsLine> Lines = new List<CreditsLine>();
public List<CreditsLine> Lines = [];

public partial struct CreditsLine
{

public string Text;

public string Font;

public int Size;

public string Alignment;

public Color Clr;
public Color TextColor;

public Alignments GetAlignment()
{
switch (Alignment)
return Alignment switch
{
case "center":
return Alignments.CenterH;
case "right":
return Alignments.Right;
default:
return Alignments.Left;
}
"center" => Alignments.CenterH,
"right" => Alignments.Right,
_ => Alignments.Left,
};
}

}

}
98 changes: 35 additions & 63 deletions Intersect.Client/Interface/Menu/CreditsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,108 +8,80 @@

namespace Intersect.Client.Interface.Menu;


public partial class CreditsWindow : IMainMenuWindow
public partial class CreditsWindow : ImagePanel, IMainMenuWindow
{
private readonly MainMenu _mainMenu;
private readonly RichLabel _richLabel;

private Button mBackBtn;

//Content
private ScrollControl mCreditsContent;

//Parent
private Label mCreditsHeader;

//Controls
private ImagePanel mCreditsWindow;

private MainMenu mMainMenu;

private RichLabel mRichLabel;

//Init
public CreditsWindow(Canvas parent, MainMenu mainMenu)
public CreditsWindow(Canvas parent, MainMenu mainMenu) : base(parent, "CreditsWindow")
{
//Assign References
mMainMenu = mainMenu;
_mainMenu = mainMenu;

//Main Menu Window
mCreditsWindow = new ImagePanel(parent, "CreditsWindow");
var _creditsHeader = new Label(this, "CreditsHeader") { Text = Strings.Credits.Title };

//Menu Header
mCreditsHeader = new Label(mCreditsWindow, "CreditsHeader");
mCreditsHeader.SetText(Strings.Credits.Title);
var _creditsContent = new ScrollControl(this, "CreditsScrollview");
_creditsContent.EnableScroll(false, true);

mCreditsContent = new ScrollControl(mCreditsWindow, "CreditsScrollview");
mCreditsContent.EnableScroll(false, true);
_richLabel = new RichLabel(_creditsContent, "CreditsLabel");

mRichLabel = new RichLabel(mCreditsContent, "CreditsLabel");
var _btnBack = new Button(this, "BackButton") { Text = Strings.Credits.Back };
_btnBack.Clicked += BackBtn_Clicked;

//Back Button
mBackBtn = new Button(mCreditsWindow, "BackButton");
mBackBtn.SetText(Strings.Credits.Back);
mBackBtn.Clicked += BackBtn_Clicked;

mCreditsWindow.LoadJsonUi(GameContentManager.UI.Menu, Graphics.Renderer.GetResolutionString());
LoadJsonUi(GameContentManager.UI.Menu, Graphics.Renderer?.GetResolutionString());
}

private void BackBtn_Clicked(Base sender, ClickedEventArgs arguments)
{
Hide();
mMainMenu.Show();
}

//Methods
public void Update()
{
}

public void Hide()
{
mCreditsWindow.IsHidden = true;
_mainMenu.Show();
}

public void Show()
public override void Show()
{
mCreditsWindow.IsHidden = false;
mRichLabel.ClearText();
base.Show();
_richLabel.ClearText();
var credits = new Credits();
var creditsFile = Path.Combine(ClientConfiguration.ResourcesDirectory, "credits.json");

if (File.Exists(creditsFile))
{
credits = JsonConvert.DeserializeObject<Credits>(File.ReadAllText(creditsFile));
}
else
{
var line = new Credits.CreditsLine();
line.Text = "Insert your credits here!";
line.Alignment = "center";
line.Size = 12;
line.Clr = Intersect.Color.White;
line.Font = "sourcesansproblack";
var line = new Credits.CreditsLine
{
Text = "Insert your credits here!",
Alignment = "center",
Size = 12,
TextColor = Color.White,
Font = "sourcesansproblack"
};

credits.Lines.Add(line);
}

File.WriteAllText(creditsFile, JsonConvert.SerializeObject(credits, Formatting.Indented));

foreach (var line in credits.Lines)
foreach (var line in credits?.Lines ?? [])
{
if (line.Text.Trim().Length == 0)
{
mRichLabel.AddLineBreak();
_richLabel.AddLineBreak();
}
else
{
mRichLabel.AddText(
line.Text, new Color(line.Clr.A, line.Clr.R, line.Clr.G, line.Clr.B), line.GetAlignment(),
GameContentManager.Current.GetFont(line.Font, line.Size)
_richLabel.AddText(
text: line.Text,
color: new Color(line.TextColor?.A ?? 255, line.TextColor?.R ?? 255, line.TextColor?.G ?? 255, line.TextColor?.B ?? 255),
alignment: line.GetAlignment(),
font: GameContentManager.Current.GetFont(line.Font, line.Size)
);

mRichLabel.AddLineBreak();
_richLabel.AddLineBreak();
}
}

mRichLabel.SizeToChildren(false, true);
_ = _richLabel.SizeToChildren(false, true);
}

}
Loading