kusa_pixel/
lib.rs

1//! A pixel art painter for people who are sick of GUIs.
2
3// Publish:
4//
5// (1) Version up on Cargo.toml.
6// (2) `cargo run --example example1`
7// (3) `cargo doc --open`
8// (4) Comit to Git-hub.
9// (5) `cargo publish --dry-run`
10// (6) `cargo publish`
11
12extern crate camera_controllers;
13extern crate find_folder;
14extern crate gfx;
15extern crate gfx_device_gl;
16extern crate image;
17extern crate piston_window;
18extern crate rand;
19extern crate serde;
20extern crate serde_json;
21extern crate shader_version;
22extern crate vecmath;
23
24mod data;
25mod grid;
26mod paint_tool;
27mod piston_wrapper;
28mod settings;
29mod window_operation;
30
31use crate::piston_wrapper::kusa_image::write_k_image;
32use crate::piston_wrapper::kusa_image::KusaImage;
33use crate::settings::*;
34use crate::window_operation::*;
35use std::path::Path;
36
37pub fn run() {
38    // 構成(^~^)
39    let app = KusaApp::default();
40
41    // 設定ファイルを読み込もうぜ☆(^~^)
42    let mut settings = match Settings::load(&app.settings_path) {
43        Ok(x) => x,
44        Err(_) => {
45            let settings = Settings::default();
46            settings.save(&app.settings_example_path);
47            settings
48        }
49    };
50
51    println!("Debug   | Load image {}", settings.image_file);
52    // Start by loading the image file
53    let mut k_image = match image::open(Path::new(&settings.image_file)) {
54        Ok(img) => {
55            let k_image = KusaImage::load_image(&img);
56            // Priority is given to the width and height of the image file rather than the configuration file
57            settings.image_width = k_image.width;
58            settings.image_height = k_image.height;
59            k_image
60        }
61        Err(_e) => {
62            // If there is no image file, create a new one with the size specified in the configuration file
63            let mut k_image = KusaImage::new(settings.image_width, settings.image_height);
64            write_k_image(&mut k_image, &settings.image_file);
65            k_image
66        }
67    };
68
69    show_window(&app, settings, &mut k_image);
70}
71
72pub struct KusaApp {
73    settings_path: String,
74    settings_example_path: String,
75}
76impl Default for KusaApp {
77    fn default() -> Self {
78        KusaApp {
79            settings_path: "settings.json".to_string(),
80            settings_example_path: "settings-example.json".to_string(),
81        }
82    }
83}