Compare commits
5 Commits
8f4635eaba
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 32ffe82815 | |||
| a1412f03d2 | |||
| b2d372f8a7 | |||
| 96b6c523d7 | |||
| de02b27949 |
@@ -27,11 +27,11 @@
|
||||
<property name="orientation">vertical</property>
|
||||
<property name="homogeneous">True</property>
|
||||
<child>
|
||||
<object class="GtkLabel" id="header">
|
||||
<object class="GtkLabel" id="name">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="events">GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK | GDK_PROPERTY_CHANGE_MASK | GDK_VISIBILITY_NOTIFY_MASK | GDK_PROXIMITY_IN_MASK | GDK_PROXIMITY_OUT_MASK | GDK_SUBSTRUCTURE_MASK | GDK_SCROLL_MASK | GDK_TOUCH_MASK | GDK_SMOOTH_SCROLL_MASK | GDK_TOUCHPAD_GESTURE_MASK | GDK_TABLET_PAD_MASK</property>
|
||||
<property name="label" translatable="yes">Cell</property>
|
||||
<property name="label" translatable="yes">Name</property>
|
||||
<property name="justify">right</property>
|
||||
<attributes>
|
||||
<attribute name="weight" value="bold"/>
|
||||
@@ -53,10 +53,16 @@
|
||||
<property name="valign">end</property>
|
||||
<property name="margin_right">5</property>
|
||||
<property name="margin_bottom">5</property>
|
||||
<property name="relief">none</property>
|
||||
<property name="direction">none</property>
|
||||
<property name="popover">desc_sheet</property>
|
||||
<child>
|
||||
<placeholder/>
|
||||
<object class="GtkImage">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">accessories-dictionary-symbolic</property>
|
||||
<property name="icon_size">3</property>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
|
||||
169
res/main.glade
169
res/main.glade
@@ -7,6 +7,18 @@
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">contact-new-symbolic</property>
|
||||
</object>
|
||||
<object class="GtkImage" id="image1">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">preferences-system-symbolic</property>
|
||||
</object>
|
||||
<object class="GtkTextBuffer" id="pawn_desc"/>
|
||||
<object class="GtkImage" id="show_list_img">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">view-list-bullet-symbolic</property>
|
||||
<property name="icon_size">1</property>
|
||||
</object>
|
||||
<object class="GtkApplicationWindow" id="app">
|
||||
<property name="can_focus">False</property>
|
||||
<property name="events">GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK | GDK_PROPERTY_CHANGE_MASK | GDK_VISIBILITY_NOTIFY_MASK | GDK_PROXIMITY_IN_MASK | GDK_PROXIMITY_OUT_MASK | GDK_SUBSTRUCTURE_MASK | GDK_SCROLL_MASK | GDK_TOUCH_MASK | GDK_SMOOTH_SCROLL_MASK | GDK_TOUCHPAD_GESTURE_MASK | GDK_TABLET_PAD_MASK</property>
|
||||
@@ -16,30 +28,55 @@
|
||||
<property name="can_focus">False</property>
|
||||
<property name="title" translatable="yes">Play Mat</property>
|
||||
<property name="subtitle" translatable="yes">version 0.1</property>
|
||||
<property name="spacing">0</property>
|
||||
<property name="spacing">10</property>
|
||||
<property name="show_close_button">True</property>
|
||||
<child>
|
||||
<object class="GtkToggleButton" id="show_panel">
|
||||
<object class="GtkButtonBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="halign">start</property>
|
||||
<property name="valign">start</property>
|
||||
<property name="action_name">app.panel_switch</property>
|
||||
<property name="image">show_list_img</property>
|
||||
<property name="always_show_image">True</property>
|
||||
<property name="active">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="layout_style">expand</property>
|
||||
<child>
|
||||
<object class="GtkToggleButton" id="show_panel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="action_name">app.panel_switch</property>
|
||||
<property name="image">show_list_img</property>
|
||||
<property name="always_show_image">True</property>
|
||||
<property name="active">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">True</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="add_pawn">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="image">add_pawn_img</property>
|
||||
<property name="always_show_image">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">True</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="add_btn">
|
||||
<object class="GtkButton" id="options_btn">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="halign">start</property>
|
||||
<property name="valign">center</property>
|
||||
<property name="image">add_pawn_img</property>
|
||||
<property name="image">image1</property>
|
||||
<property name="always_show_image">True</property>
|
||||
<style>
|
||||
<class name="destructive-action"/>
|
||||
</style>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="position">1</property>
|
||||
@@ -51,7 +88,7 @@
|
||||
<object class="GtkPaned" id="panel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="position">80</property>
|
||||
<property name="position">160</property>
|
||||
<child>
|
||||
<object class="GtkListBox" id="pawn_list">
|
||||
<property name="visible">True</property>
|
||||
@@ -135,10 +172,104 @@
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
<object class="GtkImage" id="show_list_img">
|
||||
<property name="visible">True</property>
|
||||
<object class="GtkWindow" id="new_pawn_dialog">
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">view-list-bullet-symbolic</property>
|
||||
<property name="icon_size">1</property>
|
||||
<property name="modal">True</property>
|
||||
<property name="deletable">False</property>
|
||||
<property name="transient_for">app</property>
|
||||
<child type="titlebar">
|
||||
<object class="GtkHeaderBar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="title" translatable="yes">Ajouter un personnage</property>
|
||||
<child>
|
||||
<object class="GtkButton" id="confirm">
|
||||
<property name="label">gtk-apply</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="use_stock">True</property>
|
||||
<property name="always_show_image">True</property>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="abort">
|
||||
<property name="label">gtk-cancel</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="use_stock">True</property>
|
||||
<property name="always_show_image">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkGrid">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="margin_left">5</property>
|
||||
<property name="margin_right">5</property>
|
||||
<property name="margin_top">5</property>
|
||||
<property name="margin_bottom">5</property>
|
||||
<property name="row_spacing">5</property>
|
||||
<child>
|
||||
<object class="GtkEntry" id="name_entry">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="width_chars">40</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left_attach">1</property>
|
||||
<property name="top_attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel" id="name_label">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="label" translatable="yes">Name</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left_attach">0</property>
|
||||
<property name="top_attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel" id="desc_label">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="valign">start</property>
|
||||
<property name="label" translatable="yes">Description</property>
|
||||
<property name="ellipsize">end</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left_attach">0</property>
|
||||
<property name="top_attach">1</property>
|
||||
<property name="height">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkTextView" id="desc_entry">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="margin_top">5</property>
|
||||
<property name="margin_bottom">5</property>
|
||||
<property name="vexpand">True</property>
|
||||
<property name="wrap_mode">word</property>
|
||||
<property name="buffer">pawn_desc</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left_attach">1</property>
|
||||
<property name="top_attach">1</property>
|
||||
<property name="height">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</interface>
|
||||
|
||||
@@ -2,15 +2,26 @@
|
||||
<!-- Generated with glade 3.22.1 -->
|
||||
<interface>
|
||||
<requires lib="gtk+" version="3.20"/>
|
||||
<object class="GtkImage" id="image1">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">send-to-symbolic</property>
|
||||
</object>
|
||||
<object class="GtkImage" id="image2">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">accessories-calculator-symbolic</property>
|
||||
</object>
|
||||
<object class="GtkBox" id="pawn">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="margin_top">10</property>
|
||||
<property name="homogeneous">True</property>
|
||||
<child>
|
||||
<object class="GtkLabel" id="name">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="margin_left">10</property>
|
||||
<property name="hexpand">True</property>
|
||||
<property name="label" translatable="yes">Name</property>
|
||||
</object>
|
||||
<packing>
|
||||
@@ -24,16 +35,19 @@
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="margin_left">8</property>
|
||||
<property name="spacing">3</property>
|
||||
<property name="layout_style">start</property>
|
||||
<property name="valign">center</property>
|
||||
<property name="margin_right">5</property>
|
||||
<property name="homogeneous">True</property>
|
||||
<property name="layout_style">expand</property>
|
||||
<child>
|
||||
<object class="GtkButton" id="place_btn">
|
||||
<property name="label" translatable="yes">Place</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="image">image1</property>
|
||||
<property name="relief">half</property>
|
||||
<property name="always_show_image">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
@@ -43,11 +57,13 @@
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="stats_btn">
|
||||
<property name="label" translatable="yes">Stats</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="image">image2</property>
|
||||
<property name="relief">half</property>
|
||||
<property name="always_show_image">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">True</property>
|
||||
|
||||
135
src/grid.rs
135
src/grid.rs
@@ -4,6 +4,7 @@ pub struct Grid {
|
||||
inner: gtk::Grid,
|
||||
}
|
||||
|
||||
/// Position of a cell as (x,y) tuple.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct CellPosition(i32, i32);
|
||||
|
||||
@@ -22,74 +23,119 @@ impl Grid {
|
||||
}
|
||||
}
|
||||
|
||||
/// A single cell of the grid
|
||||
struct Cell {
|
||||
/// The inner widget
|
||||
inner: CellWidget,
|
||||
position: CellPosition,
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
fn new(position: CellPosition) -> Self {
|
||||
Cell {
|
||||
inner: CellWidget::new(position),
|
||||
position,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<gtk::EventBox> for Cell {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> >k::EventBox {
|
||||
self.inner.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// The cell widget is either empty or has a pawn
|
||||
/// Pawn can be moved around cells using drag-n-drop. However, due to my inability
|
||||
/// to dynamically change the behavior with set/unset methods, all cells are always
|
||||
/// drag and drop at once.
|
||||
/// It is usable though, just weird. When you grab an empty cell you'll make a button appear.
|
||||
/// Know that if you drop a cell on itself, it will empty itself !
|
||||
#[derive(Debug, Clone)]
|
||||
struct CellWidget {
|
||||
eventbox: gtk::EventBox,
|
||||
position: gtk::Label,
|
||||
header: gtk::Label,
|
||||
name: gtk::Label,
|
||||
desc_btn: gtk::Button,
|
||||
targets: Vec<gtk::TargetEntry>,
|
||||
}
|
||||
|
||||
impl CellWidget {
|
||||
fn new(pos: CellPosition, header_text: &str) -> Self {
|
||||
fn new(pos: CellPosition) -> Self {
|
||||
let cell_src = include_str!("../res/cell.glade");
|
||||
let builder = gtk::Builder::new_from_string(cell_src);
|
||||
// Retrieve children
|
||||
let eventbox: gtk::EventBox = builder.get_object("cell").unwrap();
|
||||
let header: gtk::Label = builder.get_object("header").unwrap();
|
||||
header.set_text(header_text);
|
||||
let position: gtk::Label = builder.get_object("position").unwrap();
|
||||
position.set_text(&format!("{}x{}", pos.0, pos.1));
|
||||
let desc_btn: gtk::Button = builder.get_object("desc_btn").unwrap();
|
||||
desc_btn.set_visible(false);
|
||||
let cell = CellWidget {
|
||||
eventbox,
|
||||
position,
|
||||
header,
|
||||
desc_btn,
|
||||
eventbox: builder.get_object("cell").unwrap(),
|
||||
position: builder.get_object("position").unwrap(),
|
||||
name: builder.get_object("name").unwrap(),
|
||||
desc_btn: builder.get_object("desc_btn").unwrap(),
|
||||
targets: vec![
|
||||
gtk::TargetEntry::new("text/plain;charset=utf-8", gtk::TargetFlags::SAME_APP, 0),
|
||||
],
|
||||
};
|
||||
// Drag-and-drop capacity
|
||||
// The data to be sent
|
||||
let targets = vec![gtk::TargetEntry::new(
|
||||
"text/plain",
|
||||
gtk::TargetFlags::SAME_APP,
|
||||
0,
|
||||
)];
|
||||
cell.desc_btn.set_visible(false);
|
||||
cell.position.set_text(&format!("{}x{}", pos.0, pos.1));
|
||||
cell.set_drag();
|
||||
cell.set_drop();
|
||||
Self::set_content(&cell, None);
|
||||
cell
|
||||
}
|
||||
|
||||
fn unset_drag(&self) { dbg!("Unset drag"); self.eventbox.drag_source_unset(); }
|
||||
|
||||
fn set_drag(&self) {
|
||||
dbg!("Set drag");
|
||||
// Acting as source
|
||||
cell.eventbox.drag_source_set(
|
||||
self.eventbox.drag_source_set(
|
||||
gdk::ModifierType::MODIFIER_MASK,
|
||||
&targets,
|
||||
&self.targets,
|
||||
gdk::DragAction::MOVE,
|
||||
);
|
||||
let h = cell.header.clone();
|
||||
let c = self.clone();
|
||||
// Send data to the drop site
|
||||
cell.eventbox
|
||||
self.eventbox
|
||||
.connect_drag_data_get(move |_, _, data, info, time| {
|
||||
println!("Send...");
|
||||
// TODO: Refactoring, this is the inverse of 'placing',
|
||||
// building a PawnData instead of destructuring it.
|
||||
if let Some(to_send) = h.get_text() {
|
||||
if let Some(to_send) = c.name
|
||||
.get_text()
|
||||
{
|
||||
dbg!(&to_send);
|
||||
data.set_text(&to_send);
|
||||
}
|
||||
else {
|
||||
dbg!("Should not happen !!");
|
||||
}
|
||||
});
|
||||
let c = cell.clone();
|
||||
// Empty the cell on successfull move
|
||||
cell.eventbox.connect_drag_data_delete(move |w, drag| {
|
||||
let c = self.clone();
|
||||
self.eventbox.connect_drag_data_delete(move |w, drag| {
|
||||
println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded()));
|
||||
Self::set_content(&c, None);
|
||||
});
|
||||
}
|
||||
|
||||
fn unset_drop(&self) { dbg!("Unset drop"); self.eventbox.drag_dest_unset(); }
|
||||
|
||||
fn set_drop(&self) {
|
||||
dbg!("Set drop");
|
||||
// Acting as destination
|
||||
cell.eventbox.drag_dest_set(
|
||||
self.eventbox.drag_dest_set(
|
||||
gtk::DestDefaults::ALL,
|
||||
&targets,
|
||||
&self.targets,
|
||||
gdk::DragAction::MOVE,
|
||||
);
|
||||
// Retrieve data from the source
|
||||
let c = cell.clone();
|
||||
cell.eventbox
|
||||
let c = self.clone();
|
||||
self.eventbox
|
||||
.connect_drag_data_received(move |w, _, _, _, data, _, _| {
|
||||
// Check if cell is not already occupied
|
||||
if !(c.header.get_text().unwrap().as_str() == "") {
|
||||
if !(c.name.get_text().unwrap().as_str() == "") {
|
||||
// TODO: Find what to do to abort, since
|
||||
// DragContext methods don't seem to work...
|
||||
println!("Overriding !!");
|
||||
@@ -102,7 +148,6 @@ impl CellWidget {
|
||||
}
|
||||
};
|
||||
});
|
||||
cell
|
||||
}
|
||||
|
||||
/// Updates content of the Cell
|
||||
@@ -110,10 +155,10 @@ impl CellWidget {
|
||||
println!("Set content to {:?}", &data);
|
||||
// TODO: there is surely something cleaner...
|
||||
if let Some(name) = data {
|
||||
self.header.set_text(name);
|
||||
self.name.set_text(name);
|
||||
self.desc_btn.set_visible(true);
|
||||
} else {
|
||||
self.header.set_text("");
|
||||
self.name.set_text("");
|
||||
self.desc_btn.set_visible(false);
|
||||
};
|
||||
}
|
||||
@@ -138,23 +183,3 @@ impl AsRef<gtk::EventBox> for CellWidget {
|
||||
}
|
||||
}
|
||||
|
||||
struct Cell {
|
||||
inner: CellWidget,
|
||||
position: CellPosition,
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
fn new(position: CellPosition) -> Self {
|
||||
Cell {
|
||||
inner: CellWidget::new(position, ""),
|
||||
position,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<gtk::EventBox> for Cell {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> >k::EventBox {
|
||||
self.inner.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
99
src/main.rs
99
src/main.rs
@@ -20,8 +20,74 @@ mod pawn;
|
||||
pub struct AppData {
|
||||
/// A slot for any pawn waiting for placement
|
||||
pending: Option<pawn::PawnData>,
|
||||
/// Owned list of pawns
|
||||
pawns: Vec<pawn::Pawn>,
|
||||
pawns: Option<pawn::PawnList>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
struct NewPawnDialog {
|
||||
root: gtk::Window,
|
||||
name: gtk::Entry,
|
||||
desc: gtk::TextBuffer,
|
||||
confirm: gtk::Button,
|
||||
abort: gtk::Button,
|
||||
}
|
||||
|
||||
impl NewPawnDialog {
|
||||
|
||||
fn init(builder: >k::Builder, app_state: AppState) -> Self {
|
||||
let dialog = NewPawnDialog {
|
||||
root: builder.get_object("new_pawn_dialog").unwrap(),
|
||||
name: builder.get_object("name_entry").unwrap(),
|
||||
desc: builder.get_object("pawn_desc").unwrap(),
|
||||
confirm: builder.get_object("confirm").unwrap(),
|
||||
abort: builder.get_object("abort").unwrap(),
|
||||
};
|
||||
let d = dialog.clone();
|
||||
dialog.abort.connect_clicked(move |_| { d.reset(); d.hide(); });
|
||||
let d = dialog.clone();
|
||||
dialog.confirm.connect_clicked(move |_| {
|
||||
if let Some(data) = d.take_data() {
|
||||
let pawn = pawn::Pawn::from_data(data);
|
||||
println!("Create {:?}", pawn);
|
||||
app_state.add_pawn(&pawn);
|
||||
d.reset();
|
||||
d.hide();
|
||||
};
|
||||
});
|
||||
dialog
|
||||
}
|
||||
|
||||
fn reset(&self) {
|
||||
self.name.set_text("");
|
||||
self.desc.set_text("");
|
||||
}
|
||||
|
||||
fn take_data(&self) -> Option<pawn::PawnData> {
|
||||
let name = String::from(self.name.get_text().unwrap().as_str());
|
||||
let description = {
|
||||
let (ref start, ref end) = self.desc.get_bounds();
|
||||
String::from(
|
||||
self.desc.get_text(start, end, true).unwrap().as_str()
|
||||
)
|
||||
};
|
||||
if name.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(pawn::PawnData {
|
||||
name,
|
||||
description,
|
||||
position: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn show(&self) {
|
||||
self.root.show_all();
|
||||
}
|
||||
|
||||
fn hide(&self) {
|
||||
self.root.set_visible(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// A sharable state for the whole application
|
||||
@@ -35,6 +101,20 @@ impl std::ops::Deref for AppState {
|
||||
}
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
|
||||
fn add_pawn(&self, pawn: &pawn::Pawn) {
|
||||
pawn.connect_place(self.clone());
|
||||
pawn.connect_stats();
|
||||
self.borrow_mut()
|
||||
.pawns
|
||||
.as_ref()
|
||||
.expect("PawnList was not initialized !")
|
||||
.add(&pawn);
|
||||
dbg!(pawn);
|
||||
}
|
||||
}
|
||||
|
||||
struct App {
|
||||
inner: gtk::Application,
|
||||
state: AppState,
|
||||
@@ -75,13 +155,8 @@ impl App {
|
||||
// Pawn list
|
||||
let pawn_list = builder.get_object("pawn_list").unwrap();
|
||||
let pawn_list = pawn::PawnList::init(pawn_list);
|
||||
app_state.borrow_mut().pawns.replace(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
|
||||
let grid: gtk::Grid = builder.get_object("map").unwrap();
|
||||
@@ -89,9 +164,17 @@ impl App {
|
||||
// * 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());
|
||||
|
||||
let dialog = NewPawnDialog::init(&builder, app_state.clone());
|
||||
let new_pawn_btn: gtk::Button = builder.get_object("add_pawn").unwrap();
|
||||
new_pawn_btn.connect_clicked(move |_| { Self::on_add_pawn(&dialog)});
|
||||
});
|
||||
}
|
||||
|
||||
fn on_add_pawn(win: &NewPawnDialog) {
|
||||
win.show();
|
||||
}
|
||||
|
||||
/// Creates a simple action and connects the given handler to its activate signal.
|
||||
fn new_action<F: Fn(&gio::SimpleAction, &Option<glib::Variant>) + 'static>(
|
||||
app: >k::Application,
|
||||
|
||||
16
src/pawn.rs
16
src/pawn.rs
@@ -17,6 +17,7 @@ impl PawnList {
|
||||
let row = gtk::ListBoxRow::new();
|
||||
row.add(pawn.as_ref());
|
||||
self.inner.add(&row);
|
||||
self.inner.show_all();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +25,7 @@ impl PawnList {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PawnData {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub position: Option<String>, // Content of label from CellWidget
|
||||
}
|
||||
|
||||
@@ -37,7 +39,7 @@ pub struct Pawn {
|
||||
}
|
||||
|
||||
impl Pawn {
|
||||
pub fn new<S: Into<String>>(name: S) -> Self {
|
||||
pub fn new<S: Into<String>>(name: S, description: S) -> Self {
|
||||
let pawn_src = include_str!("../res/pawn.glade");
|
||||
let builder = gtk::Builder::new_from_string(pawn_src);
|
||||
let name = name.into();
|
||||
@@ -47,6 +49,7 @@ impl Pawn {
|
||||
Pawn {
|
||||
data: PawnData {
|
||||
name,
|
||||
description: description.into(),
|
||||
position: None,
|
||||
},
|
||||
widget,
|
||||
@@ -55,13 +58,16 @@ impl Pawn {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_data(data: PawnData) -> Self {
|
||||
Self::new(data.name, data.description)
|
||||
}
|
||||
|
||||
pub fn connect_place(&self, state: AppState) {
|
||||
let name = self.data.name.clone();
|
||||
let data = self.data.clone();
|
||||
self.place_btn.connect_clicked(move |_| {
|
||||
println!("Placing {}...", name);
|
||||
println!("Placing {}...", &data.name);
|
||||
let mut state = state.0.borrow_mut();
|
||||
state.pending = Some(data.clone()); // ???
|
||||
let _ = state.pending.replace(data.clone()); // ???
|
||||
});
|
||||
}
|
||||
|
||||
@@ -81,7 +87,7 @@ impl AsRef<gtk::Box> for Pawn {
|
||||
pub fn pawn_factory() -> Vec<Pawn> {
|
||||
let mut pawns = Vec::with_capacity(3);
|
||||
for name in &["Lomion", "Oilosse", "Fefi"] {
|
||||
pawns.push(Pawn::new(*name));
|
||||
pawns.push(Pawn::new(*name, ""));
|
||||
}
|
||||
pawns
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user