Better UI Scaling

This commit is contained in:
TheBrokenRail 2024-11-28 05:25:22 -05:00
parent cfac7d0a12
commit 814217a259
8 changed files with 59 additions and 12 deletions

View File

@ -53,6 +53,7 @@
* `Proper Entity Shading` (Enabled By Default)
* `Fix Sugar Position In Hand` (Enabled By Default)
* `Fix Reloading Textures On Resize` (Enabled By Default)
* `Improved UI Scaling` (Enabled By Default)
* Existing Functionality (All Enabled By Default)
* `Fix Screen Rendering When Hiding HUD`
* `Sanitize Usernames`

Binary file not shown.

Before

Width:  |  Height:  |  Size: 115 KiB

After

Width:  |  Height:  |  Size: 125 KiB

View File

@ -1,5 +1,6 @@
#include <vector>
#include <limits>
#include <cmath>
#include <libreborn/util.h>
@ -138,14 +139,16 @@ void ConfigurationUI::draw_main() const {
state.render_distance = render_distances[render_distance_index];
}
// UI Scale
std::string scale_format = "%i";
scale_format += 'x';
const int precision = std::floor(state.gui_scale) == state.gui_scale ? 0 : 1;
std::string scale_format = "%." + std::to_string(precision) + "fx";
if (state.gui_scale <= AUTO_GUI_SCALE) {
scale_format = "Auto";
}
int gui_scale_int = int(state.gui_scale);
if (ImGui::SliderInt(labels[2], &gui_scale_int, 0, 8, scale_format.c_str())) {
state.gui_scale = float(gui_scale_int);
char display_gui_scale[64];
sprintf(display_gui_scale, scale_format.c_str(), state.gui_scale);
float new_gui_scale = state.gui_scale;
if (ImGui::SliderFloat(labels[2], &new_gui_scale, 0, 8, display_gui_scale, ImGuiSliderFlags_NoRoundToFormat)) {
state.gui_scale = step_value(new_gui_scale);
if (state.gui_scale < AUTO_GUI_SCALE) {
state.gui_scale = AUTO_GUI_SCALE;
}

View File

@ -65,3 +65,6 @@ std::string format_time(const char *fmt, int time);
// Default MCPI Port
// This Macro DOES NOT Control MCPI
#define DEFAULT_MULTIPLAYER_PORT 19132
// Step Value
float step_value(float value, float step = 0.5f);

View File

@ -44,6 +44,7 @@ CATEGORY User Interface
TRUE Improved Button Hover Behavior
TRUE Classic Item Count UI
TRUE Click Buttons On Mouse Down
TRUE Improved UI Scaling
CATEGORY Rendering
CATEGORY Optimizations
TRUE Optimized Chunk Sorting

View File

@ -3,6 +3,7 @@
#include <sys/stat.h>
#include <cstring>
#include <ctime>
#include <cmath>
#include <libreborn/util.h>
#include <libreborn/config.h>
@ -135,3 +136,8 @@ std::string format_time(const char *fmt, const int time) {
// This Will Break In 2038
return _format_time(fmt, time);
}
// Step
float step_value(const float value, const float step) {
return std::round(value / step) * step;
}

View File

@ -1,8 +1,11 @@
#include <libreborn/patch.h>
#include <libreborn/env.h>
#include <libreborn/util.h>
#include <symbols/minecraft.h>
#include <media-layer/core.h>
#include <GLES/gl.h>
#include <SDL/SDL.h>
@ -245,6 +248,29 @@ static void LocalPlayer_openTextEdit_injection(__attribute__((unused)) LocalPlay
}
}
// Better GUI Scaling
static void set_gui_scale(const float new_scale) {
union {
float a;
void *b;
} pun = {};
pun.a = new_scale;
patch_address((void *) 0x17520, pun.b);
}
static float calculate_scale(const float value, const float default_value) {
constexpr float initial_scale = 2.5f;
const float scale = initial_scale * (value / default_value);
return step_value(scale);
}
static void Minecraft_setSize_injection(Minecraft_setSize_t original, Minecraft *self, const int width, const int height) {
// Calculate Scale
const float a = calculate_scale(float(width), DEFAULT_WIDTH);
const float b = calculate_scale(float(height), DEFAULT_HEIGHT);
set_gui_scale(std::min(a, b));
// Call Original Method
original(self, width, height);
}
// Init
void _init_misc_ui() {
// Food Overlay
@ -303,18 +329,24 @@ void _init_misc_ui() {
}
// Custom GUI Scale
bool patch_gui_scaling = false;
const char *gui_scale_str = getenv(MCPI_GUI_SCALE_ENV);
if (gui_scale_str != nullptr) {
float gui_scale;
env_value_to_obj(gui_scale, gui_scale_str);
if (gui_scale > 0) {
patch_gui_scaling = true;
set_gui_scale(gui_scale);
}
}
if (feature_has("Improved UI Scaling", server_disabled) && !patch_gui_scaling) {
overwrite_calls(Minecraft_setSize, Minecraft_setSize_injection);
patch_gui_scaling = true;
}
if (patch_gui_scaling) {
unsigned char nop_patch[4] = {0x00, 0xf0, 0x20, 0xe3}; // "nop"
patch((void *) 0x173e8, nop_patch);
patch((void *) 0x173f0, nop_patch);
uint32_t gui_scale_raw;
memcpy(&gui_scale_raw, &gui_scale, sizeof (gui_scale_raw));
patch_address((void *) 0x17520, (void *) gui_scale_raw);
}
}
// Don't Wrap Text On '\r' Or '\t' Because They Are Actual Characters In MCPI

View File

@ -17,6 +17,7 @@ method LevelStorageSource *getLevelSource() = 0x16e84;
method void handleMouseDown(int param_1, bool can_destroy) = 0x1584c;
method void handleBuildAction(uint *build_action_intention) = 0x15920;
method void joinMultiplayer(const PingedCompatibleServer &server) = 0x165f4;
method void setSize(int width, int height) = 0x173a4;
virtual-method void update() = 0x24;
virtual-method int handleBack(bool do_nothing) = 0x34;