rust-cgui 0.1.15

A low-level cross-platform GUI library
Documentation

/*
 * This file is an example cross-platform GUI written using the cgui library.
 */

// Import the lib
extern crate cgui;

// This is how I parse "MouseMove,250,300" into x,y integers
#[macro_use]
extern crate scan_fmt;

use cgui::{RenderBack,CWin};

use std::ffi::{CStr};

// Regular 'ol main
fn main() {
  
  let mut w = CWin::new();
  
  // We store some metadata unrelated to cwin
  let square_size = 5;
  let mut x = 15;
  let mut y = 15;
  
  // If you directly use event_tick you must call this before your event loop
  w.init();
  
  loop {
    let evt_string = w.event_tick();
    if evt_string.starts_with("WinClose") {
      println!("Quitting!");
      break;
    }
    else if evt_string.starts_with("KeyPress,") {
      let pressed_key = scan_fmt!(&evt_string, "KeyPress,{}", String);
      match pressed_key {
        None => { continue; } // continue event loop
        _ => { }
      }
      let pressed_key = pressed_key.unwrap();
      // User wants to quit
      if pressed_key == "q" || pressed_key == "Q" {
        println!("Quitting!");
        break;
      }
      
      // If we will mutate, blue bg over old square
      if pressed_key == "w" || pressed_key == "a" || pressed_key == "s" || pressed_key == "d" {
        write_square(&mut w, x as u32, y as u32, square_size as u32, [0, 0, 255]);
      }
      
      // Easier arrow keys
      if pressed_key == "w" {
        if y - square_size > 0 {
          y -= square_size;
        }
      }
      else if pressed_key == "a" {
        if x - square_size > 0 {
          x -= square_size;
        }
      }
      else if pressed_key == "s" {
        if y + square_size < w.rep.height {
          y += square_size;
        }
      }
      else if pressed_key == "d" {
        if x + square_size < w.rep.width {
          x += square_size;
        }
      }
      
      // If we mutated, redraw
      if pressed_key == "w" || pressed_key == "a" || pressed_key == "s" || pressed_key == "d" {
        // paint red square
        write_square(&mut w, x as u32, y as u32, square_size as u32, [255, 0, 0]);
        w.redraw_dirty();
      }
      
      if y > 50 {
        write_msg(&mut w);
        w.redraw_dirty();
      }
      
    }
    else if evt_string.starts_with("WinShown") {
      // paint blue bg over everything
      for x in 0..w.rep.width {
        for y in 0..w.rep.height {
          w.write_px(x as u32, y as u32, [255, 255, 255]); // white px
        }
      }
      // and a black square
      write_square(&mut w, x as u32, y as u32, square_size as u32, [0, 0, 0]);
      w.redraw_dirty();
      
    }
    else {
      println!("Got unknown evt_string: {}", evt_string);
    }
  }
  
}

fn write_square(w: &mut CWin, x: u32, y: u32, square_size: u32, pixel: [u8; 3]) {
  write_rect(w, x, y, x+square_size, y+square_size, pixel);
}

fn write_rect(w: &mut CWin, x1: u32, y1: u32, x2: u32, y2: u32, pixel: [u8; 3]) {
  for x in x1..x2 {
    for y in y1..y2 {
      w.write_px(x, y, pixel); // blue px
    }
  }
}

fn write_msg(w: &mut CWin) {
  { // "H"
    write_rect(w, 5, 5,  6, 20, [0, 0, 0]);
    write_rect(w, 5, 12, 15, 13, [0, 0, 0]);
    write_rect(w, 15, 5, 16, 20, [0, 0, 0]);
  }
  { // "I"
    write_rect(w, 20, 5,  30, 6, [0, 0, 0]);
    write_rect(w, 24, 5,  25, 20, [0, 0, 0]);
    write_rect(w, 20, 19,  30, 20, [0, 0, 0]);
  }
}