Add edges
This commit is contained in:
parent
100366957b
commit
4c2cb18819
81
src/main.rs
81
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 ////////////////////
|
||||
|
||||
fn encode_png(image: &DynamicImage) -> Vec<u8> {
|
||||
@ -259,15 +302,30 @@ fn main() {
|
||||
frame.draw(move |f| {
|
||||
use draw::*;
|
||||
let mut state = state_clone.borrow_mut();
|
||||
// Background
|
||||
let image = &mut state.image;
|
||||
if let Some(image) = image.as_mut() {
|
||||
image.scale(f.w(), f.h(), false, true);
|
||||
image.draw(f.x(), f.y(), f.w(), f.h());
|
||||
}
|
||||
// Nodes
|
||||
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);
|
||||
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);
|
||||
@ -275,26 +333,11 @@ fn main() {
|
||||
match e {
|
||||
Event::Push => {
|
||||
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 {
|
||||
EditMode::Node => {
|
||||
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();
|
||||
},
|
||||
EditMode::Edge => {
|
||||
todo!();
|
||||
}
|
||||
EditMode::Node => dispatch::node_press(&mut state, coords),
|
||||
EditMode::Edge => dispatch::edge_press(&mut state, coords),
|
||||
}
|
||||
true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user