Mario Editor: Tool selection alpha

This commit is contained in:
zv0n 2021-05-07 09:43:57 +02:00
parent c383654349
commit f89d36c177
9 changed files with 123 additions and 10 deletions

View File

@ -18,7 +18,7 @@ endif
COMMON_OBJECTS = blocks.${OBJEXT} global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT}
MARIO_OBJECTS = mario.${OBJEXT} mario_visitor.${OBJEXT} ${COMMON_OBJECTS}
EDITOR_OBJECTS = editor.${OBJEXT} edit_box.${OBJEXT} editor_visitor.${OBJEXT} ${COMMON_OBJECTS}
EDITOR_OBJECTS = editor.${OBJEXT} edit_box.${OBJEXT} tool_box.${OBJEXT} editor_visitor.${OBJEXT} ${COMMON_OBJECTS}
ifeq ($(UNAME_S),Linux)
MARIO_OBJECTS += libsdlpp.so
@ -63,6 +63,8 @@ editor.${OBJEXT}: editor.cpp ../sdlpp/sdlpp.hpp sprites.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
edit_box.${OBJEXT}: edit_box.cpp ../sdlpp/sdlpp.hpp sprites.hpp edit_box.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
tool_box.${OBJEXT}: tool_box.cpp ../sdlpp/sdlpp.hpp sprites.hpp tool_box.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
editor_visitor.${OBJEXT}: editor_visitor.cpp ../sdlpp/sdlpp.hpp sprites.hpp editor_visitor.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
libsdlpp.so: ../sdlpp

View File

@ -1,4 +1,7 @@
#include "edit_box.hpp"
#include "objectids.hpp"
#include "blocks.hpp"
#include "sprites.hpp"
EditBox::EditBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender(BLOCK_SIZE + x*BLOCK_SIZE, 4*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
_x = x;

View File

@ -1,14 +1,15 @@
#ifndef EDIT_BOX_H
#define EDIT_BOX_H
#include "../sdlpp/sdlpp.hpp"
#include "objectids.hpp"
#include "blocks.hpp"
#include "sprites.hpp"
class EditBox : public SDLPP::RectangleRender {
public:
EditBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer);
SDLPP::Vec2D<int> getIndexes() const;
void visit( SDLPP::Visitor &visitor ) override;
virtual SDLPP::Vec2D<int> getIndexes() const;
virtual void visit( SDLPP::Visitor &visitor ) override;
private:
int _x;
int _y;
};
#endif

View File

@ -19,6 +19,7 @@
#include "../sdlpp/sdlpp_mouse.hpp"
#include "edit_box.hpp"
#include "editor_visitor.hpp"
#include "tool_box.hpp"
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
bool quit = false;
@ -32,7 +33,12 @@ int current_start_index = 0;
int current_max_index = 0;
uint64_t current_block = 0;
SDLPP::Vec2D<int> current_box = {0, 0};
SDLPP::Vec2D<int> current_tool_box = {0, 0};
std::vector<std::shared_ptr<SDLPP::RenderObject>> tools{};
std::shared_ptr<SDLPP::RenderObject> current_tool = nullptr;
int current_tool_index = 0;
int max_tool_index = 0;
std::shared_ptr<SDLPP::Texture> g_placeholder_texture = nullptr;
@ -67,12 +73,12 @@ void updateTool() {
void handleKeyUp( SDL_Keycode key ) {
switch ( key ) {
case SDLK_a:
current_block = (current_block + possibleBlocks.size() - 1) % possibleBlocks.size();
updateTool();
// current_block = (current_block + possibleBlocks.size() - 1) % possibleBlocks.size();
// updateTool();
break;
case SDLK_d:
current_block = (current_block + 1) % possibleBlocks.size();
updateTool();
// current_block = (current_block + 1) % possibleBlocks.size();
// updateTool();
break;
case SDLK_w:
case SDLK_s:
@ -110,6 +116,7 @@ void pollEvents( SDLPP::Scene &scene ) {
if(visitor.foundEditBox()) {
current_tool->setPos(BLOCK_SIZE + current_box.getX() * BLOCK_SIZE, 4*BLOCK_SIZE + current_box.getY() * BLOCK_SIZE);
}
current_tool_box = visitor.getToolBoxIndexes();
}
break;
case SDL_MOUSEBUTTONUP:
@ -145,6 +152,13 @@ void pollEvents( SDLPP::Scene &scene ) {
scene.setZIndex(obj, 1);
}
}
if(current_tool_box.getX() != -1) {
auto index = current_tool_index + current_tool_box.getY() * 4 + current_tool_box.getX();
if(index < tools.size()) {
current_block = index;
updateTool();
}
}
break;
case SDL_MOUSEBUTTONDOWN:
previous_selected_flags = current_selected_flags;
@ -262,6 +276,43 @@ int main() {
scene->addObject( mouse );
current_max_index = objects.size() - 18;
// tools
for(int i = 0; i < 4; i++) {
auto tool_box1 = std::make_shared<ToolBox>(i, 0, renderer);
auto tool_box2 = std::make_shared<ToolBox>(i, 1, renderer);
scene->addObject(tool_box1);
scene->addObject(tool_box2);
// std::cout << "TOOL BOX POS: " << tool_box1->getPos().getX() << ", " << tool_box1->getPos().getY() << std::endl;
// std::cout << "TOOL BOX POS: " << tool_box2->getPos().getX() << ", " << tool_box2->getPos().getY() << std::endl;
}
int tool_index = 0;
for(auto &block : possibleBlocks) {
tools.push_back(createTerrainBlock(block, OVERWORLD, renderer, false));
tools.back()->setHidden(true);
auto x = tool_index % 4;
auto y = tool_index / 4;
// TODO add 14 and 1 as constants somewhere
// TODO investigate
tools.back()->setPos(13*BLOCK_SIZE + x*BLOCK_SIZE, BLOCK_SIZE + y*BLOCK_SIZE);
// std::cout << "TOOL POS: " << tools.back()->getPos().getX() << ", " << tools.back()->getPos().getY() << std::endl;
scene->addObject(tools.back());
tool_index = (tool_index + 1) % 8;
}
for(int i = 0; i < 5; i++) {
auto line = std::make_shared<SDLPP::LineRenderer>(
14*BLOCK_SIZE + i*BLOCK_SIZE, BLOCK_SIZE, 14 * BLOCK_SIZE + i*BLOCK_SIZE, 3*BLOCK_SIZE, renderer, "#282828");
line->setPermanent();
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
scene->addObject(line);
}
for(int i = 0; i < 3; i++) {
auto line = std::make_shared<SDLPP::LineRenderer>(
14*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, 14 * BLOCK_SIZE + 4*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, renderer, "#282828");
line->setPermanent();
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
scene->addObject(line);
}
g_placeholder_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
current_tool = createTerrainBlock(possibleBlocks[current_block], OVERWORLD, renderer, g_placeholder_texture, false);
@ -276,6 +327,10 @@ int main() {
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 );
for(int i = current_tool_index; i < (tools.size() < current_tool_index + 8 ? tools.size() : current_tool_index + 8); i++) {
tools[i]->setHidden(false);
}
auto base = SDL_GetTicks();
int frames = 0;
std::thread inputThread( doInput, scene );

View File

@ -2,6 +2,7 @@
#include "../sdlpp/sdlpp_renderobject.hpp"
#include "objectids.hpp"
#include "edit_box.hpp"
#include "tool_box.hpp"
#define SELECTED_LEFT_MAP 0x00000001
#define SELECTED_RIGHT_MAP 0x00000002
@ -23,6 +24,10 @@ void MouseVisitor::visit( const SDLPP::RenderObject &obj ) {
edit_box = true;
edit_box_location = dynamic_cast<const EditBox&>(obj).getIndexes();
break;
case EDITOR_TOOL_ID:
tool_box = true;
tool_box_location = dynamic_cast<const ToolBox&>(obj).getIndexes();
break;
default:
break;
}

View File

@ -20,6 +20,12 @@ public:
SDLPP::Vec2D<int> getEditBoxIndexes() {
return edit_box_location;
}
bool foundToolBox() {
return tool_box;
}
SDLPP::Vec2D<int> getToolBoxIndexes() {
return tool_box_location;
}
virtual void setVisitorType( uint64_t type ) override {
_type = type;
}
@ -33,7 +39,9 @@ public:
private:
uint64_t select_flags = 0;
bool edit_box = false;
bool tool_box = false;
SDLPP::Vec2D<int> edit_box_location = {-1, -1};
SDLPP::Vec2D<int> tool_box_location = {-1, -1};
uint64_t _type;
};

View File

@ -34,6 +34,7 @@
#define EDITOR_LEFT_MAP_ID 0xF003
#define EDITOR_RIGHT_MAP_ID 0xF004
#define EDITOR_TERRAIN_ID 0xF005
#define EDITOR_TOOL_ID 0xF006
#define TOOL_VISITOR_TYPE 0xE001
#define MOUSE_VISITOR_TYPE 0xE002

23
mario/tool_box.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "tool_box.hpp"
#include "objectids.hpp"
#include "blocks.hpp"
#include "sprites.hpp"
ToolBox::ToolBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender(14*BLOCK_SIZE + x*BLOCK_SIZE, 1*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
_x = x;
_y = y;
setId(EDITOR_TOOL_ID);
setColiderColor("#FF00AA");
setColor("#FFFFFF88");
setPermanent();
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
addCollision(SDLPP::RectColider(0,0,1,1));
}
SDLPP::Vec2D<int> ToolBox::getIndexes() const {
return {_x, _y};
}
void ToolBox::visit( SDLPP::Visitor &visitor ) {
visitor.visit( *this );
}

15
mario/tool_box.hpp Normal file
View File

@ -0,0 +1,15 @@
#ifndef TOOL_BOX_H
#define TOOL_BOX_H
#include "../sdlpp/sdlpp.hpp"
class ToolBox : public SDLPP::RectangleRender {
public:
ToolBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer);
virtual SDLPP::Vec2D<int> getIndexes() const;
virtual void visit( SDLPP::Visitor &visitor ) override;
private:
int _x;
int _y;
};
#endif