11use bevy:: {
2- ecs:: entity_disabling:: Disabled , prelude :: * , sprite :: Anchor , text :: TextBounds ,
3- window:: RequestRedraw ,
2+ ecs:: entity_disabling:: Disabled , math :: AspectRatio , prelude :: * , sprite :: Anchor ,
3+ text :: TextBounds , window:: RequestRedraw ,
44} ;
55
66use crate :: {
7- BoardPosition , CurrentBoard ,
7+ BoardPosition , CurrentBoard , WorldSpaceViewPort ,
88 solver:: { FeasibleConstellations , RandomMoveChances , UniqueSolutions } ,
99 total_progress:: TotalProgress ,
1010} ;
@@ -27,6 +27,7 @@ fn toggle_stats(
2727 With < OverallSuccessRatioText > ,
2828 With < TotalProgressText > ,
2929 With < NextMoveChanceText > ,
30+ With < UniqueSolutionsText > ,
3031 ) > ,
3132 Or < ( With < Disabled > , Without < Disabled > ) > ,
3233 ) ,
@@ -74,6 +75,7 @@ impl Plugin for StatsPlugin {
7475 app. add_observer ( update_overall_success) ;
7576 app. add_observer ( update_total_progress) ;
7677 app. add_observer ( update_solution_count) ;
78+ app. add_systems ( Update , update_solution_text_pos) ;
7779 app. add_observer ( update_unique_solutions) ;
7880 app. add_observer ( toggle_stats) ;
7981 }
@@ -97,6 +99,16 @@ struct UniqueSolutionsText;
9799#[ derive( Component ) ]
98100struct OverallSuccessRatioText ;
99101
102+ #[ derive( Component ) ]
103+ enum TextPosition {
104+ TopLeft ,
105+ TopRight ,
106+ BottomLeft ,
107+ BottomRight ,
108+ AboveOrLeft ,
109+ BelowOrRight ,
110+ }
111+
100112fn update_stats ( mut commands : Commands ) {
101113 commands. trigger ( UpdateStats ) ;
102114}
@@ -118,23 +130,16 @@ fn add_text(mut commands: Commands, asset_server: Res<AssetServer>) {
118130 font_size : 50.0 ,
119131 ..default ( )
120132 } ;
121- let title_pos =
122- Vec3 :: from ( ( BoardPosition { x : 4 , y : 4 } . to_world_space ( ) , 1. ) ) + Vec3 :: new ( 0.5 , -0.5 , 0.0 ) ;
123- let title_pos_1 =
124- Vec3 :: from ( ( BoardPosition { x : 1 , y : 4 } . to_world_space ( ) , 1. ) ) + Vec3 :: new ( 0.5 , -0.5 , 0.0 ) ;
125133 let title_pos_2 =
126134 Vec3 :: from ( ( BoardPosition { x : 4 , y : 1 } . to_world_space ( ) , 1. ) ) + Vec3 :: new ( 0.5 , -0.5 , 0.0 ) ;
127- let title_pos_3 =
128- Vec3 :: from ( ( BoardPosition { x : 1 , y : 1 } . to_world_space ( ) , 1. ) ) + Vec3 :: new ( 0.5 , -0.5 , 0.0 ) ;
129- let title_pos_4 =
130- Vec3 :: from ( ( BoardPosition { x : 6 , y : 2 } . to_world_space ( ) , 1. ) ) + Vec3 :: new ( 0.5 , -0.5 , 0.0 ) ;
131135 commands
132136 . spawn ( (
137+ TextPosition :: TopLeft ,
133138 Text2d :: new ( "\u{1D4AB} (\u{1D437} ) \u{2248} " ) ,
134- Transform :: from_scale ( Vec3 :: new ( 0.005 , 0.005 , 0.005 ) ) . with_translation ( title_pos ) ,
139+ Transform :: from_scale ( Vec3 :: new ( 0.005 , 0.005 , 0.005 ) ) ,
135140 medium_font. clone ( ) ,
136141 TextLayout :: new_with_justify ( Justify :: Left ) ,
137- Anchor :: TOP_LEFT ,
142+ Anchor :: CENTER ,
138143 OverallSuccessRatioText ,
139144 ) )
140145 . with_child ( ( TextSpan ( " ... ?" . into ( ) ) , medium_font. clone ( ) ) )
@@ -144,21 +149,23 @@ fn add_text(mut commands: Commands, asset_server: Res<AssetServer>) {
144149 ) ) ;
145150 commands
146151 . spawn ( (
152+ TextPosition :: TopRight ,
147153 Text2d :: new ( "unique solutions" ) ,
148- Transform :: from_scale ( Vec3 :: new ( 0.005 , 0.005 , 0.005 ) ) . with_translation ( title_pos_4 ) ,
154+ Transform :: from_scale ( Vec3 :: new ( 0.005 , 0.005 , 0.005 ) ) ,
149155 medium_font. clone ( ) ,
150- TextLayout :: new_with_justify ( Justify :: Left ) ,
151- Anchor :: TOP_LEFT ,
156+ TextLayout :: new_with_justify ( Justify :: Center ) ,
157+ Anchor :: CENTER ,
152158 UniqueSolutionsText ,
153159 ) )
154160 . with_child ( ( TextSpan ( "" . into ( ) ) , large_font. clone ( ) ) ) ;
155161 commands
156162 . spawn ( (
163+ TextPosition :: BottomLeft ,
157164 Text2d :: new ( "" ) ,
158- Transform :: from_scale ( Vec3 :: new ( 0.005 , 0.005 , 0.005 ) ) . with_translation ( title_pos_1 ) ,
165+ Transform :: from_scale ( Vec3 :: new ( 0.005 , 0.005 , 0.005 ) ) ,
159166 large_font. clone ( ) ,
160167 TextLayout :: new_with_justify ( Justify :: Center ) ,
161- Anchor :: TOP_RIGHT ,
168+ Anchor :: CENTER ,
162169 NextMoveChanceText ,
163170 ) )
164171 . with_child ( ( TextSpan ( "? / ?\n " . into ( ) ) , large_font. clone ( ) ) )
@@ -168,12 +175,13 @@ fn add_text(mut commands: Commands, asset_server: Res<AssetServer>) {
168175 ) ) ;
169176 commands
170177 . spawn ( (
178+ TextPosition :: BottomRight ,
171179 Text2d :: new ( "you have seen " ) ,
172- Transform :: from_scale ( Vec3 :: new ( 0.004 , 0.004 , 0.004 ) ) . with_translation ( title_pos_2 ) ,
180+ Transform :: from_scale ( Vec3 :: new ( 0.004 , 0.004 , 0.004 ) ) ,
173181 small_font. clone ( ) ,
174182 TextLayout :: new ( Justify :: Center , LineBreak :: WordBoundary ) ,
175183 TextBounds :: from ( Vec2 :: new ( 600.0 , 300.0 ) ) ,
176- Anchor :: BOTTOM_LEFT ,
184+ Anchor :: CENTER ,
177185 TotalProgressText ,
178186 ) )
179187 . with_child ( ( TextSpan ( "?%" . into ( ) ) , large_font. clone ( ) ) )
@@ -184,12 +192,13 @@ fn add_text(mut commands: Commands, asset_server: Res<AssetServer>) {
184192 . with_child ( ( TextSpan ( "" . into ( ) ) , small_font. clone ( ) ) ) ;
185193 commands
186194 . spawn ( (
195+ TextPosition :: AboveOrLeft ,
187196 Text2d :: new ( "you have found " ) ,
188- Transform :: from_scale ( Vec3 :: new ( 0.004 , 0.004 , 0.004 ) ) . with_translation ( title_pos_3 ) ,
197+ Transform :: from_scale ( Vec3 :: new ( 0.005 , 0.005 , 0.005 ) ) ,
189198 small_font. clone ( ) ,
190199 TextLayout :: new ( Justify :: Center , LineBreak :: WordBoundary ) ,
191200 TextBounds :: from ( Vec2 :: new ( 600.0 , 300.0 ) ) ,
192- Anchor :: BOTTOM_RIGHT ,
201+ Anchor :: CENTER ,
193202 SolutionText ,
194203 ) )
195204 . with_child ( ( TextSpan ( " ? " . into ( ) ) , large_font. clone ( ) ) )
@@ -198,6 +207,62 @@ fn add_text(mut commands: Commands, asset_server: Res<AssetServer>) {
198207 . with_child ( ( TextSpan ( " of which are unique!" . into ( ) ) , small_font. clone ( ) ) ) ;
199208}
200209
210+ fn update_solution_text_pos (
211+ ws_view_port : Res < WorldSpaceViewPort > ,
212+ camera : Single < & Camera > ,
213+ text : Query < ( & mut Transform , & TextPosition ) > ,
214+ ) {
215+ let camera = * camera;
216+ let board_tl = Vec2 :: new ( -1.5 , 1.5 ) ;
217+ let board_tr = Vec2 :: new ( 1.5 , 1.5 ) ;
218+ let board_bl = Vec2 :: new ( -1.5 , -1.5 ) ;
219+ let board_br = Vec2 :: new ( 1.5 , -1.5 ) ;
220+
221+ let Some ( view_port) = camera. logical_viewport_rect ( ) else {
222+ return ;
223+ } ;
224+
225+ for ( mut transform, pos) in text {
226+ let ( a, b) = match pos {
227+ TextPosition :: TopLeft => ( ws_view_port. top_left , board_tl) ,
228+ TextPosition :: TopRight => ( ws_view_port. top_right , board_tr) ,
229+ TextPosition :: BottomLeft => ( ws_view_port. bottom_left , board_bl) ,
230+ TextPosition :: BottomRight => ( ws_view_port. bottom_right , board_br) ,
231+ TextPosition :: AboveOrLeft => {
232+ if view_port. width ( ) > view_port. height ( ) {
233+ // landscape
234+ (
235+ ( ws_view_port. top_left + ws_view_port. bottom_left ) / 2.0 ,
236+ Vec2 :: new ( -3.5 , 0.0 ) ,
237+ )
238+ } else {
239+ // portrait
240+ (
241+ ( ws_view_port. top_left + ws_view_port. top_right ) / 2.0 ,
242+ Vec2 :: new ( 0.0 , 3.5 ) ,
243+ )
244+ }
245+ }
246+ TextPosition :: BelowOrRight => {
247+ if view_port. width ( ) > view_port. height ( ) {
248+ // landscape
249+ (
250+ ( ws_view_port. top_right + ws_view_port. bottom_right ) / 2.0 ,
251+ Vec2 :: new ( -3.5 , 0.0 ) ,
252+ )
253+ } else {
254+ // portrait
255+ (
256+ ( ws_view_port. bottom_left + ws_view_port. bottom_right ) / 2.0 ,
257+ Vec2 :: new ( 0.0 , -3.5 ) ,
258+ )
259+ }
260+ }
261+ } ;
262+ transform. translation = Vec3 :: from ( ( ( a. xy ( ) + b) / 2.0 , 1.5 ) ) ;
263+ }
264+ }
265+
201266fn update_overall_success (
202267 _trigger : On < UpdateStats > ,
203268 overall_success_text : Query < Entity , With < OverallSuccessRatioText > > ,
@@ -304,7 +369,7 @@ fn update_unique_solutions(
304369 let msg = if let Some ( unique_solutions) = unique_solutions {
305370 format ! ( "\n {}" , unique_solutions. 0 . len( ) )
306371 } else {
307- format ! ( "\n calculating unique solutions ... " )
372+ format ! ( "\n ? " )
308373 } ;
309374
310375 for text in unique_solutions_text {
0 commit comments