Blame view

emulateur/epsilon-nofrendo/apps/exam_pop_up_controller.cpp 5.27 KB
6663b6c9   adorian   projet complet av...
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  #include "exam_pop_up_controller.h"
  #include "apps_container.h"
  #include "i18n.h"
  #include "global_preferences.h"
  #include <assert.h>
  
  ExamPopUpController::ExamPopUpController(ExamPopUpControllerDelegate * delegate) :
    ViewController(nullptr),
    m_contentView(this),
    m_isActivatingExamMode(false),
    m_delegate(delegate)
  {
  }
  
  void ExamPopUpController::setActivatingExamMode(bool activatingExamMode) {
    if (m_isActivatingExamMode != activatingExamMode) {
      m_isActivatingExamMode = activatingExamMode;
      m_contentView.setMessages(activatingExamMode);
    }
  }
  
  View * ExamPopUpController::view() {
    return &m_contentView;
  }
  
  void ExamPopUpController::viewDidDisappear() {
    if (m_isActivatingExamMode == false) {
      m_delegate->examDeactivatingPopUpIsDismissed();
    }
  }
  
  void ExamPopUpController::didBecomeFirstResponder() {
    m_contentView.setSelectedButton(0, app());
  }
  
  bool ExamPopUpController::handleEvent(Ion::Events::Event event) {
    if (event == Ion::Events::Left && m_contentView.selectedButton() == 1) {
      m_contentView.setSelectedButton(0, app());
      return true;
    }
    if (event == Ion::Events::Right && m_contentView.selectedButton() == 0) {
      m_contentView.setSelectedButton(1, app());
      return true;
    }
    return false;
  }
  
  ExamPopUpController::ContentView::ContentView(Responder * parentResponder) :
    m_cancelButton(parentResponder, I18n::Message::Cancel, Invocation([](void * context, void * sender) {
      ExamPopUpController * controller = (ExamPopUpController *)context;
      Container * container = (Container *)controller->app()->container();
      container->activeApp()->dismissModalViewController();
    }, parentResponder), KDText::FontSize::Small),
    m_okButton(parentResponder, I18n::Message::Ok, Invocation([](void * context, void * sender) {
      ExamPopUpController * controller = (ExamPopUpController *)context;
      GlobalPreferences::ExamMode nextExamMode = controller->isActivatingExamMode() ? GlobalPreferences::ExamMode::Activate : GlobalPreferences::ExamMode::Desactivate;
      GlobalPreferences::sharedGlobalPreferences()->setExamMode(nextExamMode);
      AppsContainer * container = (AppsContainer *)controller->app()->container();
      if (controller->isActivatingExamMode()) {
        container->reset();
      } else {
        Ion::LED::setColor(KDColorBlack);
      }
      container->refreshPreferences();
      container->activeApp()->dismissModalViewController();
    }, parentResponder), KDText::FontSize::Small),
    m_warningTextView(KDText::FontSize::Small, I18n::Message::Warning, 0.5, 0.5, KDColorWhite, KDColorBlack),
    m_messageTextView1(KDText::FontSize::Small, I18n::Message::Default, 0.5, 0.5, KDColorWhite, KDColorBlack),
    m_messageTextView2(KDText::FontSize::Small, I18n::Message::Default, 0.5, 0.5, KDColorWhite, KDColorBlack),
    m_messageTextView3(KDText::FontSize::Small, I18n::Message::Default, 0.5, 0.5, KDColorWhite, KDColorBlack)
  {
  }
  
  void ExamPopUpController::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
    ctx->fillRect(bounds(), KDColorBlack);
  }
  
  void ExamPopUpController::ContentView::setSelectedButton(int selectedButton, App * app) {
    m_cancelButton.setHighlighted(selectedButton == 0);
    m_okButton.setHighlighted(selectedButton == 1);
    if (selectedButton == 0) {
      app->setFirstResponder(&m_cancelButton);
    } else {
      app->setFirstResponder(&m_okButton);
    }
  }
  
  int ExamPopUpController::ContentView::selectedButton() {
    if (m_cancelButton.isHighlighted()) {
      return 0;
    }
    return 1;
  }
  
  void ExamPopUpController::ContentView::setMessages(bool activingExamMode) {
    if (activingExamMode) {
      m_messageTextView1.setMessage(I18n::Message::ActiveExamModeMessage1);
      m_messageTextView2.setMessage(I18n::Message::ActiveExamModeMessage2);
      m_messageTextView3.setMessage(I18n::Message::ActiveExamModeMessage3);
    } else {
      m_messageTextView1.setMessage(I18n::Message::ExitExamMode1);
      m_messageTextView2.setMessage(I18n::Message::ExitExamMode2);
      m_messageTextView3.setMessage(I18n::Message::Default);
    }
  }
  
  int ExamPopUpController::ContentView::numberOfSubviews() const {
    return 6;
  }
  
  View * ExamPopUpController::ContentView::subviewAtIndex(int index) {
    switch (index) {
      case 0:
        return &m_warningTextView;
      case 1:
        return &m_messageTextView1;
      case 2:
        return &m_messageTextView2;
      case 3:
        return &m_messageTextView3;
      case 4:
        return &m_cancelButton;
      case 5:
        return &m_okButton;
      default:
        assert(false);
        return nullptr;
    }
  }
  
  void ExamPopUpController::ContentView::layoutSubviews() {
    KDCoordinate height = bounds().height();
    KDCoordinate width = bounds().width();
    KDCoordinate textHeight = KDText::charSize(KDText::FontSize::Small).height();
    m_warningTextView.setFrame(KDRect(0, k_topMargin, width, textHeight));
    m_messageTextView1.setFrame(KDRect(0, k_topMargin+k_paragraphHeight+textHeight, width, textHeight));
    m_messageTextView2.setFrame(KDRect(0, k_topMargin+k_paragraphHeight+2*textHeight, width, textHeight));
    m_messageTextView3.setFrame(KDRect(0, k_topMargin+k_paragraphHeight+3*textHeight, width, textHeight));
    m_cancelButton.setFrame(KDRect(k_buttonMargin, height-k_buttonMargin-k_buttonHeight, (width-3*k_buttonMargin)/2, k_buttonHeight));
    m_okButton.setFrame(KDRect(2*k_buttonMargin+(width-3*k_buttonMargin)/2, height-k_buttonMargin-k_buttonHeight, (width-3*k_buttonMargin)/2, k_buttonHeight));
  }