Skip to content

Commit

Permalink
Merge branch 'master' into image-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrgani authored Oct 21, 2024
2 parents 2ff39dd + 455f3fb commit 1a079a0
Show file tree
Hide file tree
Showing 32 changed files with 103 additions and 92 deletions.
19 changes: 17 additions & 2 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,24 @@ impl Camera2D {
///
/// Point is a screen space position, often mouse x and y.
pub fn screen_to_world(&self, point: Vec2) -> Vec2 {
let dims = self
.viewport()
.map(|(vx, vy, vw, vh)| Rect {
x: vx as f32,
y: screen_height() - (vy + vh) as f32,
w: vw as f32,
h: vh as f32,
})
.unwrap_or(Rect {
x: 0.0,
y: 0.0,
w: screen_width(),
h: screen_height(),
});

let point = vec2(
point.x / screen_width() * 2. - 1.,
1. - point.y / screen_height() * 2.,
(point.x - dims.x) / dims.w * 2. - 1.,
1. - (point.y - dims.y) / dims.h * 2.,
);
let inv_mat = self.matrix().inverse();
let transform = inv_mat.transform_point3(vec3(point.x, point.y, 0.));
Expand Down
4 changes: 2 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ impl Color {
}

/// Create a vec4 of red, green, blue, and alpha components.
pub fn to_vec(&self) -> glam::Vec4 {
pub const fn to_vec(&self) -> glam::Vec4 {
glam::Vec4::new(self.r, self.g, self.b, self.a)
}

/// Create a color from a vec4 of red, green, blue, and alpha components.
pub fn from_vec(vec: glam::Vec4) -> Self {
pub const fn from_vec(vec: glam::Vec4) -> Self {
Self::new(vec.x, vec.y, vec.z, vec.w)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl AnimatedSprite {
}

/// Currently chosen animation
pub fn current_animation(&self) -> usize {
pub const fn current_animation(&self) -> usize {
self.current_animation
}

Expand Down
2 changes: 1 addition & 1 deletion src/experimental/camera/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Default for Camera {
}

impl Camera {
pub fn new(offset: Vec2, scale: f32) -> Self {
pub const fn new(offset: Vec2, scale: f32) -> Self {
Self {
offset,
scale,
Expand Down
6 changes: 3 additions & 3 deletions src/experimental/coroutines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ enum CoroutineState {
}

impl CoroutineState {
pub fn is_value(&self) -> bool {
pub const fn is_value(&self) -> bool {
matches!(self, CoroutineState::Value(_))
}

pub fn is_nothing(&self) -> bool {
pub const fn is_nothing(&self) -> bool {
matches!(self, CoroutineState::Nothing)
}

Expand Down Expand Up @@ -256,7 +256,7 @@ impl Future for TimerDelayFuture {
}
}

pub fn wait_seconds(time: f32) -> TimerDelayFuture {
pub const fn wait_seconds(time: f32) -> TimerDelayFuture {
TimerDelayFuture {
remaining_time: time,
}
Expand Down
10 changes: 5 additions & 5 deletions src/experimental/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<T: 'static> Clone for Handle<T> {
impl<T: 'static> Copy for Handle<T> {}

impl<T> Handle<T> {
pub fn null() -> Handle<T> {
pub const fn null() -> Handle<T> {
Handle {
id: None,
_marker: PhantomData,
Expand All @@ -75,7 +75,7 @@ impl<T> Handle<T> {
HandleUntyped(self.id.unwrap())
}

pub fn as_trait<T1: ?Sized>(&self) {}
pub const fn as_trait<T1: ?Sized>(&self) {}
}

pub(crate) struct Lens<T> {
Expand Down Expand Up @@ -127,7 +127,7 @@ pub struct RefMut<T: 'static> {
}

impl<T: 'static> RefMut<T> {
pub fn handle(&self) -> Handle<T> {
pub const fn handle(&self) -> Handle<T> {
Handle {
id: self.handle.id,
_marker: PhantomData,
Expand Down Expand Up @@ -190,7 +190,7 @@ pub struct RefMutAny<'a> {
}

impl<'a> RefMutAny<'a> {
pub fn handle<T>(&self) -> Handle<T> {
pub const fn handle<T>(&self) -> Handle<T> {
Handle {
id: Some(self.handle.0),
_marker: PhantomData,
Expand Down Expand Up @@ -700,6 +700,6 @@ pub(crate) fn in_fixed_update() -> bool {
unsafe { get_scene() }.in_fixed_update
}

pub(crate) fn fixed_frame_time() -> f32 {
pub(crate) const fn fixed_frame_time() -> f32 {
CONST_FPS as _
}
4 changes: 2 additions & 2 deletions src/experimental/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<T: scene::Node + 'static> StateMachine<T> {
}
}

pub fn state(&self) -> usize {
pub const fn state(&self) -> usize {
match self {
StateMachine::Ready(state_machine) => state_machine.state(),
StateMachine::InUse {
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<T: 'static> StateMachineOwned<T> {
self.next_state = Some(state);
}

pub fn state(&self) -> usize {
pub const fn state(&self) -> usize {
self.current_state
}

Expand Down
6 changes: 3 additions & 3 deletions src/math/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ pub struct Circle {
}

impl Circle {
pub fn new(x: f32, y: f32, r: f32) -> Self {
pub const fn new(x: f32, y: f32, r: f32) -> Self {
Circle { x, y, r }
}

pub fn point(&self) -> Vec2 {
pub const fn point(&self) -> Vec2 {
vec2(self.x, self.y)
}

pub fn radius(&self) -> f32 {
pub const fn radius(&self) -> f32 {
self.r
}

Expand Down
12 changes: 6 additions & 6 deletions src/math/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ impl Rect {
/// * `y` - y-coordinate of the top-left corner.
/// * `w` - width of the `Rect`, going to the right.
/// * `h` - height of the `Rect`, going down.
pub fn new(x: f32, y: f32, w: f32, h: f32) -> Rect {
pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Rect {
Rect { x, y, w, h }
}

/// Returns the top-left corner of the `Rect`.
pub fn point(&self) -> Vec2 {
pub const fn point(&self) -> Vec2 {
vec2(self.x, self.y)
}

/// Returns the size (width and height) of the `Rect`.
pub fn size(&self) -> Vec2 {
pub const fn size(&self) -> Vec2 {
vec2(self.w, self.h)
}

Expand All @@ -37,7 +37,7 @@ impl Rect {
}

/// Returns the left edge of the `Rect`
pub fn left(&self) -> f32 {
pub const fn left(&self) -> f32 {
self.x
}

Expand All @@ -47,7 +47,7 @@ impl Rect {
}

/// Returns the top edge of the `Rect`
pub fn top(&self) -> f32 {
pub const fn top(&self) -> f32 {
self.y
}

Expand Down Expand Up @@ -128,7 +128,7 @@ pub struct RectOffset {
}

impl RectOffset {
pub fn new(left: f32, right: f32, top: f32, bottom: f32) -> RectOffset {
pub const fn new(left: f32, right: f32, top: f32, bottom: f32) -> RectOffset {
RectOffset {
left,
right,
Expand Down
8 changes: 4 additions & 4 deletions src/quad_gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct DrawCall {
}

impl DrawCall {
fn new(
const fn new(
texture: Option<miniquad::TextureId>,
model: glam::Mat4,
draw_mode: DrawMode,
Expand Down Expand Up @@ -534,7 +534,7 @@ impl PipelinesStorage {
GlPipeline(id)
}

fn get(&self, draw_mode: DrawMode, depth_enabled: bool) -> GlPipeline {
const fn get(&self, draw_mode: DrawMode, depth_enabled: bool) -> GlPipeline {
match (draw_mode, depth_enabled) {
(DrawMode::Triangles, false) => Self::TRIANGLES_PIPELINE,
(DrawMode::Triangles, true) => Self::TRIANGLES_DEPTH_PIPELINE,
Expand Down Expand Up @@ -814,11 +814,11 @@ impl QuadGl {
crate::get_context().projection_matrix()
}

pub fn get_active_render_pass(&self) -> Option<RenderPass> {
pub const fn get_active_render_pass(&self) -> Option<RenderPass> {
self.state.render_pass
}

pub fn is_depth_test_enabled(&self) -> bool {
pub const fn is_depth_test_enabled(&self) -> bool {
self.state.depth_test_enable
}

Expand Down
2 changes: 1 addition & 1 deletion src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub struct Frame {
}

impl Frame {
fn new() -> Frame {
const fn new() -> Frame {
Frame {
full_frame_time: 0.0,
zones: vec![],
Expand Down
4 changes: 2 additions & 2 deletions src/text/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ impl Atlas {
self.sprites.get(&key).cloned()
}

pub fn width(&self) -> u16 {
pub const fn width(&self) -> u16 {
self.image.width() as u16
}

pub fn height(&self) -> u16 {
pub const fn height(&self) -> u16 {
self.image.height() as u16
}

Expand Down
12 changes: 4 additions & 8 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl TexturesContext {
// fn remove(&mut self, texture: TextureSlotId) {
// self.textures.remove(texture);
// }
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.textures.len()
}
pub fn garbage_collect(&mut self, ctx: &mut miniquad::Context) {
Expand Down Expand Up @@ -710,7 +710,7 @@ impl Texture2D {
},
}
}
pub(crate) fn unmanaged(texture: miniquad::TextureId) -> Texture2D {
pub(crate) const fn unmanaged(texture: miniquad::TextureId) -> Texture2D {
Texture2D {
texture: TextureHandle::Unmanaged(texture),
}
Expand Down Expand Up @@ -761,11 +761,7 @@ impl Texture2D {
let height = img.height() as u16;
let bytes = img.into_raw();

let t = Self::from_rgba8(width, height, &bytes);

let ctx = get_context();

t
Self::from_rgba8(width, height, &bytes)
}

/// Creates a Texture2D from an [Image].
Expand All @@ -775,7 +771,7 @@ impl Texture2D {

/// Creates a Texture2D from a miniquad
/// [Texture](https://docs.rs/miniquad/0.3.0-alpha/miniquad/graphics/struct.Texture.html)
pub fn from_miniquad_texture(texture: miniquad::TextureId) -> Texture2D {
pub const fn from_miniquad_texture(texture: miniquad::TextureId) -> Texture2D {
Texture2D {
texture: TextureHandle::Unmanaged(texture),
}
Expand Down
2 changes: 1 addition & 1 deletion src/texture/slotmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl TextureIdSlotMap {
}

/// Returns the number of elements in the slot map.
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.num_elems as usize
}

Expand Down
12 changes: 6 additions & 6 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ impl Window {
);
}

pub fn top_level(&self) -> bool {
pub const fn top_level(&self) -> bool {
self.parent.is_none()
}

pub fn full_rect(&self) -> Rect {
pub const fn full_rect(&self) -> Rect {
Rect::new(self.position.x, self.position.y, self.size.x, self.size.y)
}

Expand All @@ -215,7 +215,7 @@ impl Window {
self.cursor.area.y = position.y + self.title_height + self.window_margin.top;
}

pub fn title_rect(&self) -> Rect {
pub const fn title_rect(&self) -> Rect {
Rect::new(
self.position.x,
self.position.y,
Expand Down Expand Up @@ -267,7 +267,7 @@ pub(crate) struct TabSelector {
}

impl TabSelector {
fn new() -> Self {
const fn new() -> Self {
TabSelector {
counter: 0,
wants: None,
Expand Down Expand Up @@ -970,7 +970,7 @@ impl Ui {
)
}

pub fn is_mouse_captured(&self) -> bool {
pub const fn is_mouse_captured(&self) -> bool {
self.input.cursor_grabbed
}

Expand Down Expand Up @@ -998,7 +998,7 @@ impl Ui {
self.active_window.map_or(false, |wnd| self.is_focused(wnd))
}

pub fn is_dragging(&self) -> bool {
pub const fn is_dragging(&self) -> bool {
self.dragging.is_some()
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct Cursor {
}

impl Cursor {
pub fn new(area: Rect, margin: f32) -> Cursor {
pub const fn new(area: Rect, margin: f32) -> Cursor {
Cursor {
margin,
x: margin,
Expand Down
6 changes: 3 additions & 3 deletions src/ui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ pub struct Input {
}

impl Input {
pub fn is_mouse_down(&self) -> bool {
pub const fn is_mouse_down(&self) -> bool {
self.is_mouse_down && self.cursor_grabbed == false && self.window_active
}

pub fn click_down(&self) -> bool {
pub const fn click_down(&self) -> bool {
self.click_down && self.cursor_grabbed == false && self.window_active
}

pub fn click_up(&self) -> bool {
pub const fn click_up(&self) -> bool {
self.click_up && self.cursor_grabbed == false && self.window_active
}

Expand Down
Loading

0 comments on commit 1a079a0

Please sign in to comment.