minecraft-pi-reborn/mods/src/input/toggle.c

100 lines
3.1 KiB
C
Raw Normal View History

2021-07-04 23:02:45 +00:00
#include <libreborn/libreborn.h>
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2021-07-04 23:02:45 +00:00
2022-06-25 21:30:08 +00:00
#include "input-internal.h"
#include <mods/input/input.h>
#include <mods/feature/feature.h>
2024-02-03 03:25:22 +00:00
#include <mods/misc/misc.h>
2021-07-04 23:02:45 +00:00
// Handle Toggle Options
2024-02-03 03:25:22 +00:00
static bool _handle_toggle_options(Minecraft *minecraft, int key) {
Options *options = &minecraft->options;
if (key == 0x70) {
// Toggle Hide GUI
options->hide_gui = options->hide_gui ^ 1;
return 1;
} else if (key == 0x74) {
// Toggle Third Person
options->third_person = (options->third_person + 1) % 3;
return 1;
} else {
return 0;
}
}
static void _fix_third_person(Minecraft *minecraft) {
// Fix Broken Value From Third-Person OptionsButton Toggle
// (Because Front-Facing Code Repurposes A Boolean As A Ternary)
Options *options = &minecraft->options;
if (options->third_person == 3) {
options->third_person = 0;
2022-05-17 22:31:25 +00:00
}
}
// Font-Facing View
2024-01-06 11:30:23 +00:00
static void invert_rotation(Entity *entity) {
2022-09-22 03:06:58 +00:00
if (entity != NULL) {
2024-01-06 11:30:23 +00:00
entity->yaw = 180.f + entity->yaw;
entity->old_yaw = 180.f + entity->old_yaw;
entity->pitch = -entity->pitch;
entity->old_pitch = -entity->old_pitch;
2022-09-22 03:06:58 +00:00
}
}
2024-01-06 11:30:23 +00:00
static void revert_rotation(Entity *entity) {
2022-09-22 03:06:58 +00:00
if (entity != NULL) {
2024-01-06 11:30:23 +00:00
entity->yaw = -180.f + entity->yaw;
entity->old_yaw = -180.f + entity->old_yaw;
entity->pitch = -entity->pitch;
entity->old_pitch = -entity->old_pitch;
2022-09-22 03:06:58 +00:00
}
}
static int is_front_facing = 0;
2024-01-06 11:30:23 +00:00
static LocalPlayer *stored_player = NULL;
static void GameRenderer_setupCamera_injection(GameRenderer *game_renderer, float param_1, int param_2) {
2022-05-17 22:31:25 +00:00
// Get Objects
2024-01-06 11:30:23 +00:00
Minecraft *minecraft = game_renderer->minecraft;
stored_player = minecraft->player;
2022-05-17 22:31:25 +00:00
// Check If In Third-Person
2024-01-06 11:30:23 +00:00
Options *options = &minecraft->options;
is_front_facing = (options->third_person == 2);
2022-05-17 22:31:25 +00:00
// Invert Rotation
2022-09-22 03:06:58 +00:00
if (is_front_facing) {
2024-01-06 11:30:23 +00:00
invert_rotation((Entity *) stored_player);
2022-05-17 22:31:25 +00:00
}
// Call Original Method
2024-01-07 08:23:43 +00:00
GameRenderer_setupCamera(game_renderer, param_1, param_2);
2022-05-17 22:31:25 +00:00
// Revert
2022-09-22 03:06:58 +00:00
if (is_front_facing) {
2024-01-06 11:30:23 +00:00
revert_rotation((Entity *) stored_player);
2022-09-22 03:06:58 +00:00
}
}
2024-01-06 11:30:23 +00:00
static void ParticleEngine_render_injection(ParticleEngine *particle_engine, Entity *entity, float param_2) {
2022-09-22 03:06:58 +00:00
// Invert Rotation
2024-01-06 11:30:23 +00:00
if (is_front_facing && (Entity *) stored_player == entity) {
invert_rotation((Entity *) stored_player);
2022-09-22 03:06:58 +00:00
}
// Call Original Method
2024-01-07 08:23:43 +00:00
ParticleEngine_render(particle_engine, entity, param_2);
2022-09-22 03:06:58 +00:00
// Revert
2024-01-06 11:30:23 +00:00
if (is_front_facing && (Entity *) stored_player == entity) {
revert_rotation((Entity *) stored_player);
2021-07-04 23:02:45 +00:00
}
}
// Init
void _init_toggle() {
2024-02-03 03:25:22 +00:00
if (feature_has("Bind Common Toggleable Options To Function Keys", server_disabled)) {
misc_run_on_game_key_press(_handle_toggle_options);
misc_run_on_update(_fix_third_person);
2022-05-17 22:31:25 +00:00
2024-02-03 03:25:22 +00:00
// Font-Facing View
2022-05-17 22:31:25 +00:00
overwrite_calls((void *) GameRenderer_setupCamera, (void *) GameRenderer_setupCamera_injection);
2022-09-22 03:06:58 +00:00
overwrite_calls((void *) ParticleEngine_render, (void *) ParticleEngine_render_injection);
2022-05-17 22:31:25 +00:00
}
2021-07-04 23:02:45 +00:00
}