mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 06:11:16 +00:00
Move the ItemRenderer to the item_rendering module
This commit is contained in:
parent
b0fd400cd1
commit
c7ff67d0fc
7 changed files with 50 additions and 51 deletions
|
@ -289,7 +289,7 @@ pub type RenderingCache<T> = vec_arena::Arena<CachedGraphicsData<T>>;
|
||||||
/// 1. A series of low-level rendering primitives can be rendered into a frame, that's started using [GraphicsBackend::new_frame].
|
/// 1. A series of low-level rendering primitives can be rendered into a frame, that's started using [GraphicsBackend::new_frame].
|
||||||
/// The low-level rendering primitives are intended to be fast and ready for rendering.
|
/// The low-level rendering primitives are intended to be fast and ready for rendering.
|
||||||
pub trait GraphicsBackend: Sized {
|
pub trait GraphicsBackend: Sized {
|
||||||
type ItemRenderer: crate::items::ItemRenderer;
|
type ItemRenderer: crate::item_rendering::ItemRenderer;
|
||||||
fn new_renderer(&mut self, clear_color: &Color) -> Self::ItemRenderer;
|
fn new_renderer(&mut self, clear_color: &Color) -> Self::ItemRenderer;
|
||||||
fn flush_renderer(&mut self, renderer: Self::ItemRenderer);
|
fn flush_renderer(&mut self, renderer: Self::ItemRenderer);
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,12 @@ LICENSE END */
|
||||||
//! module for rendering the tree of items
|
//! module for rendering the tree of items
|
||||||
|
|
||||||
use super::graphics::{GraphicsBackend, RenderingCache};
|
use super::graphics::{GraphicsBackend, RenderingCache};
|
||||||
use super::items::ItemRef;
|
use super::items::*;
|
||||||
use crate::component::ComponentRc;
|
use crate::component::ComponentRc;
|
||||||
|
use crate::graphics::{Point, Rect};
|
||||||
use crate::item_tree::ItemVisitorResult;
|
use crate::item_tree::ItemVisitorResult;
|
||||||
use crate::slice::Slice;
|
use crate::slice::Slice;
|
||||||
|
use core::pin::Pin;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
|
|
||||||
/// This structure must be present in items that are Rendered and contains information.
|
/// This structure must be present in items that are Rendered and contains information.
|
||||||
|
@ -61,8 +63,6 @@ pub(crate) fn render_component_items<Backend: GraphicsBackend>(
|
||||||
renderer: &mut Backend::ItemRenderer,
|
renderer: &mut Backend::ItemRenderer,
|
||||||
origin: crate::graphics::Point,
|
origin: crate::graphics::Point,
|
||||||
) {
|
) {
|
||||||
use crate::items::ItemRenderer;
|
|
||||||
|
|
||||||
let renderer = RefCell::new(renderer);
|
let renderer = RefCell::new(renderer);
|
||||||
|
|
||||||
crate::item_tree::visit_items_with_post_visit(
|
crate::item_tree::visit_items_with_post_visit(
|
||||||
|
@ -71,10 +71,8 @@ pub(crate) fn render_component_items<Backend: GraphicsBackend>(
|
||||||
|_, item, _, translation| {
|
|_, item, _, translation| {
|
||||||
let saved_clip = renderer.borrow_mut().clip_rects();
|
let saved_clip = renderer.borrow_mut().clip_rects();
|
||||||
|
|
||||||
item.as_ref().render(
|
item.as_ref()
|
||||||
*translation,
|
.render(*translation, &mut (*renderer.borrow_mut() as &mut dyn ItemRenderer));
|
||||||
&mut (*renderer.borrow_mut() as &mut dyn crate::items::ItemRenderer),
|
|
||||||
);
|
|
||||||
|
|
||||||
let origin = item.as_ref().geometry().origin;
|
let origin = item.as_ref().geometry().origin;
|
||||||
let translation = *translation + euclid::Vector2D::new(origin.x, origin.y);
|
let translation = *translation + euclid::Vector2D::new(origin.x, origin.y);
|
||||||
|
@ -97,3 +95,31 @@ pub(crate) fn free_item_rendering_data<'a, Backend: GraphicsBackend>(
|
||||||
renderer.borrow().release_item_graphics_cache(cached_rendering_data)
|
renderer.borrow().release_item_graphics_cache(cached_rendering_data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait ItemRenderer {
|
||||||
|
/// will draw a rectangle in (pos.x + rect.x)
|
||||||
|
fn draw_rectangle(&mut self, pos: Point, rect: Pin<&Rectangle>);
|
||||||
|
fn draw_border_rectangle(&mut self, pos: Point, rect: Pin<&BorderRectangle>);
|
||||||
|
fn draw_image(&mut self, pos: Point, image: Pin<&Image>);
|
||||||
|
fn draw_clipped_image(&mut self, pos: Point, image: Pin<&ClippedImage>);
|
||||||
|
fn draw_text(&mut self, pos: Point, text: Pin<&Text>);
|
||||||
|
fn draw_text_input(&mut self, pos: Point, text_input: Pin<&TextInput>);
|
||||||
|
fn draw_path(&mut self, pos: Point, path: Pin<&Path>);
|
||||||
|
fn combine_clip(&mut self, pos: Point, clip: &Pin<&Clip>);
|
||||||
|
fn clip_rects(&self) -> SharedVector<Rect>;
|
||||||
|
fn reset_clip(&mut self, rects: SharedVector<Rect>);
|
||||||
|
|
||||||
|
/// Returns the scale factor
|
||||||
|
fn scale_factor(&self) -> f32;
|
||||||
|
|
||||||
|
/// Draw a pixmap in position indicated by the `pos`.
|
||||||
|
/// The pixmap will be taken from cache if the cache is valid, otherwise, update_fn will be called
|
||||||
|
/// with a callback that need to be called once with `fn (width, height, data)` where data are the
|
||||||
|
/// argb premultiplied pixel values
|
||||||
|
fn draw_cached_pixmap(
|
||||||
|
&mut self,
|
||||||
|
item_cache: &CachedRenderingData,
|
||||||
|
pos: Point,
|
||||||
|
update_fn: &dyn Fn(&mut dyn FnMut(u32, u32, &[u8])),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -45,37 +45,9 @@ pub use text::*;
|
||||||
mod image;
|
mod image;
|
||||||
pub use self::image::*;
|
pub use self::image::*;
|
||||||
|
|
||||||
pub trait ItemRenderer {
|
|
||||||
/// will draw a rectangle in (pos.x + rect.x)
|
|
||||||
fn draw_rectangle(&mut self, pos: Point, rect: Pin<&Rectangle>);
|
|
||||||
fn draw_border_rectangle(&mut self, pos: Point, rect: Pin<&BorderRectangle>);
|
|
||||||
fn draw_image(&mut self, pos: Point, image: Pin<&Image>);
|
|
||||||
fn draw_clipped_image(&mut self, pos: Point, image: Pin<&ClippedImage>);
|
|
||||||
fn draw_text(&mut self, pos: Point, text: Pin<&Text>);
|
|
||||||
fn draw_text_input(&mut self, pos: Point, text_input: Pin<&TextInput>);
|
|
||||||
fn draw_path(&mut self, pos: Point, path: Pin<&Path>);
|
|
||||||
fn combine_clip(&mut self, pos: Point, clip: &Pin<&Clip>);
|
|
||||||
fn clip_rects(&self) -> SharedVector<Rect>;
|
|
||||||
fn reset_clip(&mut self, rects: SharedVector<Rect>);
|
|
||||||
|
|
||||||
/// Returns the scale factor
|
|
||||||
fn scale_factor(&self) -> f32;
|
|
||||||
|
|
||||||
/// Draw a pixmap in position indicated by the `pos`.
|
|
||||||
/// The pixmap will be taken from cache if the cache is valid, otherwise, update_fn will be called
|
|
||||||
/// with a callback that need to be called once with `fn (width, height, data)` where data are the
|
|
||||||
/// argb premultiplied pixel values
|
|
||||||
fn draw_cached_pixmap(
|
|
||||||
&mut self,
|
|
||||||
item_cache: &CachedRenderingData,
|
|
||||||
pos: Point,
|
|
||||||
update_fn: &dyn Fn(&mut dyn FnMut(u32, u32, &[u8])),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Alias for `&mut dyn ItemRenderer`. Required so cbingen generates the ItemVTable
|
/// Alias for `&mut dyn ItemRenderer`. Required so cbingen generates the ItemVTable
|
||||||
/// despite the presence of trait object
|
/// despite the presence of trait object
|
||||||
type ItemRendererRef<'a> = &'a mut dyn ItemRenderer;
|
type ItemRendererRef<'a> = &'a mut dyn crate::item_rendering::ItemRenderer;
|
||||||
|
|
||||||
/// Items are the nodes in the render tree.
|
/// Items are the nodes in the render tree.
|
||||||
#[vtable]
|
#[vtable]
|
||||||
|
@ -207,7 +179,7 @@ impl Item for Rectangle {
|
||||||
|
|
||||||
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
||||||
|
|
||||||
fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) {
|
fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) {
|
||||||
(*backend).draw_rectangle(pos, self)
|
(*backend).draw_rectangle(pos, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,7 +244,7 @@ impl Item for BorderRectangle {
|
||||||
|
|
||||||
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
||||||
|
|
||||||
fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) {
|
fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) {
|
||||||
(*backend).draw_border_rectangle(pos, self)
|
(*backend).draw_border_rectangle(pos, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -384,7 +356,7 @@ impl Item for TouchArea {
|
||||||
|
|
||||||
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
||||||
|
|
||||||
fn render(self: Pin<&Self>, _pos: Point, _backend: &mut &mut dyn ItemRenderer) {}
|
fn render(self: Pin<&Self>, _pos: Point, _backend: &mut ItemRendererRef) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemConsts for TouchArea {
|
impl ItemConsts for TouchArea {
|
||||||
|
@ -443,7 +415,7 @@ impl Item for Clip {
|
||||||
|
|
||||||
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
||||||
|
|
||||||
fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) {
|
fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) {
|
||||||
(*backend).combine_clip(pos, &self)
|
(*backend).combine_clip(pos, &self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -506,7 +478,7 @@ impl Item for Path {
|
||||||
|
|
||||||
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
||||||
|
|
||||||
fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) {
|
fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) {
|
||||||
(*backend).draw_path(pos, self)
|
(*backend).draw_path(pos, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -580,7 +552,7 @@ impl Item for Flickable {
|
||||||
|
|
||||||
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
||||||
|
|
||||||
fn render(self: Pin<&Self>, _pos: Point, _backend: &mut &mut dyn ItemRenderer) {}
|
fn render(self: Pin<&Self>, _pos: Point, _backend: &mut ItemRendererRef) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemConsts for Flickable {
|
impl ItemConsts for Flickable {
|
||||||
|
@ -686,7 +658,7 @@ impl Item for Window {
|
||||||
|
|
||||||
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {}
|
||||||
|
|
||||||
fn render(self: Pin<&Self>, _pos: Point, _backend: &mut &mut dyn ItemRenderer) {}
|
fn render(self: Pin<&Self>, _pos: Point, _backend: &mut ItemRendererRef) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemConsts for Window {
|
impl ItemConsts for Window {
|
||||||
|
|
|
@ -19,11 +19,12 @@ When adding an item or a property, it needs to be kept in sync with different pl
|
||||||
- For the C++ code (new item only): the cbindgen.rs to export the new item, and the `using` declaration in sixtyfps.h
|
- For the C++ code (new item only): the cbindgen.rs to export the new item, and the `using` declaration in sixtyfps.h
|
||||||
- Don't forget to update the documentation
|
- Don't forget to update the documentation
|
||||||
*/
|
*/
|
||||||
use super::{Item, ItemConsts, ItemRc, ItemRenderer};
|
use super::{Item, ItemConsts, ItemRc};
|
||||||
use crate::eventloop::ComponentWindow;
|
use crate::eventloop::ComponentWindow;
|
||||||
use crate::graphics::{Point, Rect, Resource};
|
use crate::graphics::{Point, Rect, Resource};
|
||||||
use crate::input::{FocusEvent, InputEventResult, KeyEvent, KeyEventResult, MouseEvent};
|
use crate::input::{FocusEvent, InputEventResult, KeyEvent, KeyEventResult, MouseEvent};
|
||||||
use crate::item_rendering::CachedRenderingData;
|
use crate::item_rendering::CachedRenderingData;
|
||||||
|
use crate::item_rendering::ItemRenderer;
|
||||||
use crate::layout::LayoutInfo;
|
use crate::layout::LayoutInfo;
|
||||||
#[cfg(feature = "rtti")]
|
#[cfg(feature = "rtti")]
|
||||||
use crate::rtti::*;
|
use crate::rtti::*;
|
||||||
|
|
|
@ -20,7 +20,7 @@ When adding an item or a property, it needs to be kept in sync with different pl
|
||||||
- Don't forget to update the documentation
|
- Don't forget to update the documentation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use super::{Item, ItemConsts, ItemRc, ItemRenderer};
|
use super::{Item, ItemConsts, ItemRc};
|
||||||
use crate::eventloop::ComponentWindow;
|
use crate::eventloop::ComponentWindow;
|
||||||
use crate::font::HasFont;
|
use crate::font::HasFont;
|
||||||
use crate::graphics::{Color, Point, Rect};
|
use crate::graphics::{Color, Point, Rect};
|
||||||
|
@ -28,7 +28,7 @@ use crate::input::{
|
||||||
FocusEvent, InputEventResult, KeyEvent, KeyEventResult, KeyboardModifiers, MouseEvent,
|
FocusEvent, InputEventResult, KeyEvent, KeyEventResult, KeyboardModifiers, MouseEvent,
|
||||||
MouseEventType,
|
MouseEventType,
|
||||||
};
|
};
|
||||||
use crate::item_rendering::CachedRenderingData;
|
use crate::item_rendering::{CachedRenderingData, ItemRenderer};
|
||||||
use crate::layout::LayoutInfo;
|
use crate::layout::LayoutInfo;
|
||||||
#[cfg(feature = "rtti")]
|
#[cfg(feature = "rtti")]
|
||||||
use crate::rtti::*;
|
use crate::rtti::*;
|
||||||
|
|
|
@ -10,12 +10,12 @@ LICENSE END */
|
||||||
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
use sixtyfps_corelib::eventloop::ComponentWindow;
|
||||||
use sixtyfps_corelib::graphics::{
|
use sixtyfps_corelib::graphics::{
|
||||||
Color, GraphicsBackend, GraphicsWindow, Point, Rect, RenderingCache, Resource,
|
Color, GraphicsBackend, GraphicsWindow, Point, Rect, RenderingCache, Resource,
|
||||||
};
|
};
|
||||||
use sixtyfps_corelib::item_rendering::CachedRenderingData;
|
use sixtyfps_corelib::item_rendering::{CachedRenderingData, ItemRenderer};
|
||||||
use sixtyfps_corelib::items::Item;
|
use sixtyfps_corelib::items::Item;
|
||||||
use sixtyfps_corelib::{eventloop::ComponentWindow, items::ItemRenderer};
|
|
||||||
use sixtyfps_corelib::{Property, SharedVector};
|
use sixtyfps_corelib::{Property, SharedVector};
|
||||||
|
|
||||||
type CanvasRc = Rc<RefCell<femtovg::Canvas<femtovg::renderer::OpenGl>>>;
|
type CanvasRc = Rc<RefCell<femtovg::Canvas<femtovg::renderer::OpenGl>>>;
|
||||||
|
|
|
@ -32,8 +32,8 @@ use sixtyfps_corelib::graphics::{Point, Rect};
|
||||||
use sixtyfps_corelib::input::{
|
use sixtyfps_corelib::input::{
|
||||||
FocusEvent, InputEventResult, KeyEvent, KeyEventResult, MouseEvent, MouseEventType,
|
FocusEvent, InputEventResult, KeyEvent, KeyEventResult, MouseEvent, MouseEventType,
|
||||||
};
|
};
|
||||||
use sixtyfps_corelib::item_rendering::CachedRenderingData;
|
use sixtyfps_corelib::item_rendering::{CachedRenderingData, ItemRenderer};
|
||||||
use sixtyfps_corelib::items::{Item, ItemConsts, ItemRc, ItemRenderer, ItemVTable};
|
use sixtyfps_corelib::items::{Item, ItemConsts, ItemRc, ItemVTable};
|
||||||
use sixtyfps_corelib::layout::LayoutInfo;
|
use sixtyfps_corelib::layout::LayoutInfo;
|
||||||
use sixtyfps_corelib::rtti::*;
|
use sixtyfps_corelib::rtti::*;
|
||||||
use sixtyfps_corelib::{Callback, ItemVTable_static, Property, SharedString, SharedVector};
|
use sixtyfps_corelib::{Callback, ItemVTable_static, Property, SharedString, SharedVector};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue