Skip to content

Commit

Permalink
completely reworked menu
Browse files Browse the repository at this point in the history
  • Loading branch information
hero622 committed Aug 23, 2024
1 parent 2b4e952 commit 8fbe453
Show file tree
Hide file tree
Showing 21 changed files with 843 additions and 179 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Photon [![CI](https://github.com/hero622/photon/actions/workflows/CI.yml/badge.svg)](https://github.com/hero622/photon/actions/workflows/CI.yml) [![C++](https://img.shields.io/badge/language-C%2B%2B-f34b7d)](https://en.wikipedia.org/wiki/C%2B%2B) [![Portal 2](https://img.shields.io/badge/game-Portal%202-blue)](https://store.steampowered.com/app/620/Portal_2/) [![Platform](https://img.shields.io/badge/platform-Windows%20%26%20Linux-green)](https://en.wikipedia.org/wiki/Cross-platform_software)
# <img src="resource/icon.png" width="23"/> Photon [![CI](https://github.com/hero622/photon/actions/workflows/CI.yml/badge.svg)](https://github.com/hero622/photon/actions/workflows/CI.yml) [![C++](https://img.shields.io/badge/language-C%2B%2B-f34b7d)](https://en.wikipedia.org/wiki/C%2B%2B) [![Portal 2](https://img.shields.io/badge/game-Portal%202-blue)](https://store.steampowered.com/app/620/Portal_2/) [![Platform](https://img.shields.io/badge/platform-Windows%20%26%20Linux-green)](https://en.wikipedia.org/wiki/Cross-platform_software)

**Photon** is a mod loader for Portal 2.

Expand Down
12 changes: 9 additions & 3 deletions example-mod/src/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,27 @@ void c_photon_mod::on_event( const char* msg ) {

photon_api::mod_info_t c_photon_mod::get_info( ) {
photon_api::mod_info_t info;
info.name = "example mod";
info.name = "Example Mod";
info.author = "hero";
info.version = "0.0.1";
return info;
}

void c_photon_mod::paint_menu( ) {
static bool example_checkbox_val;
photon->menu->checkbox( example_checkbox_val, "example checkbox" );
static bool example_toggle_val;
photon->menu->toggle( example_toggle_val, "example toggle" );

static int example_slider_val;
photon->menu->slider( example_slider_val, 0, 100, "example slider" );

static float example_sliderf_val;
photon->menu->sliderf( example_sliderf_val, 0.f, 10.f, "example sliderf" );

static color_t example_colorpicker_val;
photon->menu->colorpicker( example_colorpicker_val, "example colorpicker" );

photon->menu->separator( "example separator" );

static std::size_t example_combo_val;
const char* example_combo_items[] = { "value 1", "value 2", "value 3" };
photon->menu->combo( example_combo_val, example_combo_items, ARRAY_LEN( example_combo_items ), "example combo" );
Expand Down
Binary file added resource/arrow-left-solid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/arrows-up-down-left-right-solid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/gear-solid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/list-solid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
349 changes: 349 additions & 0 deletions resource/resource.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/core/huds/huds.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace huds {
inline int safezone_x = 8;
inline int safezone_y = 8;

inline bool edit = false;

void paint( );
void paint_ui( );
} // namespace huds
377 changes: 263 additions & 114 deletions src/core/menu/framework.cpp

Large diffs are not rendered by default.

42 changes: 32 additions & 10 deletions src/core/menu/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
namespace gui {
namespace framework {
namespace colors {
inline color_t bg = color_t( 0, 0, 0, 200 );
inline color_t black = color_t( 0, 0, 0, 255 );
inline color_t white = color_t( 255, 255, 255, 255 );
inline color_t dark = color_t( 64, 64, 64, 255 );
inline color_t darker = color_t( 32, 32, 32, 255 );
// constant colors
inline color_t white = color_t( 255, 255, 255 );
inline color_t gray = color_t( 64, 64, 64 );
inline color_t green = color_t( 36, 140, 45 );
inline color_t red = color_t( 168, 43, 43 );

// theme colors
inline color_t bg;
inline color_t fg;
inline color_t text;
inline color_t accent;
inline color_t disabled;
} // namespace colors

namespace fonts {
inline h_font smaller;
inline h_font normal;
inline h_font title;
inline h_font icon;
inline h_font bigtitle;
} // namespace fonts

struct menu_t {
Expand Down Expand Up @@ -47,16 +55,30 @@ namespace gui {
};
inline dropdown_t cur_dropdown;

struct colorpicker_t {

};
inline colorpicker_t cur_colorpicker;

inline std::string cur_slider; // FIXME: make this somehow be only for current menu but still persist
inline int scroll_offset; // ditto

void set_theme( bool dark = false );

void begin( vec2_t pos, vec2_t size );
void end( );
bool tab( int& selected, vec2_t pos, vec2_t size, const std::string& title );
bool mod( mods::mod_info_t* info );
bool tab( int& selected, vec2_t pos, vec2_t size, const std::string& label, bool use_texture = false );
bool mod( mods::mod_info_t& info );
void split( int width );
bool icon_button( vec2_t size, const std::string& texture );

bool button( vec2_t size, const std::string& label );
bool checkbox( bool& val, const std::string& label );
bool button( vec2_t size, const std::string& label, bool enabled = true, h_font font = fonts::normal, color_t color = colors::accent );
bool toggle( bool& val, const std::string& label );
void slider( int& val, int min, int max, const std::string& label );
void sliderf( float& val, float min, float max, const std::string& label );
void colorpicker( color_t& val, const std::string& label );
void combo( std::size_t& val, const std::vector< std::string >& items, const std::string& label );
void multicombo( std::size_t& val, const std::vector< std::string >& items, const std::string& label );
void separator( const std::string& label );
} // namespace framework
} // namespace gui
84 changes: 63 additions & 21 deletions src/core/menu/gui.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "gui.h"

#include "../resource/resource.h"
#include "core/convars/convars.h"
#include "core/huds/huds.h"
#include "core/interfaces/interfaces.h"
Expand All @@ -12,7 +13,7 @@
SIGNAL_CALLBACK( void, __rescall, lock_cursor ) {
static void* input_ctx = interfaces::engine_client->get_input_context( 0 );

if ( gui::open ) {
if ( gui::open || huds::edit ) {
interfaces::surface->unlock_cursor( );

interfaces::input_stack_system->set_cursor_visible( input_ctx, true );
Expand All @@ -35,29 +36,37 @@ SIGNAL_CALLBACK( void, __rescall, paint, paint_mode_t, mode ) {

photon->common->post_event( &plugin, "paint" );

if ( photon->input->get_key_press( key_insert ) )
gui::open = !gui::open;
if ( photon->input->get_key_press( key_insert ) ) {
gui::open = !gui::open;
huds::edit = false;
}

if ( photon->input->get_key_press( key_escape ) ) {
gui::open = true;
huds::edit = false;
}

if ( gui::open ) {
if ( gui::open )
gui::paint( );

if ( huds::edit )
huds::paint_ui( );
}
}

interfaces::surface->finish_drawing( );
}

// block input to the game when photon's menu is open, only works in game, not in the menu
SIGNAL_CALLBACK( int, __rescall, in_key_event, int, eventcode, button_code_t, keynum, const char*, current_binding ) {
if ( gui::open )
if ( gui::open || huds::edit )
return 0;

return original( ecx, eventcode, keynum, current_binding );
}

// block input to the menu, vgui has its own input system for some reason, so we have to hook another function
SIGNAL_CALLBACK( void, __rescall, update_button_state, const int*, event ) {
if ( gui::open ) {
if ( gui::open || huds::edit ) {
/*
* so we cant actually just return here because theres other
* functions calling SetKeyCodeState and SetMouseCodeState,
Expand All @@ -74,14 +83,24 @@ SIGNAL_CALLBACK( void, __rescall, update_button_state, const int*, event ) {
}

bool gui::initialize( ) {
framework::set_theme( false );

photon->render->create_font( framework::fonts::smaller, "D-DIN EXP", 16, false, fontflag_antialias );
photon->render->create_font( framework::fonts::normal, "D-DIN EXP", 20, true, fontflag_antialias );
photon->render->create_font( framework::fonts::title, "D-DIN EXP", 24, true, fontflag_antialias );
photon->render->create_font( framework::fonts::bigtitle, "D-DIN EXP", 32, true, fontflag_antialias );

photon->render->load_texture( "photon_icon", resource::icons::photon, 50, 50, sizeof( resource::icons::photon ) );
photon->render->load_texture( "photon_list", resource::icons::list, 32, 32, sizeof( resource::icons::list ) );
photon->render->load_texture( "photon_gear", resource::icons::gear, 32, 32, sizeof( resource::icons::gear ) );
photon->render->load_texture( "photon_left_arrow", resource::icons::left_arrow, 32, 32, sizeof( resource::icons::left_arrow ) );
photon->render->load_texture( "photon_arrows", resource::icons::arrows, 32, 32, sizeof( resource::icons::arrows ) );

photon->signal->get( "lock_cursor" )->add_callback( &lock_cursor_cbk );
photon->signal->get( "paint" )->add_callback( &paint_cbk );
photon->signal->get( "in_key_event" )->add_callback( &in_key_event_cbk );
photon->signal->get( "update_button_state" )->add_callback( &update_button_state_cbk );

photon->render->create_font( framework::fonts::normal, "Segoe UI Light", 22, false, fontflag_antialias );
photon->render->create_font( framework::fonts::title, "Segoe UI Light", 30, false, fontflag_antialias );

dx9::initialize( );

return true;
Expand All @@ -90,41 +109,64 @@ bool gui::initialize( ) {
void gui::uninitialize( ) {
dx9::uninitialize( );

photon->render->destruct_font( framework::fonts::bigtitle );
photon->render->destruct_font( framework::fonts::title );
photon->render->destruct_font( framework::fonts::normal );
photon->render->destruct_font( framework::fonts::smaller );
}

void gui::paint( ) {
const auto screen_size = photon->render->get_screen_size( );
const auto screen_half = screen_size / 2;

const int tab_height = 50;
const auto menu_size = vec2_t( 700, 0.32f * screen_size.y );
const auto menu_pos = vec2_t( screen_size.x / 2 - menu_size.x / 2, screen_size.y / 2 + tab_height / 2 + 8 );
constexpr int tab_height = 56;
const auto menu_size = vec2_t( 740, 0.4f * screen_size.y );
const auto menu_pos = vec2_t( screen_half.x - menu_size.x / 2, screen_half.y + tab_height / 2 + 12 );

static int tab = 1;

static int tab = 1;
static photon_api::i_photon_mod* cur_mod;

// draw title
const auto title_size = photon->render->get_text_size( framework::fonts::bigtitle, "PHOTON" );
photon->render->draw_texture( screen_half.x - title_size.x / 2 - 25 - 4, screen_half.y - 90, 50, 50, "photon_icon" );
photon->render->draw_text( screen_half.x + 25 + 4, screen_half.y - 80, framework::fonts::bigtitle, framework::colors::white, true, "PHOTON" );

framework::begin( menu_pos, menu_size );

if ( framework::tab( tab, vec2_t( screen_size.x / 2 - 90 - 130 - 6, screen_size.y / 2 - tab_height / 2 ), vec2_t( 130, tab_height ), "profiles" ) ) {
if ( framework::tab( tab, { screen_half.x - 100 - tab_height - 8, screen_half.y - tab_height / 2 }, { tab_height, tab_height }, "photon_list", true ) ) {
}
if ( framework::tab( tab, vec2_t( screen_size.x / 2 - 90, screen_size.y / 2 - tab_height / 2 ), vec2_t( 180, tab_height ), "modules" ) ) {
if ( framework::tab( tab, { screen_half.x - 100, screen_half.y - tab_height / 2 }, { 200, tab_height }, "MODS" ) ) {
if ( !cur_mod ) {
for ( auto& mod : mods::mod_list ) {
if ( framework::mod( &mod.second ) )
if ( framework::mod( mod.second ) )
cur_mod = mod.second.ptr;
}
} else {
if ( framework::button( vec2_t( 80, 30 ), "< back" ) )
if ( framework::icon_button( { 56, 56 }, "photon_left_arrow" ) )
cur_mod = nullptr;

if ( cur_mod )
if ( framework::icon_button( { 56, 56 }, "photon_arrows" ) ) {
gui::open = false;
huds::edit = true;
}

framework::split( 56 );

if ( cur_mod ) {
framework::separator( util::ssprintf( "%s settings", cur_mod->get_info( ).name ).c_str( ) );

cur_mod->paint_menu( );
}
}
}
if ( framework::tab( tab, vec2_t( screen_size.x / 2 + 90 + 6, screen_size.y / 2 - tab_height / 2 ), vec2_t( 130, tab_height ), "settings" ) ) {
if ( framework::tab( tab, { screen_half.x + 100 + 8, screen_half.y - tab_height / 2 }, { tab_height, tab_height }, "photon_gear", true ) ) {
static bool dark_mode = false;
if ( framework::toggle( dark_mode, "dark mode" ) )
framework::set_theme( dark_mode );

static bool fast_loads = true;
if ( framework::checkbox( fast_loads, "fast loads" ) )
if ( framework::toggle( fast_loads, "fast loads" ) )
convars::set_fast_loads( fast_loads );

framework::slider( huds::safezone_x, 0, 32, "hud safezone x" );
Expand Down
7 changes: 5 additions & 2 deletions src/core/shared/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
bool c_menu::button( vec2_t size, const char* label ) {
return gui::framework::button( size, label );
}
bool c_menu::checkbox( bool& val, const char* label ) {
return gui::framework::checkbox( val, label );
bool c_menu::toggle( bool& val, const char* label ) {
return gui::framework::toggle( val, label );
}
void c_menu::slider( int& val, int min, int max, const char* label ) {
return gui::framework::slider( val, min, max, label );
Expand All @@ -36,3 +36,6 @@ void c_menu::multicombo( std::size_t& val, const char* items[], std::size_t item

return gui::framework::multicombo( val, items_vector, label );
}
void c_menu::separator( const char* label ) {
return gui::framework::separator( label );
}
Loading

0 comments on commit 8fbe453

Please sign in to comment.