From c6d9823cfa6290e46310470ac58dadacbdbd3948 Mon Sep 17 00:00:00 2001
From: Sebastian Gaida <gaida@ca-digit.com>
Date: Tue, 24 Aug 2021 13:28:09 +0200
Subject: [PATCH] [#89] create get focussed window function

---
 include/vkcv/Window.hpp | 22 +++++++++++------
 src/vkcv/Window.cpp     | 53 +++++++++++++++++++++++++----------------
 2 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/include/vkcv/Window.hpp b/include/vkcv/Window.hpp
index d7506371..82f21331 100644
--- a/include/vkcv/Window.hpp
+++ b/include/vkcv/Window.hpp
@@ -1,9 +1,4 @@
 #pragma once
-/**
- * @authors Sebastian Gaida
- * @file src/vkcv/Window.hpp
- * @brief Window class to handle a basic rendering surface and input
- */
 
 #define NOMINMAX
 #include <algorithm>
@@ -16,11 +11,12 @@ struct GLFWwindow;
 namespace vkcv {
 
     class Window {
+		friend class WindowManager;
 	private:
     	std::string m_title;
     	bool m_resizable;
 		GLFWwindow *m_window;
-		
+
     public:
     	/**
     	 * creates an uninitialized #Window
@@ -60,6 +56,13 @@ namespace vkcv {
         [[nodiscard]]
         bool isOpen() const;
 
+		/**
+		 * gets the currently focused window and returns it
+		 * only accessible to WindowManager
+		 * @return
+		 */
+		static Window& getFocusedWindow();
+
         /**
          * polls all events on the GLFWwindow
          */
@@ -115,7 +118,12 @@ namespace vkcv {
          * Destructor of #Window, terminates GLFW
          */
         virtual ~Window();
-        
+
+        /**
+         * destroys the window
+         */
+		void destroyWindow();
+
         /**
          * gets the windows framebuffer size
          * @param width
diff --git a/src/vkcv/Window.cpp b/src/vkcv/Window.cpp
index 7dce58ff..d51bb65a 100644
--- a/src/vkcv/Window.cpp
+++ b/src/vkcv/Window.cpp
@@ -1,8 +1,3 @@
-/**
- * @authors Sebastian Gaida
- * @file src/vkcv/Window.cpp
- * @brief Window class to handle a basic rendering surface and input
- */
 
 #include <thread>
 #include <vector>
@@ -63,20 +58,7 @@ namespace vkcv {
 	static std::vector<GLFWwindow*> s_Windows;
 
 	void Window_onGamepadEvent(int gamepadIndex) {
-		size_t activeWindowIndex = std::find_if(
-				s_Windows.begin(),
-				s_Windows.end(),
-				[](GLFWwindow* window){return glfwGetWindowAttrib(window, GLFW_FOCUSED);}
-				) - s_Windows.begin();
-	
-		// fixes index getting out of bounds (e.g. if there is no focused window)
-		activeWindowIndex *= (activeWindowIndex < s_Windows.size());
-  
-		auto window = static_cast<Window *>(glfwGetWindowUserPointer(s_Windows[activeWindowIndex]));
-
-		if (window != nullptr) {
-			window->e_gamepad(gamepadIndex);
-		}
+			Window::getFocusedWindow().e_gamepad(gamepadIndex);
 	}
 	
 	static GLFWwindow* createGLFWWindow(const char *windowTitle, int width, int height, bool resizable) {
@@ -162,6 +144,21 @@ namespace vkcv {
             glfwTerminate();
         }
     }
+
+    void Window::destroyWindow() {
+		Window::e_mouseButton.unlock();
+		Window::e_mouseMove.unlock();
+		Window::e_mouseScroll.unlock();
+		Window::e_resize.unlock();
+		Window::e_key.unlock();
+		Window::e_char.unlock();
+		Window::e_gamepad.unlock();
+
+		if (m_window) {
+			s_Windows.erase(std::find(s_Windows.begin(), s_Windows.end(), m_window));
+			glfwDestroyWindow(m_window);
+		}
+	}
     
     Window::Window(const Window &other) :
     m_title(other.getTitle()),
@@ -291,5 +288,21 @@ namespace vkcv {
 			height = 0;
 		}
     }
-    
+
+	Window& Window::getFocusedWindow() {
+		static Window defaultWindow;
+
+		auto activeWindowIterator = std::find_if(
+				s_Windows.begin(),
+				s_Windows.end(),
+				[](GLFWwindow *window) { return glfwGetWindowAttrib(window, GLFW_FOCUSED); }
+		);
+
+		if( activeWindowIterator == s_Windows.end() )
+		{
+			return defaultWindow;
+		}
+		Window& window = *static_cast<Window *>(glfwGetWindowUserPointer(*activeWindowIterator));
+		return window;
+	}
 }
-- 
GitLab