Skip to content

Commit

Permalink
Allow object viewer scaling, Fix zoom reset
Browse files Browse the repository at this point in the history
  • Loading branch information
netniV committed Mar 27, 2024
1 parent 22810de commit 427629a
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 70 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## 0.6.1

- Switch to xmake
- When ui_scale set to 0.0f disable all scaling features
_
- Add ui_scale_viewer to scale the size of object viewers
- Fix ui_scale or ui_scale_viewer so when set to 0.0f disable those scaling features
- Fix zoom_min and zoom_max no longer set default system zoom (only presets can)

## 0.6.0

- Add customisable hotkeys
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## Features

- Set system UI scale + adjustment factor
- Set viewer UI scale
- Set system zoom
- default
- maximum
Expand Down Expand Up @@ -66,7 +67,7 @@ The following keybinds have recently been added:
Key | Shortcut
--: | ---
MINUS | Zoom (min)
EQUAL | Zoom (default)
EQUALS | Zoom (default)
BACKSPACE | Zoom (max)
B | Bookmarks
F | Factions
Expand All @@ -80,6 +81,8 @@ Shift-T | Away Teams
X | ExoComp
Z | Daily Missions
` | Open Alliance Chat - Side of Screen
SHIFT-PGUP | UI Viewer Scale Up
SHIFT-PGDOWN | UI Viewer Scale Down

## Contributing / Building

Expand Down
11 changes: 9 additions & 2 deletions example_community_patch_settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ ui_scale = 0.9
# This specifies the amount to adjust the scale by, 0.05 being default
ui_scale_adjust = 0.05

# This specifies the amount to scale the UI Object Viewers by, 1.0 being default and anything above 1 making it larger than default, defaults to 0.9
ui_scale_viewer = 1.0

vsync = 1

# This controls the maximum zoom distance in System View, default is 2500.
# If you set this too a very high value the distance at which ship text labels are shown is also increased.
zoom = 2500


[sync]
# Token for auth on remote system
token = ''
Expand Down Expand Up @@ -313,6 +315,12 @@ ui_scaledown = 'PGDOWN'
# Scale the UI up (expand the HUD)
ui_scaleup = 'PGUP'

# Scale the UI Object Viewer down (shrink the Object Viewer)
ui_scaleviewerdown = 'SHIFT-PGDOWN'

# Scale the UI Object Viewer up (expand the Object Viewer)
ui_scaleviewerup = 'SHIFT-PGUP'

# Adjust zoom to Presets
zoom_preset1 = 'F1'
zoom_preset2 = 'F2'
Expand All @@ -334,4 +342,3 @@ zoom_out = 'E'
zoom_max = 'MINUS'
zoom_min = 'BACKSPACE'
zoom_reset = '='

16 changes: 16 additions & 0 deletions mods/src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ void Config::AdjustUiScale(bool scaleUp)
}
}

void Config::AdjustUiViewerScale(bool scaleUp)
{
if (this->ui_scale_viewer != 0.0f) {
auto old_scale = this->ui_scale_viewer;
auto scale_factor = (scaleUp ? 1.0f : -1.0f) * this->ui_scale_adjust;
auto new_scale = this->ui_scale_viewer + (scale_factor * 0.25f);
this->ui_scale_viewer = std::clamp(new_scale, 0.1f, 2.0f);

spdlog::info("UI Viewer has been scaled {}, was {}, now {} (unclamped {})", (scaleUp ? "UP" : "DOWN"), old_scale,
this->ui_scale_viewer, new_scale);
}
}

std::string get_config_type_as_string(toml::node_type type)
{
switch (type) {
Expand Down Expand Up @@ -288,6 +301,7 @@ void Config::Load()

this->ui_scale = get_config_or_default(config, parsed, "graphics", "ui_scale", 0.9f);
this->ui_scale_adjust = get_config_or_default(config, parsed, "graphics", "ui_scale_adjust", 0.05f);
this->ui_scale_viewer = get_config_or_default(config, parsed, "graphics", "ui_scale_viewer", 100.0f);
this->zoom = get_config_or_default(config, parsed, "graphics", "zoom", 2500.f);
this->free_resize = get_config_or_default(config, parsed, "graphics", "free_resize", true);
this->adjust_scale_res = get_config_or_default(config, parsed, "graphics", "adjust_scale_res", false);
Expand Down Expand Up @@ -432,6 +446,8 @@ void Config::Load()
parse_config_shortcut(config, parsed, "zoom_reset", GameFunction::ZoomReset, "=");
parse_config_shortcut(config, parsed, "ui_scaleup", GameFunction::UiScaleUp, "PGUP");
parse_config_shortcut(config, parsed, "ui_scaledown", GameFunction::UiScaleDown, "PGDOWN");
parse_config_shortcut(config, parsed, "ui_scaleviewerup", GameFunction::UiViewerScaleUp, "SHIFT-PGUP");
parse_config_shortcut(config, parsed, "ui_scaleviewerdown", GameFunction::UiViewerScaleDown, "SHIFT-PGDOWN");
parse_config_shortcut(config, parsed, "log_debug", GameFunction::LogLevelDebug, "F9");
parse_config_shortcut(config, parsed, "log_trace", GameFunction::LogLevelTrace, "SHIFT-F9");
parse_config_shortcut(config, parsed, "log_info", GameFunction::LogLevelInfo, "F11");
Expand Down
2 changes: 2 additions & 0 deletions mods/src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class Config
static void Save(toml::table config, std::string_view filename, bool apply_warning = true);
void Load();
void AdjustUiScale(bool scaleUp);
void AdjustUiViewerScale(bool scaleUp);

public:
float ui_scale;
float ui_scale_adjust;
float ui_scale_viewer;
float zoom;
bool free_resize;
bool adjust_scale_res;
Expand Down
2 changes: 2 additions & 0 deletions mods/src/patches/gamefunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ enum GameFunction {
ZoomReset,
UiScaleUp,
UiScaleDown,
UiViewerScaleUp,
UiViewerScaleDown,
ActionPrimary,
ActionSecondary,
ActionView,
Expand Down
4 changes: 4 additions & 0 deletions mods/src/patches/parts/hotkeys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ void ScreenManager_Update_Hook(auto original, ScreenManager* _this)
config->AdjustUiScale(true);
} else if (MapKey::IsPressed(GameFunction::UiScaleDown)) {
config->AdjustUiScale(false);
} else if (MapKey::IsPressed(GameFunction::UiViewerScaleUp)) {
config->AdjustUiViewerScale(true);
} else if (MapKey::IsPressed(GameFunction::UiViewerScaleDown)) {
config->AdjustUiViewerScale(false);
} else if (MapKey::IsDown(GameFunction::TogglePreviewLocate)) {
config->disable_preview_locate = !config->disable_preview_locate;
} else if (MapKey::IsDown(GameFunction::TogglePreviewRecall)) {
Expand Down
46 changes: 1 addition & 45 deletions mods/src/patches/parts/sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <prime/Hub.h>
#include <prime/LanguageManager.h>
#include <prime/ServiceResponse.h>

#include <str_utils.h>
#include <Digit.PrimeServer.Models.pb.h>

#include <nlohmann/json.hpp>
Expand All @@ -32,9 +32,6 @@
#include <queue>
#include <string>

static std::string to_string(const std::wstring& str);
static std::wstring to_wstring(const std::string& str);

namespace http
{

Expand Down Expand Up @@ -191,47 +188,6 @@ static void write_data(std::string file_data)

} // namespace http

#include <simdutf.h>

static std::wstring to_wstring(const std::string& str)
{
#if _WIN32
return winrt::to_hstring(str).c_str();
#else
size_t expected_utf32words = simdutf::utf32_length_from_utf8(str.data(), str.length());
std::unique_ptr<char32_t[]> utf32_output{new char32_t[expected_utf32words]};
size_t utf16words = simdutf::convert_utf8_to_utf32(str.data(), str.length(), utf32_output.get());
return std::wstring(utf32_output.get(), utf32_output.get() + utf16words);
#endif
}

static std::wstring to_wstring(Il2CppString* str)
{
#if _WIN32
return str->chars;
#else
size_t expected_utf32words = simdutf::utf32_length_from_utf16(str->chars, str->length);
std::unique_ptr<char32_t[]> utf32_output{new char32_t[expected_utf32words]};
size_t utf16words = simdutf::convert_utf16_to_utf32(str->chars, str->length, utf32_output.get());
return std::wstring(utf32_output.get(), utf32_output.get() + utf16words);
#endif
}

static std::string to_string(const std::wstring& str)
{
#if _WIN32
size_t expected_utf16words = simdutf::utf8_length_from_utf16((char16_t*)str.data(), str.length());
std::unique_ptr<char8_t[]> utf8_output{new char8_t[expected_utf16words]};
size_t utf16words = simdutf::convert_utf16_to_utf8((char16_t*)str.data(), str.length(), (char*)utf8_output.get());
return std::string(utf8_output.get(), utf8_output.get() + utf16words);
#else
size_t expected_utf32words = simdutf::utf8_length_from_utf32((char32_t*)str.data(), str.length());
std::unique_ptr<char8_t[]> utf32_output{new char8_t[expected_utf32words]};
size_t utf16words = simdutf::convert_utf32_to_utf8((char32_t*)str.data(), str.length(), (char*)utf32_output.get());
return std::string(utf32_output.get(), utf32_output.get() + utf16words);
#endif
}

std::mutex m;
std::condition_variable cv;
std::queue<std::string> sync_data_queue;
Expand Down
62 changes: 55 additions & 7 deletions mods/src/patches/parts/ui_scale.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include "config.h"
#include <config.h>

#include <il2cpp/il2cpp_helper.h>

#include <prime/CanvasScaler.h>
#include <prime/ScreenManager.h>
#include <prime/Transform.h>

#include <spdlog/spdlog.h>
#include <spud/detour.h>

#include <prime/Vector3.h>
#include <str_utils.h>

void SetResolution_Hook(auto original, int x, int y, int mode, int unk)
{
spdlog::trace("Setting resoltuion {} x {}", x, y);
Expand Down Expand Up @@ -45,18 +49,62 @@ void ScreenManager_UpdateCanvasRootScaleFactor_Hook(auto original, ScreenManager
}
}

BOOL SetWindowPos_Hook(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
{
spdlog::trace("Window size/position {} (x) {} (y) {} (cx) {} (cy)", X, Y, cx, cy);
return SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
}

void CanvasController_Show(auto original, CanvasController* _this)
{
const auto ui_scale_viewer = Config::Get().ui_scale_viewer;
if (ui_scale_viewer != 0.0f && to_wstring(_this->name) == L"ObjectViewerTemplate_Canvas") {
auto transform = _this->transform;
auto localScale = transform->localScale;
localScale->x = ui_scale_viewer;
localScale->y = ui_scale_viewer;
localScale->z = ui_scale_viewer;
transform->localScale = localScale;
}
original(_this);
}

void InstallUiScaleHooks()
{
auto screen_manager_helper = il2cpp_get_class_helper("Assembly-CSharp", "Digit.Client.UI", "ScreenManager");
auto ptr_update_scale = screen_manager_helper.GetMethod("UpdateCanvasRootScaleFactor");
if (!ptr_update_scale) {
return;
if (ptr_update_scale) {
SPUD_STATIC_DETOUR(ptr_update_scale, ScreenManager_UpdateCanvasRootScaleFactor_Hook);
static auto SetResolution = il2cpp_resolve_icall_typed<void(int, int, int, int)>(
"UnityEngine.Screen::SetResolution(System.Int32,System.Int32,UnityEngine.FullScreenMode,System.Int32)");
SPUD_STATIC_DETOUR(SetResolution, SetResolution_Hook);
}

SPUD_STATIC_DETOUR(ptr_update_scale, ScreenManager_UpdateCanvasRootScaleFactor_Hook);
static auto SetResolution = il2cpp_resolve_icall_typed<void(int, int, int, int)>(
"UnityEngine.Screen::SetResolution(System.Int32,System.Int32,UnityEngine.FullScreenMode,System.Int32)");
SPUD_STATIC_DETOUR(SetResolution, SetResolution_Hook);
spdlog::info("Finding CanvasController");

auto canvas_controller_helper = il2cpp_get_class_helper("Assembly-CSharp", "Digit.Client.UI", "CanvasController");

spdlog::info("Finding CanvasController_Show");

auto ptr_canvas_show = canvas_controller_helper.GetMethodSpecial("Show", [](auto count, const Il2CppType** params) {
if (count != 2) {
return false;
}

auto p1 = params[0]->type;
auto p2 = params[1]->type;

spdlog::info("Finding CanvasController_Show Params {} - {}", p1, p2);

if (p1 == IL2CPP_TYPE_I4 && p2 == IL2CPP_TYPE_BOOLEAN) {
return true;
}
return false;
});
if (ptr_canvas_show) {
spdlog::info("Hooking CanvasController_Show");
SPUD_STATIC_DETOUR(ptr_canvas_show, CanvasController_Show);
}

Config::RefreshDPI();
}
26 changes: 15 additions & 11 deletions mods/src/patches/parts/zoom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void NavigationZoom_Update_Hook(auto original, NavigationZoom *_this)
const auto dt = GetDeltaTime();
auto zoomDelta = 0.0f;
bool do_absolute_zoom = false;
bool do_store_zoom = false;
auto config = &Config::Get();

if (!Key::IsInputFocused()) {
Expand All @@ -58,25 +59,30 @@ void NavigationZoom_Update_Hook(auto original, NavigationZoom *_this)

do_absolute_zoom = true;
if (MapKey::IsDown(GameFunction::ZoomPreset1)) {
zoomDelta = config->system_zoom_preset_1;
zoomDelta = config->system_zoom_preset_1;
do_store_zoom = true;
} else if (MapKey::IsDown(GameFunction::ZoomPreset2)) {
zoomDelta = config->system_zoom_preset_2;
zoomDelta = config->system_zoom_preset_2;
do_store_zoom = true;
} else if (MapKey::IsDown(GameFunction::ZoomPreset3)) {
zoomDelta = config->system_zoom_preset_3;
zoomDelta = config->system_zoom_preset_3;
do_store_zoom = true;
} else if (MapKey::IsDown(GameFunction::ZoomPreset4)) {
zoomDelta = config->system_zoom_preset_4;
zoomDelta = config->system_zoom_preset_4;
do_store_zoom = true;
} else if (MapKey::IsDown(GameFunction::ZoomPreset5)) {
zoomDelta = config->system_zoom_preset_5;
zoomDelta = config->system_zoom_preset_5;
do_store_zoom = true;
}

if (config->hotkeys_extended) {
if (MapKey::IsDown(GameFunction::ZoomReset)) {
do_absolute_zoom = false;
do_default_zoom = true;
} else if (MapKey::IsDown(GameFunction::ZoomMin)) {
zoomDelta = config->zoom;
zoomDelta = config->zoom;
} else if (MapKey::IsDown(GameFunction::ZoomMax)) {
zoomDelta = 100;
zoomDelta = 100;
}
}

Expand Down Expand Up @@ -116,10 +122,8 @@ void NavigationZoom_Update_Hook(auto original, NavigationZoom *_this)
}
}

if (zoomDelta > 0.0f && config->use_presets_as_default) {
if (!do_default_zoom && do_absolute_zoom) {
StoreZoom("System Preset Default from Preset", config->default_system_zoom, _this);
}
if (zoomDelta > 0.0f && config->use_presets_as_default && do_store_zoom) {
StoreZoom("System Preset Default from Preset", config->default_system_zoom, _this);
}

do_default_zoom = false;
Expand Down
27 changes: 27 additions & 0 deletions mods/src/prime/Canvas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "il2cpp/il2cpp_helper.h"

struct Canvas {
public:
__declspec(property(get = __get_scaleFactor, put = __set_scaleFactor)) float scaleFactor;

private:
static IL2CppClassHelper& get_class_helper()
{
static auto class_helper = il2cpp_get_class_helper("UnityEngine.UI", "UnityEngine.UI", "Canvas");
return class_helper;
}

public:
float __get_scaleFactor()
{
static auto field = get_class_helper().GetProperty("scaleFactor");
return *field.Get<float>(this);
}
void __set_scaleFactor(float v)
{
static auto field = get_class_helper().GetProperty("scaleFactor");
field.SetRaw<float>(this, v);
}
};
Loading

0 comments on commit 427629a

Please sign in to comment.