Skip to content

Commit

Permalink
Add first rev of orbtk
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Dec 14, 2015
1 parent cec98f2 commit bcd5b18
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 1 deletion.
115 changes: 115 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "orbtk"
description = "The Orbital Widget Toolkit"
repository = "https://github.com/redox/orbtk"
version = "0.0.1"
license-file = "LICENSE"
readme = "README.md"
authors = ["Jeremy Soller <jackpot51@gmail.com>"]

[lib]
name = "orbtk"
path = "src/lib.rs"

[dependencies]
sdl2 = "0.9"
sdl2_image = "0.3"
sdl2_ttf = "0.9"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Redox
Copyright (c) 2015 Jeremy Soller

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# orbtk
The Orbital Widget Toolkit. Compatible with redox and SDL2.

[![Travis Build Status](https://travis-ci.org/redox/orbtk.svg?branch=master)](https://travis-ci.org/redox/orbtk)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)

6 changes: 6 additions & 0 deletions src/click.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::Point;

pub trait Click {
fn click(&mut self, point: Point);
fn on_click(&mut self, func: Box<Fn(&mut Self, Point)>) -> &mut Self;
}
84 changes: 84 additions & 0 deletions src/label.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use super::{Click, Color, Event, EventOption, MouseEvent, Point, Rect, Renderer, Widget};

use std::sync::Arc;

pub struct Label {
pub rect: Rect,
pub text: String,
pub bg: Color,
pub fg: Color,
on_click: Option<Arc<Box<Fn(&mut Label, Point)>>>,
last_mouse: Option<MouseEvent>,
}

impl Label {
pub fn new(rect: Rect, text: &str) -> Box<Self> {
Box::new(Label {
rect: rect,
text: text.to_string(),
bg: Color::rgb(255, 255, 255),
fg: Color::rgb(0, 0, 0),
on_click: None,
last_mouse: None,
})
}
}

impl Click for Label {
fn click(&mut self, point: Point){
let on_click_option = match self.on_click {
Some(ref on_click) => Some(on_click.clone()),
None => None
};

if let Some(on_click) = on_click_option {
on_click(self, point);
}
}

fn on_click(&mut self, func: Box<Fn(&mut Self, Point)>) -> &mut Self {
self.on_click = Some(Arc::new(func));

self
}
}

impl Widget for Label {
fn draw(&self, renderer: &mut Renderer) {
renderer.rect(self.rect, self.bg);

let mut x = 0;
for c in self.text.chars() {
if x + 8 <= self.rect.width as isize {
renderer.char(Point::new(x + self.rect.x, self.rect.y), c, self.fg);
}
x += 8;
}
}

fn event(&mut self, event: &Event) {
match event.to_option() {
EventOption::Mouse(mouse_event) => {
let mut click = false;

if self.rect.contains(Point::new(mouse_event.x, mouse_event.y)){
if let Some(last_mouse) = self.last_mouse {
if last_mouse.left_button && ! mouse_event.left_button {
click = true;
}
}

self.last_mouse = Some(mouse_event);
} else {
self.last_mouse = None;
}

if click {
let point = Point::new(mouse_event.x - self.rect.x, mouse_event.y - self.rect.y);
self.click(point);
}
},
_ => ()
}
}
}
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![crate_type="lib"]

extern crate orbital;

pub use orbital::{Color, Event, EventOption, KeyEvent, MouseEvent, Point, Size};

pub use click::Click;
pub use label::Label;
pub use progress_bar::ProgressBar;
pub use rect::Rect;
pub use renderer::Renderer;
pub use widget::Widget;
pub use window::Window;

pub mod click;
pub mod label;
pub mod progress_bar;
pub mod rect;
pub mod renderer;
pub mod widget;
pub mod window;
87 changes: 87 additions & 0 deletions src/progress_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use super::{Click, Color, Event, EventOption, MouseEvent, Point, Rect, Renderer, Widget};

use std::cmp::{min, max};
use std::sync::Arc;

pub struct ProgressBar {
pub rect: Rect,
pub value: isize,
pub minimum: isize,
pub maximum: isize,
pub bg: Color,
pub fg: Color,
on_click: Option<Arc<Box<Fn(&mut ProgressBar, Point)>>>,
last_mouse: Option<MouseEvent>,
}

impl ProgressBar {
pub fn new(rect: Rect, value: isize) -> Box<Self> {
Box::new(ProgressBar {
rect: rect,
value: value,
minimum: 0,
maximum: 100,
bg: Color::rgb(255, 255, 255),
fg: Color::rgb(65, 139, 212),
on_click: None,
last_mouse: None,
})
}
}

impl Click for ProgressBar {
fn click(&mut self, point: Point){
let on_click_option = match self.on_click {
Some(ref on_click) => Some(on_click.clone()),
None => None
};

if let Some(on_click) = on_click_option {
on_click(self, point);
}
}

fn on_click(&mut self, func: Box<Fn(&mut Self, Point)>) -> &mut Self {
self.on_click = Some(Arc::new(func));

self
}
}

impl Widget for ProgressBar {
fn draw(&self, renderer: &mut Renderer) {
renderer.rect(self.rect, self.bg);
renderer.rect(Rect::new(
self.rect.x,
self.rect.y,
((self.rect.width as isize * max(0, min(self.maximum, self.value - self.minimum)))/max(1, self.maximum - self.minimum)) as usize,
self.rect.height
), self.fg);
}

fn event(&mut self, event: &Event) {
match event.to_option() {
EventOption::Mouse(mouse_event) => {
let mut click = false;

if self.rect.contains(Point::new(mouse_event.x, mouse_event.y)){
if let Some(last_mouse) = self.last_mouse {
if last_mouse.left_button && ! mouse_event.left_button {
click = true;
}
}

self.last_mouse = Some(mouse_event);
} else {
self.last_mouse = None;
}

if click {
let point = Point::new(mouse_event.x - self.rect.x, mouse_event.y - self.rect.y);
self.click(point);
}
},
_ => ()
}
}
}
25 changes: 25 additions & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::Point;

#[derive(Clone, Copy, Debug, Default)]
pub struct Rect {
pub x: isize,
pub y: isize,
pub width: usize,
pub height: usize,
}

impl Rect {
pub fn new(x: isize, y: isize, width: usize, height: usize) -> Rect {
Rect {
x: x,
y: y,
width: width,
height: height,
}
}

pub fn contains(&self, p: Point) -> bool {
p.x >= self.x && p.x < self.x + self.width as isize &&
p.y >= self.y && p.y < self.y + self.height as isize
}
}
6 changes: 6 additions & 0 deletions src/renderer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::{Color, Point, Rect};

pub trait Renderer {
fn char(&mut self, pos: Point, c: char, color: Color);
fn rect(&mut self, rect: Rect, color: Color);
}
8 changes: 8 additions & 0 deletions src/widget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::{Event, Renderer};

use std::any::Any;

pub trait Widget : Any {
fn draw(&self, renderer: &mut Renderer);
fn event(&mut self, event: &Event);
}
Loading

0 comments on commit bcd5b18

Please sign in to comment.