abletk/abletk-common/src/lib.rs

116 lines
2.6 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/.
*/
pub mod brush;
pub mod color;
pub mod rect;
use brush::Brush;
use color::Color;
use raw_window_handle::RawWindowHandle;
use rect::Rect;
#[cfg(windows)]
use abletk_direct2d as backend;
#[cfg(target_os = "linux")]
use abletk_cairo as backend;
pub struct Renderer {
renderer: backend::Renderer,
x: u32,
y: u32,
}
impl Renderer {
pub fn new(handle: RawWindowHandle) -> Self {
let handle = match handle {
#[cfg(windows)]
RawWindowHandle::Win32(handle) => handle,
#[cfg(target_os = "linux")]
RawWindowHandle::Xlib(handle) => handle,
_ => todo!(),
};
Self {
renderer: backend::Renderer::new(handle),
x: 0,
y: 0,
}
}
pub fn begin_draw(&mut self) {
self.renderer.begin_draw()
}
pub fn clear(&self, color: Color) {
self.renderer.clear(color)
}
pub fn draw_rect(&self, width: u32, height: u32) {
self.renderer.draw_rect(Rect::new(
self.x as f32,
self.y as f32,
width as f32,
height as f32,
))
}
pub fn draw_text<S: AsRef<str>>(&self, text: S) {
self.renderer.draw_text(
text.as_ref(),
Rect::new(
self.x as f32,
self.y as f32,
// these two need to be as big as possible to make sure that
// text is not cut off or wrapped
f32::INFINITY,
f32::INFINITY,
),
)
}
pub fn end_draw(&self) {
self.renderer.end_draw()
}
pub fn fill_rect(&self, width: u32, height: u32) {
self.renderer.fill_rect(Rect::new(
self.x as f32,
self.y as f32,
width as f32,
height as f32,
))
}
pub fn get_text_size(&self, text: &str) -> (u32, u32) {
self.renderer.get_text_size(text)
}
pub fn position(&self) -> (u32, u32) {
(self.x, self.y)
}
pub fn position_at(&mut self, x: u32, y: u32) {
self.x = x;
self.y = y
}
pub fn resized(&mut self, width: u32, height: u32) {
self.renderer.resized(width, height)
}
pub fn set_brush(&mut self, brush: Brush) {
self.renderer.set_brush(brush)
}
pub fn size(&self) -> (u32, u32) {
self.renderer.size()
}
}