Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/frontend/StarInterfaceCursor.cpp
blob: 9fa01f7046864ecf6907153a282f13e07d7b5018 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "StarInterfaceCursor.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
#include "StarImageMetadataDatabase.hpp"

namespace Star {

InterfaceCursor::InterfaceCursor() {
  resetCursor();
}

void InterfaceCursor::resetCursor() {
  auto& root = Root::singleton();
  auto assets = root.assets();
  setCursor(assets->json("/interface.config:defaultCursor").toString());
}

void InterfaceCursor::setCursor(String const& configFile) {
  if (m_configFile == configFile)
    return;

  m_configFile = configFile;

  auto& root = Root::singleton();
  auto assets = root.assets();
  auto imageMetadata = root.imageMetadataDatabase();

  auto config = assets->json(m_configFile);

  m_offset = jsonToVec2I(config.get("offset"));
  if (config.contains("image")) {
    m_drawable = config.getString("image");
    m_size = Vec2I{imageMetadata->imageSize(config.getString("image"))};
  } else {
    m_drawable = Animation(config.get("animation"), "/interface");
    m_size = Vec2I(m_drawable.get<Animation>().drawable(1.0f).boundBox(false).size());
  }

  m_scale = config.getUInt("scale", 0);
}

Drawable InterfaceCursor::drawable() const {
  if (m_drawable.is<String>())
    return Drawable::makeImage(m_drawable.get<String>(), 1.0f, false, {});
  else
    return m_drawable.get<Animation>().drawable(1.0f);
}

Vec2I InterfaceCursor::size() const {
  return m_size;
}

Vec2I InterfaceCursor::offset() const {
  return m_offset;
}

unsigned int InterfaceCursor::scale(unsigned int interfaceScale) const {
  return m_scale ? m_scale : interfaceScale;
}

void InterfaceCursor::update(float dt) {
  if (m_drawable.is<Animation>()) {
    m_drawable.get<Animation>().update(dt);
  }
}

}