summaryrefslogtreecommitdiff
path: root/libraries/pake/source/widgets/fixed-text.cpp
blob: 69c4046986ce2394d148169b2631caa9338e1793 (plain) (blame)
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
72
73
#include <pake/widgets/fixed-text.hpp>

static void draw_if_true(
  daguerre::hilbert_color &out, const bool &in,
  const daguerre::hilbert_color &param) {
  if (in) out = param;
}

namespace pake::widgets {

  fixed_text::fixed_text(
    std::string &&text,
    const daguerre::fixed_font<bool> *font,
    daguerre::hilbert_color bg,
    daguerre::hilbert_color fg,
    halign ha, valign va)
  : font(font), bg(bg), fg(fg),
    text(std::move(text)),
    ha(ha), va(va) {}

  void fixed_text::set_text(std::string &&text) {
    this->text = std::move(text);
    text_changed = true;
  }

  void fixed_text::render(
    dirtiable_image &onto, int x_off, int y_off, bool force) {

    if (force || text_changed) {

      //TODO: check overflow

      onto.dirty.fill(true, x_off, y_off, width, height);

      onto.image.fill(  bg, x_off, y_off, width, height);

      switch (ha) {
      case halign::left:
        break;
      case halign::center:
        x_off = width / 2 - text.size() * font->glyph_width / 2;
        break;
      case halign::right:
        x_off = width - text.size() * font->glyph_width;
        break;
      }

      switch (va) {
      case valign::top:
        break;
      case valign::center:
        y_off = height / 2 - font->glyph_height / 2;
        break;
      case valign::bottom:
        y_off = height - font->glyph_width;
        break;
      }

      onto.image.render_text(
        *font, fg, x_off, y_off,
        text.data(), draw_if_true);

      text_changed = false;

    }

  }

  void fixed_text::notify_size(int width, int height) {
    this->width = width; this->height = height;
  }

}