-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
439 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, | ||
_ => () | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, | ||
_ => () | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.