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
use super::Time;
use crate::core::Window;
use crate::gfx::Graphics;
use crate::input::{Gamepads, Keyboard, Mouse};
use directories::ProjectDirs;
use std::cell::Cell;
use std::fmt::{Debug, Formatter};
use std::ops::Deref;
use std::path::Path;
use std::rc::Rc;
/// Handle to the game's core systems.
///
/// This handle can be cloned and passed around freely to give objects access to the context.
#[derive(Clone)]
pub struct Context(pub(crate) Rc<ContextData>);
/// Context data.
#[derive(Clone)]
pub struct ContextData {
pub window: Window,
pub time: Time,
pub mouse: Mouse,
pub keyboard: Keyboard,
pub gamepads: Gamepads,
pub graphics: Graphics,
#[cfg(feature = "lua")]
pub lua: mlua::WeakLua,
#[cfg(feature = "lua")]
pub reload_lua: Cell<bool>,
pub quit_requested: Cell<bool>,
pub dirs: ProjectDirs,
}
impl Deref for Context {
type Target = ContextData;
#[inline]
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl Debug for Context {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Context").finish_non_exhaustive()
}
}
impl Context {
#[cfg(feature = "lua")]
pub fn from_lua(lua: &mlua::Lua) -> mlua::AppDataRef<'_, Self> {
lua.app_data_ref::<Self>().unwrap()
}
#[inline]
pub fn dt(&self) -> f32 {
self.time.delta()
}
#[inline]
pub fn quit(&self) {
self.quit_requested.set(true);
}
#[inline]
pub fn quit_requested(&self) -> bool {
self.quit_requested.get()
}
#[cfg(feature = "lua")]
pub fn reload_lua(&self) {
self.reload_lua.set(true);
}
#[cfg(feature = "lua")]
pub fn reload_lua_requested(&self) -> bool {
self.reload_lua.get()
}
/// Returns the path to the game's cache directory.
///
/// |Platform | Example |
/// | ------- | ---------------------------------------------------- |
/// | Linux | /home/alice/.cache/appname |
/// | macOS | /Users/Alice/Library/Caches/Org-Name.App-Name |
/// | Windows | C:\Users\Alice\AppData\Local\Org Name\App Name\cache |
#[inline]
pub fn cache_dir(&self) -> &Path {
self.dirs.cache_dir()
}
/// Returns the path to the game's config directory.
///
/// |Platform | Example |
/// | ------- | ---------------------------------------------------------- |
/// | Linux | /home/alice/.config/appname |
/// | macOS | /Users/Alice/Library/Application Support/Org-Name.App-Name |
/// | Windows | C:\Users\Alice\AppData\Roaming\Org Name\App Name\config |
#[inline]
pub fn config_dir(&self) -> &Path {
self.dirs.config_dir()
}
/// Returns the path to the game's local config directory.
///
/// |Platform | Example |
/// | ------- | ---------------------------------------------------------- |
/// | Linux | /home/alice/.config/appname |
/// | macOS | /Users/Alice/Library/Application Support/Org-Name.App-Name |
/// | Windows | C:\Users\Alice\AppData\Local\Org Name\App Name\config |
#[inline]
pub fn config_local_dir(&self) -> &Path {
self.dirs.config_local_dir()
}
/// Returns the path to the game's data directory.
///
/// |Platform | Example |
/// | ------- | ---------------------------------------------------------- |
/// | Linux | /home/alice/.local/share/appname |
/// | macOS | /Users/Alice/Library/Application Support/Org-Name.App-Name |
/// | Windows | C:\Users\Alice\AppData\Roaming\Org Name\App Name\data |
#[inline]
pub fn data_dir(&self) -> &Path {
self.dirs.data_dir()
}
/// Returns the path to the game's local data directory.
///
/// |Platform | Example |
/// | ------- | ---------------------------------------------------------- |
/// | Linux | /home/alice/.local/share/appname |
/// | macOS | /Users/Alice/Library/Application Support/Org-Name.App-Name |
/// | Windows | C:\Users\Alice\AppData\Local\Org Name\App Name\data |
#[inline]
pub fn data_local_dir(&self) -> &Path {
self.dirs.data_local_dir()
}
/// Returns the path to the game's preference directory.
///
/// |Platform | Example |
/// | ------- | ------------------------------------------------------- |
/// | Linux | /home/alice/.config/appname |
/// | macOS | /Users/Alice/Library/Preferences/Org-Name.App-Name |
/// | Windows | C:\Users\Alice\AppData\Roaming\Org Name\App Name\config |
#[inline]
pub fn preferences_dir(&self) -> &Path {
self.dirs.preference_dir()
}
}