add battery-ui #6

Merged
LuckyTurtleDev merged 6 commits from battery-ui into main 2024-07-06 20:45:06 +00:00
3 changed files with 36 additions and 19 deletions
Showing only changes of commit 255df67d7b - Show all commits

View file

@ -8,14 +8,17 @@ use crate::{
#[derive(Debug)]
pub struct Gost {
/// current electric charge of the Ghost
charge: u32,
pub charge: f32,
/// max electric charge of the Ghost
max_charge: u32,
pub max_charge: f32
}
impl Default for Gost {
fn default() -> Self {
Self { charge: 100, max_charge: 100 }
Self {
charge: 1000.0,
max_charge: 1000.0
}
}
}

View file

@ -11,14 +11,14 @@ const GAME_NAME: &str = "Powercreep";
#[derive(Debug)]
struct State {
activity: Activity,
gohst: Gost,
gohst: Gost
}
impl Default for State {
fn default() -> Self {
Self {
activity: Activity::House,
gohst: Default::default(),
gohst: Default::default()
}
}
}

View file

@ -1,17 +1,31 @@
use comfy::egui::Pos2;
use comfy::egui::Rect;
use comfy::EngineContext;
use comfy::egui;
use comfy::GREEN;
use comfy::RED;
use crate::State;
use comfy::{
draw_rect, draw_rect_outline, screen_height, screen_to_world, screen_width,
EngineContext, Vec2, BLUE, RED
};
pub fn draw(state: &State, engine: &EngineContext) {
egui::Area::new("batterie_show")
.anchor(egui::Align2::RIGHT_BOTTOM, egui::vec2(0.0, 0.0))
.show(egui(), |ui| {
let painter = ui.painter();
painter.rect(Rect{min: Pos2::new(0.0, 0.0), max: Pos2::new(32.0, 32.0)}, 0.0, RED, (4.0, GREEN));
});
// seperate fill state into smaller section for better readability
let section_count = 5;
let mut start_positon = screen_to_world(Vec2::new(screen_width(), screen_height()));
// section size in world codinates
let section_size = Vec2::new(0.5, 0.25);
start_positon.x -= 0.5 * section_size.x + 0.5 * section_size.y;
start_positon.y += 0.5 * section_size.y + 0.5 * section_size.y;
// draw fill level
{
let gohst = &state.gohst;
let percent = gohst.charge / gohst.max_charge;
let mut size = section_size;
size.y = section_size.y * section_count as f32 * percent;
let mut position = start_positon;
position.y += 0.5 * -section_size.y + 0.5 * size.y;
draw_rect(position, size, BLUE, 100);
}
// draw sections
for i in 0 .. section_count {
let mut position = start_positon;
position.y += i as f32 * section_size.y;
draw_rect_outline(position, section_size, 0.1, RED, 100);
}
}