mirl 9.2.0

Miners Rust Lib - A massive collection of ever growing and changing functions, structs, and enums. Check the description for compatibility and toggleable features! (Most of the lib is controlled by flags/features so the lib can continue to be lightweight despite its size)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#[cfg(feature = "keycodes")]
#[cfg(all(feature = "svg", feature = "system"))]
#[cfg(feature = "std")]
pub use mouse::Cursor;

/// All backend related structs/traits
#[cfg(feature = "std")]
pub mod windowing;
#[cfg(feature = "std")]
#[deprecated(note = "Replace `glfw` with `framework::glfw` instead ")]
#[cfg(feature = "glfw")]
#[cfg(not(target_arch = "wasm32"))]
pub use windowing::glfw;
#[deprecated(note = "Replace `minifb` with `framework::minifb` instead")]
#[cfg(feature = "minifb")]
#[cfg(not(target_arch = "wasm32"))]
pub use windowing::minifb;
#[deprecated(
    note = "Replace `framework_traits` with `windowing::traits` instead"
)]
#[cfg(feature = "std")]
#[cfg(feature = "system")]
pub use windowing::traits as framework_traits;
#[allow(clippy::non_minimal_cfg)]
#[cfg(all(
    //feature = "svg",
    // feature = "system",
    //feature = "keycodes",
    feature = "std"
))]
// Window associates
/// Everything do to with cursors
pub mod mouse;

/// A modular system of accessing files/folders
#[cfg(feature = "std")]
pub mod file_system;

/// There is a lot of duplicate functionality between backends, why not share them?
pub mod shared;
/// Time related stuff, it's not a lot I Reckon
pub mod time;

/// The standart key-board/code detection
#[cfg(feature = "system")]
#[cfg(feature = "keycodes")]
#[cfg(feature = "keyboard_query")]
pub mod keyboard;

#[cfg(feature = "system")]
#[cfg(feature = "std")]
use crate::extensions::*;
use crate::math::NumberWithMonotoneOps;
#[cfg(feature = "std")]
// #[cfg(feature = "system")]
// #[cfg(any(
//     target_arch = "wasm32",
//     target_os = "linux",
//     target_os = "windows"
// ))]
use crate::prelude::Buffer;

/// Time trait, IDK
pub const trait Time {
    /// Get time in seconds
    fn get_elapsed_time(&self) -> f64;
}
#[cfg_attr(all(feature = "strum"), derive(strum::EnumIter))]
#[cfg_attr(all(feature = "enum_ext"), enum_ext::enum_extend)]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
// #[cfg_attr(feature = "wincode", derive(wincode::SchemaWrite, wincode::SchemaRead))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// A cursor style, what else to say?
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "c_compatible", repr(C))]
pub enum CursorStyle {
    /// Default Pointer
    Default,
    /// Open hand
    HandOpen,
    /// Closed hand
    HandClosed,
    /// Default cursor with an extra arrow (e.g. clickable text)
    Alias,
    /// Resize vertically + Resize horizontally
    AllScroll,
    /// Arrow pointing to the bottom left ⬋
    ArrowBottomLeft,
    /// Arrow pointing to the bottom right ⬊
    ArrowBottomRight,
    /// Arrow down with a _ at the end
    SideBottom,
    /// A plus shape
    Cell,
    /// Default cursor rotated to be vertical
    CenteredPointer,
    /// Horizontal resizing
    ResizeHorizontally,
    /// Eyedropper
    ColorPicker,
    /// Default cursor with ≡ attached
    ContextMenu,
    /// Default cursor with a plus
    Copy,
    /// Cross
    Crosshair,
    /// Closed hand with an 🚫 attached
    HandClosedNoDrop,
    /// Arrow pointing down
    ArrowDown,
    /// Tip of an ink pen
    Draft,
    /// Small pointers in all directions like this: ◄ ►
    Fleur,
    /// Question mark
    Help,
    /// Arrow pointing left
    ArrowLeft,
    /// Arrow left with a stopper |←
    SideLeft,
    /// Default cursor with a 🚫 attached
    NoDrop,
    /// "🚫"
    NotAllowed,
    /// A Pencil
    Pencil,
    /// Skull
    Pirate,
    /// Hand with pointing index finger
    Pointer,
    /// Arrow pointing right
    ArrowRight,
    /// Mirrored version of normal cursor
    MirroredPointer,
    /// Arrow pointing right with a stopper →|
    SideRight,
    /// Resize top right to bottom left
    ResizeNESW,
    /// Resize top left to bottom right
    ResizeNWSE,
    /// Resize horizontally
    SizeHor,
    /// Resize vertically
    ResizeVertically,
    /// I Beam
    Text,
    /// Arrow pointing up top left
    ArrowTopLeft,
    /// Arrow pointing up top right
    ArrowTopRight,
    /// Arrow pointing up with an _ on top
    SideTop,
    /// Arrow pointing up
    ArrowUp,
    /// I Beam rotated 90°
    VerticalText,
    /// Magnifying glass with plus
    ZoomIn,
    /// Magnifying glass with minus
    ZoomOut,
}
#[cfg_attr(all(feature = "strum"), derive(strum::EnumIter))]
#[cfg_attr(all(feature = "enum_ext"), enum_ext::enum_extend)]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
// #[cfg_attr(feature = "wincode", derive(wincode::SchemaWrite, wincode::SchemaRead))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Supported (and unsupported) mouse buttons
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "c_compatible", repr(C))]
pub enum MouseButton {
    /// ✨ The left mouse button ✨
    Left,
    /// ✨ The right mouse button ✨
    Right,
    /// ✨ The button between the left and right mouse buttons ✨
    Middle,
    /// An extra niche button some mice have
    Extra1,
    /// Another extra niche button some mice have
    Extra2,
    /// A freakish amalgamation of human invention
    Extra3,
    /// No one should be allowed this much power.
    Extra4,
    /// You can't expect to be able to expect everything ¯\_(ツ)_/¯
    Unsupported,
}

/// Represents digital keys using `KeyCodes` of which there should be plenty enough to pretty all libraries that use their own `KeyCodes`
pub mod keycodes;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
// #[cfg_attr(feature = "wincode", derive(wincode::SchemaWrite, wincode::SchemaRead))]
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// Settings for spawning in a window
#[cfg_attr(feature = "c_compatible", repr(C))]
pub struct WindowSettings {
    /// Remove the border of the window
    pub borderless: bool,
    /// If the title should be displayed
    pub title_visible: bool,
    /// Window render level -> Topmost, normal, bottommost
    pub window_level: WindowLevel,
    /// Position on screen
    pub position: (i32, i32),
    /// Size of the spawning window
    pub size: (i32, i32),
    /// If the window should be resizable
    pub resizable: bool,
    /// If the window should have the default os menu around it (fullscreen, minimize, close). The border however will still retain
    pub os_menu: bool,
    /// If the window should be considered visible to the user or not
    pub visible: bool,
}

#[cfg(feature = "system")]
#[cfg(feature = "std")]
impl WindowSettings {
    /// Get the settings on default settings
    /// For size a buffer is required
    /// For position, it will be centered on the screen
    #[must_use]
    #[cfg(any(
        target_arch = "wasm32",
        target_os = "linux",
        target_os = "windows"
    ))]
    pub fn default(buffer: &Buffer) -> Self {
        let size =
            (buffer.width, buffer.height).try_tuple_into().unwrap_or_default();
        Self {
            borderless: false,
            title_visible: true,
            window_level: WindowLevel::Normal,
            position: crate::system::get_center_of_screen_for_object(
                size.0, size.1,
            ),

            resizable: false,
            os_menu: true,
            size,
            visible: true,
        }
    }
    /// Set the visibility of the window
    #[must_use]
    pub const fn set_visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }
    /// Set the size of the window
    #[must_use]
    pub const fn set_size(mut self, size: (i32, i32)) -> Self {
        self.size = size;
        self
    }
    #[must_use]
    /// Set the size of the window to the size of the given buffer
    pub fn set_size_to_buffer(mut self, buffer: &Buffer) -> Self {
        self.size =
            (buffer.width, buffer.height).try_tuple_into().unwrap_or_default();
        self
    }
    #[must_use]
    /// Set the position of the window
    pub const fn set_position(mut self, position: (i32, i32)) -> Self {
        self.position = position;
        self
    }
    #[must_use]
    /// Set if the title should be visible
    pub const fn set_title_visible(mut self, title: bool) -> Self {
        self.title_visible = title;
        self
    }
    #[must_use]
    /// Sets if the border should be hidden
    pub const fn set_borderless(mut self, borderless: bool) -> Self {
        self.borderless = borderless;
        self
    }
    #[must_use]
    /// Set the render level of the window (topmost, normal, bottommost)
    pub const fn set_window_level(mut self, window_level: WindowLevel) -> Self {
        self.window_level = window_level;
        self
    }
    #[must_use]
    /// Set if the window should be resizable by the user
    pub const fn set_resizable(mut self, resizable: bool) -> Self {
        self.resizable = resizable;
        self
    }
    #[must_use]
    /// Set if the default os decoration should be visible (fullscreen, minimize, close)
    pub const fn set_os_menu(mut self, os_menu: bool) -> Self {
        self.os_menu = os_menu;
        self
    }
    #[must_use]
    #[cfg(any(
        target_arch = "wasm32",
        target_os = "linux",
        target_os = "windows"
    ))]
    /// Sets the position of the window to be centered on the screen
    pub fn center_window(mut self) -> Self {
        self.position = crate::system::get_center_of_screen_for_object(
            self.size.0,
            self.size.1,
        );
        self
    }
    /// Multiply the size of the window
    #[must_use]
    pub const fn multiply_size(mut self, by: i32) -> Self {
        self.size = self.size.mul((by, by));
        self
    }
    #[cfg(any(
        target_arch = "wasm32",
        target_os = "linux",
        target_os = "windows"
    ))]
    /// Multiply the size of the window
    #[must_use]
    pub fn fullscreen(mut self) -> Self {
        use crate::system::action::Screen;
        // The screen cannot be negative so unwrapping is safe
        self.size = {
            use crate::misc::EasyUnwrapUnchecked;

            crate::system::Os::get_screen_resolution()
                .try_tuple_into()
                .easy_unwrap_unchecked()
        };
        self
    }
}
#[cfg_attr(all(feature = "strum"), derive(strum::EnumIter))]
#[cfg_attr(all(feature = "enum_ext"), enum_ext::enum_extend)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
// #[cfg_attr(feature = "wincode", derive(wincode::SchemaWrite, wincode::SchemaRead))]
#[derive(PartialEq, Copy, Clone, Debug, Eq, PartialOrd, Ord)]
/// The render level of the window the os should use
/// Any window on the same render level will move in front of every other window on that same level if the user clicks them
#[cfg_attr(feature = "c_compatible", repr(C))]
pub enum WindowLevel {
    /// Render layer on the bottom -> Always under '[`WindowLevel::Normal`]'
    AlwaysOnBottom,
    /// Render layer sandwiched in the middle of '[`WindowLevel::AlwaysOnBottom`]' and '[`WindowLevel::AlwaysOnTop`]'
    Normal,
    /// Render layer on the top -> Always on top of '[`WindowLevel::Normal`]'
    AlwaysOnTop,
}

// #[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// A thread safe double buffer when fast isn't fast enough
#[derive(Debug)]
#[cfg(feature = "std")]
#[cfg_attr(feature = "c_compatible", repr(C))]
pub struct DoubleBuffer {
    front: Buffer,
    back: Buffer,
    front_is_back: std::sync::atomic::AtomicBool, // true if front buffer is the "back" buffer
}

#[cfg(feature = "std")]
impl DoubleBuffer {
    /// Creates a new instance of the double buffer starting out empty
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            front: Buffer::new_empty((width, height)),
            back: Buffer::new_empty((width, height)),
            front_is_back: std::sync::atomic::AtomicBool::new(false),
        }
    }

    /// Renderer reads from the front buffer
    pub fn read(&self) -> &Buffer {
        if self.front_is_back.load(std::sync::atomic::Ordering::Acquire) {
            &self.back
        } else {
            &self.front
        }
    }

    /// Sim writes to the back buffer, then swaps
    pub fn write(&mut self, new_data: Buffer) {
        if self.front_is_back.load(std::sync::atomic::Ordering::Acquire) {
            self.front = new_data;
            self.front_is_back
                .store(false, std::sync::atomic::Ordering::Release);
        } else {
            self.back = new_data;
            self.front_is_back
                .store(true, std::sync::atomic::Ordering::Release);
        }
    }
}
use crate::prelude::{IntoPatch, TryIntoPatch};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// A struct to convert between 0.0-1.0 and the metrics of the screen
#[cfg_attr(feature = "c_compatible", repr(C))]
pub struct ScreenNormalizer<S> {
    screen_width: S,
    screen_height: S,
}
impl<S: Copy + NumberWithMonotoneOps> ScreenNormalizer<S> {
    /// Recommended is using [`crate::math::UniformRange`] in conjunction with this struct
    ///
    /// # Panics
    /// If the dimensions are invalid
    pub const fn new(screen_size: (S, S)) -> Self {
        Self {
            screen_width: screen_size.0,
            screen_height: screen_size.1,
        }
    }
    /// Convert a percentage into screen a coordinate horizontally
    pub fn try_percentile_to_x<T>(&self, p: S) -> Option<T>
    where
        S: TryIntoPatch<T>,
    {
        (p * self.screen_width).try_into_value()
    }
    /// Convert a percentage into screen a coordinate vertically
    pub fn try_percentile_to_y<T>(&self, p: S) -> Option<T>
    where
        S: TryIntoPatch<T>,
    {
        (p * self.screen_height).try_into_value()
    }
    /// Convert a percentage into screen a coordinate
    pub fn try_percentile_to_xy<T>(&self, pxy: (S, S)) -> Option<(T, T)>
    where
        S: TryIntoPatch<T>,
    {
        let x = (pxy.0 * self.screen_width).try_into_value()?;
        let y = (pxy.1 * self.screen_height).try_into_value()?;
        Some((x, y))
    }
    /// Convert a horizontal screen coordinate into a percentage
    pub fn try_x_to_percentile<T: TryIntoPatch<S>>(&self, x: T) -> Option<S> {
        if let Some(value) = (x).try_into_value() {
            return Some(value / self.screen_width);
        }
        core::intrinsics::cold_path();
        None
    }
    /// Convert a vertical screen coordinate into a percentage
    pub fn try_y_to_percentile<T: TryIntoPatch<S>>(&self, y: T) -> Option<S> {
        if let Some(value) = y.try_into_value() {
            return Some(value / self.screen_height);
        }
        core::intrinsics::cold_path();
        None
    }

    /// Convert a percentage into screen a coordinate horizontally
    pub fn percentile_to_x<T>(&self, p: S) -> T
    where
        S: IntoPatch<T>,
    {
        (p * self.screen_width).into_value()
    }
    /// Convert a percentage into screen a coordinate vertically
    pub fn percentile_to_y<T>(&self, p: S) -> T
    where
        S: IntoPatch<T>,
    {
        (p * self.screen_height).into_value()
    }
    /// Convert a percentage into screen a coordinate
    pub fn percentile_to_xy<T>(&self, pxy: (S, S)) -> (T, T)
    where
        S: IntoPatch<T>,
    {
        let x = (pxy.0 * self.screen_width).into_value();
        let y = (pxy.1 * self.screen_height).into_value();
        (x, y)
    }
    /// Convert a horizontal screen coordinate into a percentage
    pub fn x_to_percentile<T: IntoPatch<S>>(&self, x: T) -> S {
        (x).into_value() / self.screen_width
    }
    /// Convert a vertical screen coordinate into a percentage
    pub fn y_to_percentile<T: IntoPatch<S>>(&self, y: T) -> S {
        y.into_value() / self.screen_height
    }
}