skyscraper8/GUIs/skyscraper8.UI.ImGui/Forms/SatellitesConfigurationWindow.cs
2025-06-30 22:57:40 +02:00

123 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImGuiNET;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.Scraper.Storage;
using skyscraper8.Skyscraper.Scraper.Storage;
namespace SDL2Demo.Forms
{
internal class SatellitesConfigurationWindow : IRenderable
{
public SatellitesConfigurationWindow(DataStorage storage)
{
_storage = storage;
lnbPopupUuid = Guid.NewGuid().ToString();
name = "";
memoryTableUuid = Guid.NewGuid().ToString();
allPositions = storage.UiSatellitesListAll();
}
private string lnbPopupUuid;
private string memoryTableUuid;
private List<SatellitePosition> allPositions;
private readonly DataStorage _storage;
private string name;
private float degrees;
private int cardinalDirection;
private void CheckBounds()
{
if (degrees < 0.0)
degrees = 0.0f;
if (degrees > 180.0)
degrees = 180.0f;
}
private bool SaveButtonEnabled()
{
if (string.IsNullOrEmpty(name))
return false;
if (string.IsNullOrWhiteSpace(name))
return false;
if (allPositions.Count == 0)
return true;
int currentChecksum = SatellitePosition.GetChecksum(degrees, cardinalDirection);
foreach (SatellitePosition satellitePosition in allPositions)
{
if (currentChecksum == satellitePosition.Checksum)
return false;
}
return true;
}
public void Render()
{
bool pOpen = true;
ImGui.Begin("Configure satellite positions", ref pOpen);
if (ImGui.TreeNode("Add satellite position"))
{
ImGui.InputText("Name", ref name, 255);
ImGui.InputFloat("Position", ref degrees, 0.1f);
ImGui.RadioButton("East", ref cardinalDirection, 0);
ImGui.SameLine();
ImGui.RadioButton("West", ref cardinalDirection, 1);
CheckBounds();
ImGui.SameLine();
ImGui.BeginDisabled(!SaveButtonEnabled());
if (ImGui.Button("Add"))
{
SatellitePosition newPosition = new SatellitePosition(degrees, cardinalDirection, name);
allPositions.Add(newPosition);
_storage.UiSatellitesAdd(newPosition);
}
ImGui.EndDisabled();
}
ImGui.Separator();
ImGui.BeginTable(memoryTableUuid, 4);
foreach (SatellitePosition satellitePosition in allPositions)
{
ImGui.TableNextRow();
ImGui.TableSetColumnIndex(0);
ImGui.Text(satellitePosition.name);
ImGui.TableSetColumnIndex(1);
ImGui.Text(String.Format("{0:0.0}° {1}", satellitePosition.angle, satellitePosition.cardinalDirection == 0 ? "E" : "W"));
ImGui.TableSetColumnIndex(2);
ImGui.Text("");
ImGui.TableSetColumnIndex(3);
ImGui.PushID(String.Format("RemoveSat {0}", satellitePosition.Checksum));
if (ImGui.Button("Remove"))
{
_storage.UiSatellitesDelete(satellitePosition);
allPositions.Remove(satellitePosition);
break;
}
ImGui.PopID();
}
ImGui.EndTable();
ImGui.End();
if (!pOpen)
Closed = true;
}
public bool Closed { get; set; }
}
}