|
| 1 | +#include <vector> |
| 2 | +#include <map> |
| 3 | +#include <string> |
| 4 | +#include <rapidjson/document.h> |
| 5 | + |
| 6 | +using WDocument = rapidjson::GenericDocument<rapidjson::UTF16<>>; |
| 7 | +using WValue = rapidjson::GenericValue<rapidjson::UTF16<>>; |
| 8 | + |
| 9 | +class DiffLocation |
| 10 | +{ |
| 11 | +public: |
| 12 | + struct Rect |
| 13 | + { |
| 14 | + float left; |
| 15 | + float top; |
| 16 | + float width; |
| 17 | + float height; |
| 18 | + }; |
| 19 | + struct ContainerRect : public Rect |
| 20 | + { |
| 21 | + int id; |
| 22 | + int containerId; |
| 23 | + float scrollLeft; |
| 24 | + float scrollTop; |
| 25 | + float scrollWidth; |
| 26 | + float scrollHeight; |
| 27 | + float clientWidth; |
| 28 | + float clientHeight; |
| 29 | + }; |
| 30 | + struct DiffRect : public Rect |
| 31 | + { |
| 32 | + int id; |
| 33 | + int containerId; |
| 34 | + }; |
| 35 | + |
| 36 | + void read(const WDocument& doc) |
| 37 | + { |
| 38 | + std::wstring window = doc[L"window"].GetString(); |
| 39 | + std::vector<ContainerRect> containerRects; |
| 40 | + std::vector<DiffRect> diffRects; |
| 41 | + for (const auto& value : doc[L"diffRects"].GetArray()) |
| 42 | + { |
| 43 | + DiffRect rect; |
| 44 | + rect.id = value[L"id"].GetInt(); |
| 45 | + rect.containerId = value[L"containerId"].GetInt(); |
| 46 | + rect.left = value[L"left"].GetFloat(); |
| 47 | + rect.top = value[L"top"].GetFloat(); |
| 48 | + rect.width = value[L"width"].GetFloat(); |
| 49 | + rect.height = value[L"height"].GetFloat(); |
| 50 | + diffRects.push_back(rect); |
| 51 | + } |
| 52 | + for (const auto& value : doc[L"containerRects"].GetArray()) |
| 53 | + { |
| 54 | + ContainerRect rect; |
| 55 | + rect.id = value[L"id"].GetInt(); |
| 56 | + rect.containerId = value[L"containerId"].GetInt(); |
| 57 | + rect.left = value[L"left"].GetFloat(); |
| 58 | + rect.top = value[L"top"].GetFloat(); |
| 59 | + rect.width = value[L"width"].GetFloat(); |
| 60 | + rect.height = value[L"height"].GetFloat(); |
| 61 | + rect.scrollLeft = value[L"scrollLeft"].GetFloat(); |
| 62 | + rect.scrollTop = value[L"scrollTop"].GetFloat(); |
| 63 | + rect.scrollWidth = value[L"scrollWidth"].GetFloat(); |
| 64 | + rect.scrollHeight = value[L"scrollHeight"].GetFloat(); |
| 65 | + rect.clientWidth = value[L"clientWidth"].GetFloat(); |
| 66 | + rect.clientHeight = value[L"clientHeight"].GetFloat(); |
| 67 | + containerRects.push_back(rect); |
| 68 | + } |
| 69 | + for (auto it = doc[L"frameRects"].MemberBegin(); it != doc[L"frameRects"].MemberEnd(); ++it) |
| 70 | + { |
| 71 | + Rect rect; |
| 72 | + rect.left = it->value[L"left"].GetFloat(); |
| 73 | + rect.top = it->value[L"top"].GetFloat(); |
| 74 | + rect.width = it->value[L"width"].GetFloat(); |
| 75 | + rect.height = it->value[L"height"].GetFloat(); |
| 76 | + m_frameRects.insert_or_assign(it->name.GetString(), rect); |
| 77 | + } |
| 78 | + m_diffRects.insert_or_assign(window, diffRects); |
| 79 | + m_containerRects.insert_or_assign(window, containerRects); |
| 80 | + if (window == L"") |
| 81 | + { |
| 82 | + m_scrollX = doc[L"scrollX"].GetFloat(); |
| 83 | + m_scrollY = doc[L"scrollY"].GetFloat(); |
| 84 | + m_clientWidth = doc[L"clientWidth"].GetFloat(); |
| 85 | + m_clientHeight = doc[L"clientHeight"].GetFloat(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + void clear() |
| 90 | + { |
| 91 | + m_diffRects.clear(); |
| 92 | + m_containerRects.clear(); |
| 93 | + m_scrollX = 0.0f; |
| 94 | + m_scrollY = 0.0f; |
| 95 | + m_clientWidth = 0.0f; |
| 96 | + m_clientHeight = 0.0f; |
| 97 | + } |
| 98 | + |
| 99 | + void calcGlobalPosition(Rect& rect, std::wstring window) |
| 100 | + { |
| 101 | + while (!window.empty()) |
| 102 | + { |
| 103 | + rect.left += m_frameRects[window].left; |
| 104 | + rect.top += m_frameRects[window].top; |
| 105 | + size_t lastOpeningBracketPos = window.find_last_of('['); |
| 106 | + if (lastOpeningBracketPos != std::string::npos) { |
| 107 | + window = window.substr(0, lastOpeningBracketPos); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + std::vector<DiffRect> getDiffRectArray() |
| 113 | + { |
| 114 | + std::vector<DiffRect> diffRectsSerialized; |
| 115 | + for (const auto& pair: m_diffRects) |
| 116 | + { |
| 117 | + const auto& window = pair.first; |
| 118 | + for (const auto& diffRect : pair.second) |
| 119 | + { |
| 120 | + DiffRect rect; |
| 121 | + rect.id = diffRect.id; |
| 122 | + rect.containerId = diffRect.containerId; |
| 123 | + rect.left = diffRect.left; |
| 124 | + rect.top = diffRect.top; |
| 125 | + rect.width = diffRect.width; |
| 126 | + rect.height = diffRect.height; |
| 127 | + for (int containerId = diffRect.containerId; containerId != -1; ) |
| 128 | + { |
| 129 | + const ContainerRect& containerRect = m_containerRects[window][containerId]; |
| 130 | + if (containerRect.id == 0 && (containerRect.width == 0 || containerRect.height == 0)) |
| 131 | + break; |
| 132 | + clip(rect, containerRect); |
| 133 | + containerId = containerRect.containerId; |
| 134 | + } |
| 135 | + rect.left += m_scrollX; |
| 136 | + rect.top += m_scrollY; |
| 137 | + if (!window.empty()) |
| 138 | + { |
| 139 | + calcGlobalPosition(rect, window); |
| 140 | + Rect rcFrame = m_frameRects[window]; |
| 141 | + rcFrame.left += m_scrollX; |
| 142 | + rcFrame.top += m_scrollY; |
| 143 | + clip(rect, rcFrame); |
| 144 | + } |
| 145 | + diffRectsSerialized.push_back(rect); |
| 146 | + } |
| 147 | + } |
| 148 | + return diffRectsSerialized; |
| 149 | + } |
| 150 | + |
| 151 | + std::vector<ContainerRect> getContainerRectArray() |
| 152 | + { |
| 153 | + std::vector<ContainerRect> containerRectsSerialized; |
| 154 | + for (const auto& pair: m_containerRects) |
| 155 | + { |
| 156 | + const auto& window = pair.first; |
| 157 | + for (const auto& containerRect : pair.second) |
| 158 | + { |
| 159 | + ContainerRect rect; |
| 160 | + rect.id = containerRect.id; |
| 161 | + rect.containerId = containerRect.containerId; |
| 162 | + rect.left = containerRect.left + m_scrollX; |
| 163 | + rect.top = containerRect.top + m_scrollY; |
| 164 | + rect.width = containerRect.width; |
| 165 | + rect.height = containerRect.height; |
| 166 | + rect.scrollLeft = containerRect.scrollLeft; |
| 167 | + rect.scrollTop = containerRect.scrollTop; |
| 168 | + rect.scrollWidth = containerRect.scrollWidth; |
| 169 | + rect.scrollHeight = containerRect.scrollHeight; |
| 170 | + rect.clientWidth = containerRect.clientWidth; |
| 171 | + rect.clientHeight = containerRect.clientHeight; |
| 172 | + if (!window.empty()) |
| 173 | + { |
| 174 | + calcGlobalPosition(rect, window); |
| 175 | + Rect rcFrame = m_frameRects[window]; |
| 176 | + rcFrame.left += m_scrollX; |
| 177 | + rcFrame.top += m_scrollY; |
| 178 | + clip(rect, rcFrame); |
| 179 | + } |
| 180 | + containerRectsSerialized.push_back(rect); |
| 181 | + } |
| 182 | + } |
| 183 | + return containerRectsSerialized; |
| 184 | + } |
| 185 | + |
| 186 | + Rect getVisibleAreaRect() |
| 187 | + { |
| 188 | + return { m_scrollX, m_scrollY, m_clientWidth, m_clientHeight }; |
| 189 | + } |
| 190 | + |
| 191 | +private: |
| 192 | + bool clip(Rect& rect, const Rect& containerRect) |
| 193 | + { |
| 194 | + if (rect.left + rect.width < containerRect.left || |
| 195 | + rect.top + rect.height < containerRect.top || |
| 196 | + rect.left > containerRect.left + containerRect.width || |
| 197 | + rect.top > containerRect.top + containerRect.height) |
| 198 | + { |
| 199 | + rect.left = rect.top = -99999.9f; |
| 200 | + rect.width = rect.height = 0.0f; |
| 201 | + return true; |
| 202 | + } |
| 203 | + if (rect.left < containerRect.left) { |
| 204 | + rect.width -= containerRect.left - rect.left; |
| 205 | + rect.left = containerRect.left; |
| 206 | + } |
| 207 | + if (rect.top < containerRect.top) { |
| 208 | + rect.height -= containerRect.top - rect.top; |
| 209 | + rect.top = containerRect.top; |
| 210 | + } |
| 211 | + if (rect.left + rect.width > containerRect.left + containerRect.width) { |
| 212 | + rect.width = containerRect.left + containerRect.width - rect.left; |
| 213 | + } |
| 214 | + if (rect.top + rect.height > containerRect.top + containerRect.height) { |
| 215 | + rect.height = containerRect.top + containerRect.height - rect.top; |
| 216 | + } |
| 217 | + return false; |
| 218 | + } |
| 219 | + |
| 220 | + std::map<std::wstring, std::vector<DiffRect>> m_diffRects; |
| 221 | + std::map<std::wstring, std::vector<ContainerRect>> m_containerRects; |
| 222 | + std::map<std::wstring, Rect> m_frameRects; |
| 223 | + float m_scrollX = 0.0f; |
| 224 | + float m_scrollY = 0.0f; |
| 225 | + float m_clientWidth = 0.0f; |
| 226 | + float m_clientHeight = 0.0f; |
| 227 | +}; |
0 commit comments