Skip to content
Snippets Groups Projects
Commit 4dd2c3bc authored by StarArawn's avatar StarArawn
Browse files

Working text measure.

parent ca2621c6
No related branches found
No related tags found
No related merge requests found
Showing
with 163 additions and 106 deletions
......@@ -1361,17 +1361,6 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "279fb028e20b3c4c320317955b77c5e0c9701f05a1d309905d6fc702cdc5053e"
[[package]]
name = "flo_binding"
version = "2.0.2"
source = "git+https://github.com/StarArawn/flo_binding.git?rev=c78431a56df5ec082b7e1c271871e6c0ac75e81e#c78431a56df5ec082b7e1c271871e6c0ac75e81e"
dependencies = [
"desync",
"flo_rope",
"futures",
"uuid",
]
[[package]]
name = "flo_rope"
version = "0.1.0"
......@@ -1869,11 +1858,14 @@ dependencies = [
"as-any",
"bevy",
"derivative",
"flo_binding",
"desync",
"flo_rope",
"futures",
"kayak_font",
"kayak_render_macros",
"morphorm",
"resources",
"uuid",
]
[[package]]
......
......@@ -20,18 +20,19 @@ pub fn extract_texts(
_dpi: f32,
) -> Vec<ExtractQuadBundle> {
let mut extracted_texts = Vec::new();
let (background_color, layout, font_size, content, font) = match render_primitive {
let (background_color, layout, font_size, content, font, parent_size) = match render_primitive {
RenderPrimitive::Text {
color,
layout,
size,
content,
font,
} => (color, layout, *size, content, *font),
parent_size,
} => (color, layout, *size, content, font, parent_size),
_ => panic!(""),
};
let font_handle = font_mapping.get_handle(font).unwrap();
let font_handle = font_mapping.get_handle(font.clone()).unwrap();
let font = fonts.get(font_handle.clone());
if font.is_none() {
......@@ -46,7 +47,7 @@ pub fn extract_texts(
CoordinateSystem::PositiveYDown,
Alignment::Start,
(layout.posx, layout.posy + line_height),
(layout.width, layout.height),
(parent_size.0, parent_size.1),
content,
line_height,
font_size,
......
use bevy::{prelude::Handle, utils::HashMap};
use bevy::{
prelude::{Assets, Handle, Res},
utils::HashMap,
};
use kayak_font::KayakFont;
use crate::BevyContext;
pub struct FontMapping {
count: u16,
font_ids: HashMap<Handle<KayakFont>, u16>,
font_handles: HashMap<u16, Handle<KayakFont>>,
font_ids: HashMap<Handle<KayakFont>, String>,
font_handles: HashMap<String, Handle<KayakFont>>,
new_fonts: Vec<String>,
}
impl Default for FontMapping {
fn default() -> Self {
Self {
count: 0,
font_ids: HashMap::default(),
font_handles: HashMap::default(),
new_fonts: Vec::new(),
}
}
}
impl FontMapping {
pub fn add(&mut self, handle: Handle<KayakFont>) -> u16 {
pub fn add(&mut self, key: impl Into<String>, handle: Handle<KayakFont>) {
let key = key.into();
if !self.font_ids.contains_key(&handle) {
let id = self.count;
self.font_ids.insert(handle.clone(), id);
self.font_handles.insert(id, handle);
self.count += 1;
id
} else {
*self.font_ids.get(&handle).unwrap()
self.font_ids.insert(handle.clone(), key.clone());
self.new_fonts.push(key.clone());
self.font_handles.insert(key, handle);
}
}
pub fn get_handle(&self, id: u16) -> Option<Handle<KayakFont>> {
pub fn get_handle(&self, id: String) -> Option<Handle<KayakFont>> {
self.font_handles
.get(&id)
.and_then(|item| Some(item.clone()))
}
pub fn get(&self, font: &Handle<KayakFont>) -> Option<u16> {
self.font_ids.get(font).and_then(|font_id| Some(*font_id))
pub fn get(&self, font: &Handle<KayakFont>) -> Option<String> {
self.font_ids
.get(font)
.and_then(|font_id| Some(font_id.clone()))
}
pub(crate) fn add_loaded_to_kayak(
&mut self,
fonts: &Res<Assets<KayakFont>>,
context: &BevyContext,
) {
if let Ok(mut kayak_context) = context.kayak_context.write() {
let new_fonts = self.new_fonts.drain(..).collect::<Vec<_>>();
for font_key in new_fonts {
let font_handle = self.font_handles.get(&font_key).unwrap();
if let Some(font) = fonts.get(font_handle) {
kayak_context.set_asset(font_key, font.clone());
} else {
self.new_fonts.push(font_key);
}
}
}
}
}
use bevy::{
prelude::{Plugin, Res, ResMut},
prelude::{Assets, Plugin, Res, ResMut},
render::{
render_asset::RenderAssets,
renderer::{RenderDevice, RenderQueue},
......@@ -7,11 +7,16 @@ use bevy::{
RenderApp, RenderStage,
},
};
use kayak_font::bevy::{FontTextureCache, KayakFontPlugin};
use kayak_font::{
bevy::{FontTextureCache, KayakFontPlugin},
KayakFont,
};
mod extract;
mod font_mapping;
use crate::BevyContext;
use super::pipeline::UnifiedPipeline;
pub use extract::extract_texts;
pub use font_mapping::*;
......@@ -22,13 +27,22 @@ pub struct TextRendererPlugin;
impl Plugin for TextRendererPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_plugin(KayakFontPlugin)
.init_resource::<FontMapping>();
.init_resource::<FontMapping>()
.add_system(process_loaded_fonts);
let render_app = app.sub_app_mut(RenderApp);
render_app.add_system_to_stage(RenderStage::Queue, create_and_update_font_cache_texture);
}
}
fn process_loaded_fonts(
mut font_mapping: ResMut<FontMapping>,
fonts: Res<Assets<KayakFont>>,
context: Res<BevyContext>,
) {
font_mapping.add_loaded_to_kayak(&fonts, &context);
}
fn create_and_update_font_cache_texture(
device: Res<RenderDevice>,
queue: Res<RenderQueue>,
......
......@@ -29,7 +29,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
......
......@@ -20,7 +20,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let handle: Handle<bevy::render::texture::Image> = asset_server.load("kenny/panel_brown.png");
let panel_brown_handle = image_manager.get(&handle);
......@@ -40,13 +40,7 @@ fn startup(
..Style::default()
};
let clip_styles = Style {
// padding_left: StyleProp::Value(Units::Pixels(25.0)),
// padding_right: StyleProp::Value(Units::Pixels(25.0)),
// padding_top: StyleProp::Value(Units::Pixels(15.0)),
// padding_bottom: StyleProp::Value(Units::Pixels(125.0)),
..Style::default()
};
let clip_styles = Style { ..Style::default() };
let lorem_ipsum = r#"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sed tellus neque. Proin tempus ligula a mi molestie aliquam. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam venenatis consequat ultricies. Sed ac orci purus. Nullam velit nisl, dapibus vel mauris id, dignissim elementum sapien. Vestibulum faucibus sapien ut erat bibendum, id lobortis nisi luctus. Mauris feugiat at lectus at pretium. Pellentesque vitae finibus ante. Nulla non ex neque. Cras varius, lorem facilisis consequat blandit, lorem mauris mollis massa, eget consectetur magna sem vel enim. Nam aliquam risus pulvinar, volutpat leo eget, eleifend urna. Suspendisse in magna sed ligula vehicula volutpat non vitae augue. Phasellus aliquam viverra consequat. Nam rhoncus molestie purus, sed laoreet neque imperdiet eget. Sed egestas metus eget sodales congue.
......
......@@ -24,12 +24,8 @@ fn Counter(context: &mut KayakContext) {
};
let button_text_styles = Style {
bottom: StyleProp::Value(Units::Stretch(1.0)),
left: StyleProp::Value(Units::Stretch(1.0)),
right: StyleProp::Value(Units::Stretch(1.0)),
top: StyleProp::Value(Units::Stretch(1.0)),
width: StyleProp::Value(Units::Pixels(67.0)),
height: StyleProp::Value(Units::Pixels(39.0)),
..Default::default()
};
......@@ -58,7 +54,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
......
......@@ -7,11 +7,11 @@ use bevy::{
use kayak_ui::{
bevy::{BevyContext, BevyKayakUIPlugin, FontMapping, UICameraBundle},
core::{
render, rsx,
styles::{Style, StyleProp, Units},
Color, Handler, render, rsx, use_state, widget, EventType,
Index, OnEvent,
use_state, widget, Color, EventType, Handler, Index, OnEvent,
},
widgets::{App, Background, Button, Fold, If, Text, Window}
widgets::{App, Background, Button, Fold, If, Text, Window},
};
#[widget]
......@@ -42,28 +42,27 @@ fn FolderTree(context: &mut KayakContext) {
..Default::default()
};
// === Folder A === //
let fold_a_styles = Some(Style {
background_color: StyleProp::Value(Color::new(0.25882, 0.24314, 0.19608, 1.0)),
background_color: StyleProp::Value(Color::new(0.25882, 0.24314, 0.19608, 1.0)),
..Default::default()
});
let fold_a_child_styles = Style {
background_color: StyleProp::Value(Color::new(0.16863, 0.16863, 0.12549, 1.0)),
background_color: StyleProp::Value(Color::new(0.16863, 0.16863, 0.12549, 1.0)),
..fold_child_base_styles.clone()
};
let fold_a_child_child_styles = Style {
background_color: StyleProp::Value(Color::new(0.12941, 0.12941, 0.09412, 1.0)),
background_color: StyleProp::Value(Color::new(0.12941, 0.12941, 0.09412, 1.0)),
..fold_a_child_styles.clone()
};
// === Folder B === //
let fold_b_styles = Style {
background_color: StyleProp::Value(Color::new(0.19608, 0.25882, 0.21569, 1.0)),
background_color: StyleProp::Value(Color::new(0.19608, 0.25882, 0.21569, 1.0)),
..Default::default()
};
let fold_b_child_styles = Style {
background_color: StyleProp::Value(Color::new(0.11765, 0.16078, 0.12941, 1.0)),
background_color: StyleProp::Value(Color::new(0.11765, 0.16078, 0.12941, 1.0)),
..fold_child_base_styles.clone()
};
......@@ -81,15 +80,15 @@ fn FolderTree(context: &mut KayakContext) {
// === Folder C === //
let fold_c_styles = Some(Style {
background_color: StyleProp::Value(Color::new(0.25882, 0.19608, 0.23529, 1.0)),
background_color: StyleProp::Value(Color::new(0.25882, 0.19608, 0.23529, 1.0)),
..Default::default()
});
let fold_c_child_styles = Style {
background_color: StyleProp::Value(Color::new(0.16863, 0.12549, 0.15294, 1.0)),
background_color: StyleProp::Value(Color::new(0.16863, 0.12549, 0.15294, 1.0)),
..fold_child_base_styles.clone()
};
let try_style = Style {
color: StyleProp::Value(Color::new(1.0, 0.5, 0.5, 1.0)),
color: StyleProp::Value(Color::new(1.0, 0.5, 0.5, 1.0)),
..text_styles.clone()
};
......@@ -149,7 +148,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
......
......@@ -87,9 +87,9 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let main_font = asset_server.load("antiquity.kayak_font");
font_mapping.add(main_font.clone());
font_mapping.add("Antiquity", main_font.clone());
let handle: Handle<bevy::render::texture::Image> = asset_server.load("kenny/panel_brown.png");
let panel_brown_handle = image_manager.get(&handle);
......@@ -99,15 +99,10 @@ fn startup(
layout_type: StyleProp::Value(LayoutType::Column),
width: StyleProp::Value(Units::Pixels(512.0)),
height: StyleProp::Value(Units::Pixels(512.0)),
min_height: StyleProp::Value(Units::Pixels(0.0)),
padding_left: StyleProp::Value(Units::Stretch(1.0)),
padding_right: StyleProp::Value(Units::Stretch(1.0)),
padding_top: StyleProp::Value(Units::Stretch(1.0)),
padding_bottom: StyleProp::Value(Units::Stretch(1.0)),
..Style::default()
};
let app_styles = Style {
left: StyleProp::Value(Units::Stretch(1.0)),
right: StyleProp::Value(Units::Stretch(1.0)),
top: StyleProp::Value(Units::Stretch(1.0)),
bottom: StyleProp::Value(Units::Stretch(1.0)),
padding_left: StyleProp::Value(Units::Stretch(1.0)),
padding_right: StyleProp::Value(Units::Stretch(1.0)),
padding_top: StyleProp::Value(Units::Stretch(1.0)),
......@@ -116,21 +111,21 @@ fn startup(
};
let header_styles = Style {
width: StyleProp::Value(Units::Pixels(408.0)),
height: StyleProp::Value(Units::Pixels(42.0)),
// width: StyleProp::Value(Units::Pixels(408.0)),
// height: StyleProp::Value(Units::Pixels(42.0)),
bottom: StyleProp::Value(Units::Stretch(1.0)),
..Style::default()
};
let text_styles = Style {
width: StyleProp::Value(Units::Pixels(56.0)),
height: StyleProp::Value(Units::Pixels(24.0)),
// width: StyleProp::Value(Units::Pixels(56.0)),
// height: StyleProp::Value(Units::Pixels(24.0)),
..Style::default()
};
let options_button_text_styles = Style {
width: StyleProp::Value(Units::Pixels(94.0)),
height: StyleProp::Value(Units::Pixels(24.0)),
// width: StyleProp::Value(Units::Pixels(94.0)),
// height: StyleProp::Value(Units::Pixels(24.0)),
..Style::default()
};
......@@ -142,7 +137,7 @@ fn startup(
let main_font_id = font_mapping.get(&main_font);
render! {
<App styles={Some(app_styles)}>
<App>
<NinePatch
styles={Some(nine_patch_styles)}
border={Space {
......
......@@ -35,7 +35,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
commands.insert_resource(bind(GlobalCount(0)));
......
......@@ -16,7 +16,7 @@ use bevy::{
};
use kayak_ui::{
bevy::{BevyContext, BevyKayakUIPlugin, FontMapping, UICameraBundle},
core::{EventType, Index, OnEvent, render, rsx, use_effect, use_state, widget},
core::{render, rsx, use_effect, use_state, widget, EventType, Index, OnEvent},
widgets::{App, Button, Text, Window},
};
......@@ -81,26 +81,29 @@ fn EffectCounter() {
// use the `get` method on the raw state binding instead, to get the actual value.
set_effect_count(raw_count.get() * 2);
},
// In order to call this side-effect closure whenever `raw_count` updates, we need to pass it in as a dependency.
// Don't worry about the borrow checker here, `raw_count` is automatically cloned internally, so you don't need
// to do that yourself.
[raw_count]
// IMPORTANT:
// If a side-effect updates some other state, make sure you do not pass that state in as a dependency unless you have
// some checks in place to prevent an infinite loop!
[raw_count] // IMPORTANT:
// If a side-effect updates some other state, make sure you do not pass that state in as a dependency unless you have
// some checks in place to prevent an infinite loop!
);
// Passing an empty dependency array causes the callback to only run a single time: when the widget is first rendered.
use_effect!(|| {
println!("First!");
}, []);
use_effect!(
|| {
println!("First!");
},
[]
);
// Additionally, order matters with these side-effects. They will be ran in the order they are defined.
use_effect!(|| {
println!("Second!");
}, []);
use_effect!(
|| {
println!("Second!");
},
[]
);
rsx! {
<Window position={(50.0, 225.0)} size={(300.0, 150.0)} title={"Effect Example".to_string()}>
......@@ -120,7 +123,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
......@@ -146,4 +149,4 @@ fn main() {
.add_plugin(BevyKayakUIPlugin)
.add_startup_system(startup)
.run();
}
\ No newline at end of file
}
......@@ -64,7 +64,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
......
......@@ -240,8 +240,8 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
<App>
......
......@@ -62,7 +62,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
......
......@@ -17,6 +17,7 @@ pub fn DeleteButton(children: Children, styles: Option<Style>) {
render_command: StyleProp::Value(RenderCommand::Layout),
height: StyleProp::Value(Units::Pixels(32.0)),
width: StyleProp::Value(Units::Pixels(30.0)),
left: StyleProp::Value(Units::Stretch(1.0)),
..base_styles
});
......
......@@ -102,7 +102,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
render! {
......
......@@ -14,7 +14,7 @@ fn startup(
) {
commands.spawn_bundle(UICameraBundle::new());
font_mapping.add(asset_server.load("roboto.kayak_font"));
font_mapping.add("Roboto", asset_server.load("roboto.kayak_font"));
let context = BevyContext::new(|context| {
let data = vec!["Text1", "Text2", "Text3", "Text4"];
......
......@@ -11,10 +11,13 @@ bevy_renderer = ["bevy", "kayak_font/bevy_renderer"]
[dependencies]
as-any = "0.2"
derivative = "2.2"
bevy = { version = "0.6.0", optional = true }
flo_binding = { git = "https://github.com/StarArawn/flo_binding.git", rev = "c78431a56df5ec082b7e1c271871e6c0ac75e81e" }
derivative = "2.2"
desync = { version = "0.7" }
flo_rope = { version = "0.1" }
futures = { version = "0.3" }
kayak_font = { path = "../kayak_font" }
kayak_render_macros = { path = "../kayak_render_macros" }
morphorm = { git = "https://github.com/geom3trik/morphorm", rev = "1243152d4cebea46fd3e5098df26402c73acae91" }
resources = "1.1"
uuid = { version = "0.8", features = ["v4"] }
use std::{collections::HashMap, path::PathBuf};
use crate::{Binding, MutableBound};
pub struct AssetStorage<T> {
assets: HashMap<PathBuf, Binding<Option<T>>>,
}
impl<T: Clone + PartialEq + Send + Sync + 'static> AssetStorage<T> {
pub fn new() -> Self {
Self {
assets: HashMap::new(),
}
}
pub fn get_asset(&mut self, key: impl Into<PathBuf>) -> &Binding<Option<T>> {
let key = key.into();
if self.assets.contains_key(&key) {
self.assets.get(&key).unwrap()
} else {
// Insert new asset if it doesn't exist yet.
self.assets.insert(key.clone(), Binding::new(None));
self.assets.get(&key).unwrap()
}
}
pub fn set_asset(&mut self, key: impl Into<PathBuf>, asset: T) {
let key = key.into();
if self.assets.contains_key(&key) {
let stored_asset = self.assets.get(&key).unwrap();
stored_asset.set(Some(asset));
} else {
self.assets.insert(key, Binding::new(Some(asset)));
}
}
}
use std::time::Instant;
pub use flo_binding::{bind, computed, notify, Binding, Bound, Changeable, ComputedBinding, MutableBound, Releasable};
pub use crate::flo_binding::{
bind, computed, notify, Binding, Bound, Changeable, ComputedBinding, MutableBound, Releasable,
};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Debouncer {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment