11use core:: f32:: consts:: PI ;
22
33use bevy_app:: { Plugin , PreUpdate } ;
4- use bevy_asset:: Handle ;
54use bevy_color:: { Alpha , Color , Hsla } ;
65use bevy_core_widgets:: {
76 CallbackTemplate , CoreSlider , CoreSliderThumb , SliderRange , SliderValue , TrackClick ,
87 ValueChange ,
98} ;
109use bevy_ecs:: {
11- bundle:: Bundle ,
1210 component:: Component ,
1311 entity:: Entity ,
1412 hierarchy:: Children ,
@@ -25,13 +23,9 @@ use bevy_ui::{
2523 FlexDirection , Gradient , InterpolationColorSpace , LinearGradient , Node , Outline , PositionType ,
2624 UiRect , UiTransform , Val , Val2 , ZIndex ,
2725} ;
28- use bevy_ui_render:: ui_material:: MaterialNode ;
2926
3027use crate :: {
31- alpha_pattern:: { AlphaPattern , AlphaPatternMaterial } ,
32- cursor:: EntityCursor ,
33- palette,
34- rounded_corners:: RoundedCorners ,
28+ alpha_pattern:: AlphaPattern , cursor:: EntityCursor , palette, rounded_corners:: RoundedCorners ,
3529} ;
3630
3731const SLIDER_HEIGHT : f32 = 16.0 ;
@@ -144,8 +138,6 @@ pub struct SliderBaseColor(pub Color);
144138
145139/// Color slider template properties, passed to [`color_slider`] function.
146140pub struct ColorSliderProps {
147- /// Slider current value
148- pub value : f32 ,
149141 /// On-change handler
150142 pub on_change : CallbackTemplate < In < ValueChange < f32 > > > ,
151143 /// Which color component we're editing
@@ -155,7 +147,6 @@ pub struct ColorSliderProps {
155147impl Default for ColorSliderProps {
156148 fn default ( ) -> Self {
157149 Self {
158- value : 0.0 ,
159150 on_change : CallbackTemplate :: Ignore ,
160151 channel : ColorChannel :: Alpha ,
161152 }
@@ -164,7 +155,7 @@ impl Default for ColorSliderProps {
164155
165156/// A color slider widget.
166157#[ derive( Component , Default , Clone ) ]
167- #[ require( SliderBaseColor ( Color :: WHITE ) ) ]
158+ #[ require( SliderValue , SliderBaseColor ( Color :: WHITE ) ) ]
168159pub struct ColorSlider {
169160 /// Which channel is being edited by this slider.
170161 pub channel : ColorChannel ,
@@ -174,8 +165,38 @@ pub struct ColorSlider {
174165#[ derive( Component , Default , Clone ) ]
175166struct ColorSliderTrack ;
176167
168+ /// Marker for the left/right endcaps
169+ #[ derive( Component , Default , Clone ) ]
170+ #[ require( BackgroundColor ) ]
171+ struct ColorSliderEndCap ;
172+
173+ #[ derive( Component , Default , Clone ) ]
174+ #[ require(
175+ BackgroundGradient ( vec![ Gradient :: Linear ( LinearGradient {
176+ angle: PI * 0.5 ,
177+ stops: vec![
178+ ColorStop :: new( Color :: NONE , Val :: Percent ( 0. ) ) ,
179+ ColorStop :: new( Color :: NONE , Val :: Percent ( 50. ) ) ,
180+ ColorStop :: new( Color :: NONE , Val :: Percent ( 100. ) ) ,
181+ ] ,
182+ color_space: InterpolationColorSpace :: Srgba ,
183+ } ) ] )
184+ ) ]
185+ struct ColorSliderGradient ;
186+
177187/// Marker for the thumb
178188#[ derive( Component , Default , Clone ) ]
189+ #[ require(
190+ Node {
191+ position_type: PositionType :: Absolute ,
192+ left: Val :: Percent ( 0. ) ,
193+ top: Val :: Percent ( 50. ) ,
194+ width: Val :: Px ( THUMB_SIZE ) ,
195+ height: Val :: Px ( THUMB_SIZE ) ,
196+ border: UiRect :: all( Val :: Px ( 2.0 ) ) ,
197+ ..Default :: default ( )
198+ }
199+ ) ]
179200struct ColorSliderThumb ;
180201
181202/// Spawn a new slider widget.
@@ -201,7 +222,6 @@ pub fn color_slider(props: ColorSliderProps) -> impl Scene {
201222 ColorSlider {
202223 channel: { props. channel. clone( ) } ,
203224 }
204- SliderValue ( { props. value} )
205225 template_value( channel_range)
206226 EntityCursor :: System ( bevy_window:: SystemCursorIcon :: Pointer )
207227 TabIndex ( 0 )
@@ -216,41 +236,24 @@ pub fn color_slider(props: ColorSliderProps) -> impl Scene {
216236 }
217237 template_value( RoundedCorners :: All . to_border_radius( TRACK_RADIUS ) )
218238 ColorSliderTrack
219- AlphaPattern
220- MaterialNode :: <AlphaPatternMaterial >( Handle :: default ( ) )
239+ AlphaPattern :: default ( )
221240 [
222241 // Left endcap
223242 (
243+ ColorSliderEndCap
224244 Node {
225245 width: Val :: Px ( { THUMB_SIZE * 0.5 } ) ,
226246 }
227247 template_value( RoundedCorners :: Left . to_border_radius( TRACK_RADIUS ) )
228- BackgroundColor ( { palette:: X_AXIS } )
229248 ) ,
230249 // Track with gradient
231250 (
232251 Node {
233252 flex_grow: 1.0 ,
234253 }
235- BackgroundGradient ( { vec![ Gradient :: Linear ( LinearGradient {
236- angle: PI * 0.5 ,
237- stops: vec![
238- ColorStop :: new( Color :: NONE , Val :: Percent ( 0. ) ) ,
239- ColorStop :: new( Color :: NONE , Val :: Percent ( 50. ) ) ,
240- ColorStop :: new( Color :: NONE , Val :: Percent ( 100. ) ) ,
241- ] ,
242- color_space: InterpolationColorSpace :: Srgba ,
243- } ) ] } )
254+ ColorSliderGradient
244255 ZIndex ( 1 )
245256 [
246- Node {
247- position_type: PositionType :: Absolute ,
248- left: Val :: Percent ( 0. ) ,
249- top: Val :: Percent ( 50. ) ,
250- width: Val :: Px ( THUMB_SIZE ) ,
251- height: Val :: Px ( THUMB_SIZE ) ,
252- border: UiRect :: all( Val :: Px ( 2.0 ) ) ,
253- }
254257 CoreSliderThumb
255258 ColorSliderThumb
256259 BorderRadius :: MAX
@@ -268,11 +271,11 @@ pub fn color_slider(props: ColorSliderProps) -> impl Scene {
268271 ) ,
269272 // Right endcap
270273 (
274+ ColorSliderEndCap
271275 Node {
272276 width: Val :: Px ( { THUMB_SIZE * 0.5 } ) ,
273277 }
274278 template_value( RoundedCorners :: Right . to_border_radius( TRACK_RADIUS ) )
275- BackgroundColor ( { palette:: Z_AXIS } )
276279 ) ,
277280 ]
278281 ]
0 commit comments