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
//! bevy_doryen is a Bevy plugin that lets you use [Bevy] with the [Doryen]
//! roguelike library.
//!
//! Usage:
//! ```no_run
//! # use bevy_app::{App, Update, Startup};
//! # use bevy_doryen::prelude::*;
//! # use bevy_doryen::{ResizeMode, MouseButton};
//! # use bevy_doryen::doryen::AppOptions;
//! # use bevy_ecs::system::IntoSystem;
//! App::new()
//!     // Insert a `DoryenPluginSettings` resource to configure the plugin.
//!     .insert_resource(DoryenPluginSettings {
//!         // `app_options` lets you configure Doryen just as if you were
//!         // using Doryen without Bevy. The default is `AppOptions::default()`.
//!         app_options: AppOptions {
//!             show_cursor: true,
//!             resizable: true,
//!             ..AppOptions::default()
//!         },
//!         // Lets you configure which mouse buttons to listen for. The default
//!         // is left, middle and right click.
//!         mouse_button_listeners: vec![
//!             MouseButton::Left,
//!             MouseButton::Middle,
//!             MouseButton::Right,
//!         ],
//!         // Lets you configure how the application should behave when resized.
//!         // The default is `ResizeMode::Nothing`. See `ResizeMode`'s
//!         // documentation for more information.
//!         resize_mode: ResizeMode::Nothing
//!     })
//!     // Add the `DoryenPlugin` to Bevy.
//!     .add_plugins(DoryenPlugin)
//!     // Add your Bevy systems like usual. Excluding startup systems, which
//!     // only run once, these systems are run during Doryen's update phase;
//!     // i.e. 60 times per second.
//!     .add_systems(Startup, init)
//!     .add_systems(Update, input)
//!     // The `Render` schedules lets you add systems that should
//!     // be run during Doryen's render phase.
//!     .add_systems(Render, render)
//!     .run();
//!
//! # fn init() { }
//! # fn input() { }
//! # fn render() { }
//! ```
//!
//! [Bevy]: https://bevyengine.org/
//! [Doryen]: https://github.com/jice-nospam/doryen-rs

// region: Coding conventions
// <editor-fold desc="Coding conventions" defaultstate="collapsed">
//
// Forbid (just no)
#![forbid(unsafe_code)]
// Deny (don't do this)
#![deny(anonymous_parameters)]
#![deny(elided_lifetimes_in_paths)]
#![deny(ellipsis_inclusive_range_patterns)]
#![deny(nonstandard_style)]
#![deny(rust_2018_idioms)]
#![deny(trivial_numeric_casts)]
#![deny(rustdoc::broken_intra_doc_links)]
//#![deny(unused)]
//
// Warn (try not to do this)
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(variant_size_differences)]
//
// Clippy conventions
//
// Deny (don't do this)
#![deny(clippy::cast_lossless)]
#![deny(clippy::default_trait_access)]
#![deny(clippy::empty_enum)]
#![deny(clippy::enum_glob_use)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::explicit_into_iter_loop)]
#![deny(clippy::explicit_iter_loop)]
#![deny(clippy::manual_filter_map)]
#![deny(clippy::filter_map_next)]
#![deny(clippy::manual_find_map)]
#![deny(clippy::if_not_else)]
#![deny(clippy::invalid_upcast_comparisons)]
#![deny(clippy::items_after_statements)]
#![deny(clippy::large_digit_groups)]
#![deny(clippy::map_flatten)]
#![deny(clippy::match_same_arms)]
#![deny(clippy::mut_mut)]
#![deny(clippy::needless_continue)]
#![deny(clippy::needless_pass_by_value)]
#![deny(clippy::map_unwrap_or)]
#![deny(clippy::redundant_closure_for_method_calls)]
#![deny(clippy::single_match_else)]
#![deny(clippy::string_add_assign)]
#![deny(clippy::type_repetition_in_bounds)]
#![deny(clippy::unseparated_literal_suffix)]
#![deny(clippy::unused_self)]
#![deny(clippy::use_self)] // Sometimes gives false positives; feel free to disable.
#![deny(clippy::used_underscore_binding)]
//
// Warn (try not to do this)
//#![warn(clippy::must_use_candidate)]
//#![warn(clippy::pub_enum_variant_names)]
#![warn(clippy::shadow_unrelated)]
#![warn(clippy::similar_names)]
#![warn(clippy::too_many_lines)]
// </editor-fold>
// endregion

mod debug_app_options;
mod input;
mod render_schedule;
mod root_console;

/// Re-export of the Doryen library types.
pub mod doryen {
    pub use doryen_rs::*;
}

use std::borrow::Cow;

use bevy_app::{App as BevyApp, AppExit, Plugin};
use bevy_ecs::event::ManualEventReader;
use bevy_ecs::prelude::{Event, Events};
use bevy_ecs::system::Resource;
use doryen::{App as DoryenApp, DoryenApi, Engine, UpdateEvent};

use crate::doryen::{AppOptions, Console};

pub use input::*;
pub use render_schedule::*;
pub use root_console::*;

/// Common imports
pub mod prelude {
    pub use crate::{DoryenPlugin, DoryenPluginSettings, Input, Render, RootConsole};
}

/// The Bevy Doryen plugin.
#[derive(Default, Clone, Copy, Debug)]
pub struct DoryenPlugin;

/// DoryenPlugin settings.
#[derive(Resource)]
pub struct DoryenPluginSettings {
    /// The [`AppOptions`] passed to the [`DoryenApp`].
    pub app_options: AppOptions,
    /// Which mouse buttons to request input data for from Doryen during the
    /// input handling.
    /// Defaults to left, middle and right mouse buttons.
    pub mouse_button_listeners: Vec<MouseButton>,
    /// What to do when the Doryen window is resized.
    pub resize_mode: ResizeMode,
}

impl std::fmt::Debug for DoryenPluginSettings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DoryenPluginSettings")
            .field(
                "app_options",
                &debug_app_options::DebugAppOptions(&self.app_options),
            )
            .field("mouse_button_listeners", &self.mouse_button_listeners)
            .field("resize_mode", &self.resize_mode)
            .finish()
    }
}

impl Default for DoryenPluginSettings {
    fn default() -> Self {
        Self {
            app_options: AppOptions::default(),
            mouse_button_listeners: vec![
                MouseButton::Left,
                MouseButton::Middle,
                MouseButton::Right,
            ],
            resize_mode: ResizeMode::Nothing,
        }
    }
}

impl Plugin for DoryenPlugin {
    fn build(&self, app: &mut BevyApp) {
        app.init_resource::<RootConsole>()
            .init_resource::<Input>()
            .init_resource::<FpsInfo>()
            .add_event::<SetFontPath>()
            .add_event::<Resized>()
            .add_event::<Capture>()
            .add_plugins(RenderSchedulePlugin)
            .set_runner(doryen_runner);
    }
}

struct DoryenPluginEngine {
    bevy_app: BevyApp,
    app_exit_event_reader: ManualEventReader<AppExit>,
    set_font_path_event_reader: ManualEventReader<SetFontPath>,
    capture_event_reader: ManualEventReader<Capture>,
    swap_console: Option<Console>,
    mouse_button_listeners: Vec<MouseButton>,
    previous_screen_size: (u32, u32),
    previous_console_size: (u32, u32),
    resize_mode: ResizeMode,
}

impl DoryenPluginEngine {
    #[inline]
    fn take_root_console_ownership(&mut self, api: &mut dyn DoryenApi) {
        use std::mem::swap;

        // Take ownership of the Doryen root console
        swap(api.con(), self.swap_console.as_mut().unwrap());

        // Insert it into the DoryenRootConsole resource
        let mut doryen_root_console = self.bevy_app.world.resource_mut::<RootConsole>();
        doryen_root_console.0 = self.swap_console.take();
    }

    #[inline]
    fn restore_root_console_ownership(&mut self, api: &mut dyn DoryenApi) {
        use std::mem::swap;

        // Take the root console out of the DoryenRootConsole resource
        let mut doryen_root_console = self
            .bevy_app
            .world
            .get_resource_mut::<RootConsole>()
            .unwrap();
        self.swap_console = doryen_root_console.0.take();

        // Hand ownership of the Doryen root console back to Doryen
        swap(api.con(), self.swap_console.as_mut().unwrap());
    }

    #[inline]
    fn handle_input(&mut self, api: &mut dyn DoryenApi) {
        let mut doryen_input = self.bevy_app.world.get_resource_mut::<Input>().unwrap();
        let input = api.input();
        doryen_input.handle_input(&self.mouse_button_listeners, input);
    }
}

impl Engine for DoryenPluginEngine {
    fn update(&mut self, api: &mut dyn DoryenApi) -> Option<UpdateEvent> {
        let mut doryen_fps_info = self.bevy_app.world.resource_mut::<FpsInfo>();
        doryen_fps_info.fps = api.fps();
        doryen_fps_info.average_fps = api.average_fps();

        self.handle_input(api);

        self.take_root_console_ownership(api);
        self.bevy_app.update();
        self.restore_root_console_ownership(api);

        // Process the latest AppExit event
        if let Some(app_exit_events) = self.bevy_app.world.get_resource_mut::<Events<AppExit>>() {
            if self
                .app_exit_event_reader
                .read(&app_exit_events)
                .last()
                .is_some()
            {
                return Some(UpdateEvent::Exit);
            }
        }

        // Process the latest SetFontPath event
        let doryen_set_font_path_events = self.bevy_app.world.resource_mut::<Events<SetFontPath>>();
        if let Some(doryen_set_font_path) = self
            .set_font_path_event_reader
            .read(&doryen_set_font_path_events)
            .last()
        {
            api.set_font_path(doryen_set_font_path.0.as_ref());
        }

        // Process the latest Capture event
        let doryen_capture_events = self.bevy_app.world.resource_mut::<Events<Capture>>();
        if let Some(doryen_capture) = self
            .capture_event_reader
            .read(&doryen_capture_events)
            .last()
        {
            return Some(UpdateEvent::Capture(doryen_capture.0.to_string()));
        }

        None
    }

    fn render(&mut self, api: &mut dyn DoryenApi) {
        self.take_root_console_ownership(api);
        self.bevy_app.world.run_schedule(MainRender);
        self.restore_root_console_ownership(api);
    }

    fn resize(&mut self, api: &mut dyn DoryenApi) {
        let (previous_width, previous_height) = self.previous_screen_size;
        let (new_width, new_height) = api.get_screen_size();

        let mut resized_events = self
            .bevy_app
            .world
            .get_resource_mut::<Events<Resized>>()
            .unwrap();
        let resized = Resized {
            previous_width,
            previous_height,
            new_width,
            new_height,
        };
        resized_events.send(resized);
        //drop(resized_events);

        match self.resize_mode {
            ResizeMode::Nothing => (),
            ResizeMode::Automatic => {
                let (previous_console_width, previous_console_height) = self.previous_console_size;

                let w_ratio = previous_width / previous_console_width;
                let h_ratio = previous_height / previous_console_height;

                let new_console_width = new_width / w_ratio;
                let new_console_height = new_height / h_ratio;
                api.con().resize(new_console_width, new_console_height);
            }
            ResizeMode::Callback(callback) => {
                self.take_root_console_ownership(api);
                callback(
                    &mut self.bevy_app.world.get_resource_mut().unwrap(),
                    resized,
                );
                self.restore_root_console_ownership(api);
            }
        }

        self.previous_screen_size = (new_width, new_height);
        self.previous_console_size = api.con().get_size();
    }
}

fn doryen_runner(mut app: BevyApp) {
    let mut resource_settings = app
        .world
        .get_resource_or_insert_with(DoryenPluginSettings::default);
    let DoryenPluginSettings {
        app_options,
        mouse_button_listeners,
        resize_mode,
    } = std::mem::take(&mut *resource_settings);

    let AppOptions {
        screen_height,
        screen_width,
        console_height,
        console_width,
        ..
    } = app_options;

    let mut doryen_app = DoryenApp::new(app_options);

    doryen_app.set_engine(Box::new(DoryenPluginEngine {
        bevy_app: app,
        app_exit_event_reader: ManualEventReader::default(),
        set_font_path_event_reader: ManualEventReader::default(),
        capture_event_reader: ManualEventReader::default(),
        swap_console: Some(Console::new(0, 0)),
        mouse_button_listeners,
        previous_screen_size: (screen_width, screen_height),
        previous_console_size: (console_width, console_height),
        resize_mode,
    }));

    doryen_app.run();
}

/// This resource contains the values given by [`fps`](DoryenApi::fps) and
/// [`average_fps`](DoryenApi::average_fps) on the current update tick.
#[derive(Debug, Default, Clone, Copy, Resource)]
pub struct FpsInfo {
    /// The value given by [`fps`](DoryenApi::fps) on the current update tick.
    pub fps: u32,
    /// The value given by [`average_fps`](DoryenApi::average_fps) on the
    /// current update tick.
    pub average_fps: u32,
}

/// When you want to change Doryen's font path, emit an event of this type.
/// bevy_doryen will call [`set_font_path`](DoryenApi::set_font_path) with the
/// provided value. See its documentation for more details.
#[derive(Clone, Debug, Event)]
pub struct SetFontPath(pub Cow<'static, str>);
impl SetFontPath {
    /// Constructs a new [`SetFontPath`]
    pub fn new(path: impl Into<Cow<'static, str>>) -> Self {
        Self(path.into())
    }
}

/// Resized event object. Whenever Doryen's [`resize`](Engine::resize) method is
/// called, an event of this type is emitted.
#[derive(Debug, Clone, Copy, Event)]
pub struct Resized {
    /// The previous width of the Doryen game window.
    pub previous_width: u32,
    /// The previous height of the Doryen game window.
    pub previous_height: u32,
    /// The new width of the Doryen game window.
    pub new_width: u32,
    /// The new height of the Doryen game window.
    pub new_height: u32,
}

/// Emitting this event causes bevy_doryen to return [`UpdateEvent::Capture`]
/// from its Doryen `update` function, which saves a screenshot. See its
/// documentation for more details.
#[derive(Debug, Clone, Event)]
pub struct Capture(pub Cow<'static, str>);
impl Capture {
    /// Constructs a new [`Capture`]
    pub fn new(path: impl Into<Cow<'static, str>>) -> Self {
        Self(path.into())
    }
}

/// How the [`DoryenPlugin`] reacts to the resize event from Doryen.
#[derive(Clone, Copy)]
pub enum ResizeMode {
    /// Do nothing when the window is resized. This is the default behavior.
    Nothing,
    /// Set the console size to match the window size automatically. This
    /// retains the ratio defined between the console size and the screen size
    /// as given in the [`AppOptions`] at the start of the program.
    Automatic,
    /// Call the given function when the resize event is triggered. Because
    /// Doryen is sensitive to when the root console is resized, the safest
    /// place to make a call to do so and always have the correct behavior is
    /// during this resize callback which comes directly from Doryen itself. The
    /// [`Resized`] event is useful for reacting to resizing within Bevy
    /// systems for other reasons, but will arrive at a point that is too late
    /// to do the root console resizing correctly.
    Callback(fn(&mut RootConsole, Resized)),
}

impl std::fmt::Debug for ResizeMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Nothing => f.write_str("Nothing"),
            Self::Automatic => f.write_str("Automatic"),
            Self::Callback(_) => f.write_str("Callback"),
        }
    }
}