adds rustfmt config, formats current code
This commit is contained in:
1
rustfmt.toml
Normal file
1
rustfmt.toml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
max_width = 89
|
||||||
81
src/grid.rs
81
src/grid.rs
@@ -4,7 +4,7 @@ pub struct Grid {
|
|||||||
inner: gtk::Grid,
|
inner: gtk::Grid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,Copy,Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct CellPosition(i32, i32);
|
pub struct CellPosition(i32, i32);
|
||||||
|
|
||||||
impl Grid {
|
impl Grid {
|
||||||
@@ -14,9 +14,7 @@ impl Grid {
|
|||||||
for i in 0..(size * size) {
|
for i in 0..(size * size) {
|
||||||
let x = i % size;
|
let x = i % size;
|
||||||
let y = i / size;
|
let y = i / size;
|
||||||
let cell = grid::Cell::new(
|
let cell = grid::Cell::new(CellPosition(x, y));
|
||||||
CellPosition(x,y)
|
|
||||||
);
|
|
||||||
cell.inner.connect_clicked(app_state.clone());
|
cell.inner.connect_clicked(app_state.clone());
|
||||||
inner.attach(cell.as_ref(), x, y, 1, 1);
|
inner.attach(cell.as_ref(), x, y, 1, 1);
|
||||||
}
|
}
|
||||||
@@ -24,11 +22,11 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct CellWidget {
|
struct CellWidget {
|
||||||
eventbox: gtk::EventBox,
|
eventbox: gtk::EventBox,
|
||||||
position: gtk::Label,
|
position: gtk::Label,
|
||||||
header : gtk::Label,
|
header: gtk::Label,
|
||||||
desc_btn: gtk::Button,
|
desc_btn: gtk::Button,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,24 +35,12 @@ impl CellWidget {
|
|||||||
let cell_src = include_str!("../res/cell.glade");
|
let cell_src = include_str!("../res/cell.glade");
|
||||||
let builder = gtk::Builder::new_from_string(cell_src);
|
let builder = gtk::Builder::new_from_string(cell_src);
|
||||||
// Retrieve children
|
// Retrieve children
|
||||||
let eventbox: gtk::EventBox =
|
let eventbox: gtk::EventBox = builder.get_object("cell").unwrap();
|
||||||
builder
|
let header: gtk::Label = builder.get_object("header").unwrap();
|
||||||
.get_object("cell")
|
|
||||||
.unwrap();
|
|
||||||
let header: gtk::Label =
|
|
||||||
builder
|
|
||||||
.get_object("header")
|
|
||||||
.unwrap();
|
|
||||||
header.set_text(header_text);
|
header.set_text(header_text);
|
||||||
let position: gtk::Label =
|
let position: gtk::Label = builder.get_object("position").unwrap();
|
||||||
builder
|
|
||||||
.get_object("position")
|
|
||||||
.unwrap();
|
|
||||||
position.set_text(&format!("{}x{}", pos.0, pos.1));
|
position.set_text(&format!("{}x{}", pos.0, pos.1));
|
||||||
let desc_btn: gtk::Button =
|
let desc_btn: gtk::Button = builder.get_object("desc_btn").unwrap();
|
||||||
builder
|
|
||||||
.get_object("desc_btn")
|
|
||||||
.unwrap();
|
|
||||||
desc_btn.set_visible(false);
|
desc_btn.set_visible(false);
|
||||||
let cell = CellWidget {
|
let cell = CellWidget {
|
||||||
eventbox,
|
eventbox,
|
||||||
@@ -64,9 +50,11 @@ impl CellWidget {
|
|||||||
};
|
};
|
||||||
// Drag-and-drop capacity
|
// Drag-and-drop capacity
|
||||||
// The data to be sent
|
// The data to be sent
|
||||||
let targets = vec![
|
let targets = vec![gtk::TargetEntry::new(
|
||||||
gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0)
|
"text/plain",
|
||||||
];
|
gtk::TargetFlags::SAME_APP,
|
||||||
|
0,
|
||||||
|
)];
|
||||||
// Acting as source
|
// Acting as source
|
||||||
cell.eventbox.drag_source_set(
|
cell.eventbox.drag_source_set(
|
||||||
gdk::ModifierType::MODIFIER_MASK,
|
gdk::ModifierType::MODIFIER_MASK,
|
||||||
@@ -75,24 +63,21 @@ impl CellWidget {
|
|||||||
);
|
);
|
||||||
let h = cell.header.clone();
|
let h = cell.header.clone();
|
||||||
// Send data to the drop site
|
// Send data to the drop site
|
||||||
cell.eventbox.connect_drag_data_get(
|
cell.eventbox
|
||||||
move |_,_,data,info,time| {
|
.connect_drag_data_get(move |_, _, data, info, time| {
|
||||||
println!("Send...");
|
println!("Send...");
|
||||||
// TODO: Refactoring, this is the inverse of 'placing',
|
// TODO: Refactoring, this is the inverse of 'placing',
|
||||||
// building a PawnData instead of destructuring it.
|
// building a PawnData instead of destructuring it.
|
||||||
if let Some(to_send) = h.get_text() {
|
if let Some(to_send) = h.get_text() {
|
||||||
data.set_text(&to_send);
|
data.set_text(&to_send);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
let c = cell.clone();
|
let c = cell.clone();
|
||||||
// Empty the cell on successfull move
|
// Empty the cell on successfull move
|
||||||
cell.eventbox.connect_drag_data_delete(
|
cell.eventbox.connect_drag_data_delete(move |w, drag| {
|
||||||
move |w,drag| {
|
println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded()));
|
||||||
println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded()));
|
Self::set_content(&c, None);
|
||||||
Self::set_content(&c, None);
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
// Acting as destination
|
// Acting as destination
|
||||||
cell.eventbox.drag_dest_set(
|
cell.eventbox.drag_dest_set(
|
||||||
gtk::DestDefaults::ALL,
|
gtk::DestDefaults::ALL,
|
||||||
@@ -101,8 +86,8 @@ impl CellWidget {
|
|||||||
);
|
);
|
||||||
// Retrieve data from the source
|
// Retrieve data from the source
|
||||||
let c = cell.clone();
|
let c = cell.clone();
|
||||||
cell.eventbox.connect_drag_data_received(
|
cell.eventbox
|
||||||
move |w,_,_,_,data,_,_| {
|
.connect_drag_data_received(move |w, _, _, _, data, _, _| {
|
||||||
// Check if cell is not already occupied
|
// Check if cell is not already occupied
|
||||||
if !(c.header.get_text().unwrap().as_str() == "") {
|
if !(c.header.get_text().unwrap().as_str() == "") {
|
||||||
// TODO: Find what to do to abort, since
|
// TODO: Find what to do to abort, since
|
||||||
@@ -116,8 +101,7 @@ impl CellWidget {
|
|||||||
eprintln!("No data !");
|
eprintln!("No data !");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
});
|
||||||
);
|
|
||||||
cell
|
cell
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,15 +120,14 @@ impl CellWidget {
|
|||||||
|
|
||||||
pub fn connect_clicked(&self, state: AppState) {
|
pub fn connect_clicked(&self, state: AppState) {
|
||||||
let c = self.clone();
|
let c = self.clone();
|
||||||
self.eventbox
|
self.eventbox.connect_button_press_event(move |_, _| {
|
||||||
.connect_button_press_event(move |_,_| {
|
let mut state = state.borrow_mut();
|
||||||
let mut state = state.borrow_mut();
|
if let Some(ref data) = state.pending.take() {
|
||||||
if let Some(ref data) = state.pending.take() {
|
println!("{:?}", data);
|
||||||
println!("{:?}", data);
|
Self::set_content(&c, Some(&data.name));
|
||||||
Self::set_content(&c, Some(&data.name));
|
};
|
||||||
};
|
gtk::Inhibit(true)
|
||||||
gtk::Inhibit(true)
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +147,7 @@ impl Cell {
|
|||||||
fn new(position: CellPosition) -> Self {
|
fn new(position: CellPosition) -> Self {
|
||||||
Cell {
|
Cell {
|
||||||
inner: CellWidget::new(position, ""),
|
inner: CellWidget::new(position, ""),
|
||||||
position
|
position,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
97
src/main.rs
97
src/main.rs
@@ -1,13 +1,13 @@
|
|||||||
extern crate gtk;
|
|
||||||
extern crate gio;
|
|
||||||
extern crate gdk;
|
extern crate gdk;
|
||||||
|
extern crate gio;
|
||||||
extern crate glib;
|
extern crate glib;
|
||||||
|
extern crate gtk;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use gtk::prelude::*;
|
|
||||||
use gio::prelude::*;
|
use gio::prelude::*;
|
||||||
|
use gtk::prelude::*;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ mod pawn;
|
|||||||
|
|
||||||
/// Content of the Application state
|
/// Content of the Application state
|
||||||
///
|
///
|
||||||
#[derive(Debug,Clone,Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct AppData {
|
pub struct AppData {
|
||||||
/// A slot for any pawn waiting for placement
|
/// A slot for any pawn waiting for placement
|
||||||
pending: Option<pawn::PawnData>,
|
pending: Option<pawn::PawnData>,
|
||||||
@@ -25,7 +25,7 @@ pub struct AppData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A sharable state for the whole application
|
/// A sharable state for the whole application
|
||||||
#[derive(Debug,Clone,Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct AppState(Rc<RefCell<AppData>>);
|
pub struct AppState(Rc<RefCell<AppData>>);
|
||||||
|
|
||||||
impl std::ops::Deref for AppState {
|
impl std::ops::Deref for AppState {
|
||||||
@@ -40,17 +40,16 @@ struct App {
|
|||||||
state: AppState,
|
state: AppState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
fn new<'a>() -> Result<Self, &'a str> {
|
fn new<'a>() -> Result<Self, &'a str> {
|
||||||
let app =
|
let app = App {
|
||||||
App {
|
inner: gtk::Application::new(
|
||||||
inner: gtk::Application::new(
|
"home.local.PlayMat",
|
||||||
"home.local.PlayMat",
|
gio::ApplicationFlags::FLAGS_NONE,
|
||||||
gio::ApplicationFlags::FLAGS_NONE
|
)
|
||||||
).expect("Failed to build Application"),
|
.expect("Failed to build Application"),
|
||||||
state: Default::default(),
|
state: Default::default(),
|
||||||
};
|
};
|
||||||
app.connect_all();
|
app.connect_all();
|
||||||
Ok(app)
|
Ok(app)
|
||||||
}
|
}
|
||||||
@@ -58,45 +57,39 @@ impl App {
|
|||||||
fn connect_all(&self) {
|
fn connect_all(&self) {
|
||||||
let app_state = self.state.clone();
|
let app_state = self.state.clone();
|
||||||
self.inner.connect_startup(|_| {});
|
self.inner.connect_startup(|_| {});
|
||||||
self.inner.connect_activate(
|
self.inner.connect_activate(move |app| {
|
||||||
move |app| {
|
println!("Activate App");
|
||||||
println!("Activate App");
|
let main_src = include_str!("../res/main.glade");
|
||||||
let main_src = include_str!("../res/main.glade");
|
let builder = gtk::Builder::new_from_string(main_src);
|
||||||
let builder = gtk::Builder::new_from_string(main_src);
|
let win: gtk::ApplicationWindow = builder.get_object("app").unwrap();
|
||||||
let win: gtk::ApplicationWindow =
|
win.set_application(app);
|
||||||
builder
|
// Set up a simple switch for the Pawn list panel
|
||||||
.get_object("app")
|
let panel: gtk::Paned = builder.get_object("panel").unwrap();
|
||||||
.unwrap();
|
Self::new_action(app, "panel_switch", move |_, _| {
|
||||||
win.set_application(app);
|
if panel.get_position() == 0 {
|
||||||
// Set up a simple switch for the Pawn list panel
|
panel.set_position(360);
|
||||||
let panel: gtk::Paned = builder.get_object("panel").unwrap();
|
} else {
|
||||||
Self::new_action(app, "panel_switch", move |_,_| {
|
panel.set_position(0);
|
||||||
if panel.get_position() == 0 {
|
|
||||||
panel.set_position(360);
|
|
||||||
} else {
|
|
||||||
panel.set_position(0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Pawn list
|
|
||||||
let pawn_list = builder.get_object("pawn_list").unwrap();
|
|
||||||
let pawn_list = pawn::PawnList::init(pawn_list);
|
|
||||||
|
|
||||||
for pawn in pawn::pawn_factory() {
|
|
||||||
pawn_list.add(&pawn);
|
|
||||||
pawn.connect_place(app_state.clone());
|
|
||||||
pawn.connect_stats();
|
|
||||||
app_state.borrow_mut().pawns.push(pawn);
|
|
||||||
}
|
}
|
||||||
win.show_all(); // Before grid because we want to hide some widgets there
|
});
|
||||||
// Initialize grid
|
// Pawn list
|
||||||
let grid: gtk::Grid = builder.get_object("map").unwrap();
|
let pawn_list = builder.get_object("pawn_list").unwrap();
|
||||||
// TODO: implement drag-drop events
|
let pawn_list = pawn::PawnList::init(pawn_list);
|
||||||
// * 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, app_state.clone());
|
|
||||||
|
|
||||||
|
for pawn in pawn::pawn_factory() {
|
||||||
|
pawn_list.add(&pawn);
|
||||||
|
pawn.connect_place(app_state.clone());
|
||||||
|
pawn.connect_stats();
|
||||||
|
app_state.borrow_mut().pawns.push(pawn);
|
||||||
}
|
}
|
||||||
);
|
win.show_all(); // Before grid because we want to hide some widgets there
|
||||||
|
// 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, app_state.clone());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a simple action and connects the given handler to its activate signal.
|
/// Creates a simple action and connects the given handler to its activate signal.
|
||||||
@@ -119,13 +112,13 @@ impl App {
|
|||||||
fn main() {
|
fn main() {
|
||||||
if gtk::init().is_err() {
|
if gtk::init().is_err() {
|
||||||
println!("Failed to initialize Gtk");
|
println!("Failed to initialize Gtk");
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
let exit_code = match App::new() {
|
let exit_code = match App::new() {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("Error !\n{}", e);
|
println!("Error !\n{}", e);
|
||||||
1
|
1
|
||||||
},
|
}
|
||||||
Ok(app) => app.run(&args().collect::<Vec<_>>()),
|
Ok(app) => app.run(&args().collect::<Vec<_>>()),
|
||||||
};
|
};
|
||||||
process::exit(exit_code);
|
process::exit(exit_code);
|
||||||
|
|||||||
39
src/pawn.rs
39
src/pawn.rs
@@ -1,8 +1,7 @@
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
/// A wrapper around gtk::ListBox for Pawns
|
/// A wrapper around gtk::ListBox for Pawns
|
||||||
#[derive(Debug,Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PawnList {
|
pub struct PawnList {
|
||||||
inner: gtk::ListBox,
|
inner: gtk::ListBox,
|
||||||
}
|
}
|
||||||
@@ -10,7 +9,7 @@ pub struct PawnList {
|
|||||||
impl PawnList {
|
impl PawnList {
|
||||||
/// Initialize from the given existing ListBox
|
/// Initialize from the given existing ListBox
|
||||||
pub fn init(inner: gtk::ListBox) -> Self {
|
pub fn init(inner: gtk::ListBox) -> Self {
|
||||||
PawnList{ inner }
|
PawnList { inner }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a Pawn to the list
|
/// Adds a Pawn to the list
|
||||||
@@ -22,7 +21,7 @@ impl PawnList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Application data related to a Pawn
|
/// Application data related to a Pawn
|
||||||
#[derive(Debug,Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PawnData {
|
pub struct PawnData {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub position: Option<String>, // Content of label from CellWidget
|
pub position: Option<String>, // Content of label from CellWidget
|
||||||
@@ -42,14 +41,14 @@ impl Pawn {
|
|||||||
let pawn_src = include_str!("../res/pawn.glade");
|
let pawn_src = include_str!("../res/pawn.glade");
|
||||||
let builder = gtk::Builder::new_from_string(pawn_src);
|
let builder = gtk::Builder::new_from_string(pawn_src);
|
||||||
let name = name.into();
|
let name = name.into();
|
||||||
let label: gtk::Label =
|
let label: gtk::Label = builder.get_object("name").unwrap();
|
||||||
builder
|
|
||||||
.get_object("name")
|
|
||||||
.unwrap();
|
|
||||||
label.set_text(&name);
|
label.set_text(&name);
|
||||||
let widget: gtk::Box = builder.get_object("pawn").unwrap();
|
let widget: gtk::Box = builder.get_object("pawn").unwrap();
|
||||||
Pawn {
|
Pawn {
|
||||||
data: PawnData{ name, position: None, },
|
data: PawnData {
|
||||||
|
name,
|
||||||
|
position: None,
|
||||||
|
},
|
||||||
widget,
|
widget,
|
||||||
place_btn: builder.get_object("place_btn").unwrap(),
|
place_btn: builder.get_object("place_btn").unwrap(),
|
||||||
stats_btn: builder.get_object("stats_btn").unwrap(),
|
stats_btn: builder.get_object("stats_btn").unwrap(),
|
||||||
@@ -59,23 +58,17 @@ impl Pawn {
|
|||||||
pub fn connect_place(&self, state: AppState) {
|
pub fn connect_place(&self, state: AppState) {
|
||||||
let name = self.data.name.clone();
|
let name = self.data.name.clone();
|
||||||
let data = self.data.clone();
|
let data = self.data.clone();
|
||||||
self.place_btn
|
self.place_btn.connect_clicked(move |_| {
|
||||||
.connect_clicked(
|
println!("Placing {}...", name);
|
||||||
move |_| {
|
let mut state = state.0.borrow_mut();
|
||||||
println!("Placing {}...", name);
|
state.pending = Some(data.clone()); // ???
|
||||||
let mut state = state.0.borrow_mut();
|
});
|
||||||
state.pending = Some(data.clone()); // ???
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn connect_stats(&self) {
|
pub fn connect_stats(&self) {
|
||||||
self.stats_btn
|
self.stats_btn.connect_clicked(move |_| {
|
||||||
.connect_clicked(
|
println!("Showing stats...");
|
||||||
move |_| {
|
});
|
||||||
println!("Showing stats...");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user