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

summaryrefslogtreecommitdiff
path: root/source/windowing/StarProgressWidget.cpp
blob: df498f460ccc1fb6cb29709302b1cdf9b8cb626c (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
69
70
71
#include "StarProgressWidget.hpp"
#include "StarLexicalCast.hpp"
#include "StarInterpolation.hpp"

namespace Star {

ProgressWidget::ProgressWidget(String const& background,
    String const& overlay,
    ImageStretchSet const& progressSet,
    GuiDirection direction)
  : m_background(background),
    m_overlay(overlay),
    m_bar(progressSet),
    m_direction(direction) {

  m_progressLevel = 0;
  m_maxLevel = 1;

  if (!m_background.empty())
    setSize(Vec2I(context()->textureSize(m_background)));
  else if (!m_overlay.empty())
    setSize(Vec2I(context()->textureSize(m_overlay)));

  m_color = Color::White;
}

void ProgressWidget::renderImpl() {
  float progress = 1;
  if (m_maxLevel > 0)
    progress = m_progressLevel / m_maxLevel;

  auto shift = [&](float begin, float end, RectF templ) {
    RectF result = templ;

    if (m_direction == GuiDirection::Horizontal) {
      result.min()[0] = lerp(begin, templ.min()[0], templ.max()[0]);
      result.max()[0] = lerp(end, templ.min()[0], templ.max()[0]);
    } else {
      result.min()[1] = lerp(begin, templ.min()[1], templ.max()[1]);
      result.max()[1] = lerp(end, templ.min()[1], templ.max()[1]);
    }

    return result;
  };

  if (!m_background.empty())
    context()->drawInterfaceQuad(m_background, shift(0, 1, RectF(Vec2F(), Vec2F(size()))), shift(0, 1, RectF(screenBoundRect())));

  context()->drawImageStretchSet(m_bar, shift(0, progress, RectF(screenBoundRect())), m_direction, m_color.toRgba());

  if (!m_overlay.empty())
    context()->drawInterfaceQuad(m_overlay, shift(0, 1, RectF({}, Vec2F(size()))), shift(0, 1, RectF(screenBoundRect())));
}

void ProgressWidget::setCurrentProgressLevel(float amount) {
  m_progressLevel = amount;
}

void ProgressWidget::setMaxProgressLevel(float amount) {
  m_maxLevel = amount;
}

void ProgressWidget::setColor(Color const& color) {
  m_color = color;
}

void ProgressWidget::setOverlay(String const& overlay) {
  m_overlay = overlay;
}

}