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
195
196
use alloc::boxed::Box;
use core::mem;
use core::ops::DerefMut;
use core::slice;
use string::{String, ToString};
use vec::Vec;
use event::*;
use graphics::color::Color;
use fs::file::*;
use io::*;
pub mod event;
pub struct Window {
x: isize,
y: isize,
w: usize,
h: usize,
t: String,
file: File,
font: Vec<u8>,
data: Vec<u32>,
}
impl Window {
pub fn new(x: isize, y: isize, w: usize, h: usize, title: &str) -> Option<Box<Self>> {
let mut font = Vec::new();
if let Some(mut font_file) = File::open("file:///ui/unifont.font") {
font_file.read_to_end(&mut font);
}
match File::open(&format!("window:///{}/{}/{}/{}/{}", x, y, w, h, title)) {
Some(file) => Some(box Window {
x: x,
y: y,
w: w,
h: h,
t: title.to_string(),
file: file,
font: font,
data: vec![0; w * h * 4],
}),
None => None
}
}
pub fn x(&self) -> isize {
self.x
}
pub fn y(&self) -> isize {
self.y
}
pub fn width(&self) -> usize {
self.w
}
pub fn height(&self) -> usize {
self.h
}
pub fn title(&self) -> String {
self.t.clone()
}
pub fn set_title(&mut self, title: &str) {
}
pub fn pixel(&mut self, x: isize, y: isize, color: Color) {
if x >= 0 && y >= 0 && x < self.w as isize && y < self.h as isize {
let offset = y as usize * self.w + x as usize;
self.data[offset] = color.data;
}
}
pub fn char(&mut self, x: isize, y: isize, c: char, color: Color) {
let mut offset = (c as usize) * 16;
for row in 0..16 {
let row_data;
if offset < self.font.len() {
row_data = self.font[offset];
} else {
row_data = 0;
}
for col in 0..8 {
let pixel = (row_data >> (7 - col)) & 1;
if pixel > 0 {
self.pixel(x + col as isize, y + row as isize, color);
}
}
offset += 1;
}
}
#[allow(unused_variables)]
pub fn set(&mut self, color: Color) {
let w = self.w;
let h = self.h;
self.rect(0, 0, w, h, color);
}
#[allow(unused_variables)]
pub fn rect(&mut self, start_x: isize, start_y: isize, w: usize, h: usize, color: Color) {
for y in start_y..start_y + h as isize {
for x in start_x..start_x + w as isize {
self.pixel(x, y, color);
}
}
}
pub fn image(&mut self, start_x: isize, start_y: isize, w: usize, h: usize, data: &[Color]) {
let mut i = 0;
for y in start_y..start_y + h as isize {
for x in start_x..start_x + w as isize {
if i < data.len() {
self.pixel(x, y, data[i])
}
i += 1;
}
}
}
pub fn poll(&mut self) -> Option<Event> {
let mut event = box Event::new();
let event_ptr: *mut Event = event.deref_mut();
match self.file.read(&mut unsafe {
slice::from_raw_parts_mut(event_ptr as *mut u8, mem::size_of::<Event>())
}) {
Some(_) => return Some(*event),
None => return None,
}
}
pub fn sync(&mut self) -> bool {
self.file.seek(SeekFrom::Start(0));
let to_write: &[u8] = unsafe{ mem::transmute::<&[u32],&[u8]>(&self.data) };
self.file.write(to_write);
return self.file.sync();
}
pub fn event_iter<'a>(&'a mut self) -> EventIter<'a> {
EventIter {
window: self,
}
}
}
pub struct EventIter<'a> {
window: &'a mut Window,
}
impl<'a> Iterator for EventIter<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
self.window.poll()
}
}