11use std:: fmt:: Display ;
22use std:: sync:: Arc ;
3+ use float_eq:: float_ne;
34
45use bevy:: prelude:: * ;
5- use notation_bevy_utils:: prelude:: { BevyUtil , ColorBackground , DockPanel , DockSide , LayoutAnchor , LayoutChangedQuery , LayoutConstraint , LayoutSize , ShapeOp , View , ViewBundle } ;
6+ use notation_bevy_utils:: prelude:: { BevyUtil , ColorBackground , LayoutAnchor , LayoutChangedQuery , LayoutSize , ShapeOp , View , ViewBundle } ;
67use notation_midi:: prelude:: MidiState ;
78use notation_model:: prelude:: {
89 Duration , Entry , HandShape6 , Interval , LaneEntry , LaneKind , ModelEntryProps , Pick , Syllable ,
910 Tab , Units ,
1011} ;
1112
12- use crate :: prelude:: { EntryPlaying , NotationAssets , NotationTheme } ;
13+ use crate :: prelude:: { EntryPlaying , NotationAssets , NotationTheme , NotationSettings } ;
1314use crate :: ui:: layout:: NotationLayout ;
1415
1516use super :: fret_finger:: { FretFingerData } ;
@@ -32,20 +33,9 @@ impl Display for GuitarView {
3233 }
3334}
3435
35- impl < ' a > DockPanel < NotationLayout < ' a > > for GuitarView {
36- fn dock_side ( & self , _engine : & NotationLayout < ' a > , _size : LayoutSize ) -> DockSide {
37- DockSide :: Left
38- }
39- }
40-
4136impl < ' a > View < NotationLayout < ' a > > for GuitarView {
4237 fn pivot ( & self ) -> LayoutAnchor {
43- LayoutAnchor :: CENTER
44- }
45- fn calc_size ( & self , engine : & NotationLayout , constraint : LayoutConstraint ) -> LayoutSize {
46- let width = constraint. max . height / engine. theme . guitar . image_size . 1
47- * engine. theme . guitar . image_size . 0 ;
48- LayoutSize :: new ( width, constraint. max . height )
38+ LayoutAnchor :: TOP
4939 }
5040}
5141
@@ -146,6 +136,8 @@ impl GuitarView {
146136 mut finger_query : Query < ( & Parent , Entity , & mut FretFingerData ) , With < FretFingerData > > ,
147137 ) {
148138 for ( entity, _view, layout) in query. iter ( ) {
139+ let guitar_height = layout. size . width * theme. guitar . image_size . 1 / theme. guitar . image_size . 0 ;
140+ let guitar_size = LayoutSize :: new ( layout. size . width , guitar_height) ;
149141 for ( parent, mut transform) in sprite_query. iter_mut ( ) {
150142 if parent. 0 == entity {
151143 let scale = layout. size . width / theme. guitar . image_size . 0 ;
@@ -155,20 +147,21 @@ impl GuitarView {
155147 }
156148 for ( parent, string_entity, mut string_data) in string_query. iter_mut ( ) {
157149 if parent. 0 == entity {
158- string_data. guitar_size = layout . size ;
150+ string_data. guitar_size = guitar_size ;
159151 string_data. update ( & mut commands, & theme, string_entity) ;
160152 }
161153 }
162- for ( parent, capo_entity , mut capo_data ) in capo_query . iter_mut ( ) {
154+ for ( parent, finger_entity , mut finger_data ) in finger_query . iter_mut ( ) {
163155 if parent. 0 == entity {
164- capo_data . guitar_size = layout . size ;
165- capo_data . update ( & mut commands, & theme, capo_entity ) ;
156+ finger_data . value . extra . guitar_size = guitar_size ;
157+ finger_data . update ( & mut commands, & theme, finger_entity ) ;
166158 }
167159 }
168- for ( parent, finger_entity , mut finger_data ) in finger_query . iter_mut ( ) {
160+ for ( parent, capo_entity , mut capo_data ) in capo_query . iter_mut ( ) {
169161 if parent. 0 == entity {
170- finger_data. value . extra . guitar_size = layout. size ;
171- finger_data. update ( & mut commands, & theme, finger_entity) ;
162+ capo_data. view_size = layout. size ;
163+ capo_data. guitar_size = guitar_size;
164+ capo_data. update ( & mut commands, & theme, capo_entity) ;
172165 }
173166 }
174167 }
@@ -289,8 +282,8 @@ impl GuitarView {
289282 string_data. update_value ( shape, fretboard, pick, meta. clone ( ) ) ;
290283 string_data. update ( & mut commands, & theme, string_entity) ;
291284 }
292- if let Some ( fretboard ) = fretboard {
293- for ( capo_entity , mut capo_data ) in capo_query . iter_mut ( ) {
285+ for ( capo_entity , mut capo_data ) in capo_query . iter_mut ( ) {
286+ if let Some ( fretboard ) = fretboard {
294287 if fretboard. capo != capo_data. capo {
295288 capo_data. capo = fretboard. capo ;
296289 capo_data. update ( & mut commands, & theme, capo_entity) ;
@@ -299,4 +292,41 @@ impl GuitarView {
299292 }
300293 }
301294 }
295+ pub fn update_y (
296+ guitar_view_query : & mut Query < & mut Transform , With < Arc < GuitarView > > > ,
297+ y : f32 ,
298+ ) {
299+ if let Ok ( mut transform) = guitar_view_query. single_mut ( ) {
300+ let trans = transform. translation ;
301+ println ! ( "GuitarView::update_y {} -> {}" , trans. y, y) ;
302+ if float_ne ! ( trans. y, y, abs <= 0.01 ) {
303+ * transform = Transform :: from_xyz ( trans. x , y, trans. z ) ;
304+ }
305+ }
306+ }
307+ pub fn adjust_y_by_capo (
308+ theme : Res < NotationTheme > ,
309+ settings : Res < NotationSettings > ,
310+ capo_query : Query < & GuitarCapoData , Changed < GuitarCapoData > > ,
311+ mut guitar_view_query : Query < & mut Transform , With < Arc < GuitarView > > > ,
312+ ) {
313+ if settings. override_guitar_y . is_some ( ) {
314+ return ;
315+ }
316+ for capo_data in capo_query. iter ( ) {
317+ if capo_data. view_size . height <= 0.0 {
318+ return ;
319+ }
320+ let y = if capo_data. view_size . height > capo_data. guitar_size . height {
321+ -capo_data. guitar_size . height / 2.0
322+ } else {
323+ let capo_y = theme
324+ . guitar
325+ . calc_fret_y ( capo_data. capo , capo_data. guitar_size . height ) ;
326+ -( capo_y + capo_data. guitar_size . height * theme. guitar . capo_height_factor )
327+ } ;
328+ println ! ( "adjust_y_by_capo {} {} {} -> {}" , capo_data. guitar_size, capo_data. view_size, capo_data. capo, y) ;
329+ Self :: update_y ( & mut guitar_view_query, y) ;
330+ }
331+ }
302332}
0 commit comments