Skip to content

Commit

Permalink
Merge pull request #414 from C7-Game/pcen/fix-popup-bugs
Browse files Browse the repository at this point in the history
[Godot 4] Fix Popup Bugs
  • Loading branch information
QuintillusCFC authored Apr 12, 2024
2 parents d4dfe76 + 83dfffb commit 8e9feff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 50 deletions.
23 changes: 15 additions & 8 deletions C7/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ enum GameState {
public AnimationManager civ3AnimData;
public AnimationTracker animTracker;

Hashtable Terrmask = new Hashtable();
GameState CurrentState = GameState.PreGame;

// CurrentlySelectedUnit is a reference directly into the game state so be careful of race conditions. TODO: Consider storing a GUID instead.
Expand All @@ -47,6 +46,8 @@ enum GameState {
Stopwatch loadTimer = new Stopwatch();
GlobalSingleton Global;

private PopupOverlay popupOverlay;

bool errorOnLoad = false;

public override void _EnterTree() {
Expand All @@ -58,6 +59,8 @@ public override void _EnterTree() {
// that gives an error if we fail to load for some reason.
public override void _Ready() {
Global = GetNode<GlobalSingleton>("/root/GlobalSingleton");
popupOverlay = GetNode<PopupOverlay>(PopupOverlay.NodePath);

try {
var animSoundPlayer = new AudioStreamPlayer();
AddChild(animSoundPlayer);
Expand Down Expand Up @@ -105,7 +108,6 @@ public override void _Ready() {
log.Information("Game scene load time: " + Convert.ToInt32(stopwatchElapsed.TotalMilliseconds) + " ms");
} catch (Exception ex) {
errorOnLoad = true;
PopupOverlay popupOverlay = GetNode<PopupOverlay>(PopupOverlay.NodePath);
string message = ex.Message;
string[] stack = ex.StackTrace.Split("\r\n"); //for some reason it is returned with \r\n in the string as one line. let's make it readable!
foreach (string line in stack) {
Expand Down Expand Up @@ -166,7 +168,7 @@ public void updateAnimations(GameData gameData) {
}

public override void _Process(double delta) {
this.processActions();
processActions();

// TODO: Is it necessary to keep the game data mutex locked for this entire method?
using (var gameDataAccess = new UIGameDataAccess()) {
Expand Down Expand Up @@ -440,6 +442,15 @@ public override void _UnhandledInput(InputEvent @event) {

// Handle Godot keybind actions
private void processActions() {
if (Input.IsActionJustPressed(C7Action.Escape) && popupOverlay.ShowingPopup) {
popupOverlay.OnHidePopup();
return;
}

// never poll for actions if UI elements are visible
if (popupOverlay.Visible) {
return;
}

if (Input.IsActionJustPressed(C7Action.EndTurn) && !this.HasCurrentlySelectedUnit()) {
log.Verbose("end_turn key pressed");
Expand Down Expand Up @@ -481,7 +492,6 @@ private void processActions() {

if (Input.IsActionJustPressed(C7Action.Escape) && !this.inUnitGoToMode) {
log.Debug("Got request for escape/quit");
PopupOverlay popupOverlay = GetNode<PopupOverlay>(PopupOverlay.NodePath);
popupOverlay.ShowPopup(new EscapeQuitPopup(), PopupOverlay.PopupCategory.Info);
}

Expand Down Expand Up @@ -520,7 +530,6 @@ private void processActions() {
}

if (Input.IsActionJustPressed(C7Action.UnitDisband)) {
PopupOverlay popupOverlay = GetNode<PopupOverlay>(PopupOverlay.NodePath);
popupOverlay.ShowPopup(new DisbandConfirmation(CurrentlySelectedUnit), PopupOverlay.PopupCategory.Advisor);
}

Expand Down Expand Up @@ -548,9 +557,7 @@ private void processActions() {
MapUnit currentUnit = gameDataAccess.gameData.GetUnit(CurrentlySelectedUnit.guid);
log.Debug(currentUnit.Describe());
if (currentUnit.canBuildCity()) {
PopupOverlay popupOverlay = GetNode<PopupOverlay>(PopupOverlay.NodePath);
popupOverlay.ShowPopup(new BuildCityDialog(controller.GetNextCityName()),
PopupOverlay.PopupCategory.Advisor);
popupOverlay.ShowPopup(new BuildCityDialog(controller.GetNextCityName()), PopupOverlay.PopupCategory.Advisor);
}
}
}
Expand Down
13 changes: 0 additions & 13 deletions C7/UIElements/Popups/BuildCityDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,4 @@ public void OnCityNameEntered(string name)
GetParent().EmitSignal("HidePopup");
}

public override void _UnhandledInput(InputEvent @event)
{
if (this.Visible) {
if (@event is InputEventKey eventKey && eventKey.Pressed)
{
if (eventKey.Keycode == Godot.Key.Escape)
{
GetViewport().SetInputAsHandled();
GetParent().EmitSignal("HidePopup");
}
}
}
}
}
45 changes: 16 additions & 29 deletions C7/UIElements/Popups/PopupOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Godot;
using ConvertCiv3Media;
using System;
using System.Diagnostics;
using Serilog;

public partial class PopupOverlay : HBoxContainer
Expand All @@ -16,26 +13,23 @@ public partial class PopupOverlay : HBoxContainer

Control currentChild = null;

public const string NodePath = "/root/C7Game/CanvasLayer/PopupOverlay";
public static readonly string NodePath = "/root/C7Game/CanvasLayer/PopupOverlay";

public enum PopupCategory {
Advisor,
Console,
Info //Sounds similar to the above, but lower-pitched in the second half
}

public override void _Ready()
public void OnHidePopup()
{
base._Ready();
}

private void OnHidePopup()
{
log.Debug("Hiding popup");
RemoveChild(currentChild);
currentChild = null;
Hide();
}

public bool ShowingPopup => currentChild is not null;

public void PlaySound(AudioStreamWav wav)
{
AudioStreamPlayer player = GetNode<AudioStreamPlayer>("PopupSound");
Expand All @@ -45,8 +39,8 @@ public void PlaySound(AudioStreamWav wav)

public void ShowPopup(Popup child, PopupCategory category)
{
if (child == null) // not necessary if we don't pass null?
{
if (child is null) {
// not necessary if we don't pass null?
log.Error("Received request to show null popup");
return;
}
Expand All @@ -60,24 +54,17 @@ public void ShowPopup(Popup child, PopupCategory category)
AddChild(child);
currentChild = child;

string soundFile = "";
switch (category)
{
case PopupCategory.Advisor:
soundFile = "Sounds/PopupAdvisor.wav";
break;
case PopupCategory.Console:
soundFile = "Sounds/PopupConsole.wav";
break;
case PopupCategory.Info:
soundFile = "Sounds/PopupInfo.wav";
break;
default:
log.Error("Invalid popup category");
break;
string soundFile = category switch {
PopupCategory.Advisor => "Sounds/PopupAdvisor.wav",
PopupCategory.Console => "Sounds/PopupConsole.wav",
PopupCategory.Info => "Sounds/PopupInfo.wav",
_ => "",
};
if (soundFile == "") {
log.Error("Invalid popup category");
}
AudioStreamWav wav = Util.LoadWAVFromDisk(Util.Civ3MediaPath(soundFile));
Visible = true;
Show();
PlaySound(wav);
}

Expand Down

0 comments on commit 8e9feff

Please sign in to comment.