Format

parent 4d3f2869
max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Unix"
indent_style = "Block"
format_strings = false
empty_item_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Visual"
imports_layout = "Mixed"
fn_args_density = "Tall"
brace_style = "SameLineWhere"
trailing_comma = "Vertical"
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
force_explicit_abi = true
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
report_todo = "Never"
report_fixme = "Never"
......@@ -6,8 +6,8 @@ The struct MathColor is needed for the calculate with bigger numbers, the Color
#[cfg(feature = "no_std")]
use alloc::vec::Vec;
use core::ops::{Add, AddAssign, Sub};
use Color;
use core::ops::{Add, Sub, AddAssign};
#[derive(Copy, Clone)]
pub struct MathColor {
......@@ -20,12 +20,16 @@ impl MathColor {
MathColor {
r: color.r() as isize,
g: color.g() as isize,
b: color.b() as isize
b: color.b() as isize,
}
}
pub fn get_multiplied_color(&mut self, iarr: f32) -> Color {
Color::rgb((self.r as f32 * iarr).round() as u8,(self.g as f32 * iarr).round() as u8,(self.b as f32 * iarr).round() as u8)
Color::rgb(
(self.r as f32 * iarr).round() as u8,
(self.g as f32 * iarr).round() as u8,
(self.b as f32 * iarr).round() as u8,
)
}
}
......@@ -34,22 +38,21 @@ impl Add for MathColor {
#[inline(always)]
fn add(self, color: MathColor) -> MathColor {
MathColor{
MathColor {
r: self.r + color.r,
g: self.g + color.g,
b: self.b + color.b
b: self.b + color.b,
}
}
}
impl AddAssign for MathColor {
#[inline(always)]
fn add_assign(&mut self, color: MathColor) {
*self = MathColor{
*self = MathColor {
r: self.r + color.r,
g: self.g + color.g,
b: self.b + color.b
b: self.b + color.b,
};
}
}
......@@ -59,10 +62,10 @@ impl Sub for MathColor {
#[inline(always)]
fn sub(self, color: MathColor) -> MathColor {
MathColor{
MathColor {
r: self.r - color.r,
g: self.g - color.g,
b: self.b - color.b
b: self.b - color.b,
}
}
}
......@@ -71,18 +74,42 @@ pub fn gauss_blur(data: &mut Vec<Color>, w: u32, h: u32, r: f32) {
let bxs = boxes_for_gauss(r, 3);
let mut tcl = data.clone();
box_blur(&mut tcl, data, w as usize, h as usize, ((bxs[0] - 1) / 2) as usize);
box_blur(&mut tcl, data, w as usize, h as usize, ((bxs[1] - 1) / 2) as usize);
box_blur(&mut tcl, data, w as usize, h as usize, ((bxs[2] - 1) / 2) as usize);
box_blur(
&mut tcl,
data,
w as usize,
h as usize,
((bxs[0] - 1) / 2) as usize,
);
box_blur(
&mut tcl,
data,
w as usize,
h as usize,
((bxs[1] - 1) / 2) as usize,
);
box_blur(
&mut tcl,
data,
w as usize,
h as usize,
((bxs[2] - 1) / 2) as usize,
);
}
fn boxes_for_gauss(sigma: f32, n: usize) -> Vec<i32> {
fn boxes_for_gauss(sigma: f32, n: usize) -> Vec<i32> {
let w_ideal: f32 = ((12.0 * sigma * sigma / n as f32) + 1.0).sqrt();
let mut wl: i32 = w_ideal.floor() as i32;
if wl % 2 == 0 { wl -= 1; };
if wl % 2 == 0 {
wl -= 1;
};
let wu: i32 = wl + 2;
let m_ideal:f32 = (12.0 * sigma * sigma - n as f32 * wl as f32 * wl as f32 - 4.0 * n as f32 * wl as f32 - 3.0 * n as f32) / (-4.0 * wl as f32 - 4.0);
let m_ideal: f32 = (12.0 * sigma * sigma
- n as f32 * wl as f32 * wl as f32
- 4.0 * n as f32 * wl as f32
- 3.0 * n as f32)
/ (-4.0 * wl as f32 - 4.0);
let m: usize = m_ideal.round() as usize;
let mut sizes = Vec::<i32>::new();
......@@ -108,30 +135,36 @@ fn box_blur_h(tcl: &mut Vec<Color>, scl: &mut Vec<Color>, w: usize, h: usize, r:
let fv = MathColor::new(tcl[ti]);
let lv = MathColor::new(tcl[ti + w - 1]);
let mut val: MathColor = MathColor{
let mut val: MathColor = MathColor {
r: (r + 1) as isize * fv.r,
g: (r + 1) as isize * fv.g,
b: (r + 1) as isize * fv.b
b: (r + 1) as isize * fv.b,
};
for j in 0..r { val += MathColor::new(tcl[ti + j]); }
for j in 0..r {
val += MathColor::new(tcl[ti + j]);
}
for _ in 0..(r + 1) {
val += MathColor::new(tcl[ri]) - fv;
scl[ti] = val.get_multiplied_color(iarr);
ti += 1; ri += 1;
ti += 1;
ri += 1;
}
for _ in (r + 1)..(w - r) {
val += MathColor::new(tcl[ri]) - MathColor::new(tcl[li]);
scl[ti] = val.get_multiplied_color(iarr);
ti += 1; ri += 1; li += 1;
ti += 1;
ri += 1;
li += 1;
}
for _ in (w - r)..w {
val += lv - MathColor::new(tcl[li]);
scl[ti] = val.get_multiplied_color(iarr);
ti += 1; li += 1;
ti += 1;
li += 1;
}
}
}
......@@ -147,30 +180,36 @@ fn box_blur_t(tcl: &mut Vec<Color>, scl: &mut Vec<Color>, w: usize, h: usize, r:
let fv = MathColor::new(tcl[ti]);
let lv = MathColor::new(tcl[ti + w * (h - 1)]);
let mut val: MathColor = MathColor{
let mut val: MathColor = MathColor {
r: (r + 1) as isize * fv.r,
g: (r + 1) as isize * fv.g,
b: (r + 1) as isize * fv.b
b: (r + 1) as isize * fv.b,
};
for j in 0..r { val += MathColor::new(tcl[ti + j * w]); }
for j in 0..r {
val += MathColor::new(tcl[ti + j * w]);
}
for _ in 0..(r + 1) {
val += MathColor::new(tcl[ri]) - fv;
scl[ti] = val.get_multiplied_color(iarr);
ti += w; ri += w;
ti += w;
ri += w;
}
for _ in (r + 1)..(h - r) {
val += MathColor::new(tcl[ri]) - MathColor::new(tcl[li]);
scl[ti] = val.get_multiplied_color(iarr);
ti += w; ri += w; li += w;
ti += w;
ri += w;
li += w;
}
for _ in (h - r)..h {
val += lv - MathColor::new(tcl[li]);
scl[ti] = val.get_multiplied_color(iarr);
ti += w; li += w;
ti += w;
li += w;
}
}
}
#[cfg(not(feature="no_std"))]
#[cfg(not(feature = "no_std"))]
use std::fmt;
/// A color
......@@ -11,12 +11,16 @@ pub struct Color {
impl Color {
/// Create a new color from RGB
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Color { data: 0xFF000000 | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32) }
Color {
data: 0xFF000000 | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32),
}
}
/// Set the alpha
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Color { data: ((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32) }
Color {
data: ((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32),
}
}
/// Get the r value
......@@ -54,18 +58,16 @@ impl Color {
}
/// Compare two colors (Do not take care of alpha)
impl PartialEq for Color{
impl PartialEq for Color {
fn eq(&self, other: &Color) -> bool {
self.r() == other.r() &&
self.g() == other.g() &&
self.b() == other.b()
self.r() == other.r() && self.g() == other.g() && self.b() == other.b()
}
}
#[cfg(not(feature="no_std"))]
#[cfg(not(feature = "no_std"))]
impl fmt::Debug for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:#010X}", {self.data})
write!(f, "{:#010X}", { self.data })
}
}
......@@ -74,10 +76,9 @@ mod tests {
#[test]
fn partial_eq() {
use Color;
assert_eq!(true, Color::rgb(1,2,3) == Color::rgba(1,2,3,200));
assert_eq!(false, Color::rgb(1,2,3) == Color::rgba(11,2,3,200));
assert_eq!(true, Color::rgba(1,2,3,200) == Color::rgba(1,2,3,200));
assert_eq!(true, Color::rgb(1, 2, 3) == Color::rgba(1, 2, 3, 200));
assert_eq!(false, Color::rgb(1, 2, 3) == Color::rgba(11, 2, 3, 200));
assert_eq!(true, Color::rgba(1, 2, 3, 200) == Color::rgba(1, 2, 3, 200));
}
}
use core::{char, mem, slice};
use core::ops::{Deref, DerefMut};
use core::{char, mem, slice};
pub const EVENT_NONE: i64 = 0;
pub const EVENT_KEY: i64 = 1;
......@@ -81,7 +81,8 @@ impl Deref for Event {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const Event as *const u8, mem::size_of::<Event>()) as &[u8]
slice::from_raw_parts(self as *const Event as *const u8, mem::size_of::<Event>())
as &[u8]
}
}
}
......@@ -89,7 +90,8 @@ impl Deref for Event {
impl DerefMut for Event {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self as *mut Event as *mut u8, mem::size_of::<Event>()) as &mut [u8]
slice::from_raw_parts_mut(self as *mut Event as *mut u8, mem::size_of::<Event>())
as &mut [u8]
}
}
}
......@@ -291,8 +293,7 @@ impl ButtonEvent {
pub fn to_event(&self) -> Event {
Event {
code: EVENT_BUTTON,
a: self.left as i64 | (self.middle as i64) << 1 |
(self.right as i64) << 2,
a: self.left as i64 | (self.middle as i64) << 1 | (self.right as i64) << 2,
b: 0,
}
}
......@@ -356,7 +357,7 @@ impl QuitEvent {
#[derive(Copy, Clone, Debug)]
pub struct FocusEvent {
/// True if window has been focused, false if not
pub focused: bool
pub focused: bool,
}
impl FocusEvent {
......@@ -370,7 +371,7 @@ impl FocusEvent {
pub fn from_event(event: Event) -> FocusEvent {
FocusEvent {
focused: event.a > 0
focused: event.a > 0,
}
}
}
......@@ -379,7 +380,7 @@ impl FocusEvent {
#[derive(Copy, Clone, Debug)]
pub struct MoveEvent {
pub x: i32,
pub y: i32
pub y: i32,
}
impl MoveEvent {
......@@ -394,7 +395,7 @@ impl MoveEvent {
pub fn from_event(event: Event) -> MoveEvent {
MoveEvent {
x: event.a as i32,
y: event.b as i32
y: event.b as i32,
}
}
}
......@@ -403,7 +404,7 @@ impl MoveEvent {
#[derive(Copy, Clone, Debug)]
pub struct ResizeEvent {
pub width: u32,
pub height: u32
pub height: u32,
}
impl ResizeEvent {
......@@ -418,7 +419,7 @@ impl ResizeEvent {
pub fn from_event(event: Event) -> ResizeEvent {
ResizeEvent {
width: event.a as u32,
height: event.b as u32
height: event.b as u32,
}
}
}
......@@ -427,7 +428,7 @@ impl ResizeEvent {
#[derive(Copy, Clone, Debug)]
pub struct ScreenEvent {
pub width: u32,
pub height: u32
pub height: u32,
}
impl ScreenEvent {
......@@ -442,7 +443,7 @@ impl ScreenEvent {
pub fn from_event(event: Event) -> ScreenEvent {
ScreenEvent {
width: event.a as u32,
height: event.b as u32
height: event.b as u32,
}
}
}
......@@ -16,37 +16,36 @@ pub struct GraphicsPath {
}
impl GraphicsPath {
pub fn new() -> GraphicsPath {
GraphicsPath {
x : 0,
y : 0,
x: 0,
y: 0,
points: Vec::new(),
}
}
/// move to position
pub fn move_to(&mut self, x: i32, y: i32){
self.points.push((x,y,PointType::Move));
pub fn move_to(&mut self, x: i32, y: i32) {
self.points.push((x, y, PointType::Move));
self.x = x;
self.y = y;
}
/// create a line between the last and new point
pub fn line_to(&mut self, x: i32, y: i32) {
self.points.push((x,y,PointType::Connect));
self.points.push((x, y, PointType::Connect));
self.x = x;
self.y = y;
}
/// quadratic bezier curve
pub fn quadratic_curve_to(&mut self, argx1: i32, argy1: i32, argx2: i32, argy2: i32){
let mut t:f32 = 0.0;
let mut u:f32;
let mut tt:f32;
let mut uu:f32;
let mut x:f32;
let mut y:f32;
pub fn quadratic_curve_to(&mut self, argx1: i32, argy1: i32, argx2: i32, argy2: i32) {
let mut t: f32 = 0.0;
let mut u: f32;
let mut tt: f32;
let mut uu: f32;
let mut x: f32;
let mut y: f32;
while t < 1.0 {
u = 1.0 - t;
......@@ -56,13 +55,13 @@ impl GraphicsPath {
x = (self.x as f32) * uu;
y = (self.y as f32) * uu;
x += 2.0 * u * t * (argx1 as f32);
y += 2.0 * u * t * (argy1 as f32);
x += 2.0 * u * t * (argx1 as f32);
y += 2.0 * u * t * (argy1 as f32);
x += tt * (argx2 as f32);
y += tt * (argy2 as f32);
t = t+0.01;
t = t + 0.01;
self.points.push((x as i32, y as i32, PointType::Connect));
}
......@@ -71,15 +70,23 @@ impl GraphicsPath {
}
/// cubic bezier curve
pub fn bezier_curve_to(&mut self, argx1: i32, argy1: i32, argx2: i32, argy2: i32, argx3: i32, argy3: i32){
let mut t:f32 = 0.0;
let mut u:f32;
let mut tt:f32;
let mut uu:f32;
let mut uuu:f32;
let mut ttt:f32;
let mut x:f32;
let mut y:f32;
pub fn bezier_curve_to(
&mut self,
argx1: i32,
argy1: i32,
argx2: i32,
argy2: i32,
argx3: i32,
argy3: i32,
) {
let mut t: f32 = 0.0;
let mut u: f32;
let mut tt: f32;
let mut uu: f32;
let mut uuu: f32;
let mut ttt: f32;
let mut x: f32;
let mut y: f32;
while t < 1.0 {
u = 1.0 - t;
......@@ -100,7 +107,7 @@ impl GraphicsPath {
x += ttt * (argx3 as f32);
y += ttt * (argy3 as f32);
t = t+0.01;
t = t + 0.01;
self.points.push((x as i32, y as i32, PointType::Connect));
}
......
#![crate_name="orbclient"]
#![crate_type="lib"]
#![cfg_attr(feature="no_std", feature(alloc))]
#![cfg_attr(feature="no_std", no_std)]
#![crate_name = "orbclient"]
#![crate_type = "lib"]
#![cfg_attr(feature = "no_std", feature(alloc))]
#![cfg_attr(feature = "no_std", no_std)]
#![deny(warnings)]
#[cfg(feature="no_std")]
#[cfg(feature = "no_std")]
extern crate alloc;
#[cfg(not(feature="no_std"))]
#[cfg(not(feature = "no_std"))]
extern crate core;
pub static FONT: &'static [u8] = include_bytes!("../res/unifont.font");
pub use color::Color;
pub use event::*;
#[cfg(not(feature="no_std"))]
pub use sys::{get_display_size, EventIter, Window};
pub use graphicspath::GraphicsPath;
pub use renderer::Renderer;
#[cfg(not(feature = "no_std"))]
pub use sys::{get_display_size, EventIter, Window};
#[cfg(not(feature = "no_std"))]
mod blur;
pub mod color;
pub mod event;
pub mod graphicspath;
pub mod renderer;
#[cfg(not(feature = "no_std"))]
mod blur;
#[derive(Clone, Copy, Debug)]
pub enum WindowFlag {
......@@ -33,19 +32,19 @@ pub enum WindowFlag {
Front,
Borderless,
Resizable,
Unclosable
Unclosable,
}
#[derive(Clone, Copy, Debug)]
pub enum Mode {
Blend, //Composite
Overwrite //Replace
Blend, //Composite
Overwrite, //Replace
}
#[cfg(all(not(feature="no_std"), target_os = "redox"))]
#[path="sys/orbital.rs"]
#[cfg(all(not(feature = "no_std"), target_os = "redox"))]
#[path = "sys/orbital.rs"]
mod sys;
#[cfg(all(not(feature="no_std"), not(target_os = "redox")))]
#[path="sys/sdl2.rs"]
#[cfg(all(not(feature = "no_std"), not(target_os = "redox")))]
#[path = "sys/sdl2.rs"]
mod sys;
This diff is collapsed.
extern crate syscall;
use std::{env, mem, slice, thread};
use std::cell::Cell;
use std::fs::File;
use std::io::{Read, Write};
use std::os::unix::io::{AsRawFd, RawFd};
use std::cell::Cell;
use std::{env, mem, slice, thread};
use color::Color;
use event::{Event, EVENT_RESIZE};
use renderer::Renderer;
use WindowFlag;
use Mode;
use WindowFlag;
pub fn get_display_size() -> Result<(u32, u32), String> {
let display_path = try!(env::var("DISPLAY").or(Err("DISPLAY not set")));
match File::open(&display_path) {
Ok(display) => {
let mut buf: [u8; 4096] = [0; 4096];
let count = syscall::fpath(display.as_raw_fd() as usize, &mut buf).map_err(|err| format!("{}", err))?;
let count = syscall::fpath(display.as_raw_fd() as usize, &mut buf)
.map_err(|err| format!("{}", err))?;
let path = unsafe { String::from_utf8_unchecked(Vec::from(&buf[..count])) };
let res = path.split(":").nth(1).unwrap_or("");
let width = res.split("/").nth(1).unwrap_or("").parse::<u32>().unwrap_or(0);
let height = res.split("/").nth(2).unwrap_or("").parse::<u32>().unwrap_or(0);
let width = res
.split("/")
.nth(1)
.unwrap_or("")
.parse::<u32>()
.unwrap_or(0);
let height = res
.split("/")
.nth(2)
.unwrap_or("")
.parse::<u32>()
.unwrap_or(0);
Ok((width, height))
},
Err(err) => Err(format!("{}", err))
}
Err(err) => Err(format!("{}", err)),
}
}
......@@ -91,7 +102,14 @@ impl Window {
}