aboutsummaryrefslogtreecommitdiff
path: root/src/cli/mod.rs
blob: e96070f4e615d5bec5f6827e47aadf9f23900dad (plain) (blame)
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
//! In-window Command line interface. Used for operations that are just easier than with GUI.
//!
//! Sometimes it is nice to have a GUI, for instance when a selection has to be made, things have to
//! be moved etc., however for operations like saving/loading and exporting, no such thing has to be
//! done and the GUI is really just slowing you down (at least in my opinion). For these operations,
//! it is much better to simply have a command do that specific thing. It is also much easier to
//! implement a new command, so features can be tested more quickly. For some things, there should
//! still be a GUI option. With the example of saving/loading, it is much easier to find some hidden
//! folder in a GUI, so that is definitely a consideration for the future.

pub mod cmd;
pub use self::cmd::*;

use crate::colours::DEFAULT_COLOURS;
use crate::math::Vec2;
use crate::Editor;
use raylib::drawing::{RaylibDraw, RaylibDrawHandle};
use raylib::ffi::KeyboardKey;
use raylib::RaylibHandle;

/// The command line interface. Should be created only once per program instance.
pub struct CLI {
    text: String,
    active: bool,
}

impl CLI {
    /// Create a CLI for this instance
    pub fn new() -> Self {
        Self {
            text: String::new(),
            active: false,
        }
    }

    /// Activates the CLI, which will now capture keyboard input and execute commands accordingly.
    pub fn activate(&mut self) {
        if !self.active {
            self.text = ";".to_owned();
            self.active = true;
        }
    }

    /// Handle input for the command line and perform any commands the user may want to run.
    pub fn update(&mut self, rl: &mut RaylibHandle, editor: &mut Editor) {
        /* Check if the CLI is currently active. If not and it should not be activated according to
         * keyboard input, there is nothing to do.
         */
        if !self.active {
            if rl.is_key_pressed(KeyboardKey::KEY_SEMICOLON) {
                // Don't write the keypress again.
                rl.get_key_pressed();
                self.activate();
            } else {
                return;
            }
        }

        // The CLI is currently active. Handle input to it.
        if let Some(key) = rl.get_key_pressed_number() {
            self.text.push(key as u8 as char);
        } else if rl.is_key_pressed(KeyboardKey::KEY_BACKSPACE) {
            self.text.pop();
        } else if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
            self.text.clear();
        }

        // When the text is empty, there is also no command marker, so set as inactive and leave.
        if self.text.is_empty() {
            self.active = false;
            return;
        }

        // Perform the entered command, when the enter-key is pressed.
        if rl.is_key_pressed(KeyboardKey::KEY_ENTER) {
            self.active = false;
            match cmd::parse_command(&self.text[1..]) {
                Ok(cmd) => match cmd.process(editor) {
                    Ok(res) => self.text = format!("SUCCESS: {}", res),
                    Err(err) => self.text = format!("ERROR: {}", err),
                },
                Err(err) => self.text = format!("SYNTAX ERROR: {}", err),
            }
        }
    }

    /// Draw the command line at the bottom of the window.
    pub fn draw(&self, rld: &mut RaylibDrawHandle) {
        let pos = Vec2::new(150., rld.get_screen_height() as f32 - 25.);

        rld.draw_rectangle_v(
            pos,
            Vec2::new(rld.get_screen_width() as f32 - pos.x, 25.),
            DEFAULT_COLOURS.cli_background,
        );
        rld.draw_text(
            &self.text,
            155,
            rld.get_screen_height() - 22,
            20,
            DEFAULT_COLOURS.cli_foreground,
        );
    }
}