blob: 4674bc0f4b482e83f8f67c2d3f61493af4a0ebcd [file] [log] [blame]
James Kuszmaul6271cf72019-09-18 20:10:08 -07001#ifndef FRC971_ANALYSIS_PLOTTING_WEBGL2_ANIMATOR_H_
2#define FRC971_ANALYSIS_PLOTTING_WEBGL2_ANIMATOR_H_
3
4#include <Eigen/Dense>
5#include <emscripten/emscripten.h>
6#include <emscripten/html5.h>
7
8#include "frc971/analysis/plotting/webgl2_plotter.h"
9
10namespace frc971 {
11namespace plotting {
12
13// TODO(james): Write some tests for this class. It shouldn't be too hard to
14// abstract out all the direct emscripten calls. Mostly it's just some
15// initialization at the moment.
16class Animator {
17 public:
18 Animator(const char *canvas_target);
19
20 Plotter *plotter() { return &plotter_; }
21
22 private:
23 Eigen::Vector2d MouseCanvasLocation(const EmscriptenMouseEvent &mouse_event);
24
25 Eigen::Vector2d CanvasToPlotLocation(const Eigen::Vector2d &canvas_loc);
26
27 void PrintZoom();
28
29 void PrintPosition(const EmscriptenMouseEvent &mouse_event);
30
31 void HandleMouseUp(const EmscriptenMouseEvent &mouse_event);
32
33 void HandleMouseDown(const EmscriptenMouseEvent &mouse_event);
34
35 void HandleMouseMove(const EmscriptenMouseEvent &mouse_event);
36
37 void HandleMouseEnter(const EmscriptenMouseEvent &mouse_event);
38
39 void SetZoomCorners(const Eigen::Vector2d &c1, const Eigen::Vector2d &c2);
40
41 void SetFilteredZoom(Eigen::Vector2d scale, Eigen::Vector2d offset);
42
43 void ResetView();
44
45 static int Redraw(double time_ms, void *data);
46 static int KeyboardCallback(int event_type,
47 const EmscriptenKeyboardEvent *key_event,
48 void *data);
49 static int WheelCallback(int event_type,
50 const EmscriptenWheelEvent *wheel_event, void *data);
51 static int MouseCallback(int event_type,
52 const EmscriptenMouseEvent *mouse_event, void *data);
53
54 int canvas_width_ = 0.0;
55 int canvas_height_ = 0.0;
56
57 // Location, in canvas coordinates of the last left click mouse-down event.
58 Eigen::Vector2d mouse_down_location_{0, 0};
59
60 // True if the user is currently dragging their mouse to zoom to a rectangle.
61 // This is used to (a) determine whether we should subsequently execute the
62 // zoom when the user releases the mouse and (b) to know when to draw a
63 // rectangle indicating where the user is zooming to.
64 bool doing_rectangle_zoom_ = false;
65
66 // The last location of the mouse when panning, so that we can calculate
67 // exactly how much the mouse has moved since the last mouse-move callback.
68 Eigen::Vector2d last_pan_mouse_location_{0, 0};
69
70 // Whether the "x" or "y" key is currently pressed on the keyboard.
71 bool x_pressed_ = false;
72 bool y_pressed_ = false;
73
74 WebglCanvasPlotter plotter_;
75};
76
77} // namespace plotting
78} // namespace frc971
79#endif // FRC971_ANALYSIS_PLOTTING_WEBGL2_ANIMATOR_H_