1
0
Fork 0
forked from wry/wry

portal: add a desktop portal

This commit is contained in:
Julian Orth 2022-07-30 19:21:30 +02:00
parent 323a6ed953
commit a162055f1d
38 changed files with 2389 additions and 27 deletions

View file

@ -15,7 +15,7 @@ use {
texture::image_target,
},
renderer::context::RenderContext,
sys::{glDisable, glEnable, GL_BLEND},
sys::{glClear, glClearColor, glDisable, glEnable, GL_BLEND, GL_COLOR_BUFFER_BIT},
Texture,
},
theme::Color,
@ -33,6 +33,10 @@ pub struct RendererBase<'a> {
}
impl RendererBase<'_> {
pub fn scale(&self) -> Fixed {
self.scale
}
pub fn physical_extents(&self) -> Rect {
Rect::new_sized(0, 0, self.fb.width, self.fb.height).unwrap()
}
@ -45,6 +49,14 @@ impl RendererBase<'_> {
(x, y)
}
pub fn scale_point_f(&self, mut x: f32, mut y: f32) -> (f32, f32) {
if self.scaled {
x = (x as f64 * self.scalef) as _;
y = (y as f64 * self.scalef) as _;
}
(x, y)
}
pub fn scale_rect(&self, mut rect: Rect) -> Rect {
if self.scaled {
let x1 = (rect.x1() as f64 * self.scalef).round() as _;
@ -56,6 +68,25 @@ impl RendererBase<'_> {
rect
}
pub fn scale_rect_f(&self, mut rect: (f32, f32, f32, f32)) -> (f32, f32, f32, f32) {
if self.scaled {
let x1 = (rect.0 as f64 * self.scalef).round() as _;
let y1 = (rect.1 as f64 * self.scalef).round() as _;
let x2 = (rect.2 as f64 * self.scalef).round() as _;
let y2 = (rect.3 as f64 * self.scalef).round() as _;
rect = (x1, y1, x2, y2)
}
rect
}
fn xf_to_f(&self, x: f32) -> f32 {
2.0 * (x / self.fb.width as f32) - 1.0
}
fn yf_to_f(&self, y: f32) -> f32 {
2.0 * (y / self.fb.height as f32) - 1.0
}
fn x_to_f(&self, x: i32) -> f32 {
2.0 * (x as f32 / self.fb.width as f32) - 1.0
}
@ -64,6 +95,13 @@ impl RendererBase<'_> {
2.0 * (y as f32 / self.fb.height as f32) - 1.0
}
pub fn clear(&self, c: &Color) {
unsafe {
glClearColor(c.r, c.g, c.b, c.a);
glClear(GL_COLOR_BUFFER_BIT);
}
}
pub fn fill_boxes(&self, boxes: &[Rect], color: &Color) {
self.fill_boxes2(boxes, color, 0, 0);
}
@ -94,6 +132,36 @@ impl RendererBase<'_> {
self.fill_boxes3(&pos, color)
}
pub fn fill_boxes_f(&self, boxes: &[(f32, f32, f32, f32)], color: &Color) {
self.fill_boxes2_f(boxes, color, 0.0, 0.0);
}
pub fn fill_boxes2_f(&self, boxes: &[(f32, f32, f32, f32)], color: &Color, dx: f32, dy: f32) {
if boxes.is_empty() {
return;
}
let (dx, dy) = self.scale_point_f(dx, dy);
let mut pos = Vec::with_capacity(boxes.len() * 12);
for bx in boxes {
let (x1, y1, x2, y2) = self.scale_rect_f(*bx);
let x1 = self.xf_to_f(x1 + dx);
let y1 = self.yf_to_f(y1 + dy);
let x2 = self.xf_to_f(x2 + dx);
let y2 = self.yf_to_f(y2 + dy);
pos.extend_from_slice(&[
// triangle 1
x2, y1, // top right
x1, y1, // top left
x1, y2, // bottom left
// triangle 2
x2, y1, // top right
x1, y2, // bottom left
x2, y2, // bottom right
]);
}
self.fill_boxes3(&pos, color)
}
fn fill_boxes3(&self, boxes: &[f32], color: &Color) {
unsafe {
glUseProgram(self.ctx.fill_prog.prog);