/* 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. Takwrap 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 Takwrap. If not, see . */ mod boardwidget; use crate::gui::boardwidget::*; use fltk::{ app::*, button::*, enums::Color, enums::FrameType, enums::Shortcut, menu::*, text::*, window::*, }; use fltk::prelude::*; #[derive(Clone, Copy)] enum GUIMessage { None, Test, BoardClick, } pub struct GUI { app: App, window: DoubleWindow, board: BoardWidget, log: TextDisplay, sender: Sender, receiver: Receiver, } 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::(); 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::empty(), MenuFlag::Normal, sender, GUIMessage::None, ); menu.add_emit( "&File/Open...\t", Shortcut::empty(), 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(()) } }