aboutsummaryrefslogtreecommitdiff
path: root/src/gui/mod.rs
blob: 341bdede456c50e2b59fd2e425f30a6e62e58ade (plain)
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
/*
    This file is part of Takwrap.

    Takwrap is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <https://www.gnu.org/licenses/>.
*/

mod boardwidget;
use crate::gui::boardwidget::*;

use fltk::{app::*, button::*, menu::*, text::*, window::*};

#[derive(Clone, Copy)]
enum GUIMessage {
    None,
    Test,
    BoardClick,
}

pub struct GUI {
    app: App,
    window: DoubleWindow,
    board: BoardWidget,
    log: TextDisplay,
    sender: Sender<GUIMessage>,
    receiver: Receiver<GUIMessage>,
}

impl GUI {
    pub fn new() -> GUI {
        let app = App::default();

        // TODO: Why is this not the same as
        /*
               let mut window = Window::default()
                   .with_size(800, 600)
                   .with_label("Takwrap")
                   .center_screen();
        */
        // Windows made that way are not resizable?

        let mut window = DoubleWindow::new(0, 0, 800, 600, "Takwrap").center_screen();

        let (sender, receiver) = channel::<GUIMessage>();

        const MENU_HEIGHT: i32 = 24;
        let mut menu = SysMenuBar::default().with_size(800, MENU_HEIGHT);
        // menu.set_text_font(Font::Helvetica);
        menu.set_color(Color::Light2);
        menu.add_emit(
            "&File/New...\t",
            Shortcut::None,
            MenuFlag::Normal,
            sender,
            GUIMessage::None,
        );
        menu.add_emit(
            "&File/Open...\t",
            Shortcut::None,
            MenuFlag::Normal,
            sender,
            GUIMessage::Test,
        );

        const LOG_WIDTH: i32 = 800 - 600 + MENU_HEIGHT;
        let mut log = TextDisplay::default()
            .with_size(LOG_WIDTH, 600 - menu.height())
            .with_pos(800 - LOG_WIDTH, menu.height());
        log.set_buffer(Some(TextBuffer::default()));
        log.insert("test\n");

        let mut board_button = Button::default()
            .with_size(800 - LOG_WIDTH, 600 - menu.height())
            .below_of(&menu, 0);
        board_button.set_frame(FrameType::EmbossedBox);
        board_button.emit(sender, GUIMessage::BoardClick);

        window.resizable(&mut board_button);
        window.end();
        window.show();

        let board = BoardWidget::new(board_button, 5);

        // button.emit(sender, GUIMessage::Foo);

        GUI {
            app,
            board,
            window,
            log,
            sender,
            receiver,
        }
    }

    pub fn run(&mut self) -> Result<(), fltk::prelude::FltkError> {
        while self.app.wait() {
            if let Some(msg) = self.receiver.recv() {
                match msg {
                    GUIMessage::BoardClick => {
                        let x = event_x();
                        let y = event_y();
                        let (x, y) = self.board.to_square(x, y);
                        self.log.insert(&format!("Board click ({},{})\n", x, y));
                    }
                    _ => (),
                }
            }
        }
        Ok(())
    }
}