Skip to content

Commit

Permalink
dev update (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlthelama authored Dec 18, 2023
2 parents 10d1b6d + a9c99e1 commit 767068d
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 75 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25)

set(NAME "SkyrimCharacterSheet")
set(VERSION 1.1.0.3)
set(VERSION 1.1.1.0)

# ---- Options ----

Expand Down
6 changes: 6 additions & 0 deletions cmake/sourcelist.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ set(sources ${sources}
src/event/input_event.h
src/event/menu_open_close_event.cpp
src/event/menu_open_close_event.h
src/hook/hook.cpp
src/hook/hook.h
src/hook/menu_control_hook.cpp
src/hook/menu_control_hook.h
src/input/menu_key_input_holder.cpp
src/input/menu_key_input_holder.h
src/main.cpp
src/mod/armor_rating_rescaled_remake.cpp
src/mod/armor_rating_rescaled_remake.h
Expand Down
2 changes: 1 addition & 1 deletion config/SkyrimCharacterSheet_Input.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"debug": true,
"debug": false,
"stat": {
"open_key_combination": [22],
"close_key_combination": [22],
Expand Down
28 changes: 6 additions & 22 deletions src/event/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace event {
[[maybe_unused]] RE::BSTEventSource<RE::InputEvent*>* a_event_source) {
open_key_combo_ = setting::input_setting::get_open_menu_key_combination();
close_key_combo_ = setting::input_setting::get_close_menu_key_combination();
open_inventory_key_combo_ = setting::input_setting::get_open_inventory_menu_key_combination();
close_inventory_key_combo_ = setting::input_setting::get_open_inventory_menu_key_combination();

if (is_one_combo_empty()) {
return event_result::kContinue;
Expand Down Expand Up @@ -56,22 +54,6 @@ namespace event {
continue;
}

if (open_key_combo_.contains(key) || close_key_combo_.contains(key) ||
open_inventory_key_combo_.contains(key) || close_inventory_key_combo_.contains(key)) {
add_key_down(key_down_list_, key);
}

if (ui->IsMenuOpen(RE::InventoryMenu::MENU_NAME) || ui->IsMenuOpen(RE::MagicMenu::MENU_NAME)) {
if (close_inventory_key_combo_ == key_down_list_ && scaleform::stats_inventory_menu::is_menu_open()) {
log_combo_set(close_inventory_key_combo_, key_down_list_);
scaleform::stats_inventory_menu::close();
}
if (open_inventory_key_combo_ == key_down_list_ && !scaleform::stats_inventory_menu::is_menu_open()) {
log_combo_set(open_inventory_key_combo_, key_down_list_);
scaleform::stats_inventory_menu::open();
}
}

if (auto* control_map = RE::ControlMap::GetSingleton(); !control_map->IsMovementControlsEnabled()) {
continue;
}
Expand All @@ -88,6 +70,10 @@ namespace event {
continue;
}

if (open_key_combo_.contains(key) || close_key_combo_.contains(key)) {
add_key_down(key_down_list_, key);
}

//logger::info("user event {}, id {}"sv, button->userEvent, button->idCode);

if (close_key_combo_ == key_down_list_) {
Expand All @@ -106,6 +92,7 @@ namespace event {
}
}
}

return event_result::kContinue;
}

Expand All @@ -123,10 +110,7 @@ namespace event {
}
}

bool input_event::is_one_combo_empty() {
return open_key_combo_.empty() || close_key_combo_.empty() || open_inventory_key_combo_.empty() ||
close_inventory_key_combo_.empty();
}
bool input_event::is_one_combo_empty() { return open_key_combo_.empty() || close_key_combo_.empty(); }

void input_event::log_combo_set(std::set<uint32_t>& a_needed, std::set<uint32_t>& a_down) {
logger::trace("key combo needed {}, down list {}"sv,
Expand Down
10 changes: 4 additions & 6 deletions src/event/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ namespace event {

std::set<uint32_t> open_key_combo_;
std::set<uint32_t> close_key_combo_;
std::set<uint32_t> open_inventory_key_combo_;
std::set<uint32_t> close_inventory_key_combo_;

std::set<uint32_t> key_down_list_;

static void add_key_down(std::set<uint32_t>& a_set, uint32_t key);

static void remove_key_down(std::set<uint32_t>& a_set, uint32_t key);

bool is_one_combo_empty();

static void log_combo_set(std::set<uint32_t>& a_needed, std::set<uint32_t>& a_down);
};
}
35 changes: 22 additions & 13 deletions src/event/menu_open_close_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace event {
}

void menu_open_close_event::sink() { RE::UI::GetSingleton()->AddEventSink(get_singleton()); }

menu_open_close_event::event_result menu_open_close_event::ProcessEvent(const RE::MenuOpenCloseEvent* a_event,
[[maybe_unused]] RE::BSTEventSource<RE::MenuOpenCloseEvent>* a_event_source) {
if (!a_event) {
Expand All @@ -20,24 +21,32 @@ namespace event {

//sometimes it can happen, if you press menu button and inventory it opens both
//that should not happen
if (a_event->opening &&
(a_event->menuName == RE::InventoryMenu::MENU_NAME || a_event->menuName == RE::MagicMenu::MENU_NAME)) {
if (scaleform::stats_menu::is_menu_open() || scaleform::faction_menu::is_menu_open()) {
scaleform::stats_menu::close();
scaleform::faction_menu::close();
if (a_event->opening) {
if (a_event->menuName == RE::InventoryMenu::MENU_NAME || a_event->menuName == RE::MagicMenu::MENU_NAME) {
if (scaleform::stats_menu::is_menu_open() || scaleform::faction_menu::is_menu_open()) {
scaleform::stats_menu::close();
scaleform::faction_menu::close();
}

if ((setting::input_setting::auto_open_inventory_menu_inventory() &&
a_event->menuName == RE::InventoryMenu::MENU_NAME) ||
(setting::input_setting::auto_open_inventory_menu_magic() &&
a_event->menuName == RE::MagicMenu::MENU_NAME)) {
scaleform::stats_inventory_menu::open();
}
}

if ((setting::input_setting::auto_open_inventory_menu_inventory() &&
a_event->menuName == RE::InventoryMenu::MENU_NAME) ||
(setting::input_setting::auto_open_inventory_menu_magic() &&
a_event->menuName == RE::MagicMenu::MENU_NAME)) {
scaleform::stats_inventory_menu::open();
if (a_event->menuName == RE::BookMenu::MENU_NAME) {
if (scaleform::stats_inventory_menu::is_menu_open()) {
scaleform::stats_inventory_menu::close();
}
}
}

if (!a_event->opening &&
(a_event->menuName == RE::InventoryMenu::MENU_NAME || a_event->menuName == RE::MagicMenu::MENU_NAME)) {
scaleform::stats_inventory_menu::close();
if (!a_event->opening) {
if (a_event->menuName == RE::InventoryMenu::MENU_NAME || a_event->menuName == RE::MagicMenu::MENU_NAME) {
scaleform::stats_inventory_menu::close();
}
}

return event_result::kContinue;
Expand Down
11 changes: 11 additions & 0 deletions src/hook/hook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "hook.h"
#include "hook/menu_control_hook.h"
#include "setting/input_setting.h"

namespace hook {
void hook::install() {
if (setting::input_setting::is_inventory_menu_enabled()) {
menu_control_hook::install();
}
}
} // hook
8 changes: 8 additions & 0 deletions src/hook/hook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace hook {
class hook {
public:
static void install();
};
} // hook
82 changes: 82 additions & 0 deletions src/hook/menu_control_hook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "menu_control_hook.h"
#include "input/menu_key_input_holder.h"
#include "scaleform/menus/stats_inventory_menu.h"
#include "setting/input_setting.h"
#include "util/key_util.h"

namespace hook {

void menu_control_hook::install() {
logger::info("Hooking ..."sv);

REL::Relocation<std::uintptr_t> menu_controls_vtbl{ RE::VTABLE_MenuControls[0] };
process_event_ = menu_controls_vtbl.write_vfunc(0x1, &menu_control_hook::process_event);

logger::info("Hooked."sv);
}

RE::BSEventNotifyControl menu_control_hook::process_event(RE::InputEvent** a_event,
RE::BSTEventSource<RE::InputEvent*>* a_source) {
auto* ui = RE::UI::GetSingleton();

if (a_event && *a_event && is_menu_open(ui)) {
auto* inventory_manager = RE::Inventory3DManager::GetSingleton();
auto* key_input = input::menu_key_input_holder::get_singleton();

for (auto* event = *a_event; event; event = event->next) {
if (inventory_manager && inventory_manager->GetRuntimeData().zoomProgress != 0.f) {
scaleform::stats_inventory_menu::close();
} else if (inventory_manager && inventory_manager->GetRuntimeData().zoomProgress == 0.f &&
!scaleform::stats_inventory_menu::is_menu_open() &&
!ui->IsMenuOpen(RE::BookMenu::MENU_NAME)) {
if (!key_input->get_menu_manual_close() &&
setting::input_setting::auto_open_inventory_menu_inventory() &&
ui->IsMenuOpen(RE::InventoryMenu::MENU_NAME) ||
setting::input_setting::auto_open_inventory_menu_magic() &&
ui->IsMenuOpen(RE::MagicMenu::MENU_NAME)) {
scaleform::stats_inventory_menu::open();
}
}

if (event->eventType != RE::INPUT_EVENT_TYPE::kButton &&
event->eventType != RE::INPUT_EVENT_TYPE::kThumbstick) {
continue;
}

if (event->HasIDCode()) {
auto* button = static_cast<RE::ButtonEvent*>(event);
auto key = button->idCode;
util::key_util::get_key_id(button->device.get(), key);

if (button->IsUp()) {
key_input->remove_key_down(key);
}

if (!button->IsDown()) {
continue;
}

if (key_input->get_open_inventory_key_combo().contains(key) ||
key_input->get_close_inventory_key_combo().contains(key)) {
key_input->add_key_down(key);
}

if (key_input->is_down_list_equal(false) && scaleform::stats_inventory_menu::is_menu_open()) {
scaleform::stats_inventory_menu::close();
key_input->set_menu_manual_close(true);
}
if (key_input->is_down_list_equal(true) && !scaleform::stats_inventory_menu::is_menu_open()) {
scaleform::stats_inventory_menu::open();
key_input->set_menu_manual_close(false);
}
}
}
}

return process_event_(this, a_event, a_source);
}

bool menu_control_hook::is_menu_open(RE::UI*& a_ui) {
return a_ui->IsMenuOpen(RE::InventoryMenu::MENU_NAME) || a_ui->IsMenuOpen(RE::MagicMenu::MENU_NAME);
}
} // hook
19 changes: 19 additions & 0 deletions src/hook/menu_control_hook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

namespace hook {
class menu_control_hook : public RE::MenuControls {
public:
static void install();

private:
RE::BSEventNotifyControl process_event(RE::InputEvent** a_event, RE::BSTEventSource<RE::InputEvent*>* a_source);

using process_event_type =
decltype(static_cast<RE::BSEventNotifyControl (RE::MenuControls::*)(RE::InputEvent* const*,
RE::BSTEventSource<RE::InputEvent*>*)>(&RE::MenuControls::ProcessEvent));

static inline REL::Relocation<process_event_type> process_event_;

static bool is_menu_open(RE::UI*& a_ui);
};
} // hook
62 changes: 62 additions & 0 deletions src/input/menu_key_input_holder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "menu_key_input_holder.h"
#include "setting/input_setting.h"
#include "util/type_util.h"

namespace input {

menu_key_input_holder* menu_key_input_holder::get_singleton() {
static menu_key_input_holder singleton;
return std::addressof(singleton);
}

std::set<uint32_t> menu_key_input_holder::get_open_inventory_key_combo() const { return open_inventory_key_combo_; }

std::set<uint32_t> menu_key_input_holder::get_close_inventory_key_combo() const {
return close_inventory_key_combo_;
}

void menu_key_input_holder::set_all() {
open_inventory_key_combo_ = setting::input_setting::get_open_inventory_menu_key_combination();
close_inventory_key_combo_ = setting::input_setting::get_close_inventory_menu_key_combination();
}

void menu_key_input_holder::add_key_down(uint32_t a_key) {
key_down_list_.insert(a_key);
if (!key_down_list_.empty()) {
logger::trace("size {}, down list {}"sv,
key_down_list_.size(),
util::type_util::get_delimited_string(key_down_list_));
}
}

void menu_key_input_holder::remove_key_down(uint32_t a_key) {
key_down_list_.erase(a_key);
if (!key_down_list_.empty()) {
logger::trace("size {}, down list {}"sv,
key_down_list_.size(),
util::type_util::get_delimited_string(key_down_list_));
}
}

void menu_key_input_holder::set_menu_manual_close(bool a_down) { menu_manual_close = a_down; }

bool menu_key_input_holder::get_menu_manual_close() const { return menu_manual_close; }

bool menu_key_input_holder::is_down_list_equal(bool a_open) {
log_combo_set(a_open);
if (a_open) {
return key_down_list_ == open_inventory_key_combo_;
} else {
return key_down_list_ == close_inventory_key_combo_;
}
}

menu_key_input_holder::menu_key_input_holder() = default;

void menu_key_input_holder::log_combo_set(bool a_open) {
logger::trace("key combo needed {}, down list {}"sv,
util::type_util::get_delimited_string(a_open ? open_inventory_key_combo_ : close_inventory_key_combo_),
util::type_util::get_delimited_string(key_down_list_));
}

} // input
40 changes: 40 additions & 0 deletions src/input/menu_key_input_holder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

namespace input {
class menu_key_input_holder {
public:
[[nodiscard]] static menu_key_input_holder* get_singleton();

[[nodiscard]] std::set<uint32_t> get_open_inventory_key_combo() const;
[[nodiscard]] std::set<uint32_t> get_close_inventory_key_combo() const;

void set_all();

void add_key_down(uint32_t a_key);
void remove_key_down(uint32_t a_key);

bool is_down_list_equal(bool a_open);

void set_menu_manual_close(bool a_down);

[[nodiscard]] bool get_menu_manual_close() const;

menu_key_input_holder(const menu_key_input_holder&) = delete;
menu_key_input_holder(menu_key_input_holder&&) = delete;
menu_key_input_holder& operator=(const menu_key_input_holder&) = delete;
menu_key_input_holder& operator=(menu_key_input_holder&&) = delete;

private:
menu_key_input_holder();
~menu_key_input_holder() = default;

std::set<uint32_t> key_down_list_;

std::set<uint32_t> open_inventory_key_combo_;
std::set<uint32_t> close_inventory_key_combo_;

bool menu_manual_close = false;

void log_combo_set(bool a_open);
};
} // input
Loading

0 comments on commit 767068d

Please sign in to comment.