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
use Input;
use Window;
use Sprite;
use Style;
use Transform;
pub struct Button {
sprite: Sprite,
xmin: f32,
xmax: f32,
ymin: f32,
ymax: f32,
held: bool,
}
impl Button {
pub fn create(window: &mut Window, pos: (f32,f32)) -> Button {
let v = [
0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0, 1.0 / 3.0, 1.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0, 1.0 / 3.0, 0.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0, 1.0 / 3.0, 1.0, 1.0, 1.0,
0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0,
];
let style = Style::create().opaque(window,
include_bytes!("res/button.ppm"));
let spr = Sprite::create(window, &v, style, 1);
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
}
pub fn update(&mut self, window: &mut Window, input: Input) -> bool {
match input {
Input::Cursor(x, y) => {
self.button_check(window, x, y);
},
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::LeftDown(x, y) => {
self.held = true;
self.button_check(window, x, y);
},
Input::LeftUp(x, y) => {
if self.held {
self.held = false;
return self.button_check(window, x, y);
}
},
Input::LeaveWindow => {
self.held = false;
self.away(window);
},
_ => {},
}
false
}
fn resize(&mut self, window: &mut Window) {
Transform::create().auto(window, (self.xmin, self.ymin))
.apply(window, &self.sprite, 0);
}
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,
];
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
}
}
}