1 | #pragma once |
2 | |
3 | #include <string> |
4 | #include "taichi/ui/utils/utils.h" |
5 | #include "input_handler.h" |
6 | |
7 | #include <vector> |
8 | #include <unordered_map> |
9 | #include <queue> |
10 | #include <list> |
11 | #include <tuple> |
12 | |
13 | #include "taichi/ui/common/canvas_base.h" |
14 | #include "taichi/ui/common/event.h" |
15 | #include "taichi/ui/common/gui_base.h" |
16 | #include "taichi/ui/common/app_config.h" |
17 | #include "taichi/program/ndarray.h" |
18 | |
19 | namespace taichi::ui { |
20 | |
21 | class WindowBase { |
22 | public: |
23 | bool is_pressed(std::string button); |
24 | |
25 | bool is_running(); |
26 | |
27 | void set_is_running(bool value); |
28 | |
29 | std::pair<float, float> get_cursor_pos(); |
30 | |
31 | std::vector<Event> get_events(EventType tag); |
32 | |
33 | bool get_event(EventType tag); |
34 | |
35 | Event get_current_event(); |
36 | |
37 | void set_current_event(const Event &event); |
38 | |
39 | virtual CanvasBase *get_canvas(); |
40 | |
41 | virtual void show(); |
42 | |
43 | virtual std::pair<uint32_t, uint32_t> get_window_shape() = 0; |
44 | |
45 | virtual void write_image(const std::string &filename) = 0; |
46 | |
47 | virtual void copy_depth_buffer_to_ndarray(const taichi::lang::Ndarray &) = 0; |
48 | |
49 | virtual std::vector<uint32_t> &get_image_buffer(uint32_t &w, uint32_t &h) = 0; |
50 | |
51 | virtual GuiBase *gui(); |
52 | |
53 | virtual ~WindowBase(); |
54 | |
55 | protected: |
56 | AppConfig config_; |
57 | GLFWwindow *glfw_window_{nullptr}; |
58 | InputHandler input_handler_; |
59 | |
60 | // used for FPS counting |
61 | double last_record_time_{0.0}; |
62 | int frames_since_last_record_{0}; |
63 | |
64 | std::list<Event> events_; |
65 | Event current_event_{EventType::Any, "" }; |
66 | |
67 | protected: |
68 | explicit WindowBase(AppConfig config); |
69 | |
70 | void set_callbacks(); |
71 | |
72 | static void key_callback(GLFWwindow *glfw_window, |
73 | int key, |
74 | int scancode, |
75 | int action, |
76 | int mode); |
77 | |
78 | static void mouse_pos_callback(GLFWwindow *glfw_window, |
79 | double xpos, |
80 | double ypos); |
81 | |
82 | static void mouse_button_callback(GLFWwindow *glfw_window, |
83 | int button, |
84 | int action, |
85 | int modifier); |
86 | }; |
87 | |
88 | } // namespace taichi::ui |
89 | |