aboutsummaryrefslogtreecommitdiff
path: root/src/controllers/timer_view.cpp
blob: 28abd5c05ba30419856d22ec6239c3767558f592 (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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <emscripten.h>

#include <cstdio>
#include <string>
#include <memory>
#include <array>

#include "models/timer.hpp"
#include "models/counter.hpp"
#include "utils/utils.hpp"

using emval = emscripten::val;

thread_local const emval document = emval::global("document");

class TimerView {

	public:

	TimerView()
		: _counters { FreqCounter(_timer), FreqCounter(_timer) }
		, _alarm_sound_triggered(false)
	{ }

	/*
	 * Timer
	 */

	void timer_display_text(std::string const & text) {
		set_inner_text_by_id("time-display", text);
	}

	void timer_record_floor(std::string const & text) {
		set_inner_text_by_id("record-floor", text);
	}

	void timer_start_stop_button_label(std::string const & text) {
		set_inner_text_by_id("start-stop-btn", text);
	}

	void timer_clear_reset_button_label(std::string const & text) {
		set_inner_text_by_id("clear-reset-btn", text);
	}

	void handle_inc_time(uint32_t amount) {
		_timer.increment_preset_ms(amount * 1000);

		_update_start_stop_label();
		_update_clear_reset_btn_label();
		_update_clear_reset_btn();
	}

	void handle_start_stop() {
		_timer.toggleStart();

		_update_start_stop_label();
		_update_clear_reset_btn_label();
		_update_clear_reset_btn();
	}

	void handle_clear_reset() {
		if(_clear_reset_btn_clears()) {
			clear();
		} else {
			reset();
		}

		_update_clear_reset_btn_label();
	}

	/*
	 * Counter
	 */

	void counter_display_text(uint32_t counter_num, std::string const & counter_text, std::string const & rate_text) {
		std::string counter_id = "counter-";
		counter_id.append(std::to_string(counter_num));

		std::string rate_id = "rate-";
		rate_id.append(std::to_string(counter_num));

		set_inner_text_by_id(counter_id, counter_text);
		set_inner_text_by_id(rate_id, rate_text);
	}

	void handle_inc_counter(uint32_t counter_num) {
		_counters[counter_num].inc();

		_update_counter(counter_num);
	}

	void handle_timer_update() {

		std::string value = _timer.counter_display_value();

		timer_display_text(value);

		double record_floor = _timer.record_floor();

		std::string rec_floor_text;
		format_rate(rec_floor_text, record_floor);

		if(_timer.overtime()) {
			std::string rec_floor_overtime;
			format_rate(rec_floor_overtime, _timer.overtime_record_floor());
			rec_floor_text.append(", ");
			rec_floor_text.append(rec_floor_overtime);
		}

		timer_record_floor(rec_floor_text);

		for(size_t i = 0; i < _counters.size(); i++)
			_update_counter(i);

		if(_timer.overtime() && !_alarm_sound_triggered) {
			_play_alarm();
			_alarm_sound_triggered = true;
		}
	}

	private:

	FreqTimer _timer;
	std::array<FreqCounter, 2> _counters;
	bool _alarm_sound_triggered;

	static void set_inner_text_by_id(std::string const & id, std::string const & text) {
		emval timer_display = document.call<emval>("getElementById", emval(id.c_str()));
		timer_display.set("innerText", text.c_str());
	}

	static void set_button_disabled(std::string const & id, bool disabled) {
		emval timer_display = document.call<emval>("getElementById", emval(id.c_str()));
		timer_display.set("disabled", disabled);
	}

	void _update_counter(uint32_t counter_num) {
		FreqCounter & counter = _counters[counter_num];

		std::string text = counter.display_value();
		double rate = counter.rate();

		std::string rate_val;
		format_rate(rate_val, rate);

		if(_timer.overtime()) {
			std::string rate_overtime;
			format_rate(rate_overtime, counter.overtime_rate());
			rate_val.append(", ");
			rate_val.append(rate_overtime);
		}

		counter_display_text(counter_num, text, rate_val);
	}

	static void _play_alarm() {

		EM_ASM({
			const handle = document.getElementById("sound-handle");

			const duration = Math.round(handle.duration * 1000.0);

			handle.currentTime = 0;
			handle.play().then(() => {
				setTimeout(() => {
					handle.pause();
				}, duration);
			});
		});

	}

	void _update_start_stop_label() {
		if(_timer.running()) {
			timer_start_stop_button_label("Stop");
		} else {
			timer_start_stop_button_label("Start");
		}
	}

	bool _clear_reset_btn_clears() const {
		return _timer.has_preset() and !_timer.started();
	}

	void _update_clear_reset_btn_label() {
		if(_clear_reset_btn_clears()) {
			timer_clear_reset_button_label("Clear");
		} else {
			timer_clear_reset_button_label("Reset");
		}
	}

	void _update_clear_reset_btn() {
		set_button_disabled("clear-reset-btn", _timer.running());
	}

	void clear() {
		_timer.clear();

		for(FreqCounter & counter : _counters)
			counter.reset();

		_alarm_sound_triggered = false;
	}

	void reset() {
		_timer.reset();

		for(FreqCounter & counter : _counters)
			counter.reset();

		_alarm_sound_triggered = false;
	}

};


EMSCRIPTEN_BINDINGS(timer_view) {
	emscripten::class_<TimerView>("TimerView")
		.constructor()
		.function("handle_start_stop",
			&TimerView::handle_start_stop
		)
		.function("handle_clear_reset",
			&TimerView::handle_clear_reset
		)
		.function("handle_inc_counter",
			&TimerView::handle_inc_counter
		)
		.function("handle_inc_time",
			&TimerView::handle_inc_time
		)
		.function("handle_timer_update",
			&TimerView::handle_timer_update
		);
}