DataBinding.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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(Element* in_parent_element, const String& in_text, const size_t index_begin_search) : parent_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. is_dirty = true;
  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 = is_dirty;
  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 (parent_element)
  86. {
  87. String rml = CreateText();
  88. parent_element->SetInnerRML(rml);
  89. }
  90. else
  91. {
  92. Log::Message(Log::LT_WARNING, "Could not update data view text, parent element no longer valid. Was it destroyed?");
  93. }
  94. }
  95. is_dirty = false;
  96. return entries_modified;
  97. }
  98. String DataViewText::CreateText() const
  99. {
  100. size_t reserve_size = text.size();
  101. for (const DataEntry& entry : data_entries)
  102. reserve_size += entry.value.size();
  103. String result;
  104. result.reserve(reserve_size);
  105. size_t previous_index = 0;
  106. for (const DataEntry& entry : data_entries)
  107. {
  108. result += text.substr(previous_index, entry.index - previous_index);
  109. result += entry.value;
  110. previous_index = entry.index;
  111. }
  112. if (previous_index < text.size())
  113. result += text.substr(previous_index);
  114. return result;
  115. }
  116. bool DataModel::GetValue(const String& name, String& out_value) const
  117. {
  118. auto it = bindings.find(name);
  119. if (it != bindings.end())
  120. {
  121. const Binding& binding = it->second;
  122. bool result = true;
  123. if (binding.type == Type::STRING)
  124. out_value = *static_cast<const String*>(binding.ptr);
  125. else if (binding.type == Type::INT)
  126. TypeConverter<int, String>::Convert(*static_cast<const int*>(binding.ptr), out_value);
  127. else
  128. {
  129. RMLUI_ERRORMSG("TODO: Implementation for the provided binding type has not been made yet.");
  130. result = false;
  131. }
  132. return result;
  133. }
  134. else
  135. {
  136. Log::Message(Log::LT_WARNING, "Could not find value named '%s' in data model.", name.c_str());
  137. }
  138. return false;
  139. }
  140. }
  141. }