Get painter and response in one egui call

This commit is contained in:
pjht 2024-01-27 17:47:52 -06:00
parent 47f2c2d4ef
commit a3b0bdbf7b
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -1,8 +1,8 @@
use std::{path::Path, sync::mpsc::Sender};
use eframe::{
egui::{self, TextureOptions, Ui, Widget, Id, Painter},
epaint::{vec2, Pos2, Rect, TextureHandle, pos2, Color32},
egui::{self, Id, Painter, Sense, TextureOptions, Ui, Widget},
epaint::{pos2, vec2, Color32, Pos2, Rect, TextureHandle},
};
use crate::audio::AudioMessage;
@ -55,18 +55,21 @@ impl Widget for Frontpanel<'_> {
);
let led_textures =
led::Textures::new(self.textures.led_on.clone(), self.textures.led_off.clone());
let resp = ui.allocate_response(
let (resp, painter) = ui.allocate_painter(
(800.0, 333.0).into(),
egui::Sense {
Sense {
click: false,
drag: false,
focusable: false,
},
);
let fp_rect = resp.rect;
dbg!(fp_rect);
let painter = Painter::new(ui.ctx().clone(), ui.layer_id(), ui.clip_rect().intersect(resp.rect));
painter.image(self.textures.fp.id(), painter.clip_rect(), NULL_UV, NULL_TINT);
let fp_rect = painter.clip_rect();
painter.image(
self.textures.fp.id(),
painter.clip_rect(),
NULL_UV,
NULL_TINT,
);
let scale_vec = painter.clip_rect().size() / vec2(800.0, 333.0);
for led in &LEDS {
let pos = (led.pos.to_vec2() * scale_vec).to_pos2() + fp_rect.left_top().to_vec2();
@ -102,7 +105,10 @@ impl Widget for Frontpanel<'_> {
let bit_mask = 1 << (16 - i);
let mut sw_state = state.ad_sws & bit_mask > 0;
ui.allocate_ui_at_rect(Rect::from_center_size(pos, vec2(11.0, 25.0)), |ui| {
if ui.add(switch::ToggleSwitch::new(&mut sw_state, &sw_textures)).drag_started() {
if ui
.add(switch::ToggleSwitch::new(&mut sw_state, &sw_textures))
.drag_started()
{
self.audio_tx.send(AudioMessage::PlaySwitchClick).unwrap();
};
});
@ -121,16 +127,16 @@ impl Widget for Frontpanel<'_> {
_ => unreachable!(),
};
ui.allocate_ui_at_rect(Rect::from_center_size(pos, vec2(11.0, 25.0)), |ui| {
if ui.add(switch::ThreePosSwitch::new(state, &sw_textures)).drag_started() {
if ui
.add(switch::ThreePosSwitch::new(state, &sw_textures))
.drag_started()
{
self.audio_tx.send(AudioMessage::PlaySwitchClick).unwrap();
};
});
}
}
ui.data_mut(|data| data.insert_temp(self.id, state));
if resp.drag_started() {
dbg!(self.id, resp.interact_pointer_pos());
}
resp
}
}