-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathmain.rs
More file actions
130 lines (107 loc) · 4.21 KB
/
Copy pathmain.rs
File metadata and controls
130 lines (107 loc) · 4.21 KB
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
use baseview::dpi::{LogicalSize, PhysicalPosition};
use baseview::gl::{GlConfig, GlContext};
use baseview::{
Event, EventStatus, HandlerError, MouseEvent, RedrawStrategy, Window, WindowContext,
WindowHandler, WindowSettings, WindowSize,
};
use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color};
use std::cell::{Cell, RefCell};
struct FemtovgExample {
window_context: WindowContext,
gl_context: GlContext,
canvas: RefCell<Canvas<OpenGl>>,
current_mouse_position: Cell<PhysicalPosition<f64>>,
}
impl FemtovgExample {
fn new(window_context: WindowContext) -> Result<Self, HandlerError> {
let Some(gl_context) = window_context.gl_context() else { unreachable!() };
unsafe { gl_context.make_current()? };
let renderer =
unsafe { OpenGl::new_from_function_cstr(|s| gl_context.get_proc_address(s)) }?;
let mut canvas = Canvas::new(renderer)?;
let size = window_context.size();
canvas.set_size(size.physical.width, size.physical.height, size.scale_factor as f32);
unsafe { gl_context.make_not_current()? };
Ok(Self {
gl_context,
window_context,
canvas: canvas.into(),
current_mouse_position: Cell::new(PhysicalPosition::default()),
})
}
}
impl WindowHandler for FemtovgExample {
fn draw(&self) -> Result<(), HandlerError> {
let context = &self.gl_context;
unsafe { context.make_current()? };
let mut canvas = self.canvas.borrow_mut();
let screen_height = canvas.height();
let screen_width = canvas.width();
// Clear
canvas.clear_rect(0, 0, screen_width, screen_height, Color::rgb(0xAA, 0xAA, 0xAA));
// Make big blue rectangle
canvas.clear_rect(
(screen_width as f32 * 0.1).floor() as u32,
(screen_height as f32 * 0.1).floor() as u32,
(screen_width as f32 * 0.8).floor() as u32,
(screen_height as f32 * 0.8).floor() as u32,
Color::rgbf(0., 0.3, 0.9),
);
let mouse_position = self.current_mouse_position.get().cast::<i32>();
// Make smol orange rectangle
canvas.clear_rect(
(mouse_position.x - 15).clamp(0, screen_width as i32 - 30) as u32,
(mouse_position.y - 15).clamp(0, screen_height as i32 - 30) as u32,
30,
30,
Color::rgbf(0.9, 0.3, 0.),
);
// Tell renderer to execute all drawing commands
canvas.flush();
context.swap_buffers()?;
unsafe { context.make_not_current()? };
Ok(())
}
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
let size = new_size.physical;
self.canvas.borrow_mut().set_size(size.width, size.height, new_size.scale_factor as f32);
Ok(())
}
fn on_event(&self, event: Event) -> EventStatus {
match event {
Event::Mouse(
MouseEvent::CursorMoved { position, .. }
| MouseEvent::DragEntered { position, .. }
| MouseEvent::DragMoved { position, .. }
| MouseEvent::DragDropped { position, .. },
) => {
self.current_mouse_position.set(position);
if position.y > 400. && !self.window_context.has_focus() {
let _ = self.window_context.focus();
}
self.window_context.request_redraw();
}
event => log_event(&event),
};
EventStatus::Captured
}
}
fn main() -> Result<(), baseview::Error> {
tracing_subscriber::fmt::init();
let window_open_options = WindowSettings::new()
.with_title("Femtovg on Baseview")
.with_size(LogicalSize::new(512, 512))
.with_redraw_strategy(RedrawStrategy::OnDemand)
.with_gl_config(GlConfig { alpha_bits: 8, ..GlConfig::default() });
Window::create(window_open_options, FemtovgExample::new)?.run_until_closed()?;
Ok(())
}
fn log_event(event: &Event) {
match event {
Event::Mouse(e) => println!("Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
Event::Window(e) => println!("Window event: {:?}", e),
_ => {}
}
}