1407 lines
52 KiB
C++
1407 lines
52 KiB
C++
#include "SDL2/SDL_keyboard.h"
|
|
#include "editor_scenes.hpp"
|
|
#include "../../sdlpp/sdlpp.hpp"
|
|
#include "../gui/gui.hpp"
|
|
#include "../sprites.hpp"
|
|
#include "../global_vars.hpp"
|
|
#include "../objectids.hpp"
|
|
#include "../blocks.hpp"
|
|
#include "../maploader.hpp"
|
|
#include "../../sdlpp/sdlpp_mouse.hpp"
|
|
#include "../edit_box.hpp"
|
|
#include "../editor_visitor.hpp"
|
|
#include "../tool_box.hpp"
|
|
#include "../blocks/coineditorblock.hpp"
|
|
#include <SDL2/SDL_keycode.h>
|
|
#include <array>
|
|
#include "../filesystem.hpp"
|
|
#include "shared_scenes.hpp"
|
|
|
|
#ifdef _WIN32
|
|
#include "../sdlpp/SDL2/SDL2_framerate.h"
|
|
#include <ctime>
|
|
#include <string>
|
|
#include <windows.h>
|
|
#else
|
|
#include <SDL2/SDL2_framerate.h>
|
|
#endif // UNIX
|
|
|
|
#define MAP_WIDTH 24
|
|
#define MAP_HEIGHT 16
|
|
|
|
bool quit_editor = false;
|
|
|
|
#define UPDATE_FLAG 0x00000002
|
|
#define MAP_LEFT_ENABLED_FLAG 0x00000004
|
|
#define MAP_RIGHT_ENABLED_FLAG 0x00000008
|
|
#define TOOL_LEFT_ENABLED_FLAG 0x00000010
|
|
#define TOOL_RIGHT_ENABLED_FLAG 0x00000020
|
|
#define MOD_LEFT_ENABLED_FLAG 0x00000040
|
|
#define MOD_RIGHT_ENABLED_FLAG 0x00000080
|
|
#define CHARACTER_LEFT_ENABLED_FLAG 0x00000100
|
|
#define CHARACTER_RIGHT_ENABLED_FLAG 0x00000200
|
|
#define LOAD_MAP_FLAG 0x00000400
|
|
#define TEXT_UPDATE_FLAG 0x00000800
|
|
#define TEXT_INPUT_FLAG 0x00001000
|
|
#define STOP_TEXT_FLAG 0x00002000
|
|
|
|
#define TOOLS_WIDTH 4
|
|
#define CHARACTER_WIDTH 3
|
|
#define MOD_WIDTH 2
|
|
#define OVERWORLD_WIDTH 2
|
|
|
|
const std::string levelsDir = "levels";
|
|
bool map_changed = false;
|
|
|
|
struct ToolType {
|
|
enum Value {
|
|
BLOCK,
|
|
MOD,
|
|
CHARACTER,
|
|
WORLD,
|
|
};
|
|
};
|
|
|
|
struct MouseInfo {
|
|
uint64_t cur_flags{};
|
|
uint64_t prev_flags{};
|
|
SDLPP::Vec2D<int> edit_box;
|
|
SDLPP::Vec2D<int> tool_box;
|
|
ToolType::Value tool_type{};
|
|
uint64_t cur_button_index{};
|
|
};
|
|
|
|
struct MapInfo {
|
|
int cur_page;
|
|
int max_page;
|
|
};
|
|
|
|
struct ToolInfo {
|
|
ToolType::Value type;
|
|
uint64_t index;
|
|
SDL_Rect texture_src;
|
|
int cur_page_tools;
|
|
int max_page_tools;
|
|
int cur_page_mods;
|
|
int max_page_mods;
|
|
int cur_page_characters;
|
|
int max_page_characters;
|
|
std::shared_ptr<Button> button_left_tools;
|
|
std::shared_ptr<Button> button_right_tools;
|
|
std::shared_ptr<Button> button_left_mods;
|
|
std::shared_ptr<Button> button_right_mods;
|
|
std::shared_ptr<Button> button_left_characters;
|
|
std::shared_ptr<Button> button_right_characters;
|
|
};
|
|
|
|
struct GlobalVars {
|
|
MouseInfo mouse{};
|
|
MapInfo map{};
|
|
ToolInfo tool{};
|
|
uint64_t flags{};
|
|
std::vector<mapColumnType> objects;
|
|
std::vector<std::shared_ptr<MarioBlock>> tools;
|
|
std::vector<std::shared_ptr<MarioBlock>> mods;
|
|
std::vector<std::shared_ptr<MarioBlock>> 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::vector<std::shared_ptr<Button>> buttons;
|
|
enum LandType::Value current_world_type{};
|
|
std::shared_ptr<MarioBlock> coin_tool;
|
|
std::shared_ptr<MarioBlock> generic_tool;
|
|
std::shared_ptr<MarioBlock> current_tool;
|
|
std::shared_ptr<SDLPP::Texture> translucent_terrain_texture;
|
|
std::shared_ptr<SDLPP::Texture> translucent_mario_texture;
|
|
std::shared_ptr<SDLPP::Texture> translucent_mod_texture;
|
|
std::shared_ptr<SDLPP::Texture> translucent_enemies_texture;
|
|
std::shared_ptr<SDLPP::RenderObject> mario;
|
|
SDLPP::Vec2D<int> mario_pos;
|
|
};
|
|
|
|
struct moveStruct {
|
|
std::shared_ptr<SDLPP::Scene> scene;
|
|
std::shared_ptr<Button> other_button;
|
|
};
|
|
|
|
GlobalVars global_vars;
|
|
std::mutex render_mutex;
|
|
std::vector<moveStruct> arrowInputs{};
|
|
std::shared_ptr<SDLPP::Scene> editorScene;
|
|
SceneStruct mainMenuScene;
|
|
// SceneStruct fileChoiceScene;
|
|
|
|
std::shared_ptr<Button> level_name_object{};
|
|
std::string level_name_text = "<no_name>";
|
|
|
|
void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene,
|
|
const std::string &filename);
|
|
|
|
void setFlag(uint64_t flag) {
|
|
global_vars.flags |= flag;
|
|
}
|
|
void unsetFlag(uint64_t flag) {
|
|
global_vars.flags &= ~flag;
|
|
}
|
|
bool getFlag(uint64_t flag) {
|
|
return global_vars.flags & flag;
|
|
}
|
|
|
|
void saveMapCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
|
std::cout << "SAVING" << std::endl;
|
|
saveMap(levelsDir + FSLib::dir_divisor + level_name_text + ".marmap",
|
|
global_vars.objects);
|
|
map_changed = false;
|
|
game_scenes.pop_back();
|
|
}
|
|
void loadMapDialogCallback(const std::string &level_name) {
|
|
std::cout << "LOADING" << std::endl;
|
|
map_changed = false;
|
|
level_name_text = level_name.substr(0, level_name.length() - 7);
|
|
setFlag(LOAD_MAP_FLAG);
|
|
setFlag(TEXT_UPDATE_FLAG);
|
|
}
|
|
|
|
void updateTool() {
|
|
auto tool_index = global_vars.tool.index;
|
|
uint64_t tool_id = 0;
|
|
switch (global_vars.tool.type) {
|
|
case ToolType::BLOCK:
|
|
tool_id = possibleBlocks[tool_index];
|
|
break;
|
|
case ToolType::MOD:
|
|
tool_id = possibleMods[tool_index];
|
|
break;
|
|
case ToolType::CHARACTER:
|
|
tool_id = possibleCharacters[tool_index];
|
|
default:
|
|
break;
|
|
}
|
|
auto tool_role = getBlockRole(tool_id);
|
|
std::shared_ptr<SDLPP::Texture> target_texture = nullptr;
|
|
SDL_Rect target_src;
|
|
switch (tool_role) {
|
|
case BlockRole::TERRAIN:
|
|
target_texture = global_vars.translucent_terrain_texture;
|
|
target_src =
|
|
global_vars.tools[global_vars.tool.index]->getTextureSourceRect();
|
|
break;
|
|
case BlockRole::MARIO:
|
|
target_texture = global_vars.translucent_mario_texture;
|
|
target_src = global_vars.characters[global_vars.tool.index]
|
|
->getTextureSourceRect();
|
|
break;
|
|
case BlockRole::MODIFIER:
|
|
target_texture = global_vars.translucent_mod_texture;
|
|
target_src =
|
|
global_vars.mods[global_vars.tool.index]->getTextureSourceRect();
|
|
break;
|
|
case BlockRole::CHARACTER:
|
|
target_texture = global_vars.translucent_enemies_texture;
|
|
target_src = global_vars.characters[global_vars.tool.index]
|
|
->getTextureSourceRect();
|
|
break;
|
|
}
|
|
global_vars.current_tool->setHidden(true);
|
|
if (tool_id == COIN_MODIFIER_ID) {
|
|
global_vars.coin_tool->setPos(global_vars.current_tool->getPos());
|
|
global_vars.current_tool = global_vars.coin_tool;
|
|
} else {
|
|
global_vars.generic_tool->setPos(global_vars.current_tool->getPos());
|
|
global_vars.current_tool = global_vars.generic_tool;
|
|
global_vars.current_tool->setTexture(target_texture, target_src);
|
|
global_vars.current_tool->setId(tool_id);
|
|
global_vars.current_tool->getCollisions()[0]->setId(tool_id);
|
|
}
|
|
global_vars.current_tool->setHidden(false);
|
|
}
|
|
|
|
void removeMario() {
|
|
if (!global_vars.mario) {
|
|
return;
|
|
}
|
|
global_vars
|
|
.objects[global_vars.mario_pos.getX()][global_vars.mario_pos.getY()]
|
|
.unsetCharacter();
|
|
global_vars.mario->destroy();
|
|
}
|
|
|
|
void setToolColor(const std::string &color) {
|
|
std::vector<std::shared_ptr<SDLPP::RenderObject>> *store = nullptr;
|
|
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;
|
|
}
|
|
if (store == nullptr) {
|
|
return;
|
|
}
|
|
auto index = global_vars.tool.index % (2 * multiplier);
|
|
store->at(index)->setColor(color);
|
|
}
|
|
|
|
void setToolColor() {
|
|
setToolColor("#FFFF8888");
|
|
}
|
|
|
|
void unsetToolColor() {
|
|
setToolColor("#FFFFFF00");
|
|
}
|
|
|
|
void toolMoveUpdateButtons(Button *left, Button *right, int &cur_page,
|
|
int &max_page, bool canAdd) {
|
|
if (cur_page == 0) {
|
|
left->disable();
|
|
} else {
|
|
left->enable();
|
|
}
|
|
if (cur_page != max_page) {
|
|
if (canAdd) {
|
|
// TOOD global button config
|
|
right->changeText(">");
|
|
right->setFontColor("#000000");
|
|
right->setFontOutlineColor("#FFFFFF88");
|
|
right->setFontColorHighlight("#000000");
|
|
right->setFontOutlineColorHighlight("#FFFFFFAA");
|
|
} else {
|
|
right->enable();
|
|
}
|
|
} else {
|
|
if (canAdd) {
|
|
right->changeText("+");
|
|
right->setFontColor("#00FF00");
|
|
right->setFontOutlineColor("#000000");
|
|
right->setFontColorHighlight("#88FF88");
|
|
right->setFontOutlineColorHighlight("#444444");
|
|
} else {
|
|
right->disable();
|
|
}
|
|
}
|
|
}
|
|
|
|
void updateToolSelection(int prev_index, ToolType::Value type) {
|
|
unsetToolColor();
|
|
int cur_page = 0;
|
|
size_t multiplier = 0;
|
|
std::vector<std::shared_ptr<MarioBlock>> *tool_vec = nullptr;
|
|
switch (type) {
|
|
case ToolType::BLOCK:
|
|
cur_page = global_vars.tool.cur_page_tools;
|
|
multiplier = 2 * TOOLS_WIDTH;
|
|
tool_vec = &global_vars.tools;
|
|
toolMoveUpdateButtons(global_vars.tool.button_left_tools.get(),
|
|
global_vars.tool.button_right_tools.get(),
|
|
cur_page, global_vars.tool.max_page_tools, false);
|
|
break;
|
|
case ToolType::MOD:
|
|
cur_page = global_vars.tool.cur_page_mods;
|
|
multiplier = 2 * MOD_WIDTH;
|
|
tool_vec = &global_vars.mods;
|
|
toolMoveUpdateButtons(global_vars.tool.button_left_mods.get(),
|
|
global_vars.tool.button_right_mods.get(),
|
|
cur_page, global_vars.tool.max_page_mods, false);
|
|
break;
|
|
case ToolType::CHARACTER:
|
|
cur_page = global_vars.tool.cur_page_characters;
|
|
multiplier = 2 * CHARACTER_WIDTH;
|
|
tool_vec = &global_vars.characters;
|
|
toolMoveUpdateButtons(global_vars.tool.button_left_characters.get(),
|
|
global_vars.tool.button_right_characters.get(),
|
|
cur_page, global_vars.tool.max_page_characters,
|
|
false);
|
|
default:
|
|
break;
|
|
}
|
|
if (tool_vec == nullptr) {
|
|
return;
|
|
}
|
|
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);
|
|
i++) {
|
|
tool_vec->at(i)->setHidden(true);
|
|
}
|
|
for (size_t i = cur;
|
|
i < (tool_vec->size() < cur + multiplier ? tool_vec->size()
|
|
: cur + multiplier);
|
|
i++) {
|
|
tool_vec->at(i)->setHidden(false);
|
|
}
|
|
if (global_vars.tool.index / multiplier == static_cast<size_t>(cur_page)) {
|
|
setToolColor();
|
|
}
|
|
}
|
|
|
|
void moveToolsLeft(ToolType::Value type) {
|
|
switch (type) {
|
|
case ToolType::BLOCK:
|
|
global_vars.tool.cur_page_tools--;
|
|
updateToolSelection(global_vars.tool.cur_page_tools + 1, type);
|
|
break;
|
|
case ToolType::MOD:
|
|
global_vars.tool.cur_page_mods--;
|
|
updateToolSelection(global_vars.tool.cur_page_mods + 1, type);
|
|
break;
|
|
case ToolType::CHARACTER:
|
|
global_vars.tool.cur_page_characters--;
|
|
updateToolSelection(global_vars.tool.cur_page_characters + 1, type);
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void moveToolsRight(ToolType::Value type) {
|
|
switch (type) {
|
|
case ToolType::BLOCK:
|
|
global_vars.tool.cur_page_tools++;
|
|
updateToolSelection(global_vars.tool.cur_page_tools - 1, type);
|
|
break;
|
|
case ToolType::MOD:
|
|
global_vars.tool.cur_page_mods++;
|
|
updateToolSelection(global_vars.tool.cur_page_mods - 1, type);
|
|
break;
|
|
case ToolType::CHARACTER:
|
|
global_vars.tool.cur_page_characters++;
|
|
updateToolSelection(global_vars.tool.cur_page_characters - 1, type);
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void updateToolIndex(uint64_t new_index, ToolType::Value new_type) {
|
|
int multiplier = 0;
|
|
int *page = nullptr;
|
|
switch (new_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;
|
|
}
|
|
unsetToolColor();
|
|
global_vars.tool.type = new_type;
|
|
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 updateToolIndex(uint64_t new_index) {
|
|
updateToolIndex(new_index, global_vars.tool.type);
|
|
}
|
|
|
|
void selectLowerTool() {
|
|
int multiplier = 0;
|
|
size_t max_index = 0;
|
|
switch (global_vars.tool.type) {
|
|
case ToolType::BLOCK:
|
|
multiplier = 2 * TOOLS_WIDTH;
|
|
max_index = global_vars.tools.size() - 1;
|
|
break;
|
|
case ToolType::MOD:
|
|
multiplier = 2 * MOD_WIDTH;
|
|
max_index = global_vars.mods.size() - 1;
|
|
break;
|
|
case ToolType::CHARACTER:
|
|
multiplier = 2 * CHARACTER_WIDTH;
|
|
max_index = global_vars.characters.size() - 1;
|
|
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 ==
|
|
static_cast<uint64_t>(multiplier - 1)) {
|
|
addition = multiplier + 1;
|
|
}
|
|
if (global_vars.tool.index == max_index) {
|
|
return;
|
|
}
|
|
if (global_vars.tool.index + addition > max_index) {
|
|
addition = 1;
|
|
}
|
|
updateToolIndex(global_vars.tool.index + addition);
|
|
}
|
|
|
|
void updateWorld() {
|
|
for (auto &block : global_vars.tools) {
|
|
block->setType(global_vars.current_world_type);
|
|
}
|
|
for (auto &block : global_vars.characters) {
|
|
block->setType(global_vars.current_world_type);
|
|
}
|
|
if (getBlockRole(global_vars.current_tool->getId()) !=
|
|
BlockRole::MODIFIER) {
|
|
global_vars.current_tool->setType(global_vars.current_world_type);
|
|
}
|
|
}
|
|
|
|
// TODO add mouse wheel control for modifiers
|
|
void handleKeyUp(SDL_Keycode key, SDLPP::Scene &scene) {
|
|
switch (key) {
|
|
case SDLK_ESCAPE:
|
|
std::cout << "Eskape" << std::endl;
|
|
game_scenes.push_back(mainMenuScene);
|
|
break;
|
|
case SDLK_a:
|
|
selectPrevTool();
|
|
break;
|
|
case SDLK_d:
|
|
selectNextTool();
|
|
break;
|
|
case SDLK_w:
|
|
selectUpperTool();
|
|
break;
|
|
case SDLK_s:
|
|
selectLowerTool();
|
|
break;
|
|
case SDLK_1:
|
|
global_vars.current_world_type = possibleLands[0];
|
|
updateWorld();
|
|
break;
|
|
case SDLK_2:
|
|
global_vars.current_world_type = possibleLands[1];
|
|
updateWorld();
|
|
break;
|
|
case SDLK_3:
|
|
global_vars.current_world_type = possibleLands[2];
|
|
updateWorld();
|
|
break;
|
|
case SDLK_4:
|
|
global_vars.current_world_type = possibleLands[3];
|
|
updateWorld();
|
|
break;
|
|
case SDLK_r:
|
|
scene.getRenderer().setRenderColiders(
|
|
!scene.getRenderer().getRenderColiders());
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void getMousePositionFlags(SDLPP::Scene &scene) {
|
|
auto mouse = scene.getObjects({ EDITOR_MOUSE_ID })[0];
|
|
// move mouse colider to mouse position
|
|
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(
|
|
scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
|
|
|
|
MouseVisitor visitor;
|
|
scene.visitCollisions(*mouse, visitor);
|
|
if (visitor.getCurButton() != global_vars.mouse.cur_button_index) {
|
|
if (global_vars.mouse.cur_button_index != (uint64_t)-1) {
|
|
global_vars.buttons[global_vars.mouse.cur_button_index]
|
|
->unsetHighlight();
|
|
}
|
|
if (visitor.getCurButton() != (uint64_t)-1) {
|
|
global_vars.buttons[visitor.getCurButton()]->setHighlight();
|
|
}
|
|
}
|
|
global_vars.mouse.cur_button_index = visitor.getCurButton();
|
|
global_vars.mouse.cur_flags = visitor.getFlags();
|
|
// + 1 because the left map arrow is on position 0
|
|
global_vars.mouse.edit_box =
|
|
visitor.getEditBoxIndexes() + SDLPP::Vec2D<int>(1, 0);
|
|
global_vars.mouse.tool_box = visitor.getToolBoxIndexes();
|
|
global_vars.mouse.tool_type =
|
|
static_cast<ToolType::Value>(visitor.getToolType());
|
|
// if we found an edit box, move tool icon to that box
|
|
if (visitor.foundEditBox()) {
|
|
const auto &box = global_vars.mouse.edit_box;
|
|
global_vars.current_tool->setPos(box.getX() * BLOCK_SIZE,
|
|
1 - MAP_HEIGHT * BLOCK_SIZE +
|
|
box.getY() * BLOCK_SIZE);
|
|
}
|
|
}
|
|
|
|
void toolMoveLeft(Button *left, Button *right, int &cur_page, int &max_page,
|
|
bool canAdd) {
|
|
cur_page--;
|
|
toolMoveUpdateButtons(left, right, cur_page, max_page, canAdd);
|
|
}
|
|
void toolMoveRight(Button *left, Button *right, int &cur_page, int &max_page,
|
|
bool canAdd) {
|
|
cur_page += 1;
|
|
toolMoveUpdateButtons(left, right, cur_page, max_page, canAdd);
|
|
}
|
|
|
|
void moveMapLeft(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
actual_input->scene->moveEverything(BLOCK_SIZE, 0);
|
|
toolMoveLeft(caller, actual_input->other_button.get(),
|
|
global_vars.map.cur_page, global_vars.map.max_page, true);
|
|
}
|
|
|
|
void moveMapRight(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
if (global_vars.map.cur_page == global_vars.map.max_page) { // add column
|
|
global_vars.objects.resize(global_vars.objects.size() + 1);
|
|
global_vars.map.max_page++;
|
|
}
|
|
toolMoveRight(actual_input->other_button.get(), caller,
|
|
global_vars.map.cur_page, global_vars.map.max_page, true);
|
|
actual_input->scene->moveEverything(-BLOCK_SIZE, 0);
|
|
}
|
|
|
|
void moveToolsLeftButton(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
toolMoveLeft(caller, actual_input->other_button.get(),
|
|
global_vars.tool.cur_page_tools,
|
|
global_vars.tool.max_page_tools, false);
|
|
updateToolSelection(global_vars.tool.cur_page_tools + 1, ToolType::BLOCK);
|
|
}
|
|
|
|
void moveToolsRightButton(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
toolMoveRight(actual_input->other_button.get(), caller,
|
|
global_vars.tool.cur_page_tools,
|
|
global_vars.tool.max_page_tools, false);
|
|
updateToolSelection(global_vars.tool.cur_page_tools - 1, ToolType::BLOCK);
|
|
}
|
|
|
|
void moveModsLeft(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
toolMoveLeft(caller, actual_input->other_button.get(),
|
|
global_vars.tool.cur_page_mods, global_vars.tool.max_page_mods,
|
|
false);
|
|
updateToolSelection(global_vars.tool.cur_page_tools + 1, ToolType::MOD);
|
|
}
|
|
|
|
void moveModsRight(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
toolMoveRight(actual_input->other_button.get(), caller,
|
|
global_vars.tool.cur_page_mods,
|
|
global_vars.tool.max_page_mods, false);
|
|
updateToolSelection(global_vars.tool.cur_page_tools - 1, ToolType::MOD);
|
|
}
|
|
|
|
void moveCharsLeft(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
toolMoveLeft(caller, actual_input->other_button.get(),
|
|
global_vars.tool.cur_page_characters,
|
|
global_vars.tool.max_page_characters, false);
|
|
updateToolSelection(global_vars.tool.cur_page_tools + 1,
|
|
ToolType::CHARACTER);
|
|
}
|
|
|
|
void moveCharsRight(void *input, Button *caller) {
|
|
auto actual_input = static_cast<moveStruct *>(input);
|
|
toolMoveRight(actual_input->other_button.get(), caller,
|
|
global_vars.tool.cur_page_characters,
|
|
global_vars.tool.max_page_characters, false);
|
|
updateToolSelection(global_vars.tool.cur_page_tools - 1,
|
|
ToolType::CHARACTER);
|
|
}
|
|
|
|
void mouseUpAction(uint64_t flags) {
|
|
if (MouseVisitor::button(flags)) {
|
|
global_vars.buttons[global_vars.mouse.cur_button_index]
|
|
->performFunction();
|
|
}
|
|
}
|
|
|
|
SDLPP::Vec2D<int> getSelectedObjectIndexes() {
|
|
// -1 because we're indexing edit boxes from 1 (due to left arrow on map)
|
|
return global_vars.mouse.edit_box +
|
|
SDLPP::Vec2D<int>(global_vars.map.cur_page - 1, 0);
|
|
}
|
|
|
|
MapObject &getSelectedObject() {
|
|
auto pos = getSelectedObjectIndexes();
|
|
return global_vars.objects[pos.getX()][pos.getY()];
|
|
}
|
|
|
|
void placeTool(SDLPP::Scene &scene) {
|
|
std::lock_guard<std::mutex> lock(render_mutex);
|
|
map_changed = true;
|
|
|
|
ToolVisitor visitor;
|
|
visitor.setSourceType(global_vars.current_world_type);
|
|
visitor.setSourceData(global_vars.current_tool->getData());
|
|
auto tool_type = getBlockRole(global_vars.current_tool->getId());
|
|
switch (tool_type) {
|
|
case BlockRole::TERRAIN:
|
|
visitor.setVisitorType(VisitorType::Terrain);
|
|
break;
|
|
case BlockRole::CHARACTER:
|
|
case BlockRole::MARIO:
|
|
visitor.setVisitorType(VisitorType::Character);
|
|
break;
|
|
default:
|
|
visitor.setVisitorType(VisitorType::Modifier);
|
|
break;
|
|
}
|
|
|
|
scene.visitCollisions(*global_vars.current_tool, visitor);
|
|
auto &obj = getSelectedObject();
|
|
if (visitor.removeBlock() && !visitor.addBlock()) {
|
|
switch (visitor.getVisitorType()) {
|
|
case VisitorType::Terrain:
|
|
obj.unsetTerrain();
|
|
break;
|
|
case VisitorType::Character:
|
|
obj.unsetCharacter();
|
|
break;
|
|
case VisitorType::Modifier:
|
|
obj.unsetModifier();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} else if (visitor.addBlock()) {
|
|
auto renderer = scene.getRendererShared();
|
|
uint64_t z_index = 1;
|
|
std::shared_ptr<MarioBlock> new_obj = nullptr;
|
|
switch (visitor.getVisitorType()) {
|
|
case VisitorType::Terrain:
|
|
obj.setTerrain(global_vars.current_tool->getId(),
|
|
global_vars.current_world_type);
|
|
new_obj = createTerrainBlock(
|
|
obj.getTerrainId(), obj.getTerrainType(), renderer,
|
|
global_vars.mouse.edit_box.getX(),
|
|
global_vars.mouse.edit_box.getY(), false, true);
|
|
new_obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
|
|
break;
|
|
case VisitorType::Character:
|
|
if (tool_type == BlockRole::MARIO) {
|
|
obj.setCharacter(MARIO_ID, global_vars.current_world_type);
|
|
new_obj = createMario(global_vars.current_world_type, renderer,
|
|
global_vars.mouse.edit_box.getX(),
|
|
global_vars.mouse.edit_box.getY(), true);
|
|
// remove mario if exists
|
|
removeMario();
|
|
global_vars.mario = new_obj;
|
|
global_vars.mario_pos = getSelectedObjectIndexes();
|
|
new_obj->getCollisions()[0]->setId(EDITOR_CHARACTER_ID);
|
|
z_index = scene.getObjects().size() - 1;
|
|
} else {
|
|
obj.setCharacter(global_vars.current_tool->getId(),
|
|
global_vars.current_world_type);
|
|
new_obj = createTerrainBlock(
|
|
obj.getCharacterId(), obj.getCharacterType(), renderer,
|
|
global_vars.mouse.edit_box.getX(),
|
|
global_vars.mouse.edit_box.getY(), false, true);
|
|
new_obj->getCollisions()[0]->setId(EDITOR_CHARACTER_ID);
|
|
dynamic_cast<MarioBlock *>(new_obj.get())->setTerrain(false);
|
|
z_index = scene.getObjects().size() - 1;
|
|
}
|
|
break;
|
|
case VisitorType::Modifier:
|
|
obj.setModifier(global_vars.current_tool->getId(),
|
|
global_vars.current_tool->getData());
|
|
new_obj = createTerrainBlock(
|
|
obj.getModifierId(), LandType::OVERWORLD, renderer,
|
|
global_vars.mouse.edit_box.getX(),
|
|
global_vars.mouse.edit_box.getY(), false, true);
|
|
new_obj->setTextureKeepSRC(global_vars.translucent_mod_texture);
|
|
new_obj->setData(global_vars.current_tool->getData());
|
|
new_obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
|
|
// TODO createModifierBlock
|
|
dynamic_cast<MarioBlock *>(new_obj.get())->setTerrain(false);
|
|
z_index = scene.getObjects().size() - 1;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
scene.addObject(new_obj);
|
|
scene.setZIndex(new_obj, z_index);
|
|
}
|
|
}
|
|
|
|
bool controlOrCommandPressed() {
|
|
return SDL_GetModState() & KMOD_CTRL || SDL_GetModState() & KMOD_GUI;
|
|
}
|
|
|
|
void pollEvents(std::shared_ptr<SDLPP::Scene> &scene) {
|
|
SDL_Event event;
|
|
while (SDLPP::getSDLEvent(event)) {
|
|
switch (event.type) {
|
|
case SDL_QUIT:
|
|
quit_editor = true;
|
|
break;
|
|
case SDL_KEYUP:
|
|
if (!getFlag(TEXT_INPUT_FLAG) && !controlOrCommandPressed()) {
|
|
handleKeyUp(event.key.keysym.sym, *scene);
|
|
} else if (!controlOrCommandPressed()) {
|
|
if (event.key.keysym.sym == SDLK_ESCAPE ||
|
|
event.key.keysym.sym == SDLK_RETURN) {
|
|
setFlag(STOP_TEXT_FLAG);
|
|
}
|
|
}
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
if (getFlag(TEXT_INPUT_FLAG)) {
|
|
if (event.key.keysym.sym == SDLK_BACKSPACE &&
|
|
!level_name_text.empty()) {
|
|
level_name_text.pop_back();
|
|
setFlag(TEXT_UPDATE_FLAG);
|
|
} else if (event.key.keysym.sym == SDLK_c &&
|
|
controlOrCommandPressed()) {
|
|
// handle copy
|
|
SDL_SetClipboardText(level_name_text.c_str());
|
|
} else if (event.key.keysym.sym == SDLK_v &&
|
|
controlOrCommandPressed()) {
|
|
// handle paste
|
|
level_name_text += SDL_GetClipboardText();
|
|
setFlag(TEXT_UPDATE_FLAG);
|
|
}
|
|
} else if (event.key.keysym.sym == SDLK_s &&
|
|
controlOrCommandPressed()) {
|
|
saveMapCallback(nullptr, nullptr);
|
|
}
|
|
break;
|
|
case SDL_WINDOWEVENT:
|
|
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
|
setFlag(UPDATE_FLAG);
|
|
}
|
|
break;
|
|
case SDL_MOUSEMOTION:
|
|
getMousePositionFlags(*scene);
|
|
break;
|
|
case SDL_MOUSEBUTTONUP:
|
|
if (global_vars.mouse.cur_flags == global_vars.mouse.prev_flags) {
|
|
mouseUpAction(global_vars.mouse.cur_flags);
|
|
}
|
|
if (global_vars.mouse.edit_box.getX() != 0) {
|
|
placeTool(*scene);
|
|
}
|
|
if (global_vars.mouse.tool_box.getX() != -1) {
|
|
auto &tool_box = global_vars.mouse.tool_box;
|
|
int multiplier = 0;
|
|
size_t max_index = 0;
|
|
size_t cur_page = 0;
|
|
switch (global_vars.mouse.tool_type) {
|
|
case ToolType::BLOCK:
|
|
multiplier = 2 * TOOLS_WIDTH;
|
|
max_index = global_vars.tools.size();
|
|
cur_page = global_vars.tool.cur_page_tools;
|
|
break;
|
|
case ToolType::MOD:
|
|
multiplier = 2 * MOD_WIDTH;
|
|
max_index = global_vars.mods.size();
|
|
cur_page = global_vars.tool.cur_page_mods;
|
|
break;
|
|
case ToolType::CHARACTER:
|
|
multiplier = 2 * CHARACTER_WIDTH;
|
|
max_index = global_vars.characters.size();
|
|
cur_page = global_vars.tool.cur_page_characters;
|
|
break;
|
|
case ToolType::WORLD:
|
|
multiplier = 2 * OVERWORLD_WIDTH;
|
|
default:
|
|
break;
|
|
}
|
|
size_t index =
|
|
tool_box.getY() * (multiplier / 2) + tool_box.getX();
|
|
if (global_vars.mouse.tool_type == ToolType::WORLD) {
|
|
global_vars.current_world_type = possibleLands[index];
|
|
updateWorld();
|
|
} else if (index < max_index) {
|
|
updateToolIndex(cur_page * multiplier + index,
|
|
global_vars.mouse.tool_type);
|
|
}
|
|
}
|
|
break;
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
// store current mouse flags in previous mouse flags
|
|
global_vars.mouse.prev_flags = global_vars.mouse.cur_flags;
|
|
break;
|
|
case SDL_MOUSEWHEEL:
|
|
if (event.wheel.y > 0) {
|
|
global_vars.current_tool->onScrollUp();
|
|
} else if (event.wheel.y < 0) {
|
|
global_vars.current_tool->onScrollDown();
|
|
}
|
|
break;
|
|
case SDL_TEXTINPUT:
|
|
if (getFlag(TEXT_INPUT_FLAG)) {
|
|
level_name_text += event.text.text;
|
|
setFlag(TEXT_UPDATE_FLAG);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 width = count_x * BLOCK_SIZE;
|
|
auto height = count_y * BLOCK_SIZE;
|
|
for (int i = 0; i < count_x + 1; i++) {
|
|
auto line_vertical = std::make_shared<SDLPP::LineRenderer>(
|
|
start_x + i * BLOCK_SIZE, start_y, start_x + i * BLOCK_SIZE,
|
|
start_y + height, renderer, "#282828");
|
|
line_vertical->setPermanent();
|
|
line_vertical->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
scene->addObject(line_vertical);
|
|
}
|
|
for (int i = 0; i < count_y + 1; i++) {
|
|
auto line_horizontal = std::make_shared<SDLPP::LineRenderer>(
|
|
start_x, start_y + i * BLOCK_SIZE, start_x + width,
|
|
start_y + i * BLOCK_SIZE, renderer, "#282828");
|
|
line_horizontal->setPermanent();
|
|
line_horizontal->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
scene->addObject(line_horizontal);
|
|
}
|
|
}
|
|
|
|
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<MarioBlock>> &tool_store,
|
|
std::vector<std::shared_ptr<SDLPP::RenderObject>> &tool_box_store,
|
|
std::shared_ptr<SDLPP::Scene> &scene, const std::string &title,
|
|
const std::shared_ptr<SDLPP::FontConfiguration> &font_config) {
|
|
tool_store.clear();
|
|
tool_box_store.clear();
|
|
|
|
auto renderer = scene->getRendererShared();
|
|
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, false);
|
|
scene->addObject(tool_box);
|
|
}
|
|
}
|
|
int tool_index = 0;
|
|
for (auto &block : blocks) {
|
|
switch (type) {
|
|
case ToolType::CHARACTER:
|
|
if (block == MARIO_ID) {
|
|
tool_store.push_back(createMario(global_vars.current_world_type,
|
|
renderer, 0, 0, true));
|
|
break;
|
|
}
|
|
// fall through
|
|
case ToolType::MOD:
|
|
tool_store.push_back(createTerrainBlock(
|
|
block, global_vars.current_world_type, renderer, false, true));
|
|
break;
|
|
case ToolType::BLOCK:
|
|
tool_store.push_back(createTerrainBlock(
|
|
block, global_vars.current_world_type, renderer, false, true));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
tool_store.back()->setHidden(true);
|
|
tool_store.back()->setPermanent();
|
|
tool_store.back()->pauseAnimation();
|
|
auto x = tool_index % count_x;
|
|
auto y = tool_index / count_x;
|
|
tool_store.back()->setPos(start_x + x * BLOCK_SIZE,
|
|
start_y + y * BLOCK_SIZE);
|
|
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);
|
|
}
|
|
}
|
|
auto tool_text = std::make_shared<SDLPP::TextRenderer>(
|
|
start_x - BLOCK_SIZE, start_y - BLOCK_SIZE, (count_x + 2) * BLOCK_SIZE,
|
|
BLOCK_SIZE, renderer, title, font_config, SDLPP_TEXT_CENTER);
|
|
tool_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
tool_text->setPermanent();
|
|
scene->addObject(tool_text);
|
|
}
|
|
|
|
void populateWorldType(
|
|
int count_x, int count_y, float start_x, float start_y,
|
|
std::shared_ptr<SDLPP::Scene> &scene, const std::string &title,
|
|
const std::shared_ptr<SDLPP::FontConfiguration> &font_config) {
|
|
auto renderer = scene->getRendererShared();
|
|
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(ToolType::WORLD);
|
|
scene->addObject(tool_box);
|
|
}
|
|
}
|
|
int tool_index = 0;
|
|
for (auto &type : possibleLands) {
|
|
auto land = createTerrainBlock(BRICK_ID, type, renderer, false, true);
|
|
auto x = tool_index % count_x;
|
|
auto y = tool_index / count_x;
|
|
land->setId(EDITOR_WORLD_CHANGE_ID);
|
|
land->setPermanent();
|
|
land->setPos(start_x + x * BLOCK_SIZE, start_y + y * BLOCK_SIZE);
|
|
scene->addObject(land);
|
|
tool_index = (tool_index + 1) % (2 * count_x);
|
|
}
|
|
auto tool_text = std::make_shared<SDLPP::TextRenderer>(
|
|
start_x, start_y - BLOCK_SIZE, count_x * BLOCK_SIZE, BLOCK_SIZE,
|
|
renderer, title, font_config, SDLPP_TEXT_CENTER);
|
|
tool_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
tool_text->setPermanent();
|
|
scene->addObject(tool_text);
|
|
}
|
|
|
|
void enableTextInput(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
|
setFlag(TEXT_INPUT_FLAG);
|
|
SDL_StartTextInput();
|
|
}
|
|
|
|
void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene,
|
|
const std::string &filename) {
|
|
auto renderer = scene->getRendererShared();
|
|
auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36);
|
|
auto font_config = std::make_shared<SDLPP::FontConfiguration>(
|
|
font, "#000000", "#282828", 0.05);
|
|
auto bg = std::make_shared<SDLPP::RectangleRender>(
|
|
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true);
|
|
bg->setPermanent();
|
|
bg->setId(1);
|
|
scene->addObject(bg);
|
|
|
|
global_vars.current_world_type = LandType::OVERWORLD;
|
|
|
|
if (filename.empty() || !FSLib::exists(filename)) {
|
|
loadEmptyMap(global_vars.objects, MAP_WIDTH);
|
|
} else {
|
|
loadMap(scene, global_vars.mario, filename, global_vars.objects, true,
|
|
MAP_WIDTH);
|
|
for (int i = 0; i < global_vars.objects.size(); i++) {
|
|
for (int j = 0; j < 16; j++) {
|
|
if (global_vars.objects[i][j].hasCharacter() &&
|
|
global_vars.objects[i][j].getCharacterId() == MARIO_ID) {
|
|
global_vars.mario_pos = { i, j };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// map
|
|
for (int i = 0; i < MAP_WIDTH; i++) {
|
|
for (int j = 0; j < MAP_HEIGHT; j++) {
|
|
scene->addObject(std::make_shared<EditBox>(
|
|
i, j, BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH,
|
|
MAP_HEIGHT, renderer));
|
|
}
|
|
}
|
|
createGrid(BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH, MAP_HEIGHT,
|
|
scene);
|
|
// tools
|
|
populateToolGrid(TOOLS_WIDTH, 2, (MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE,
|
|
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, ToolType::BLOCK,
|
|
possibleBlocks, global_vars.tools, global_vars.tool_boxes,
|
|
scene, "TERRAIN", font_config);
|
|
createGrid((MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE,
|
|
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, TOOLS_WIDTH, 2, scene);
|
|
// mods
|
|
populateToolGrid(MOD_WIDTH, 2, 5 * BLOCK_SIZE,
|
|
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, ToolType::MOD,
|
|
possibleMods, global_vars.mods, global_vars.mod_boxes,
|
|
scene, "MODS", font_config);
|
|
createGrid(5 * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, MOD_WIDTH, 2,
|
|
scene);
|
|
// characters
|
|
populateToolGrid(CHARACTER_WIDTH, 2, (MOD_WIDTH + 8) * BLOCK_SIZE,
|
|
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, ToolType::CHARACTER,
|
|
possibleCharacters, global_vars.characters,
|
|
global_vars.character_boxes, scene, "CHARACTERS",
|
|
font_config);
|
|
createGrid((MOD_WIDTH + 8) * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE,
|
|
CHARACTER_WIDTH, 2, scene);
|
|
|
|
// world type
|
|
populateWorldType(OVERWORLD_WIDTH, 2, BLOCK_SIZE,
|
|
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, scene, "WORLD",
|
|
font_config);
|
|
createGrid(BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, OVERWORLD_WIDTH,
|
|
2, scene);
|
|
|
|
global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH;
|
|
// arrowInputs[1] - rightMapInput, arrowInputs[0] - leftMapInput
|
|
toolMoveUpdateButtons(
|
|
arrowInputs[1].other_button.get(), arrowInputs[0].other_button.get(),
|
|
global_vars.map.cur_page, global_vars.map.max_page, true);
|
|
|
|
auto mouse =
|
|
std::make_shared<SDLPP::RectangleRender>(0.01, 0.01, 0, 0, renderer);
|
|
mouse->setMinWidth(1);
|
|
mouse->setMinHeight(1);
|
|
mouse->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
mouse->setId(EDITOR_MOUSE_ID);
|
|
mouse->setColiderColor("#00FF00");
|
|
mouse->addCollision(SDLPP::RectColider({ 0, 0 }, { 1, 1 }));
|
|
scene->addObject(mouse);
|
|
|
|
global_vars.translucent_terrain_texture = g_translucent_terrain_texture;
|
|
global_vars.translucent_mario_texture = std::make_shared<SDLPP::Texture>(
|
|
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY);
|
|
global_vars.translucent_mario_texture->setAlpha(100);
|
|
global_vars.generic_tool =
|
|
createTerrainBlock(possibleBlocks[global_vars.tool.index],
|
|
global_vars.current_world_type, renderer, false);
|
|
global_vars.generic_tool->setTextureKeepSRC(
|
|
global_vars.translucent_terrain_texture);
|
|
global_vars.generic_tool->removeCollisions();
|
|
global_vars.generic_tool->addCollision(
|
|
SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
|
|
global_vars.current_tool = global_vars.generic_tool;
|
|
|
|
global_vars.coin_tool = createTerrainBlock(
|
|
COIN_MODIFIER_ID, LandType::OVERWORLD, renderer, 0, 0, false, true);
|
|
global_vars.coin_tool->setData(1);
|
|
global_vars.coin_tool->removeCollisions();
|
|
global_vars.coin_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
|
|
global_vars.translucent_mod_texture = g_translucent_mod_texture;
|
|
global_vars.translucent_enemies_texture = g_translucent_enemies_texture;
|
|
dynamic_cast<MarioBlock &>(*global_vars.current_tool).setTool();
|
|
scene->addObject(global_vars.current_tool);
|
|
scene->addObject(global_vars.coin_tool);
|
|
global_vars.coin_tool->setHidden(true);
|
|
scene->moveZTop(global_vars.current_tool);
|
|
scene->moveZTop(global_vars.coin_tool);
|
|
|
|
scene->moveEverything(BLOCK_SIZE, 0);
|
|
|
|
updateToolSelection(0, ToolType::BLOCK);
|
|
updateToolSelection(0, ToolType::MOD);
|
|
updateToolSelection(0, ToolType::CHARACTER);
|
|
setToolColor();
|
|
|
|
for (auto &button : global_vars.buttons) {
|
|
scene->addObject(button);
|
|
}
|
|
|
|
// level_name label
|
|
auto level_name_label = std::make_shared<SDLPP::TextRenderer>(
|
|
0.1, 0.1, BLOCK_SIZE * 8, 0.05, renderer, "Level Name:", font_config,
|
|
SDLPP_TEXT_LEFT);
|
|
level_name_label->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
level_name_label->setPermanent();
|
|
scene->addObject(level_name_label);
|
|
|
|
setFlag(UPDATE_FLAG);
|
|
global_vars.tool.cur_page_tools = 0;
|
|
global_vars.tool.cur_page_mods = 0;
|
|
global_vars.tool.cur_page_characters = 0;
|
|
}
|
|
|
|
std::shared_ptr<SDLPP::Scene>
|
|
createEditorMainScene(std::shared_ptr<SDLPP::Renderer> &renderer) {
|
|
auto scene = std::make_shared<SDLPP::Scene>(renderer);
|
|
|
|
global_vars.tool.max_page_tools =
|
|
(possibleBlocks.size() - 1) / (2 * TOOLS_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);
|
|
|
|
// create buttons
|
|
ButtonConfig default_button_theme{};
|
|
default_button_theme.bg_color = "#FFFFFF88";
|
|
default_button_theme.bg_color_highlight = "#FFFFFFBB";
|
|
default_button_theme.bg_color_disabled = "#AAAAAA88";
|
|
default_button_theme.font_color = "#000000";
|
|
default_button_theme.font_color_highlight = "#000000";
|
|
default_button_theme.font_color_disabled = "#555555";
|
|
default_button_theme.font_outline_color = "#FFFFFF88";
|
|
default_button_theme.font_outline_color_highlight = "#FFFFFFAA";
|
|
default_button_theme.font_outline_color_disabled = "#787878";
|
|
default_button_theme.outline = 0.1;
|
|
|
|
// left map arrow
|
|
arrowInputs.resize(8);
|
|
auto &left_map_input = arrowInputs[0];
|
|
left_map_input.scene = scene;
|
|
left_map_input.other_button = nullptr;
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
0, 1 - MAP_HEIGHT * BLOCK_SIZE, BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE,
|
|
renderer, "<", default_button_theme, moveMapLeft, &left_map_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
// right map arrow
|
|
auto &right_map_input = arrowInputs[1];
|
|
right_map_input.scene = scene;
|
|
right_map_input.other_button = global_vars.buttons.back();
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
(MAP_WIDTH + 1) * BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, BLOCK_SIZE,
|
|
MAP_HEIGHT * BLOCK_SIZE, renderer, ">", default_button_theme,
|
|
moveMapRight, &right_map_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
left_map_input.other_button = global_vars.buttons.back();
|
|
// ensure disabled/enabled properly
|
|
toolMoveUpdateButtons(
|
|
right_map_input.other_button.get(), global_vars.buttons.back().get(),
|
|
global_vars.map.cur_page, global_vars.map.max_page, true);
|
|
|
|
// left tool arrow
|
|
auto &left_tool_input = arrowInputs[2];
|
|
left_tool_input.scene = scene;
|
|
left_tool_input.other_button = nullptr;
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
(MAP_WIDTH - TOOLS_WIDTH - 1) * BLOCK_SIZE,
|
|
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, renderer,
|
|
"<", default_button_theme, moveToolsLeftButton, &left_tool_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
global_vars.tool.button_left_tools = global_vars.buttons.back();
|
|
// right tool arrow
|
|
auto &right_tool_input = arrowInputs[3];
|
|
right_tool_input.scene = scene;
|
|
right_tool_input.other_button = global_vars.buttons.back();
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
MAP_WIDTH * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, BLOCK_SIZE,
|
|
2 * BLOCK_SIZE, renderer, ">", default_button_theme,
|
|
moveToolsRightButton, &right_tool_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
left_tool_input.other_button = global_vars.buttons.back();
|
|
global_vars.tool.button_right_tools = global_vars.buttons.back();
|
|
// ensure disabled/enabled properly
|
|
toolMoveUpdateButtons(right_tool_input.other_button.get(),
|
|
global_vars.buttons.back().get(),
|
|
global_vars.tool.cur_page_tools,
|
|
global_vars.tool.max_page_tools, false);
|
|
|
|
// left mod arrow
|
|
auto &left_mod_input = arrowInputs[4];
|
|
left_mod_input.scene = scene;
|
|
left_mod_input.other_button = nullptr;
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
4 * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, BLOCK_SIZE,
|
|
2 * BLOCK_SIZE, renderer, "<", default_button_theme, moveModsLeft,
|
|
&left_mod_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
global_vars.tool.button_left_mods = global_vars.buttons.back();
|
|
// right mod arrow
|
|
auto &right_mod_input = arrowInputs[5];
|
|
right_mod_input.scene = scene;
|
|
right_mod_input.other_button = global_vars.buttons.back();
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
(MOD_WIDTH + 5) * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE,
|
|
BLOCK_SIZE, 2 * BLOCK_SIZE, renderer, ">", default_button_theme,
|
|
moveModsRight, &right_mod_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
left_mod_input.other_button = global_vars.buttons.back();
|
|
global_vars.tool.button_right_mods = global_vars.buttons.back();
|
|
// ensure disabled/enabled properly
|
|
toolMoveUpdateButtons(
|
|
right_mod_input.other_button.get(), global_vars.buttons.back().get(),
|
|
global_vars.tool.cur_page_mods, global_vars.tool.max_page_mods, false);
|
|
|
|
// left character arrow
|
|
auto &left_character_input = arrowInputs[6];
|
|
left_character_input.scene = scene;
|
|
left_character_input.other_button = nullptr;
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
(MOD_WIDTH + 7) * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE,
|
|
BLOCK_SIZE, 2 * BLOCK_SIZE, renderer, "<", default_button_theme,
|
|
moveCharsLeft, &left_character_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
global_vars.tool.button_left_characters = global_vars.buttons.back();
|
|
// right character arrow
|
|
auto &right_character_input = arrowInputs[7];
|
|
right_character_input.scene = scene;
|
|
right_character_input.other_button = global_vars.buttons.back();
|
|
global_vars.buttons.emplace_back(std::make_shared<Button>(
|
|
(MOD_WIDTH + 8 + CHARACTER_WIDTH) * BLOCK_SIZE,
|
|
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, renderer,
|
|
">", default_button_theme, moveCharsRight, &right_character_input));
|
|
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
SDLPP::OBJ_CENTER);
|
|
global_vars.buttons.back()->setPermanent();
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
global_vars.tool.button_right_characters = global_vars.buttons.back();
|
|
// ensure disabled/enabled properly
|
|
toolMoveUpdateButtons(right_character_input.other_button.get(),
|
|
global_vars.buttons.back().get(),
|
|
global_vars.tool.cur_page_characters,
|
|
global_vars.tool.max_page_characters, false);
|
|
left_character_input.other_button = global_vars.buttons.back();
|
|
|
|
// level name
|
|
level_name_object = std::make_shared<Button>(
|
|
0.1, 0.15, BLOCK_SIZE * 16, BLOCK_SIZE * 2, renderer, level_name_text,
|
|
default_button_theme, enableTextInput, nullptr);
|
|
level_name_object->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
level_name_object->setPermanent();
|
|
level_name_object->setColor("#FFFFFF88");
|
|
global_vars.buttons.push_back(level_name_object);
|
|
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
|
|
|
|
openMapEditor(scene, "");
|
|
|
|
return scene;
|
|
}
|
|
|
|
void quitEditor(bool actually_quit) {
|
|
g_quit = actually_quit;
|
|
}
|
|
|
|
void editorAdditionalRender(std::shared_ptr<SDLPP::Scene> &scene) {
|
|
if (quit_editor) {
|
|
if (map_changed) {
|
|
game_scenes.push_back(createYesNoScene(
|
|
scene->getRendererShared(),
|
|
"Map hasn't been saved. Actually quit?", quitEditor));
|
|
quit_editor = false;
|
|
} else {
|
|
quitEditor(true);
|
|
}
|
|
}
|
|
for (auto &button : global_vars.buttons) {
|
|
button->update();
|
|
}
|
|
if (getFlag(UPDATE_FLAG)) {
|
|
scene->updateSizeAndPosition();
|
|
mainMenuScene.scene->updateSizeAndPosition();
|
|
unsetFlag(UPDATE_FLAG);
|
|
}
|
|
if (getFlag(LOAD_MAP_FLAG)) {
|
|
editorScene->resetScene();
|
|
openMapEditor(editorScene, levelsDir + FSLib::dir_divisor +
|
|
level_name_text + ".marmap");
|
|
unsetFlag(LOAD_MAP_FLAG);
|
|
}
|
|
if (getFlag(TEXT_UPDATE_FLAG)) {
|
|
level_name_object->changeText(level_name_text);
|
|
unsetFlag(TEXT_UPDATE_FLAG);
|
|
setFlag(UPDATE_FLAG);
|
|
}
|
|
if (getFlag(STOP_TEXT_FLAG)) {
|
|
SDL_StopTextInput();
|
|
unsetFlag(TEXT_INPUT_FLAG);
|
|
unsetFlag(STOP_TEXT_FLAG);
|
|
}
|
|
}
|
|
|
|
SceneStruct createEditorScene(std::shared_ptr<SDLPP::Renderer> &renderer) {
|
|
SceneStruct ret{};
|
|
ret.scene = createEditorMainScene(renderer);
|
|
ret.doInput = pollEvents;
|
|
ret.additionalRender = editorAdditionalRender;
|
|
editorScene = ret.scene;
|
|
|
|
mainMenuScene = createEditorMainMenuScene(renderer, &quit_editor);
|
|
// fileChoiceScene = createEditorFileChoiceScene(renderer);
|
|
return ret;
|
|
}
|