| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- /*
- * This source file is part of RmlUi, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://github.com/mikke89/RmlUi
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- * 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 "precompiled.h"
- #include "../../Include/RmlUi/Core/DataBinding.h"
- #include "../../Include/RmlUi/Core/Element.h"
- namespace Rml {
- namespace Core {
- DataViewText::DataViewText(const DataModel& model, ElementText* in_parent_element, const String& in_text, const size_t index_begin_search) : element(in_parent_element->GetObserverPtr())
- {
- text.reserve(in_text.size());
- bool success = true;
- size_t previous_close_brackets = 0;
- size_t begin_brackets = index_begin_search;
- while ((begin_brackets = in_text.find("{{", begin_brackets)) != String::npos)
- {
- text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.begin() + begin_brackets);
- const size_t begin_name = begin_brackets + 2;
- const size_t end_name = in_text.find("}}", begin_name);
- if (end_name == String::npos)
- {
- success = false;
- break;
- }
- DataEntry entry;
- entry.index = text.size();
- entry.name = (String)StringUtilities::StripWhitespace(StringView(in_text.data() + begin_name, in_text.data() + end_name));
- data_entries.push_back(std::move(entry));
- previous_close_brackets = end_name + 2;
- begin_brackets = previous_close_brackets;
- }
- if (data_entries.empty())
- success = false;
- if (success && previous_close_brackets < in_text.size())
- text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.end());
- if (success)
- {
- Update(model);
- }
- else
- {
- text.clear();
- data_entries.clear();
- }
- }
- bool DataViewText::Update(const DataModel& model)
- {
- bool entries_modified = false;
- for (DataEntry& entry : data_entries)
- {
- String value;
- bool result = model.GetValue(entry.name, value);
- if (result && entry.value != value)
- {
- entry.value = value;
- entries_modified = true;
- }
- }
- if (entries_modified)
- {
- if (element)
- {
- RMLUI_ASSERTMSG(rmlui_dynamic_cast<ElementText*>(element.get()), "Somehow the element type was changed from ElementText since construction of the view. Should not be possible?");
- if(auto text_element = static_cast<ElementText*>(element.get()))
- {
- String new_text = BuildText();
- text_element->SetText(new_text);
- }
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Could not update data view text, parent element no longer valid. Was it destroyed?");
- }
- }
- return entries_modified;
- }
- String DataViewText::BuildText() const
- {
- size_t reserve_size = text.size();
- for (const DataEntry& entry : data_entries)
- reserve_size += entry.value.size();
- String result;
- result.reserve(reserve_size);
- size_t previous_index = 0;
- for (const DataEntry& entry : data_entries)
- {
- result += text.substr(previous_index, entry.index - previous_index);
- result += entry.value;
- previous_index = entry.index;
- }
- if (previous_index < text.size())
- result += text.substr(previous_index);
- return result;
- }
- DataViewAttribute::DataViewAttribute(const DataModel& model, Element* element, const String& attribute_name, const String& value_name)
- : element(element->GetObserverPtr()), attribute_name(attribute_name), value_name(value_name)
- {
- Update(model);
- }
- bool DataViewAttribute::Update(const DataModel& model)
- {
- bool result = false;
- String value;
- if (model.GetValue(value_name, value))
- {
- Variant* variant = element->GetAttribute(attribute_name);
- if (!variant || (variant && variant->Get<String>() != value))
- {
- element->SetAttribute(attribute_name, value);
- result = true;
- }
- }
- return result;
- }
- DataViewIf::DataViewIf(const DataModel& model, Element* element, const String& binding_name) : element(element->GetObserverPtr()), binding_name(binding_name)
- {
- Update(model);
- }
- bool DataViewIf::Update(const DataModel& model)
- {
- bool result = false;
- bool value = false;
- if (model.GetValue(binding_name, value))
- {
- bool is_visible = (element->GetLocalStyleProperties().count(PropertyId::Display) == 0);
- if(is_visible != value)
- {
- if (value)
- element->RemoveProperty(PropertyId::Display);
- else
- element->SetProperty(PropertyId::Display, Property(Style::Display::None));
- result = true;
- }
- }
- return result;
- }
- DataControllerAttribute::DataControllerAttribute(const DataModel& model, const String& in_attribute_name, const String& in_value_name) : attribute_name(in_attribute_name), value_name(in_value_name)
- {
- String value;
- bool result = model.GetValue(value_name, value);
- if (!model.IsWritable(value_name))
- {
- attribute_name.clear();
- value_name.clear();
- }
- }
- bool DataControllerAttribute::Update(Element* element, const DataModel& model)
- {
- bool result = false;
- if (dirty)
- {
- result = model.SetValue(value_name, element->GetAttribute<String>(attribute_name, ""));
- dirty = false;
- }
- return result;
- }
- bool DataModel::GetValue(const String& name, String& out_value) const
- {
- bool success = true;
- auto it = bindings.find(name);
- if (it != bindings.end())
- {
- const Binding& binding = it->second;
- if (binding.type == Type::STRING)
- out_value = *static_cast<const String*>(binding.ptr);
- else if (binding.type == Type::INT)
- success = TypeConverter<int, String>::Convert(*static_cast<const int*>(binding.ptr), out_value);
- else
- {
- RMLUI_ERRORMSG("TODO: Implementation for the provided binding type has not been made yet.");
- success = false;
- }
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
- success = false;
- }
- return success;
- }
- bool DataModel::GetValue(const String& name, bool& out_value) const
- {
- bool success = true;
- auto it = bindings.find(name);
- if (it != bindings.end())
- {
- const Binding& binding = it->second;
- if (binding.type == Type::STRING)
- success = TypeConverter<String, bool>::Convert(*static_cast<const String*>(binding.ptr), out_value);
- else if (binding.type == Type::INT)
- success = TypeConverter<int, bool>::Convert(*static_cast<const int*>(binding.ptr), out_value);
- else
- {
- RMLUI_ERRORMSG("TODO: Implementation for the provided binding type has not been made yet.");
- success = false;
- }
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
- success = false;
- }
- return success;
- }
- bool DataModel::SetValue(const String& name, const String& value) const
- {
- bool result = true;
- auto it = bindings.find(name);
- if (it != bindings.end())
- {
- const Binding& binding = it->second;
- if (binding.writable)
- {
- if (binding.type == Type::STRING)
- *static_cast<String*>(binding.ptr) = value;
- else if (binding.type == Type::INT)
- result = TypeConverter<String, int>::Convert(value, *static_cast<int*>(binding.ptr));
- else
- {
- RMLUI_ERRORMSG("TODO: Implementation for the provided binding type has not been made yet.");
- result = false;
- }
- }
- else
- {
- RMLUI_ERRORMSG("Controller attempted to write to a non-writable binding.");
- result = false;
- }
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
- result = false;
- }
- return false;
- }
- bool DataModel::IsWritable(const String& name) const
- {
- bool result = false;
- auto it = bindings.find(name);
- if (it != bindings.end())
- result = it->second.writable;
- return result;
- }
- }
- }
|