pub fn point<N>(x: N, y: N) -> Point<N>
Expand description
A convenience function for generating Point
s.
Examples found in repository?
examples/main.rs (line 116)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
fn main() {
let events_loop = glutin::event_loop::EventLoop::new();
let window = glutin::window::WindowBuilder::new()
.with_title("Rusttyper Example")
.with_inner_size(glutin::dpi::LogicalSize::new(512.0, 512.0));
let context = glutin::ContextBuilder::new()
.with_vsync(true)
.with_multisampling(8);
let display = glium::Display::new(window, context, &events_loop).unwrap();
let mut font = rusttyper::Render::new(&display, 512, (0.1, 0.1));
font.add_fonts(&include_bytes!("../DroidSans.ttf")[..]).unwrap();
let uidraw = rusttyper::simple2d::Simple2d::create(&display).unwrap();
let mut count = 0usize;
let mut mouse = Point { x: 0.0, y: 0.0 };
events_loop.run(move |event, _window, flow| {
let mut stop = false;
let mut draw = false;
use glutin::event::{Event, WindowEvent, KeyboardInput, VirtualKeyCode};
match event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => stop = true,
Event::WindowEvent { event: WindowEvent::Destroyed, .. } => stop = true,
Event::WindowEvent { event: WindowEvent::KeyboardInput { input: KeyboardInput { virtual_keycode: Some(VirtualKeyCode::Escape), .. }, .. }, .. } => stop = true,
Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, .. } => {
mouse.x = position.x as f32;
mouse.y = position.y as f32;
},
Event::RedrawRequested(_) => draw = true,
Event::NewEvents(_) => {
count += 1;
display.gl_window().window().request_redraw();
return;
},
_ => return,
}
if stop {
*flow = glutin::event_loop::ControlFlow::Exit;
return;
} else {
use std::time::*;
*flow = glutin::event_loop::ControlFlow::WaitUntil(Instant::now() + Duration::from_secs_f64(1.0 / 30.0));
}
if !draw { return; }
let mut buffer = ::rusttyper::RunBuffer::new();
{
let (screen_width, screen_height) = display.get_framebuffer_dimensions();
let (screen_width, screen_height) = (screen_width as f32, screen_height as f32);
let margin = 0.1;
let input_field_width = (screen_width - 2.0 * margin * screen_width) as u32;
let start = ((screen_width * margin) as i32, (screen_height * margin) as i32);
use rusttyper::{Style, TextBlock};
let m = |n| (count * n % 255) as u8;
let colorful = Style {
color: [m(5), m(16), m(32), 255],
.. Style::default()
};
let big = Style {
scale: 32.0,
.. Style::default()
};
let small = Style {
scale: 10.0,
.. Style::default()
};
buffer.push_blocks(TextBlock {
origin: start,
width: input_field_width,
}, vec![
TEXT.into(),
"\n...Did I mention that we have ".into(),
(colorful, "COLORS").into(),
"?".into(),
"\nAnd also ".into(),
(big, "FONT SIZES").into(),
"?".into(),
(small, " (okay that might be too big)").into(),
].into_iter());
/*buffer.push_blocks(TextBlock {
origin: start,
width: input_field_width,
}, vec!["hey_guys".into()].into_iter());*/
{
let query = point(mouse.x - start.0 as f32, mouse.y - start.1 as f32);
let query = buffer.measure_area(&mut font, query);
if let Some(query) = query.2 {
buffer.write((mouse.x as i32, mouse.y as i32), input_field_width, "@");
let area = query.area;
let area = Rect {
min: point((area.min.x + start.0 as f32) as i32, (area.min.y + start.1 as f32) as i32),
max: point((area.max.x + start.0 as f32) as i32, (area.max.y + start.1 as f32) as i32),
};
buffer.write((area.min.x, area.min.y), input_field_width, "@");
buffer.write((area.max.x, area.max.y), input_field_width, "@");
/*{
let bit = buffer.parts.get(query.run_index)
.map(|bit| -> &str { &*bit.text })
.unwrap_or("<WRONG RUN>")
;
let i = query.char_index;
let lo = if i > 5 {
i - 5
} else {
i
};
let hi = if i + 5 < bit.len() {
i + 5
} else {
bit.len()
};
println!("{} {} {:?} {:?}", query.run_index, query.char_index, bit.get(query.char_index..query.char_index+1).unwrap_or("><"), &bit.get(lo..hi).unwrap_or("><"));
}*/
}
}
buffer.write((0, screen_height as i32), input_field_width, format!("{}", count));
}
let mut target = display.draw();
target.clear_color(1.0, 1.0, 1.0, 0.0);
uidraw.draw(&display, &mut target, &mut font, &mut buffer).unwrap();
target.finish().unwrap();
});
}