This is based on the RmlUi implementation.
| 229 | |
| 230 | // This is based on the RmlUi implementation. |
| 231 | std::shared_ptr<ElementDefinition> StyleSheet::getElementStyles( UIWidget* element, |
| 232 | const bool& applyPseudo ) const { |
| 233 | static StyleSheetStyleVector applicableNodes; |
| 234 | applicableNodes.clear(); |
| 235 | |
| 236 | const std::string& tag = element->getElementTag(); |
| 237 | const std::string& id = element->getId(); |
| 238 | |
| 239 | std::array<size_t, 4> nodeHash; |
| 240 | int numHashes = 2; |
| 241 | |
| 242 | nodeHash[0] = 0; |
| 243 | nodeHash[1] = this->nodeHash( tag, "" ); |
| 244 | |
| 245 | if ( !id.empty() ) { |
| 246 | numHashes = 4; |
| 247 | nodeHash[2] = this->nodeHash( "", id ); |
| 248 | nodeHash[3] = this->nodeHash( tag, id ); |
| 249 | } |
| 250 | |
| 251 | for ( int i = 0; i < numHashes; i++ ) { |
| 252 | auto itNodes = mNodeIndex.find( nodeHash[i] ); |
| 253 | if ( itNodes != mNodeIndex.end() ) { |
| 254 | const StyleSheetStyleVector& nodes = itNodes->second; |
| 255 | for ( StyleSheetStyle* node : nodes ) { |
| 256 | if ( node->isMediaValid() && node->getSelector().select( element, applyPseudo ) ) { |
| 257 | applicableNodes.push_back( node ); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | std::sort( applicableNodes.begin(), applicableNodes.end(), StyleSheetNodeSort ); |
| 264 | |
| 265 | if ( applicableNodes.empty() ) |
| 266 | return nullptr; |
| 267 | |
| 268 | size_t seed = 0; |
| 269 | for ( const StyleSheetStyle* node : applicableNodes ) |
| 270 | HashCombine( seed, node ); |
| 271 | |
| 272 | auto cacheIterator = mNodeCache.find( seed ); |
| 273 | if ( cacheIterator != mNodeCache.end() ) |
| 274 | return cacheIterator->second; |
| 275 | |
| 276 | auto newDefinition = std::make_shared<ElementDefinition>( applicableNodes ); |
| 277 | mNodeCache[seed] = newDefinition; |
| 278 | |
| 279 | return newDefinition; |
| 280 | } |
| 281 | |
| 282 | const std::vector<std::shared_ptr<StyleSheetStyle>>& StyleSheet::getStyles() const { |
| 283 | return mNodes; |