first steps

This commit is contained in:
2019-06-03 14:59:52 +02:00
commit ca3696f63f
6 changed files with 443 additions and 0 deletions

119
src/main.rs Normal file
View File

@@ -0,0 +1,119 @@
extern crate gtk;
extern crate gio;
extern crate gdk;
extern crate glib;
use gtk::BoxExt;
use gtk::prelude::*;
use gio::prelude::*;
use std::env::args;
use std::process;
mod grid;
mod pawn {
pub struct Pawn {
name: String
}
fn pawn_factory() -> Vec<Pawn> {
let mut pawns = Vec::with_capacity(3);
for name in &["Lomion", "Oilosse", "Fefi"] {
pawns.push(Pawn{ name: name.to_string() });
}
pawns
}
#[cfg(test)]
mod tests {
#[test]
fn test_pawn_factory() {}
}
}
struct App {
inner: gtk::Application,
pawns: Vec<pawn::Pawn>,
}
impl App {
fn new<'a>() -> Result<Self, &'a str> {
let app =
App {
inner: gtk::Application::new(
"home.local.PlayMat",
gio::ApplicationFlags::FLAGS_NONE
).expect("Failed to build Application"),
pawns: Vec::new(),
};
app.inner.connect_startup(|_| {});
app.inner.connect_activate(Self::on_activate);
Ok(app)
}
fn on_activate(app: &gtk::Application) {
println!("Activate App");
let main_src = include_str!("../res/main.glade");
let builder = gtk::Builder::new_from_string(main_src);
let win: gtk::ApplicationWindow =
builder
.get_object("app")
.unwrap();
win.set_application(app);
// Set up a simple switch for the Pawn list panel
let panel: gtk::Paned = builder.get_object("panel").unwrap();
Self::new_action(app, "panel_switch", move |_,_| {
if panel.get_position() == 0 {
panel.set_position(360);
} else {
panel.set_position(0);
}
});
// Initialize grid
let grid: gtk::Grid = builder.get_object("map").unwrap();
// TODO: implement drag-drop events
// * From pawn_list to cell : place pawn on dest cell
// * From cell to cell : move pawn from source to dest cell
let _grid = grid::Grid::init(grid, 10);
// TODO:
// * Display creature description when hovering over its position (cell)
// TODO: Pawn list
win.show_all();
}
/// Creates a simple action and connects the given handler to its activate signal.
pub fn new_action<F: Fn(&gio::SimpleAction, &Option<glib::Variant>) + 'static>(
app: &gtk::Application,
name: &str,
f: F,
) {
let action = gio::SimpleAction::new(name, None);
action.connect_activate(f);
app.add_action(&action);
}
fn run(&self, args: &[String]) -> i32 {
self.inner.run(args)
}
}
fn main() {
if gtk::init().is_err() {
println!("Failed to initialize Gtk");
return
}
let exit_code = match App::new() {
Err(e) => {
println!("Error !\n{}", e);
1
},
Ok(app) => app.run(&args().collect::<Vec<_>>()),
};
process::exit(exit_code);
}