From 1dcc017daffdc6bd3970fc16ec02cece0ed9c3fe Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Wed, 29 Mar 2017 23:51:53 +0200
Subject: [PATCH] - reimplemented the position display, but changed its
 position a bit upward. - activated the RenderOverlay event, now that it can
 be called from the correct spot, i.e. right after the top level HUD messages
 are drawn. The system's status output will still be drawn on top of them.

---
 src/d_main.cpp                                |  1 -
 src/events.cpp                                | 10 +++-
 src/events.h                                  |  6 +-
 src/g_statusbar/shared_sbar.cpp               | 12 +++-
 wadsrc/static/zscript/events.txt              |  2 +-
 wadsrc/static/zscript/statusbar/statusbar.txt | 57 +++++--------------
 6 files changed, 38 insertions(+), 50 deletions(-)

diff --git a/src/d_main.cpp b/src/d_main.cpp
index 2ba45fe1d4..80a37b21e4 100644
--- a/src/d_main.cpp
+++ b/src/d_main.cpp
@@ -900,7 +900,6 @@ void D_Display ()
 		NetUpdate ();			// send out any new accumulation
 		// normal update
 		// draw ZScript UI stuff
-		//E_RenderOverlay();
 		C_DrawConsole (hw2d);	// draw console
 		M_Drawer ();			// menu is drawn even on top of everything
 		FStat::PrintStat ();
diff --git a/src/events.cpp b/src/events.cpp
index 5a40a39835..970b68da5f 100755
--- a/src/events.cpp
+++ b/src/events.cpp
@@ -448,6 +448,12 @@ void E_Console(int player, FString name, int arg1, int arg2, int arg3, bool manu
 		handler->ConsoleProcess(player, name, arg1, arg2, arg3, manual);
 }
 
+void E_RenderOverlay(EHudState state)
+{
+	for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
+		handler->RenderOverlay(state);
+}
+
 bool E_CheckUiProcessors()
 {
 	for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
@@ -468,7 +474,6 @@ bool E_CheckRequireMouse()
 
 // normal event loopers (non-special, argument-less)
 DEFINE_EVENT_LOOPER(RenderFrame)
-DEFINE_EVENT_LOOPER(RenderOverlay)
 DEFINE_EVENT_LOOPER(WorldLightning)
 DEFINE_EVENT_LOOPER(WorldTick)
 DEFINE_EVENT_LOOPER(UiTick)
@@ -798,7 +803,7 @@ void DStaticEventHandler::RenderFrame()
 	}
 }
 
-void DStaticEventHandler::RenderOverlay()
+void DStaticEventHandler::RenderOverlay(EHudState state)
 {
 	IFVIRTUAL(DStaticEventHandler, RenderOverlay)
 	{
@@ -806,6 +811,7 @@ void DStaticEventHandler::RenderOverlay()
 		if (func == DStaticEventHandler_RenderOverlay_VMPtr)
 			return;
 		FRenderEvent e = E_SetupRenderEvent();
+		e.HudState = int(state);
 		VMValue params[2] = { (DStaticEventHandler*)this, &e };
 		GlobalVMStack.Call(func, params, 2, nullptr, 0, nullptr);
 	}
diff --git a/src/events.h b/src/events.h
index a2f014a32b..5ef7f85aa4 100755
--- a/src/events.h
+++ b/src/events.h
@@ -5,6 +5,7 @@
 #include "serializer.h"
 #include "d_event.h"
 #include "d_gui.h"
+#include "sbar.h"
 
 class DStaticEventHandler;
 
@@ -48,7 +49,7 @@ void E_UiTick();
 // called on each render frame once.
 void E_RenderFrame();
 // called after everything's been rendered, but before console/menus
-void E_RenderOverlay();
+void E_RenderOverlay(EHudState state);
 // this executes when a player enters the level (once). PlayerEnter+inhub = RETURN
 void E_PlayerEntered(int num, bool fromhub);
 // this executes when a player respawns. includes resurrect cheat.
@@ -141,7 +142,7 @@ public:
 
 	//
 	void RenderFrame();
-	void RenderOverlay();
+	void RenderOverlay(EHudState state);
 
 	//
 	void PlayerEntered(int num, bool fromhub);
@@ -175,6 +176,7 @@ struct FRenderEvent
 	DAngle ViewRoll;
 	double FracTic = 0; // 0..1 value that describes where we are inside the current gametic, render-wise.
 	AActor* Camera = nullptr;
+	int HudState;
 };
 
 struct FWorldEvent
diff --git a/src/g_statusbar/shared_sbar.cpp b/src/g_statusbar/shared_sbar.cpp
index adec61bf5d..1cb3fe32cd 100644
--- a/src/g_statusbar/shared_sbar.cpp
+++ b/src/g_statusbar/shared_sbar.cpp
@@ -61,6 +61,7 @@
 #include "p_acs.h"
 #include "r_data/r_translate.h"
 #include "sbarinfo.h"
+#include "events.h"
 
 #include "../version.h"
 
@@ -975,7 +976,14 @@ void DBaseStatusBar::Draw (EHudState state)
 	}
 
 	if (idmypos)
-	{ // Draw current coordinates
+	{ 
+		// Draw current coordinates
+		IFVIRTUAL(DBaseStatusBar, DrawMyPos)
+		{
+			VMValue params[] = { (DObject*)this };
+			GlobalVMStack.Call(func, params, countof(params), nullptr, 0);
+		}
+		V_SetBorderNeedRefresh();
 	}
 
 	if (viewactive)
@@ -1150,6 +1158,8 @@ void DBaseStatusBar::DrawTopStuff (EHudState state)
 		DrawMessages (HUDMSGLayer_OverMap, (state == HUD_StatusBar) ? GetTopOfStatusbar() : SCREENHEIGHT);
 	}
 	DrawMessages (HUDMSGLayer_OverHUD, (state == HUD_StatusBar) ? GetTopOfStatusbar() : SCREENHEIGHT);
+	E_RenderOverlay(state);
+
 	DrawConsistancy ();
 	DrawWaiting ();
 	if (ShowLog && MustDrawLog(state)) DrawLog ();
diff --git a/wadsrc/static/zscript/events.txt b/wadsrc/static/zscript/events.txt
index 0c0947f45a..f3a281e0c5 100755
--- a/wadsrc/static/zscript/events.txt
+++ b/wadsrc/static/zscript/events.txt
@@ -305,7 +305,7 @@ class StaticEventHandler : Object native play version("2.4")
 
     //
     //virtual native ui void RenderFrame(RenderEvent e);
-    //virtual native ui void RenderOverlay(RenderEvent e);
+    virtual native ui void RenderOverlay(RenderEvent e);
     
     //
     virtual native void PlayerEntered(PlayerEvent e);
diff --git a/wadsrc/static/zscript/statusbar/statusbar.txt b/wadsrc/static/zscript/statusbar/statusbar.txt
index 8a7476f8f4..238e177bb2 100644
--- a/wadsrc/static/zscript/statusbar/statusbar.txt
+++ b/wadsrc/static/zscript/statusbar/statusbar.txt
@@ -682,59 +682,30 @@ class BaseStatusBar native ui
 
 	virtual void DrawMyPos()
 	{
-		/*
-		int height = SmallFont->GetHeight();
-		char labels[3] = { 'X', 'Y', 'Z' };
+		int height = SmallFont.GetHeight();
 		int i;
 
 		int vwidth;
 		int vheight;
 		int xpos;
 		int y;
+		let scalevec = GetHUDScale();
+		int scale = int(scalevec.X);
 
-		if (active_con_scaletext() == 1)
-		{
-			vwidth = SCREENWIDTH;
-			vheight = SCREENHEIGHT;
-			xpos = vwidth - 80;
-			y = SBarTop - height;
-		}
-		else if (active_con_scaletext() > 1)
-		{
-			vwidth = SCREENWIDTH / active_con_scaletext();
-			vheight = SCREENHEIGHT / active_con_scaletext();
-			xpos = vwidth - SmallFont->StringWidth("X: -00000")-6;
-			y = SBarTop/4 - height;
-		}
-		else
-		{
-			vwidth = SCREENWIDTH/2;
-			vheight = SCREENHEIGHT/2;
-			xpos = vwidth - SmallFont->StringWidth("X: -00000")-6;
-			y = SBarTop/2 - height;
-		}
+		vwidth = screen.GetWidth() / scale;
+		vheight = screen.GetHeight() / scale;
+		xpos = vwidth - SmallFont.StringWidth("X: -00000")-6;
+		y = GetTopOfStatusBar() / (3*scale) - height;
 
-		if (gameinfo.gametype == GAME_Strife)
+		Vector3 pos = CPlayer.mo.Pos;
+		
+		for (i = 0; i < 3; y += height, ++i)
 		{
-			if (active_con_scaletext() == 1)
-				y -= height * 4;
-			else if (active_con_scaletext() > 3)
-				y -= height;
-			else
-				y -= height * 2;
+			double v = i == 0? pos.X : i == 1? pos.Y : pos.Z;
+			String line = String.Format("%c: %d", int("X") + i, v);
+			screen.DrawText (SmallFont, Font.CR_GREEN, xpos, y, line, DTA_KeepRatio, true,
+				DTA_VirtualWidth, vwidth, DTA_VirtualHeight, vheight);
 		}
-
-		DVector3 pos = CPlayer->mo->Pos();
-		for (i = 2; i >= 0; y -= height, --i)
-		{
-			mysnprintf (line, countof(line), "%c: %d", labels[i], int(pos[i]));
-			screen->DrawText (SmallFont, CR_GREEN, xpos, y, line, 
-				DTA_KeepRatio, true,
-				DTA_VirtualWidth, vwidth, DTA_VirtualHeight, vheight, 				
-				TAG_DONE);
-			V_SetBorderNeedRefresh();
-		}
-		*/
 	}
 	
 	//============================================================================