1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
use serde::{Deserialize, Serialize};
/// A color in the RGBA format.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const WHITE: Self = Self::rgb(255, 255, 255);
pub const BLACK: Self = Self::rgb(0, 0, 0);
pub const RED: Self = Self::rgb(255, 0, 0);
pub const GREEN: Self = Self::rgb(0, 255, 0);
pub const BLUE: Self = Self::rgb(0, 0, 255);
pub const YELLOW: Self = Self::rgb(255, 255, 0);
pub const CYAN: Self = Self::rgb(0, 255, 255);
pub const MAGENTA: Self = Self::rgb(255, 0, 255);
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::rgba(r, g, b, 255)
}
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
}
impl From<u32> for Color {
fn from(value: u32) -> Self {
let r = ((value >> 24) & 0xFF) as u8;
let g = ((value >> 16) & 0xFF) as u8;
let b = ((value >> 8) & 0xFF) as u8;
let a = (value & 0xFF) as u8;
Color::rgba(r, g, b, a)
}
}
impl From<Color> for u32 {
fn from(value: Color) -> Self {
let r = value.r as u32;
let g = value.g as u32;
let b = value.b as u32;
let a = value.a as u32;
(r << 24) | (g << 16) | (b << 8) | a
}
}
#[cfg(feature = "bevy")]
mod _bevy {
use super::*;
impl From<bevy::color::Color> for Color {
fn from(value: bevy::color::Color) -> Self {
let value = value.to_srgba();
Self::rgba(
(value.red * 255.0) as u8,
(value.green * 255.0) as u8,
(value.blue * 255.0) as u8,
(value.alpha * 255.0) as u8,
)
}
}
impl From<Color> for bevy::color::Color {
fn from(value: Color) -> Self {
bevy::color::Color::srgba(
value.r as f32 / 255.0,
value.g as f32 / 255.0,
value.b as f32 / 255.0,
value.a as f32 / 255.0,
)
}
}
}
#[cfg(feature = "image")]
mod _image {
use super::*;
impl From<image::Rgba<u8>> for Color {
fn from(value: image::Rgba<u8>) -> Self {
Self::rgba(value[0], value[1], value[2], value[3])
}
}
impl From<Color> for image::Rgba<u8> {
fn from(value: Color) -> Self {
[value.r, value.g, value.b, value.a].into()
}
}
}
pub mod textures {
use std::borrow::Cow;
use serde::{Deserialize, Serialize};
use crate::{
content::ContentId,
math::{Rectangle, UVec2},
};
use super::Color;
/// Options for creating a texture.
#[derive(Clone, Serialize, Deserialize)]
pub struct TextureCreationOptions<'a> {
/// The width of the texture.
pub width: u32,
/// The height of the texture.
pub height: u32,
/// The data of the texture. This is currently a placeholder for future use.
pub data: Option<Cow<'a, [u8]>>,
}
impl From<(u32, u32)> for TextureCreationOptions<'_> {
fn from((width, height): (u32, u32)) -> Self {
Self {
width,
height,
data: None,
}
}
}
/// A handle to a texture.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(transparent)]
pub struct TextureHandle(u32);
#[cfg(feature = "internal")]
impl TextureHandle {
/// Create a new texture handle.
pub fn new(value: u32) -> Self {
Self(value)
}
/// Get the ID of the texture handle.
pub fn id(&self) -> u32 {
self.0
}
}
/// An action to perform on a texture.
#[derive(Clone, Serialize, Deserialize)]
pub enum TextureAction {
/// Clear the texture with a color.
Clear(Color),
/// Draw pixels on the texture.
DrawPixels(Box<[DrawPixel]>),
/// Draw a rectangle on the texture.
DrawRect {
start: UVec2,
end: UVec2,
color: Color,
},
/// Draw text on the texture.
DrawText {
font: ContentId,
text: String,
top_left: UVec2,
letter_spacing: u32,
full_color: Option<Color>,
alpha_mode: AlphaMode,
},
// DrawTexture {
// texture: ContentId,
// options: DrawTextureOpts,
// },
/// Draw a script texture on the texture.
DrawScriptTexture {
handle: TextureHandle,
options: DrawTextureOpts,
},
}
/// Controls how alpha (transparency) is handled when drawing.
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
pub enum AlphaMode {
/// The texture is drawn completely opaque, ignoring alpha values.
/// Any pixels drawn will completely replace the existing pixels.
#[default]
Opaque,
/// Alpha values below the threshold are considered fully transparent,
/// while values above or equal to the threshold are considered fully opaque.
/// The threshold should be between 0.0 and 1.0.
Mask(f32),
/// Alpha values are used to blend the new pixels with existing pixels.
/// The alpha channel determines the opacity of each pixel being drawn.
Blend,
}
/// Options for drawing a texture.
#[derive(Default, Clone, Copy, Serialize, Deserialize)]
pub struct DrawTextureOpts {
/// The source rectangle of the texture to draw.
pub source_rect: Option<Rectangle>,
/// The target rectangle of the texture to draw to.
pub target_rect: Option<Rectangle>,
}
/// A pixel to draw on a texture.
#[derive(Clone, Copy, Serialize, Deserialize)]
pub struct DrawPixel {
/// The position of the pixel.
pub pos: UVec2,
/// The color of the pixel.
pub color: Color,
}
impl From<(UVec2, Color)> for DrawPixel {
fn from((position, color): (UVec2, Color)) -> Self {
Self {
pos: position,
color,
}
}
}
impl From<(u32, u32, Color)> for DrawPixel {
fn from((x, y, color): (u32, u32, Color)) -> Self {
Self {
pos: UVec2 { x, y },
color,
}
}
}
}