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
use bevy_app::{App, Update};
use bevy_doryen::doryen::{AppOptions, TextAlign, DEFAULT_CONSOLE_HEIGHT, DEFAULT_CONSOLE_WIDTH};
use bevy_doryen::prelude::*;
use bevy_doryen::{ResizeMode, Resized};
use bevy_ecs::prelude::EventReader;
use bevy_ecs::system::{Res, ResMut, Resource};

#[derive(Resource)]
struct ResizeData {
    width: u32,
    height: u32,
    mouse_pos: (f32, f32),
}

pub fn main() {
    App::new()
        .insert_resource(DoryenPluginSettings {
            app_options: AppOptions {
                window_title: String::from("resizable console"),
                vsync: false,
                ..Default::default()
            },
            resize_mode: ResizeMode::Callback(resize_callback),
            ..Default::default()
        })
        .add_plugins(DoryenPlugin)
        .insert_resource(ResizeData {
            width: DEFAULT_CONSOLE_WIDTH,
            height: DEFAULT_CONSOLE_HEIGHT,
            mouse_pos: (0.0, 0.0),
        })
        .add_systems(Update, (update_mouse_position, resize_events))
        .add_systems(Render, render)
        .run();
}

fn update_mouse_position(mut resize_data: ResMut<ResizeData>, input: Res<Input>) {
    resize_data.mouse_pos = input.mouse_pos();
}

fn resize_callback(root_console: &mut RootConsole, resized: Resized) {
    root_console.resize(resized.new_width / 8, resized.new_height / 8)
}

fn resize_events(mut resize_data: ResMut<ResizeData>, mut event_reader: EventReader<Resized>) {
    if let Some(resized) = event_reader.read().last() {
        resize_data.width = resized.new_width / 8;
        resize_data.height = resized.new_height / 8;
    }
}

fn render(mut root_console: ResMut<RootConsole>, resize_data: Res<ResizeData>) {
    root_console.rectangle(
        0,
        0,
        resize_data.width,
        resize_data.height,
        Some((128, 128, 128, 255)),
        Some((0, 0, 0, 255)),
        Some(' ' as u16),
    );
    root_console.area(
        10,
        10,
        5,
        5,
        Some((255, 64, 64, 255)),
        Some((128, 32, 32, 255)),
        Some('&' as u16),
    );
    root_console.print(
        (resize_data.width / 2) as i32,
        (resize_data.height / 2) as i32,
        &format!("{} x {}", resize_data.width, resize_data.height),
        TextAlign::Center,
        None,
        None,
    );
    root_console.back(
        resize_data.mouse_pos.0 as i32,
        resize_data.mouse_pos.1 as i32,
        (255, 255, 255, 255),
    );
}