Ver Fonte

Databindings: Remove DataModelHandle::Update, instead, data models are automatically updated on Context::Update.

Michael Ragazzon há 5 anos atrás
pai
commit
4f4324b7ca

+ 1 - 5
Include/RmlUi/Core/DataModel.h

@@ -75,7 +75,7 @@ public:
 
 	void OnElementRemove(Element* element);
 
-	bool Update();
+	bool Update(bool clear_dirty_variables);
 
 private:
 	UniquePtr<DataViews> views;
@@ -102,10 +102,6 @@ public:
 	DataModelHandle(DataModel* model = nullptr) : model(model)
 	{}
 
-	void Update() {
-		model->Update();
-	}
-
 	bool IsVariableDirty(const String& variable_name) {
 		return model->IsVariableDirty(variable_name);
 	}

+ 0 - 16
Samples/basic/databinding/src/main.cpp

@@ -58,11 +58,6 @@ namespace BasicExample {
 
 		return true;
 	}
-
-	void Update()
-	{
-		model_handle.Update();
-	}
 }
 
 
@@ -154,8 +149,6 @@ namespace EventsExample {
 				model_handle.DirtyVariable("list");
 			}
 		}
-
-		model_handle.Update();
 	}
 }
 
@@ -276,8 +269,6 @@ namespace InvadersExample {
 			}
 			invaders_data.time_last_weapons_launched = t;
 		}
-
-		model_handle.Update();
 	}
 }
 
@@ -301,11 +292,6 @@ namespace FormsExample {
 
 		return true;
 	}
-
-	void Update()
-	{
-		model_handle.Update();
-	}
 }
 
 
@@ -377,10 +363,8 @@ void GameLoop()
 {
 	const double t = Rml::GetSystemInterface()->GetElapsedTime();
 	
-	BasicExample::Update();
 	EventsExample::Update();
 	InvadersExample::Update(t);
-	FormsExample::Update();
 
 	context->Update();
 

+ 0 - 6
Samples/invaders/src/HighScores.cpp

@@ -81,12 +81,6 @@ void HighScores::Shutdown()
 	delete instance;
 }
 
-void HighScores::Update()
-{
-	if (instance->model_handle)
-		instance->model_handle.Update();
-}
-
 int HighScores::GetHighScore()
 {
 	if (instance->scores.empty())

+ 0 - 2
Samples/invaders/src/HighScores.h

@@ -38,8 +38,6 @@ public:
 	static void Initialise(Rml::Context* context);
 	static void Shutdown();
 
-	static void Update();
-
 	static int GetHighScore();
 
 	/// Two functions to add a score to the chart.

+ 0 - 2
Samples/invaders/src/main.cpp

@@ -47,8 +47,6 @@ ShellRenderInterfaceExtensions *shell_renderer;
 
 void GameLoop()
 {
-	HighScores::Update();
-
 	context->Update();
 
 	shell_renderer->PrepareRenderBuffer();

+ 10 - 0
Source/Core/Context.cpp

@@ -167,6 +167,10 @@ bool Context::Update()
 {
 	RMLUI_ZoneScoped;
 
+	// Update all data models first
+	for (auto& data_model : data_models)
+		data_model.second->Update(true);
+
 	root->Update(density_independent_pixel_ratio);
 
 	for (int i = 0; i < root->GetNumChildren(); ++i)
@@ -273,6 +277,12 @@ ElementDocument* Context::LoadDocument(Stream* stream)
 	PluginRegistry::NotifyDocumentLoad(document);
 	document->DispatchEvent(EventId::Load, Dictionary());
 
+	// Data models are updated after the 'load' event so that the user has a chance to change
+	// any data variables first. We do not clear dirty variables here, since users may need to
+	// retrieve whether or not eg. a data variable has changed in a controller.
+	for (auto& data_model : data_models)
+		data_model.second->Update(false);
+
 	document->UpdateDocument();
 
 	return document;

+ 6 - 3
Source/Core/DataModel.cpp

@@ -365,10 +365,13 @@ void DataModel::OnElementRemove(Element* element)
 	attached_elements.erase(element);
 }
 
-bool DataModel::Update() 
+bool DataModel::Update(bool clear_dirty_variables)
 {
-	bool result = views->Update(*this, dirty_variables);
-	dirty_variables.clear();
+	const bool result = views->Update(*this, dirty_variables);
+
+	if (clear_dirty_variables)
+		dirty_variables.clear();
+	
 	return result;
 }