Browse Source

Shell improvement: Move RmlUi context out of shell renderer.

Michael Ragazzon 4 years ago
parent
commit
1504be8b94

+ 1 - 1
Samples/basic/animation/src/main.cpp

@@ -378,7 +378,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	EventInstancer event_listener_instancer;
 	Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);

+ 1 - 1
Samples/basic/benchmark/src/main.cpp

@@ -289,7 +289,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	EventInstancer event_listener_instancer;
 	Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);

+ 1 - 1
Samples/basic/bitmapfont/src/main.cpp

@@ -115,7 +115,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
     // Load bitmap font
 	if (!Rml::LoadFontFace("basic/bitmapfont/data/Comfortaa_Regular_22.fnt"))

+ 1 - 1
Samples/basic/customlog/src/main.cpp

@@ -102,7 +102,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/basic/databinding/src/main.cpp

@@ -446,7 +446,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 	
 	Shell::LoadFonts("assets/");
 

+ 1 - 3
Samples/basic/demo/src/main.cpp

@@ -498,9 +498,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
-	
-	context->SetDensityIndependentPixelRatio(1.0f);
+	Shell::SetContext(context);
 
 	DemoEventListenerInstancer event_listener_instancer;
 	Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);

+ 1 - 1
Samples/basic/drag/src/main.cpp

@@ -107,7 +107,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/basic/loaddocument/src/main.cpp

@@ -100,7 +100,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/basic/lottie/src/main.cpp

@@ -96,7 +96,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/basic/transform/src/main.cpp

@@ -184,7 +184,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/basic/treeview/src/main.cpp

@@ -101,7 +101,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/invaders/src/main.cpp

@@ -110,7 +110,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 	// Initialise the RmlUi debugger.
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	// Load the font faces required for Invaders.
 	Shell::LoadFonts("assets/");

+ 1 - 1
Samples/luainvaders/src/main.cpp

@@ -103,7 +103,7 @@ int main(int, char**)
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	// Load the font faces required for Invaders.
 	Shell::LoadFonts("assets/");

+ 1 - 5
Samples/shell/include/Shell.h

@@ -90,12 +90,8 @@ public:
 	/// Get clipboard text.
 	static void GetClipboardText(Rml::String& text);
 	
-	/// Sets the context to send window resized events to.
-	/// @param[in] context The context to send  events to.
+	/// Sets the RmlUi context to send window resized events to.
 	static void SetContext(Rml::Context* context);
-
-private:
-	static Rml::Context* context;
 };
 
 #include "ShellSystemInterface.h"

+ 2 - 7
Samples/shell/include/ShellRenderInterfaceExtensions.h

@@ -43,13 +43,8 @@ public:
      * @param[in] width width of viewport
      * @param[in] height height of viewport
 	 */
-    virtual void SetViewport(int width, int height) = 0;
-	
-    /**
-	 * @param[in] context Rml::Context to set dimensions on when SetViewport is called
-     */
-    virtual void SetContext(void *context) = 0;
-	
+	virtual void SetViewport(int width, int height) = 0;
+
 	/// Attach the internal window buffer to a native window
 	/// @param[in] nativeWindow A handle to the OS specific native window handle
 	virtual bool AttachToNative(void *nativeWindow) = 0;

+ 0 - 2
Samples/shell/include/ShellRenderInterfaceOpenGL.h

@@ -79,7 +79,6 @@ public:
 
 	// ShellRenderInterfaceExtensions
 	void SetViewport(int width, int height) override;
-	void SetContext(void *context) override;
 	bool AttachToNative(void *nativeWindow) override;
 	void DetachFromNative(void) override;
 	void PrepareRenderBuffer(void) override;
@@ -89,7 +88,6 @@ protected:
 	int m_width;
 	int m_height;
 	bool m_transform_enabled;
-	void *m_rmlui_context;
 	
 #if defined(RMLUI_PLATFORM_MACOSX)
 	AGLContext gl_context;

+ 1 - 1
Samples/shell/src/ShellRenderInterfaceOpenGL.cpp

@@ -35,7 +35,7 @@
 
 #define GL_CLAMP_TO_EDGE 0x812F
 
-ShellRenderInterfaceOpenGL::ShellRenderInterfaceOpenGL() : m_width(0), m_height(0), m_transform_enabled(false), m_rmlui_context(nullptr)
+ShellRenderInterfaceOpenGL::ShellRenderInterfaceOpenGL() : m_width(0), m_height(0), m_transform_enabled(false)
 {
 
 }

+ 27 - 6
Samples/shell/src/macosx/ShellMacOSX.cpp

@@ -53,15 +53,31 @@ static const EventTypeSpec WINDOW_EVENTS[] = {
 	{ kEventClassWindow, kEventWindowBoundsChanged },
 };
 
+static Rml::Context* context = nullptr;
+static ShellRenderInterfaceExtensions* shell_renderer;
+static UniquePtr<ShellFileInterface> file_interface;
+
 static WindowRef window;
 static timeval start_time;
 static Rml::String clipboard_text;
-
-static Rml::UniquePtr<ShellFileInterface> file_interface;
+static int window_width = 0;
+static int window_height = 0;
 
 static void IdleTimerCallback(EventLoopTimerRef timer, EventLoopIdleTimerMessage inState, void* p);
 static OSStatus EventHandler(EventHandlerCallRef next_handler, EventRef event, void* p);
 
+static void UpdateWindowDimensions(int width = 0, int height = 0)
+{
+	if (width > 0)
+		window_width = width;
+	if (height > 0)
+		window_height = height;
+	if (context)
+		context->SetDimensions(Rml::Vector2i(window_width, window_height));
+	if (shell_renderer)
+		shell_renderer->SetViewport(window_width, window_height);
+}
+
 bool Shell::Initialise()
 {
 	gettimeofday(&start_time, nullptr);
@@ -104,12 +120,12 @@ Rml::String Shell::FindSamplesRoot()
 	return (executable_path + "../../../" + path);
 }
 
-static ShellRenderInterfaceExtensions *shell_renderer;
-
 bool Shell::OpenWindow(const char* name, ShellRenderInterfaceExtensions *_shell_renderer, unsigned int width, unsigned int height, bool allow_resize)
 {
 	shell_renderer = _shell_renderer;
 	Rect content_bounds = { 60, 20, 60 + height, 20 + width };
+	window_width = width;
+	window_height = height;
 
 	OSStatus result = CreateNewWindow(kDocumentWindowClass,
 									  (allow_resize ? (kWindowStandardDocumentAttributes | kWindowLiveResizeAttribute) :
@@ -257,7 +273,6 @@ void Shell::SetMouseCursor(const Rml::String& cursor_name)
 	// Not implemented
 }
 
-
 void Shell::SetClipboardText(const Rml::String& text)
 {
 	// Todo: interface with system clipboard
@@ -270,6 +285,12 @@ void Shell::GetClipboardText(Rml::String& text)
 	text = clipboard_text;
 }
 
+void Shell::SetContext(Rml::Context* new_context)
+{
+	context = new_context;
+	UpdateWindowDimensions();
+}
+
 static void IdleTimerCallback(EventLoopTimerRef timer, EventLoopIdleTimerMessage inState, void* p)
 {
 	Shell::ShellIdleFunction function = (Shell::ShellIdleFunction) p;
@@ -300,7 +321,7 @@ static OSStatus EventHandler(EventHandlerCallRef next_handler, EventRef event, v
 						UInt32 width = bounds.right - bounds.left;
 						UInt32 height = bounds.bottom - bounds.top;
 
-						shell_renderer->SetViewport(width, height);
+						UpdateWindowDimensions((int)width, (int)height);
 					}
 					break;
 			}

+ 0 - 11
Samples/shell/src/macosx/ShellRenderInterfaceExtensionsOpenGL_MacOSX.cpp

@@ -29,15 +29,9 @@
 #include <ShellRenderInterfaceExtensions.h>
 #include <ShellRenderInterfaceOpenGL.h>
 #include <Carbon/Carbon.h>
-#include <RmlUi/Core/Context.h>
 #include <RmlUi/Core/Platform.h>
 #include <RmlUi/Core/Types.h>
 
-void ShellRenderInterfaceOpenGL::SetContext(void *context)
-{
-	m_rmlui_context = context;
-}
-
 void ShellRenderInterfaceOpenGL::SetViewport(int width, int height)
 {
 	if(m_width != width || m_height != height) {
@@ -55,11 +49,6 @@ void ShellRenderInterfaceOpenGL::SetViewport(int width, int height)
 		glLoadMatrixf(view);
 
 		aglUpdateContext(gl_context);
-
-		if(m_rmlui_context != nullptr)
-		{
-			((Rml::Context*)m_rmlui_context)->SetDimensions(Rml::Vector2i(width, height));
-		}
 	}
 }
 

+ 0 - 10
Samples/shell/src/win32/ShellRenderInterfaceExtensionsOpenGL_Win32.cpp

@@ -29,17 +29,12 @@
 #include <ShellRenderInterfaceExtensions.h>
 #include <ShellRenderInterfaceOpenGL.h>
 #include <win32/IncludeWindows.h>
-#include <RmlUi/Core/Context.h>
 #include <RmlUi/Core/Platform.h>
 #include <RmlUi/Core/Profiling.h>
 #include <RmlUi/Core/Types.h>
 #include <Shell.h>
 #include <string.h>
 
-void ShellRenderInterfaceOpenGL::SetContext(void *context)
-{
-	m_rmlui_context = context;
-}
 
 void ShellRenderInterfaceOpenGL::SetViewport(int width, int height)
 {
@@ -56,11 +51,6 @@ void ShellRenderInterfaceOpenGL::SetViewport(int width, int height)
 		view = Rml::Matrix4f::Identity();
 		glMatrixMode(GL_MODELVIEW);
 		glLoadMatrixf(view.data());
-
-		if(m_rmlui_context != nullptr)
-		{
-			((Rml::Context*)m_rmlui_context)->SetDimensions(Rml::Vector2i(width, height));
-		}
 	}
 }
 

+ 28 - 2
Samples/shell/src/win32/ShellWin32.cpp

@@ -37,12 +37,18 @@
 
 static LRESULT CALLBACK WindowProcedure(HWND window_handle, UINT message, WPARAM w_param, LPARAM l_param);
 
+static Rml::Context* context = nullptr;
+static ShellRenderInterfaceExtensions* shell_renderer = nullptr;
+
 static bool activated = true;
 static bool running = false;
 static Rml::U16String instance_name;
 static HWND window_handle = nullptr;
 static HINSTANCE instance_handle = nullptr;
 
+static int window_width = 0;
+static int window_height = 0;
+
 static double time_frequency;
 static LARGE_INTEGER time_startup;
 
@@ -57,6 +63,18 @@ static HCURSOR cursor_text = nullptr;
 static HCURSOR cursor_unavailable = nullptr;
 
 
+static void UpdateWindowDimensions(int width = 0, int height = 0)
+{
+	if (width > 0)
+		window_width = width;
+	if (height > 0)
+		window_height = height;
+	if (context)
+		context->SetDimensions(Rml::Vector2i(window_width, window_height));
+	if (shell_renderer)
+		shell_renderer->SetViewport(window_width, window_height);
+}
+
 bool Shell::Initialise()
 {
 	instance_handle = GetModuleHandle(nullptr);
@@ -128,7 +146,6 @@ Rml::String Shell::FindSamplesRoot()
 	return Rml::String();
 }
 
-static ShellRenderInterfaceExtensions *shell_renderer = nullptr;
 bool Shell::OpenWindow(const char* in_name, ShellRenderInterfaceExtensions *_shell_renderer, unsigned int width, unsigned int height, bool allow_resize)
 {
 	WNDCLASSW window_class;
@@ -173,6 +190,9 @@ bool Shell::OpenWindow(const char* in_name, ShellRenderInterfaceExtensions *_she
 		return false;
 	}
 
+	window_width = width;
+	window_height = height;
+
 	instance_name = name;
 
 	DWORD style = (allow_resize ? WS_OVERLAPPEDWINDOW : (WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX));
@@ -376,6 +396,12 @@ void Shell::GetClipboardText(Rml::String& text)
 	}
 }
 
+void Shell::SetContext(Rml::Context* new_context)
+{
+	context = new_context;
+	UpdateWindowDimensions();
+}
+
 static LRESULT CALLBACK WindowProcedure(HWND local_window_handle, UINT message, WPARAM w_param, LPARAM l_param)
 {
 	// See what kind of message we've got.
@@ -406,7 +432,7 @@ static LRESULT CALLBACK WindowProcedure(HWND local_window_handle, UINT message,
 		{
 			int width = LOWORD(l_param);
 			int height = HIWORD(l_param);
-			shell_renderer->SetViewport(width, height);
+			UpdateWindowDimensions(width, height);
 		}
 		break;
 

+ 0 - 11
Samples/shell/src/x11/ShellRenderInterfaceExtensionsOpenGL_X11.cpp

@@ -30,16 +30,10 @@
 #include <ShellRenderInterfaceOpenGL.h>
 #include <X11/Xlib.h>
 #include <X11/extensions/xf86vmode.h>
-#include <RmlUi/Core/Context.h>
 #include <RmlUi/Core/Platform.h>
 #include <RmlUi/Core/Types.h>
 #include "../../include/Shell.h"
 
-void ShellRenderInterfaceOpenGL::SetContext(void *context)
-{
-	m_rmlui_context = context;
-}
-
 
 void ShellRenderInterfaceOpenGL::SetViewport(int width, int height)
 {
@@ -56,11 +50,6 @@ void ShellRenderInterfaceOpenGL::SetViewport(int width, int height)
 		view = Rml::Matrix4f::Identity();
 		glMatrixMode(GL_MODELVIEW);
 		glLoadMatrixf(view.data());
-
-		if(m_rmlui_context != nullptr)
-		{
-			((Rml::Context*)m_rmlui_context)->SetDimensions(Rml::Vector2i(width, height));
-		}
 	}
 }
 

+ 34 - 8
Samples/shell/src/x11/ShellX11.cpp

@@ -28,7 +28,9 @@
 
 #include <Shell.h>
 #include <ShellOpenGL.h>
-#include <RmlUi/Core.h>
+#include <RmlUi/Core/Core.h>
+#include <RmlUi/Core/Context.h>
+#include <RmlUi/Core/Types.h>
 #include "ShellFileInterface.h"
 #include <x11/InputX11.h>
 #include <X11/Xlib.h>
@@ -45,13 +47,22 @@
 #include <stdarg.h>
 #include <string.h>
 
+static Rml::Context* context = nullptr;
+static ShellRenderInterfaceExtensions* shell_renderer = nullptr;
+
 static bool running = false;
 static int screen = -1;
 static timeval start_time;
 static Rml::String clipboard_text;
+static int window_width = 0;
+static int window_height = 0;
 
 static Rml::UniquePtr<ShellFileInterface> file_interface;
 
+static Display* display = nullptr;
+static XVisualInfo* visual_info = nullptr;
+static Window window = 0;
+
 static Cursor cursor_default = 0;
 static Cursor cursor_move = 0;
 static Cursor cursor_pointer = 0;
@@ -60,6 +71,18 @@ static Cursor cursor_cross = 0;
 static Cursor cursor_text = 0;
 static Cursor cursor_unavailable = 0;
 
+static void UpdateWindowDimensions(int width = 0, int height = 0)
+{
+	if (width > 0)
+		window_width = width;
+	if (height > 0)
+		window_height = height;
+	if (context)
+		context->SetDimensions(Rml::Vector2i(window_width, window_height));
+	if (shell_renderer)
+		shell_renderer->SetViewport(window_width, window_height);
+}
+
 static bool isRegularFile(const Rml::String& path)
 {
 	struct stat sb;
@@ -126,18 +149,15 @@ Rml::String Shell::FindSamplesRoot()
 	return Rml::String();
 }
 
-static Display* display = nullptr;
-static XVisualInfo* visual_info = nullptr;
-static Window window = 0;
-
-static ShellRenderInterfaceExtensions *shell_renderer = nullptr;
-
 bool Shell::OpenWindow(const char* name, ShellRenderInterfaceExtensions *_shell_renderer, unsigned int width, unsigned int height, bool allow_resize)
 {
 	display = XOpenDisplay(0);
 	if (display == nullptr)
 		return false;
 
+	window_width = width;
+	window_height = height;
+
 	// This initialise they keyboard to keycode mapping system of X11
 	// itself.  It must be done here as it needs to query the connected
 	// X server display for information about its install keymap abilities.
@@ -300,7 +320,7 @@ void Shell::EventLoop(ShellIdleFunction idle_function)
 					int x = event.xconfigure.width;
 					int y = event.xconfigure.height;
 
-					shell_renderer->SetViewport(x, y);
+					UpdateWindowDimensions(x, y);
 				}
 				break;
 				
@@ -412,3 +432,9 @@ void Shell::GetClipboardText(Rml::String& text)
 	// Todo: interface with system clipboard
 	text = clipboard_text;
 }
+
+void Shell::SetContext(Rml::Context* new_context)
+{
+	context = new_context;
+	UpdateWindowDimensions();
+}

+ 1 - 1
Samples/tutorial/datagrid/src/main.cpp

@@ -82,7 +82,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/tutorial/datagrid_tree/src/main.cpp

@@ -86,7 +86,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/tutorial/drag/src/main.cpp

@@ -84,7 +84,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Samples/tutorial/template/src/main.cpp

@@ -83,7 +83,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 1
Tests/Source/VisualTests/main.cpp

@@ -110,7 +110,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 
 	Rml::Debugger::Initialise(context);
 	Input::SetContext(context);
-	shell_renderer->SetContext(context);
+	Shell::SetContext(context);
 
 	Shell::LoadFonts("assets/");