Fix front panel not allocating size correctly

This commit is contained in:
pjht 2024-01-27 18:47:54 -06:00
parent a3b0bdbf7b
commit 9e0073df41
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
3 changed files with 76 additions and 75 deletions

View File

@ -1,7 +1,7 @@
use std::{path::Path, sync::mpsc::Sender};
use eframe::{
egui::{self, Id, Painter, Sense, TextureOptions, Ui, Widget},
egui::{self, Id, Painter, Response, Sense, TextureOptions, Ui, Widget},
epaint::{pos2, vec2, Color32, Pos2, Rect, TextureHandle},
};
@ -55,24 +55,19 @@ impl Widget for Frontpanel<'_> {
);
let led_textures =
led::Textures::new(self.textures.led_on.clone(), self.textures.led_off.clone());
let (resp, painter) = ui.allocate_painter(
(800.0, 333.0).into(),
let resp = ui.allocate_response(
vec2(800.0, 333.0),
Sense {
click: false,
drag: false,
focusable: false,
},
);
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);
let fp_rect = resp.rect;
ui.allocate_ui_at_rect(fp_rect, |ui| {
ui.image(&self.textures.fp);
for led in &LEDS {
let pos = (led.pos.to_vec2() * scale_vec).to_pos2() + fp_rect.left_top().to_vec2();
let pos = led.pos + fp_rect.left_top().to_vec2();
let led_data = 0xAAAA;
let led_on = (led_data & led.mask) > 0;
ui.allocate_ui_at_rect(Rect::from_center_size(pos, vec2(16.0, 16.0)), |ui| {
@ -83,7 +78,7 @@ impl Widget for Frontpanel<'_> {
// 1..17: A/D left to right
// 17..25: Action left to right
for (i, switch) in SWITCHES.iter().enumerate() {
let pos = (switch.pos.to_vec2() * scale_vec).to_pos2() + fp_rect.left_top().to_vec2();
let pos = switch.pos + fp_rect.left_top().to_vec2();
if i == 0 {
ui.allocate_ui_at_rect(Rect::from_center_size(pos, vec2(11.0, 25.0)), |ui| {
let mut power_inv = !state.power;
@ -136,6 +131,7 @@ impl Widget for Frontpanel<'_> {
});
}
}
});
ui.data_mut(|data| data.insert_temp(self.id, state));
resp
}

View File

@ -64,7 +64,7 @@ impl<'a> ThreePosSwitch<'a> {
impl Widget for ThreePosSwitch<'_> {
fn ui(self, ui: &mut eframe::egui::Ui) -> eframe::egui::Response {
let (resp, painter) = ui.allocate_painter((11.0, 25.0).into(), Sense::drag());
let (mut resp, painter) = ui.allocate_painter((11.0, 25.0).into(), Sense::drag());
painter.image(
self.textures.get_for_state(*self.state),
Rect::from_min_size(painter.clip_rect().left_top(), vec2(11.0, 25.0)),
@ -79,8 +79,10 @@ impl Widget for ThreePosSwitch<'_> {
} else {
*self.state = SwitchState::Up;
};
resp.mark_changed();
} else if resp.drag_released() {
*self.state = SwitchState::Neut;
resp.mark_changed();
}
resp
}
@ -99,7 +101,7 @@ impl<'a> ToggleSwitch<'a> {
impl Widget for ToggleSwitch<'_> {
fn ui(self, ui: &mut eframe::egui::Ui) -> eframe::egui::Response {
let (resp, painter) = ui.allocate_painter((11.0, 25.0).into(), Sense::drag());
let (mut resp, painter) = ui.allocate_painter((11.0, 25.0).into(), Sense::drag());
painter.image(
self.textures.get_for_bool(*self.state),
Rect::from_min_size(painter.clip_rect().left_top(), vec2(11.0, 25.0)),
@ -108,6 +110,7 @@ impl Widget for ToggleSwitch<'_> {
);
if resp.drag_started() {
*self.state = !*self.state;
resp.mark_changed();
}
resp
}

View File

@ -226,7 +226,7 @@ impl eframe::App for AltairEmulator {
});
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(Label::new("Hello"));
ui.style_mut().debug.debug_on_hover = true;
ui.add(Frontpanel::new(
Id::new("frontpanel"),
&self.textures,
@ -589,6 +589,8 @@ impl eframe::App for AltairEmulator {
// self.cpu.finish_m_cycle(data);
// self.update_fp();
// }
//
ui.add(Label::new("Hello"));
});
// let old_fan_enabled = self.options.fan_enabled;