Add edges
This commit is contained in:
parent
100366957b
commit
4c2cb18819
83
src/main.rs
83
src/main.rs
@ -85,6 +85,49 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////// Dispatching Functions ////////////////////
|
||||||
|
|
||||||
|
mod dispatch {
|
||||||
|
use super::*;
|
||||||
|
type Coords = (f32, f32);
|
||||||
|
pub(super) fn node_press(state: &mut AppState, coords: Coords) {
|
||||||
|
let (pos_x, pos_y) = coords;
|
||||||
|
if app::event_button() == 1 {
|
||||||
|
let name = dialog::input_default("Node", "");
|
||||||
|
if let Some(name) = name {
|
||||||
|
state.board.add_node(pos_x, pos_y, name);
|
||||||
|
}
|
||||||
|
} else if app::event_button() == 3 {
|
||||||
|
let id = state.board.nearest_node(pos_x, pos_y);
|
||||||
|
if let Some(id) = id {
|
||||||
|
state.board.remove_node(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
app::redraw();
|
||||||
|
}
|
||||||
|
pub(super) fn edge_press(state: &mut AppState, coords: Coords) {
|
||||||
|
let (pos_x, pos_y) = coords;
|
||||||
|
if app::event_button() == 1 {
|
||||||
|
match (state.selected_node, state.board.nearest_node(pos_x, pos_y)) {
|
||||||
|
(Some(selected), Some(nearest)) => {
|
||||||
|
if selected == nearest {
|
||||||
|
state.selected_node = None;
|
||||||
|
} else {
|
||||||
|
let node = state.board.nodes.get_mut(&selected).unwrap();
|
||||||
|
node.edges.insert(nearest);
|
||||||
|
}
|
||||||
|
app::redraw();
|
||||||
|
}
|
||||||
|
(None, Some(nearest)) => {
|
||||||
|
state.selected_node = Some(nearest);
|
||||||
|
app::redraw();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//////////////////// Utility Functions ////////////////////
|
//////////////////// Utility Functions ////////////////////
|
||||||
|
|
||||||
fn encode_png(image: &DynamicImage) -> Vec<u8> {
|
fn encode_png(image: &DynamicImage) -> Vec<u8> {
|
||||||
@ -259,15 +302,30 @@ fn main() {
|
|||||||
frame.draw(move |f| {
|
frame.draw(move |f| {
|
||||||
use draw::*;
|
use draw::*;
|
||||||
let mut state = state_clone.borrow_mut();
|
let mut state = state_clone.borrow_mut();
|
||||||
|
// Background
|
||||||
let image = &mut state.image;
|
let image = &mut state.image;
|
||||||
if let Some(image) = image.as_mut() {
|
if let Some(image) = image.as_mut() {
|
||||||
image.scale(f.w(), f.h(), false, true);
|
image.scale(f.w(), f.h(), false, true);
|
||||||
image.draw(f.x(), f.y(), f.w(), f.h());
|
image.draw(f.x(), f.y(), f.w(), f.h());
|
||||||
}
|
}
|
||||||
|
// Nodes
|
||||||
let board = &state.board;
|
let board = &state.board;
|
||||||
for node in board.nodes.values() {
|
for (&id, node) in &board.nodes {
|
||||||
|
// Draw the node
|
||||||
let (x, y) = f.from_coords(node.x, node.y);
|
let (x, y) = f.from_coords(node.x, node.y);
|
||||||
draw_text(&node.name, x, y);
|
if state.selected_node == Some(id) {
|
||||||
|
set_draw_color(Color::Red);
|
||||||
|
draw_text(&node.name, x, y);
|
||||||
|
set_draw_color(Color::Black);
|
||||||
|
} else {
|
||||||
|
draw_text(&node.name, x, y);
|
||||||
|
}
|
||||||
|
// Draw edges
|
||||||
|
for other_id in &node.edges {
|
||||||
|
let other = board.nodes.get(other_id).unwrap();
|
||||||
|
let (x1, y1) = f.from_coords(other.x, other.y);
|
||||||
|
draw_line(x, y, x1, y1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let state_clone = Rc::clone(&state);
|
let state_clone = Rc::clone(&state);
|
||||||
@ -275,26 +333,11 @@ fn main() {
|
|||||||
match e {
|
match e {
|
||||||
Event::Push => {
|
Event::Push => {
|
||||||
let mut state = state_clone.borrow_mut();
|
let mut state = state_clone.borrow_mut();
|
||||||
let (pos_x, pos_y) = f.to_coords(app::event_x(), app::event_y());
|
let coords = f.to_coords(app::event_x(), app::event_y());
|
||||||
|
|
||||||
match state.edit_mode {
|
match state.edit_mode {
|
||||||
EditMode::Node => {
|
EditMode::Node => dispatch::node_press(&mut state, coords),
|
||||||
if app::event_button() == 1 {
|
EditMode::Edge => dispatch::edge_press(&mut state, coords),
|
||||||
let name = dialog::input_default("Node", "");
|
|
||||||
if let Some(name) = name {
|
|
||||||
state.board.add_node(pos_x, pos_y, name);
|
|
||||||
}
|
|
||||||
} else if app::event_button() == 3 {
|
|
||||||
let id = state.board.nearest_node(pos_x, pos_y);
|
|
||||||
if let Some(id) = id {
|
|
||||||
state.board.remove_node(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
app::redraw();
|
|
||||||
},
|
|
||||||
EditMode::Edge => {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user