1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
use super::*;

#[derive(Default)]
pub struct Button {
	offset: Vec2,
	size: Vec2,
	scale: f32,
	text: String,
	easing: f32,
	pub pressed: bool,
	pub hovered: bool,
}
impl Button {
	pub fn draw<'s>(&'s mut self, r: &mut RenderLock<'s>, t: &'s Theme, pos: Vec2, size: Vec2, text: &str) -> bool {
		if self.text != text || self.size != size {
			let (offset, scale) = util::fit_text(text, t, size);

			*self = Self {
				offset,
				size,
				scale,
				text: text.into(),
				..*self
			};
		}

		let Self { easing, pressed, hovered, .. } = self;
		let delta = 1. / (t.easing * (*easing - 2.).abs());
		*easing = (*easing + if *hovered { delta } else { -delta }).clamp(0., 1.);
		let color = t.fg.mix(*easing, t.fg_focus).mix(*pressed, t.highlight);

		*pressed &= *hovered;
		let p = *pressed;
		r.draw_with_logic(
			Rect { pos, size, color },
			move |e, _, _| {
				let mut pass = |s: &Mod| *pressed = s.pressed();
				match e {
					OfferFocus => return DropFocus,
					MouseButton { state, .. } => pass(state),
					Keyboard { key, state } if *key == Key::Space => pass(state),
					_ => (),
				}
				Reject
			},
			0,
		);
		*hovered = r.hovered();
		r.draw(Text {
			pos: pos.sum(self.offset),
			color: t.text.mix(*hovered, t.text_focus).mix(p, t.text_highlight),
			scale: self.scale,
			text,
			font: &t.font,
		});
		p
	}
}