aboutsummaryrefslogtreecommitdiff
path: root/src/client_main.rs
diff options
context:
space:
mode:
authorArne Dußin2021-01-27 14:01:50 +0100
committerArne Dußin2021-02-02 22:16:15 +0100
commitf92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch)
tree20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/client_main.rs
parentc60a6d07efb120724b308e29e8e70f27c87c952d (diff)
downloadgraf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz
graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/client_main.rs')
-rw-r--r--src/client_main.rs178
1 files changed, 65 insertions, 113 deletions
diff --git a/src/client_main.rs b/src/client_main.rs
index 22251c3..e7d1a2d 100644
--- a/src/client_main.rs
+++ b/src/client_main.rs
@@ -21,135 +21,87 @@
#[macro_use]
extern crate log;
-pub mod cli;
-pub mod colours;
-pub mod config;
-pub mod editor;
-pub mod grid;
-pub mod gui;
-pub mod input;
-pub mod map;
+pub mod client;
pub mod math;
-pub mod snapping;
+pub mod net;
+pub mod server;
pub mod stable_vec;
-pub mod svg;
-pub mod tool;
-pub mod transform;
pub mod transformable;
+pub mod world;
-use cli::CLI;
-use config::Config;
-use editor::Editor;
-use float_cmp::F64Margin;
-use gui::{DimensionIndicator, ToolSidebar};
-use input::Input;
-use raylib::prelude::*;
-use snapping::Snapper;
-use std::ffi::CString;
-use std::io;
-use transform::Transform;
-
-/// Location of the file containing the style used for the raylib user interface.
-pub const GUI_STYLE: &str = "assets/style/cyber.rgs";
-/// Location of the graf karto configuration options file.
-pub const CONFIG_FILE: &str = "config.ron";
-
-/// The acceptable error that is used throughout the project for two floats to be considered equal.
-/// If it is set too low, the editor might not work properly, if set too high, the granularity may
-/// become too low for certain purposes.
-pub const FLOAT_MARGIN: F64Margin = F64Margin {
- epsilon: 10000. * f64::EPSILON,
- ulps: 0,
-};
+use clap::{App, Arg};
+use server::DEFAULT_PORT;
+use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
fn main() {
pretty_env_logger::init();
- let (mut rl, thread) = raylib::init().resizable().title("Hello there!").build();
- rl.set_target_fps(120);
- rl.set_exit_key(None);
-
- // Load the configuration file, if available.
- let config = match Config::from_file(CONFIG_FILE) {
- Ok(config) => config,
- Err(err) => {
- /* Create a default config file if it doesn't exist, otherwise leave the incorrectly
- * formatted/corrupted or otherwise unreadable file alone.
- */
- let config = Config::default();
- if err.kind() == io::ErrorKind::NotFound {
- warn!("Could not find a configuration file. Creating default.");
- config
- .write_file(CONFIG_FILE)
- .expect("Could not write config file.");
- } else {
- error!(
- "Could not read configuration file: {}\nUsing defaults for this run.",
- err
- );
- }
+ println!("Graf Karto version: {}", clap::crate_version!());
+ let default_port = DEFAULT_PORT.to_string();
+ let matches = App::new("Graf Karto")
+ .version(clap::crate_version!())
+ .author(clap::crate_authors!())
+ .about(clap::crate_description!())
+ .arg(Arg::with_name("connect")
+ .short("c")
+ .value_name("SERVER_ADDRESS")
+ .help("Specify an IP in case an external server should be used (starts a local server if not set)."))
+ .arg(Arg::with_name("port")
+ .short("p")
+ .value_name("SERVER_PORT")
+ .help("Set the port the server listens on or should listen on. When starting a local server, others may be tried if it cannot be bound.")
+ .default_value(&default_port))
+ .arg(Arg::with_name("ipv4")
+ .short("v4")
+ .help("Use virgin IPv4 instead of chad IPv6.. you monster"))
+ .get_matches();
- config
+ let use_ipv4 = matches.is_present("ipv4");
+ let mut server_port = match matches
+ .value_of("port")
+ .expect("No port found, eventhough it should have a default value")
+ .parse::<u16>()
+ {
+ Ok(port) => port,
+ Err(e) => {
+ error!("Not a valid server port: {:?}", e);
+ warn!("Using default port {}", DEFAULT_PORT);
+ DEFAULT_PORT
}
};
- // Load the preferred gui style
- rl.gui_load_style(Some(
- &CString::new(GUI_STYLE).expect("Could not create C string from style file name"),
- ));
-
- let mut input = Input::new(&rl);
- config::register_bindings(&config, &mut input);
- let mut editor = Editor::new(&mut rl, &thread, config);
- let mut dimension_indicator = DimensionIndicator::new();
- let mut tool_sidebar = ToolSidebar::new(&mut rl, &thread, &mut input);
- let mut snapper = Snapper::default();
- let mut cli = CLI::new(&mut input);
-
- let mut transform = Transform::new();
- let mut last_mouse_pos = rl.get_mouse_position();
- while !rl.window_should_close() {
- let screen_width = rl.get_screen_width();
- let screen_height = rl.get_screen_height();
-
- input.update(&mut rl);
-
- // Move the canvas together with the mouse
- if rl.is_mouse_button_down(MouseButton::MOUSE_MIDDLE_BUTTON) {
- transform.move_by_px(&(rl.get_mouse_position() - last_mouse_pos).into());
+ let server_address = match matches.value_of("connect") {
+ None => {
+ // Local server will be started.
+ if use_ipv4 {
+ IpAddr::V4(Ipv4Addr::LOCALHOST)
+ } else {
+ IpAddr::V6(Ipv6Addr::LOCALHOST)
+ }
}
- // Update the last mouse position
- last_mouse_pos = rl.get_mouse_position();
-
- let mouse_wheel_move = rl.get_mouse_wheel_move();
- if mouse_wheel_move != 0. {
- // Zoom in for positive and zoom out for negative mouse wheel rotations.
- let scale_factor = if mouse_wheel_move > 0. { 1.2 } else { 1. / 1.2 };
- transform.try_zoom(
- &rl.get_mouse_position().into(),
- mouse_wheel_move.abs() as f64 * scale_factor,
- );
+ Some(addr) => {
+ if use_ipv4 {
+ IpAddr::V4(dbg!(addr).parse().expect("Not a valid IPv4 address"))
+ } else {
+ IpAddr::V6(dbg!(addr).parse().expect("Not a valid IPv6 address"))
+ }
}
+ };
- cli.update(&mut editor, &mut input);
- dimension_indicator.update(editor.map_mut(), &mut rl);
- snapper.update(&mut rl, cli.active());
- editor.update(&mut rl, &transform, &snapper, &mut input);
- tool_sidebar.update(screen_height as u16, &mut input);
+ let server_handle = if !matches.is_present("connect") {
+ Some({
+ let (server_handle, port) =
+ server::start_any_port(use_ipv4).expect("Unable to start local server");
+ server_port = port;
+ server_handle
+ })
+ } else {
+ None
+ };
- // Drawing section
- {
- let mut d = rl.begin_drawing(&thread);
- d.clear_background(Color::BLACK);
- grid::draw_grid(&mut d, screen_width, screen_height, &transform);
- editor.map().draw(&mut d, &transform);
+ client::run(SocketAddr::new(server_address, server_port));
- editor.draw_tools(&mut d, &transform);
- tool_sidebar.draw(&mut d, &mut editor);
- snapper.draw(&mut d);
- gui::position_indicator_draw(&mut d, last_mouse_pos.into(), &transform, &snapper);
- dimension_indicator.draw(&mut d, &transform);
- cli.draw(&mut d);
- }
+ if let Some(handle) = server_handle {
+ handle.join().expect("Server thread closed unexpectedly.");
}
}