mirror of
https://github.com/lisk77/comet.git
synced 2025-10-24 05:48:50 +00:00
feat: added the Color component to the base components in the ECS
This commit is contained in:
parent
4b6b5e64e9
commit
4d4c6aad90
14 changed files with 136 additions and 23 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
use crate::{sRgba, Color, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Hsla {
|
pub struct Hsla {
|
||||||
hue: f32,
|
hue: f32,
|
||||||
saturation: f32,
|
saturation: f32,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsla, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
use crate::{sRgba, Color, Hsla, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Hsva {
|
pub struct Hsva {
|
||||||
hue: f32,
|
hue: f32,
|
||||||
saturation: f32,
|
saturation: f32,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsla, Hsva, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
use crate::{sRgba, Color, Hsla, Hsva, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Hwba {
|
pub struct Hwba {
|
||||||
hue: f32,
|
hue: f32,
|
||||||
whiteness: f32,
|
whiteness: f32,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Laba {
|
pub struct Laba {
|
||||||
lightness: f32,
|
lightness: f32,
|
||||||
a: f32,
|
a: f32,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, LinearRgba, Oklaba, Oklcha, Xyza};
|
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, LinearRgba, Oklaba, Oklcha, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Lcha {
|
pub struct Lcha {
|
||||||
lightness: f32,
|
lightness: f32,
|
||||||
chroma: f32,
|
chroma: f32,
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,6 @@ mod lcha;
|
||||||
mod oklaba;
|
mod oklaba;
|
||||||
mod oklcha;
|
mod oklcha;
|
||||||
|
|
||||||
pub trait Color {
|
pub trait Color: Copy {
|
||||||
fn to_wgpu(&self) -> wgpu::Color;
|
fn to_wgpu(&self) -> wgpu::Color;
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use wgpu;
|
use wgpu;
|
||||||
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, Oklaba, Oklcha, Xyza};
|
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, Oklaba, Oklcha, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct LinearRgba {
|
pub struct LinearRgba {
|
||||||
red: f32,
|
red: f32,
|
||||||
green: f32,
|
green: f32,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklcha, Xyza};
|
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklcha, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Oklaba {
|
pub struct Oklaba {
|
||||||
lightness: f32,
|
lightness: f32,
|
||||||
a: f32,
|
a: f32,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Xyza};
|
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Xyza};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Oklcha {
|
pub struct Oklcha {
|
||||||
lightness: f32,
|
lightness: f32,
|
||||||
chroma: f32,
|
chroma: f32,
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use crate::{math::Vec4, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba,
|
||||||
/// There are two variants: `sRgba<u8>` and `sRgba<f32>`
|
/// There are two variants: `sRgba<u8>` and `sRgba<f32>`
|
||||||
/// The first one is your standard 0..255 RGB and the second is the normalized version with range 0..1
|
/// The first one is your standard 0..255 RGB and the second is the normalized version with range 0..1
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct sRgba<T> {
|
pub struct sRgba<T> {
|
||||||
red: T,
|
red: T,
|
||||||
green: T,
|
green: T,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha};
|
use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
pub struct Xyza {
|
pub struct Xyza {
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ comet_math = { path = "../comet_math" }
|
||||||
comet_resources = { path = "../comet_resources" }
|
comet_resources = { path = "../comet_resources" }
|
||||||
comet_log = { path = "../comet_log" }
|
comet_log = { path = "../comet_log" }
|
||||||
comet_structs = { path = "../comet_structs" }
|
comet_structs = { path = "../comet_structs" }
|
||||||
|
comet_colors = { path = "../comet_colors" }
|
||||||
|
wgpu = { version = "22.0" }
|
||||||
|
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
bit-set = "0.8.0"
|
bit-set = "0.8.0"
|
||||||
|
|
@ -8,6 +8,7 @@ use crate::math::{
|
||||||
Vec2,
|
Vec2,
|
||||||
Vec3
|
Vec3
|
||||||
};
|
};
|
||||||
|
use comet_colors::Color as ColorTrait;
|
||||||
use component_derive::Component;
|
use component_derive::Component;
|
||||||
use crate::{Entity, Scene};
|
use crate::{Entity, Scene};
|
||||||
|
|
||||||
|
|
@ -46,7 +47,7 @@ pub struct Rectangle2D{
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Render2D {
|
pub struct Render2D {
|
||||||
is_visible: bool,
|
is_visible: bool,
|
||||||
texture: &'static str,
|
texture_name: &'static str,
|
||||||
scale: Vec2
|
scale: Vec2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,6 +63,16 @@ pub struct Text {
|
||||||
content: &'static str,
|
content: &'static str,
|
||||||
font: &'static str,
|
font: &'static str,
|
||||||
font_size: f32,
|
font_size: f32,
|
||||||
|
color: Color,
|
||||||
|
is_visible: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Color {
|
||||||
|
r: f32,
|
||||||
|
g: f32,
|
||||||
|
b: f32,
|
||||||
|
a: f32
|
||||||
}
|
}
|
||||||
|
|
||||||
// ##################################################
|
// ##################################################
|
||||||
|
|
@ -228,14 +239,14 @@ impl Render for Render2D {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_texture(&self) -> String {
|
fn get_texture(&self) -> String {
|
||||||
self.texture.clone().parse().unwrap()
|
self.texture_name.clone().parse().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use the actual file name of the texture instead of the path
|
/// Use the actual file name of the texture instead of the path
|
||||||
/// e.g. "comet_icon.png" instead of "resources/textures/comet_icon.png"
|
/// e.g. "comet_icon.png" instead of "resources/textures/comet_icon.png"
|
||||||
/// The resource manager will already look in the resources/textures folder
|
/// The resource manager will already look in the resources/textures folder
|
||||||
fn set_texture(&mut self, texture: &'static str) {
|
fn set_texture(&mut self, texture: &'static str) {
|
||||||
self.texture = texture;
|
self.texture_name = texture;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -283,15 +294,6 @@ impl Transform3D {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Camera2D {
|
impl Camera2D {
|
||||||
/// Creates a Camera2D component.
|
|
||||||
///
|
|
||||||
/// # Parameters
|
|
||||||
/// - `dimensions`: The dimensions of the camera as a `Vec2` (width, height).
|
|
||||||
/// - `zoom`: The zoom level of the camera.
|
|
||||||
/// - `priority`: The priority of the camera, with lower numbers indicating higher priority.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// - Returns a `Camera2D` instance.
|
|
||||||
pub fn new(dimensions: Vec2, zoom: f32, priority: u8) -> Self {
|
pub fn new(dimensions: Vec2, zoom: f32, priority: u8) -> Self {
|
||||||
Self {
|
Self {
|
||||||
dimensions,
|
dimensions,
|
||||||
|
|
@ -355,3 +357,108 @@ impl Camera for Camera2D {
|
||||||
Mat4::OPENGL_CONV * Mat4::orthographic_projection(left, right, bottom, top, 1.0, 0.0)
|
Mat4::OPENGL_CONV * Mat4::orthographic_projection(left, right, bottom, top, 1.0, 0.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Text {
|
||||||
|
pub fn new(content: &'static str, font: &'static str, font_size: f32, is_visible: bool, color: impl ColorTrait) -> Self {
|
||||||
|
Self {
|
||||||
|
content,
|
||||||
|
font,
|
||||||
|
font_size,
|
||||||
|
color: Color::from_wgpu_color(color.to_wgpu()),
|
||||||
|
is_visible
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn content(&self) -> &'static str {
|
||||||
|
self.content
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_content(&mut self, content: &'static str) {
|
||||||
|
self.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn font(&self) -> &'static str {
|
||||||
|
self.font
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_font(&mut self, font: &'static str) {
|
||||||
|
self.font = font;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn font_size(&self) -> f32 {
|
||||||
|
self.font_size
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_font_size(&mut self, font_size: f32) {
|
||||||
|
self.font_size = font_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn color(&self) -> Color {
|
||||||
|
self.color
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_visible(&self) -> bool {
|
||||||
|
self.is_visible
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Color {
|
||||||
|
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
r,
|
||||||
|
g,
|
||||||
|
b,
|
||||||
|
a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn r(&self) -> f32 {
|
||||||
|
self.r
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_r(&mut self, r: f32) {
|
||||||
|
self.r = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn g(&self) -> f32 {
|
||||||
|
self.g
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_g(&mut self, g: f32) {
|
||||||
|
self.g = g;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn b(&self) -> f32 {
|
||||||
|
self.b
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_b(&mut self, b: f32) {
|
||||||
|
self.b = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn a(&self) -> f32 {
|
||||||
|
self.a
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_a(&mut self, a: f32) {
|
||||||
|
self.a = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_wgpu_color(color: wgpu::Color) -> Self {
|
||||||
|
Self {
|
||||||
|
r: color.r as f32,
|
||||||
|
g: color.g as f32,
|
||||||
|
b: color.b as f32,
|
||||||
|
a: color.a as f32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_wgpu(&self) -> wgpu::Color {
|
||||||
|
wgpu::Color {
|
||||||
|
r: self.r as f64,
|
||||||
|
g: self.g as f64,
|
||||||
|
b: self.b as f64,
|
||||||
|
a: self.a as f64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,10 @@ impl ComponentSet {
|
||||||
pub fn is_subset(&self, other: &ComponentSet) -> bool {
|
pub fn is_subset(&self, other: &ComponentSet) -> bool {
|
||||||
self.set.is_subset(&other.set)
|
self.set.is_subset(&other.set)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_vec(&self) -> Vec<TypeId> {
|
||||||
|
self.set.iter().cloned().collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hash for ComponentSet {
|
impl Hash for ComponentSet {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue