Skip to content

Commit 589c83b

Browse files
committed
improve scaling
1 parent f1ff675 commit 589c83b

5 files changed

Lines changed: 100 additions & 41 deletions

File tree

solitaire-game/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ fn camera_setup(mut commands: Commands) {
5454
commands.spawn(Camera2d);
5555
}
5656

57-
fn scale_viewport(mut camera_query: Query<&mut Projection, With<Camera>>) {
58-
let Ok(mut projection) = camera_query.single_mut() else {
57+
fn scale_viewport(mut camera_query: Query<(&mut Projection, &Camera)>) {
58+
let Ok((mut projection, camera)) = camera_query.single_mut() else {
5959
return;
6060
};
61+
let scale = match camera.logical_viewport_rect() {
62+
Some(view_port) => 12.,
63+
None => 8.0,
64+
};
6165
if let Projection::Orthographic(projection2d) = &mut *projection {
6266
projection2d.scaling_mode = ScalingMode::AutoMin {
63-
min_width: 12.,
64-
min_height: 12.,
67+
min_width: scale,
68+
min_height: scale,
6569
}
6670
}
6771
}
@@ -116,6 +120,7 @@ impl Plugin for PegSolitaire {
116120

117121
app.add_observer(update_solution);
118122
app.add_systems(Startup, (camera_setup, scale_viewport).chain());
123+
app.add_systems(Update, scale_viewport);
119124
app.add_systems(PostUpdate, highlight_selected);
120125
app.add_systems(PreUpdate, calc_view_port);
121126
}

solitaire-game/src/stats.rs

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use 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

66
use 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)]
98100
struct OverallSuccessRatioText;
99101

102+
#[derive(Component)]
103+
enum TextPosition {
104+
TopLeft,
105+
TopRight,
106+
BottomLeft,
107+
BottomRight,
108+
AboveOrLeft,
109+
BelowOrRight,
110+
}
111+
100112
fn 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+
201266
fn 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!("\ncalculating unique solutions ...")
372+
format!("\n?")
308373
};
309374

310375
for text in unique_solutions_text {

solitaire-game/src/status.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,14 @@ fn draw_solution(
1818
) {
1919
let cam = camera_query;
2020
if let Some(view_port) = cam.0.logical_viewport_rect() {
21-
let y_size = view_port.max.y - view_port.min.y;
22-
let x_size = view_port.max.x - view_port.min.x;
23-
24-
let (start, end) = if y_size > x_size {
21+
let (start, end) = {
2522
let pos_vp = (Vec2::new(view_port.min.x, view_port.max.y), view_port.max);
2623
(
2724
viewport_to_world(pos_vp.0, cam.0, cam.1).unwrap_or_default()
2825
+ Vec3::new(0.6, 0.6, 0.0),
2926
viewport_to_world(pos_vp.1, cam.0, cam.1).unwrap_or_default()
3027
+ Vec3::new(-0.6, 0.6, 0.0),
3128
)
32-
} else {
33-
let pos_vp = (view_port.min, Vec2::new(view_port.min.x, view_port.max.y));
34-
(
35-
viewport_to_world(pos_vp.0, cam.0, cam.1).unwrap_or_default()
36-
+ Vec3::new(0.6, -0.6, 0.0),
37-
viewport_to_world(pos_vp.1, cam.0, cam.1).unwrap_or_default()
38-
+ Vec3::new(0.6, 0.6, 0.0),
39-
)
4029
};
4130

4231
for (i, _mov) in solution.0.clone().into_iter().enumerate() {

solitaire-solver/src/solution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
collections::{self, BTreeMap},
2+
collections::BTreeMap,
33
fmt::{Display, Formatter, Result},
44
};
55

solitaire-solver/src/unique_solutions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::solution::SolutionMultiset;
2-
use crate::{Board, Move, unique_solutions};
2+
use crate::{Board, Move};
33
use crate::{HashSet, Solution};
44
use std::collections::BTreeMap;
55

0 commit comments

Comments
 (0)