Create iced_core and iced_native

This commit is contained in:
Héctor Ramón Jiménez 2019-09-20 19:15:31 +02:00
parent b83a4b42dd
commit b9e0f74948
81 changed files with 2576 additions and 2709 deletions

View file

@ -1,20 +1,15 @@
use super::{into_color, Renderer};
use ggez::graphics::{self, mint, Align, Scale, Text, TextFragment};
use iced::text;
use iced_native::{text, Layout, Node, Style};
use std::cell::RefCell;
use std::f32;
impl text::Renderer for Renderer<'_> {
fn node(
&self,
style: iced::Style,
content: &str,
size: Option<u16>,
) -> iced::Node {
fn node(&self, text: &iced_native::Text) -> Node {
let font = self.font;
let font_cache = graphics::font_cache(self.context);
let content = String::from(content);
let content = String::from(&text.content);
// TODO: Investigate why stretch tries to measure this MANY times
// with every ancestor's bounds.
@ -23,20 +18,22 @@ impl text::Renderer for Renderer<'_> {
// I noticed that the first measure is the one that matters in
// practice. Here, we use a RefCell to store the cached measurement.
let measure = RefCell::new(None);
let size = size.map(f32::from).unwrap_or(self.font_size);
let size = text.size.map(f32::from).unwrap_or(self.font_size);
iced::Node::with_measure(style, move |bounds| {
let style = Style::default().width(text.width);
iced_native::Node::with_measure(style, move |bounds| {
let mut measure = measure.borrow_mut();
if measure.is_none() {
let bounds = (
match bounds.width {
iced::Number::Undefined => f32::INFINITY,
iced::Number::Defined(w) => w,
iced_native::Number::Undefined => f32::INFINITY,
iced_native::Number::Defined(w) => w,
},
match bounds.height {
iced::Number::Undefined => f32::INFINITY,
iced::Number::Defined(h) => h,
iced_native::Number::Undefined => f32::INFINITY,
iced_native::Number::Defined(h) => h,
},
);
@ -57,7 +54,7 @@ impl text::Renderer for Renderer<'_> {
let (width, height) = font_cache.dimensions(&text);
let size = iced::Size {
let size = iced_native::Size {
width: width as f32,
height: height as f32,
};
@ -75,30 +72,23 @@ impl text::Renderer for Renderer<'_> {
})
}
fn draw(
&mut self,
bounds: iced::Rectangle,
content: &str,
size: Option<u16>,
color: Option<iced::Color>,
horizontal_alignment: text::HorizontalAlignment,
_vertical_alignment: text::VerticalAlignment,
) {
let size = size.map(f32::from).unwrap_or(self.font_size);
fn draw(&mut self, text: &iced_native::Text, layout: Layout<'_>) {
let size = text.size.map(f32::from).unwrap_or(self.font_size);
let bounds = layout.bounds();
let mut text = Text::new(TextFragment {
text: String::from(content),
let mut ggez_text = Text::new(TextFragment {
text: text.content.clone(),
font: Some(self.font),
scale: Some(Scale { x: size, y: size }),
..Default::default()
});
text.set_bounds(
ggez_text.set_bounds(
mint::Point2 {
x: bounds.width,
y: bounds.height,
},
match horizontal_alignment {
match text.horizontal_alignment {
text::HorizontalAlignment::Left => graphics::Align::Left,
text::HorizontalAlignment::Center => graphics::Align::Center,
text::HorizontalAlignment::Right => graphics::Align::Right,
@ -107,12 +97,14 @@ impl text::Renderer for Renderer<'_> {
graphics::queue_text(
self.context,
&text,
&ggez_text,
mint::Point2 {
x: bounds.x,
y: bounds.y,
},
color.or(Some(iced::Color::BLACK)).map(into_color),
text.color
.or(Some(iced_native::Color::BLACK))
.map(into_color),
);
}
}