use libc::{c_float, c_uint};
use std::ptr;
use traits::Wrappable;
use graphics::{Drawable, Transformable, Shape, IntRect, FloatRect, Color, Texture,
RenderTarget, Transform, RenderStates};
use sfml_types::Vector2f;
use sfml_types::sfBool;
use csfml_graphics_sys as ffi;
pub struct CircleShape<'s> {
circle_shape: *mut ffi::sfCircleShape,
texture: Option<&'s Texture>
}
impl<'s> CircleShape<'s> {
pub fn new() -> Option<CircleShape<'s>> {
let circle = unsafe { ffi::sfCircleShape_create() };
if circle.is_null() {
None
} else {
Some(CircleShape {
circle_shape: circle,
texture: None
})
}
}
pub fn new_with_texture(texture: &'s Texture) -> Option<CircleShape<'s>> {
let circle = unsafe { ffi::sfCircleShape_create() };
if circle.is_null() {
None
} else {
unsafe {
ffi::sfCircleShape_setTexture(circle, texture.unwrap(), sfBool::SFTRUE);
}
Some(CircleShape {
circle_shape: circle,
texture: Some(texture)
})
}
}
pub fn new_init(radius: f32,
point_count: u32) -> Option<CircleShape<'s>> {
let circle = unsafe { ffi::sfCircleShape_create() };
if circle.is_null() {
None
} else {
unsafe {
ffi::sfCircleShape_setRadius(circle, radius as c_float);
ffi::sfCircleShape_setPointCount(circle, point_count as c_uint);
}
Some(CircleShape {
circle_shape: circle,
texture: None
})
}
}
pub fn clone_opt(&self) -> Option<CircleShape<'s>> {
let circle = unsafe { ffi::sfCircleShape_copy(self.circle_shape) };
if circle.is_null() {
None
} else {
Some(CircleShape {
circle_shape: circle,
texture: self.texture
})
}
}
pub fn set_radius(&self, radius: f32) {
unsafe {
ffi::sfCircleShape_setRadius(self.circle_shape, radius as c_float)
}
}
pub fn get_radius(&self) -> f32 {
unsafe {
ffi::sfCircleShape_getRadius(self.circle_shape) as f32
}
}
pub fn set_point_count(&mut self, count: u32) {
unsafe {
ffi::sfCircleShape_setPointCount(self.circle_shape, count as c_uint)
}
}
}
impl<'s> Drawable for CircleShape<'s> {
fn draw<RT:RenderTarget>(&self, render_target: &mut RT, render_states: &mut RenderStates) {
render_target.draw_circle_shape(self, render_states)
}
}
impl<'s> Transformable for CircleShape<'s> {
fn set_position(&mut self, position: &Vector2f) {
unsafe {
ffi::sfCircleShape_setPosition(self.circle_shape, *position)
}
}
fn set_position2f(&mut self, x: f32, y: f32) {
unsafe {
ffi::sfCircleShape_setPosition(self.circle_shape,
Vector2f::new(x, y))
}
}
fn set_rotation(&mut self, angle: f32) {
unsafe {
ffi::sfCircleShape_setRotation(self.circle_shape, angle as c_float)
}
}
fn set_scale(&mut self, scale: &Vector2f) {
unsafe {
ffi::sfCircleShape_setScale(self.circle_shape, *scale)
}
}
fn set_scale2f(&mut self, scale_x: f32, scale_y: f32) {
unsafe {
ffi::sfCircleShape_setScale(self.circle_shape,
Vector2f::new(scale_x, scale_y))
}
}
fn set_origin(&mut self, origin: &Vector2f) {
unsafe {
ffi::sfCircleShape_setOrigin(self.circle_shape, *origin)
}
}
fn set_origin2f(&mut self, x: f32, y: f32) {
unsafe {
ffi::sfCircleShape_setOrigin(self.circle_shape, Vector2f::new(x, y))
}
}
fn get_position(&self) -> Vector2f {
unsafe {
ffi::sfCircleShape_getPosition(self.circle_shape)
}
}
fn get_rotation(&self) -> f32 {
unsafe {
ffi::sfCircleShape_getRotation(self.circle_shape) as f32
}
}
fn get_scale(&self) -> Vector2f {
unsafe {
ffi::sfCircleShape_getScale(self.circle_shape)
}
}
fn get_origin(&self) -> Vector2f {
unsafe {
ffi::sfCircleShape_getOrigin(self.circle_shape)
}
}
fn move_(&mut self, offset: &Vector2f) {
unsafe {
ffi::sfCircleShape_move(self.circle_shape, *offset)
}
}
fn move2f(&mut self, offset_x: f32, offset_y: f32) {
unsafe {
ffi::sfCircleShape_move(self.circle_shape,
Vector2f::new(offset_x, offset_y))
}
}
fn rotate(&mut self, angle: f32) {
unsafe {
ffi::sfCircleShape_rotate(self.circle_shape, angle as c_float)
}
}
fn scale(&mut self, factors: &Vector2f) {
unsafe {
ffi::sfCircleShape_scale(self.circle_shape, *factors)
}
}
fn scale2f(&mut self, factor_x: f32, factor_y: f32) {
unsafe {
ffi::sfCircleShape_scale(self.circle_shape,
Vector2f::new(factor_x, factor_y))
}
}
fn get_transform(&self) -> Transform {
unsafe {
Transform(ffi::sfCircleShape_getTransform(self.circle_shape))
}
}
fn get_inverse_transform(&self) -> Transform {
unsafe {
Transform(ffi::sfCircleShape_getInverseTransform(self.circle_shape))
}
}
}
impl<'s> Shape<'s> for CircleShape<'s> {
fn set_texture(&mut self,
texture: &'s Texture,
reset_rect: bool) {
self.texture = Some(texture);
unsafe {
ffi::sfCircleShape_setTexture(self.circle_shape, texture.unwrap(), sfBool::from_bool(reset_rect))
}
}
fn disable_texture(&mut self) {
self.texture = None;
unsafe {
ffi::sfCircleShape_setTexture(self.circle_shape,
ptr::null_mut(),
sfBool::SFTRUE)
}
}
fn set_texture_rect(&mut self, rect: &IntRect) {
unsafe {
ffi::sfCircleShape_setTextureRect(self.circle_shape, *rect)
}
}
fn set_fill_color(&mut self, color: &Color) {
unsafe {
ffi::sfCircleShape_setFillColor(self.circle_shape, color.0)
}
}
fn set_outline_color(&mut self, color: &Color) {
unsafe {
ffi::sfCircleShape_setOutlineColor(self.circle_shape, color.0)
}
}
fn set_outline_thickness(&mut self, thickness: f32) {
unsafe {
ffi::sfCircleShape_setOutlineThickness(self.circle_shape,
thickness as c_float)
}
}
fn get_texture(&self) -> Option<&'s Texture> {
self.texture
}
fn get_texture_rect(&self) -> IntRect {
unsafe {
ffi::sfCircleShape_getTextureRect(self.circle_shape)
}
}
fn get_fill_color(&self) -> Color {
unsafe {
Color(ffi::sfCircleShape_getFillColor(self.circle_shape))
}
}
fn get_outline_color(&self) -> Color {
unsafe {
Color(ffi::sfCircleShape_getOutlineColor(self.circle_shape))
}
}
fn get_outline_thickness(&self) -> f32 {
unsafe {
ffi::sfCircleShape_getOutlineThickness(self.circle_shape) as f32
}
}
fn get_point_count(&self) -> u32 {
unsafe {
ffi::sfCircleShape_getPointCount(self.circle_shape) as u32
}
}
fn get_point(&self, index: u32) -> Vector2f {
unsafe {
ffi::sfCircleShape_getPoint(self.circle_shape, index as c_uint)
}
}
fn get_local_bounds(&self) -> FloatRect {
unsafe {
ffi::sfCircleShape_getLocalBounds(self.circle_shape)
}
}
fn get_global_bounds(&self) -> FloatRect {
unsafe {
ffi::sfCircleShape_getGlobalBounds(self.circle_shape)
}
}
}
impl<'s> Clone for CircleShape<'s> {
fn clone(&self) -> CircleShape<'s> {
let circle = unsafe { ffi::sfCircleShape_copy(self.circle_shape) };
if circle.is_null() {
panic!("Not enough memory to clone CircleShape")
} else {
CircleShape {
circle_shape: circle,
texture: self.texture
}
}
}
}
impl<'s> Drop for CircleShape<'s> {
fn drop(&mut self) {
unsafe {
ffi::sfCircleShape_destroy(self.circle_shape)
}
}
}
impl<'s> Wrappable<*mut ffi::sfCircleShape> for CircleShape<'s> {
fn wrap(circle_shape: *mut ffi::sfCircleShape) -> CircleShape<'s> {
CircleShape {
circle_shape: circle_shape,
texture: None
}
}
fn unwrap(&self) -> *mut ffi::sfCircleShape {
self.circle_shape
}
}