refactor state management and input handling into separate classes

This commit is contained in:
2026-02-21 22:00:33 +01:00
parent f8fe9e35d6
commit 0d3913e27e
11 changed files with 452 additions and 326 deletions

43
include/input.hpp Normal file
View File

@ -0,0 +1,43 @@
#ifndef __INPUT_HPP_
#define __INPUT_HPP_
#include "renderer.hpp"
#include "state.hpp"
class InputHandler {
public:
StateManager &state;
Renderer &renderer;
int hov_x;
int hov_y;
int sel_x;
int sel_y;
bool has_block_add_xy = false;
int block_add_x = -1;
int block_add_y = -1;
public:
InputHandler(StateManager &state, Renderer &renderer)
: state(state), renderer(renderer), hov_x(-1), hov_y(-1), sel_x(-1),
sel_y(-1), has_block_add_xy(false), block_add_x(-1), block_add_y(-1) {}
InputHandler(const InputHandler &copy) = delete;
InputHandler &operator=(const InputHandler &copy) = delete;
InputHandler(InputHandler &&move) = delete;
InputHandler &operator=(InputHandler &&move) = delete;
~InputHandler() {}
public:
auto HandleMouseHover() -> void;
auto HandleMouse() -> void;
auto HandleKeys() -> void;
auto HandleInput() -> void;
};
#endif

View File

@ -1,6 +1,8 @@
#ifndef __MASS_SPRINGS_HPP_
#define __MASS_SPRINGS_HPP_
#include "config.hpp"
#include "klotski.hpp"
#include <raylib.h>
#include <raymath.h>
#include <string>
@ -97,7 +99,7 @@ public:
std::unordered_map<std::string, Spring> springs;
public:
MassSpringSystem() : last_build(1000) {};
MassSpringSystem() : last_build(REPULSION_GRID_REFRESH) {};
MassSpringSystem(const MassSpringSystem &copy) = delete;
MassSpringSystem &operator=(const MassSpringSystem &copy) = delete;
@ -110,14 +112,13 @@ private:
auto BuildGrid() -> void;
public:
auto AddMass(float mass, Vector3 position, bool fixed,
const std::string &state) -> void;
auto AddMass(float mass, Vector3 position, bool fixed, const State &state)
-> void;
auto GetMass(const std::string &state) -> Mass &;
auto GetMass(const State &state) -> Mass &;
auto AddSpring(const std::string &massA, const std::string &massB,
float spring_constant, float dampening_constant,
float rest_length) -> void;
auto AddSpring(const State &massA, const State &massB, float spring_constant,
float dampening_constant, float rest_length) -> void;
auto Clear() -> void;
@ -130,6 +131,8 @@ public:
auto EulerUpdate(float delta_time) -> void;
auto VerletUpdate(float delta_time) -> void;
auto InvalidateGrid() -> void;
};
#endif

View File

@ -1,5 +1,5 @@
#ifndef __STATES_HPP_
#define __STATES_HPP_
#ifndef __PRESETS_HPP_
#define __PRESETS_HPP_
#include <functional>
#include <vector>
@ -200,13 +200,13 @@ inline auto state_new_century_wc(const State &state) -> bool {
return state.state == "F4x5:21......1121..12bb..12........12111111..";
}
std::vector<StateGenerator> generators{
static std::vector<StateGenerator> generators{
state_simple_1r, state_simple_2r, state_simple_3r, state_complex_1r,
state_complex_2r, state_complex_3r, state_complex_4f, state_complex_5r,
state_complex_6r, state_klotski, state_century, state_super_century,
state_new_century};
std::vector<WinCondition> win_conditions{
static std::vector<WinCondition> win_conditions{
state_simple_1r_wc, state_simple_2r_wc, state_simple_3r_wc,
state_complex_1r_wc, state_complex_2r_wc, state_complex_3r_wc,
state_complex_4f_wc, state_complex_5r_wc, state_complex_6r_wc,

View File

@ -48,7 +48,6 @@ private:
RenderTexture render_target;
RenderTexture klotski_target;
RenderTexture menu_target;
std::unordered_set<State> winning_states;
public:
bool mark_solutions;
@ -77,26 +76,22 @@ public:
}
public:
auto UpdateWinningStates(const MassSpringSystem &masssprings,
const WinCondition win_condition) -> void;
auto AddWinningState(const State &state, const WinCondition win_condition)
-> void;
auto UpdateCamera(const MassSpringSystem &masssprings, const State &current)
-> void;
auto UpdateTextureSizes() -> void;
auto DrawMassSprings(const MassSpringSystem &masssprings,
const State &current) -> void;
const State &current,
const std::unordered_set<State> &winning_states) -> void;
auto DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x,
int sel_y, int block_add_x, int block_add_y,
const WinCondition win_condition) -> void;
auto DrawMenu(const MassSpringSystem &masssprings, int current_preset,
const State &current_state) -> void;
const State &current_state,
const std::unordered_set<State> &winning_states) -> void;
auto DrawTextures() -> void;
};

58
include/state.hpp Normal file
View File

@ -0,0 +1,58 @@
#ifndef __STATE_HPP_
#define __STATE_HPP_
#include "config.hpp"
#include "klotski.hpp"
#include "mass_springs.hpp"
#include "presets.hpp"
#include <raymath.h>
class StateManager {
public:
MassSpringSystem &mass_springs;
int current_preset;
State current_state;
State previous_state;
bool edited = false;
std::unordered_set<State> winning_states;
public:
StateManager(MassSpringSystem &mass_springs)
: mass_springs(mass_springs), current_preset(0),
current_state(generators[current_preset]()),
previous_state(current_state), edited(false) {
mass_springs.AddMass(MASS, Vector3Zero(), false, current_state);
}
StateManager(const StateManager &copy) = delete;
StateManager &operator=(const StateManager &copy) = delete;
StateManager(StateManager &&move) = delete;
StateManager &operator=(StateManager &&move) = delete;
~StateManager() {}
public:
auto LoadPreset(int preset) -> void;
auto ResetState() -> void;
auto PreviousPreset() -> void;
auto NextPreset() -> void;
auto FillGraph() -> void;
auto UpdateGraph() -> void;
auto ClearGraph() -> void;
auto FindWinningStates() -> void;
auto CurrentWinCondition() -> WinCondition;
};
#endif