Skip to content

Commit

Permalink
add slider and wip combobox menu component
Browse files Browse the repository at this point in the history
  • Loading branch information
hero622 committed Nov 18, 2023
1 parent 9a42d0f commit d010caf
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 12 deletions.
12 changes: 10 additions & 2 deletions example-mod/src/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ wh_api::wh_mod_info_t *c_wormhole_mod::get_info( ) {
}

void c_wormhole_mod::paint_menu( ) {
static bool example_value;
wh->menu->checkbox( example_value, "example checkbox" );
static bool example_checkbox_val;
wh->menu->checkbox( example_checkbox_val, "example checkbox" );
static int example_slider_val;
wh->menu->slider( example_slider_val, 0, 100, "example slider" );
static float example_sliderf_val;
wh->menu->sliderf( example_sliderf_val, 0.f, 10.f, "example sliderf" );
static int example_combo_val;
static bool example_combo_open;
const char *items[] = { "value 1", "value 2", "value 3" };
wh->menu->combo( example_combo_val, example_combo_open, items, 3, "example combo" );
}
129 changes: 123 additions & 6 deletions src/core/menu/framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "core/mods/mods.h"

#include <algorithm>

void gui::framework::begin( sdk::vec2_t pos, sdk::vec2_t size ) {
cur_menu = menu_t( );

Expand All @@ -18,6 +20,34 @@ void gui::framework::begin( sdk::vec2_t pos, sdk::vec2_t size ) {
}

void gui::framework::end( ) {
if ( cur_menu.cur_combo_items.size( ) ) {
auto cur_pos = cur_menu.pos + cur_menu.cur_combo_pos;

const auto size = sdk::vec2_t( 140, 26 );

for ( const auto &item : cur_menu.cur_combo_items ) {
wh->render->draw_text( cur_pos.x + 8, cur_pos.y + 2, fonts::normal, colors::white, false, item );

wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, size.y, colors::dark );

cur_pos.y += 25;
}

// bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
// bool clicking = hover && wh->input->get_key_press( sdk::mouse_left );

// if ( clicking ) {
// }

// wh->render->draw_text( cur_pos.x, cur_pos.y, fonts::normal, colors::white, false, label );

// cur_pos.y += text_size.y;

// wh->render->draw_text( cur_pos.x + 8, cur_pos.y + 2, fonts::normal, colors::white, false, items[ val ] );

// cur_menu.cursor.y += size.y + text_size.y + 4;
}

wh->portal2->surface->set_clip_rect( 0, 0, wh->render->get_screen_size( ).x, wh->render->get_screen_size( ).y );
wh->portal2->surface->enable_clipping = false;
}
Expand Down Expand Up @@ -128,14 +158,101 @@ bool gui::framework::button( sdk::vec2_t size, std::string label ) {
void gui::framework::checkbox( bool &val, std::string label ) {
const auto cur_pos = cur_menu.pos + cur_menu.cursor;

bool hover = wh->input->is_cursor_in_area( cur_pos.x, cur_pos.y, cur_pos.x + 20, cur_pos.y + 20 );
bool clicking = hover && wh->input->get_key_held( sdk::mouse_left );

wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, 20, 20, val ? colors::white : colors::dark );
wh->render->draw_filled_rect( cur_pos.x + 3, cur_pos.y + 3, 14, 14, val ? colors::white : sdk::color_t( 0, 0, 0, 0 ) );
const auto size = sdk::vec2_t( 20, 20 );

wh->render->draw_text( cur_pos.x + 24, cur_pos.y - 2, fonts::normal, colors::white, false, label );
bool hover = wh->input->is_cursor_in_area( cur_pos.x, cur_pos.y, cur_pos.x + size.x, cur_pos.y + size.y );
bool clicking = hover && wh->input->get_key_held( sdk::mouse_left );

if ( hover && wh->input->get_key_press( sdk::mouse_left ) )
val = !val;

wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, size.y, val ? colors::white : colors::dark );
wh->render->draw_filled_rect( cur_pos.x + 3, cur_pos.y + 3, size.x - 6, size.y - 6, val ? colors::white : sdk::color_t( 0, 0, 0, 0 ) );

wh->render->draw_text( cur_pos.x + size.x + 4, cur_pos.y - 2, fonts::normal, colors::white, false, label );

cur_menu.cursor.y += size.y + 4;
}

void gui::framework::slider( int &val, int min, int max, std::string label ) {
auto cur_pos = cur_menu.pos + cur_menu.cursor;

const auto size = sdk::vec2_t( 140, 20 );
const auto text_size = wh->render->get_text_size( fonts::normal, label );

bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
bool clicking = hover && wh->input->get_key_held( sdk::mouse_left );

float value = ( float ) val / ( max - min );

if ( clicking ) {
value = ( wh->input->get_cursor_position( ).x - ( cur_pos.x + 3 ) ) / ( size.x - 6 );
val = value * ( max - min );
}

wh->render->draw_text( cur_pos.x, cur_pos.y, fonts::normal, colors::white, false, label );

cur_pos.y += text_size.y;

wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, size.y, clicking ? colors::white : colors::dark );
wh->render->draw_filled_rect( cur_pos.x + 3, cur_pos.y + 3, value * ( size.x - 6 ), size.y - 6, colors::white );

wh->render->draw_text( cur_pos.x + size.x + 4, cur_pos.y - 2, fonts::normal, colors::white, false, utils::string::ssprintf( "%d", val ) );

cur_menu.cursor.y += size.y + text_size.y + 4;
}

void gui::framework::sliderf( float &val, float min, float max, std::string label ) {
auto cur_pos = cur_menu.pos + cur_menu.cursor;

const auto size = sdk::vec2_t( 140, 20 );
const auto text_size = wh->render->get_text_size( fonts::normal, label );

bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
bool clicking = hover && wh->input->get_key_held( sdk::mouse_left );

float value = val / ( max - min );

if ( clicking ) {
value = ( wh->input->get_cursor_position( ).x - ( cur_pos.x + 3 ) ) / ( size.x - 6 );
val = value * ( max - min );
}

wh->render->draw_text( cur_pos.x, cur_pos.y, fonts::normal, colors::white, false, label );

cur_pos.y += text_size.y;

wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, size.y, clicking ? colors::white : colors::dark );
wh->render->draw_filled_rect( cur_pos.x + 3, cur_pos.y + 3, value * ( size.x - 6 ), size.y - 6, colors::white );

wh->render->draw_text( cur_pos.x + size.x + 4, cur_pos.y - 2, fonts::normal, colors::white, false, utils::string::ssprintf( "%.1f", val ) );

cur_menu.cursor.y += size.y + text_size.y + 4;
}

void gui::framework::combo( int &val, bool &open, std::vector<std::string> items, std::string label ) {
auto cur_pos = cur_menu.pos + cur_menu.cursor;

const auto size = sdk::vec2_t( 140, 26 );
const auto text_size = wh->render->get_text_size( fonts::normal, label );

bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
bool clicking = hover && wh->input->get_key_press( sdk::mouse_left );

if ( clicking )
open = !open;

if ( open ) {
cur_menu.cur_combo_pos = sdk::vec2_t( cur_menu.cursor.x, cur_menu.cursor.y + size.y + text_size.y );
cur_menu.cur_combo_items = items;
}

wh->render->draw_text( cur_pos.x, cur_pos.y, fonts::normal, colors::white, false, label );

cur_pos.y += text_size.y;

wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, size.y, open ? colors::white : colors::dark );
wh->render->draw_text( cur_pos.x + 8, cur_pos.y + 2, fonts::normal, colors::white, false, items[ val ] );

cur_menu.cursor.y += size.y + text_size.y + 4;
}
6 changes: 6 additions & 0 deletions src/core/menu/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace gui {

int tab_count;
int mod_count;

sdk::vec2_t cur_combo_pos;
std::vector<std::string> cur_combo_items;
};
inline menu_t cur_menu;

Expand All @@ -38,5 +41,8 @@ namespace gui {

bool button( sdk::vec2_t size, std::string label );
void checkbox( bool &val, std::string label );
void slider( int &val, int min, int max, std::string label );
void sliderf( float &val, float min, float max, std::string label );
void combo( int &val, bool &open, std::vector<std::string> items, std::string label );
} // namespace framework
} // namespace gui
18 changes: 15 additions & 3 deletions src/core/shared/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@

#include "core/menu/framework.h"

#include <array>
#include <iostream>
#include <vector>

bool c_menu::button( sdk::vec2_t size, const char *label ) {
return gui::framework::button( size, label );
}
void c_menu::checkbox( bool &val, const char *label ) {
return gui::framework::checkbox( val, label );
}
void c_menu::slider( int &val, int min, int max, const char *label ) {
return gui::framework::slider( val, min, max, label );
}
void c_menu::sliderf( float &val, float min, float max, const char *label ) {
return gui::framework::sliderf( val, min, max, label );
}
void c_menu::colorpicker( sdk::color_t &val, const char *label ) {
void c_menu::colorpicker( sdk::color_t &val, bool &open, const char *label ) {
}
void c_menu::combo( int &val, const char **items, const char *label ) {
void c_menu::combo( int &val, bool &open, const char *items[], int items_count, const char *label ) {
std::vector<std::string> items_vector;
for ( std::size_t i = 0; i < items_count; ++i ) {
items_vector.push_back( items[ i ] );
}

return gui::framework::combo( val, open, items_vector, label );
}
void c_menu::multicombo( int &val, const char **items, const char *label ) {
void c_menu::multicombo( int &val, bool &open, const char *items[], int items_count, const char *label ) {
}
2 changes: 1 addition & 1 deletion src/wormhole-sdk

0 comments on commit d010caf

Please sign in to comment.