Numworks Epsilon  1.4.1
Graphing Calculator Operating System
zoom_parameter_controller.cpp
Go to the documentation of this file.
2 #include <assert.h>
3 #include <cmath>
4 
5 namespace Shared {
6 
8  ViewController(parentResponder),
9  m_contentView(curveView),
10  m_interactiveRange(interactiveRange)
11 {
12 }
13 
15  return I18n::translate(I18n::Message::Zoom);
16 }
17 
19  return &m_contentView;
20 }
21 
23  if (event == Ion::Events::Plus) {
24  float meanX = (m_interactiveRange->xMin()+m_interactiveRange->xMax())/2.0f;
25  float meanY = (m_interactiveRange->yMin()+m_interactiveRange->yMax())/2.0f;
26  m_interactiveRange->zoom(2.0f/3.0f, meanX, meanY);
27  m_contentView.curveView()->reload();
28  return true;
29  }
30  if (event == Ion::Events::Minus) {
31  float meanX = (m_interactiveRange->xMin()+m_interactiveRange->xMax())/2.0f;
32  float meanY = (m_interactiveRange->yMin()+m_interactiveRange->yMax())/2.0f;
33  m_interactiveRange->zoom(3.0f/2.0f, meanX, meanY);
34  m_contentView.curveView()->reload();
35  return true;
36  }
37  if (event == Ion::Events::Up) {
38  m_interactiveRange->panWithVector(0.0f, m_interactiveRange->yGridUnit());
39  m_contentView.curveView()->reload();
40  return true;
41  }
42  if (event == Ion::Events::Down) {
43  m_interactiveRange->panWithVector(0.0f, -m_interactiveRange->yGridUnit());
44  m_contentView.curveView()->reload();
45  return true;
46  }
47  if (event == Ion::Events::Left) {
48  m_interactiveRange->panWithVector(-m_interactiveRange->xGridUnit(), 0.0f);
49  m_contentView.curveView()->reload();
50  return true;
51  }
52  if (event == Ion::Events::Right) {
53  m_interactiveRange->panWithVector(m_interactiveRange->xGridUnit(), 0.0f);
54  m_contentView.curveView()->reload();
55  return true;
56  }
57 
58  return false;
59 }
60 
62  m_contentView.curveView()->setOkView(nullptr);
63 }
64 
66  m_contentView.layoutSubviews();
67 }
68 
69 /* Content View */
70 
71 ZoomParameterController::ContentView::ContentView(CurveView * curveView) :
72  m_curveView(curveView)
73 {
74 }
75 
76 int ZoomParameterController::ContentView::numberOfSubviews() const {
77  return 2;
78 }
79 
80 View * ZoomParameterController::ContentView::subviewAtIndex(int index) {
81  assert(index >= 0 && index < 2);
82  if (index == 0) {
83  return m_curveView;
84  }
85  return &m_legendView;
86 }
87 
88 void ZoomParameterController::ContentView::layoutSubviews() {
89  m_curveView->setFrame(KDRect(0, 0, bounds().width(), bounds().height() - k_legendHeight));
90  m_legendView.setFrame(KDRect(0, bounds().height() - k_legendHeight, bounds().width(), k_legendHeight));
91 }
92 
93 CurveView * ZoomParameterController::ContentView::curveView() {
94  return m_curveView;
95 }
96 
97 /* Legend View */
98 
99 ZoomParameterController::ContentView::LegendView::LegendView()
100 {
101  I18n::Message messages[k_numberOfLegends] = {I18n::Message::Move, I18n::Message::ToZoom, I18n::Message::Or};
102  float horizontalAlignments[k_numberOfLegends] = {1.0f, 1.0f, 0.5f};
103  for (int i = 0; i < k_numberOfLegends; i++) {
104  m_legends[i].setFontSize(KDText::FontSize::Small);
105  m_legends[i].setMessage(messages[i]);
106  m_legends[i].setBackgroundColor(Palette::GreyBright);
107  m_legends[i].setAlignment(horizontalAlignments[i], 0.5f);
108  }
110  for (int i = 0; i < k_numberOfTokens ; i++) {
111  m_legendPictograms[i].setType(tokenTypes[i]);
112  }
113 }
114 
115 void ZoomParameterController::ContentView::LegendView::drawRect(KDContext * ctx, KDRect rect) const {
116  ctx->fillRect(KDRect(0, bounds().height() - k_legendHeight, bounds().width(), k_legendHeight), Palette::GreyBright);
117 }
118 
119 int ZoomParameterController::ContentView::LegendView::numberOfSubviews() const {
120  return k_numberOfLegends+k_numberOfTokens;
121 }
122 
123 View * ZoomParameterController::ContentView::LegendView::subviewAtIndex(int index) {
124  assert(index >= 0 && index < k_numberOfTokens+k_numberOfLegends);
125  if (index < k_numberOfLegends) {
126  return &m_legends[index];
127  }
128  return &m_legendPictograms[index-k_numberOfLegends];
129 }
130 
131 void ZoomParameterController::ContentView::LegendView::layoutSubviews() {
132  KDCoordinate height = bounds().height();
133  KDCoordinate xOrigin = 0;
134  KDCoordinate legendWidth = m_legends[0].minimalSizeForOptimalDisplay().width();
135  m_legends[0].setFrame(KDRect(xOrigin, 0, legendWidth, height));
136  xOrigin += legendWidth;
137  for (int i = 0; i < k_numberOfTokens - 2; i++) {
138  m_legendPictograms[i].setFrame(KDRect(xOrigin, 0, k_tokenWidth, height));
139  xOrigin += k_tokenWidth;
140  }
141  xOrigin = bounds().width()/2;
142  for (int i = 1; i < k_numberOfLegends; i++) {
143  KDCoordinate legendWidth = m_legends[i].minimalSizeForOptimalDisplay().width();
144  m_legends[i].setFrame(KDRect(xOrigin, 0, legendWidth, height));
145  xOrigin += legendWidth;
146  m_legendPictograms[k_numberOfTokens - 3 + i].setFrame(KDRect(xOrigin, 0, k_tokenWidth, height));
147  xOrigin += k_tokenWidth;
148  }
149 }
150 
151 }
ZoomParameterController(Responder *parentResponder, InteractiveCurveViewRange *interactiveCurveViewRange, CurveView *curveView)
constexpr Event Plus
Definition: events.h:107
#define assert(e)
Definition: assert.h:9
constexpr Event Minus
Definition: events.h:108
int16_t KDCoordinate
Definition: coordinate.h:6
bool handleEvent(Ion::Events::Event event) override
constexpr Event Up
Definition: events.h:62
constexpr Event Down
Definition: events.h:63
void zoom(float ratio, float x, float y)
enum Message uint16_t enum Language uint16_t const char * translate(Message m, Language l=(Language) 0)
Definition: i18n.cpp:5
constexpr Event Left
Definition: events.h:61
Definition: rect.h:26
void fillRect(KDRect rect, KDColor color)
Definition: context_rect.cpp:8
Definition: view.h:23
constexpr Event Right
Definition: events.h:64
static constexpr KDColor GreyBright
Definition: palette.h:13