summaryrefslogtreecommitdiff
path: root/cheat/tf2/ui_form.h
blob: 07915287c12da3eab1583b9f585f15d2049fab0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma once
#include "ui_base_item.h"

namespace ui
{
	class c_form : public base_item {
	public:
		c_form( int x, int y, int w, int h, const char* name, int max_h = 0, bool collapse = false ) :
			base_item( x, y, w, h, name ), m_dynamic( !h ), m_max_height( max_h ), m_collapse( collapse ) { };

		virtual int x( ) const override {
			return m_x + 10;
		}

		virtual int y( ) const override {
			return m_y + m_scroll_offset + 9;
		}

		virtual bool is_form( ) const override {
			return true;
		}

		virtual bool is_hovered( ) override {
			int mouse_x, mouse_y;
			ui_get_cursor_pos( mouse_x, mouse_y );

			int x = get_relative_x( );
			int y = get_relative_y( );

			return mouse_x > x && mouse_x < x + w( )
				&& mouse_y > y && mouse_y < y + h( );
		}

		bool is_collapse_hovered( ) {
			if( !m_collapse ) return false;

			int mouse_x, mouse_y;
			ui_get_cursor_pos( mouse_x, mouse_y );

			int x = get_relative_x( ) + m_width - 8;
			int y = get_relative_y( ) + 2;

			return mouse_x >= x && mouse_x <= x + 5
				&& mouse_y >= y && mouse_y <= y + 7;
		}

		bool is_collapsed( ) {
			return ( m_collapse && !m_open );
		}

		inline void update_size( ) {
			if ( !m_dynamic ) return;

			int total_height{ ITEM_OFFSET * 2 };
			for ( auto& it : m_items ) {
				if ( it->get_visible( ) ) {
					auto item_height = it->get_total_height( ) + ITEM_OFFSET;
					if( m_max_height && total_height + item_height > m_max_height ) {
						total_height = m_max_height;
						break;
					}
					total_height += it->get_total_height( ) + ITEM_OFFSET;
				}
			}

			m_height = total_height;
		}

		virtual int get_total_height( ) const override {
			if( !m_open && m_collapse ) return 18;
			return m_height + 5;
		}

		int get_total_item_height( ) {
			int total_height{ ITEM_OFFSET * 2 };
			for( auto& it : m_items ) {
				if( it->get_visible( ) ) {
					auto item_height = it->get_total_height( ) + ITEM_OFFSET;
					total_height += it->get_total_height( ) + ITEM_OFFSET;
				}
			}

			return total_height;
		}

		void input( ) {
			if( m_collapse ) {
				bool hovered = is_collapse_hovered( );
				bool clicked = g_input.is_key_pressed( KEYS_MOUSE1 );
				if( hovered && clicked ) {
					if( !m_was_pressed ) {
						m_open ^= 1;
					}
					m_was_pressed = true;
				}
				else {
					m_was_pressed = false;
				}
			}

			if( m_max_height && get_total_item_height( ) > m_max_height ) {
				if( !m_disabled && is_hovered( ) ) {
					auto scroll_state = g_input.get_scroll_state( );
					if( !!scroll_state && m_was_hovered ) {
						scroll_state > 0 ? m_scroll_offset += 13 : m_scroll_offset -= 13;
					}
					m_scroll_offset = std::clamp( m_scroll_offset, -( get_total_item_height( ) - m_height + 3 ), 0 );
				}
			}
			else {
				m_scroll_offset = 0;
			}
		}

		virtual void render( ) override {
			update_size( );
			input( );
			int x = get_relative_x( );
			int y = get_relative_y( );

			int text_w, text_h;
			ui_get_text_size( text_w, text_h, m_text );

			if( m_collapse ) {
				if( !m_open && m_collapse ) {
					ui_draw_outlined_rect( x, y, m_width, 14, ui_get_accent_col( ) );
					ui_draw_line( x + 3, y, x + text_w + 1, y, ui_get_bg_col( ) );
					ui_draw_string( x + 3, y - 7, false, ui_get_text_col( ), m_text );

					int button_x = x + m_width - 6;
					int button_y = y + 2;

					ui_draw_string( button_x, button_y, true,
						is_collapse_hovered( ) ? ui_get_accent_col( ) : ui_get_text_col( ), "+" );
					return;
				}
			}

			for( int i{ }; i < 8; ++i ) {
				clr_t col = ui_get_bg_col( ) * ( 0.72f + i * 0.04f );
				ui_draw_rect( x, y + i, m_width, m_height - i * 2, col );
			}

			ui_draw_outlined_rect( x, y, m_width, m_height, ui_get_accent_col( ) );

			if( m_max_height && get_total_item_height( ) > m_height ) {
				const size_t height = get_total_height( ) - 20;
				const float delta = ( float )( get_total_item_height( ) - height + 1 );
				const float slider_step = ( ( float )( height ) / delta );
				const float slider_height = slider_step * 13.f;

				size_t slider_pos = static_cast< size_t >( slider_step * m_scroll_offset );
				ui_draw_rect( x + m_width - 7, y + 8, 4, height, ui_get_disabled_col( ) );
				ui_draw_rect( x + m_width - 7, y - slider_pos + 8, 4, ( int )slider_height + 1, ui_get_accent_col( ) );
			}

			if( is_hovered( ) != m_was_hovered ) {
				bool backup = m_disabled;
				if( !backup ) {
					set_disabled( !is_hovered( ) );
				}
				m_disabled = backup;
			}

			ui_draw_line( x + 3, y, x + text_w + 1, y, ui_get_bg_col( ) );
			ui_draw_string( x + 3, y - 7, false, ui_get_text_col( ), m_text );
			if( m_collapse ) {
				int button_x = x + m_width - 6;
				int button_y = y + 2;

				ui_draw_string( button_x, button_y, true,
					is_collapse_hovered( ) ? ui_get_accent_col( ) : ui_get_text_col( ), "-" );
			}

			m_was_hovered = is_hovered( );
		}

	protected:
		bool m_dynamic{ };
		bool m_was_hovered{ };
		int m_max_height{ };
		int m_scroll_offset{ };
		bool m_was_pressed{ };
		bool m_collapse{ };
		bool m_open{ true };
	};
}