aboutsummaryrefslogtreecommitdiff
path: root/src/ui/mapper/BrushMapper.cpp
blob: d6fb8e4c8c3f02d6fef363b2335d5858147a5fef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "cru/ui/mapper/BrushMapper.h"
#include "cru/base/xml/XmlNode.h"
#include "cru/platform/graphics/Brush.h"
#include "cru/platform/graphics/Factory.h"
#include "cru/platform/gui/UiApplication.h"
#include "cru/ui/mapper/Mapper.h"
#include "cru/ui/mapper/MapperRegistry.h"

#include <memory>

namespace cru::ui::mapper {
bool BrushMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) {
  auto color_mapper = MapperRegistry::GetInstance()->GetMapper<Color>();
  return color_mapper->XmlElementIsOfThisType(node) || node->HasTag("Brush");
}

std::shared_ptr<platform::graphics::IBrush> BrushMapper::DoMapFromXml(
    xml::XmlElementNode* node) {
  auto graphics_factory =
      platform::gui::IUiApplication::GetInstance()->GetGraphicsFactory();
  auto color_mapper = MapperRegistry::GetInstance()->GetMapper<Color>();

  try {
    auto color = color_mapper->MapFromXml(node);
    return graphics_factory->CreateSolidColorBrush(color);
  } catch (const MapException&) {
  }

  for (auto child : node->GetChildren()) {
    if (auto c = child->AsElement()) {
      if (color_mapper->XmlElementIsOfThisType(node)) {
        auto color = color_mapper->MapFromXml(node);
        return graphics_factory->CreateSolidColorBrush(color);
      } else {
        throw MapException("Invalid child element of Brush.");
      }
    } else if (child->IsTextNode()) {
      throw MapException("Text node is not allowed in Brush.");
    }
  }

  throw MapException("Brush doesn't have content.");
}
}  // namespace cru::ui::mapper