summaryrefslogtreecommitdiff
path: root/cheat/internal_rewrite/ui_dropdown.h
diff options
context:
space:
mode:
authorboris <wzn@moneybot.cc>2018-11-28 16:00:02 +1300
committerboris <wzn@moneybot.cc>2018-11-28 16:00:02 +1300
commit3d412a4b30a9f7c7f51ea6562e694315948bd3da (patch)
tree26d67dfd1f3e5fd12903ad13e85d0cb8bcf8f21c /cheat/internal_rewrite/ui_dropdown.h
parente4729e4393d90271a3814c7a79950a660c48325a (diff)
cleaned up
in short, the cheat and loader are now separate solutions. unused stuff was moved into the legacy solution in case anyone wants to compile it or whatever. i can change this back if you want to. also, i configured the loader to compile in x64, and have separate build types for linux and win64
Diffstat (limited to 'cheat/internal_rewrite/ui_dropdown.h')
-rw-r--r--cheat/internal_rewrite/ui_dropdown.h219
1 files changed, 219 insertions, 0 deletions
diff --git a/cheat/internal_rewrite/ui_dropdown.h b/cheat/internal_rewrite/ui_dropdown.h
new file mode 100644
index 0000000..12a9e98
--- /dev/null
+++ b/cheat/internal_rewrite/ui_dropdown.h
@@ -0,0 +1,219 @@
+#pragma once
+#include "ui_dropdown_item.h"
+
+namespace ui
+{
+ template < typename t = int >
+ class c_dropdown : public base_item {
+ public:
+ c_dropdown( int x, int y, int w, const char* name, t* setting,
+ std::vector< dropdowns::dropdown_item_t< t > >* items, size_t max_items = 8 ) :
+ base_item( x, y, w, 16, name ), m_dropdown_items( items ), m_setting( setting ),
+ m_max_items( max_items ) { }
+
+ virtual bool is_hovered( ) override {
+ if ( m_disabled ) return false;
+
+ int x = get_relative_x( );
+ int y = get_relative_y( ) + 12;
+ int h = m_height;
+
+ int mouse_x, mouse_y;
+ ui_get_cursor_pos( mouse_x, mouse_y );
+
+
+ return mouse_x >= x && mouse_x <= x + m_width
+ && mouse_y >= y && mouse_y <= y + h;
+ }
+
+ inline bool is_any_item_hovered( ) {
+ if ( m_disabled || !m_active ) return false;
+
+ int x = get_relative_x( );
+ int y = get_relative_y( ) + m_height + 12;
+ int h = m_height * ( std::min< size_t >(
+ m_dropdown_items->size( ), m_max_items ) );
+
+ int mouse_x, mouse_y;
+ ui_get_cursor_pos( mouse_x, mouse_y );
+
+
+ return mouse_x >= x && mouse_x <= x + m_width
+ && mouse_y >= y && mouse_y <= y + h;
+ }
+
+ virtual int get_total_height( ) const override {
+ return m_height + 13;
+ }
+
+ void draw_box( const int& x, const int& y, const char* str, bool hovered = false ) {
+ ui_draw_rect( x, y, m_width, m_height, hovered ? ui_get_disabled_col( ) : ui_get_bg_col( ) );
+ ui_draw_line( x, y + m_height, x + m_width, y + m_height, ui_get_accent_col( ) );
+
+ ui_draw_string( x + m_width / 2, y + 2, true, ui_get_text_col( ), str );
+ }
+
+ void update_value( ) {
+ for ( auto& it : *m_dropdown_items ) {
+ if ( it.m_value == *m_setting ) {
+ m_selected_item = &it;
+ }
+ }
+ }
+
+ void draw_items( const int& x, const int& y ) {
+ auto& items = *m_dropdown_items;
+ auto it = &items.front( );
+ int offset = m_height + 1;
+ int hovered = 0;
+ int mouse_x, mouse_y;
+ ui_get_cursor_pos( mouse_x, mouse_y );
+
+ auto is_hovered = [ & ] ( int y_offset ) {
+ return mouse_x >= x && mouse_x <= x + m_width
+ && mouse_y >= y + y_offset && mouse_y <= y + y_offset + m_height;
+ };
+
+
+ for ( size_t i = items.size( ) > m_max_items ? m_curr_scroll : 0;
+ i < std::min< size_t >( m_dropdown_items->size( ), m_max_items + m_curr_scroll );
+ ++i, offset += m_height + 1
+ ) {
+ it = &items.at( i );
+
+ draw_box( x, y + offset, it->m_name );
+
+ if ( is_hovered( offset ) ) {
+ hovered = offset;
+ if ( g_input.is_key_pressed( KEYS_MOUSE1 ) ) {
+ m_selected_item = it;
+ *m_setting = it->m_value;
+ m_active = false;
+ m_has_clicked_item = true;
+ }
+ }
+ }
+
+ if ( hovered ) {
+ ui_draw_outlined_rect( x - 1, y - 1 + hovered,
+ m_width + 1, m_height + 1, ui_get_text_col( ) );
+ }
+ }
+
+ void input( ) {
+ static float enable_time{ };
+ static bool needs_reenable{ };
+ bool active_backup = m_active;
+ bool active_changed = false;
+
+ if ( is_hovered( ) && g_input.is_key_pressed( KEYS_MOUSE1 ) ) {
+ if ( !m_mouse_held ) {
+ m_active = !m_active;
+ }
+ m_mouse_held = true;
+ }
+ else if ( !is_any_item_hovered( ) ) {
+ m_mouse_held = false;
+ }
+
+ if ( !is_hovered( ) && g_input.is_key_pressed( KEYS_MOUSE1 ) && !is_any_item_hovered( ) ) {
+ m_active = false;
+ }
+
+ if ( GetTickCount( ) * 0.001f > enable_time && needs_reenable) {
+ set_disabled_callbacks( false );
+ needs_reenable = false;
+ }
+
+
+ active_changed = m_active != active_backup;
+ //disable input on all items
+ if ( active_changed || m_has_clicked_item ) {
+ if ( !m_active ) {
+ enable_time = GetTickCount( ) * 0.001f + 0.220f;
+ needs_reenable = true;
+ }
+ else {
+ set_disabled_callbacks( true );
+ }
+
+ m_has_clicked_item = false;
+ }
+
+ if ( m_selected_item ) {
+ *m_setting = m_selected_item->m_value;
+ }
+
+ if ( m_active && m_dropdown_items->size( ) > m_max_items ) {
+ int scroll_input = g_input.get_scroll_state( );
+
+ if ( m_curr_scroll > 0 || scroll_input < 0 ) //we dont want scroll to loop around from 0 to max
+ m_curr_scroll -= scroll_input; //because positive is scroll up, we gotta flip it
+
+ if ( m_curr_scroll > m_dropdown_items->size( ) - m_max_items )
+ m_curr_scroll = m_dropdown_items->size( ) - m_max_items;
+ }
+ }
+
+ virtual void render( ) override {
+ int x = get_relative_x( );
+ int y = get_relative_y( );
+
+ bool restore = false;
+ RECT prev_rect{ };
+
+ if ( m_active ) {
+ restore = true;
+ g_d3d.get_device( )->GetScissorRect( &prev_rect );
+
+ RECT new_rect{
+ prev_rect.left,
+ prev_rect.top,
+ g_d3d.m_width,
+ g_d3d.m_height,
+ };
+
+ g_d3d.get_device( )->SetScissorRect( &new_rect );
+
+ draw_items( x, y + 11 );
+
+
+ //draw scrollbar
+ size_t total_items = m_dropdown_items->size( );
+ if ( total_items > m_max_items ) {
+ const size_t height = ( m_height + 1 ) * m_max_items;
+ const float slider_step = ( float )( height ) / float( total_items - m_max_items + 1 );
+
+ size_t slider_pos = static_cast< size_t >( slider_step * m_curr_scroll );
+ ui_draw_rect( x + m_width - 1, y + slider_pos + m_height + 13, 2, ( int )slider_step, ui_get_accent_col( ) );
+ }
+ }
+
+ update_value( );
+ input( );
+
+ ui_draw_string( x + 2, y, false, ui_get_text_col( ), m_text );
+ ui_draw_rect( x, y + 13, m_width, m_height, ui_get_disabled_col( ) );
+ ui_draw_outlined_rect( x - 1, y + 12, m_width + 1, m_height + 1,
+ is_hovered( ) || m_active ? ui_get_text_col( ) : ui_get_accent_col( ) );
+
+ if ( m_selected_item ) {
+ ui_draw_string( x + m_width / 2, y + 14, true, ui_get_text_col( ), m_selected_item->m_name );
+ }
+
+ if( restore ) {
+ g_d3d.get_device( )->SetScissorRect( &prev_rect );
+ }
+ }
+
+ protected:
+ std::vector< dropdowns::dropdown_item_t< t > >* m_dropdown_items{ };
+ dropdowns::dropdown_item_t< t >* m_selected_item{ };
+ bool m_active = false;
+ bool m_mouse_held = false;
+ bool m_has_clicked_item = true;
+ t* m_setting{ };
+ size_t m_max_items{ };
+ size_t m_curr_scroll{ };
+ };
+} \ No newline at end of file