Browse Source

Removed the old mouse cursor code, and replaced it with a system interface method. Drawing a cursor manually will always lag behind the system cursor, thus it is better to let the user set the system cursor.

Michael Ragazzon 6 years ago
parent
commit
957f7239d6

+ 7 - 26
Include/Rocket/Core/Context.h

@@ -120,26 +120,10 @@ public:
 	/// Unloads all loaded documents.
 	void UnloadAllDocuments();
 
-	/// Adds a previously-loaded cursor document as a mouse cursor within this context. This allows you to share
-	/// cursors between contexts.
-	/// @param[in] cursor_document The document to add as a cursor into this context.
-	void AddMouseCursor(ElementDocument* cursor_document);
-	/// Loads a document as a mouse cursor within this context.
-	/// @param[in] cursor_document_path The path to the document to load as a cursor.
-	/// @return The loaded cursor document, or NULL if no document was loaded. The document is returned with a reference owned by the caller.
-	ElementDocument* LoadMouseCursor(const String& cursor_document_path);
-	/// Unload the given cursor.
-	/// @param[in] cursor_name The name of cursor to unload.
-	void UnloadMouseCursor(const String& cursor_name);
-	/// Unloads all currently loaded cursors.
-	void UnloadAllMouseCursors();
-	/// Sets a cursor as the active cursor.
-	/// @param[in] cursor_name The name of the cursor to activate.
-	/// @return True if a cursor exists with the given name, false if not.
-	bool SetMouseCursor(const String& cursor_name);
-	/// Shows or hides the cursor.
-	/// @param[in] show True to show the cursor, false to hide it.
-	void ShowMouseCursor(bool show);
+	/// Enable or disable handling mouse cursor from this context.
+	/// Only a single context should handle the mouse cursor at the same time.
+	/// @param[in] show True to enable mouse cursor handling, false to disable.
+	void EnableMouseCursor(bool enable);
 
 	/// Returns the first document in the context with the given id.
 	/// @param[in] id The id of the desired document.
@@ -281,12 +265,9 @@ private:
 	// The time the last click occured.
 	double last_click_time;
 
-	typedef std::map< String, ElementDocument* > CursorMap;
-	CursorMap cursors;
-	ElementReference default_cursor;
-	ElementReference active_cursor;
-	bool show_cursor;
-
+	// Enables cursor handling.
+	bool enable_cursor;
+	// Document attached to cursor (e.g. while dragging).
 	ElementDocument* cursor_proxy;
 
 	// The element that is currently being dragged (or about to be dragged).

+ 4 - 0
Include/Rocket/Core/SystemInterface.h

@@ -79,6 +79,10 @@ public:
 	/// @return True to continue execution, false to break into the debugger.
 	virtual bool LogMessage(Log::Type type, const String& message);
 
+	/// Set mouse cursor.
+	/// @param[in] cursor_name Cursor name to activate.
+	virtual void SetMouseCursor(const String& cursor_name);
+
 	/// Activate keyboard (for touchscreen devices)
 	virtual void ActivateKeyboard();
 	

+ 13 - 112
Source/Core/Context.cpp

@@ -65,7 +65,7 @@ Context::Context(const String& name) : name(name), dimensions(0, 0), mouse_posit
 	document_focus_history.push_back(root);
 	focus = root;
 
-	show_cursor = true;
+	enable_cursor = false;
 
 	drag_started = false;
 	drag_verbose = false;
@@ -80,7 +80,6 @@ Context::~Context()
 	PluginRegistry::NotifyContextDestroy(this);
 
 	UnloadAllDocuments();
-	UnloadAllMouseCursors();
 
 	ReleaseUnloadedDocuments();
 
@@ -218,17 +217,6 @@ bool Context::Render()
 		cursor_proxy->Render();
 	}
 
-	// Render the cursor document if we have one and we're showing the cursor.
-	if (active_cursor &&
-		show_cursor)
-	{
-		active_cursor->Update();
-		active_cursor->SetOffset(Vector2f((float)Math::Clamp(mouse_position.x, 0, dimensions.x),
-			(float)Math::Clamp(mouse_position.y, 0, dimensions.y)),
-			NULL);
-		active_cursor->Render();
-	}
-
 	render_interface->context = NULL;
 
 	return true;
@@ -378,101 +366,10 @@ void Context::UnloadAllDocuments()
 	root->ReleaseElements(root->deleted_children);
 }
 
-// Adds a previously-loaded cursor document as a mouse cursor within this context.
-void Context::AddMouseCursor(ElementDocument* cursor_document)
+// Enables or disables the mouse cursor.
+void Context::EnableMouseCursor(bool enable)
 {
-	cursor_document->AddReference();
-
-	CursorMap::iterator i = cursors.find(cursor_document->GetTitle());
-	if (i != cursors.end())
-	{
-		if (active_cursor == (*i).second)
-			active_cursor = cursor_document;
-
-		if (default_cursor == (*i).second)
-			default_cursor = cursor_document;
-
-		(*i).second->RemoveReference();
-	}
-	cursors[cursor_document->GetTitle()] = cursor_document;
-
-	if (!default_cursor)
-	{
-		default_cursor = cursor_document;
-		active_cursor = cursor_document;
-	}
-}
-
-// Loads a document as a mouse cursor.
-ElementDocument* Context::LoadMouseCursor(const String& document_path)
-{
-	StreamFile* stream = new StreamFile();
-	if (!stream->Open(document_path))
-		return NULL;
-
-	// Load the document from the stream.
-	ElementDocument* document = Factory::InstanceDocumentStream(this, stream);
-	if (document == NULL)
-	{
-		stream->RemoveReference();
-		return NULL;
-	}
-
-	AddMouseCursor(document);
-
-	// Bind the events, run the layout and fire the 'onload' event.
-	ElementUtilities::BindEventAttributes(document);
-	document->UpdateLayout();
-	document->DispatchEvent(LOAD, Dictionary(), false);
-
-	stream->RemoveReference();
-
-	return document;
-}
-
-// Unload the given cursor.
-void Context::UnloadMouseCursor(const String& cursor_name)
-{
-	CursorMap::iterator i = cursors.find(cursor_name);
-	if (i != cursors.end())
-	{
-		if (default_cursor == (*i).second)
-			default_cursor = NULL;
-
-		if (active_cursor == (*i).second)
-			active_cursor = default_cursor;
-
-		(*i).second->RemoveReference();
-		cursors.erase(i);
-	}
-}
-
-// Unloads all currently loaded cursors.
-void Context::UnloadAllMouseCursors()
-{
-	while (!cursors.empty())
-		UnloadMouseCursor((*cursors.begin()).first.CString());
-}
-
-// Sets a cursor as the active cursor.
-bool Context::SetMouseCursor(const String& cursor_name)
-{
-	CursorMap::iterator i = cursors.find(cursor_name);
-	if (i == cursors.end())
-	{
-		active_cursor = default_cursor;
-		Log::Message(Log::LT_WARNING, "Failed to find cursor '%s' in context '%s', reverting to default cursor.", cursor_name.CString(), name.CString());
-		return false;
-	}
-
-	active_cursor = (*i).second;
-	return true;
-}
-
-// Shows or hides the cursor.
-void Context::ShowMouseCursor(bool show)
-{
-	show_cursor = show;
+	enable_cursor = enable;
 }
 
 // Returns the first document found in the root with the given id.
@@ -1037,11 +934,15 @@ void Context::UpdateHoverChain(const Dictionary& parameters, const Dictionary& d
 
 	hover = GetElementAtPoint(position);
 
-	if (!hover ||
-		hover->GetProperty(CURSOR)->unit == Property::KEYWORD)
-		active_cursor = default_cursor;
-	else
-		SetMouseCursor(hover->GetProperty< String >(CURSOR));
+	if(enable_cursor)
+	{
+		String new_mouse_cursor;
+
+		if (hover && hover->GetProperty(CURSOR)->unit != Property::KEYWORD)
+			new_mouse_cursor = hover->GetProperty< String >(CURSOR);
+
+		GetSystemInterface()->SetMouseCursor(new_mouse_cursor);
+	}
 
 	// Build the new hover chain.
 	ElementSet new_hover_chain;

+ 4 - 0
Source/Core/SystemInterface.cpp

@@ -72,6 +72,10 @@ bool SystemInterface::LogMessage(Log::Type ROCKET_UNUSED_PARAMETER(logtype), con
 }
 #endif	
 
+void SystemInterface::SetMouseCursor(const String& cursor_name)
+{
+}
+
 int SystemInterface::TranslateString(String& translated, const String& input)
 {
 	translated = input;

+ 7 - 1
Source/Debugger/SystemInterface.cpp

@@ -65,7 +65,13 @@ bool SystemInterface::LogMessage(Core::Log::Type type, const Core::String& messa
 
 	return application_interface->LogMessage(type, message);
 }
-	
+
+// Set mouse cursor.
+void SystemInterface::SetMouseCursor(const Core::String& cursor_name)
+{
+	application_interface->SetMouseCursor(cursor_name);
+}
+
 // Activate keyboard (for touchscreen devices)
 void SystemInterface::ActivateKeyboard()
 {

+ 4 - 0
Source/Debugger/SystemInterface.h

@@ -66,6 +66,10 @@ public:
 	/// @return True to continue execution, false to break into the debugger.
 	virtual bool LogMessage(Core::Log::Type type, const Core::String& message);
 
+	/// Set mouse cursor.
+	/// @param[in] cursor_name Cursor name to activate.
+	virtual void SetMouseCursor(const Core::String& cursor_name);
+
 	/// Activate keyboard (for touchscreen devices)
 	virtual void ActivateKeyboard();