DataBinding.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "../../Include/RmlUi/Core/DataBinding.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. namespace Rml {
  32. namespace Core {
  33. DataViewText::DataViewText(const DataModel& model, ElementText* in_parent_element, const String& in_text, const size_t index_begin_search) : element(in_parent_element->GetObserverPtr())
  34. {
  35. text.reserve(in_text.size());
  36. bool success = true;
  37. size_t previous_close_brackets = 0;
  38. size_t begin_brackets = index_begin_search;
  39. while ((begin_brackets = in_text.find("{{", begin_brackets)) != String::npos)
  40. {
  41. text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.begin() + begin_brackets);
  42. const size_t begin_name = begin_brackets + 2;
  43. const size_t end_name = in_text.find("}}", begin_name);
  44. if (end_name == String::npos)
  45. {
  46. success = false;
  47. break;
  48. }
  49. DataEntry entry;
  50. entry.index = text.size();
  51. entry.name = (String)StringUtilities::StripWhitespace(StringView(in_text.data() + begin_name, in_text.data() + end_name));
  52. data_entries.push_back(std::move(entry));
  53. previous_close_brackets = end_name + 2;
  54. begin_brackets = previous_close_brackets;
  55. }
  56. if (data_entries.empty())
  57. success = false;
  58. if (success && previous_close_brackets < in_text.size())
  59. text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.end());
  60. if (success)
  61. {
  62. Update(model);
  63. }
  64. else
  65. {
  66. text.clear();
  67. data_entries.clear();
  68. }
  69. }
  70. bool DataViewText::Update(const DataModel& model)
  71. {
  72. bool entries_modified = false;
  73. for (DataEntry& entry : data_entries)
  74. {
  75. String value;
  76. bool result = model.GetValue(entry.name, value);
  77. if (result && entry.value != value)
  78. {
  79. entry.value = value;
  80. entries_modified = true;
  81. }
  82. }
  83. if (entries_modified)
  84. {
  85. if (element)
  86. {
  87. 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?");
  88. if(auto text_element = static_cast<ElementText*>(element.get()))
  89. {
  90. String new_text = BuildText();
  91. text_element->SetText(new_text);
  92. }
  93. }
  94. else
  95. {
  96. Log::Message(Log::LT_WARNING, "Could not update data view text, parent element no longer valid. Was it destroyed?");
  97. }
  98. }
  99. return entries_modified;
  100. }
  101. String DataViewText::BuildText() const
  102. {
  103. size_t reserve_size = text.size();
  104. for (const DataEntry& entry : data_entries)
  105. reserve_size += entry.value.size();
  106. String result;
  107. result.reserve(reserve_size);
  108. size_t previous_index = 0;
  109. for (const DataEntry& entry : data_entries)
  110. {
  111. result += text.substr(previous_index, entry.index - previous_index);
  112. result += entry.value;
  113. previous_index = entry.index;
  114. }
  115. if (previous_index < text.size())
  116. result += text.substr(previous_index);
  117. return result;
  118. }
  119. DataViewAttribute::DataViewAttribute(const DataModel& model, Element* element, const String& attribute_name, const String& value_name)
  120. : element(element->GetObserverPtr()), attribute_name(attribute_name), value_name(value_name)
  121. {
  122. Update(model);
  123. }
  124. bool DataViewAttribute::Update(const DataModel& model)
  125. {
  126. bool result = false;
  127. String value;
  128. if (model.GetValue(value_name, value))
  129. {
  130. Variant* variant = element->GetAttribute(attribute_name);
  131. if (!variant || (variant && variant->Get<String>() != value))
  132. {
  133. element->SetAttribute(attribute_name, value);
  134. result = true;
  135. }
  136. }
  137. return result;
  138. }
  139. DataViewIf::DataViewIf(const DataModel& model, Element* element, const String& binding_name) : element(element->GetObserverPtr()), binding_name(binding_name)
  140. {
  141. Update(model);
  142. }
  143. bool DataViewIf::Update(const DataModel& model)
  144. {
  145. bool result = false;
  146. bool value = false;
  147. if (model.GetValue(binding_name, value))
  148. {
  149. bool is_visible = (element->GetLocalStyleProperties().count(PropertyId::Display) == 0);
  150. if(is_visible != value)
  151. {
  152. if (value)
  153. element->RemoveProperty(PropertyId::Display);
  154. else
  155. element->SetProperty(PropertyId::Display, Property(Style::Display::None));
  156. result = true;
  157. }
  158. }
  159. return result;
  160. }
  161. 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)
  162. {
  163. String value;
  164. bool result = model.GetValue(value_name, value);
  165. if (!model.IsWritable(value_name))
  166. {
  167. attribute_name.clear();
  168. value_name.clear();
  169. }
  170. }
  171. bool DataControllerAttribute::Update(Element* element, const DataModel& model)
  172. {
  173. bool result = false;
  174. if (dirty)
  175. {
  176. result = model.SetValue(value_name, element->GetAttribute<String>(attribute_name, ""));
  177. dirty = false;
  178. }
  179. return result;
  180. }
  181. bool DataModel::GetValue(const String& name, String& out_value) const
  182. {
  183. bool success = true;
  184. auto it = bindings.find(name);
  185. if (it != bindings.end())
  186. {
  187. const Binding& binding = it->second;
  188. if (binding.type == Type::STRING)
  189. out_value = *static_cast<const String*>(binding.ptr);
  190. else if (binding.type == Type::INT)
  191. success = TypeConverter<int, String>::Convert(*static_cast<const int*>(binding.ptr), out_value);
  192. else
  193. {
  194. RMLUI_ERRORMSG("TODO: Implementation for the provided binding type has not been made yet.");
  195. success = false;
  196. }
  197. }
  198. else
  199. {
  200. Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
  201. success = false;
  202. }
  203. return success;
  204. }
  205. bool DataModel::GetValue(const String& name, bool& out_value) const
  206. {
  207. bool success = true;
  208. auto it = bindings.find(name);
  209. if (it != bindings.end())
  210. {
  211. const Binding& binding = it->second;
  212. if (binding.type == Type::STRING)
  213. success = TypeConverter<String, bool>::Convert(*static_cast<const String*>(binding.ptr), out_value);
  214. else if (binding.type == Type::INT)
  215. success = TypeConverter<int, bool>::Convert(*static_cast<const int*>(binding.ptr), out_value);
  216. else
  217. {
  218. RMLUI_ERRORMSG("TODO: Implementation for the provided binding type has not been made yet.");
  219. success = false;
  220. }
  221. }
  222. else
  223. {
  224. Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
  225. success = false;
  226. }
  227. return success;
  228. }
  229. bool DataModel::SetValue(const String& name, const String& value) const
  230. {
  231. bool result = true;
  232. auto it = bindings.find(name);
  233. if (it != bindings.end())
  234. {
  235. const Binding& binding = it->second;
  236. if (binding.writable)
  237. {
  238. if (binding.type == Type::STRING)
  239. *static_cast<String*>(binding.ptr) = value;
  240. else if (binding.type == Type::INT)
  241. result = TypeConverter<String, int>::Convert(value, *static_cast<int*>(binding.ptr));
  242. else
  243. {
  244. RMLUI_ERRORMSG("TODO: Implementation for the provided binding type has not been made yet.");
  245. result = false;
  246. }
  247. }
  248. else
  249. {
  250. RMLUI_ERRORMSG("Controller attempted to write to a non-writable binding.");
  251. result = false;
  252. }
  253. }
  254. else
  255. {
  256. Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
  257. result = false;
  258. }
  259. return false;
  260. }
  261. bool DataModel::IsWritable(const String& name) const
  262. {
  263. bool result = false;
  264. auto it = bindings.find(name);
  265. if (it != bindings.end())
  266. result = it->second.writable;
  267. return result;
  268. }
  269. }
  270. }