From 14a1611e9e17e0d4d1f547a6c9972ff9f7488502 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Thu, 1 Aug 2019 00:09:51 +0200 Subject: [PATCH] Draw bar for backlight --- src/backlight.rs | 14 +++++++++++--- src/draw.rs | 23 +++++++++++++++++++++++ src/main.rs | 2 +- src/module.rs | 19 +++++++++++++------ 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/backlight.rs b/src/backlight.rs index 388bacb..3fc0746 100644 --- a/src/backlight.rs +++ b/src/backlight.rs @@ -1,6 +1,6 @@ use crate::buffer::Buffer; use crate::color::Color; -use crate::draw::{draw_text, ROBOTO_REGULAR}; +use crate::draw::{draw_bar, draw_text, ROBOTO_REGULAR}; use crate::module::{Input, ModuleImpl}; use chrono::{DateTime, Local}; @@ -103,12 +103,20 @@ impl ModuleImpl for Backlight { buf.memset(bg); draw_text( ROBOTO_REGULAR, - &mut buf.subdimensions((0, 0, 256, 24)), + &mut buf.subdimensions((0, 0, 128, 24)), bg, &Color::new(1.0, 1.0, 1.0, 1.0), 24.0, - &format!("backlight: {:}%", self.brightness() as u8), + "backlight", )?; + draw_bar( + &mut buf.subdimensions((128, 0, 384, 24)), + bg, + &Color::new(1.0, 1.0, 1.0, 1.0), + 384, + 24, + self.brightness() / 100.0, + ); Ok(vec![buf.get_signed_bounds()]) } diff --git a/src/draw.rs b/src/draw.rs index 8c52741..7dfb872 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -101,3 +101,26 @@ pub fn draw_text_fixed_width( Ok(()) } + +pub fn draw_bar( + buf: &mut Buffer, + background_color: &Color, + color: &Color, + length: u32, + height: u32, + fill: f32, +) -> Result<(), ::std::io::Error> { + let mut buf = buf.subdimensions((0, 0, length, height)); + + let mut fill_pos = (((length as f32) * fill) as u32); + if fill_pos > length { + fill_pos = length; + } + for y in 0..height { + for x in 0..fill_pos { + buf.put((x, y), &background_color.blend(color, 1.0)); + } + } + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index c0a565c..ca3e5c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -369,7 +369,7 @@ impl App { ]; if let Ok(m) = Backlight::new() { - modules.push(Module::new(Box::new(m), (720, 0, 256, 24))); + modules.push(Module::new(Box::new(m), (720, 0, 512, 32))); } App { diff --git a/src/module.rs b/src/module.rs index eef2815..abeb5a6 100644 --- a/src/module.rs +++ b/src/module.rs @@ -11,12 +11,19 @@ pub enum Input { } impl Input { - pub fn offset(&self, offset: (u32, u32)) -> Input{ - match self { - Input::Click{pos, button} => Input::Click{pos: (pos.0 - offset.0, pos.1 - offset.1), button: *button}, - Input::Scroll{pos, x, y} => Input::Scroll{pos: (pos.0 - offset.0, pos.1 - offset.1), x: *x, y: *y}, - } - } + pub fn offset(&self, offset: (u32, u32)) -> Input { + match self { + Input::Click { pos, button } => Input::Click { + pos: (pos.0 - offset.0, pos.1 - offset.1), + button: *button, + }, + Input::Scroll { pos, x, y } => Input::Scroll { + pos: (pos.0 - offset.0, pos.1 - offset.1), + x: *x, + y: *y, + }, + } + } } pub trait ModuleImpl {