Commit bcd5b188 authored by Jeremy Soller's avatar Jeremy Soller

Add first rev of orbtk

parent cec98f2f
[root]
name = "orbtk"
version = "0.0.1"
dependencies = [
"sdl2 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2_image 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2_ttf 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "advapi32-sys"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num"
version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-serialize"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "sdl2"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sdl2-sys"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sdl2_image"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sdl2_ttf"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[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"
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
......
# 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)
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;
}
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);
}
},
_ => ()
}
}
}
#![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;
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);
}
},
_ => ()
}
}
}
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
}
}
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);
}
use super::{Event, Renderer};
use std::any::Any;
pub trait Widget : Any {
fn draw(&self, renderer: &mut Renderer);
fn event(&mut self, event: &Event);
}
use super::{Color, Point, Rect, Renderer, Widget, orbital};
pub struct WindowRenderer<'a> {
inner: &'a mut Box<orbital::Window>
}
impl<'a> WindowRenderer<'a> {
pub fn new(inner: &'a mut Box<orbital::Window>) -> WindowRenderer {
WindowRenderer {
inner: inner
}
}
}
impl<'a> Renderer for WindowRenderer<'a> {
fn char(&mut self, pos: Point, c: char, color: Color) {
self.inner.char(pos.x, pos.y, c, color);
}
fn rect(&mut self, rect: Rect, color: Color) {
self.inner.rect(rect.x, rect.y, rect.width, rect.height, color);
}
}
impl<'a> Drop for WindowRenderer<'a> {
fn drop(&mut self) {
self.inner.sync();
}
}
pub struct Window {
inner: Box<orbital::Window>,
pub widgets: Vec<Box<Widget>>,
pub bg: Color,
}
impl Window {
pub fn new(rect: Rect, title: &str) -> Box<Self> {
Box::new(Window {
inner: orbital::Window::new(rect.x, rect.y, rect.width, rect.height, title).unwrap(),
widgets: Vec::new(),
bg: Color::rgb(237, 233, 227),
})
}
pub fn draw(&mut self) {
self.inner.set(self.bg);
let mut renderer = WindowRenderer::new(&mut self.inner);
for widget in self.widgets.iter() {
widget.draw(&mut renderer);
}
}
pub fn exec(&mut self) {
self.draw();
while let Some(event) = self.inner.poll() {
for mut widget in self.widgets.iter_mut() {
widget.event(&event);
}
self.draw();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment