Mario Editor: add tool highlighting + WSAD controls

This commit is contained in:
zv0n 2021-05-29 19:17:45 +02:00
parent 3cb36fe180
commit 58974f8a7e
3 changed files with 159 additions and 38 deletions

View File

@ -80,6 +80,9 @@ struct GlobalVars {
std::vector< std::shared_ptr< SDLPP::RenderObject > > tools;
std::vector< std::shared_ptr< SDLPP::RenderObject > > mods;
std::vector< std::shared_ptr< SDLPP::RenderObject > > characters;
std::vector< std::shared_ptr< SDLPP::RenderObject > > tool_boxes;
std::vector< std::shared_ptr< SDLPP::RenderObject > > mod_boxes;
std::vector< std::shared_ptr< SDLPP::RenderObject > > character_boxes;
std::shared_ptr< SDLPP::RenderObject > current_tool;
std::shared_ptr< SDLPP::Texture > translucent_terrain_texture;
std::shared_ptr< SDLPP::Texture > translucent_mario_texture;
@ -142,29 +145,60 @@ void removeMario() {
global_vars.mario->destroy();
}
void setToolColor(const std::string &color) {
std::vector<std::shared_ptr<SDLPP::RenderObject>> *store;
int multiplier = 0;
switch(global_vars.tool.type) {
case ToolType::BLOCK:
multiplier = TOOLS_WIDTH;
store = &global_vars.tool_boxes;
break;
case ToolType::MOD:
multiplier = MOD_WIDTH;
store = &global_vars.mod_boxes;
break;
case ToolType::CHARACTER:
multiplier = CHARACTER_WIDTH;
store = &global_vars.character_boxes;
default:
break;
}
auto index = global_vars.tool.index % (2*multiplier);
store->at(index)->setColor(color);
}
void setToolColor() {
setToolColor("#FFFF8888");
}
void unsetToolColor() {
setToolColor("#FFFFFF00");
}
void updateToolSelection( int prev_index, ToolType::Value type ) {
size_t cur = 0;
unsetToolColor();
size_t cur_page = 0;
size_t multiplier = 0;
std::vector< std::shared_ptr< SDLPP::RenderObject > > *tool_vec = nullptr;
switch(type) {
case ToolType::BLOCK:
cur = global_vars.tool.cur_page_tools;
cur_page = global_vars.tool.cur_page_tools;
multiplier = 2*TOOLS_WIDTH;
tool_vec = &global_vars.tools;
break;
case ToolType::MOD:
cur = global_vars.tool.cur_page_mods;
cur_page = global_vars.tool.cur_page_mods;
multiplier = 2*MOD_WIDTH;
tool_vec = &global_vars.mods;
break;
case ToolType::CHARACTER:
cur = global_vars.tool.cur_page_characters;
cur_page = global_vars.tool.cur_page_characters;
multiplier = 2*CHARACTER_WIDTH;
tool_vec = &global_vars.characters;
default:
break;
}
cur *= multiplier;
auto cur = cur_page * multiplier;
size_t prev = prev_index * multiplier;
for ( size_t i = prev;
i < ( tool_vec->size() < prev + multiplier ? tool_vec->size() : prev + multiplier );
@ -177,6 +211,8 @@ void updateToolSelection( int prev_index, ToolType::Value type ) {
i++ ) {
tool_vec->at(i)->setHidden( false );
}
if(global_vars.tool.index / multiplier == cur_page)
setToolColor();
}
void moveToolsLeft(ToolType::Value type) {
@ -215,33 +251,36 @@ void moveToolsRight(ToolType::Value type) {
}
}
// TODO add red outline to currently selected tool
// add WSAD navigation for the red highlight
// add mouse wheel navigation
void selectPrevTool() {
void updateToolIndex(uint64_t new_index) {
int multiplier = 0;
int *page = nullptr;
switch(global_vars.tool.type) {
case ToolType::BLOCK:
multiplier = 2*TOOLS_WIDTH;
page = &global_vars.tool.cur_page_tools;
break;
case ToolType::MOD:
multiplier = 2*MOD_WIDTH;
page = &global_vars.tool.cur_page_mods;
break;
case ToolType::CHARACTER:
multiplier = 2*CHARACTER_WIDTH;
page = &global_vars.tool.cur_page_characters;
default:
break;
}
if ( global_vars.tool.index == 0 )
return;
if ( global_vars.tool.index % multiplier == 0 ) {
moveToolsLeft(global_vars.tool.type);
}
global_vars.tool.index--;
unsetToolColor();
global_vars.tool.index = new_index;
setToolColor();
updateTool();
if(new_index / multiplier != static_cast<uint64_t>(*page)) {
auto prev = *page;
*page = new_index / multiplier;
updateToolSelection(prev, global_vars.tool.type);
}
}
void selectNextTool() {
void selectLowerTool() {
int multiplier = 0;
size_t max_index = 0;
switch(global_vars.tool.type) {
@ -259,13 +298,78 @@ void selectNextTool() {
default:
break;
}
if( global_vars.tool.index % multiplier >= static_cast<uint64_t>(multiplier/2) || global_vars.tool.index + multiplier/2 > max_index )
return;
updateToolIndex(global_vars.tool.index + multiplier/2);
}
void selectUpperTool() {
int multiplier = 0;
switch(global_vars.tool.type) {
case ToolType::BLOCK:
multiplier = 2 * TOOLS_WIDTH;
break;
case ToolType::MOD:
multiplier = 2 * MOD_WIDTH;
break;
case ToolType::CHARACTER:
multiplier = 2 * CHARACTER_WIDTH;
default:
break;
}
if( global_vars.tool.index % multiplier < static_cast<uint64_t>(multiplier/2) )
return;
updateToolIndex(global_vars.tool.index - multiplier/2);
}
void selectPrevTool() {
int multiplier = 0;
switch(global_vars.tool.type) {
case ToolType::BLOCK:
multiplier = TOOLS_WIDTH;
break;
case ToolType::MOD:
multiplier = MOD_WIDTH;
break;
case ToolType::CHARACTER:
multiplier = CHARACTER_WIDTH;
default:
break;
}
int subtraction = 1;
if ( global_vars.tool.index % multiplier == 0 )
subtraction = multiplier + 1;
if ( global_vars.tool.index == 0 || global_vars.tool.index - subtraction > static_cast<uint64_t>(-multiplier) )
return;
updateToolIndex(global_vars.tool.index - subtraction);
}
void selectNextTool() {
size_t max_index = 0;
int multiplier = 0;
switch(global_vars.tool.type) {
case ToolType::BLOCK:
max_index = global_vars.tools.size() - 1;
multiplier = TOOLS_WIDTH;
break;
case ToolType::MOD:
max_index = global_vars.mods.size() - 1;
multiplier = MOD_WIDTH;
break;
case ToolType::CHARACTER:
max_index = global_vars.characters.size() - 1;
multiplier = CHARACTER_WIDTH;
default:
break;
}
int addition = 1;
if ( global_vars.tool.index % multiplier == (multiplier - 1) )
addition = multiplier + 1;
if ( global_vars.tool.index == max_index )
return;
if ( global_vars.tool.index % multiplier == (multiplier-1) ) {
moveToolsRight(global_vars.tool.type);
}
global_vars.tool.index++;
updateTool();
if ( global_vars.tool.index + addition > max_index )
addition = 1;
updateToolIndex(global_vars.tool.index + addition);
}
void setFlag( uint64_t flag ) {
@ -278,6 +382,7 @@ bool getFlag( uint64_t flag ) {
return global_vars.flags & flag;
}
// TODO add mouse wheel control for modifiers
void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
case SDLK_ESCAPE:
@ -289,6 +394,12 @@ void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) {
case SDLK_d:
selectNextTool();
break;
case SDLK_w:
selectUpperTool();
break;
case SDLK_s:
selectLowerTool();
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
@ -509,11 +620,10 @@ void pollEvents( SDLPP::Scene &scene ) {
default:
break;
}
global_vars.tool.type = global_vars.mouse.tool_type;
size_t index = tool_box.getY() * (multiplier/2) + tool_box.getX();
if ( index < max_index ) {
global_vars.tool.index = cur_page * multiplier + index;
updateTool();
global_vars.tool.type = global_vars.mouse.tool_type;
updateToolIndex(cur_page * multiplier + index);
}
}
break;
@ -597,13 +707,12 @@ std::pair<std::shared_ptr<SDLPP::TextRenderer>, std::shared_ptr<SDLPP::TextRende
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::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();
for ( int i = 0; i < count_x; i++ ) {
for(int j = 0; j < count_y; j++) {
for ( int i = 0; i < count_x; i++ ) {
auto tool_box = std::make_shared< ToolBox >( i, j,
start_x, start_y, renderer );
tool_box->setType(type);
start_x, start_y, renderer, false );
scene->addObject( tool_box );
}
}
@ -629,6 +738,16 @@ void populateToolGrid(int count_x, int count_y, float start_x, float start_y, To
scene->addObject( tool_store.back() );
tool_index = ( tool_index + 1 ) % (2*count_x);
}
for(int j = 0; j < count_y; j++) {
for ( int i = 0; i < count_x; i++ ) {
auto tool_box = std::make_shared< ToolBox >( i, j,
start_x, start_y, renderer );
tool_box->setType(type);
scene->addObject( tool_box );
tool_box->setColor("#00000000");
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 ) {
@ -718,19 +837,19 @@ int main() {
}
// tools
populateToolGrid(TOOLS_WIDTH, 2, (MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE, BLOCK_SIZE, ToolType::BLOCK, possibleBlocks, global_vars.tools, scene);
populateToolGrid(TOOLS_WIDTH, 2, (MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE, 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 right_tool_arrow = arrows.second;
createGrid(( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE, TOOLS_WIDTH, 2, scene);
// mods
populateToolGrid(MOD_WIDTH, 2, 2 * BLOCK_SIZE, BLOCK_SIZE, ToolType::MOD, possibleMods, global_vars.mods, scene);
populateToolGrid(MOD_WIDTH, 2, 2 * BLOCK_SIZE, BLOCK_SIZE, ToolType::MOD, 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 right_mod_arrow = arrows.second;
createGrid(2*BLOCK_SIZE, BLOCK_SIZE, MOD_WIDTH, 2, scene);
// characters
populateToolGrid(CHARACTER_WIDTH, 2, (MOD_WIDTH+5) * BLOCK_SIZE, BLOCK_SIZE, ToolType::CHARACTER, possibleCharacters, global_vars.characters, scene);
populateToolGrid(CHARACTER_WIDTH, 2, (MOD_WIDTH+5) * BLOCK_SIZE, 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 right_char_arrow = arrows.second;
@ -777,6 +896,7 @@ int main() {
updateToolSelection( 0, ToolType::BLOCK );
updateToolSelection( 0, ToolType::MOD );
updateToolSelection( 0, ToolType::CHARACTER );
setToolColor();
auto base = SDL_GetTicks();
int frames = 0;

View File

@ -3,7 +3,7 @@
#include "blocks.hpp"
#include "sprites.hpp"
ToolBox::ToolBox(int x, int y, double start_x, double start_y, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender(start_x + x*BLOCK_SIZE, start_y + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
ToolBox::ToolBox(int x, int y, double start_x, double start_y, std::shared_ptr<SDLPP::Renderer> renderer, bool coliders) : SDLPP::RectangleRender(start_x + x*BLOCK_SIZE, start_y + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
_x = x;
_y = y;
setId(EDITOR_TOOL_ID);
@ -11,6 +11,7 @@ ToolBox::ToolBox(int x, int y, double start_x, double start_y, std::shared_ptr<S
setColor("#FFFFFF88");
setPermanent();
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
if(coliders)
addCollision(SDLPP::RectColider(0,0,1,1));
}

View File

@ -5,7 +5,7 @@
class ToolBox : public SDLPP::RectangleRender {
public:
ToolBox(int x, int y, double start_x, double start_y, std::shared_ptr<SDLPP::Renderer> renderer);
ToolBox(int x, int y, double start_x, double start_y, std::shared_ptr<SDLPP::Renderer> renderer, bool coliders = true);
virtual SDLPP::Vec2D<int> getIndexes() const;
virtual void visit( SDLPP::Visitor &visitor ) override;
uint64_t getType() const {