Split drawing into functions
This commit is contained in:
parent
335079ac14
commit
f3cee84c51
@ -1,5 +1,6 @@
|
||||
use egui::{
|
||||
Button, Color32, FontId, Frame, Grid, Key, Label, RichText, Rounding, Stroke, Style, Vec2,
|
||||
Button, Color32, FontId, Frame, Grid, Key, Label, RichText, Rounding, ScrollArea, Stroke,
|
||||
Style, Vec2,
|
||||
};
|
||||
use rpn_rs::calc::{errors::CalculatorError, Calculator};
|
||||
use tracing::{error, info};
|
||||
@ -56,6 +57,76 @@ impl TemplateApp {
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_stack(&mut self, ui: &mut egui::Ui) {
|
||||
ui.add(Label::new(
|
||||
RichText::new(
|
||||
self.calculator
|
||||
.stack
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|e| e.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
)
|
||||
.background_color(egui::Color32::RED)
|
||||
.font(DEFAULT_FONT)
|
||||
.size(STACK_FONT_SIZE)
|
||||
.color(Color32::WHITE),
|
||||
));
|
||||
}
|
||||
|
||||
fn draw_error(&mut self, ui: &mut egui::Ui) {
|
||||
if let Some(ref e) = self.latest_error {
|
||||
ui.label(
|
||||
RichText::new(e.to_string())
|
||||
.font(DEFAULT_FONT)
|
||||
.size(STACK_FONT_SIZE)
|
||||
.color(Color32::RED),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_input(&mut self, ui: &mut egui::Ui) {
|
||||
egui::Frame::none()
|
||||
.fill(egui::Color32::RED)
|
||||
.inner_margin(5.0)
|
||||
.stroke(Stroke::new(10.0, Color32::BLUE))
|
||||
.show(ui, |ui| {
|
||||
ui.label(
|
||||
RichText::new(self.calculator.get_l())
|
||||
.color(Color32::WHITE)
|
||||
.font(DEFAULT_FONT),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
fn draw_buttons(&mut self, ui: &mut egui::Ui) {
|
||||
Grid::new("grid").spacing(BUTTON_SPACING).show(ui, |ui| {
|
||||
for row in BUTTON_LAYOUT.iter() {
|
||||
for button in row.iter() {
|
||||
let label = RichText::new(button.value)
|
||||
.font(DEFAULT_FONT)
|
||||
.color(Color32::WHITE);
|
||||
if ui
|
||||
.add_enabled(
|
||||
button.enabled,
|
||||
Button::new(label)
|
||||
.stroke(Stroke::NONE)
|
||||
.rounding(Rounding::none())
|
||||
.fill(Color32::from_gray(0x12)),
|
||||
)
|
||||
.on_hover_text(button.help)
|
||||
.clicked()
|
||||
{
|
||||
info!("Clicked button {}", button.value);
|
||||
self.calculator_input(button.value);
|
||||
}
|
||||
}
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn handle_input(&mut self, i: &egui::InputState) {
|
||||
if i.events.is_empty() {
|
||||
return;
|
||||
@ -161,62 +232,15 @@ impl eframe::App for TemplateApp {
|
||||
ui.hyperlink("https://gitea.austen-wares.com/stonewareslord/rpn_rs");
|
||||
|
||||
// Buttons
|
||||
Grid::new("grid").spacing(BUTTON_SPACING).show(ui, |ui| {
|
||||
for row in BUTTON_LAYOUT.iter() {
|
||||
for button in row.iter() {
|
||||
let label = RichText::new(button.value)
|
||||
.font(DEFAULT_FONT)
|
||||
.color(Color32::WHITE);
|
||||
if ui
|
||||
.add_enabled(
|
||||
button.enabled,
|
||||
Button::new(label)
|
||||
// .frame(false)
|
||||
.stroke(Stroke::NONE)
|
||||
.rounding(Rounding::none())
|
||||
.fill(Color32::from_gray(0x12)),
|
||||
)
|
||||
.on_hover_text(button.help)
|
||||
.clicked()
|
||||
{
|
||||
info!("Clicked button {}", button.value);
|
||||
self.calculator_input(button.value);
|
||||
}
|
||||
}
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
self.draw_buttons(ui);
|
||||
|
||||
// Stack
|
||||
// ui.add_sized( Vec2::new(ui.available_width(), ui.available_height()),
|
||||
ui.add(Label::new(
|
||||
RichText::new(
|
||||
self.calculator
|
||||
.stack
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|e| e.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
)
|
||||
.background_color(egui::Color32::RED)
|
||||
.font(DEFAULT_FONT)
|
||||
.size(STACK_FONT_SIZE)
|
||||
.color(Color32::WHITE),
|
||||
));
|
||||
|
||||
egui::Frame::none()
|
||||
.fill(egui::Color32::RED)
|
||||
.inner_margin(5.0)
|
||||
.stroke(Stroke::new(10.0, Color32::BLUE))
|
||||
.show(ui, |ui| {
|
||||
ui.label(
|
||||
RichText::new(self.calculator.get_l())
|
||||
.color(Color32::WHITE)
|
||||
.font(DEFAULT_FONT),
|
||||
);
|
||||
});
|
||||
ScrollArea::vertical().show(ui, |ui| {
|
||||
self.draw_stack(ui);
|
||||
});
|
||||
|
||||
self.draw_input(ui);
|
||||
// Reset the error state and update `self.latest_error` if required
|
||||
match std::mem::take(&mut self.error_state) {
|
||||
ErrorState::NoModify => {}
|
||||
@ -224,15 +248,7 @@ impl eframe::App for TemplateApp {
|
||||
ErrorState::Clear => self.latest_error = None,
|
||||
}
|
||||
|
||||
if let Some(ref e) = self.latest_error {
|
||||
ui.label(
|
||||
RichText::new(e.to_string())
|
||||
.font(DEFAULT_FONT)
|
||||
.size(STACK_FONT_SIZE)
|
||||
.color(Color32::RED),
|
||||
);
|
||||
}
|
||||
|
||||
self.draw_error(ui);
|
||||
egui::warn_if_debug_build(ui);
|
||||
});
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user