Mario Editor: refactoring

This commit is contained in:
zv0n 2021-05-09 20:21:53 +02:00
parent 83cda5f860
commit ede8bbbe8b

View File

@ -30,25 +30,38 @@ struct MouseInfo {
};
};
struct MapInfo {
enum Value {
CUR_PAGE = 0,
MAX_PAGE = 1,
};
};
struct ToolInfo {
enum Value {
INDEX = 0,
CUR_PAGE = 1,
MAX_PAGE = 2,
};
};
std::shared_ptr< SDLPP::Renderer > g_renderer = nullptr;
bool g_quit = false;
bool g_update_size = false;
std::vector< mapColumnType > g_objects = {};
std::vector< std::shared_ptr< SDLPP::RenderObject > > g_tools{};
std::shared_ptr< SDLPP::RenderObject > g_current_tool = nullptr;
std::mutex g_destruction_mutex;
// current mouse flags, previous mouse flags, selected edit box, selected tool
// box
std::tuple< uint64_t, uint64_t, SDLPP::Vec2D< int >, SDLPP::Vec2D< int > >
g_mouse_info;
int g_current_start_index = 0;
int g_current_max_index = 0;
uint64_t g_current_block = 0;
std::vector< std::shared_ptr< SDLPP::RenderObject > > g_tools{};
std::shared_ptr< SDLPP::RenderObject > g_current_tool = nullptr;
int g_current_tool_index = 0;
int g_max_tool_index = 0;
// current page, max page
std::tuple< int, int > g_map_info;
// tool index (in possibleBlocks), current pange, max page
std::tuple<uint64_t, int, int> g_tool_info;
std::shared_ptr< SDLPP::Texture > g_placeholder_texture = nullptr;
std::shared_ptr< SDLPP::Texture > g_placeholder_mario = nullptr;
@ -59,7 +72,7 @@ SDLPP::Vec2D< int > g_mario_pos = { 0, 0 };
enum LandType::Value g_current_world_type = LandType::OVERWORLD;
void updateTool() {
auto tool_role = getBlockRole( possibleBlocks[g_current_block] );
auto tool_role = getBlockRole( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)] );
std::shared_ptr< SDLPP::Texture > target_texture = nullptr;
switch ( tool_role ) {
case BlockRole::TERRAIN:
@ -74,11 +87,11 @@ void updateTool() {
break;
}
g_current_tool->setTexture(
target_texture, getSourceRectByID( possibleBlocks[g_current_block],
target_texture, getSourceRectByID( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)],
g_current_world_type ) );
g_current_tool->setId( possibleBlocks[g_current_block] );
g_current_tool->setId( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)] );
g_current_tool->getCollisions()[0]->setId(
possibleBlocks[g_current_block] );
possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)] );
}
void removeMario() {
@ -94,7 +107,7 @@ void removeMario() {
void updateToolSelection( int prev_index ) {
size_t prev = prev_index * 8;
size_t cur = g_current_tool_index * 8;
size_t cur = std::get<ToolInfo::CUR_PAGE>(g_tool_info) * 8;
for ( size_t i = prev;
i < ( g_tools.size() < prev + 8 ? g_tools.size() : prev + 8 ); i++ ) {
g_tools[i]->setHidden( true );
@ -106,34 +119,34 @@ void updateToolSelection( int prev_index ) {
}
void moveToolsLeft() {
g_current_tool_index--;
updateToolSelection( g_current_tool_index + 1 );
std::get<ToolInfo::CUR_PAGE>(g_tool_info)--;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) + 1 );
}
void moveToolsRight() {
g_current_tool_index++;
updateToolSelection( g_current_tool_index - 1 );
std::get<ToolInfo::CUR_PAGE>(g_tool_info)++;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) - 1 );
}
// TODO add red outline to currently selected tool
// add WSAD navigation for the red highlight
void selectPrevTool() {
if ( g_current_block == 0 )
if ( std::get<ToolInfo::INDEX>(g_tool_info) == 0 )
return;
if ( g_current_block % 8 == 0 ) {
if ( std::get<ToolInfo::INDEX>(g_tool_info) % 8 == 0 ) {
moveToolsLeft();
}
g_current_block--;
std::get<ToolInfo::INDEX>(g_tool_info)--;
updateTool();
}
void selectNextTool() {
if ( g_current_block == g_tools.size() - 1 )
if ( std::get<ToolInfo::INDEX>(g_tool_info) == g_tools.size() - 1 )
return;
if ( g_current_block % 8 == 7 ) {
if ( std::get<ToolInfo::INDEX>(g_tool_info) % 8 == 7 ) {
moveToolsRight();
}
g_current_block++;
std::get<ToolInfo::INDEX>(g_tool_info)++;
updateTool();
}
@ -178,33 +191,33 @@ void getMousePositionFlags( SDLPP::Scene &scene ) {
}
void mouseUpAction( uint64_t flags, SDLPP::Scene &scene ) {
if ( MouseVisitor::moveMapLeft( flags ) && g_current_start_index != 0 ) {
g_current_start_index -= 1;
if ( MouseVisitor::moveMapLeft( flags ) && std::get<MapInfo::CUR_PAGE>(g_map_info) != 0 ) {
std::get<MapInfo::CUR_PAGE>(g_map_info)--;
scene.moveEverything( BLOCK_SIZE, 0 );
}
if ( MouseVisitor::moveMapRight( flags ) ) {
if ( g_current_start_index == g_current_max_index ) {
if ( std::get<MapInfo::CUR_PAGE>(g_map_info) == std::get<MapInfo::MAX_PAGE>(g_map_info) ) {
// add column
// TODO 18 as constant
g_objects.resize( g_current_max_index + 18 + 1 );
g_current_max_index++;
g_objects.resize( std::get<MapInfo::MAX_PAGE>(g_map_info) + 18 + 1 );
std::get<MapInfo::MAX_PAGE>(g_map_info)++;
}
g_current_start_index += 1;
std::get<MapInfo::CUR_PAGE>(g_map_info) += 1;
scene.moveEverything( -BLOCK_SIZE, 0 );
}
if ( MouseVisitor::moveToolsLeft( flags ) && g_current_tool_index != 0 ) {
g_current_tool_index -= 1;
updateToolSelection( g_current_tool_index + 1 );
if ( MouseVisitor::moveToolsLeft( flags ) && std::get<ToolInfo::CUR_PAGE>(g_tool_info) != 0 ) {
std::get<ToolInfo::CUR_PAGE>(g_tool_info) -= 1;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) + 1 );
}
if ( MouseVisitor::moveToolsRight( flags ) &&
g_current_tool_index != g_max_tool_index ) {
g_current_tool_index += 1;
updateToolSelection( g_current_tool_index - 1 );
std::get<ToolInfo::CUR_PAGE>(g_tool_info) != std::get<ToolInfo::MAX_PAGE>(g_tool_info) ) {
std::get<ToolInfo::CUR_PAGE>(g_tool_info) += 1;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) - 1 );
}
}
SDLPP::Vec2D< int > getSelectedObjectPosition() {
return { g_current_start_index +
return { std::get<MapInfo::CUR_PAGE>(g_map_info) +
std::get< MouseInfo::EDIT_BOX >( g_mouse_info ).getX(),
std::get< MouseInfo::EDIT_BOX >( g_mouse_info ).getY() };
}
@ -320,7 +333,7 @@ void pollEvents( SDLPP::Scene &scene ) {
std::get< MouseInfo::TOOL_BOX >( g_mouse_info );
size_t index = tool_box.getY() * 4 + tool_box.getX();
if ( index < g_tools.size() ) {
g_current_block = g_current_tool_index * 8 + index;
std::get<ToolInfo::INDEX>(g_tool_info) = std::get<ToolInfo::CUR_PAGE>(g_tool_info) * 8 + index;
updateTool();
}
}
@ -446,10 +459,10 @@ int main() {
mouse->setColiderColor( "#00FF00" );
mouse->addCollision( SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
scene->addObject( mouse );
g_current_max_index = g_objects.size() - 18;
std::get<MapInfo::MAX_PAGE>(g_map_info) = g_objects.size() - 18;
// tools
g_max_tool_index = ( possibleBlocks.size() - 1 ) / 8;
std::get<ToolInfo::MAX_PAGE>(g_tool_info) = ( possibleBlocks.size() - 1 ) / 8;
for ( int i = 0; i < 4; i++ ) {
auto tool_box1 = std::make_shared< ToolBox >( i, 0, g_renderer );
auto tool_box2 = std::make_shared< ToolBox >( i, 1, g_renderer );
@ -539,7 +552,7 @@ int main() {
g_placeholder_mario = std::make_shared< SDLPP::Texture >(
g_renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_placeholder_mario->setAlpha( 100 );
g_current_tool = createTerrainBlock( possibleBlocks[g_current_block],
g_current_tool = createTerrainBlock( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)],
LandType::OVERWORLD, g_renderer,
g_placeholder_texture, false );
g_current_tool->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
@ -570,24 +583,24 @@ int main() {
frames = 0;
base = SDL_GetTicks();
}
if ( g_current_start_index == 0 ) {
if ( std::get<MapInfo::CUR_PAGE>(g_map_info) == 0 ) {
left->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else {
left->setTextColor( font, "#000000", "#282828", 0.05 );
}
if ( g_current_start_index == g_current_max_index ) {
if ( std::get<MapInfo::CUR_PAGE>(g_map_info) == std::get<MapInfo::MAX_PAGE>(g_map_info) ) {
right->setTextColor( font, "#00FF00", "#000000", 0.1 );
right->changeText( "+" );
} else {
right->setTextColor( font, "#000000", "#282828", 0.05 );
right->changeText( ">" );
}
if ( g_current_tool_index == 0 ) {
if ( std::get<ToolInfo::CUR_PAGE>(g_tool_info) == 0 ) {
left_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else {
left_tool->setTextColor( font, "#000000", "#282828", 0.05 );
}
if ( g_current_tool_index == g_max_tool_index ) {
if ( std::get<ToolInfo::CUR_PAGE>(g_tool_info) == std::get<ToolInfo::MAX_PAGE>(g_tool_info) ) {
right_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else {
right_tool->setTextColor( font, "#000000", "#282828", 0.05 );