added some traits to Vec2

This commit is contained in:
dani 2023-07-02 11:21:50 +00:00
parent 707a71b432
commit efe476fdda
2 changed files with 94 additions and 22 deletions

View File

@ -1,5 +1,5 @@
use crate::vec2::Vec2;
use crate::Rect;
use crate::{HexMap, Rect};
use std::fs;
use std::fs::File;
use std::rc::Rc;
@ -55,6 +55,10 @@ impl Image {
self.data.as_slice()
}
//pub fn draw_hexmap(&mut self, pos: Vec2<i32>, hexmap: &HexMap) {
// for pos in Vec2::zero()..pos {}
//}
pub fn draw_image(&mut self, pos: Vec2<i32>, image: &Image) {
self.draw_image_partial(
pos,
@ -68,27 +72,16 @@ impl Image {
pub fn draw_image_partial(&mut self, pos: Vec2<i32>, image: &Image, src_rect: Rect<i32>) {
//todo: write proper implementation later
for y in 0..src_rect.size.y {
for x in 0..src_rect.size.x {
for i in Vec2::zero().iter_to(src_rect.size) {
//todo: implement better(very stupid to do per pixel)
if x + pos.x >= 0
&& y + pos.y >= 0
&& x + pos.x < self.size.x
&& y + pos.y < self.size.y
if i.x + pos.x >= 0
&& i.y + pos.y >= 0
&& i.x + pos.x < self.size.x
&& i.y + pos.y < self.size.y
{
let p = image.get_pixel(Vec2 {
x: x + src_rect.pos.x,
y: y + src_rect.pos.y,
});
let p = image.get_pixel(i + src_rect.pos);
if p > 0 {
self.set_pixel(
Vec2 {
x: x + pos.x,
y: y + pos.y,
},
p,
);
}
self.set_pixel(i + pos, p);
}
}
}

View File

@ -1,4 +1,9 @@
use num::{Num, ToPrimitive};
use std::cmp::Ordering;
use std::cmp::Ordering::{Greater, Less};
use std::iter::Step;
use std::ops::{Add, AddAssign, Range, Sub, SubAssign};
use Ordering::Equal;
#[derive(Copy, Clone)]
pub struct Vec2<T: Num + Copy + ToPrimitive> {
@ -6,6 +11,12 @@ pub struct Vec2<T: Num + Copy + ToPrimitive> {
pub y: T,
}
pub struct Vec2Iter<T: Num + Copy + ToPrimitive> {
start: Vec2<T>,
end: Vec2<T>,
current: Vec2<T>,
}
impl<T: Num + Copy + ToPrimitive> Vec2<T> {
//pub static
pub fn zero() -> Self {
@ -16,6 +27,16 @@ impl<T: Num + Copy + ToPrimitive> Vec2<T> {
}
//pub
pub fn iter_to(self, other: Self) -> Vec2Iter<T> {
Vec2Iter {
start: self,
end: other,
current: Vec2 {
x: self.x - T::one(),
y: self.y,
},
}
}
pub fn size(&self) -> usize {
(self.x * self.y).to_usize().unwrap()
}
@ -29,3 +50,61 @@ impl<T: Num + Copy + ToPrimitive + Default> Default for Vec2<T> {
}
}
}
impl<T: Num + Copy + ToPrimitive> PartialEq for Vec2<T> {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl<T: Num + Copy + ToPrimitive + PartialOrd> Iterator for Vec2Iter<T> {
type Item = Vec2<T>;
fn next(&mut self) -> Option<Self::Item> {
self.current.x = self.current.x + T::one();
if self.current.x >= self.end.x {
self.current.y = self.current.y + T::one();
self.current.x = self.start.x;
if self.current.y >= self.end.y {
return None;
}
}
Some(self.current)
}
}
impl<T: Num + Copy + ToPrimitive> Add for Vec2<T> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl<T: Num + Copy + ToPrimitive> Sub for Vec2<T> {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl<T: Num + Copy + ToPrimitive + AddAssign> AddAssign for Vec2<T> {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl<T: Num + Copy + ToPrimitive + SubAssign> SubAssign for Vec2<T> {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}