aboutsummaryrefslogtreecommitdiff
path: root/src/ui/style/Condition.cpp
blob: f4866c04711ff16d0e72ea698cbab2c3382dcf01 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "cru/ui/style/Condition.hpp"
#include <memory>

#include "cru/common/ClonablePtr.hpp"
#include "cru/common/Event.hpp"
#include "cru/ui/controls/Control.hpp"
#include "cru/ui/controls/IClickableControl.hpp"
#include "cru/ui/helper/ClickDetector.hpp"

namespace cru::ui::style {
CompoundCondition::CompoundCondition(
    std::vector<ClonablePtr<Condition>> conditions)
    : conditions_(std::move(conditions)) {}

std::vector<IBaseEvent*> CompoundCondition::ChangeOn(
    controls::Control* control) const {
  std::vector<IBaseEvent*> result;

  for (auto condition : conditions_) {
    for (auto e : condition->ChangeOn(control)) {
      result.push_back(e);
    }
  }

  return result;
}

bool AndCondition::Judge(controls::Control* control) const {
  for (auto condition : conditions_) {
    if (!condition->Judge(control)) return false;
  }
  return true;
}

bool OrCondition::Judge(controls::Control* control) const {
  for (auto condition : conditions_) {
    if (condition->Judge(control)) return true;
  }
  return false;
}

FocusCondition::FocusCondition(bool has_focus) : has_focus_(has_focus) {}

std::vector<IBaseEvent*> FocusCondition::ChangeOn(
    controls::Control* control) const {
  return {control->GainFocusEvent()->Direct(),
          control->LoseFocusEvent()->Direct()};
}

bool FocusCondition::Judge(controls::Control* control) const {
  return control->HasFocus() == has_focus_;
}

std::vector<IBaseEvent*> HoverCondition::ChangeOn(
    controls::Control* control) const {
  return {control->MouseEnterEvent()->Direct(),
          control->MouseLeaveEvent()->Direct()};
}

bool HoverCondition::Judge(controls::Control* control) const {
  return control->IsMouseOver() == hover_;
}

ClickStateCondition::ClickStateCondition(helper::ClickState click_state)
    : click_state_(click_state) {}

std::vector<IBaseEvent*> ClickStateCondition::ChangeOn(
    controls::Control* control) const {
  auto clickable_control = dynamic_cast<controls::IClickableControl*>(control);
  if (clickable_control) {
    return {clickable_control->ClickStateChangeEvent()};
  } else {
    return {};
  }
}

bool ClickStateCondition::Judge(controls::Control* control) const {
  auto clickable_control = dynamic_cast<controls::IClickableControl*>(control);
  if (clickable_control) {
    return clickable_control->GetClickState() == click_state_;
  }
  return false;
}
}  // namespace cru::ui::style