|
| 1 | +//! Quick module demonstration (Story 8.2) |
| 2 | +//! |
| 3 | +//! This example demonstrates the one-liner convenience functions in the `quick` module. |
| 4 | +//! The quick module provides the simplest possible API for common dotmax tasks. |
| 5 | +//! |
| 6 | +//! # Running this example |
| 7 | +//! |
| 8 | +//! Without image support (demonstrates grid/show): |
| 9 | +//! ```bash |
| 10 | +//! cargo run --example quick_demo |
| 11 | +//! ``` |
| 12 | +//! |
| 13 | +//! With image support (demonstrates all functions): |
| 14 | +//! ```bash |
| 15 | +//! cargo run --example quick_demo --features image |
| 16 | +//! ``` |
| 17 | +//! |
| 18 | +//! # What this example shows |
| 19 | +//! |
| 20 | +//! 1. `quick::grid()` - Create a terminal-sized grid |
| 21 | +//! 2. `quick::grid_sized()` - Create a grid with specific dimensions |
| 22 | +//! 3. `quick::show()` - Display a grid and wait for keypress |
| 23 | +//! 4. `quick::load_image()` - Load an image into a grid (image feature) |
| 24 | +//! 5. `quick::show_image()` - One-line image display (image feature) |
| 25 | +
|
| 26 | +use dotmax::primitives::{draw_circle, draw_line, draw_rectangle}; |
| 27 | +use dotmax::quick; |
| 28 | + |
| 29 | +fn main() -> Result<(), dotmax::DotmaxError> { |
| 30 | + println!("=== dotmax quick module demo ===\n"); |
| 31 | + |
| 32 | + // Example 1: Create a terminal-sized grid and draw on it |
| 33 | + println!("1. Creating terminal-sized grid with quick::grid()"); |
| 34 | + let grid = quick::grid()?; |
| 35 | + println!( |
| 36 | + " Grid created: {}x{} cells ({}x{} dots)", |
| 37 | + grid.width(), |
| 38 | + grid.height(), |
| 39 | + grid.dot_width(), |
| 40 | + grid.dot_height() |
| 41 | + ); |
| 42 | + |
| 43 | + // Example 2: Create a grid with specific dimensions |
| 44 | + println!("\n2. Creating 40x20 grid with quick::grid_sized(40, 20)"); |
| 45 | + let mut grid = quick::grid_sized(40, 20)?; |
| 46 | + println!(" Grid created: {}x{} cells", grid.width(), grid.height()); |
| 47 | + |
| 48 | + // Draw some shapes on the grid |
| 49 | + println!(" Drawing shapes..."); |
| 50 | + |
| 51 | + // Get dimensions for drawing |
| 52 | + let dot_w = grid.dot_width() as u32; |
| 53 | + let dot_h = grid.dot_height() as u32; |
| 54 | + |
| 55 | + // Draw a border (draw_rectangle takes i32 for position, u32 for size) |
| 56 | + draw_rectangle(&mut grid, 0, 0, dot_w, dot_h)?; |
| 57 | + |
| 58 | + // Draw diagonal lines (draw_line takes i32 for all coordinates) |
| 59 | + let w = dot_w as i32 - 1; |
| 60 | + let h = dot_h as i32 - 1; |
| 61 | + draw_line(&mut grid, 0, 0, w, h)?; |
| 62 | + draw_line(&mut grid, w, 0, 0, h)?; |
| 63 | + |
| 64 | + // Draw a circle in the center (draw_circle takes i32 for position) |
| 65 | + let center_x = w / 2; |
| 66 | + let center_y = h / 2; |
| 67 | + draw_circle(&mut grid, center_x, center_y, 15)?; |
| 68 | + |
| 69 | + // Display the grid |
| 70 | + println!("\n3. Displaying grid with quick::show()"); |
| 71 | + println!(" Press any key to continue after viewing...\n"); |
| 72 | + quick::show(&grid)?; |
| 73 | + println!(" Grid displayed and dismissed."); |
| 74 | + |
| 75 | + // Image examples (only with image feature) |
| 76 | + #[cfg(feature = "image")] |
| 77 | + { |
| 78 | + use std::path::Path; |
| 79 | + |
| 80 | + // Try to find a sample image |
| 81 | + let sample_paths = [ |
| 82 | + "tests/fixtures/images/sample.png", |
| 83 | + "examples/sample.png", |
| 84 | + "sample.png", |
| 85 | + ]; |
| 86 | + |
| 87 | + let sample_image = sample_paths.iter().find(|p| Path::new(p).exists()); |
| 88 | + |
| 89 | + if let Some(image_path) = sample_image { |
| 90 | + println!("\n4. Loading image with quick::load_image()"); |
| 91 | + let image_grid = quick::load_image(image_path)?; |
| 92 | + println!( |
| 93 | + " Loaded {} into {}x{} grid", |
| 94 | + image_path, |
| 95 | + image_grid.width(), |
| 96 | + image_grid.height() |
| 97 | + ); |
| 98 | + |
| 99 | + println!("\n5. Displaying image with quick::show_image() - one line!"); |
| 100 | + println!(" Press any key after viewing...\n"); |
| 101 | + quick::show_image(image_path)?; |
| 102 | + println!(" Image displayed and dismissed."); |
| 103 | + } else { |
| 104 | + println!("\n4-5. Skipping image demos (no sample image found)"); |
| 105 | + println!(" Try placing a sample.png in the examples/ directory"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + #[cfg(not(feature = "image"))] |
| 110 | + { |
| 111 | + println!("\n4-5. Image demos require --features image"); |
| 112 | + println!(" Run: cargo run --example quick_demo --features image"); |
| 113 | + } |
| 114 | + |
| 115 | + println!("\n=== Demo complete ==="); |
| 116 | + Ok(()) |
| 117 | +} |
0 commit comments