mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-26 00:38:44 -06:00
Compare commits
No commits in common. "15d1184451f8087f3670b3124cabe358e0d3173d" and "6b33e9cdc987bcddea2ecfe7df960b0d34316723" have entirely different histories.
15d1184451
...
6b33e9cdc9
|
@ -1,111 +0,0 @@
|
|||
use std::time::Instant;
|
||||
use glam::{UVec2, vec4};
|
||||
use glium::{backend::glutin::SimpleWindowBuilder, Surface};
|
||||
use winit::{
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{EventLoopBuilder, ControlFlow}
|
||||
};
|
||||
use kubi_ui::{
|
||||
KubiUi,
|
||||
element::{
|
||||
progress_bar::ProgressBar,
|
||||
container::{Container, Sides, Alignment},
|
||||
text::Text
|
||||
},
|
||||
UiSize,
|
||||
elements,
|
||||
};
|
||||
use kubi_ui_glium::GliumUiRenderer;
|
||||
|
||||
fn main() {
|
||||
kubi_logging::init();
|
||||
|
||||
let event_loop = EventLoopBuilder::new().build().unwrap();
|
||||
let (window, display) = SimpleWindowBuilder::new().build(&event_loop);
|
||||
window.set_title("Mom Downloader 2000");
|
||||
|
||||
let mut kui = KubiUi::new();
|
||||
let mut backend = GliumUiRenderer::new(&display);
|
||||
|
||||
let font_handle = kui.add_font_from_bytes(include_bytes!("../../assets/fonts/roboto/Roboto-Regular.ttf"));
|
||||
|
||||
let instant = Instant::now();
|
||||
|
||||
event_loop.run(|event, window_target| {
|
||||
window_target.set_control_flow(ControlFlow::Poll);
|
||||
match event {
|
||||
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
||||
window_target.exit();
|
||||
},
|
||||
Event::AboutToWait => {
|
||||
let mut frame = display.draw();
|
||||
frame.clear_color_srgb(0., 0., 0., 1.);
|
||||
|
||||
let resolution = UVec2::from(display.get_framebuffer_dimensions()).as_vec2();
|
||||
|
||||
kui.begin();
|
||||
|
||||
let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5);
|
||||
|
||||
kui.add(Container {
|
||||
align: (Alignment::Center, Alignment::Center),
|
||||
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)),
|
||||
background: Some(vec4(0.1, 0.1, 0.1, 1.)),
|
||||
elements: vec![Box::new(Container {
|
||||
gap: 5.,
|
||||
padding: Sides::all(10.),
|
||||
align: (Alignment::Begin, Alignment::Begin),
|
||||
size: (UiSize::Pixels(450.), UiSize::Auto),
|
||||
background: Some(vec4(0.2, 0.2, 0.5, 1.)),
|
||||
elements: elements(|el| {
|
||||
if instant.elapsed().as_secs_f32() < 5. {
|
||||
el.add(Text {
|
||||
text: "Downloading your mom...".into(),
|
||||
font: font_handle,
|
||||
text_size: 32,
|
||||
..Default::default()
|
||||
});
|
||||
el.add(ProgressBar {
|
||||
value: mom_ratio,
|
||||
..Default::default()
|
||||
});
|
||||
el.add(Text {
|
||||
text: format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.).into(),
|
||||
font: font_handle,
|
||||
text_size: 24,
|
||||
..Default::default()
|
||||
});
|
||||
} else if instant.elapsed().as_secs() < 10 {
|
||||
el.add(Text {
|
||||
text: "Error 413 Request Entity Too Large".into(),
|
||||
font: font_handle,
|
||||
color: vec4(1., 0.125, 0.125, 1.),
|
||||
text_size: 26,
|
||||
..Default::default()
|
||||
});
|
||||
el.add(Text {
|
||||
text: format!("Exiting in {}...", 10 - instant.elapsed().as_secs()).into(),
|
||||
font: font_handle,
|
||||
text_size: 24,
|
||||
..Default::default()
|
||||
})
|
||||
} else {
|
||||
window_target.exit();
|
||||
}
|
||||
}),
|
||||
..Default::default()
|
||||
})],
|
||||
..Default::default()
|
||||
}, resolution);
|
||||
|
||||
kui.end();
|
||||
|
||||
backend.update(&kui);
|
||||
backend.draw(&mut frame, resolution);
|
||||
|
||||
frame.finish().unwrap();
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}).unwrap();
|
||||
}
|
72
kubi-ui-examples/examples/text.rs
Normal file
72
kubi-ui-examples/examples/text.rs
Normal file
|
@ -0,0 +1,72 @@
|
|||
use std::time::Instant;
|
||||
use glam::{UVec2, vec4};
|
||||
use glium::{backend::glutin::SimpleWindowBuilder, Surface};
|
||||
use winit::{
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{EventLoopBuilder, ControlFlow}
|
||||
};
|
||||
use kubi_ui::{
|
||||
KubiUi,
|
||||
element::{
|
||||
UiElement,
|
||||
progress_bar::ProgressBar,
|
||||
container::{Container, Sides, Alignment},
|
||||
rect::Rect, text::Text
|
||||
},
|
||||
interaction::IntoInteractable,
|
||||
UiSize,
|
||||
UiDirection, IfModified,
|
||||
};
|
||||
use kubi_ui_glium::GliumUiRenderer;
|
||||
|
||||
fn main() {
|
||||
kubi_logging::init();
|
||||
|
||||
let event_loop = EventLoopBuilder::new().build().unwrap();
|
||||
let (window, display) = SimpleWindowBuilder::new().build(&event_loop);
|
||||
|
||||
let mut kui = KubiUi::new();
|
||||
let mut backend = GliumUiRenderer::new(&display);
|
||||
|
||||
let font_handle = kui.add_font_from_bytes(include_bytes!("../../assets/fonts/roboto/Roboto-Regular.ttf"));
|
||||
|
||||
event_loop.run(|event, window_target| {
|
||||
window_target.set_control_flow(ControlFlow::Poll);
|
||||
match event {
|
||||
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
||||
window_target.exit();
|
||||
},
|
||||
Event::AboutToWait => {
|
||||
let mut frame = display.draw();
|
||||
frame.clear_color_srgb(0., 0., 0., 1.);
|
||||
|
||||
let resolution = UVec2::from(display.get_framebuffer_dimensions()).as_vec2();
|
||||
|
||||
kui.begin();
|
||||
|
||||
kui.add(Container {
|
||||
gap: 5.,
|
||||
padding: Sides::all(5.),
|
||||
align: (Alignment::Begin, Alignment::Begin),
|
||||
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)),
|
||||
elements: vec![
|
||||
Box::new(Text {
|
||||
text: "Hello_world".into(),
|
||||
font: font_handle,
|
||||
..Default::default()
|
||||
}),
|
||||
],
|
||||
..Default::default()
|
||||
}, resolution);
|
||||
|
||||
kui.end();
|
||||
|
||||
backend.update(&kui);
|
||||
backend.draw(&mut frame, resolution);
|
||||
|
||||
frame.finish().unwrap();
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}).unwrap();
|
||||
}
|
|
@ -1,116 +0,0 @@
|
|||
use std::{time::Instant, vec};
|
||||
use glam::{UVec2, vec4};
|
||||
use glium::{backend::glutin::SimpleWindowBuilder, Surface};
|
||||
use winit::{
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{EventLoopBuilder, ControlFlow}
|
||||
};
|
||||
use kubi_ui::{
|
||||
KubiUi,
|
||||
element::{
|
||||
progress_bar::ProgressBar,
|
||||
container::{Container, Sides, Alignment},
|
||||
text::Text, rect::Rect, spacer::Spacer
|
||||
},
|
||||
UiSize,
|
||||
elements,
|
||||
};
|
||||
use kubi_ui_glium::GliumUiRenderer;
|
||||
|
||||
fn main() {
|
||||
kubi_logging::init();
|
||||
|
||||
let event_loop = EventLoopBuilder::new().build().unwrap();
|
||||
let (window, display) = SimpleWindowBuilder::new().build(&event_loop);
|
||||
window.set_title("Text rendering test");
|
||||
|
||||
let mut kui = KubiUi::new();
|
||||
let mut backend = GliumUiRenderer::new(&display);
|
||||
|
||||
let font_handle = kui.add_font_from_bytes(include_bytes!("../../assets/fonts/roboto/Roboto-Regular.ttf"));
|
||||
let instant = Instant::now();
|
||||
|
||||
event_loop.run(|event, window_target| {
|
||||
window_target.set_control_flow(ControlFlow::Poll);
|
||||
match event {
|
||||
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
||||
window_target.exit();
|
||||
},
|
||||
Event::AboutToWait => {
|
||||
let mut frame = display.draw();
|
||||
frame.clear_color_srgb(0., 0., 0., 1.);
|
||||
|
||||
let resolution = UVec2::from(display.get_framebuffer_dimensions()).as_vec2();
|
||||
|
||||
kui.begin();
|
||||
|
||||
kui.add(Container {
|
||||
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)),
|
||||
background: Some(vec4(0.1, 0.1, 0.1, 1.)),
|
||||
elements: elements(|elem| {
|
||||
elem.add(Text {
|
||||
text: "THIS LINE SHOULD BE SHARP!".into(),
|
||||
..Default::default()
|
||||
});
|
||||
elem.add(Text {
|
||||
text: "THIS LINE SHOULD BE SHARP!".into(),
|
||||
text_size: 32,
|
||||
..Default::default()
|
||||
});
|
||||
elem.add(Text {
|
||||
text: "All lines except 3 and 6 below will be blurry:".into(),
|
||||
..Default::default()
|
||||
});
|
||||
for size in [9, 12, 16, 18, 24, 32] {
|
||||
elem.add(Text {
|
||||
text: "Testing default font, Proggy Tiny".into(),
|
||||
text_size: size,
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
elem.add(Rect {
|
||||
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)),
|
||||
color: Some(vec4(0., 0., 1., 1.)),
|
||||
});
|
||||
elem.add(Rect {
|
||||
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)),
|
||||
color: Some(vec4(1., 1., 0., 1.)),
|
||||
});
|
||||
elem.add(Text {
|
||||
text: "Hello, world!\nżółty liść. życie nie ma sensu i wszyscy zginemy;\nтест кирилиці їїїїїїїїїїї\njapanese text: テスト".into(),
|
||||
font: font_handle,
|
||||
text_size: 32,
|
||||
..Default::default()
|
||||
});
|
||||
if instant.elapsed().as_secs() & 1 != 0 {
|
||||
elem.add(Rect {
|
||||
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)),
|
||||
color: Some(vec4(1., 0., 0., 1.)),
|
||||
});
|
||||
elem.add(Rect {
|
||||
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)),
|
||||
color: Some(vec4(0., 0., 0., 1.)),
|
||||
});
|
||||
elem.add(Spacer(100.));
|
||||
elem.add(Text {
|
||||
text: "FLAG SHOULD NOT OVERLAP WITH TEXT".into(),
|
||||
text_size: 64,
|
||||
color: vec4(1., 0., 1., 1.),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
}),
|
||||
..Default::default()
|
||||
}, resolution);
|
||||
|
||||
kui.end();
|
||||
|
||||
backend.update(&kui);
|
||||
backend.draw(&mut frame, resolution);
|
||||
|
||||
frame.finish().unwrap();
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}).unwrap();
|
||||
}
|
|
@ -6,7 +6,16 @@ precision highp sampler2D;
|
|||
out vec4 out_color;
|
||||
in vec4 vtx_color;
|
||||
in vec2 vtx_uv;
|
||||
uniform bool use_tex;
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
out_color = vtx_color;
|
||||
//if (vtx_color.w <= 0.) discard;
|
||||
vec4 tex_color;
|
||||
if (use_tex) {
|
||||
tex_color = texture(tex, vtx_uv);
|
||||
} else {
|
||||
tex_color = vec4(1.);
|
||||
}
|
||||
out_color = tex_color * vtx_color;
|
||||
}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
#version 300 es
|
||||
|
||||
precision highp float;
|
||||
precision highp sampler2D;
|
||||
|
||||
out vec4 out_color;
|
||||
in vec4 vtx_color;
|
||||
in vec2 vtx_uv;
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
out_color = texture(tex, vtx_uv) * vtx_color;
|
||||
}
|
|
@ -7,7 +7,7 @@ use glium::{
|
|||
texture::{SrgbTexture2d, RawImage2d},
|
||||
index::PrimitiveType,
|
||||
implement_vertex,
|
||||
uniform, uniforms::{Sampler, SamplerBehavior, SamplerWrapFunction},
|
||||
uniform, uniforms::DynamicUniforms,
|
||||
};
|
||||
use kubi_ui::{
|
||||
KubiUi,
|
||||
|
@ -17,7 +17,6 @@ use kubi_ui::{
|
|||
|
||||
const VERTEX_SHADER: &str = include_str!("../shaders/vertex.vert");
|
||||
const FRAGMENT_SHADER: &str = include_str!("../shaders/fragment.frag");
|
||||
const FRAGMENT_SHADER_TEX: &str = include_str!("../shaders/fragment_tex.frag");
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(C)]
|
||||
|
@ -115,7 +114,6 @@ struct GlDrawCall {
|
|||
pub struct GliumUiRenderer {
|
||||
context: Rc<Context>,
|
||||
program: glium::Program,
|
||||
program_tex: glium::Program,
|
||||
font_texture: Option<Rc<SrgbTexture2d>>,
|
||||
plan: Vec<GlDrawCall>,
|
||||
}
|
||||
|
@ -125,7 +123,6 @@ impl GliumUiRenderer {
|
|||
log::info!("init glium backend for kui");
|
||||
Self {
|
||||
program: Program::from_source(facade, VERTEX_SHADER, FRAGMENT_SHADER, None).unwrap(),
|
||||
program_tex: Program::from_source(facade, VERTEX_SHADER, FRAGMENT_SHADER_TEX, None).unwrap(),
|
||||
context: Rc::clone(facade.get_context()),
|
||||
font_texture: None,
|
||||
plan: vec![]
|
||||
|
@ -203,13 +200,11 @@ impl GliumUiRenderer {
|
|||
frame.draw(
|
||||
vtx_buffer,
|
||||
idx_buffer,
|
||||
&self.program_tex,
|
||||
&self.program,
|
||||
&uniform! {
|
||||
resolution: resolution.to_array(),
|
||||
tex: Sampler(bind_texture.as_ref(), SamplerBehavior {
|
||||
wrap_function: (SamplerWrapFunction::Clamp, SamplerWrapFunction::Clamp, SamplerWrapFunction::Clamp),
|
||||
..Default::default()
|
||||
}),
|
||||
tex: bind_texture.as_ref(),
|
||||
use_tex: true,
|
||||
},
|
||||
¶ms,
|
||||
).unwrap();
|
||||
|
@ -220,6 +215,7 @@ impl GliumUiRenderer {
|
|||
&self.program,
|
||||
&uniform! {
|
||||
resolution: resolution.to_array(),
|
||||
use_tex: false,
|
||||
},
|
||||
¶ms,
|
||||
).unwrap();
|
||||
|
|
|
@ -160,15 +160,12 @@ impl UiDrawPlan {
|
|||
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
|
||||
layout.append(
|
||||
&[tr.internal_font(*font)],
|
||||
&TextStyle::new(text, *size as f32, 0)
|
||||
&TextStyle::new(&text, *size as f32, 0)
|
||||
);
|
||||
let glyphs = layout.glyphs();
|
||||
|
||||
//let mut rpos_x = 0.;
|
||||
for layout_glyph in glyphs {
|
||||
if !layout_glyph.char_data.rasterize() {
|
||||
continue
|
||||
}
|
||||
let vidx = swapper.current().vertices.len() as u32;
|
||||
let glyph = tr.glyph(*font, layout_glyph.parent, layout_glyph.key.px as u8);
|
||||
//rpos_x += glyph.metrics.advance_width;//glyph.metrics.advance_width;
|
||||
|
|
|
@ -88,19 +88,10 @@ impl Default for Container {
|
|||
|
||||
impl Container {
|
||||
pub fn measure_max_inner_size(&self, layout: &LayoutInfo) -> Vec2 {
|
||||
let outer_size_x = match self.size.0 {
|
||||
UiSize::Auto => layout.max_size.x,
|
||||
UiSize::Percentage(p) => layout.max_size.x * p,
|
||||
UiSize::Pixels(p) => p,
|
||||
};
|
||||
let outer_size_y = match self.size.1 {
|
||||
UiSize::Auto => layout.max_size.y,
|
||||
UiSize::Percentage(p) => layout.max_size.y * p,
|
||||
UiSize::Pixels(p) => p,
|
||||
};
|
||||
vec2(
|
||||
outer_size_x - (self.padding.left + self.padding.right),
|
||||
outer_size_y - (self.padding.top + self.padding.bottom),
|
||||
//TODO take explicit size into account
|
||||
layout.max_size - vec2(
|
||||
self.padding.left + self.padding.right,
|
||||
self.padding.top + self.padding.bottom,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ use crate::{
|
|||
draw::{UiDrawCommand, UiDrawCommands}
|
||||
};
|
||||
|
||||
pub struct Spacer(pub f32);
|
||||
pub struct Spacer(f32);
|
||||
|
||||
impl Default for Spacer {
|
||||
fn default() -> Self {
|
||||
|
|
|
@ -13,18 +13,16 @@ pub struct Text {
|
|||
pub text: Cow<'static, str>,
|
||||
pub size: (UiSize, UiSize),
|
||||
pub color: Vec4,
|
||||
pub font: FontHandle,
|
||||
pub text_size: u8,
|
||||
pub font: FontHandle
|
||||
}
|
||||
|
||||
impl Default for Text {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
text: "".into(),
|
||||
size: (UiSize::Auto, UiSize::Auto),
|
||||
size: (UiSize::Percentage(1.), UiSize::Pixels(32.)),
|
||||
color: Vec4::new(1., 1., 1., 1.),
|
||||
font: FontHandle(0),
|
||||
text_size: 16,
|
||||
font: FontHandle(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +37,7 @@ impl UiElement for Text {
|
|||
UiSize::Pixels(pixels) => pixels,
|
||||
},
|
||||
match self.size.1 {
|
||||
UiSize::Auto => self.text_size as f32,
|
||||
UiSize::Auto => layout.max_size.y,
|
||||
UiSize::Percentage(percentage) => layout.max_size.y * percentage,
|
||||
UiSize::Pixels(pixels) => pixels,
|
||||
},
|
||||
|
@ -53,7 +51,7 @@ impl UiElement for Text {
|
|||
draw.add(UiDrawCommand::Text {
|
||||
text: self.text.clone(),
|
||||
position: layout.position,
|
||||
size: self.text_size,
|
||||
size: 32,
|
||||
color: self.color,
|
||||
font: self.font
|
||||
});
|
||||
|
|
|
@ -114,16 +114,3 @@ pub struct LayoutInfo {
|
|||
pub max_size: Vec2,
|
||||
pub direction: UiDirection,
|
||||
}
|
||||
|
||||
pub struct ElementList(Vec<Box<dyn UiElement>>);
|
||||
|
||||
impl ElementList {
|
||||
pub fn add(&mut self, element: impl UiElement + 'static) {
|
||||
self.0.push(Box::new(element));
|
||||
}
|
||||
}
|
||||
pub fn elements(f: impl FnOnce(&mut ElementList)) -> Vec<Box<dyn UiElement>> {
|
||||
let mut elements = ElementList(Vec::new());
|
||||
f(&mut elements);
|
||||
elements.0
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue