2013-08-23 22:15:00 +00:00
# include "GuiMetaDataEd.h"
2013-08-18 14:16:11 +00:00
# include "../Renderer.h"
# include "../Log.h"
2013-09-15 19:11:39 +00:00
# include "AsyncReqComponent.h"
2013-09-17 21:50:49 +00:00
# include "../Settings.h"
2013-09-19 23:41:14 +00:00
# include "GuiGameScraper.h"
2013-10-10 00:50:42 +00:00
# include <boost/filesystem.hpp>
2013-10-16 23:46:05 +00:00
# include "GuiMsgBoxYesNo.h"
2013-08-18 14:16:11 +00:00
2013-08-21 19:49:33 +00:00
# define MDED_RESERVED_ROWS 3
2013-08-18 14:16:11 +00:00
2013-09-17 21:50:49 +00:00
GuiMetaDataEd : : GuiMetaDataEd ( Window * window , MetaDataList * md , const std : : vector < MetaDataDecl > & mdd , ScraperSearchParams scraperParams ,
2013-08-23 22:15:00 +00:00
const std : : string & header , std : : function < void ( ) > saveCallback , std : : function < void ( ) > deleteFunc ) : GuiComponent ( window ) ,
2013-09-17 21:50:49 +00:00
mScraperParams ( scraperParams ) ,
2013-08-23 02:41:40 +00:00
mBox ( mWindow , " :/frame.png " , 0xAAAAAAFF , 0xCCCCCCFF ) ,
2013-09-19 23:41:14 +00:00
mList ( window , Eigen : : Vector2i ( 2 , mdd . size ( ) + MDED_RESERVED_ROWS ) ) ,
2013-08-23 22:15:00 +00:00
mHeader ( window ) ,
2013-09-19 23:41:14 +00:00
mMetaDataDecl ( mdd ) ,
2013-08-23 22:15:00 +00:00
mMetaData ( md ) ,
mSavedCallback ( saveCallback ) , mDeleteFunc ( deleteFunc ) ,
2013-08-22 20:29:50 +00:00
mDeleteButton ( window ) , mFetchButton ( window ) , mSaveButton ( window )
2013-08-18 14:16:11 +00:00
{
2013-09-19 23:41:14 +00:00
unsigned int sw = Renderer : : getScreenWidth ( ) ;
unsigned int sh = Renderer : : getScreenHeight ( ) ;
2013-08-18 14:16:11 +00:00
addChild ( & mBox ) ;
2013-09-19 23:41:14 +00:00
2013-08-23 22:15:00 +00:00
addChild ( & mHeader ) ;
mHeader . setText ( header ) ;
2013-08-18 14:16:11 +00:00
2013-08-22 20:29:50 +00:00
//initialize buttons
2013-08-23 22:15:00 +00:00
mDeleteButton . setText ( " DELETE " , mDeleteFunc ? 0xFF0000FF : 0x555555FF ) ;
if ( mDeleteFunc )
2013-10-16 23:46:05 +00:00
{
std : : function < void ( ) > deleteFileAndSelf = [ & ] { mDeleteFunc ( ) ; delete this ; } ;
std : : function < void ( ) > pressedFunc = [ this , deleteFileAndSelf ] { mWindow - > pushGui ( new GuiMsgBoxYesNo ( mWindow , " This will delete a file! \n Are you sure? " , deleteFileAndSelf ) ) ; } ;
mDeleteButton . setPressedFunc ( pressedFunc ) ;
}
2013-08-23 17:21:22 +00:00
2013-09-19 23:41:14 +00:00
mFetchButton . setText ( " FETCH " , 0x00FF00FF ) ;
mFetchButton . setPressedFunc ( std : : bind ( & GuiMetaDataEd : : fetch , this ) ) ;
2013-08-22 20:29:50 +00:00
mSaveButton . setText ( " SAVE " , 0x0000FFFF ) ;
2013-08-23 22:15:00 +00:00
mSaveButton . setPressedFunc ( [ & ] { save ( ) ; delete this ; } ) ;
2013-08-22 20:29:50 +00:00
2013-08-18 14:16:11 +00:00
//initialize metadata list
addChild ( & mList ) ;
populateList ( mdd ) ;
2013-09-19 23:41:14 +00:00
mList . setPosition ( ( sw - mList . getSize ( ) . x ( ) ) / 2.0f , ( sh - mList . getSize ( ) . y ( ) ) / 2.0f ) ; //center it
mList . resetCursor ( ) ;
mBox . fitTo ( mList . getSize ( ) , mList . getPosition ( ) , Eigen : : Vector2f ( 12 , 12 ) ) ;
mHeader . setPosition ( mList . getPosition ( ) ) ;
mHeader . setSize ( mList . getSize ( ) . x ( ) , 0 ) ;
mHeader . setCentered ( true ) ;
2013-08-18 14:16:11 +00:00
}
2013-08-23 22:15:00 +00:00
GuiMetaDataEd : : ~ GuiMetaDataEd ( )
2013-08-18 14:16:11 +00:00
{
2013-09-19 23:41:14 +00:00
for ( auto iter = mLabels . begin ( ) ; iter ! = mLabels . end ( ) ; iter + + )
{
delete * iter ;
}
for ( auto iter = mEditors . begin ( ) ; iter ! = mEditors . end ( ) ; iter + + )
{
delete * iter ;
}
2013-08-18 14:16:11 +00:00
}
2013-08-23 22:15:00 +00:00
void GuiMetaDataEd : : populateList ( const std : : vector < MetaDataDecl > & mdd )
2013-08-18 14:16:11 +00:00
{
// PATH //(centered, not part of componentlist)
//---GAME NAME--- //(at 1,1; size 3,1) (TextEditComponent)
//DEL SCRP --- //(buttons)
//Fav: Y/N --- //metadata start
//Desc: ... --- //multiline texteditcomponent
//Img: ... ---
//--- SAVE ---
using namespace Eigen ;
int y = 0 ;
2013-08-22 20:29:50 +00:00
//fetch button
2013-09-19 23:41:14 +00:00
mList . setEntry ( Vector2i ( 0 , y ) , Vector2i ( 1 , 1 ) , & mFetchButton , true , ComponentListComponent : : AlignLeft ) ;
//delete button
mList . setEntry ( Vector2i ( 1 , y ) , Vector2i ( 1 , 1 ) , & mDeleteButton , true , ComponentListComponent : : AlignRight ) ;
2013-09-15 19:11:39 +00:00
2013-08-18 14:16:11 +00:00
y + + ;
for ( auto iter = mdd . begin ( ) ; iter ! = mdd . end ( ) ; iter + + )
{
TextComponent * label = new TextComponent ( mWindow ) ;
label - > setText ( iter - > key ) ;
mList . setEntry ( Vector2i ( 0 , y ) , Vector2i ( 1 , 1 ) , label , false , ComponentListComponent : : AlignLeft ) ;
2013-08-22 20:29:50 +00:00
mLabels . push_back ( label ) ;
2013-08-18 14:16:11 +00:00
GuiComponent * ed = MetaDataList : : makeEditor ( mWindow , iter - > type ) ;
2013-09-19 23:41:14 +00:00
ed - > setSize ( Renderer : : getScreenWidth ( ) * 0.4f , ed - > getSize ( ) . y ( ) ) ;
2013-08-23 22:15:00 +00:00
ed - > setValue ( mMetaData - > get ( iter - > key ) ) ;
2013-08-18 14:16:11 +00:00
mList . setEntry ( Vector2i ( 1 , y ) , Vector2i ( 1 , 1 ) , ed , true , ComponentListComponent : : AlignRight ) ;
2013-08-22 20:29:50 +00:00
mEditors . push_back ( ed ) ;
2013-08-18 14:16:11 +00:00
y + + ;
}
2013-08-22 20:29:50 +00:00
//save button
mList . setEntry ( Vector2i ( 0 , y ) , Vector2i ( 2 , 1 ) , & mSaveButton , true , ComponentListComponent : : AlignCenter ) ;
}
2013-08-23 22:15:00 +00:00
void GuiMetaDataEd : : save ( )
2013-08-22 20:29:50 +00:00
{
for ( unsigned int i = 0 ; i < mLabels . size ( ) ; i + + )
{
2013-08-23 22:15:00 +00:00
mMetaData - > set ( mLabels . at ( i ) - > getValue ( ) , mEditors . at ( i ) - > getValue ( ) ) ;
2013-08-22 20:29:50 +00:00
}
2013-08-23 17:21:22 +00:00
2013-08-23 22:15:00 +00:00
if ( mSavedCallback )
mSavedCallback ( ) ;
2013-08-23 17:21:22 +00:00
}
2013-09-15 19:11:39 +00:00
void GuiMetaDataEd : : fetch ( )
2013-09-17 21:50:49 +00:00
{
2013-09-19 23:41:14 +00:00
GuiGameScraper * scr = new GuiGameScraper ( mWindow , mScraperParams , std : : bind ( & GuiMetaDataEd : : fetchDone , this , std : : placeholders : : _1 ) ) ;
mWindow - > pushGui ( scr ) ;
scr - > search ( ) ;
2013-09-17 21:50:49 +00:00
}
2013-09-19 23:41:14 +00:00
void GuiMetaDataEd : : fetchDone ( MetaDataList result )
2013-09-15 19:11:39 +00:00
{
2013-10-13 19:07:48 +00:00
//TODO - replace this with resolveMetaDataAssetsAsync!
2013-10-10 00:50:42 +00:00
//this is a little tricky:
//go through the list of returned results, if anything is an image and the path looks like a URL:
// (1) start an async download + resize (will create an AsyncReq that blocks further user input)
// (when this is finished, call result.set(key, newly_downloaded_file_path) and call fetchDone() again)
// (2) return from this function immediately
for ( auto it = mMetaDataDecl . begin ( ) ; it ! = mMetaDataDecl . end ( ) ; it + + )
{
std : : string key = it - > key ;
std : : string val = result . get ( it - > key ) ;
//val is /probably/ a URL
if ( it - > type = = MD_IMAGE_PATH & & HttpReq : : isUrl ( val ) )
{
2013-10-13 19:07:48 +00:00
downloadImageAsync ( mWindow , val , getSaveAsPath ( mScraperParams , key , val ) ,
2013-10-10 00:50:42 +00:00
[ this , result , key ] ( std : : string filePath ) mutable - > void {
//skip it
if ( filePath . empty ( ) )
LOG ( LogError ) < < " Error resolving boxart " ;
result . set ( key , filePath ) ;
this - > fetchDone ( result ) ;
} ) ;
return ;
}
}
2013-09-19 23:41:14 +00:00
for ( unsigned int i = 0 ; i < mEditors . size ( ) ; i + + )
{
2013-09-28 16:38:31 +00:00
//don't overwrite statistics
if ( mMetaDataDecl . at ( i ) . isStatistic )
continue ;
2013-10-10 00:50:42 +00:00
const std : : string & key = mMetaDataDecl . at ( i ) . key ;
2013-09-19 23:41:14 +00:00
mEditors . at ( i ) - > setValue ( result . get ( key ) ) ;
}
2013-09-15 19:11:39 +00:00
}
2013-10-16 23:46:05 +00:00
bool GuiMetaDataEd : : input ( InputConfig * config , Input input )
{
if ( GuiComponent : : input ( config , input ) )
return true ;
if ( input . value ! = 0 & & config - > isMappedTo ( " b " , input ) )
{
delete this ;
return true ;
}
return false ;
}