Advertisement
Guest User

[IMGUI] Spetskantroy Listsz

a guest
Aug 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. void Spectators::draw( ) {
  2. if ( !g_game.engine( )->is_in_game( ) || !g_eva.view_entity( ) )
  3. return;
  4.  
  5. // view entity's name
  6. std::string view_name( g_eva.view_info( ).m_name );
  7. if ( view_name.empty( ) )
  8. return;
  9.  
  10. // time to reach min / max alpha
  11. constexpr float opacity_fade_time = 255.f / 0.3f;
  12.  
  13. // update observed state
  14. m_observed = ( m_spectator_count > 0 );
  15.  
  16. // fade in/out
  17. if ( m_observed && m_spectator_text_opacity < 255 )
  18. m_spectator_text_opacity += opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
  19. else if ( !m_observed && m_spectator_text_opacity > 0 )
  20. m_spectator_text_opacity -= opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
  21.  
  22. // need to clamp to be sure value is within range
  23. math::clamp( m_spectator_text_opacity, 0, 255 );
  24.  
  25. // draw the "spectators" text if the alpha is greater than 0
  26. if ( m_spectator_text_opacity )
  27. g_draw.m_fonts->console( ).draw( "spectators", 8, g_draw.height( ) / 2, Color::white( m_spectator_text_opacity ) );
  28.  
  29. // iterate through all player entities
  30. auto y = g_draw.height( ) / 2 + g_draw.m_fonts->console( ).height( ) + 2;
  31. for ( int i = 1; i < g_game.globals( )->m_max_clients; ++i ) {
  32. auto player = ( csgo::C_BasePlayer * )g_game.entity_list( )->get_client_entity( i );
  33. if ( !player || player->alive( ) )
  34. continue;
  35.  
  36. // ref to this entity's alpha in speclist
  37. auto &opacity = m_opacities[ i ];
  38.  
  39. // get this player's observe target - make sure they're spectating our view entity
  40. auto observed = ( csgo::C_BasePlayer * )g_game.entity_list( )->get_client_entity_from_handle( player->m_hObserverTarget( ) );
  41.  
  42. // is the observe target our view entity ?
  43. bool observing_view_entity = observed ? ( observed == g_eva.view_entity( ) ) : false;
  44.  
  45. // fade in / out
  46. if ( observing_view_entity && opacity < 255 ) {
  47. opacity += opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
  48. m_spectator_count++;
  49. } else if ( !observing_view_entity && opacity > 0 ) {
  50. opacity -= opacity_fade_time * std::max( g_game.globals( )->m_frametime, 0.01f );
  51. m_spectator_count--;
  52. }
  53.  
  54. // clamp opacity to make sure alpha is within range
  55. math::clamp( opacity, 0, 255 );
  56.  
  57. // opacity at 0, don't draw
  58. if ( !opacity )
  59. continue;
  60.  
  61. // get this observing player's name
  62. csgo::IVEngineClient::player_info_t spectator_info;
  63. if ( !g_game.engine( )->get_player_info( i, &spectator_info ) )
  64. continue;
  65.  
  66. std::string spec_name( spectator_info.m_name );
  67. spec_name.append( " -> " );
  68.  
  69. // get the size of the line we're going to render
  70. auto size = g_draw.m_fonts->console( ).size( spec_name );
  71.  
  72. // render the text - choose colour based on team
  73. auto team = player->m_iTeamNum( );
  74. g_draw.m_fonts->console( ).draw( spec_name, 16, y, ( team == csgo::TEAM_COUNTER_TERRORIST ) ? Color::light_blue( opacity ) : ( team == csgo::TEAM_TERRORIST ) ? Color::orange( opacity ) : Color::white( opacity ) );
  75.  
  76. // draw our name
  77. g_draw.m_fonts->console( ).draw( view_name, 16 + size.m_width, y, Color::[censored]_green( opacity ) );
  78.  
  79. // increment value of y by the height of the drawn string
  80. y += size.m_height + 2;
  81. }
  82. }
  83.  
  84. void Spectators::register_listeners( ) {
  85. m_round_start = std::make_shared<csgo::IStandardEventListener>( [&]( csgo::IGameEvent *event ) {
  86. // reset spectator count
  87. m_spectator_count = 0;
  88. m_spectator_text_opacity = 0;
  89. } );
  90.  
  91. m_player_death = std::make_shared<csgo::IStandardEventListener>( [&]( csgo::IGameEvent *event ) {
  92. if ( !g_eva.view_entity( ) )
  93. return;
  94.  
  95. // make sure the view entity is the dying client
  96. auto userid = g_game.engine( )->get_player_for_user_id( event->get_int( "userid" ) );
  97. if ( userid != g_game.client_state( )->m_view_entity )
  98. return;
  99.  
  100. // reset spectator count
  101. m_spectator_count = 0;
  102. m_spectator_text_opacity = 0;
  103. } );
  104.  
  105. g_eva.register_listener( m_round_start, "round_start" );
  106. g_eva.register_listener( m_player_death, "player_death" );
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement