#include "externals.hh" #include "utilities.hh" void disableButton( ) { ImGui::PushStyleColor( ImGuiCol_Button , ImColor( .3f , .3f , .3f ) ); ImGui::PushStyleColor( ImGuiCol_ButtonHovered , ImColor( .3f , .3f , .3f ) ); ImGui::PushStyleColor( ImGuiCol_ButtonActive , ImColor( .3f , .3f , .3f ) ); } /*----------------------------------------------------------------------------*/ void updateAngle( __rw__ float& initial , __rd__ const float delta ) { initial = fmod( initial + delta + 540 , 360 ) - 180; } void anglesToMatrix( __rd__ float const* angles , __wr__ float* matrix ) { float c[3] , s[3]; for ( int i = 0 ; i < 3 ; i ++ ) { const float a = M_PI * angles[ i ] / 180; c[i] = cos( a ); s[i] = sin( a ); } matrix[0] = c[1]*c[2]; matrix[1] = s[0]*s[1]*c[2] - c[0]*s[2]; matrix[2] = s[0]*s[2] + c[0]*s[1]*c[2]; matrix[3] = c[1]*s[2]; matrix[4] = c[0]*c[2] + s[0]*s[1]*s[2]; matrix[5] = c[0]*s[1]*s[2] - s[0]*c[2]; matrix[6] = -s[1]; matrix[7] = s[0]*c[1]; matrix[8] = c[0]*c[1]; } /*= T_FilesWatcher ===========================================================*/ T_FilesWatcher::T_FilesWatcher( ) : fd( inotify_init1( O_NONBLOCK ) ) { } T_FilesWatcher::T_FilesWatcher( T_FilesWatcher&& other ) noexcept : fd( 0 ) , watched( std::move( other.watched ) ) { std::swap( fd , other.fd ); other.watched.clear( ); for ( T_WatchedFiles* wf : watched ) { if ( wf ) { wf->watcher = this; } } } T_FilesWatcher::~T_FilesWatcher( ) { if ( fd ) { close( fd ); } for ( T_WatchedFiles* wf : watched ) { if ( wf ) { wf->watcher = nullptr; } } } void T_FilesWatcher::check( ) { for ( T_WatchedFiles* wf : watched ) { if ( wf ) { wf->triggered = false; } } inotify_event ie; while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) { if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) { continue; } for ( T_WatchedFiles* wf : watched ) { if ( !wf || wf->triggered ) { continue; } auto const& idl( wf->identifiers ); if ( find( idl , ie.wd ) != idl.end( ) ) { wf->triggered = true; wf->callback( ); } } } } /*= T_WatchedFiles ===========================================================*/ T_WatchedFiles::T_WatchedFiles( T_WatchedFiles&& other ) noexcept : watcher( other.watcher ) , callback( other.callback ) , triggered( other.triggered ) , identifiers( std::move( other.identifiers ) ) { if ( watcher ) { other.watcher = nullptr; *( find( watcher->watched , &other ) ) = this; } } T_WatchedFiles::T_WatchedFiles( __rw__ T_FilesWatcher& watcher , __rd__ const F_OnFileChanges callback ) : watcher( &watcher ) , callback( callback ) , triggered( false ) { watcher.watched.push_back( this ); } T_WatchedFiles::~T_WatchedFiles( ) { clear( ); if ( watcher ) { watcher->watched.erase( find( watcher->watched , this ) ); } } void T_WatchedFiles::clear( ) { if ( watcher ) { const auto fd( watcher->fd ); for ( int wd : identifiers ) { inotify_rm_watch( fd , wd ); } } identifiers.clear( ); } bool T_WatchedFiles::watch( __rd__ std::string const& file ) { static constexpr auto inFlags( IN_CLOSE_WRITE | IN_DELETE_SELF ); if ( watcher ) { const auto wd( inotify_add_watch( watcher->fd , file.c_str( ) , inFlags ) ); if ( wd == -1 ) { return false; } if ( find( identifiers , wd ) == identifiers.end( ) ) { identifiers.push_back( wd ); } return true; } return false; } /*= T_Camera =================================================================*/ void T_Camera::handleDND( __rd__ ImVec2 const& move , __rd__ const bool hasCtrl , __rd__ const bool hasShift , __rd__ const bool lmb // Left mouse button ) { if ( move.x == 0 || move.y == 0 ) { return; } const float fdx( move.x * .1f * ( hasCtrl ? 1.f : .1f ) ); const float fdy( move.y * .1f * ( hasCtrl ? 1.f : .1f ) ); if ( lmb && hasShift ) { // Left mouse button, shift - move camera const auto side( glm::normalize( glm::cross( up , dir ) ) ); lookAt += .01f * ( side * fdx + up * fdy ); } else if ( lmb ) { // Left mouse button, no shift - change yaw/pitch updateAngle( angles.y , fdx ); updateAngle( angles.x , fdy ); } else { // Right mouse button - change roll updateAngle( angles.z , fdx ); } update( ); } void T_Camera::handleWheel( __rd__ const float wheel , __rd__ const bool hasCtrl , __rd__ const bool hasShift ) { const float delta( wheel * ( hasCtrl ? 1.f : .1f) ); if ( hasShift ) { fov = std::max( 1.f , std::min( 179.f , fov + delta ) ); } else { distance = std::max( .01f , distance - delta ); } update( ); } /*----------------------------------------------------------------------------*/ void T_Camera::makeUI( ) { if ( !ImGui::CollapsingHeader( "Camera" ) ) { return; } const bool changed[] = { ImGui::DragFloat3( "Look at" , &lookAt.x ) , ImGui::DragFloat( "Distance" , &distance , .1f , .1f , 1e8 , "%.1f" ) , ImGui::DragFloat3( "Angles" , &angles.x , .01f , -180 , 180 ) , ImGui::DragFloat( "FoV" , &fov , .01f , .01f , 179.9f ) }; for ( unsigned i = 0 ; i < sizeof( changed ) / sizeof( bool ) ; i ++ ) { if ( changed[ i ] ) { update( ); break; } } } /*----------------------------------------------------------------------------*/ void T_Camera::update( ) { anglesToMatrix( &angles.x , &rotMat[ 0 ].x ); dir = glm::vec3( 0 , 0 , -distance ) * rotMat; up = glm::vec3( 0 , 1 , 0 ) * rotMat; pos = lookAt - dir; np = 2 * tan( M_PI * ( 180. - fov ) / 360. ); }