Skip to content

Commit

Permalink
[wip]
Browse files Browse the repository at this point in the history
* added item count to food/potions
* added position and font size setting to mcm
* the inventory event sink is not the best, but should do for now ...
  • Loading branch information
mlthelama committed Jan 8, 2023
1 parent ae295e8 commit 7b4b0be
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ set(BUILD_TESTS OFF)

project(
LamasTinyHUD
VERSION 1.0.0.3
VERSION 1.0.0.4
LANGUAGES CXX
)

Expand Down
2 changes: 2 additions & 0 deletions cmake/sourcelist.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ set(sources ${sources}
src/PCH.h
src/equip/equip_slot.cpp
src/equip/equip_slot.h
src/event/inventory_event.cpp
src/event/inventory_event.h
src/event/key_manager.cpp
src/event/key_manager.h
src/event/sink_event.cpp
Expand Down
26 changes: 26 additions & 0 deletions mcm/Config/LamasTinyHUD/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,32 @@
"formatString": "{2}",
"sourceType": "ModSettingFloat"
}
},
{
"id": "fSlotCountTextOffset:HudSetting",
"text": "$LamasTinyHUD_HudSetting_SlotCountTextOffset_OptionText",
"type": "slider",
"help": "$LamasTinyHUD_HudSetting_SlotCountTextOffset_InfoText",
"valueOptions": {
"min": 0.0,
"max": 300.0,
"step": 1.0,
"formatString": "{0}",
"sourceType": "ModSettingFloat"
}
},
{
"id": "fSlotCountTextFontSize:HudSetting",
"text": "$LamasTinyHUD_HudSetting_SlotCountTextFontSize_OptionText",
"type": "slider",
"help": "$LamasTinyHUD_HudSetting_SlotCountTextFontSize_InfoText",
"valueOptions": {
"min": 10.0,
"max": 35.0,
"step": 1.0,
"formatString": "{0}",
"sourceType": "ModSettingFloat"
}
}
]
}
Expand Down
4 changes: 3 additions & 1 deletion mcm/Config/LamasTinyHUD/settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ fKeyIconScaleHeight = 0.400000
uIconOpacity = 125
fIconScaleWidth = 0.110000
fIconScaleHeight = 0.110000
uSlotButtonFeedback = 200
uSlotButtonFeedback = 200
fSlotCountTextOffset = 10
fSlotCountTextFontSize = 20
Binary file modified mcm/Interface/Translations/LamasTinyHUD_english.txt
Binary file not shown.
50 changes: 50 additions & 0 deletions src/event/inventory_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "inventory_event.h"

#include "handle/page_handle.h"

namespace event {
inventory_event* inventory_event::get_singleton() {
static inventory_event singleton;
return std::addressof(singleton);
}

void inventory_event::sink() {
if (const auto event = RE::Inventory::GetEventSource(); event) {
event->AddEventSink<RE::Inventory::Event>(get_singleton());
logger::info("Registered {} handler"sv, typeid(RE::Inventory::Event).name());
}
}

inventory_event::event_result inventory_event::ProcessEvent(const RE::Inventory::Event* a_event,
RE::BSTEventSource<RE::Inventory::Event>*) {
if (!a_event) {
return event_result::kContinue;
}

if (!a_event->objRefr->IsPlayer() && !a_event->objRefr->IsPlayerRef()) {
return event_result::kContinue;
}

//TODO i might use something different, as far as testing goes, removing it does not notify
//TODO use something else
//those should include food and potions
if (a_event->entryData && a_event->entryData->object->IsMagicItem()) {
const auto page_handle = handle::page_handle::get_singleton();
for (auto pages = page_handle->get_page(); auto [position, page] : pages) {
for (const auto setting : page->slot_settings) {
if (setting->type == util::selection_type::item && setting->form->formID == a_event->entryData->
object->GetFormID()) {
const auto diff = a_event->newCount - a_event->prevCount;
setting->item_count = setting->item_count + diff;
logger::trace("Name {}, old {}, new {}"sv,
a_event->entryData->GetDisplayName(),
a_event->prevCount,
a_event->newCount);
}
}
}
}

return event_result::kContinue;
}
}
25 changes: 25 additions & 0 deletions src/event/inventory_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

namespace event {
class inventory_event final : public RE::BSTEventSink<RE::Inventory::Event> {
public:
using event_result = RE::BSEventNotifyControl;

[[nodiscard]] static inventory_event* get_singleton();

static void sink();

inventory_event(const inventory_event&) = delete;
inventory_event(inventory_event&&) = delete;

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

protected:
event_result ProcessEvent(const RE::Inventory::Event* a_event, RE::BSTEventSource<RE::Inventory::Event>*) override;

private:
inventory_event() = default;
~inventory_event() override = default;
};
}
2 changes: 1 addition & 1 deletion src/event/key_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace event {
page_setting->fade_setting->action = handle::fade_setting::action::out;
page_setting->fade_setting->alpha = handle::fade_setting::alpha::min;
page_setting->fade_setting->current_alpha = static_cast<uint32_t>(handle::fade_setting::alpha::max);
logger::trace("done settinging fade for position {}"sv, static_cast<uint32_t>(page_setting->pos));
logger::trace("done setting fade for position {}"sv, static_cast<uint32_t>(page_setting->pos));
}*/

if (button->IsDown() && (key_ == key_top_action_ || key_ == key_right_action_ || key_ == key_bottom_action_
Expand Down
3 changes: 3 additions & 0 deletions src/event/sink_event.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "sink_event.h"

#include "inventory_event.h"
#include "key_manager.h"

namespace event {
void sink_events() {
key_manager::sink();
inventory_event::sink();

logger::info("added all sinks.");
}
Expand Down
1 change: 1 addition & 0 deletions src/handle/page/slot_setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ namespace handle {
acton_type action = acton_type::default_action;
hand_equip equip = hand_equip::total;
RE::BGSEquipSlot* equip_slot = nullptr;
uint32_t item_count = 0;
};
}
22 changes: 20 additions & 2 deletions src/handle/page_handle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "page_handle.h"
#include "equip/equip_slot.h"
#include "item/inventory.h"
#include "util/string_util.h"

namespace handle {
Expand Down Expand Up @@ -51,6 +52,7 @@ namespace handle {
slot->equip = a_hand;
RE::BGSEquipSlot* equip_slot = nullptr;
get_equip_slots(element->type, a_hand, equip_slot, element->left);
get_item_count(element->form, slot->item_count, element->type);
slot->equip_slot = equip_slot;

slots->push_back(slot);
Expand All @@ -73,7 +75,7 @@ namespace handle {

page->offset_setting = offset;

//TODO for now
//TODO for now the right hand or the first setting defines the icon
page->icon_type = get_icon_type(slots->front()->type, slots->front()->form);
page->icon_opacity = a_opacity;

Expand Down Expand Up @@ -203,7 +205,7 @@ namespace handle {
if (actor_value == RE::ActorValue::kNone) {
actor_value = effect->data.primaryAV;
}

switch (actor_value) {
case RE::ActorValue::kAlteration:
case RE::ActorValue::kConjuration:
Expand Down Expand Up @@ -263,4 +265,20 @@ namespace handle {
a_icon = ui::icon_image_type::potion_default;
}
}

void page_handle::get_item_count(RE::TESForm*& a_form, uint32_t& a_count, const util::selection_type a_type) {
if (a_type != util::selection_type::item) {
a_count = 0;
return;
}
auto player = RE::PlayerCharacter::GetSingleton();
for (auto potential_items = item::inventory::get_inventory_magic_items(player);
const auto& [item, invData] : potential_items) {
if (invData.second->object->formID == a_form->formID) {
a_count = invData.first;
break;
}
}
logger::trace("Item {}, count {}"sv, a_form->GetName(), a_count);
}
}
1 change: 1 addition & 0 deletions src/handle/page_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace handle {
static void get_icon_for_weapon_type(RE::TESForm*& a_form, ui::icon_image_type& a_icon);
static void get_icon_for_spell(RE::TESForm*& a_form, ui::icon_image_type& a_icon);
static void get_icon_for_item(RE::TESForm*& a_form, ui::icon_image_type& a_icon);
static void get_item_count(RE::TESForm*& a_form, uint32_t& a_count, util::selection_type a_type);

struct page_handle_data {
std::map<page_setting::position, page_setting*> page_settings;
Expand Down
6 changes: 3 additions & 3 deletions src/handle/setting_execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace handle {
void setting_execute::execute_settings(const std::vector<slot_setting*>& a_slots) {
logger::trace("got {} settings execute"sv, a_slots.size());
auto player = RE::PlayerCharacter::GetSingleton();
for (const auto slot : a_slots) {
for (auto slot : a_slots) {
logger::trace("executing setting for type {}, action {}, form {} ..."sv,
static_cast<uint32_t>(slot->type),
static_cast<uint32_t>(slot->action),
Expand Down Expand Up @@ -57,13 +57,13 @@ namespace handle {
}
}

void setting_execute::execute_setting(const slot_setting* a_slot, RE::PlayerCharacter*& a_player) {
void setting_execute::execute_setting(slot_setting*& a_slot, RE::PlayerCharacter*& a_player) {
switch (a_slot->type) {
case util::selection_type::unset:
logger::warn("nothing to do, nothing set"sv);
break;
case util::selection_type::item:
item::potion::consume_potion(a_slot->form, a_player);
item::potion::consume_potion(a_slot, a_player);
break;
case util::selection_type::magic:
magic::spell::cast_magic(a_slot->form, a_slot->action, a_slot->equip_slot, a_player);
Expand Down
2 changes: 1 addition & 1 deletion src/handle/setting_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ namespace handle {
RE::ActorEquipManager*& a_actor_equip_manager);

private:
static void execute_setting(const slot_setting* a_slot, RE::PlayerCharacter*& a_player);
static void execute_setting(slot_setting*& a_slot, RE::PlayerCharacter*& a_player);
};
}
10 changes: 6 additions & 4 deletions src/item/potion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#include "inventory.h"

namespace item {
void potion::consume_potion(const RE::TESForm* a_form, RE::PlayerCharacter*& a_player) {
logger::trace("try to consume {}"sv, a_form->GetName());
void potion::consume_potion(handle::slot_setting*& a_slot, RE::PlayerCharacter*& a_player) {
logger::trace("try to consume {}"sv, a_slot->form->GetName());

RE::TESBoundObject* obj = nullptr;
RE::InventoryEntryData inv_data;
uint32_t left;
for (auto potential_items = inventory::get_inventory_magic_items(a_player);
const auto& [item, invData] : potential_items) {
if (invData.second->object->formID == a_form->formID) {
if (invData.second->object->formID == a_slot->form->formID) {
obj = item;
inv_data = *invData.second;
left = invData.first;
Expand All @@ -27,9 +27,10 @@ namespace item {


if (obj->As<RE::MagicItem>()->IsFood()) {
logger::trace("trying to equip/eat a food item {}"sv, obj->GetName());
logger::trace("trying to equip/eat a food item {}, count left {}"sv, obj->GetName(), left);
const auto equip_manager = RE::ActorEquipManager::GetSingleton();
equip_manager->EquipObject(a_player, obj);
a_slot->item_count = left -1;
logger::trace("equipped/ate a food item {}"sv, obj->GetName());
return;
}
Expand All @@ -42,6 +43,7 @@ namespace item {

//build a "cache" with formid and count, validate after consumption
a_player->DrinkPotion(alchemy_potion, inv_data.extraLists->front());
a_slot->item_count = left -1;
logger::trace("drank potion {}. return."sv, alchemy_potion->GetName());
}
}
3 changes: 2 additions & 1 deletion src/item/potion.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once
#include "handle/page/slot_setting.h"

namespace item {
class potion {
public:
static void consume_potion(const RE::TESForm* a_form, RE::PlayerCharacter*& a_player);
static void consume_potion(handle::slot_setting*& a_slot, RE::PlayerCharacter*& a_player);
};
}
10 changes: 8 additions & 2 deletions src/setting/mcm_setting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ namespace config {
static float hud_image_position_height;
static float hud_slot_position_offset;
static float hud_key_position_offset;

static float icon_scale_width;
static float icon_scale_height;
static uint32_t icon_opacity;
static uint32_t slot_button_feedback;

static float key_icon_scale_width;
static float key_icon_scale_height;
static float slot_count_text_offset;
static float slot_count_text_font_size;

void mcm_setting::read_setting() {
logger::info("reading mcm ini files");
Expand Down Expand Up @@ -138,6 +138,10 @@ namespace config {
slot_button_feedback = static_cast<uint32_t>(mcm.GetLongValue("HudSetting", "uSlotButtonFeedback", 200));
key_icon_scale_width = static_cast<float>(mcm.GetDoubleValue("HudSetting", "fKeyIconScaleWidth", 0.4));
key_icon_scale_height = static_cast<float>(mcm.GetDoubleValue("HudSetting", "fKeyIconScaleHeight", 0.4));
slot_count_text_offset = static_cast<float>(mcm.GetDoubleValue("HudSetting", "fSlotCountTextOffset", 10));
slot_count_text_font_size = static_cast<float>(mcm.GetDoubleValue("HudSetting",
"fSlotCountTextFontSize",
20));
};

read_mcm(mcm_default_setting);
Expand Down Expand Up @@ -196,4 +200,6 @@ namespace config {
uint32_t mcm_setting::get_slot_button_feedback() { return slot_button_feedback; }
float mcm_setting::get_key_icon_scale_width() { return key_icon_scale_width; }
float mcm_setting::get_key_icon_scale_height() { return key_icon_scale_height; }
float mcm_setting::get_slot_count_text_offset() { return slot_count_text_offset; }
float mcm_setting::get_slot_count_text_font_size() { return slot_count_text_font_size; }
}
2 changes: 2 additions & 0 deletions src/setting/mcm_setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ namespace config {
static uint32_t get_slot_button_feedback();
static float get_key_icon_scale_width();
static float get_key_icon_scale_height();
static float get_slot_count_text_offset();
static float get_slot_count_text_font_size();
};
}
Loading

0 comments on commit 7b4b0be

Please sign in to comment.