73 lines
1.9 KiB
Rust
Executable File
73 lines
1.9 KiB
Rust
Executable File
/*
|
|
* Copyright (C) 2022 Umut İnan Erdoğan <umutinanerdogan@pm.me>
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
use abletk_common::{Renderer, brush::Brush, color::Color, rgb};
|
|
use winit::event::MouseButton;
|
|
use crate::{widget::Widget, layout::position::Position, window::Window};
|
|
|
|
pub struct Label {
|
|
text: String,
|
|
fg_color: Color,
|
|
bg_color: Color,
|
|
}
|
|
|
|
impl Widget for Label {
|
|
fn draw(&self, renderer: &mut Renderer) {
|
|
let pos = self.position(renderer);
|
|
renderer.set_brush(Brush::Solid(self.bg_color));
|
|
renderer.fill_rect(pos.width(), pos.height());
|
|
renderer.set_brush(Brush::Solid(self.fg_color));
|
|
renderer.draw_text(&self.text);
|
|
}
|
|
|
|
fn position(&self, renderer: &mut Renderer) -> Position {
|
|
let (width, height) = renderer.get_text_size(&self.text);
|
|
Position::new(0, 0, width, height)
|
|
}
|
|
|
|
fn mouse_pressed(&mut self,
|
|
renderer: &mut Renderer,
|
|
window: &mut Window,
|
|
button: MouseButton,
|
|
x: u32,
|
|
y: u32,
|
|
) {
|
|
eprintln!("Label::mouse_pressed {} {x}, {y}", self.text);
|
|
}
|
|
|
|
fn mouse_released(&mut self,
|
|
renderer: &mut Renderer,
|
|
window: &mut Window,
|
|
button: MouseButton,
|
|
x: u32,
|
|
y: u32,
|
|
) {
|
|
eprintln!("Label::mouse_released {} {x}, {y}", self.text);
|
|
}
|
|
}
|
|
|
|
impl Label {
|
|
pub fn new<S: Into<String>>(text: S) -> Self {
|
|
Self {
|
|
text: text.into(),
|
|
fg_color: rgb!(0x000000FF),
|
|
bg_color: rgb!(0x00000000),
|
|
}
|
|
}
|
|
|
|
pub fn color(mut self, color: Color) -> Self {
|
|
self.fg_color = color;
|
|
self
|
|
}
|
|
|
|
pub fn bg_color(mut self, bg: Color) -> Self {
|
|
self.bg_color = bg;
|
|
self
|
|
}
|
|
}
|