2
0
Эх сурвалжийг харах

Create new demo sample (only a skeleton for now)

Michael Ragazzon 6 жил өмнө
parent
commit
ebd29bb73c

+ 4 - 1
CMakeLists.txt

@@ -398,7 +398,7 @@ endmacro()
 if(BUILD_SAMPLES)
 if(BUILD_SAMPLES)
 	include(SampleFileList)
 	include(SampleFileList)
 
 
-	set(samples treeview customlog drag loaddocument transform bitmapfont animation benchmark)
+	set(samples treeview customlog drag loaddocument transform bitmapfont animation benchmark demo)
 	set(tutorials template datagrid datagrid_tree drag)
 	set(tutorials template datagrid datagrid_tree drag)
 	
 	
 if(NOT BUILD_FRAMEWORK)
 if(NOT BUILD_FRAMEWORK)
@@ -661,6 +661,9 @@ if(BUILD_SAMPLES)
 	install(DIRECTORY ${PROJECT_SOURCE_DIR}/Samples/basic/benchmark/data
 	install(DIRECTORY ${PROJECT_SOURCE_DIR}/Samples/basic/benchmark/data
 			DESTINATION ${SAMPLES_DIR}/basic/benchmark
 			DESTINATION ${SAMPLES_DIR}/basic/benchmark
 	)
 	)
+	install(DIRECTORY ${PROJECT_SOURCE_DIR}/Samples/basic/demo/data
+			DESTINATION ${SAMPLES_DIR}/basic/demo
+	)
 	install(DIRECTORY ${PROJECT_SOURCE_DIR}/Samples/basic/transform/data
 	install(DIRECTORY ${PROJECT_SOURCE_DIR}/Samples/basic/transform/data
 			DESTINATION ${SAMPLES_DIR}/basic/transform
 			DESTINATION ${SAMPLES_DIR}/basic/transform
 	)
 	)

+ 7 - 0
Cmake/SampleFileList.cmake

@@ -48,6 +48,13 @@ set(customlog_SRC_FILES
     ${PROJECT_SOURCE_DIR}/Samples/basic/customlog/src/SystemInterface.cpp
     ${PROJECT_SOURCE_DIR}/Samples/basic/customlog/src/SystemInterface.cpp
 )
 )
 
 
+set(demo_HDR_FILES
+)
+
+set(demo_SRC_FILES
+    ${PROJECT_SOURCE_DIR}/Samples/basic/demo/src/main.cpp
+)
+
 set(drag_HDR_FILES
 set(drag_HDR_FILES
     ${PROJECT_SOURCE_DIR}/Samples/basic/drag/src/DragListener.h
     ${PROJECT_SOURCE_DIR}/Samples/basic/drag/src/DragListener.h
     ${PROJECT_SOURCE_DIR}/Samples/basic/drag/src/Inventory.h
     ${PROJECT_SOURCE_DIR}/Samples/basic/drag/src/Inventory.h

+ 1 - 1
Cmake/gen_samplelists.sh

@@ -7,7 +7,7 @@ hdr='set(sample_HDR_FILES'
 srcdir='${PROJECT_SOURCE_DIR}'
 srcdir='${PROJECT_SOURCE_DIR}'
 srcpath=Samples
 srcpath=Samples
 samples=( 'shell'
 samples=( 'shell'
-	'basic/animation' 'basic/benchmark' 'basic/bitmapfont' 'basic/customlog' 'basic/drag' 'basic/loaddocument' 'basic/treeview' 'basic/transform'
+	'basic/animation' 'basic/benchmark' 'basic/bitmapfont' 'basic/customlog' 'basic/demo' 'basic/drag' 'basic/loaddocument' 'basic/treeview' 'basic/transform'
 	'basic/sdl2' 'basic/sfml2'
 	'basic/sdl2' 'basic/sfml2'
 	'basic/directx10'
 	'basic/directx10'
 	'tutorial/template' 'tutorial/datagrid' 'tutorial/datagrid_tree' 'tutorial/drag'
 	'tutorial/template' 'tutorial/datagrid' 'tutorial/datagrid_tree' 'tutorial/drag'

+ 4 - 0
Samples/assets/invader.rcss

@@ -39,6 +39,10 @@
 	button-hover:  247px 45px 159px 45px;
 	button-hover:  247px 45px 159px 45px;
 	button-active: 247px 90px 159px 45px;
 	button-active: 247px 90px 159px 45px;
 	
 	
+	button-inner: 259px 19px 135px 1px;
+	button-inner-hover:  259px 64px 135px 1px;
+	button-inner-active: 259px 109px 135px 1px;
+	
 	text-l: 162px 192px 14px 31px;
 	text-l: 162px 192px 14px 31px;
 	text-c: 176px 192px 1px 31px;
 	text-c: 176px 192px 1px 31px;
 	
 	

+ 112 - 0
Samples/basic/demo/data/demo.rml

@@ -0,0 +1,112 @@
+<rml>
+<head>
+<link type="text/template" href="../../../assets/window.rml"/>
+<title>Demo Sample</title>
+<style>
+body.window
+{
+	max-width: 2000px;
+	max-height: 2000px;
+	width: 1600px;
+	height: 750px;
+}
+div#title_bar div#icon
+{
+	display: none;
+}
+
+
+tabset
+{
+	display: block;
+	margin: 30px auto 0 auto;
+	width: 900px;
+	height: 600px;
+}
+tabs
+{
+    display: block;
+    height: 40px;
+	text-align: left;
+	padding-left: 10px;
+}
+tab
+{
+    width: 100px;
+	padding: 0px 20px;
+	line-height: 40px;
+	decorator: tiled-horizontal( datagridheader-l, datagridheader-c, datagridheader-r );
+	image-color: #cff9;
+	
+	font-size: 16px;
+	color: #ccc;
+	text-align: center;
+}
+tab:hover
+{
+	image-color: #fffc;
+}
+tab:active
+{
+	image-color: #fee;
+}
+tab:selected
+{
+	image-color: #fff;
+	color: #fff;
+}
+panels
+{
+    display: block;
+    height: 460px;
+	padding: 20px 20px;
+	border: 1px #ddd;
+    background-color: #ccc3;
+}
+panel
+{
+    display: block;
+    width: 100%;
+    height: 100%;
+}
+
+#decorators button
+{
+	width: 250px;
+	height: 150px;
+	line-height: 150px;
+}
+#decorators button.ninepatch
+{
+	decorator: ninepatch(button, button-inner);
+}
+
+</style>
+</head>
+
+<body template="window">
+
+<div style="font-size: 0.85em; text-align: left;" id="fps"></div>
+
+<tabset id="menu">
+<tab>Buttons</tab>
+<panel>
+	<button id="start_game">Start Game</button><br />
+	<button id="high_scores" onkeydown="hello">High Scores</button><br />
+	<button id="options">Options</button><br />
+	<button id="help">Help</button><br />
+	<button id="exit" onclick="exit">Exit</button>
+</panel>
+<tab>Controls</tab>
+<panel>
+	<div>Yype something here: <input style="vertical-align: -7px;" type="text" value="Sample text"/></div>
+</panel>
+<tab>Decorators</tab>
+<panel id="decorators">
+	<button>Normal enlarged</button>
+	<button class="ninepatch">Ninepatch</button>
+</panel>
+</tabset>
+
+</body>
+</rml>

+ 260 - 0
Samples/basic/demo/src/main.cpp

@@ -0,0 +1,260 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2018 Michael R. P. Ragazzon
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include <RmlUi/Core.h>
+#include <RmlUi/Controls.h>
+#include <RmlUi/Debugger.h>
+#include <Input.h>
+#include <Shell.h>
+#include <ShellRenderInterfaceOpenGL.h>
+#include <RmlUi/Core/TransformPrimitive.h>
+
+#include <sstream>
+
+bool run_loop = true;
+bool single_loop = false;
+
+
+class DemoWindow : public Rml::Core::EventListener
+{
+public:
+	DemoWindow(const Rml::Core::String &title, const Rml::Core::Vector2f &position, Rml::Core::Context *context)
+	{
+		using namespace Rml::Core;
+		document = context->LoadDocument("basic/demo/data/demo.rml");
+		if (document != nullptr)
+		{
+			{
+				document->GetElementById("title")->SetInnerRML(title);
+				document->SetProperty(PropertyId::Left, Property(position.x, Property::PX));
+				document->SetProperty(PropertyId::Top, Property(position.y, Property::PX));
+			}
+
+			document->Show();
+		}
+	}
+
+	~DemoWindow()
+	{
+		if (document)
+		{
+			document->Close();
+		}
+	}
+
+
+	void ProcessEvent(Rml::Core::Event& event) override
+	{
+		using namespace Rml::Core;
+
+		switch (event.GetId())
+		{
+		case EventId::Keydown:
+		{
+			Rml::Core::Input::KeyIdentifier key_identifier = (Rml::Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
+
+			if (key_identifier == Rml::Core::Input::KI_SPACE)
+			{
+				run_loop = !run_loop;
+			}
+			else if (key_identifier == Rml::Core::Input::KI_RETURN)
+			{
+				run_loop = false;
+				single_loop = true;
+			}
+			else if (key_identifier == Rml::Core::Input::KI_ESCAPE)
+			{
+				Shell::RequestExit();
+			}
+			else if (key_identifier == Rml::Core::Input::KI_F8)
+			{
+				Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
+			}
+		}
+		break;
+
+		default:
+			break;
+		}
+	}
+
+	Rml::Core::ElementDocument * GetDocument() {
+		return document;
+	}
+
+private:
+	Rml::Core::ElementDocument *document;
+};
+
+
+Rml::Core::Context* context = nullptr;
+ShellRenderInterfaceExtensions *shell_renderer;
+DemoWindow* window = nullptr;
+
+
+void GameLoop()
+{
+	if(run_loop || single_loop)
+	{
+		context->Update();
+
+		shell_renderer->PrepareRenderBuffer();
+		context->Render();
+		shell_renderer->PresentRenderBuffer();
+
+		single_loop = false;
+	}
+
+	static double t_prev = 0.0f;
+	double t = Shell::GetElapsedTime();
+	float dt = float(t - t_prev);
+	static int count_frames = 0;
+	count_frames += 1;
+
+	if (window && dt > 0.2f)
+	{
+		t_prev = t;
+		auto el = window->GetDocument()->GetElementById("fps");
+		float fps = float(count_frames) / dt;
+		count_frames = 0;
+		el->SetInnerRML(Rml::Core::CreateString( 20, "FPS: %f", fps ));
+	}
+}
+
+
+
+
+class Event : public Rml::Core::EventListener
+{
+public:
+	Event(const Rml::Core::String& value) : value(value) {}
+
+	void ProcessEvent(Rml::Core::Event& event) override
+	{
+		using namespace Rml::Core;
+
+		if (value == "exit")
+			Shell::RequestExit();
+	}
+
+	void OnDetach(Rml::Core::Element* element) override { delete this; }
+
+private:
+	Rml::Core::String value;
+};
+
+
+
+class EventInstancer : public Rml::Core::EventListenerInstancer
+{
+public:
+	Rml::Core::EventListener* InstanceEventListener(const Rml::Core::String& value, Rml::Core::Element* element) override
+	{
+		return new Event(value);
+	}
+};
+
+
+#if defined RMLUI_PLATFORM_WIN32
+#include <windows.h>
+int APIENTRY WinMain(HINSTANCE RMLUI_UNUSED_PARAMETER(instance_handle), HINSTANCE RMLUI_UNUSED_PARAMETER(previous_instance_handle), char* RMLUI_UNUSED_PARAMETER(command_line), int RMLUI_UNUSED_PARAMETER(command_show))
+#else
+int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
+#endif
+{
+#ifdef RMLUI_PLATFORM_WIN32
+	RMLUI_UNUSED(instance_handle);
+	RMLUI_UNUSED(previous_instance_handle);
+	RMLUI_UNUSED(command_line);
+	RMLUI_UNUSED(command_show);
+#else
+	RMLUI_UNUSED(argc);
+	RMLUI_UNUSED(argv);
+#endif
+
+	const int width = 1800;
+	const int height = 1000;
+
+
+	ShellRenderInterfaceOpenGL opengl_renderer;
+	shell_renderer = &opengl_renderer;
+
+	// Generic OS initialisation, creates a window and attaches OpenGL.
+	if (!Shell::Initialise() ||
+		!Shell::OpenWindow("Demo Sample", shell_renderer, width, height, true))
+	{
+		Shell::Shutdown();
+		return -1;
+	}
+
+	// RmlUi initialisation.
+	Rml::Core::SetRenderInterface(&opengl_renderer);
+	opengl_renderer.SetViewport(width, height);
+
+	ShellSystemInterface system_interface;
+	Rml::Core::SetSystemInterface(&system_interface);
+
+	Rml::Core::Initialise();
+
+	// Create the main RmlUi context and set it on the shell's input layer.
+	context = Rml::Core::CreateContext("main", Rml::Core::Vector2i(width, height));
+	if (context == nullptr)
+	{
+		Rml::Core::Shutdown();
+		Shell::Shutdown();
+		return -1;
+	}
+
+	Rml::Controls::Initialise();
+	Rml::Debugger::Initialise(context);
+	Input::SetContext(context);
+	shell_renderer->SetContext(context);
+
+	EventInstancer event_listener_instancer;
+	Rml::Core::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
+
+	Shell::LoadFonts("assets/");
+
+	window = new DemoWindow("Demo sample", Rml::Core::Vector2f(81, 100), context);
+	window->GetDocument()->AddEventListener(Rml::Core::EventId::Keydown, window);
+	window->GetDocument()->AddEventListener(Rml::Core::EventId::Keyup, window);
+	window->GetDocument()->AddEventListener(Rml::Core::EventId::Animationend, window);
+
+
+	Shell::EventLoop(GameLoop);
+
+	delete window;
+
+	// Shutdown RmlUi.
+	Rml::Core::Shutdown();
+
+	Shell::CloseWindow();
+	Shell::Shutdown();
+
+	return 0;
+}