Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 049a400

Browse files
committedJan 10, 2024
Add keyindings for increasing and decreaseing the circle size
1 parent b8ceae2 commit 049a400

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Keybindings:
99
- `B`: toggle the screen border collision
1010
- `Arrow Keys (Up/Down/Left/Right)`: Add/Remove 1/1/100/100 particles
1111
- `J/K`: decrease/increase the multipler
12+
- `+/-`: increase/decrease ball size
1213
- `ESC`: quit
1314

1415
More options with ./particles --help

‎src/main.rs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Keybindings:
1616
B: Toggle Screen Border Collisions
1717
Arrow Keys (Up/Down/Left/Right): Add/Remove 1/1/100/100 Particles
1818
J/K: Decrease/Increase Multiplier
19+
-/+: Decrease/Increase Ball Size
1920
ESC: Quit"#
2021
)]
2122
struct Cli {
@@ -67,6 +68,10 @@ struct Cli {
6768
#[arg(long)]
6869
no_multipler: bool,
6970

71+
/// If set, the ball_size label will not be drawn
72+
#[arg(long)]
73+
no_ball_size_label: bool,
74+
7075
/// If set, no labels will be drawn.
7176
/// Equivalent to setting all the no_* flags
7277
#[arg(long)]
@@ -94,10 +99,14 @@ fn main() {
9499
0.99
95100
};
96101

97-
let ball_size = if let Some(ball_size) = args.ball_size {
102+
let mut ball_size = if let Some(ball_size) = args.ball_size {
98103
ball_size
99104
} else {
100-
20.0
105+
if args.balls {
106+
20.0
107+
} else {
108+
1.0
109+
}
101110
};
102111

103112
if args.no_labels {
@@ -106,6 +115,7 @@ fn main() {
106115
args.no_border = true;
107116
args.no_attract = true;
108117
args.no_multipler = true;
118+
args.no_ball_size_label = true;
109119
}
110120

111121
let mut width = if let Some(w) = args.width { w } else { 800 };
@@ -130,31 +140,24 @@ fn main() {
130140
width = raylib::GetScreenWidth();
131141
height = raylib::GetScreenHeight();
132142

143+
// Maybe check if it possible to do this with a match statement :)
133144
if raylib::IsKeyPressed(raylib::KeyboardKey::KeyR as i32) {
134145
particles = (0..particle_count)
135146
.map(|_| particle::Particle::new(width, height))
136147
.collect();
137-
}
138-
139-
if raylib::IsKeyPressed(raylib::KeyboardKey::KeySpace as i32)
148+
} else if raylib::IsKeyPressed(raylib::KeyboardKey::KeySpace as i32)
140149
|| raylib::IsMouseButtonPressed(0)
141150
{
142151
attract = !attract;
143-
}
144-
145-
if raylib::IsKeyPressed(raylib::KeyboardKey::KeyB as i32) {
152+
} else if raylib::IsKeyPressed(raylib::KeyboardKey::KeyB as i32) {
146153
border = !border;
147-
}
148-
149-
if raylib::IsKeyDown(raylib::KeyboardKey::KeyK as i32) {
154+
} else if raylib::IsKeyDown(raylib::KeyboardKey::KeyK as i32) {
150155
multiplier += 0.1
151156
} else if raylib::IsKeyDown(raylib::KeyboardKey::KeyJ as i32) {
152157
if multiplier >= 0.0 {
153158
multiplier -= 0.1;
154159
}
155-
}
156-
157-
if raylib::IsKeyDown(raylib::KeyboardKey::KeyUp as i32) {
160+
} else if raylib::IsKeyDown(raylib::KeyboardKey::KeyUp as i32) {
158161
particles.push(particle::Particle::new(width, height));
159162

160163
particle_count += 1;
@@ -176,6 +179,20 @@ fn main() {
176179
particles.push(particle::Particle::new(width, height));
177180
}
178181
particle_count += 100;
182+
} else if raylib::IsKeyDown(raylib::KeyboardKey::KeyPlus as i32)
183+
|| raylib::IsKeyDown(raylib::KeyboardKey::KeyAdd as i32)
184+
{
185+
ball_size += 1.0;
186+
args.balls = true;
187+
} else if raylib::IsKeyDown(raylib::KeyboardKey::KeyMinus as i32)
188+
|| raylib::IsKeyDown(raylib::KeyboardKey::KeySubtract as i32)
189+
{
190+
if ball_size >= 2.0 {
191+
args.balls = true;
192+
ball_size -= 1.0;
193+
} else {
194+
args.balls = false;
195+
}
179196
}
180197

181198
let mouse_position = raylib::GetMousePosition();
@@ -276,6 +293,23 @@ fn main() {
276293
);
277294
}
278295

296+
if !args.no_ball_size_label {
297+
let text = CString::new(format!("Ball Size: {:.01}", ball_size)).unwrap();
298+
let text_ptr = text.as_ptr();
299+
raylib::DrawText(
300+
text_ptr,
301+
10,
302+
110,
303+
20,
304+
raylib::Color {
305+
r: 255,
306+
g: 255,
307+
b: 255,
308+
a: 255,
309+
},
310+
);
311+
}
312+
279313
raylib::EndDrawing();
280314
}
281315
raylib::CloseWindow();

‎src/raylib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ pub struct Vector2 {
3939
pub y: f32,
4040
}
4141
pub enum KeyboardKey {
42-
KeyB = 66, // Key: B | b
43-
KeyR = 82, // Key: R | r
44-
KeySpace = 32, // Key: Space
45-
KeyUp = 265, // Key: Up
46-
KeyDown = 264, // Key: Down
47-
KeyLeft = 263, // Key: Left
48-
KeyRight = 262, // Key: Right
49-
KeyK = 75, // Key: K | k
50-
KeyJ = 74, // Key: J | j
42+
KeyB = 66, // Key: B | b
43+
KeyR = 82, // Key: R | r
44+
KeySpace = 32, // Key: Space
45+
KeyUp = 265, // Key: Up
46+
KeyDown = 264, // Key: Down
47+
KeyLeft = 263, // Key: Left
48+
KeyRight = 262, // Key: Right
49+
KeyK = 75, // Key: K | k
50+
KeyJ = 74, // Key: J | j
51+
KeyPlus = 44, // Key: + (Plus)
52+
KeyMinus = 45, // Key: - (Minus)
53+
KeySubtract = 333, // Key: - (Minus)
54+
KeyAdd = 334, // Key: + (Plus)
5155
}

0 commit comments

Comments
 (0)
Please sign in to comment.