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
use xege_ffi::*;
use crate::{Key, KeyFlags, KeyMsg, MouseMsg, Point};
/// Window handle.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Window(pub(crate) *mut ());
impl Window {
/// Move Window to (x, y) relative the screen.
///
/// # Parameters
/// * `x` - The x coordinate of the new position.
/// * `y` - The y coordinate of the new position.
/// * `redraw` - If true, the window will be redrawn.
pub fn move_to(&mut self, x: i32, y: i32, redraw: bool) {
unsafe { ege_movewindow(x, y, redraw) };
}
/// Resize the window to (width, height).
///
/// # Parameters
/// * `width` - The new width of the window.
/// * `height` - The new height of the window.
pub fn resize(&mut self, width: i32, height: i32) {
unsafe { ege_resizewindow(width, height) };
}
/// Flushes the window.
pub fn flush(&mut self) {
unsafe { ege_flushwindow() };
}
/// Show the window.
///
/// # Note
/// If the initialization mode contains `Init::Hide`,
/// then this function needs to be called for the window to display.
pub fn show(&mut self) {
unsafe { ege_showwindow() };
}
/// Hide the window.
pub fn hide(&mut self) {
unsafe { ege_hidewindow() };
}
/// Get the state of specified key.
///
/// # Parameters
/// * `key` - The key to check.
///
/// # Returns
/// * `true` if the key is pressed, `false` otherwise.
pub fn keystate(&self, key: Key) -> bool {
unsafe { ege_keystate(<Key as Into<u32>>::into(key) as i32) }
}
/// Get next character message.
///
/// # Returns
/// * `Some(char)` if has character message, `None` otherwise.
///
/// # Bug
/// When inputting through an input method, this function produces unexpected character capture.
/// The `KeyMsg::Char` variant returned by the `getmsg` function should be used instead.
#[deprecated(since = "0.1.0", note = "Use `KeyMsg::Char` and `getmsg` instead.")]
pub fn getchar(&self) -> Option<char> {
if unsafe { ege_kbhit() != 0 } {
char::from_u32(unsafe { ege_getch() as u32 })
} else {
None
}
}
/// Get next key message.
///
/// # Returns
/// * `Some((KeyMsg, KeyFlags))` if has key message, `None` otherwise.
pub fn getmsg(&self) -> Option<(KeyMsg, KeyFlags)> {
if unsafe { ege_kbmsg() != 0 } {
let msg = unsafe { ege_getkey() };
let keymsg = match msg.msg {
xege_ffi::ege_key_msg_e_key_msg_down => KeyMsg::Down(Key::from(msg.key as u32)),
xege_ffi::ege_key_msg_e_key_msg_up => KeyMsg::Up(Key::from(msg.key as u32)),
xege_ffi::ege_key_msg_e_key_msg_char => {
KeyMsg::Char(char::from_u32(msg.key as u32)?)
}
_ => unreachable!(),
};
let mut flags = KeyFlags::none();
if (msg.flags & xege_ffi::ege_key_flag_e_key_flag_shift as u32) != 0 {
flags |= KeyFlags::Shift;
}
if (msg.flags & xege_ffi::ege_key_flag_e_key_flag_ctrl as u32) != 0 {
flags |= KeyFlags::Ctrl;
}
if (msg.flags & xege_ffi::ege_key_flag_e_key_flag_first_down as u32) != 0 {
flags |= KeyFlags::First;
}
Some((keymsg, flags))
} else {
None
}
}
/// Flush the key message buffer.
pub fn flushkey(&self) {
unsafe { ege_flushkey() };
}
/// Get next mouse message.
///
/// # Returns
/// * `Some(MouseMsg)` if has mouse message, `None` otherwise.
pub fn getmouse(&self) -> Option<MouseMsg> {
if unsafe { ege_mousemsg() != 0 } {
let msg = unsafe { ege_getmouse() };
Some(MouseMsg { msg })
} else {
None
}
}
/// Get the current mouse position.
///
/// # Returns
/// The current mouse position.
pub fn mousepos(&self) -> Point {
let mut p = Point { x: 0, y: 0 };
unsafe { ege_mousepos(&mut p.x, &mut p.y) };
p
}
/// Set the mouse cursor visible or not.
///
/// # Parameters
/// * `visible` - If true, the mouse cursor will be visible.
pub fn showmouse(&self, show: bool) {
unsafe { ege_showmouse(show as i32) };
}
/// Flush the mouse message buffer.
pub fn flushmouse(&self) {
unsafe { ege_flushmouse() };
}
}