about summary refs log tree commit diff stats
path: root/eye_indicator.cpp
blob: 03e234e37740cd472a8233154761f810582565e4 (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
#include "eye_indicator.h"

EyeIndicator::EyeIndicator(wxWindow* parent) : wxWindow(parent, wxID_ANY) {
  SetMinSize({32, 32});

  Redraw();

  Bind(wxEVT_PAINT, &EyeIndicator::OnPaint, this);
}

void EyeIndicator::SetChecked(bool checked) {
  if (intended_checked_ != checked) {
    intended_checked_ = checked;

    Redraw();
  }
}

const wxImage& EyeIndicator::GetUncheckedImage() {
  static wxImage* unchecked_image =
      new wxImage("assets/unchecked.png", wxBITMAP_TYPE_PNG);
  return *unchecked_image;
}

const wxImage& EyeIndicator::GetCheckedImage() {
  static wxImage* checked_image =
      new wxImage("assets/checked.png", wxBITMAP_TYPE_PNG);
  return *checked_image;
}

void EyeIndicator::OnPaint(wxPaintEvent& event) {
  if (GetSize() != rendered_.GetSize() ||
      intended_checked_ != rendered_checked_) {
    Redraw();
  }

  wxPaintDC dc(this);
  dc.DrawBitmap(rendered_, 0, 0);

  event.Skip();
}

void EyeIndicator::Redraw() {
  rendered_ =
      wxBitmap((intended_checked_ ? GetCheckedImage() : GetUncheckedImage())
                   .Scale(GetSize().GetWidth(), GetSize().GetHeight(),
                          wxIMAGE_QUALITY_NORMAL));
  rendered_checked_ = intended_checked_;
}