From 610d309ead101e9b372711f6c65b8085241cbb29 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 2 May 2024 00:43:24 +0200 Subject: [PATCH] dynamic crosshair stuff --- kubi/src/settings.rs | 96 +++++++++++++++++++------------------ kubi/src/ui/crosshair_ui.rs | 21 ++++---- 2 files changed, 62 insertions(+), 55 deletions(-) diff --git a/kubi/src/settings.rs b/kubi/src/settings.rs index ad0ca27..a0abbbe 100644 --- a/kubi/src/settings.rs +++ b/kubi/src/settings.rs @@ -1,47 +1,49 @@ -use shipyard::{Unique, AllStoragesView}; - -pub enum FullscreenMode { - Borderless, - Exclusive, -} - -pub struct FullscreenSettings { - pub mode: FullscreenMode, -} - -#[derive(Unique)] -pub struct GameSettings { - pub vsync: bool, - pub fullscreen: Option, - pub msaa: Option, - pub max_anisotropy: Option, - /// there's a 1 chunk border of loaded but invisible around this - pub render_distance: u8, - pub mouse_sensitivity: f32, - pub debug_draw_current_chunk_border: bool, -} -impl Default for GameSettings { - fn default() -> Self { - Self { - vsync: false, - fullscreen: None, - msaa: Some(4), - max_anisotropy: Some(16), - render_distance: match true { - cfg!(debug_assertions) => 5, - cfg!(target_os = "android") => 6, - #[allow(unreachable_patterns)] _ => 7, - }, - mouse_sensitivity: 1., - debug_draw_current_chunk_border: false, //cfg!(not(target_os = "android")) && cfg!(debug_assertions), - } - } -} - -pub fn load_settings( - storages: AllStoragesView -) { - log::info!("loading game settings"); - //todo - storages.add_unique(GameSettings::default()); -} +use shipyard::{Unique, AllStoragesView}; + +pub enum FullscreenMode { + Borderless, + Exclusive, +} + +pub struct FullscreenSettings { + pub mode: FullscreenMode, +} + +#[derive(Unique)] +pub struct GameSettings { + pub vsync: bool, + pub fullscreen: Option, + pub msaa: Option, + pub max_anisotropy: Option, + /// there's a 1 chunk border of loaded but invisible around this + pub render_distance: u8, + pub mouse_sensitivity: f32, + pub debug_draw_current_chunk_border: bool, + pub dynamic_crosshair: bool, +} +impl Default for GameSettings { + fn default() -> Self { + Self { + vsync: false, + fullscreen: None, + msaa: Some(4), + max_anisotropy: Some(16), + render_distance: match true { + cfg!(debug_assertions) => 5, + cfg!(target_os = "android") => 6, + #[allow(unreachable_patterns)] _ => 7, + }, + mouse_sensitivity: 1., + debug_draw_current_chunk_border: false, //cfg!(not(target_os = "android")) && cfg!(debug_assertions), + dynamic_crosshair: true, + } + } +} + +pub fn load_settings( + storages: AllStoragesView +) { + log::info!("loading game settings"); + //todo + storages.add_unique(GameSettings::default()); +} diff --git a/kubi/src/ui/crosshair_ui.rs b/kubi/src/ui/crosshair_ui.rs index 4b7213c..b62c498 100644 --- a/kubi/src/ui/crosshair_ui.rs +++ b/kubi/src/ui/crosshair_ui.rs @@ -1,6 +1,6 @@ use std::f32::consts::PI; -use glam::uvec2; +use glam::{uvec2, Vec2}; use hui::{ draw::{ImageHandle, TextureFormat}, element::{container::Container, image::Image, transformer::ElementTransformExt, UiElementExt}, @@ -8,7 +8,7 @@ use hui::{ size }; use shipyard::{AllStoragesViewMut, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View}; -use crate::{hui_integration::UiState, player::MainPlayer, rendering::WindowSize, world::raycast::LookingAtBlock}; +use crate::{hui_integration::UiState, player::MainPlayer, rendering::WindowSize, settings::GameSettings, world::raycast::LookingAtBlock}; const CROSSHAIR_SIZE: usize = 9; const CROSSHAIR: &[u8] = &[ @@ -38,10 +38,15 @@ pub fn draw_crosshair( size: UniqueView, player: View, raycast: View, + settings: UniqueView, ) { - let mut cursor_active = false; - if let Some((_, raycast)) = (&player, &raycast).iter().next() { - cursor_active = raycast.0.is_some(); + let mut active = false; + if settings.dynamic_crosshair { + if let Some((_, raycast)) = (&player, &raycast).iter().next() { + active = raycast.0.is_some(); + } + } else { + active = true; } Container::default() @@ -49,11 +54,11 @@ pub fn draw_crosshair( .with_align(Alignment::Center) .with_children(|ui| { Image::new(crosshair.0) - .with_color((1., 1., 1., 0.5)) + .with_color((1., 1., 1., if active { 0.5 } else { 0.3 })) .with_size(size!(18, 18)) .transform() - .scale(glam::Vec2::splat(if cursor_active { 1. } else { 0.66 })) - .rotate(if cursor_active { 0. } else { PI / 4. }) + .scale(Vec2::splat(if active { 1. } else { 0.66 })) + .rotate(if active { 0. } else { PI / 4. }) .add_child(ui); }) .add_root(&mut ui.hui, uvec2(size.0.x & !1, size.0.y & !1).as_vec2());