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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use sfml::window::{ContextSettings, VideoMode, Close};
use sfml::window::event::Event;
use sfml::graphics::{RenderWindow, RenderTarget, Color, View};
use sfml::system::vector2::{Vector2f, ToVec};
use sfml::window::keyboard::Key as SfmlKey;
use key::{Key, SpecialKey};
use std::mem;
use std::thread;
pub struct EventState {
pub right: bool,
pub left: bool,
pub chars: String,
pub keys: Vec<SpecialKey>,
}
impl EventState {
pub fn new() -> Self {
EventState {
right: false,
left: false,
chars: String::new(),
keys: Vec::new(),
}
}
}
pub struct Window {
window: RenderWindow,
pub event: EventState,
}
impl Window {
pub fn new(title: &str) -> Window {
Window {
window: RenderWindow::new(VideoMode::new_init(800, 600, 32),
title,
Close,
&ContextSettings::default())
.expect("Could not construct window"),
event: EventState::new(),
}
}
pub fn width(&mut self, width: u32) -> &mut Window {
let mut s = self.window.get_size();
s.x = width;
self.window.set_size(&s);
self.goto(0.0, 0.0);
self
}
pub fn height(&mut self, height: u32) -> &mut Window {
let mut s = self.window.get_size();
s.y = height;
self.window.set_size(&s);
self.goto(0.0, 0.0);
self
}
pub fn mouse(&self) -> (f32, f32) {
let pos = self.window.get_mouse_position().to_vector2f();
(pos.x, pos.y)
}
pub fn mx(&self) -> f32 {
self.mouse().0
}
pub fn my(&self) -> f32 {
self.mouse().1
}
pub fn is_open(&self) -> bool {
self.window.is_open()
}
pub fn left_click(&self) -> bool {
self.event.left
}
pub fn right_click(&self) -> bool {
self.event.right
}
pub fn key<T: Key>(&self, k: T) -> bool {
k.is_pressed(self)
}
pub fn poll_char(&mut self) -> String {
mem::replace(&mut self.event.chars, String::new())
}
pub fn clear(&mut self, r: u8, g: u8, b: u8) {
self.window.clear(&Color::new_rgb(r, g, b));
self.event.chars.clear();
self.event.left = false;
self.event.right = false;
}
pub fn listen(&mut self) {
for e in self.window.events() {
match e {
Event::TextEntered {
code: c
} => self.event.chars.push(c),
Event::MouseLeft => self.event.left = true,
Event::MouseEntered => self.event.right = true,
Event::Closed => self.window.close(),
Event::Resized {
width,
height,
} => {
self.width(width);
self.height(height);
}
Event::KeyPressed {
code: c,
..
} => {
match c {
SfmlKey::Escape => self.event.keys.push(SpecialKey::Escape),
SfmlKey::LControl | SfmlKey::RControl =>
self.event.keys.push(SpecialKey::Control),
SfmlKey::LShift | SfmlKey::RShift =>
self.event.keys.push(SpecialKey::Shift),
SfmlKey::LAlt | SfmlKey::RAlt => self.event.keys.push(SpecialKey::Alt),
SfmlKey::Return => self.event.keys.push(SpecialKey::Return),
SfmlKey::BackSpace => self.event.keys.push(SpecialKey::Backspace),
SfmlKey::Left => self.event.keys.push(SpecialKey::Left),
SfmlKey::Right => self.event.keys.push(SpecialKey::Right),
SfmlKey::Up => self.event.keys.push(SpecialKey::Up),
SfmlKey::Down => self.event.keys.push(SpecialKey::Down),
_ => {}
}
}
_ => {}
}
}
}
pub fn update(&mut self) -> bool {
thread::sleep_ms(50);
self.window.display();
self.is_open()
}
pub fn goto(&mut self, x: f32, y: f32) {
let size = self.window.get_size().to_vector2f();
self.window.set_view(&View::new_init(&(Vector2f::new(x, y) + size / 2.0), &size).unwrap());
}
pub fn to_sfml_window(&mut self) -> &mut RenderWindow {
&mut self.window
}
}