136 lines
9.5 KiB
C++
136 lines
9.5 KiB
C++
#pragma once
|
|
#include "module.h"
|
|
#include "../game/inventory_tools.h"
|
|
#include "../gui/hud_card.h"
|
|
|
|
class inventory_tools_module : public module {
|
|
public:
|
|
inventory_tools_module():module("inventory tools","steal, dump and middle-click sorting in container screens",category::utility) {
|
|
buttons_=&add_toggle("UI buttons",true);middle_=&add_toggle("middle-click sort",true);
|
|
hotbar_=&add_toggle("include hotbar when dumping",false);
|
|
delay_=&add_int("action delay (ms)",75,30,500);
|
|
x_=&add_float("buttons X (screen fraction)",.5f,0,1);y_=&add_float("buttons Y (screen fraction)",.1f,0,1);
|
|
add_action("stop",[]{inventory_tools::queue(inventory_tools::action::stop);});
|
|
instance_=this;inventory_tools::initialize();
|
|
}
|
|
void on_enable() override {
|
|
if(!inventory_tools::ready){set_enabled(false);notifications::push("Inventory tools unavailable","The container hooks could not be verified.",notifications::kind::warning);return;}
|
|
inventory_tools::enabled=true;input::inventory_handler=&handle_input;
|
|
}
|
|
void on_disable() override {inventory_tools::enabled=false;input::inventory_handler=nullptr;left_owned_=false;middle_owned_=false;right_owned_=false;{std::lock_guard lock(button_mutex_);picker_={};}inventory_tools::reset();}
|
|
void on_render() override {
|
|
inventory_tools::delay=delay_->int_value;inventory_tools::include_hotbar=hotbar_->bool_value;
|
|
button_enabled_=buttons_->bool_value;middle_enabled_=middle_->bool_value;
|
|
const auto state=inventory_tools::latest();
|
|
if(!inventory_tools::fresh(state) || !inventory_tools::playing_ui() || !buttons_->bool_value) {
|
|
std::lock_guard lock(button_mutex_);picker_={};return;
|
|
}
|
|
const auto size=ImGui::GetIO().DisplaySize;
|
|
const ImVec2 pos{std::clamp(size.x*x_->float_value-156.f,0.f,(std::max)(0.f,size.x-312.f)),std::clamp(size.y*y_->float_value,0.f,(std::max)(0.f,size.y-28.f))};
|
|
{std::lock_guard lock(button_mutex_);button_pos_=pos;button_stamp_=GetTickCount64();}
|
|
auto* draw=ImGui::GetForegroundDrawList();
|
|
const bool chest=inventory_tools::find(state,"container_items")!=nullptr;
|
|
const char* labels[]={"steal","dump",state.busy ? "stop" : "sort"};
|
|
for(int i=0;i<3;++i) {
|
|
const ImVec2 at{pos.x+i*104.f,pos.y};
|
|
hud_card::background(draw,at,{100,26},1,true);
|
|
const auto color=theme::u32(i<2 && !chest || state.cursor ? theme::muted() : theme::fg());
|
|
const auto text=fonts::text_size(fonts::semibold,fonts::k_body_size,labels[i]);
|
|
hud_card::text(draw,fonts::semibold,1,{at.x+(100-text.x)/2,at.y+(26-text.y)/2},color,labels[i],true);
|
|
}
|
|
draw_picker(state,pos);
|
|
}
|
|
private:
|
|
setting *buttons_,*middle_,*hotbar_,*delay_,*x_,*y_;
|
|
static inline inventory_tools_module* instance_=nullptr;
|
|
inline static std::mutex button_mutex_;inline static ImVec2 button_pos_;inline static uint64_t button_stamp_=0;
|
|
inline static std::atomic<bool> left_owned_{false},middle_owned_{false},right_owned_{false};
|
|
struct picker_state {
|
|
uintptr_t controller=0;inventory_tools::action operation=inventory_tools::action::steal;
|
|
int scroll=0,visible=0;ImVec2 pos{},size{};uint64_t stamp=0;std::vector<world::item> items;
|
|
};
|
|
inline static picker_state picker_;
|
|
void draw_picker(inventory_tools::snapshot const& state,ImVec2 buttons) {
|
|
std::lock_guard lock(button_mutex_);
|
|
if(!picker_.controller)return;
|
|
if(picker_.controller!=state.controller || state.cursor || !inventory_tools::find(state,"container_items")){picker_={};return;}
|
|
picker_.items=inventory_tools::choices(state,picker_.operation,hotbar_->bool_value);
|
|
const auto screen=ImGui::GetIO().DisplaySize;
|
|
picker_.visible=(std::min)(int(picker_.items.size()),(std::min)(10,(std::max)(1,int((screen.y-64)/22))));
|
|
picker_.scroll=std::clamp(picker_.scroll,0,(std::max)(0,int(picker_.items.size())-picker_.visible));
|
|
picker_.size={264.f,48.f+22.f*(std::max)(1,picker_.visible)};
|
|
const float x=buttons.x+(picker_.operation==inventory_tools::action::dump ? 104.f : 0.f);
|
|
const float y=buttons.y+32+picker_.size.y<=screen.y ? buttons.y+32 : buttons.y-picker_.size.y-6;
|
|
picker_.pos={std::clamp(x,0.f,(std::max)(0.f,screen.x-picker_.size.x)),std::clamp(y,0.f,(std::max)(0.f,screen.y-picker_.size.y))};
|
|
picker_.stamp=GetTickCount64();const auto p=picker_.pos;auto* draw=ImGui::GetForegroundDrawList();
|
|
theme::panel(draw,p,{p.x+picker_.size.x,p.y+picker_.size.y});
|
|
hud_card::text(draw,fonts::semibold,1,{p.x+12,p.y+7},theme::u32(theme::fg()),picker_.operation==inventory_tools::action::steal ? "steal item" : "dump item",true);
|
|
const auto mouse=ImGui::GetIO().MousePos;
|
|
if(picker_.items.empty())hud_card::text(draw,fonts::body,1,{p.x+12,p.y+31},theme::u32(theme::muted()),"no items available",true);
|
|
for(int row=0;row<picker_.visible;++row) {
|
|
const auto& item=picker_.items[picker_.scroll+row];const float y=p.y+28+row*22;
|
|
if(mouse.x>=p.x && mouse.x<p.x+picker_.size.x && mouse.y>=y && mouse.y<y+22)
|
|
theme::control(draw,{p.x+4,y},{p.x+picker_.size.x-4,y+21},false,true);
|
|
const auto name=world::item_label(item.name),count=std::to_string(item.count);
|
|
draw->PushClipRect({p.x+12,y},{p.x+picker_.size.x-58,y+22},true);
|
|
hud_card::text(draw,fonts::body,1,{p.x+12,y+5},theme::u32(theme::fg()),name.c_str(),true);draw->PopClipRect();
|
|
const auto width=fonts::text_size(fonts::body,fonts::k_body_size,count.c_str()).x;
|
|
hud_card::text(draw,fonts::body,1,{p.x+picker_.size.x-12-width,y+5},theme::u32(theme::muted()),count.c_str(),true);
|
|
}
|
|
const char* hint=int(picker_.items.size())>picker_.visible ? "all matching stacks / wheel to scroll" : "transfers all matching stacks";
|
|
hud_card::text(draw,fonts::body,.8f,{p.x+12,p.y+picker_.size.y-16},theme::u32(theme::muted()),hint,true);
|
|
}
|
|
inline static std::atomic<bool> button_enabled_{true},middle_enabled_{true};
|
|
static bool handle_input(HWND,UINT message,WPARAM wp,LPARAM param) {
|
|
if(message==WM_LBUTTONUP && left_owned_){left_owned_=false;return true;}
|
|
if(message==WM_RBUTTONUP && right_owned_){right_owned_=false;return true;}
|
|
if(message==WM_MBUTTONUP && middle_owned_){middle_owned_=false;return true;}
|
|
if(!instance_ || !inventory_tools::playing_ui())return false;
|
|
const auto state=inventory_tools::latest();if(!inventory_tools::fresh(state))return false;
|
|
if((message==WM_MBUTTONDOWN || message==WM_MBUTTONDBLCLK) && middle_enabled_ && inventory_tools::find(state,state.hover) && state.hover!="hotbar_items") {
|
|
inventory_tools::queue(inventory_tools::action::sort);middle_owned_=true;return true;
|
|
}
|
|
const bool left=message==WM_LBUTTONDOWN || message==WM_LBUTTONDBLCLK;
|
|
const bool right=message==WM_RBUTTONDOWN || message==WM_RBUTTONDBLCLK;
|
|
std::lock_guard lock(button_mutex_);
|
|
if(picker_.controller && (picker_.controller!=state.controller || !button_enabled_))picker_={};
|
|
if(picker_.controller && (message==WM_KEYDOWN || message==WM_SYSKEYDOWN) && wp==VK_ESCAPE){picker_={};return true;}
|
|
if(picker_.controller && message==WM_MOUSEWHEEL) {
|
|
const int steps=short(HIWORD(wp))/WHEEL_DELTA;
|
|
picker_.scroll=std::clamp(picker_.scroll-steps,0,(std::max)(0,int(picker_.items.size())-picker_.visible));return true;
|
|
}
|
|
if(!left && !right)return false;
|
|
if(!button_enabled_){if(state.busy)inventory_tools::queue(inventory_tools::action::stop);return false;}
|
|
const int x=short(LOWORD(param)),y=short(HIWORD(param));const auto pos=button_pos_;
|
|
const bool button_hit=GetTickCount64()-button_stamp_<=150 && x>=pos.x && x<pos.x+308 && y>=pos.y && y<pos.y+26 && int(x-pos.x)%104<100;
|
|
if(picker_.controller) {
|
|
const auto p=picker_.pos;
|
|
if(left && GetTickCount64()-picker_.stamp<=150 && x>=p.x && x<p.x+picker_.size.x && y>=p.y+28 && y<p.y+28+picker_.visible*22) {
|
|
const int selected=picker_.scroll+int((y-p.y-28)/22);
|
|
if(!state.cursor)inventory_tools::queue(picker_.operation,"",picker_.items[selected].name,picker_.controller);
|
|
picker_={};left_owned_=true;return true;
|
|
}
|
|
picker_={};
|
|
if(!right || !button_hit){if(left)left_owned_=true;else right_owned_=true;return true;}
|
|
}
|
|
if(!button_hit){if(left && state.busy)inventory_tools::queue(inventory_tools::action::stop);return false;}
|
|
const int button=int((x-pos.x)/104);
|
|
if(right) {
|
|
if(button==2)return false;
|
|
right_owned_=true;
|
|
if(!state.cursor && inventory_tools::find(state,"container_items")) {
|
|
if(state.busy)inventory_tools::queue(inventory_tools::action::stop);
|
|
picker_.controller=state.controller;picker_.operation=button==0 ? inventory_tools::action::steal : inventory_tools::action::dump;
|
|
}
|
|
return true;
|
|
}
|
|
left_owned_=true;
|
|
if(!state.cursor) {
|
|
if(button==2)inventory_tools::queue(state.busy ? inventory_tools::action::stop : inventory_tools::action::sort,
|
|
inventory_tools::find(state,"container_items") ? "container_items" : "inventory_items");
|
|
else if(inventory_tools::find(state,"container_items"))inventory_tools::queue(button==0 ? inventory_tools::action::steal : inventory_tools::action::dump);
|
|
}
|
|
return true;
|
|
}
|
|
};
|