2018-05-22 12:00:30 +00:00
# include "galaxy_menu.h"
2018-05-23 11:46:02 +00:00
# include <algorithm>
2018-05-22 12:00:30 +00:00
GalaxyMenu : : GalaxyMenu ( )
{
}
GalaxyMenu : : ~ GalaxyMenu ( )
{
}
2018-05-23 11:46:02 +00:00
bool GalaxyMenu : : InitGalaxyMenu ( std : : string config_json , float scale ) {
try
{
/*..Inner Init..*/
menuScale = scale ;
2018-05-22 12:00:30 +00:00
/*...Read ptree from config...*/
2018-05-23 11:46:02 +00:00
std : : string res_path = " ../../../assets/ " ;
boost : : property_tree : : ptree config_pt ;
boost : : property_tree : : json_parser : : read_json ( res_path + config_json , config_pt ) ;
/*..error ptree..*/
boost : : property_tree : : ptree error_pt ;
error_pt . put ( " error " , " something goes wrong at InitGalaxyMenu " ) ;
/*..Init Menu..*/
2018-05-25 11:51:26 +00:00
BOOST_FOREACH ( auto menu_pt , config_pt . get_child ( " Space " , error_pt ) ) {
2018-05-23 11:46:02 +00:00
Galaxy galax ;
galax . name = menu_pt . second . get < std : : string > ( " name " , " error " ) ;
galax . scale = menu_pt . second . get < float > ( " scale " , 0.0f ) ;
galax . texture = menu_pt . second . get < std : : string > ( " texture " , " error " ) ;
galax . position = Eigen : : Vector2f ( menu_pt . second . get < float > ( " position.x_coord " , 0.0f ) , menu_pt . second . get < float > ( " position.y_coord " , 0.0f ) ) ;
//boost::property_tree::ptree stars = menu_pt.second.get<boost::property_tree::ptree>("Stars", error_pt);
BOOST_FOREACH ( auto stars_pt , menu_pt . second . get_child ( " Stars " , error_pt ) ) {
StarObject star ;
star . name = stars_pt . second . get < std : : string > ( " name " , " error " ) ;
star . scale = stars_pt . second . get < float > ( " scale " , 0.0f ) ;
star . texture = stars_pt . second . get < std : : string > ( " texture " , " error " ) ;
star . position = Eigen : : Vector2f ( stars_pt . second . get < float > ( " position.x_coord " , 0.0f ) , stars_pt . second . get < float > ( " position.y_coord " , 0.0f ) ) ;
/*..strings replace by objects..*/
BOOST_FOREACH ( auto levels_pt , stars_pt . second . get_child ( " levels " ) ) {
star . levels . push_back ( levels_pt . second . get < std : : string > ( " name " , " empty " ) ) ;
}
galax . Stars . push_back ( star ) ;
}
galaxies . push_back ( galax ) ;
}
return true ;
}
catch ( boost : : property_tree : : ptree_error ) {
return false ;
}
}
2018-05-24 11:44:58 +00:00
void GalaxyMenu : : UpdateGalaxyMenu ( float s_width , float s_height ) {
/*..Reset..*/
galaxies_params . clear ( ) ;
stars_params . clear ( ) ;
2018-05-23 11:46:02 +00:00
/*..Menu ancestor geometry..*/
2018-05-25 11:51:26 +00:00
float gameScreenWidth = s_width * anchorSize ;
float gameScreenHeight = s_height * anchorSize ;
2018-05-23 11:46:02 +00:00
Eigen : : Vector2f gameScreenCenter = Eigen : : Vector2f ( gameScreenWidth / 2 , gameScreenHeight / 2 ) ;
/*..Menu geometry..*/
xDimension = menuScale * gameScreenWidth ;
yDimension = menuScale * gameScreenHeight ;
Eigen : : Vector2f currentMenuPos = Eigen : : Vector2f ( gameScreenCenter ( 0 ) + ( gameScreenWidth / 2 /*relative to the screen x-dimension*/ ) * menuPosition ( 0 ) , gameScreenCenter ( 1 ) + ( gameScreenHeight / 2 /*relative to the screen y-dimension*/ ) * menuPosition ( 1 ) ) ;
/*..Galaxies geometry..*/
for ( int i = 0 ; i < galaxies . size ( ) ; i + + ) {
2018-05-24 11:44:58 +00:00
* SE : : Console < < " galaxy_ " + std : : to_string ( i ) ;
2018-05-23 11:46:02 +00:00
Eigen : : Vector2f tex_size = textureSizeNormalize (
2018-05-24 11:44:58 +00:00
Eigen : : Vector2f (
( ( float ) SE : : ResourceManager - > TexList . GetTextureWidth ( " galaxy_ " + std : : to_string ( i ) ) ) ,
( ( float ) SE : : ResourceManager - > TexList . GetTextureHeight ( " galaxy_ " + std : : to_string ( i ) ) ) )
2018-05-23 11:46:02 +00:00
) ; // normalized
galaxies_params . push_back ( std : : make_pair (
Eigen : : Vector2f (
2018-05-25 11:51:26 +00:00
currentMenuPos ( 0 ) + ( xDimension / 2 ) * galaxies [ i ] . position ( 0 ) ,
2018-05-24 11:44:58 +00:00
currentMenuPos ( 1 ) + ( yDimension / 2 ) * galaxies [ i ] . position ( 1 ) ) ,
Eigen : : Vector2f (
( tex_size ( 0 ) * galaxies [ i ] . scale ) * menuScale ,
( tex_size ( 1 ) * galaxies [ i ] . scale ) * menuScale
2018-05-23 11:46:02 +00:00
)
) ) ;
2018-05-25 11:51:26 +00:00
* SE : : Console < < " dimensions: " < < std : : to_string ( ( ( tex_size ( 0 ) * galaxies [ i ] . scale ) * menuScale ) ) < < " " < < std : : to_string ( ( ( tex_size ( 1 ) * galaxies [ i ] . scale ) * menuScale ) ) ;
2018-05-23 11:46:02 +00:00
/*..Stars geometry..*/
std : : vector < std : : pair < Eigen : : Vector2f , Eigen : : Vector2f > > star_params ;
for ( int j = 0 ; j < galaxies [ i ] . Stars . size ( ) ; j + + ) {
2018-05-24 11:44:58 +00:00
* SE : : Console < < " star_ " + std : : to_string ( i ) + " _ " + std : : to_string ( j ) ;
tex_size = textureSizeNormalize (
Eigen : : Vector2f (
( ( float ) SE : : ResourceManager - > TexList . GetTextureWidth ( " star_ " + std : : to_string ( i ) + " _ " + std : : to_string ( j ) ) ) ,
( ( float ) SE : : ResourceManager - > TexList . GetTextureHeight ( " star_ " + std : : to_string ( i ) + " _ " + std : : to_string ( j ) ) ) )
) ; // normalized
2018-05-23 11:46:02 +00:00
star_params . push_back ( std : : make_pair (
Eigen : : Vector2f (
2018-05-24 11:44:58 +00:00
galaxies_params [ i ] . first ( 0 ) + ( galaxies_params [ i ] . second ( 0 ) / 2 ) * galaxies [ i ] . Stars [ j ] . position ( 0 ) ,
galaxies_params [ i ] . first ( 1 ) + ( galaxies_params [ i ] . second ( 1 ) / 2 ) * galaxies [ i ] . Stars [ j ] . position ( 1 )
2018-05-23 11:46:02 +00:00
) ,
2018-05-24 11:44:58 +00:00
Eigen : : Vector2f (
( tex_size ( 0 ) * galaxies [ i ] . Stars [ j ] . scale ) * galaxies [ i ] . scale * menuScale ,
( tex_size ( 1 ) * galaxies [ i ] . Stars [ j ] . scale ) * galaxies [ i ] . scale * menuScale
)
2018-05-23 11:46:02 +00:00
) ) ;
}
stars_params . push_back ( star_params ) ;
}
/*..Level list geometry..*/
}
Eigen : : Vector2f GalaxyMenu : : textureSizeNormalize ( Eigen : : Vector2f texVec , int t_type ) {
float tex_ratio = texVec ( 0 ) / texVec ( 1 ) ;
float x_dim , y_dim ;
float Xmax ; // Max normalized texture width
float Xmin ;
float Ymax ; // Max normalized texture height
float Ymin ;
if ( t_type = = 0 ) {
2018-05-25 11:51:26 +00:00
Xmax = SE : : Renderer - > GetScreenWidth ( ) ;
Xmin = Xmax ;
Ymax = SE : : Renderer - > GetScreenHeight ( ) ;
Ymin = Ymax ;
2018-05-23 11:46:02 +00:00
}
else { // temp for star textures
2018-05-25 11:51:26 +00:00
Xmax = ( ( ( float ) SE : : Renderer - > GetScreenWidth ( ) ) / 2 ) ;
Xmin = Xmax ;
Ymax = ( ( ( float ) SE : : Renderer - > GetScreenHeight ( ) ) / 2 ) ;
Ymin = Ymax ;
2018-05-23 11:46:02 +00:00
}
if ( texVec ( 0 ) > texVec ( 1 ) ) {
x_dim = val_clamp ( texVec ( 0 ) , Xmin , Xmax ) ;
y_dim = x_dim / tex_ratio ;
}
else {
y_dim = val_clamp ( texVec ( 1 ) , Ymin , Ymax ) ;
x_dim = y_dim * tex_ratio ;
2018-05-25 11:51:26 +00:00
}
2018-05-24 11:44:58 +00:00
* SE : : Console < < " ============== " ;
* SE : : Console < < std : : to_string ( texVec ( 0 ) ) ;
* SE : : Console < < std : : to_string ( texVec ( 1 ) ) ;
2018-05-25 11:51:26 +00:00
* SE : : Console < < " if value 0.0000 - must be textures not inited at main_code.cpp or texture-name is wrong " ;
2018-05-24 11:44:58 +00:00
* SE : : Console < < " -------------- " ;
2018-05-23 11:46:02 +00:00
return Eigen : : Vector2f ( x_dim , y_dim ) ;
}
float GalaxyMenu : : val_clamp ( float val , float min , float max ) {
if ( val < min )
return min ;
else if ( val > max )
return max ;
else
return val ;
2018-05-24 11:44:58 +00:00
}
void GalaxyMenu : : DrawGalaxyMenu ( ) {
for ( int i = 0 ; i < galaxies_params . size ( ) ; i + + ) {
glBindTexture ( GL_TEXTURE_2D , SE : : ResourceManager - > TexList [ " galaxy_ " + std : : to_string ( i ) ] ) ;
SE : : Renderer - > DrawRect (
Eigen : : Vector2f (
galaxies_params [ i ] . first ( 0 ) - galaxies_params [ i ] . second ( 0 ) / 2 ,
galaxies_params [ i ] . first ( 1 ) - galaxies_params [ i ] . second ( 1 ) / 2
) ,
Eigen : : Vector2f (
galaxies_params [ i ] . first ( 0 ) + galaxies_params [ i ] . second ( 0 ) / 2 ,
galaxies_params [ i ] . first ( 1 ) + galaxies_params [ i ] . second ( 1 ) / 2
)
) ; // DrawRect
/*..Draw stars..*/
if ( stars_params . size ( ) > = i ) {
for ( int j = 0 ; j < stars_params [ i ] . size ( ) ; j + + ) {
glBindTexture ( GL_TEXTURE_2D , SE : : ResourceManager - > TexList [ " star_ " + std : : to_string ( i ) + " _ " + std : : to_string ( j ) ] ) ;
SE : : Renderer - > DrawRect (
Eigen : : Vector2f (
stars_params [ i ] [ j ] . first ( 0 ) - stars_params [ i ] [ j ] . second ( 0 ) / 2 ,
stars_params [ i ] [ j ] . first ( 1 ) - stars_params [ i ] [ j ] . second ( 1 ) / 2
) ,
Eigen : : Vector2f (
stars_params [ i ] [ j ] . first ( 0 ) + stars_params [ i ] [ j ] . second ( 0 ) / 2 ,
stars_params [ i ] [ j ] . first ( 1 ) + stars_params [ i ] [ j ] . second ( 1 ) / 2
)
) ; // DrawRect
}
}
}
}
2018-05-25 11:51:26 +00:00
void GalaxyMenu : : InteractWithGalaxy ( size_t dt ) {
if ( timer_active ) {
// ::::::::::::: timer active ::::::::::::::
if ( menuState = = 0 ) { // main view
//std::pair<Eigen::Vector2f, Eigen::Vector2f> spacePlane = std::make_pair(findCorner(0, 0), findCorner(1, 1));
}
if ( menuState = = 1 ) { // zoomed galaxy
}
if ( menuState = = 2 ) { // level select view
}
// \_/\_/\_/\_/ timer active \_/\_/\_/\_/
}
else {
// ::::::::::::: timer inactive ::::::::::::::
if ( lastTapPos ! = Eigen : : Vector2f ( - 9999.9f , - 9999.9f ) ) {
if ( menuState = = 0 ) { // main view
//if ((currentTapShift(0) <= 0.01f && currentTapShift(0) >= -0.01f) && (currentTapShift(1) <= 0.01f && currentTapShift(1) >= -0.01f)) {
if ( currentTapShift ( 0 ) = = 0.f & & currentTapShift ( 1 ) = = 0.f ) {
// OnTapDown->OnTapUp
//int y = findGalaxyByPos(Eigen::Vector2f());
Eigen : : Vector2f t = currentTapShift ;
* SE : : Console < < " DU vec( " + std : : to_string ( t ( 0 ) ) + " , " + std : : to_string ( t ( 1 ) ) + " ) " ;
}
else {
// OnTapDown->OnMove->OnTapUp
Eigen : : Vector2f t = currentTapShift ;
* SE : : Console < < " DMU vec( " + std : : to_string ( t ( 0 ) ) + " , " + std : : to_string ( t ( 1 ) ) + " ) " ;
}
}
if ( menuState = = 1 ) { // zoomed galaxy
if ( ( currentTapShift ( 0 ) < = 0.01f & & currentTapShift ( 0 ) > = - 0.01f ) & & ( currentTapShift ( 1 ) < = 0.01f & & currentTapShift ( 1 ) > = - 0.01f ) ) {
// OnTapDown->OnTapUp
}
else {
// OnTapDown->OnMove->OnTapUp
}
}
if ( menuState = = 2 ) { // level select view
if ( ( currentTapShift ( 0 ) < = 0.01f & & currentTapShift ( 0 ) > = - 0.01f ) & & ( currentTapShift ( 1 ) < = 0.01f & & currentTapShift ( 1 ) > = - 0.01f ) ) {
// OnTapDown->OnTapUp
}
else {
// OnTapDown->OnMove->OnTapUp
}
}
lastTapPos = Eigen : : Vector2f ( - 9999.9f , - 9999.9f ) ; // tap position reset
}
// \_/\_/\_/\_/ timer inactive \_/\_/\_/\_/
}
// timer reset
if ( timer_active ) {
interact_timer + = ( float ) dt ;
}
else if ( interact_timer ! = 0.f ) {
interact_timer = 0.f ;
currentTapShift = Eigen : : Vector2f ( 0.f , 0.f ) ;
}
}
void GalaxyMenu : : tapDown ( Eigen : : Vector2f pos ) {
if ( ! timer_active ) {
timer_active = true ;
lastTapPos = pos ;
}
}
void GalaxyMenu : : tapUp ( Eigen : : Vector2f pos ) {
if ( timer_active ) {
timer_active = false ;
// lastTapPos = vec(0,0) useless for now
}
}
void GalaxyMenu : : tapMove ( Eigen : : Vector2f shift ) {
if ( timer_active ) {
currentTapShift = shift ; // shift need to be fixed
}
}
Eigen : : Vector2f GalaxyMenu : : findCorner ( int x_c , int y_c ) {
float x_pos = 0.f ;
float y_pos = 0.f ;
float x_p , y_p ;
if ( x_c > 0 ) { // ===
if ( y_c > 0 ) { // ---
for ( int i = 0 ; i < galaxies_params . size ( ) ; i + + ) {
x_p = galaxies_params [ i ] . first ( 0 ) + ( galaxies_params [ i ] . second ( 0 ) / 2 ) ;
y_p = galaxies_params [ i ] . first ( 1 ) + ( galaxies_params [ i ] . second ( 1 ) / 2 ) ;
x_pos = graterV ( x_pos , x_p ) ;
y_pos = graterV ( y_pos , y_p ) ;
for ( int j = 0 ; j < stars_params [ i ] . size ( ) ; j + + ) {
x_p = stars_params [ i ] [ j ] . first ( 0 ) + ( stars_params [ i ] [ j ] . second ( 0 ) ) ;
y_p = stars_params [ i ] [ j ] . first ( 1 ) + ( stars_params [ i ] [ j ] . second ( 1 ) ) ;
x_pos = graterV ( x_pos , x_p ) ;
y_pos = graterV ( y_pos , y_p ) ;
}
}
}
else { // ---
for ( int i = 0 ; i < galaxies_params . size ( ) ; i + + ) {
x_p = galaxies_params [ i ] . first ( 0 ) + ( galaxies_params [ i ] . second ( 0 ) / 2 ) ;
y_p = galaxies_params [ i ] . first ( 1 ) - ( galaxies_params [ i ] . second ( 1 ) / 2 ) ;
x_pos = graterV ( x_pos , x_p ) ;
y_pos = lowerV ( y_pos , y_p ) ;
for ( int j = 0 ; j < stars_params [ i ] . size ( ) ; j + + ) {
x_p = stars_params [ i ] [ j ] . first ( 0 ) + ( stars_params [ i ] [ j ] . second ( 0 ) ) ;
y_p = stars_params [ i ] [ j ] . first ( 1 ) - ( stars_params [ i ] [ j ] . second ( 1 ) ) ;
x_pos = graterV ( x_pos , x_p ) ;
y_pos = lowerV ( y_pos , y_p ) ;
}
}
} // ---
}
else { // ===
if ( y_c > 0 ) { // ---
for ( int i = 0 ; i < galaxies_params . size ( ) ; i + + ) {
x_p = galaxies_params [ i ] . first ( 0 ) - ( galaxies_params [ i ] . second ( 0 ) / 2 ) ;
y_p = galaxies_params [ i ] . first ( 1 ) + ( galaxies_params [ i ] . second ( 1 ) / 2 ) ;
x_pos = lowerV ( x_pos , x_p ) ;
y_pos = graterV ( y_pos , y_p ) ;
for ( int j = 0 ; j < stars_params [ i ] . size ( ) ; j + + ) {
x_p = stars_params [ i ] [ j ] . first ( 0 ) - ( stars_params [ i ] [ j ] . second ( 0 ) ) ;
y_p = stars_params [ i ] [ j ] . first ( 1 ) + ( stars_params [ i ] [ j ] . second ( 1 ) ) ;
x_pos = lowerV ( x_pos , x_p ) ;
y_pos = graterV ( y_pos , y_p ) ;
}
}
}
else { // ---
for ( int i = 0 ; i < galaxies_params . size ( ) ; i + + ) {
x_p = galaxies_params [ i ] . first ( 0 ) - ( galaxies_params [ i ] . second ( 0 ) / 2 ) ;
y_p = galaxies_params [ i ] . first ( 1 ) - ( galaxies_params [ i ] . second ( 1 ) / 2 ) ;
x_pos = lowerV ( x_pos , x_p ) ;
y_pos = lowerV ( y_pos , y_p ) ;
for ( int j = 0 ; j < stars_params [ i ] . size ( ) ; j + + ) {
x_p = stars_params [ i ] [ j ] . first ( 0 ) - ( stars_params [ i ] [ j ] . second ( 0 ) ) ;
y_p = stars_params [ i ] [ j ] . first ( 1 ) - ( stars_params [ i ] [ j ] . second ( 1 ) ) ;
x_pos = lowerV ( x_pos , x_p ) ;
y_pos = lowerV ( y_pos , y_p ) ;
}
}
} // ---
} // ===
return Eigen : : Vector2f ( x_pos , y_pos ) ;
}
float GalaxyMenu : : graterV ( float first_val , float second_val ) {
if ( first_val > second_val )
return first_val ;
else
return second_val ;
}
float GalaxyMenu : : lowerV ( float first_val , float second_val ) {
if ( first_val < second_val )
return first_val ;
else
return second_val ;
}
void GalaxyMenu : : galaxyFocus ( int index ) {
}
int GalaxyMenu : : findGalaxyByPos ( Eigen : : Vector2f pos ) {
for ( int i = 0 ; i < galaxies_params . size ( ) ; i + + ) {
if ( pos ( 0 ) > = ( galaxies_params [ i ] . first ( 0 ) - galaxies_params [ i ] . second ( 0 ) / 2 ) & & pos ( 0 ) < = ( galaxies_params [ i ] . first ( 0 ) + galaxies_params [ i ] . second ( 0 ) / 2 ) ) {
if ( pos ( 1 ) > = ( galaxies_params [ i ] . first ( 1 ) - galaxies_params [ i ] . second ( 1 ) / 2 ) & & pos ( 1 ) < = ( galaxies_params [ i ] . first ( 1 ) + galaxies_params [ i ] . second ( 1 ) / 2 ) ) {
return i ;
}
}
}
2018-05-24 11:44:58 +00:00
2018-05-25 11:51:26 +00:00
return - 1 ;
2018-05-22 12:00:30 +00:00
}