Mario Editor: formatting

This commit is contained in:
zv0n 2021-05-29 19:24:47 +02:00
parent 58974f8a7e
commit 3bef592cb1

View File

@ -98,17 +98,17 @@ enum LandType::Value g_current_world_type = LandType::OVERWORLD;
void updateTool() { void updateTool() {
auto tool_index = global_vars.tool.index; auto tool_index = global_vars.tool.index;
uint64_t tool_id = 0; uint64_t tool_id = 0;
switch(global_vars.tool.type) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
tool_id = possibleBlocks[tool_index]; tool_id = possibleBlocks[tool_index];
break; break;
case ToolType::MOD: case ToolType::MOD:
tool_id = possibleMods[tool_index]; tool_id = possibleMods[tool_index];
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
tool_id = possibleCharacters[tool_index]; tool_id = possibleCharacters[tool_index];
default: default:
break; break;
} }
auto tool_role = getBlockRole( tool_id ); auto tool_role = getBlockRole( tool_id );
std::shared_ptr< SDLPP::Texture > target_texture = nullptr; std::shared_ptr< SDLPP::Texture > target_texture = nullptr;
@ -126,8 +126,7 @@ void updateTool() {
break; break;
} }
global_vars.current_tool->setTexture( global_vars.current_tool->setTexture(
target_texture, target_texture, getSourceRectByID( tool_id, g_current_world_type ) );
getSourceRectByID( tool_id, g_current_world_type ) );
global_vars.current_tool->setId( tool_id ); global_vars.current_tool->setId( tool_id );
global_vars.current_tool->getCollisions()[0]->setId( tool_id ); global_vars.current_tool->getCollisions()[0]->setId( tool_id );
} }
@ -145,34 +144,34 @@ void removeMario() {
global_vars.mario->destroy(); global_vars.mario->destroy();
} }
void setToolColor(const std::string &color) { void setToolColor( const std::string &color ) {
std::vector<std::shared_ptr<SDLPP::RenderObject>> *store; std::vector< std::shared_ptr< SDLPP::RenderObject > > *store;
int multiplier = 0; int multiplier = 0;
switch(global_vars.tool.type) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
multiplier = TOOLS_WIDTH; multiplier = TOOLS_WIDTH;
store = &global_vars.tool_boxes; store = &global_vars.tool_boxes;
break; break;
case ToolType::MOD: case ToolType::MOD:
multiplier = MOD_WIDTH; multiplier = MOD_WIDTH;
store = &global_vars.mod_boxes; store = &global_vars.mod_boxes;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
multiplier = CHARACTER_WIDTH; multiplier = CHARACTER_WIDTH;
store = &global_vars.character_boxes; store = &global_vars.character_boxes;
default: default:
break; break;
} }
auto index = global_vars.tool.index % (2*multiplier); auto index = global_vars.tool.index % ( 2 * multiplier );
store->at(index)->setColor(color); store->at( index )->setColor( color );
} }
void setToolColor() { void setToolColor() {
setToolColor("#FFFF8888"); setToolColor( "#FFFF8888" );
} }
void unsetToolColor() { void unsetToolColor() {
setToolColor("#FFFFFF00"); setToolColor( "#FFFFFF00" );
} }
void updateToolSelection( int prev_index, ToolType::Value type ) { void updateToolSelection( int prev_index, ToolType::Value type ) {
@ -180,196 +179,205 @@ void updateToolSelection( int prev_index, ToolType::Value type ) {
size_t cur_page = 0; size_t cur_page = 0;
size_t multiplier = 0; size_t multiplier = 0;
std::vector< std::shared_ptr< SDLPP::RenderObject > > *tool_vec = nullptr; std::vector< std::shared_ptr< SDLPP::RenderObject > > *tool_vec = nullptr;
switch(type) { switch ( type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
cur_page = global_vars.tool.cur_page_tools; cur_page = global_vars.tool.cur_page_tools;
multiplier = 2*TOOLS_WIDTH; multiplier = 2 * TOOLS_WIDTH;
tool_vec = &global_vars.tools; tool_vec = &global_vars.tools;
break; break;
case ToolType::MOD: case ToolType::MOD:
cur_page = global_vars.tool.cur_page_mods; cur_page = global_vars.tool.cur_page_mods;
multiplier = 2*MOD_WIDTH; multiplier = 2 * MOD_WIDTH;
tool_vec = &global_vars.mods; tool_vec = &global_vars.mods;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
cur_page = global_vars.tool.cur_page_characters; cur_page = global_vars.tool.cur_page_characters;
multiplier = 2*CHARACTER_WIDTH; multiplier = 2 * CHARACTER_WIDTH;
tool_vec = &global_vars.characters; tool_vec = &global_vars.characters;
default: default:
break; break;
} }
auto cur = cur_page * multiplier; auto cur = cur_page * multiplier;
size_t prev = prev_index * multiplier; size_t prev = prev_index * multiplier;
for ( size_t i = prev; for ( size_t i = prev;
i < ( tool_vec->size() < prev + multiplier ? tool_vec->size() : prev + multiplier ); i < ( tool_vec->size() < prev + multiplier ? tool_vec->size()
: prev + multiplier );
i++ ) { i++ ) {
tool_vec->at(i)->setHidden( true ); tool_vec->at( i )->setHidden( true );
} }
for ( size_t i = cur; for ( size_t i = cur;
i < ( tool_vec->size() < cur + multiplier ? tool_vec->size() i < ( tool_vec->size() < cur + multiplier ? tool_vec->size()
: cur + multiplier ); : cur + multiplier );
i++ ) { i++ ) {
tool_vec->at(i)->setHidden( false ); tool_vec->at( i )->setHidden( false );
} }
if(global_vars.tool.index / multiplier == cur_page) if ( global_vars.tool.index / multiplier == cur_page )
setToolColor(); setToolColor();
} }
void moveToolsLeft(ToolType::Value type) { void moveToolsLeft( ToolType::Value type ) {
switch(type) { switch ( type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
global_vars.tool.cur_page_tools--; global_vars.tool.cur_page_tools--;
updateToolSelection( global_vars.tool.cur_page_tools + 1, type ); updateToolSelection( global_vars.tool.cur_page_tools + 1, type );
break; break;
case ToolType::MOD: case ToolType::MOD:
global_vars.tool.cur_page_mods--; global_vars.tool.cur_page_mods--;
updateToolSelection( global_vars.tool.cur_page_mods + 1, type ); updateToolSelection( global_vars.tool.cur_page_mods + 1, type );
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
global_vars.tool.cur_page_characters--; global_vars.tool.cur_page_characters--;
updateToolSelection( global_vars.tool.cur_page_characters + 1, type ); updateToolSelection( global_vars.tool.cur_page_characters + 1, type );
default: default:
break; break;
} }
} }
void moveToolsRight(ToolType::Value type) { void moveToolsRight( ToolType::Value type ) {
switch(type) { switch ( type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
global_vars.tool.cur_page_tools++; global_vars.tool.cur_page_tools++;
updateToolSelection( global_vars.tool.cur_page_tools - 1, type ); updateToolSelection( global_vars.tool.cur_page_tools - 1, type );
break; break;
case ToolType::MOD: case ToolType::MOD:
global_vars.tool.cur_page_mods++; global_vars.tool.cur_page_mods++;
updateToolSelection( global_vars.tool.cur_page_mods - 1, type ); updateToolSelection( global_vars.tool.cur_page_mods - 1, type );
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
global_vars.tool.cur_page_characters++; global_vars.tool.cur_page_characters++;
updateToolSelection( global_vars.tool.cur_page_characters - 1, type ); updateToolSelection( global_vars.tool.cur_page_characters - 1, type );
default: default:
break; break;
} }
} }
void updateToolIndex(uint64_t new_index) { void updateToolIndex( uint64_t new_index ) {
int multiplier = 0; int multiplier = 0;
int *page = nullptr; int *page = nullptr;
switch(global_vars.tool.type) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
multiplier = 2*TOOLS_WIDTH; multiplier = 2 * TOOLS_WIDTH;
page = &global_vars.tool.cur_page_tools; page = &global_vars.tool.cur_page_tools;
break; break;
case ToolType::MOD: case ToolType::MOD:
multiplier = 2*MOD_WIDTH; multiplier = 2 * MOD_WIDTH;
page = &global_vars.tool.cur_page_mods; page = &global_vars.tool.cur_page_mods;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
multiplier = 2*CHARACTER_WIDTH; multiplier = 2 * CHARACTER_WIDTH;
page = &global_vars.tool.cur_page_characters; page = &global_vars.tool.cur_page_characters;
default: default:
break; break;
} }
unsetToolColor(); unsetToolColor();
global_vars.tool.index = new_index; global_vars.tool.index = new_index;
setToolColor(); setToolColor();
updateTool(); updateTool();
if(new_index / multiplier != static_cast<uint64_t>(*page)) { if ( new_index / multiplier != static_cast< uint64_t >( *page ) ) {
auto prev = *page; auto prev = *page;
*page = new_index / multiplier; *page = new_index / multiplier;
updateToolSelection(prev, global_vars.tool.type); updateToolSelection( prev, global_vars.tool.type );
} }
} }
void selectLowerTool() { void selectLowerTool() {
int multiplier = 0; int multiplier = 0;
size_t max_index = 0; size_t max_index = 0;
switch(global_vars.tool.type) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
multiplier = 2 * TOOLS_WIDTH; multiplier = 2 * TOOLS_WIDTH;
max_index = global_vars.tools.size() - 1; max_index = global_vars.tools.size() - 1;
break; break;
case ToolType::MOD: case ToolType::MOD:
multiplier = 2 * MOD_WIDTH; multiplier = 2 * MOD_WIDTH;
max_index = global_vars.mods.size() - 1; max_index = global_vars.mods.size() - 1;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
multiplier = 2 * CHARACTER_WIDTH; multiplier = 2 * CHARACTER_WIDTH;
max_index = global_vars.characters.size() - 1; max_index = global_vars.characters.size() - 1;
default: default:
break; break;
} }
if( global_vars.tool.index % multiplier >= static_cast<uint64_t>(multiplier/2) || global_vars.tool.index + multiplier/2 > max_index ) if ( global_vars.tool.index % multiplier >=
static_cast< uint64_t >( multiplier / 2 ) ||
global_vars.tool.index + multiplier / 2 > max_index ) {
return; return;
updateToolIndex(global_vars.tool.index + multiplier/2); }
updateToolIndex( global_vars.tool.index + multiplier / 2 );
} }
void selectUpperTool() { void selectUpperTool() {
int multiplier = 0; int multiplier = 0;
switch(global_vars.tool.type) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
multiplier = 2 * TOOLS_WIDTH; multiplier = 2 * TOOLS_WIDTH;
break; break;
case ToolType::MOD: case ToolType::MOD:
multiplier = 2 * MOD_WIDTH; multiplier = 2 * MOD_WIDTH;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
multiplier = 2 * CHARACTER_WIDTH; multiplier = 2 * CHARACTER_WIDTH;
default: default:
break; break;
} }
if( global_vars.tool.index % multiplier < static_cast<uint64_t>(multiplier/2) ) if ( global_vars.tool.index % multiplier <
static_cast< uint64_t >( multiplier / 2 ) ) {
return; return;
updateToolIndex(global_vars.tool.index - multiplier/2); }
updateToolIndex( global_vars.tool.index - multiplier / 2 );
} }
void selectPrevTool() { void selectPrevTool() {
int multiplier = 0; int multiplier = 0;
switch(global_vars.tool.type) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
multiplier = TOOLS_WIDTH; multiplier = TOOLS_WIDTH;
break; break;
case ToolType::MOD: case ToolType::MOD:
multiplier = MOD_WIDTH; multiplier = MOD_WIDTH;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
multiplier = CHARACTER_WIDTH; multiplier = CHARACTER_WIDTH;
default: default:
break; break;
} }
int subtraction = 1; int subtraction = 1;
if ( global_vars.tool.index % multiplier == 0 ) if ( global_vars.tool.index % multiplier == 0 )
subtraction = multiplier + 1; subtraction = multiplier + 1;
if ( global_vars.tool.index == 0 || global_vars.tool.index - subtraction > static_cast<uint64_t>(-multiplier) ) if ( global_vars.tool.index == 0 ||
global_vars.tool.index - subtraction >
static_cast< uint64_t >( -multiplier ) ) {
return; return;
updateToolIndex(global_vars.tool.index - subtraction); }
updateToolIndex( global_vars.tool.index - subtraction );
} }
void selectNextTool() { void selectNextTool() {
size_t max_index = 0; size_t max_index = 0;
int multiplier = 0; int multiplier = 0;
switch(global_vars.tool.type) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
max_index = global_vars.tools.size() - 1; max_index = global_vars.tools.size() - 1;
multiplier = TOOLS_WIDTH; multiplier = TOOLS_WIDTH;
break; break;
case ToolType::MOD: case ToolType::MOD:
max_index = global_vars.mods.size() - 1; max_index = global_vars.mods.size() - 1;
multiplier = MOD_WIDTH; multiplier = MOD_WIDTH;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
max_index = global_vars.characters.size() - 1; max_index = global_vars.characters.size() - 1;
multiplier = CHARACTER_WIDTH; multiplier = CHARACTER_WIDTH;
default: default:
break; break;
} }
int addition = 1; int addition = 1;
if ( global_vars.tool.index % multiplier == (multiplier - 1) ) if ( global_vars.tool.index % multiplier == ( multiplier - 1 ) )
addition = multiplier + 1; addition = multiplier + 1;
if ( global_vars.tool.index == max_index ) if ( global_vars.tool.index == max_index )
return; return;
if ( global_vars.tool.index + addition > max_index ) if ( global_vars.tool.index + addition > max_index )
addition = 1; addition = 1;
updateToolIndex(global_vars.tool.index + addition); updateToolIndex( global_vars.tool.index + addition );
} }
void setFlag( uint64_t flag ) { void setFlag( uint64_t flag ) {
@ -419,7 +427,8 @@ void getMousePositionFlags( SDLPP::Scene &scene ) {
global_vars.mouse.cur_flags = visitor.getFlags(); global_vars.mouse.cur_flags = visitor.getFlags();
global_vars.mouse.edit_box = visitor.getEditBoxIndexes(); global_vars.mouse.edit_box = visitor.getEditBoxIndexes();
global_vars.mouse.tool_box = visitor.getToolBoxIndexes(); global_vars.mouse.tool_box = visitor.getToolBoxIndexes();
global_vars.mouse.tool_type = static_cast<ToolType::Value>(visitor.getToolType()); global_vars.mouse.tool_type =
static_cast< ToolType::Value >( visitor.getToolType() );
// if we found an edit box, move tool icon to that box // if we found an edit box, move tool icon to that box
if ( visitor.foundEditBox() ) { if ( visitor.foundEditBox() ) {
const auto &box = global_vars.mouse.edit_box; const auto &box = global_vars.mouse.edit_box;
@ -447,32 +456,39 @@ void mouseUpAction( uint64_t flags, SDLPP::Scene &scene ) {
if ( MouseVisitor::moveToolsLeft( flags ) && if ( MouseVisitor::moveToolsLeft( flags ) &&
global_vars.tool.cur_page_tools != 0 ) { global_vars.tool.cur_page_tools != 0 ) {
global_vars.tool.cur_page_tools--; global_vars.tool.cur_page_tools--;
updateToolSelection( global_vars.tool.cur_page_tools + 1, ToolType::BLOCK ); updateToolSelection( global_vars.tool.cur_page_tools + 1,
ToolType::BLOCK );
} }
if ( MouseVisitor::moveToolsRight( flags ) && if ( MouseVisitor::moveToolsRight( flags ) &&
global_vars.tool.cur_page_tools != global_vars.tool.max_page_tools ) { global_vars.tool.cur_page_tools != global_vars.tool.max_page_tools ) {
global_vars.tool.cur_page_tools++; global_vars.tool.cur_page_tools++;
updateToolSelection( global_vars.tool.cur_page_tools - 1, ToolType::BLOCK ); updateToolSelection( global_vars.tool.cur_page_tools - 1,
ToolType::BLOCK );
} }
if ( MouseVisitor::moveModsLeft( flags ) && if ( MouseVisitor::moveModsLeft( flags ) &&
global_vars.tool.cur_page_mods != 0 ) { global_vars.tool.cur_page_mods != 0 ) {
global_vars.tool.cur_page_mods--; global_vars.tool.cur_page_mods--;
updateToolSelection( global_vars.tool.cur_page_mods + 1, ToolType::MOD ); updateToolSelection( global_vars.tool.cur_page_mods + 1,
ToolType::MOD );
} }
if ( MouseVisitor::moveModsRight( flags ) && if ( MouseVisitor::moveModsRight( flags ) &&
global_vars.tool.cur_page_mods != global_vars.tool.max_page_mods ) { global_vars.tool.cur_page_mods != global_vars.tool.max_page_mods ) {
global_vars.tool.cur_page_mods++; global_vars.tool.cur_page_mods++;
updateToolSelection( global_vars.tool.cur_page_mods - 1, ToolType::MOD ); updateToolSelection( global_vars.tool.cur_page_mods - 1,
ToolType::MOD );
} }
if ( MouseVisitor::moveCharactersLeft( flags ) && if ( MouseVisitor::moveCharactersLeft( flags ) &&
global_vars.tool.cur_page_characters != 0 ) { global_vars.tool.cur_page_characters != 0 ) {
global_vars.tool.cur_page_characters--; global_vars.tool.cur_page_characters--;
updateToolSelection( global_vars.tool.cur_page_characters + 1, ToolType::CHARACTER ); updateToolSelection( global_vars.tool.cur_page_characters + 1,
ToolType::CHARACTER );
} }
if ( MouseVisitor::moveCharactersRight( flags ) && if ( MouseVisitor::moveCharactersRight( flags ) &&
global_vars.tool.cur_page_characters != global_vars.tool.max_page_characters ) { global_vars.tool.cur_page_characters !=
global_vars.tool.max_page_characters ) {
global_vars.tool.cur_page_characters++; global_vars.tool.cur_page_characters++;
updateToolSelection( global_vars.tool.cur_page_characters - 1, ToolType::CHARACTER ); updateToolSelection( global_vars.tool.cur_page_characters - 1,
ToolType::CHARACTER );
} }
} }
@ -535,8 +551,9 @@ void placeTool( SDLPP::Scene &scene ) {
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
break; break;
case VisitorType::Modifier: case VisitorType::Modifier:
if(tool_type == BlockRole::MARIO) { if ( tool_type == BlockRole::MARIO ) {
std::get< MapObject::CHARACTER_TYPE >( obj ) = LandType::OVERWORLD; std::get< MapObject::CHARACTER_TYPE >( obj ) =
LandType::OVERWORLD;
std::get< MapObject::CHARACTER_ID >( obj ) = MARIO_ID; std::get< MapObject::CHARACTER_ID >( obj ) = MARIO_ID;
std::get< MapObject::MODIFIER_TYPE >( obj ) = 0; std::get< MapObject::MODIFIER_TYPE >( obj ) = 0;
std::get< MapObject::MODIFIER_DATA >( obj ) = 0; std::get< MapObject::MODIFIER_DATA >( obj ) = 0;
@ -552,15 +569,18 @@ void placeTool( SDLPP::Scene &scene ) {
// TODO BlockRole::Character // TODO BlockRole::Character
} else { } else {
// TODO data // TODO data
std::get< MapObject::MODIFIER_TYPE >( obj ) = global_vars.current_tool->getId(); std::get< MapObject::MODIFIER_TYPE >( obj ) =
global_vars.current_tool->getId();
std::get< MapObject::MODIFIER_DATA >( obj ) = 0; std::get< MapObject::MODIFIER_DATA >( obj ) = 0;
new_obj = createTerrainBlock( new_obj = createTerrainBlock(
global_vars.current_tool->getId(), LandType::OVERWORLD, global_vars.current_tool->getId(), LandType::OVERWORLD,
renderer, 1 + global_vars.mouse.edit_box.getX(), renderer, 1 + global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(), global_vars.translucent_terrain_texture, true ); global_vars.mouse.edit_box.getY(),
global_vars.translucent_terrain_texture, true );
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
// TODO createModifierBlock // TODO createModifierBlock
dynamic_cast<MarioBlock*>(new_obj.get())->setTerrain(false); dynamic_cast< MarioBlock * >( new_obj.get() )
->setTerrain( false );
z_index = scene.getObjects().size() - 1; z_index = scene.getObjects().size() - 1;
} }
break; break;
@ -602,28 +622,29 @@ void pollEvents( SDLPP::Scene &scene ) {
int multiplier = 0; int multiplier = 0;
size_t max_index = 0; size_t max_index = 0;
size_t cur_page = 0; size_t cur_page = 0;
switch(global_vars.mouse.tool_type) { switch ( global_vars.mouse.tool_type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
multiplier = 2*TOOLS_WIDTH; multiplier = 2 * TOOLS_WIDTH;
max_index = global_vars.tools.size(); max_index = global_vars.tools.size();
cur_page = global_vars.tool.cur_page_tools; cur_page = global_vars.tool.cur_page_tools;
break; break;
case ToolType::MOD: case ToolType::MOD:
multiplier = 2*MOD_WIDTH; multiplier = 2 * MOD_WIDTH;
max_index = global_vars.mods.size(); max_index = global_vars.mods.size();
cur_page = global_vars.tool.cur_page_mods; cur_page = global_vars.tool.cur_page_mods;
break; break;
case ToolType::CHARACTER: case ToolType::CHARACTER:
multiplier = 2*CHARACTER_WIDTH; multiplier = 2 * CHARACTER_WIDTH;
max_index = global_vars.characters.size(); max_index = global_vars.characters.size();
cur_page = global_vars.tool.cur_page_characters; cur_page = global_vars.tool.cur_page_characters;
default: default:
break; break;
} }
size_t index = tool_box.getY() * (multiplier/2) + tool_box.getX(); size_t index =
tool_box.getY() * ( multiplier / 2 ) + tool_box.getX();
if ( index < max_index ) { if ( index < max_index ) {
global_vars.tool.type = global_vars.mouse.tool_type; global_vars.tool.type = global_vars.mouse.tool_type;
updateToolIndex(cur_page * multiplier + index); updateToolIndex( cur_page * multiplier + index );
} }
} }
break; break;
@ -648,31 +669,35 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
} }
} }
void createGrid(double start_x, double start_y, int count_x, int count_y, std::shared_ptr<SDLPP::Scene> &scene) { void createGrid( double start_x, double start_y, int count_x, int count_y,
std::shared_ptr< SDLPP::Scene > &scene ) {
auto renderer = scene->getRendererShared(); auto renderer = scene->getRendererShared();
auto width = count_x * BLOCK_SIZE; auto width = count_x * BLOCK_SIZE;
auto height = count_y * BLOCK_SIZE; auto height = count_y * BLOCK_SIZE;
for ( int i = 0; i < count_x + 1; i++ ) { for ( int i = 0; i < count_x + 1; i++ ) {
auto line_vertical = std::make_shared< SDLPP::LineRenderer >( auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
start_x + i * BLOCK_SIZE, start_y, start_x + i * BLOCK_SIZE, start_y + height, start_x + i * BLOCK_SIZE, start_y, start_x + i * BLOCK_SIZE,
renderer, "#282828" ); start_y + height, renderer, "#282828" );
line_vertical->setPermanent(); line_vertical->setPermanent();
line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject( line_vertical ); scene->addObject( line_vertical );
} }
for (int i = 0; i < count_y + 1; i++) { for ( int i = 0; i < count_y + 1; i++ ) {
auto line_horizontal = std::make_shared< SDLPP::LineRenderer >( auto line_horizontal = std::make_shared< SDLPP::LineRenderer >(
start_x, start_y + i * BLOCK_SIZE, start_x, start_y + i * BLOCK_SIZE, start_x + width,
start_x + width, start_y + i * BLOCK_SIZE, start_y + i * BLOCK_SIZE, renderer, "#282828" );
renderer, "#282828" );
line_horizontal->setPermanent(); line_horizontal->setPermanent();
line_horizontal->setAlignment( SDLPP::OBJ_CENTER, line_horizontal->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
SDLPP::OBJ_CENTER );
scene->addObject( line_horizontal ); scene->addObject( line_horizontal );
} }
} }
std::pair<std::shared_ptr<SDLPP::TextRenderer>, std::shared_ptr<SDLPP::TextRenderer>> createArrowControls(double left_x, double right_x, double start_y, double height, uint64_t left_id, uint64_t right_id, std::shared_ptr<SDLPP::Scene> &scene, std::shared_ptr<SDLPP::FontConfiguration> &font_config) { std::pair< std::shared_ptr< SDLPP::TextRenderer >,
std::shared_ptr< SDLPP::TextRenderer > >
createArrowControls(
double left_x, double right_x, double start_y, double height,
uint64_t left_id, uint64_t right_id, std::shared_ptr< SDLPP::Scene > &scene,
std::shared_ptr< SDLPP::FontConfiguration > &font_config ) {
auto renderer = scene->getRendererShared(); auto renderer = scene->getRendererShared();
// white rectangles // white rectangles
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >( auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
@ -692,7 +717,8 @@ std::pair<std::shared_ptr<SDLPP::TextRenderer>, std::shared_ptr<SDLPP::TextRende
scene->addObject( rectangle2 ); scene->addObject( rectangle2 );
auto left = std::make_shared< SDLPP::TextRenderer >( auto left = std::make_shared< SDLPP::TextRenderer >(
left_x, start_y + height / 2.0 - 0.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, "<", font_config ); left_x, start_y + height / 2.0 - 0.5 * BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE, renderer, "<", font_config );
left->setId( 0 ); left->setId( 0 );
left->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); left->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
left->setPermanent(); left->setPermanent();
@ -704,25 +730,31 @@ std::pair<std::shared_ptr<SDLPP::TextRenderer>, std::shared_ptr<SDLPP::TextRende
right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right->setPermanent(); right->setPermanent();
scene->addObject( right ); scene->addObject( right );
return {left, right}; return { left, right };
} }
void populateToolGrid(int count_x, int count_y, float start_x, float start_y, ToolType::Value type, const std::vector<uint64_t> &blocks, std::vector<std::shared_ptr<SDLPP::RenderObject>> &tool_store, std::vector<std::shared_ptr<SDLPP::RenderObject>> &tool_box_store, std::shared_ptr<SDLPP::Scene> &scene) { void populateToolGrid(
int count_x, int count_y, float start_x, float start_y,
ToolType::Value type, const std::vector< uint64_t > &blocks,
std::vector< std::shared_ptr< SDLPP::RenderObject > > &tool_store,
std::vector< std::shared_ptr< SDLPP::RenderObject > > &tool_box_store,
std::shared_ptr< SDLPP::Scene > &scene ) {
auto renderer = scene->getRendererShared(); auto renderer = scene->getRendererShared();
for(int j = 0; j < count_y; j++) { for ( int j = 0; j < count_y; j++ ) {
for ( int i = 0; i < count_x; i++ ) { for ( int i = 0; i < count_x; i++ ) {
auto tool_box = std::make_shared< ToolBox >( i, j, auto tool_box = std::make_shared< ToolBox >( i, j, start_x, start_y,
start_x, start_y, renderer, false ); renderer, false );
scene->addObject( tool_box ); scene->addObject( tool_box );
} }
} }
int tool_index = 0; int tool_index = 0;
for ( auto &block : blocks ) { for ( auto &block : blocks ) {
switch(type) { switch ( type ) {
case ToolType::CHARACTER: case ToolType::CHARACTER:
if(block == MARIO_ID) if ( block == MARIO_ID )
tool_store.push_back(createMario(LandType::OVERWORLD, renderer, 0, 0)); tool_store.push_back(
else createMario( LandType::OVERWORLD, renderer, 0, 0 ) );
else
case ToolType::BLOCK: case ToolType::BLOCK:
case ToolType::MOD: case ToolType::MOD:
tool_store.push_back( createTerrainBlock( tool_store.push_back( createTerrainBlock(
@ -732,52 +764,52 @@ void populateToolGrid(int count_x, int count_y, float start_x, float start_y, To
tool_store.back()->setPermanent(); tool_store.back()->setPermanent();
auto x = tool_index % count_x; auto x = tool_index % count_x;
auto y = tool_index / count_x; auto y = tool_index / count_x;
tool_store.back()->setPos( start_x + tool_store.back()->setPos( start_x + x * BLOCK_SIZE,
x * BLOCK_SIZE, start_y + y * BLOCK_SIZE );
start_y + y * BLOCK_SIZE );
scene->addObject( tool_store.back() ); scene->addObject( tool_store.back() );
tool_index = ( tool_index + 1 ) % (2*count_x); tool_index = ( tool_index + 1 ) % ( 2 * count_x );
} }
for(int j = 0; j < count_y; j++) { for ( int j = 0; j < count_y; j++ ) {
for ( int i = 0; i < count_x; i++ ) { for ( int i = 0; i < count_x; i++ ) {
auto tool_box = std::make_shared< ToolBox >( i, j, auto tool_box =
start_x, start_y, renderer ); std::make_shared< ToolBox >( i, j, start_x, start_y, renderer );
tool_box->setType(type); tool_box->setType( type );
scene->addObject( tool_box ); scene->addObject( tool_box );
tool_box->setColor("#00000000"); tool_box->setColor( "#00000000" );
tool_box_store.push_back(tool_box); tool_box_store.push_back( tool_box );
} }
} }
} }
void checkArrowsEnabled( uint64_t cur_page, uint64_t max_page, uint64_t left_flag, uint64_t right_flag, std::shared_ptr<SDLPP::TextRenderer> &left_arrow, std::shared_ptr<SDLPP::TextRenderer> &right_arrow, std::shared_ptr<SDLPP::Font> &font, bool map = false ) { void checkArrowsEnabled( uint64_t cur_page, uint64_t max_page,
if ( cur_page == 0 && uint64_t left_flag, uint64_t right_flag,
getFlag( left_flag ) ) { std::shared_ptr< SDLPP::TextRenderer > &left_arrow,
std::shared_ptr< SDLPP::TextRenderer > &right_arrow,
std::shared_ptr< SDLPP::Font > &font,
bool map = false ) {
if ( cur_page == 0 && getFlag( left_flag ) ) {
setFlag( UPDATE_FLAG ); setFlag( UPDATE_FLAG );
unsetFlag( left_flag ); unsetFlag( left_flag );
left_arrow->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 ); left_arrow->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else if ( cur_page != 0 && } else if ( cur_page != 0 && !getFlag( left_flag ) ) {
!getFlag( left_flag ) ) {
setFlag( UPDATE_FLAG ); setFlag( UPDATE_FLAG );
setFlag( left_flag ); setFlag( left_flag );
left_arrow->setTextColor( font, "#000000", "#282828", 0.05 ); left_arrow->setTextColor( font, "#000000", "#282828", 0.05 );
} }
if ( cur_page == max_page && if ( cur_page == max_page && getFlag( right_flag ) ) {
getFlag( right_flag ) ) {
setFlag( UPDATE_FLAG ); setFlag( UPDATE_FLAG );
unsetFlag( right_flag ); unsetFlag( right_flag );
if(map) { if ( map ) {
right_arrow->setTextColor( font, "#00FF00", "#000000", 0.1 ); right_arrow->setTextColor( font, "#00FF00", "#000000", 0.1 );
right_arrow->changeText( "+" ); right_arrow->changeText( "+" );
} else { } else {
right_arrow->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 ); right_arrow->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} }
} else if ( cur_page != max_page && } else if ( cur_page != max_page && !getFlag( right_flag ) ) {
!getFlag( right_flag ) ) {
setFlag( UPDATE_FLAG ); setFlag( UPDATE_FLAG );
setFlag( right_flag ); setFlag( right_flag );
right_arrow->setTextColor( font, "#000000", "#282828", 0.05 ); right_arrow->setTextColor( font, "#000000", "#282828", 0.05 );
if(map) { if ( map ) {
right_arrow->changeText( ">" ); right_arrow->changeText( ">" );
} }
} }
@ -789,7 +821,7 @@ int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
#else #else
int main() { int main() {
#endif #endif
//TODO the code setting up UI is terrible, terrible mess, make it cleaner // TODO the code setting up UI is terrible, terrible mess, make it cleaner
SDLPP::init(); SDLPP::init();
SDLPP::Window w( "Mario editor!" ); SDLPP::Window w( "Mario editor!" );
w.setResizable( true ); w.setResizable( true );
@ -803,9 +835,8 @@ int main() {
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_mario_texture = std::make_shared< SDLPP::Texture >( g_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture = g_translucent_terrain_texture = std::make_shared< SDLPP::Texture >(
std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png", renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture->setAlpha( 100 ); g_translucent_terrain_texture->setAlpha( 100 );
auto scene = std::make_shared< SDLPP::Scene >( renderer ); auto scene = std::make_shared< SDLPP::Scene >( renderer );
@ -816,7 +847,8 @@ int main() {
scene->addObject( bg ); scene->addObject( bg );
// TODO file name // TODO file name
loadMap( scene, global_vars.mario, "test_binary.bin", renderer, global_vars.objects ); loadMap( scene, global_vars.mario, "test_binary.bin", renderer,
global_vars.objects );
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 ); auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
auto font_config = std::make_shared< SDLPP::FontConfiguration >( auto font_config = std::make_shared< SDLPP::FontConfiguration >(
@ -824,10 +856,14 @@ int main() {
// create grids and arrow controls // create grids and arrow controls
// map // map
auto arrows = createArrowControls(0, (MAP_WIDTH + 1) * BLOCK_SIZE, (MAP_WIDTH + 2 - MAP_HEIGHT) * BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE, EDITOR_LEFT_MAP_ID, EDITOR_RIGHT_MAP_ID, scene, font_config); auto arrows = createArrowControls(
0, ( MAP_WIDTH + 1 ) * BLOCK_SIZE,
( MAP_WIDTH + 2 - MAP_HEIGHT ) * BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE,
EDITOR_LEFT_MAP_ID, EDITOR_RIGHT_MAP_ID, scene, font_config );
auto left_map_arrow = arrows.first; auto left_map_arrow = arrows.first;
auto right_map_arrow = arrows.second; auto right_map_arrow = arrows.second;
createGrid(BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH, MAP_HEIGHT, scene); createGrid( BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH, MAP_HEIGHT,
scene );
// edit blocks on map // edit blocks on map
for ( int i = 0; i < MAP_WIDTH; i++ ) { for ( int i = 0; i < MAP_WIDTH; i++ ) {
for ( int j = 0; j < MAP_HEIGHT; j++ ) { for ( int j = 0; j < MAP_HEIGHT; j++ ) {
@ -837,30 +873,50 @@ int main() {
} }
// tools // tools
populateToolGrid(TOOLS_WIDTH, 2, (MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE, BLOCK_SIZE, ToolType::BLOCK, possibleBlocks, global_vars.tools, global_vars.tool_boxes, scene); populateToolGrid( TOOLS_WIDTH, 2, ( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE,
arrows = createArrowControls((MAP_WIDTH - TOOLS_WIDTH - 1) * BLOCK_SIZE, MAP_WIDTH * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_TOOL_ID, EDITOR_RIGHT_TOOL_ID, scene, font_config); BLOCK_SIZE, ToolType::BLOCK, possibleBlocks,
global_vars.tools, global_vars.tool_boxes, scene );
arrows = createArrowControls( ( MAP_WIDTH - TOOLS_WIDTH - 1 ) * BLOCK_SIZE,
MAP_WIDTH * BLOCK_SIZE, BLOCK_SIZE,
2 * BLOCK_SIZE, EDITOR_LEFT_TOOL_ID,
EDITOR_RIGHT_TOOL_ID, scene, font_config );
auto left_tool_arrow = arrows.first; auto left_tool_arrow = arrows.first;
auto right_tool_arrow = arrows.second; auto right_tool_arrow = arrows.second;
createGrid(( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE, TOOLS_WIDTH, 2, scene); createGrid( ( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE,
TOOLS_WIDTH, 2, scene );
// mods // mods
populateToolGrid(MOD_WIDTH, 2, 2 * BLOCK_SIZE, BLOCK_SIZE, ToolType::MOD, possibleMods, global_vars.mods, global_vars.mod_boxes, scene); populateToolGrid( MOD_WIDTH, 2, 2 * BLOCK_SIZE, BLOCK_SIZE, ToolType::MOD,
arrows = createArrowControls(BLOCK_SIZE, (MOD_WIDTH + 2) * BLOCK_SIZE, BLOCK_SIZE, 2*BLOCK_SIZE, EDITOR_LEFT_MOD_ID, EDITOR_RIGHT_MOD_ID, scene, font_config); possibleMods, global_vars.mods, global_vars.mod_boxes,
scene );
arrows = createArrowControls(
BLOCK_SIZE, ( MOD_WIDTH + 2 ) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE,
EDITOR_LEFT_MOD_ID, EDITOR_RIGHT_MOD_ID, scene, font_config );
auto left_mod_arrow = arrows.first; auto left_mod_arrow = arrows.first;
auto right_mod_arrow = arrows.second; auto right_mod_arrow = arrows.second;
createGrid(2*BLOCK_SIZE, BLOCK_SIZE, MOD_WIDTH, 2, scene); createGrid( 2 * BLOCK_SIZE, BLOCK_SIZE, MOD_WIDTH, 2, scene );
// characters // characters
populateToolGrid(CHARACTER_WIDTH, 2, (MOD_WIDTH+5) * BLOCK_SIZE, BLOCK_SIZE, ToolType::CHARACTER, possibleCharacters, global_vars.characters, global_vars.character_boxes, scene); populateToolGrid( CHARACTER_WIDTH, 2, ( MOD_WIDTH + 5 ) * BLOCK_SIZE,
arrows = createArrowControls((MOD_WIDTH + 4) * BLOCK_SIZE, (MOD_WIDTH + 5 + CHARACTER_WIDTH) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_CHARACTER_ID, EDITOR_RIGHT_CHARACTER_ID, scene, font_config); BLOCK_SIZE, ToolType::CHARACTER, possibleCharacters,
global_vars.characters, global_vars.character_boxes,
scene );
arrows = createArrowControls(
( MOD_WIDTH + 4 ) * BLOCK_SIZE,
( MOD_WIDTH + 5 + CHARACTER_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE,
2 * BLOCK_SIZE, EDITOR_LEFT_CHARACTER_ID, EDITOR_RIGHT_CHARACTER_ID,
scene, font_config );
auto left_char_arrow = arrows.first; auto left_char_arrow = arrows.first;
auto right_char_arrow = arrows.second; auto right_char_arrow = arrows.second;
createGrid((MOD_WIDTH + 5) * BLOCK_SIZE, BLOCK_SIZE, CHARACTER_WIDTH, 2, scene); createGrid( ( MOD_WIDTH + 5 ) * BLOCK_SIZE, BLOCK_SIZE, CHARACTER_WIDTH, 2,
scene );
global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH; global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH;
global_vars.tool.max_page_tools = ( possibleBlocks.size() - 1 ) / (2*TOOLS_WIDTH); global_vars.tool.max_page_tools =
global_vars.tool.max_page_mods = ( possibleMods.size() - 1 ) / (2*MOD_WIDTH); ( possibleBlocks.size() - 1 ) / ( 2 * TOOLS_WIDTH );
global_vars.tool.max_page_characters = ( possibleCharacters.size() - 1 ) / (2*CHARACTER_WIDTH); global_vars.tool.max_page_mods =
( possibleMods.size() - 1 ) / ( 2 * MOD_WIDTH );
global_vars.tool.max_page_characters =
( possibleCharacters.size() - 1 ) / ( 2 * CHARACTER_WIDTH );
auto mouse = std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0, auto mouse = std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0,
renderer ); renderer );
@ -924,10 +980,22 @@ int main() {
frames = 0; frames = 0;
base = SDL_GetTicks(); base = SDL_GetTicks();
} }
checkArrowsEnabled(global_vars.map.cur_page, global_vars.map.max_page, MAP_LEFT_ENABLED_FLAG, MAP_RIGHT_ENABLED_FLAG, left_map_arrow, right_map_arrow, font, true); checkArrowsEnabled( global_vars.map.cur_page, global_vars.map.max_page,
checkArrowsEnabled(global_vars.tool.cur_page_tools, global_vars.tool.max_page_tools, TOOL_LEFT_ENABLED_FLAG, TOOL_RIGHT_ENABLED_FLAG, left_tool_arrow, right_tool_arrow, font); MAP_LEFT_ENABLED_FLAG, MAP_RIGHT_ENABLED_FLAG,
checkArrowsEnabled(global_vars.tool.cur_page_mods, global_vars.tool.max_page_mods, MOD_LEFT_ENABLED_FLAG, MOD_RIGHT_ENABLED_FLAG, left_mod_arrow, right_mod_arrow, font); left_map_arrow, right_map_arrow, font, true );
checkArrowsEnabled(global_vars.tool.cur_page_characters, global_vars.tool.max_page_characters, CHARACTER_LEFT_ENABLED_FLAG, CHARACTER_RIGHT_ENABLED_FLAG, left_char_arrow, right_char_arrow, font); checkArrowsEnabled( global_vars.tool.cur_page_tools,
global_vars.tool.max_page_tools,
TOOL_LEFT_ENABLED_FLAG, TOOL_RIGHT_ENABLED_FLAG,
left_tool_arrow, right_tool_arrow, font );
checkArrowsEnabled( global_vars.tool.cur_page_mods,
global_vars.tool.max_page_mods,
MOD_LEFT_ENABLED_FLAG, MOD_RIGHT_ENABLED_FLAG,
left_mod_arrow, right_mod_arrow, font );
checkArrowsEnabled( global_vars.tool.cur_page_characters,
global_vars.tool.max_page_characters,
CHARACTER_LEFT_ENABLED_FLAG,
CHARACTER_RIGHT_ENABLED_FLAG, left_char_arrow,
right_char_arrow, font );
if ( getFlag( UPDATE_FLAG ) ) { if ( getFlag( UPDATE_FLAG ) ) {
scene->updateSizeAndPosition(); scene->updateSizeAndPosition();
unsetFlag( UPDATE_FLAG ); unsetFlag( UPDATE_FLAG );