diff options
| author | HampusM <hampus@hampusmat.com> | 2026-09-22 18:22:46 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-09-22 18:22:46 +0200 |
| commit | c73ab25c9dbe40284cfd38beaf31e2a20a9810de (patch) | |
| tree | 51c5817a0c252a172c03a47e36886e563a4e623d /engine/src/ui | |
| parent | 2ccda0cd5f81909fddc74d95f960f781a64bcae1 (diff) | |
refactor(engine): fix portion of clippy lints
Diffstat (limited to 'engine/src/ui')
| -rw-r--r-- | engine/src/ui/dear_imgui.rs | 123 | ||||
| -rw-r--r-- | engine/src/ui/view/transform_3d.rs | 2 | ||||
| -rw-r--r-- | engine/src/ui/view/world.rs | 386 |
3 files changed, 257 insertions, 254 deletions
diff --git a/engine/src/ui/dear_imgui.rs b/engine/src/ui/dear_imgui.rs index 0a50ff7..c5f7ce2 100644 --- a/engine/src/ui/dear_imgui.rs +++ b/engine/src/ui/dear_imgui.rs @@ -130,12 +130,14 @@ pub struct Extension impl Extension { + #[must_use] pub fn with_start_enabled(mut self, start_enabled: bool) -> Self { self.start_enabled = start_enabled; self } + #[must_use] pub fn with_settings_ini_file( mut self, settings_ini_file_path: Option<PathBuf>, @@ -164,10 +166,12 @@ impl ecs::extension::Extension for Extension tracing::error!("Failed to set path of imgui settings ini file: {err}"); } - assert!( - dear_imgui_rs::HAS_FREETYPE, - "Freetype font rasterizer support is not enabled" - ); + const { + assert!( + dear_imgui_rs::HAS_FREETYPE, + "Freetype font rasterizer support is not enabled" + ); + } unsafe { context.ctx.font_atlas().add_font_from_memory_ttf( @@ -223,8 +227,8 @@ fn handle_window_changed( .set_display_framebuffer_scale([hidpi_factor as f32, hidpi_factor as f32]); let window_size_logical = PhysicalSize { - width: window.inner_size.width as f64, - height: window.inner_size.height as f64, + width: f64::from(window.inner_size.width), + height: f64::from(window.inner_size.height), } .to_logical::<f64>(hidpi_factor); @@ -272,7 +276,7 @@ fn update( window_surface.id, assets, render_passes, - &shader_context, + shader_context, ) else { *state = State::NotInitialized; return Ok(()); @@ -341,11 +345,9 @@ fn initialize_context( shader_context: &ShaderContext, ) -> Option<(AssetHandle<ShaderModuleSource>, RenderingObjectId, Mesh)> { - let shader_asset = if let Some(shader_asset) = + let Some(shader_asset) = assets.get_handle_to_loaded::<ShaderModuleSource>(SHADER_ASSET_LABEL.clone()) - { - shader_asset - } else { + else { assets.store_with_label( SHADER_ASSET_LABEL.clone(), ShaderModuleSource { @@ -362,19 +364,21 @@ fn initialize_context( return None; }; - let hidpi_factor = window.scale_factor().round(); + #[allow(clippy::cast_possible_truncation)] + let hidpi_factor = window.scale_factor().round() as f32; context .ctx .get_io_mut() - .set_display_framebuffer_scale([hidpi_factor as f32, hidpi_factor as f32]); + .set_display_framebuffer_scale([hidpi_factor, hidpi_factor]); let window_size = Dimens { width: window.inner_size.width as f32, height: window.inner_size.height as f32, }; - let window_logical_size = window_size / (hidpi_factor as f32); + #[allow(clippy::cast_possible_truncation)] + let window_logical_size = window_size / hidpi_factor; context .ctx @@ -455,6 +459,7 @@ fn update_inputs( let mouse_pos = mouse.position.to_logical::<f64>(window.scale_factor()); + #[allow(clippy::cast_possible_truncation)] io.add_mouse_pos_event([mouse_pos.x as f32, mouse_pos.y as f32]); if !mouse.curr_tick_scroll_delta.is_zero() { @@ -610,8 +615,8 @@ fn add_drawing_render_pass( height: rect.rect.h.into(), }, offset: Vec2 { - x: rect.rect.x as u32, - y: rect.rect.y as u32, + x: u32::from(rect.rect.x), + y: u32::from(rect.rect.y), }, }, }); @@ -715,7 +720,7 @@ fn add_drawing_render_pass( }, MeshNamedVertexAttr { label: VertexLabel::Color, - value: vertex.rgba().map(|elem| (elem as f32) / 255.0), + value: vertex.rgba().map(|elem| f32::from(elem) / 255.0), }, MeshNamedVertexAttr { label: VertexLabel::UvFromTopLeft, @@ -733,51 +738,47 @@ fn add_drawing_render_pass( }); for command in draw_list.commands() { - match command { - ImguiDrawCmd::Elements { count, cmd_params } => { - let Some(scissor_box) = - calc_draw_cmd_scissor_box(draw_data, &cmd_params) - else { - continue; - }; + if let ImguiDrawCmd::Elements { count, cmd_params } = command { + let Some(scissor_box) = calc_draw_cmd_scissor_box(draw_data, &cmd_params) + else { + continue; + }; - let tex_id = cmd_params.texture_id; + let tex_id = cmd_params.texture_id; - let Some(texture_object_id) = - texture_lookup.get(TextureLookupId::from(tex_id)) - else { - tracing::error!( - "Unknown texture {}. Skipping skipping draw command", - tex_id.id() - ); - continue; - }; + let Some(texture_object_id) = + texture_lookup.get(TextureLookupId::from(tex_id)) + else { + tracing::error!( + "Unknown texture {}. Skipping skipping draw command", + tex_id.id() + ); + continue; + }; - render_pass.commands.extend([ - RenderingCommand::UpdateDrawProperties( - DrawProperties { scissor_box, ..Default::default() }, - DrawPropertiesUpdateFlags::SCISSOR_BOX, - ), - RenderingCommand::SetShaderBinding( - shader_object_id, - shader_cursor.field("main_texture").binding( - ShaderBindingValue::Texture( - *texture_object_id, - ShaderBindingTextureKind::Texture2D, - ), - )?, - ), - RenderingCommand::DrawMesh( - mesh_obj_id, - DrawMeshOptions::builder() - .element_offset(cmd_params.idx_offset.try_into().unwrap()) - .vertex_offset(cmd_params.vtx_offset.try_into().unwrap()) - .element_cnt(count.try_into().unwrap()) - .build(), - ), - ]); - } - _ => {} + render_pass.commands.extend([ + RenderingCommand::UpdateDrawProperties( + DrawProperties { scissor_box, ..Default::default() }, + DrawPropertiesUpdateFlags::SCISSOR_BOX, + ), + RenderingCommand::SetShaderBinding( + shader_object_id, + shader_cursor.field("main_texture").binding( + ShaderBindingValue::Texture( + *texture_object_id, + ShaderBindingTextureKind::Texture2D, + ), + )?, + ), + RenderingCommand::DrawMesh( + mesh_obj_id, + DrawMeshOptions::builder() + .element_offset(cmd_params.idx_offset.try_into().unwrap()) + .vertex_offset(cmd_params.vtx_offset.try_into().unwrap()) + .element_cnt(count.try_into().unwrap()) + .build(), + ), + ]); } } } @@ -973,12 +974,12 @@ mod inner_context_wrapper ctx.io_mut() .set_backend_flags(dear_imgui_rs::BackendFlags::RENDERER_HAS_TEXTURES); - let _renderer_consumer = ctx.create_renderer_consumer().unwrap(); + let renderer_consumer = ctx.create_renderer_consumer().unwrap(); Self { ctx, frame: null_mut(), - _renderer_consumer, + _renderer_consumer: renderer_consumer, } } diff --git a/engine/src/ui/view/transform_3d.rs b/engine/src/ui/view/transform_3d.rs index a426b2a..9c3dbf1 100644 --- a/engine/src/ui/view/transform_3d.rs +++ b/engine/src/ui/view/transform_3d.rs @@ -124,5 +124,5 @@ fn flatten_matrix(mat: [[f32; 4]; 4]) -> [f32; 4 * 4] unreachable!(); }; - flattened.clone() + *flattened } diff --git a/engine/src/ui/view/world.rs b/engine/src/ui/view/world.rs index 6bb9755..95f29d0 100644 --- a/engine/src/ui/view/world.rs +++ b/engine/src/ui/view/world.rs @@ -39,8 +39,8 @@ const BUTTON_RED_ACTIVE: [f32; 4] = [162.0, 68.0, 51.0, 255.0]; pub struct State { spawning_entity: Option<Uid>, - popup_state: Option<PopupState>, - textures: Option<Textures>, + popup: Option<PopupState>, + icon_textures: Option<IconTextures>, } pub fn show( @@ -55,11 +55,12 @@ pub fn show( let State { spawning_entity, - popup_state, - textures, + popup: popup_state, + icon_textures, } = &mut *state; - let textures = textures.get_or_insert_with(|| Textures::new(dear_imgui_context)); + let icon_textures = + icon_textures.get_or_insert_with(|| IconTextures::new(dear_imgui_context)); let Some(frame) = dear_imgui_context.frame() else { return Ok(()); @@ -84,7 +85,7 @@ pub fn show( create_entity_widgets( frame, popup_state, - textures, + icon_textures, &ent_handle, world, &mut actions, @@ -164,7 +165,7 @@ fn create_spawn_button_widgets( fn create_entity_widgets( frame: &dear_imgui_bindings::Ui, popup_state: &mut Option<PopupState>, - textures: &Textures, + icon_textures: &IconTextures, ent_handle: &EntityHandle, world: &World, actions: &mut Actions, @@ -182,12 +183,12 @@ fn create_entity_widgets( return; } - let ent_title = create_entity_title(&ent_handle); + let ent_title = create_entity_title(ent_handle); let mut is_open = false; frame - .table(&format!("ent_{}_table", ent_handle.uid())) + .table(format!("ent_{}_table", ent_handle.uid())) .headers(false) .sizing_policy(dear_imgui_bindings::TableSizingPolicy::FixedFit) .columns([ @@ -209,7 +210,7 @@ fn create_entity_widgets( create_add_component_button_widget( frame, - &ent_handle, + ent_handle, &ent_title, popup_state, world, @@ -217,13 +218,13 @@ fn create_entity_widgets( frame.table_next_column(); - create_despawn_button_widget(frame, textures, ent_handle, actions); + create_despawn_button_widget(frame, icon_textures, ent_handle, actions); frame.table_next_column(); create_rename_entity_button_widget( frame, - textures, + icon_textures, ent_handle, &ent_title, popup_state, @@ -235,7 +236,7 @@ fn create_entity_widgets( if component_id.is_pair() { create_pair_component_widgets( frame, - textures, + icon_textures, ent_handle, component_id, world, @@ -320,15 +321,15 @@ fn create_component_widgets( ItemRef::Mutable(&mut *component), &mut component_changed, ItemInfo { - item_title: component_info.name, - item_tag: &format!( + title: component_info.name, + tag: &format!( "world_view_entity_{}_component_{}", ent_handle.uid(), component_info.name, ), - item_type_name: None, - item_type, - item_is_read_only: false, + type_name: None, + ty: item_type, + is_read_only: false, }, &[], ); @@ -342,7 +343,7 @@ fn create_component_widgets( fn create_pair_component_widgets( frame: &dear_imgui_bindings::Ui, - textures: &Textures, + icon_textures: &IconTextures, ent_handle: &EntityHandle, pair_id: Uid, world: &World, @@ -418,7 +419,7 @@ fn create_pair_component_widgets( let Some(pair_data_ty) = pair_data_comp_info.type_reflection else { frame.same_line(); - frame.image(textures.warning_icon_tex_id, [16.0, 16.0]); + frame.image(icon_textures.warning, [16.0, 16.0]); if frame.is_item_hovered() { frame.tooltip_text(format!( @@ -450,14 +451,14 @@ fn create_pair_component_widgets( ItemRef::Mutable(&mut *pair_data), &mut component_changed, ItemInfo { - item_title: pair_data_comp_info.name, - item_tag: &format!( + title: pair_data_comp_info.name, + tag: &format!( "world_view_entity_{}_pair_component_{pair_id}", ent_handle.uid(), ), - item_type_name: None, - item_type, - item_is_read_only: false, + type_name: None, + ty: item_type, + is_read_only: false, }, &[], ); @@ -519,7 +520,7 @@ fn create_add_component_button_widget( fn create_despawn_button_widget( frame: &dear_imgui_bindings::Ui, - textures: &Textures, + icon_textures: &IconTextures, ent_handle: &EntityHandle, actions: &mut Actions, ) @@ -541,8 +542,8 @@ fn create_despawn_button_widget( if frame .image_button_config( - &format!("ent_{}_despawn_button", ent_handle.uid()), - textures.despawn_icon_tex_id, + format!("ent_{}_despawn_button", ent_handle.uid()), + icon_textures.despawn, [14.0, 14.0], ) .build() @@ -553,7 +554,7 @@ fn create_despawn_button_widget( fn create_rename_entity_button_widget( frame: &dear_imgui_bindings::Ui, - textures: &Textures, + icon_textures: &IconTextures, ent_handle: &EntityHandle, ent_title: &str, popup_state: &mut Option<PopupState>, @@ -561,8 +562,8 @@ fn create_rename_entity_button_widget( { if frame .image_button_config( - &format!("ent_{}_rename_button", ent_handle.uid()), - textures.edit_icon_tex_id, + format!("ent_{}_rename_button", ent_handle.uid()), + icon_textures.edit, [16.0, 16.0], ) .build() @@ -716,7 +717,7 @@ fn enter_add_component_popup_state( search_text: String::with_capacity(20), searched_components: searchable_components.clone(), searchable_components, - selected_search_result: -1, + selected_search_result: None, new_component: None, })); @@ -755,15 +756,15 @@ fn show_add_component_popup( add_component_popup_state.searched_components = add_component_popup_state .searchable_components .iter() - .cloned() - .filter(|(_, searchable_comp_info, _)| { + .filter(|&(_, searchable_comp_info, _)| { searchable_comp_info .name .contains(&add_component_popup_state.search_text) }) + .cloned() .collect(); - add_component_popup_state.selected_search_result = -1; + add_component_popup_state.selected_search_result = None; } frame.spacing(); @@ -771,7 +772,7 @@ fn show_add_component_popup( let _item_width_token = frame.push_item_width(-1.0); - if frame + if let Some(selected_search_result_index) = frame .list_box_config("##add_component_popup_search_results") .build_extended( frame, @@ -793,8 +794,7 @@ fn show_add_component_popup( ) { let (selected_comp_id, selected_comp_info, _) = add_component_popup_state - .searched_components - [add_component_popup_state.selected_search_result as usize] + .searched_components[selected_search_result_index] .clone(); let Some(selected_component_type) = selected_comp_info.type_reflection @@ -844,14 +844,14 @@ fn show_add_component_popup( ItemRef::Mutable(&mut **new_component), &mut component_changed, ItemInfo { - item_title: new_component_info.name, - item_tag: &format!( + title: new_component_info.name, + tag: &format!( "add_component_popup_component_{}", new_component_info.name, ), - item_type_name: None, - item_type, - item_is_read_only: false, + type_name: None, + ty: item_type, + is_read_only: false, }, &[], ); @@ -914,11 +914,11 @@ pub enum ComponentUserCreatable struct ItemInfo<'a> { - item_title: &'a str, - item_tag: &'a str, - item_type_name: Option<&'static str>, - item_type: ItemType, - item_is_read_only: bool, + title: &'a str, + tag: &'a str, + type_name: Option<&'static str>, + ty: ItemType, + is_read_only: bool, } enum ItemType @@ -943,10 +943,10 @@ impl ItemType Self::Reflected(TypeReflection::Reference(ref_ty)) => { ItemType::Reflected(ref_ty.ty).spans_multiple_rows() } - Self::Color(..) => false, - Self::Reflected(TypeReflection::Literal(_)) | Self::String | Self::CowStr => { - false - } + Self::Reflected(TypeReflection::Literal(_)) + | Self::String + | Self::CowStr + | Self::Color(..) => false, Self::Reflected(_) => unimplemented!(), } } @@ -993,7 +993,7 @@ fn get_item_type(ty: Option<&'static TypeReflection>, type_id: TypeId) } if let Some(ty) = ty { - return Some(ItemType::Reflected(ty)); + Some(ItemType::Reflected(ty)) } else if type_id == TypeId::of::<String>() { Some(ItemType::String) } else if type_id == TypeId::of::<Cow<'static, str>>() { @@ -1086,7 +1086,7 @@ impl<Value> Deref for OwnedOrRefMut<'_, Value> fn deref(&self) -> &Self::Target { match self { - Self::Ref(value) => *value, + Self::Ref(value) => value, Self::Owned(value) => value, } } @@ -1097,7 +1097,7 @@ impl<Value> DerefMut for OwnedOrRefMut<'_, Value> fn deref_mut(&mut self) -> &mut Self::Target { match self { - Self::Ref(value) => *value, + Self::Ref(value) => value, Self::Owned(value) => value, } } @@ -1111,7 +1111,7 @@ fn create_item_title_widget( ) { if !matches!(item_type, ItemType::Reflected(TypeReflection::Reference(_))) { - frame.text(&item_title); + frame.text(item_title); if let Some(item_type_name) = item_type_name { if frame.is_item_hovered() { @@ -1126,11 +1126,11 @@ fn add_item_to_frame<'a>( mut item: ItemRef<'_>, data_changed: &mut bool, ItemInfo { - item_title, - item_tag, - item_type_name, - item_type, - item_is_read_only, + title: item_title, + tag: item_tag, + type_name: item_type_name, + ty: item_type, + is_read_only: item_is_read_only, }: ItemInfo<'a>, prev_item_tags: &[&'a str], ) @@ -1176,85 +1176,85 @@ fn add_item_to_frame<'a>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::I8 => create_scalar_item_input::<i8>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::U16 => create_scalar_item_input::<u16>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::I16 => create_scalar_item_input::<i16>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::U32 => create_scalar_item_input::<u32>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::I32 => create_scalar_item_input::<i32>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::U64 => create_scalar_item_input::<u64>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::I64 => create_scalar_item_input::<i64>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::F32 => create_scalar_item_input::<f32>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::F64 => create_scalar_item_input::<f64>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::Usize => create_scalar_item_input::<usize>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::Isize => create_scalar_item_input::<isize>( frame, &mut item, data_changed, - &item_tag, - &prev_item_tags, + item_tag, + prev_item_tags, ), LiteralType::U128 => { let item = match item { @@ -1315,7 +1315,7 @@ fn add_item_to_frame<'a>( }; if frame.checkbox( - create_item_label(&item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), item, ) { *data_changed = true; @@ -1383,11 +1383,11 @@ fn add_item_to_frame<'a>( item_item, data_changed, ItemInfo { - item_title: array_item_name.as_ref(), - item_tag: array_item_name.as_ref(), - item_type_name: Some(array_type.item_type_name()), - item_type: array_item_type, - item_is_read_only, + title: array_item_name.as_ref(), + tag: array_item_name.as_ref(), + type_name: Some(array_type.item_type_name()), + ty: array_item_type, + is_read_only: item_is_read_only, }, &prev_item_tags, ); @@ -1428,11 +1428,11 @@ fn add_item_to_frame<'a>( ItemRef::Immutable(item_item), &mut slice_item_changed, ItemInfo { - item_title: &item_name, - item_tag: &item_name, - item_type_name: Some(slice_type.item_type_name()), - item_type: slice_item_type, - item_is_read_only: true, + title: &item_name, + tag: &item_name, + type_name: Some(slice_type.item_type_name()), + ty: slice_item_type, + is_read_only: true, }, &prev_item_tags, ); @@ -1463,11 +1463,11 @@ fn add_item_to_frame<'a>( ItemRef::Immutable(derefed_item), &mut derefed_changed, ItemInfo { - item_title, - item_tag: "derefed".into(), - item_type_name, - item_type: ref_item_type, - item_is_read_only: true, + title: item_title, + tag: "derefed", + type_name: item_type_name, + ty: ref_item_type, + is_read_only: true, }, prev_item_tags, ); @@ -1494,7 +1494,7 @@ fn add_item_to_frame<'a>( if frame .input_text( - create_item_label(&item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), item, ) .build() @@ -1522,7 +1522,7 @@ fn add_item_to_frame<'a>( if frame .input_text( - create_item_label(&item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), item.to_mut(), ) .build() @@ -1537,7 +1537,7 @@ fn add_item_to_frame<'a>( if frame .color_edit3_config( - create_item_label(&item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), &mut scratch, ) .display_mode(dear_imgui_bindings::ColorDisplayMode::Hex) @@ -1554,14 +1554,14 @@ fn add_item_to_frame<'a>( let mut item = item.into_writable::<Rgb<u8>>(); let mut scratch = [ - item.r as f32 / 255.0, - item.g as f32 / 255.0, - item.b as f32 / 255.0, + f32::from(item.r) / 255.0, + f32::from(item.g) / 255.0, + f32::from(item.b) / 255.0, ]; if frame .color_edit3_config( - create_item_label(&item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), &mut scratch, ) .display_mode(dear_imgui_bindings::ColorDisplayMode::Hex) @@ -1570,9 +1570,9 @@ fn add_item_to_frame<'a>( let [new_r, new_g, new_b] = scratch; *item = Rgb { - r: (new_r * 255.0) as u8, - g: (new_g * 255.0) as u8, - b: (new_b * 255.0) as u8, + r: float_to_u8_checked(new_r * 255.0).unwrap_or(0), + g: float_to_u8_checked(new_g * 255.0).unwrap_or(0), + b: float_to_u8_checked(new_b * 255.0).unwrap_or(0), }; *data_changed = true; @@ -1585,7 +1585,7 @@ fn add_item_to_frame<'a>( if frame .color_edit4_config( - create_item_label(&item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), &mut scratch, ) .display_mode(dear_imgui_bindings::ColorDisplayMode::Hex) @@ -1607,15 +1607,15 @@ fn add_item_to_frame<'a>( let mut item = item.into_writable::<Rgba<u8>>(); let mut scratch = [ - item.r as f32 / 255.0, - item.g as f32 / 255.0, - item.b as f32 / 255.0, - item.a as f32 / 255.0, + f32::from(item.r) / 255.0, + f32::from(item.g) / 255.0, + f32::from(item.b) / 255.0, + f32::from(item.a) / 255.0, ]; if frame .color_edit4_config( - create_item_label(&item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), &mut scratch, ) .display_mode(dear_imgui_bindings::ColorDisplayMode::Hex) @@ -1624,17 +1624,16 @@ fn add_item_to_frame<'a>( let [new_r, new_g, new_b, new_a] = scratch; *item = Rgba { - r: (new_r * 255.0) as u8, - g: (new_g * 255.0) as u8, - b: (new_b * 255.0) as u8, - a: (new_a * 255.0) as u8, + r: float_to_u8_checked(new_r * 255.0).unwrap_or(0), + g: float_to_u8_checked(new_g * 255.0).unwrap_or(0), + b: float_to_u8_checked(new_b * 255.0).unwrap_or(0), + a: float_to_u8_checked(new_a * 255.0).unwrap_or(0), }; *data_changed = true; } } - ItemType::Color(ColorItemType::Rgb, _) => unreachable!(), - ItemType::Color(ColorItemType::Rgba, _) => unreachable!(), + ItemType::Color(ColorItemType::Rgb | ColorItemType::Rgba, _) => unreachable!(), ItemType::Reflected(_) => unimplemented!(), } } @@ -1667,7 +1666,7 @@ fn create_scalar_item_input<Scalar>( if frame .input_scalar( - create_item_label(item_tag, "value_input", &prev_item_tags), + create_item_label(item_tag, "value_input", prev_item_tags), item, ) .build() @@ -1682,7 +1681,7 @@ fn item_layout_table<'frame>( ) -> dear_imgui_bindings::TableBuilder<'frame> { frame - .table(create_item_label("item_layout", "table", &prev_item_tags)) + .table(create_item_label("item_layout", "table", prev_item_tags)) .headers(false) .sizing_policy(dear_imgui_bindings::TableSizingPolicy::StretchSame) .columns([ @@ -1727,8 +1726,7 @@ fn create_struct_widgets( let field_name = field .name - .map(Cow::Borrowed) - .unwrap_or_else(|| field_index.to_string().into()); + .map_or_else(|| field_index.to_string().into(), Cow::Borrowed); let Ok(field_item) = item.get_struct_field(struct_ty, field_index) else { unreachable!(); @@ -1748,11 +1746,11 @@ fn create_struct_widgets( field_item, data_changed, ItemInfo { - item_title: field_name.as_ref(), - item_tag: field_name.as_ref(), - item_type_name: Some(field.type_name()), - item_type: field_item_type, - item_is_read_only: item_is_read_only + title: field_name.as_ref(), + tag: field_name.as_ref(), + type_name: Some(field.type_name()), + ty: field_item_type, + is_read_only: item_is_read_only || matches!( field.visibility, Visibility::Private | Visibility::PubScoped(_) @@ -1768,8 +1766,7 @@ fn create_struct_widgets( { let field_name = field .name - .map(Cow::Borrowed) - .unwrap_or_else(|| field_index.to_string().into()); + .map_or_else(|| field_index.to_string().into(), Cow::Borrowed); let Ok(field_item) = item.get_struct_field(struct_ty, field_index) else { unreachable!(); @@ -1789,11 +1786,11 @@ fn create_struct_widgets( field_item, data_changed, ItemInfo { - item_title: field_name.as_ref(), - item_tag: field_name.as_ref(), - item_type_name: Some(field.type_name()), - item_type: field_item_type, - item_is_read_only: item_is_read_only + title: field_name.as_ref(), + tag: field_name.as_ref(), + type_name: Some(field.type_name()), + ty: field_item_type, + is_read_only: item_is_read_only || matches!( field.visibility, Visibility::Private | Visibility::PubScoped(_) @@ -1821,7 +1818,7 @@ fn create_enum_widgets( if create_combo_box( frame, - create_item_label(&item_tag, "variant_select", &prev_item_tags), + create_item_label(item_tag, "variant_select", prev_item_tags), &mut curr_variant_index, enum_type.variants, |variant| { @@ -1849,8 +1846,7 @@ fn create_enum_widgets( disabled: disabled.is_some(), hover_tooltip: disabled.map(|(bad_field_name, bad_field_reason)| { format!( - "Variant cannot be selected since field {} {}", - bad_field_name, bad_field_reason + "Variant cannot be selected since field {bad_field_name} {bad_field_reason}" ) .into() }), @@ -1869,8 +1865,7 @@ fn create_enum_widgets( &mut new_variant .fields .iter() - .map(|fields| fields.fields()) - .flatten() + .flat_map(engine_reflection::EnumVariantFields::fields) .map(|field| { let Some(field_type) = field.type_reflection() else { // Variants with any field that is missing type reflection is @@ -1946,11 +1941,11 @@ fn create_enum_widgets( field_item, data_changed, ItemInfo { - item_title: field_name.as_ref(), - item_tag: field_name.as_ref(), - item_type_name: Some(field.type_name()), - item_type: field_item_type, - item_is_read_only: item_is_read_only, + title: field_name.as_ref(), + tag: field_name.as_ref(), + type_name: Some(field.type_name()), + ty: field_item_type, + is_read_only: item_is_read_only, }, &prev_item_tags, ); @@ -1986,11 +1981,11 @@ fn create_enum_widgets( field_item, data_changed, ItemInfo { - item_title: field_name.as_ref(), - item_tag: field_name.as_ref(), - item_type_name: Some(field.type_name()), - item_type: field_item_type, - item_is_read_only: item_is_read_only, + title: field_name.as_ref(), + tag: field_name.as_ref(), + type_name: Some(field.type_name()), + ty: field_item_type, + is_read_only: item_is_read_only, }, &prev_item_tags, ); @@ -2003,7 +1998,7 @@ fn create_item_label(item_tag: &str, value_name: &str, prev_item_tags: &[&str]) let mut item_label = String::with_capacity( 2 + item_tag.len() + prev_item_tags.len() - + prev_item_tags.iter().max().unwrap_or(&"".into()).len(), + + prev_item_tags.iter().max().unwrap_or(&"").len(), ); item_label.push_str("##"); @@ -2052,7 +2047,11 @@ fn get_whole_type_is_user_editable( } ItemType::Reflected(TypeReflection::Enum(enum_ty)) => { enum_ty.variants.iter().any(|variant| { - for field in variant.fields.iter().flat_map(|fields| fields.fields()) { + for field in variant + .fields + .iter() + .flat_map(engine_reflection::EnumVariantFields::fields) + { if !get_whole_type_is_user_editable( field.type_reflection(), field.type_id, @@ -2103,8 +2102,7 @@ where label, preview_label .as_ref() - .map(|preview_label| preview_label.as_ref()) - .unwrap_or(""), + .map_or("", std::convert::AsRef::as_ref), ) { for (idx, item) in items.iter().enumerate() { let is_selected = idx == *current_item; @@ -2157,10 +2155,10 @@ trait ListBoxExt fn build_extended<Item, ItemFn>( self, ui: &dear_imgui_bindings::Ui, - current_item: &mut i32, + current_item: &mut Option<usize>, items: &[Item], item_fn: &ItemFn, - ) -> bool + ) -> Option<usize> where for<'b> ItemFn: Fn(&'b Item) -> ListBoxItem<'b>; } @@ -2172,32 +2170,24 @@ where fn build_extended<Item, ItemFn>( self, ui: &dear_imgui_bindings::Ui, - current_item: &mut i32, + current_item: &mut Option<usize>, items: &[Item], item_fn: &ItemFn, - ) -> bool + ) -> Option<usize> where for<'b> ItemFn: Fn(&'b Item) -> ListBoxItem<'b>, { - let mut result = false; - - let lb = self; - - if let Some(_cb) = lb.begin(ui) { - for (idx, item) in items.iter().enumerate() { - if idx > i32::MAX as usize { - break; - } - - let idx_i32 = idx as i32; + let mut result = None; + if let Some(_lb_token) = self.begin(ui) { + for (index, item) in items.iter().enumerate() { let ListBoxItem { label: item_label, disabled: item_disabled, hover_tooltip: item_hover_tooltip, } = item_fn(item); - let is_selected = idx_i32 == *current_item; + let is_selected = Some(index) == *current_item; let disabled_token = ui.begin_disabled_with_cond(item_disabled); @@ -2206,8 +2196,8 @@ where .selected(is_selected) .build() { - *current_item = idx_i32; - result = true; + *current_item = Some(index); + result = Some(index); } disabled_token.end(); @@ -2234,24 +2224,23 @@ fn create_entity_title(ent_handle: &EntityHandle) -> String "{} {}", ent_name .as_ref() - .map(|ent_name| ent_name.name.as_ref()) - .unwrap_or("<unnamed>"), + .map_or("<unnamed>", |ent_name| ent_name.name.as_ref()), ent_handle.uid() ) } -struct Textures +struct IconTextures { - despawn_icon_tex_id: dear_imgui_bindings::ManagedTextureId, - warning_icon_tex_id: dear_imgui_bindings::ManagedTextureId, - edit_icon_tex_id: dear_imgui_bindings::ManagedTextureId, + despawn: dear_imgui_bindings::ManagedTextureId, + warning: dear_imgui_bindings::ManagedTextureId, + edit: dear_imgui_bindings::ManagedTextureId, } -impl Textures +impl IconTextures { fn new(dear_imgui_context: &mut DearImguiContext) -> Self { - let despawn_icon_tex_id = create_texture( + let despawn = create_texture( dear_imgui_context, Image::from_reader( Cursor::new(include_bytes!("../../../res/ui/delete.png")), @@ -2260,7 +2249,7 @@ impl Textures .unwrap(), ); - let warning_icon_tex_id = create_texture( + let warning = create_texture( dear_imgui_context, Image::from_reader( Cursor::new(include_bytes!("../../../res/ui/warning.png")), @@ -2269,7 +2258,7 @@ impl Textures .unwrap(), ); - let edit_icon_tex_id = create_texture( + let edit = create_texture( dear_imgui_context, Image::from_reader( Cursor::new(include_bytes!("../../../res/ui/edit.png")), @@ -2278,11 +2267,7 @@ impl Textures .unwrap(), ); - Self { - despawn_icon_tex_id, - warning_icon_tex_id, - edit_icon_tex_id, - } + Self { despawn, warning, edit } } } @@ -2294,7 +2279,7 @@ struct AddComponentPopupState search_text: String, searched_components: Vec<(Uid, ComponentInfo, ComponentUserCreatable)>, searchable_components: Vec<(Uid, ComponentInfo, ComponentUserCreatable)>, - selected_search_result: i32, + selected_search_result: Option<usize>, new_component: Option<(Box<dyn Any>, ComponentInfo, Uid)>, } @@ -2312,3 +2297,20 @@ enum PopupState AddComponent(AddComponentPopupState), RenameEntity(RenameEntityPopupState), } + +fn float_to_u8_checked(value: f32) -> Option<u8> +{ + if !value.is_finite() { + return None; + } + + if value > f32::from(u8::MAX) { + return Some(u8::MAX); + } + + if value < f32::from(u8::MIN) { + return Some(u8::MIN); + } + + Some(unsafe { value.to_int_unchecked::<u8>() }) +} |
