Files
RuinAdventurer/Scripts/Helpers/UIHandler.cs
T

117 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Godot;
public partial class UIHandler : Control
{
[Export] CodingWindow codingWindow;
[Export] RobotList robotList;
[Export] Camera3D mainCam;
[Export] Map map;
[Export] RichTextLabel FPS;
[Export] RichTextLabel RAM;
[Export] PanelContainer options;
[Export] Control uiContent;
[Export] PanelContainer menu;
[Export] PanelContainer inventory;
[Export] ResearchList researchList;
bool receivedRobotJumpSignal = false;
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
DisplayStats();
robotList.OnRobotJumpTo += (robot) =>
{
if(receivedRobotJumpSignal) return;
receivedRobotJumpSignal = true;
mainCam.Position = new Vector3(robot.Position.X, mainCam.Position.Y, robot.Position.Z + 3f);
codingWindow.SetRobot(robot);
OpenUIElement(codingWindow);
};
//Enable user to write in input fields
Control focused = GetViewport().GuiGetFocusOwner();
if (focused is LineEdit || focused is TextEdit)
return;
if (Input.IsActionJustPressed("map")) HandleMapButton();
if (Input.IsActionJustPressed("menu")) HandleMenuButton();
if (Input.IsActionJustPressed("robot_list")) HandleRobotListButton();
if (Input.IsActionJustPressed("inventory")) HandleInventoryButton();
if (Input.IsActionJustPressed("research")) HandleResearchButton();
}
public void HandleMenuButton()
{
OpenUIElement(menu);
}
public void HandleMapButton()
{
OpenUIElement(map);
if(map.Visible) map.ShowMap();
}
public void HandleRobotListButton()
{
receivedRobotJumpSignal = false;
OpenUIElement(robotList);
}
public void HandleInventoryButton()
{
OpenUIElement(inventory);
}
public void HandleResearchButton()
{
OpenUIElement(researchList);
if(researchList.Visible) researchList.SetupGraph();
}
public void DisplayStats()
{
FPS.Text = Engine.GetFramesPerSecond().ToString() + " FPS";
double memory = Process.GetCurrentProcess().WorkingSet64 / (1024 * 1024);
string memoryDisplay = memory > 1024 ? Math.Round(memory / 1024, 2).ToString() + " GB" : memory.ToString() + " MB";
RAM.Text = memoryDisplay;
}
public void ExitGame()
{
GetTree().ChangeSceneToFile("res://Scenes/MainMenu.tscn");
}
public void OpenUIElement(Control element)
{
if (element.Visible)
{
element.Hide();
}
else
{
element.Show();
}
HideUIElements(element);
}
private void HideUIElements(Control element)
{
foreach (PanelContainer child in uiContent.GetChildren())
{
if (child == element) continue;
child.Visible = false;
}
}
}