Skip to content

Commit 20c489d

Browse files
committed
slowly documenting, de-obfuscating, and ultimately trying to decipher this code after like 2 months away
1 parent fab1580 commit 20c489d

File tree

6 files changed

+109
-18
lines changed

6 files changed

+109
-18
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ instant = { version = "0.1.12", features = ["wasm-bindgen", "inaccurate"] }
1717
rand = "0.8.5"
1818
getrandom = { version = "0.2.12", features = ["js"] }
1919
ttf-parser = "0.20.0"
20+
web-sys = "0.3.69"
2021

2122
# native:
2223
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]

src/app.rs

+51-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
use crate::consts::*;
12
use crate::conway;
23
use crate::RunModes;
34
use crate::UserInterface;
5+
use crate::Viewport;
46
use conway::conway_map;
57
use eframe::egui;
68
use egui::Id;
9+
use web_sys::console;
10+
11+
12+
#[derive(Default)]
13+
pub struct MouseState {
14+
pub is_dragging: bool,
15+
pub last_pos: Option<egui::Pos2>,
16+
}
17+
18+
19+
720
use egui::LayerId;
821
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
22+
923
#[derive(serde::Deserialize, serde::Serialize)]
1024
#[serde(default)] // if we add new fields, give them default values when deserializing old state
1125
pub struct ConwaySim {
@@ -24,7 +38,10 @@ pub struct ConwaySim {
2438
view_stats: bool,
2539
first_run: bool,
2640
mode: RunModes,
41+
#[serde(skip)] // This how you opt-out of serialization of a field
42+
viewport:Viewport,
2743
}
44+
2845
// TODO: implement feature so that the user can click and drag on the main view window to move
2946
// their view around instead of using sliders, cause sliders are janky as fuck
3047

@@ -46,10 +63,12 @@ impl Default for ConwaySim {
4663
reset: false,
4764
first_run: true,
4865
mode: RunModes::default(),
66+
viewport: Viewport::default(),
4967
}
5068
}
5169
}
5270

71+
5372
impl ConwaySim {
5473
/// Called once before the first frame.
5574
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
@@ -63,9 +82,20 @@ impl ConwaySim {
6382

6483
Default::default()
6584
}
85+
fn handle_mouse_events(&mut self, ctx: &egui::Context) {
86+
87+
88+
}
89+
90+
fn handle_scroll(&mut self, ctx: &egui::Context, scroll_delta: egui::Vec2) {
91+
92+
}
6693

6794
fn update_simulation(&mut self, ctx: &egui::Context) {
6895
egui::CentralPanel::default().show(ctx, |ui| {
96+
let viewport_rect = ui.available_rect_before_wrap();
97+
self.viewport.rect = viewport_rect;
98+
6999
let gridline_layer: egui::LayerId =
70100
LayerId::new(egui::Order::Foreground, Id::from("gridlines"));
71101
let painter = egui::Painter::new(
@@ -79,8 +109,9 @@ impl ConwaySim {
79109
ui.available_rect_before_wrap(),
80110
);
81111
ui.expand_to_include_rect(painter.clip_rect());
82-
ui.expand_to_include_rect(line_painter.clip_rect());
112+
//ui.expand_to_include_rect(line_painter.clip_rect());
83113
self.rect = Some(painter.clip_rect());
114+
//Logic that actually draws the screen I think
84115
let mut shapes: Vec<egui::Shape> = if self.map.light_mode {
85116
vec![egui::Shape::rect_filled(
86117
self.rect.unwrap(),
@@ -105,6 +136,12 @@ impl ConwaySim {
105136
line_painter.extend(lines);
106137
}
107138
});
139+
if ctx.input(|i| i.pointer.secondary_clicked() && i.pointer.is_decidedly_dragging()) {
140+
println!("Dragging and such");
141+
use crate::app::console;
142+
console::log_1(&"erm, what the deuce".into());
143+
144+
}
108145

109146
if self.view_stats {
110147
egui::Window::new("Stats").show(ctx, |ui| {
@@ -117,7 +154,7 @@ impl UserInterface for ConwaySim {
117154
fn update_side_panel(&mut self, ctx: &egui::Context) {
118155
egui::SidePanel::left("Menu").show(ctx, |ui| {
119156
ui.add(
120-
egui::Slider::new(&mut self.map.cell_size, 0.1..=50.0)
157+
egui::Slider::new(&mut self.map.cell_size, CELL_MIN..=CELL_MAX)
121158
.step_by(0.1)
122159
.orientation(egui::SliderOrientation::Horizontal)
123160
.text("Cell Size"),
@@ -139,18 +176,18 @@ impl UserInterface for ConwaySim {
139176
// BUG: below sliders are bugged, if one of the viewports is updated, say, the x axis, the vertical
140177
// lines will not move along with the horizontal ones, leading to a realy weird effect, and vice
141178
// versa for the y axis, with the other lines not moving, while the vertical ones will move
142-
// ui.add(
143-
// egui::Slider::new(&mut self.map.x_axis, -1000..=1000)
144-
// .step_by(1.0)
145-
// .orientation(egui::SliderOrientation::Horizontal)
146-
// .text("X Axis"),
147-
// );
148-
// ui.add(
149-
// egui::Slider::new(&mut self.map.y_axis, -1000..=1000)
150-
// .step_by(1.0)
151-
// .orientation(egui::SliderOrientation::Horizontal)
152-
// .text("Y Axis"),
153-
// );
179+
ui.add(
180+
egui::Slider::new(&mut self.map.x_axis, -1000..=1000)
181+
.step_by(1.0)
182+
.orientation(egui::SliderOrientation::Horizontal)
183+
.text("X Axis"),
184+
);
185+
ui.add(
186+
egui::Slider::new(&mut self.map.y_axis, -1000..=1000)
187+
.step_by(1.0)
188+
.orientation(egui::SliderOrientation::Horizontal)
189+
.text("Y Axis"),
190+
);
154191
ui.add(
155192
egui::Slider::new(&mut self.map.map_size, 10..=500)
156193
.step_by(1.0)

src/conway/conway_map.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@ const NEIGHBORS: [(i32, i32); 8] = [
3737

3838
#[derive(serde::Deserialize, serde::Serialize)]
3939
#[serde(default)] // if we add new fields, give them default values when deserializing old state
40+
/// Eventually will be generalized to be a "Map" struct, but for now it's just a Conway's Game of
4041
pub struct Map {
41-
pub x_axis: i32,
42-
pub y_axis: i32,
43-
pub cell_size: f32,
42+
/// X axis of the map
43+
pub x_axis: i32,
44+
/// Y axis of the map
45+
pub y_axis: i32,
46+
/// Size of each cell in the map, will be clamped between constants CELL_MIN and CELL_MAX
47+
pub cell_size: f32,
48+
/// Size of the map, eventually I want this to be separate from our viewport
4449
pub map_size: i32,
50+
/// Speed of the simulation, in what unit? only God knows
4551
pub speed: u128,
52+
/// Frames per second
4653
pub fps: u32,
54+
///Determines the scarcity of cells in the initial state
4755
pub rand_scarcity: u32,
56+
/// Self explanatory
4857
pub light_mode: bool,
58+
/// Whether or not to draw gridlines
4959
pub lines: bool,
5060
pub is_initial: bool,
5161

@@ -133,6 +143,7 @@ impl Map {
133143
self.cells = HashSet::new();
134144
}
135145
pub fn fps_to_speed(fps: f32) -> u128 {
146+
//magic number?
136147
Duration::new(0, (1000000000.0 / fps) as u32).as_millis()
137148
}
138149
// NOTE: This could probably be useful for the refactor
@@ -196,6 +207,7 @@ impl Map {
196207
}
197208
(max_x, max_y)
198209
}
210+
//What does this do????
199211
pub fn center_cells(&mut self, rect: Rect) {
200212
let (min_x, min_y) = self.find_min();
201213
let (max_x, max_y) = self.find_max();
@@ -214,9 +226,11 @@ impl Map {
214226

215227
self.cells = elems_c;
216228
}
229+
// TODO: Refactor this code to actually work based on viewport logic instead, this might help
230+
// fix that bug I'm having regarding the x and y values not moving along when we update them
217231
pub fn draw_lines(&mut self, rect: Rect, shapes: &mut Vec<Shape>) {
218232
// Calculate stroke thickness based on cell size
219-
let stroke_thickness = self.exponential_easing(0.1, 50.0, 0.0, 2.0);
233+
let stroke_thickness = self.exponential_easing(crate::CELL_MIN, crate::CELL_MAX, 0.0, 2.0);
220234

221235
// Draw vertical grid lines
222236
for i in 0..=self.map_size {
@@ -289,6 +303,7 @@ impl Map {
289303
let exponent = -k * (self.cell_size - x_0);
290304
1.0 / (1.0 + exponent.exp())
291305
}
306+
/// Another easing function but this time we use exponential stuff cause I can
292307
fn exponential_easing(
293308
&mut self,
294309
min_cell_size: f32,

src/lib.rs

+35
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ pub mod app;
33
pub mod conway;
44
pub use app::ConwaySim;
55
pub use conway::conway_map::Map;
6+
pub use consts::*;
7+
use web_sys::console;
68

79
#[derive(Default)]
810
pub struct RunStatistics {
@@ -21,6 +23,23 @@ impl RunStatistics {
2123
}
2224
}
2325
}
26+
/// DECLARATION OF CONSTS
27+
28+
pub mod consts {
29+
pub const ZOOM_SENSITIVITY: f32 = 0.1;
30+
31+
pub const CELL_MAX: f32 = 50.0;
32+
pub const CELL_MIN: f32 = 0.1;
33+
}
34+
35+
36+
37+
38+
39+
40+
41+
42+
2443
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, serde::Deserialize, serde::Serialize)]
2544
#[serde(default)] // if we add new fields, give them default values when deserializing old state
2645
#[derive(Default)]
@@ -40,3 +59,19 @@ pub trait UserInterface {
4059
fn update_menu_bar(&self, ctx: &egui::Context);
4160
fn update_side_panel(&mut self, ctx: &egui::Context);
4261
}
62+
63+
pub struct Viewport {
64+
pub rect: egui::Rect,
65+
pub scale: f32, // Scale factor for zooming
66+
pub offset: egui::Vec2, // Offset for panning
67+
}
68+
69+
impl Default for Viewport {
70+
fn default() -> Self {
71+
Self {
72+
rect: egui::Rect::from_min_size(egui::Pos2::ZERO, egui::Vec2::ZERO),
73+
scale: 1.0,
74+
offset: egui::Vec2::default(),
75+
}
76+
}
77+
}

src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
3+
use web_sys::console;
34

45
// When compiling natively:
56
#[cfg(not(target_arch = "wasm32"))]
@@ -29,6 +30,7 @@ fn main() -> eframe::Result<()> {
2930
fn main() {
3031
// Redirect `log` message to `console.log` and friends:
3132
eframe::WebLogger::init(log::LevelFilter::Debug).ok();
33+
console::log_1(&"erm, what the deuce".into());
3234
let the_canvas_id = String::from("Cellular-Automata beta 0.0.1");
3335
let web_options = eframe::WebOptions::default();
3436
wasm_bindgen_futures::spawn_local(async {

0 commit comments

Comments
 (0)