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
 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
// gui/button.rs -- Aldaron's Device Interface / Screen
// Copyright (c) 2017-2018  Jeron A. Lau <jeron.lau@plopgrizzly.com>
// Licensed under the MIT LICENSE

use SpriteList;

use Input;
use Window;
use Sprite;
use Transform;
use Model;
use TexCoords;

/// A GUI Button Sprite.
pub struct Button {
	sprite: Sprite,
	xmin: f32,
	xmax: f32,
	ymin: f32,
	ymax: f32,
	held: bool,
}

impl Button {
	/// Add a new button to the screen.
	pub fn new(window: &mut Window, pos: (f32,f32)) -> Button {
		let model = Model::new(window,
			(&[0, 1, 2, 1, 0, 3],
			&[
				0.0,  0.0, 0.0, 1.0,
				1.0,  1.0, 0.0, 1.0,
				1.0,  0.0, 0.0, 1.0,
				0.0,  1.0, 0.0, 1.0,
			])
		);

		let tc = TexCoords::new(window, &[
			0.0, 0.0, 1.0, 1.0,
			1.0 / 3.0, 1.0, 1.0, 1.0,
			1.0 / 3.0, 0.0, 1.0, 1.0,
			0.0, 1.0, 1.0, 1.0,
		]);

//		let style = Shape::Texture(&v, 0/*include_bytes!("res/button.ppm")*/,&tc);
//		let spr = Sprite::create(window, style, 1);

		let button = window.button;
		let spr = SpriteList::new(model).texture(window, button, tc)
			.only();
		let size = window.unit_size();

		let button = Button {
			sprite: spr,
			xmax: pos.0 + size.0, xmin: pos.0,
			ymax: pos.1 + size.1, ymin: pos.1,
			held: false,
		};

		button
	}

	/// Update appearance of button, depending on state of mouse, and return
	/// `true` when the button is pressed.
	pub fn update(&mut self, window: &mut Window, input: Input) -> bool {
		match input {
			Input::Cursor(xy) => {
				if let Some((x, y)) = xy {
					self.button_check(window, x, y);
				} else {
					self.held = false;
					self.away(window);
				}
			},
			Input::Resize => {
				let pos = (self.xmin, self.ymin);
				let size = window.unit_size();

				self.xmax = pos.0 + size.0;
				self.xmin = pos.0;
				self.ymax = pos.1 + size.1;
				self.ymin = pos.1;
				self.held = false;
				self.resize(window);
				self.away(window);
			},
			Input::CursorPress(_, (x, y)) => {
				self.held = true;
				self.button_check(window, x, y);
			},
			Input::CursorRelease(_, xy) => if self.held {
				if let Some((x, y)) = xy {
					self.held = false;
					return self.button_check(window, x, y);
				}
			},
			_ => {},
		}
		false
	}

	// Private Functions

	fn resize(&mut self, window: &mut Window) {
		Transform::new().auto(window, (self.xmin, self.ymin))
			.apply(window, &mut self.sprite);
	}

	fn modify(&mut self, _window: &mut Window, num: f32) {
		let xmin = num / 3.0;
		let xmax = (num + 1.0) / 3.0;
		let _v = [
			 0.0,  0.0, 0.0, 1.0,	xmin, 0.0, 1.0, 1.0,
			 1.0,  1.0, 0.0, 1.0,	xmax, 1.0, 1.0, 1.0,
			 1.0,  0.0, 0.0, 1.0,	xmax, 0.0, 1.0, 1.0,

			 1.0,  1.0, 0.0, 1.0,	xmax, 1.0, 1.0, 1.0,
			 0.0,  0.0, 0.0, 1.0,	xmin, 0.0, 1.0, 1.0,
			 0.0,  1.0, 0.0, 1.0,	xmin, 1.0, 1.0, 1.0,
		];
// TODO
//		self.sprite.vertices(window, &v);
	}

	fn away(&mut self, window: &mut Window) {
		self.modify(window, 0.0);
	}

	fn over(&mut self, window: &mut Window) {
		self.modify(window, 1.0);
	}

	fn down(&mut self, window: &mut Window) {
		self.modify(window, 2.0);
	}

	fn button_check(&mut self, window: &mut Window, x: f32, y: f32) -> bool{
		let (xmax, xmin, ymax, ymin, held) = {
			(self.xmax, self.xmin, self.ymax, self.ymin, self.held)
		};
		if x >= xmin && x <= xmax && y >= ymin && y <= ymax {
			if held {
				self.down(window);
			}else{
				self.over(window);
			}
			true
		} else {
			self.held = false;
			self.away(window);
			false
		}
	}
}