-
Notifications
You must be signed in to change notification settings - Fork 5
/
embedded.rs
142 lines (128 loc) · 4.53 KB
/
embedded.rs
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
use egui;
use egui_glow::glow;
use fltk::{enums::*, prelude::*, *};
use std::rc::Rc;
use std::{cell::RefCell, time::Instant};
const SCREEN_WIDTH: u32 = 800;
const SCREEN_HEIGHT: u32 = 600;
fn main() {
let fltk_app = app::App::default().with_scheme(app::Scheme::Gtk);
app::get_system_colors();
app::set_font_size(20);
let mut main_win =
window::Window::new(100, 100, SCREEN_WIDTH as _, SCREEN_HEIGHT as _, None).center_screen();
let mut gl_win = window::GlWindow::new(5, 5, main_win.w() - 200, main_win.h() - 10, None);
gl_win.set_mode(Mode::Opengl3);
gl_win.end();
let mut col = group::Flex::default()
.column()
.with_size(185, 590)
.right_of(&gl_win, 5);
col.set_frame(FrameType::DownBox);
let mut frm = frame::Frame::default();
frm.set_color(Color::Red.inactive());
frm.set_frame(FrameType::FlatBox);
let mut slider = valuator::Slider::default().with_type(valuator::SliderType::HorizontalFill);
slider.clear_visible_focus();
slider.set_slider_frame(FrameType::RFlatBox);
slider.set_slider_size(0.20);
slider.set_color(Color::Blue.inactive());
slider.set_selection_color(Color::Red);
col.fixed(&mut slider, 20);
col.end();
main_win.end();
main_win.make_resizable(true);
main_win.show();
gl_win.make_current();
// Init backend
let (mut painter, egui_state) = fltk_egui::init(&mut gl_win);
let state = Rc::from(RefCell::from(egui_state));
main_win.handle({
let state = state.clone();
let mut w = gl_win.clone();
move |double_win, ev| match ev {
enums::Event::Push
| enums::Event::Released
| enums::Event::KeyDown
| enums::Event::KeyUp
| enums::Event::MouseWheel
| enums::Event::Resize
| enums::Event::Move
| enums::Event::Drag => {
// Using "if let ..." for safety.
if double_win.damage() {
double_win.clear_damage();
}
if let Ok(mut state) = state.try_borrow_mut() {
state.fuse_input(&mut w, ev);
true
} else {
false
}
}
_ => false,
}
});
// Set visual scale or egui display scaling
state.borrow_mut().set_visual_scale(1.5);
let egui_ctx = egui::Context::default();
let start_time = Instant::now();
let mut name = String::new();
let mut age: i32 = 0;
let mut quit = false;
while fltk_app.wait() {
// Clear the screen to dark red
let gl = painter.gl().as_ref();
draw_background(gl);
let mut state = state.borrow_mut();
state.input.time = Some(start_time.elapsed().as_secs_f64());
frm.set_label(&format!("Hello {}", &name));
slider.set_value(age as f64 / 120.);
let egui_output = egui_ctx.run(state.take_input(), |ctx| {
egui::CentralPanel::default().show(&ctx, |ui| {
ui.heading("My egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit_singleline(&mut name);
});
ui.add(egui::Slider::new(&mut age, 0..=120).text("age"));
if ui.button("Click each year").clicked() {
age += 1;
}
ui.label(format!("Hello '{}', age {}", name, age));
ui.separator();
if ui
.button("Quit?")
.on_hover_cursor(egui::CursorIcon::PointingHand)
.clicked()
{
quit = true;
}
});
});
if egui_ctx.has_requested_repaint() || state.window_resized() {
state.fuse_output(&mut gl_win, egui_output.platform_output);
let meshes = egui_ctx.tessellate(egui_output.shapes, gl_win.pixels_per_unit());
painter.paint_and_update_textures(
state.canvas_size,
state.pixels_per_point(),
&meshes,
&egui_output.textures_delta,
);
gl_win.swap_buffers();
gl_win.flush();
app::awake();
}
if quit {
break;
}
}
painter.destroy();
}
fn draw_background<GL: glow::HasContext>(gl: &GL) {
unsafe {
gl.clear_color(0.6, 0.3, 0.3, 1.0);
gl.clear(glow::COLOR_BUFFER_BIT);
gl.clear(glow::DEPTH_BUFFER_BIT);
}
}