Skip to content

Commit

Permalink
Moved the world decor objects editing code to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Feb 7, 2025
1 parent 152ba38 commit 023b9cc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 76 deletions.
2 changes: 2 additions & 0 deletions src/worldeditor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ add_executable(smw-worldedit WIN32
EditorPathSprites.cpp
EditorStageMarkers.h
EditorStageMarkers.cpp
EditorStructures.h
EditorStructures.cpp
EditorTileType.h
EditorTileType.cpp
EditorVehicleBoundaries.h
Expand Down
50 changes: 50 additions & 0 deletions src/worldeditor/EditorStructures.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "EditorStructures.h"

#include "Helpers.h"
#include "ResourceManager.h"
#include "world.h"


void EditorStructures::onSetupKeypress(const SDL_KeyboardEvent& event)
{
switch (event.keysym.sym) {
case SDLK_ESCAPE:
newlyEntered = false;
return;
}
}


void EditorStructures::onSetupMouseClick(const SDL_MouseButtonEvent& event)
{
if (event.button != SDL_BUTTON_LEFT)
return;

const short tileX = event.x / TILESIZE;
const short tileY = event.y / TILESIZE;

if (0 <= tileY && tileY < 15) {
if (0 <= tileX && tileX < 12) {
m_selectedTileId = WORLD_FOREGROUND_SPRITE_OFFSET + tileX + tileY * 12;
newlyEntered = false;
} else if (12 <= tileX && tileX < 14) {
m_selectedTileId = WORLD_FOREGROUND_SPRITE_ANIMATED_OFFSET + tileY + (tileX - 12) * 15;
newlyEntered = false;
}
}
}


void EditorStructures::renderSetup(CResourceManager& rm)
{
rm.spr_worldforeground[0].draw(0, 0, 0, 0, 416, 480);
rm.spr_worldforeground[0].draw(416, 0, 512, 0, 32, 480);
}


bool EditorStructures::onTileClicked(WorldMap& world, Vec2s pos, uint8_t button)
{
WorldMapTile& tile = world.getTiles().at(pos.x, pos.y);
const short newTileId = (button == SDL_BUTTON_LEFT) ? m_selectedTileId : 0;
return setTileFgWithConnection(tile, newTileId);
}
16 changes: 16 additions & 0 deletions src/worldeditor/EditorStructures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "EditorBase.h"


class EditorStructures : public EditorBase {
protected:
bool onTileClicked(WorldMap& world, Vec2s pos, uint8_t button) override;
void renderSetup(CResourceManager& rm) override;

void onSetupKeypress(const SDL_KeyboardEvent& event) override;
void onSetupMouseClick(const SDL_MouseButtonEvent& event) override;

private:
short m_selectedTileId = 0;
};
83 changes: 7 additions & 76 deletions src/worldeditor/worldeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ extern "C" FILE* __cdecl __iob_func(void) { return _iob; }

#include "EditorBackground.h"
#include "EditorBridges.h"
#include "EditorStageMarkers.h"
#include "EditorPaths.h"
#include "EditorPathSprites.h"
#include "EditorStageMarkers.h"
#include "EditorStructures.h"
#include "EditorTileType.h"
#include "EditorVehicleBoundaries.h"
#include "EditorWater.h"
Expand Down Expand Up @@ -309,7 +310,6 @@ int new_world();
int resize_world();

int editor_edit();
int editor_structureforeground();
int editor_vehicles();
int editor_stage();
int editor_start_items();
Expand All @@ -319,16 +319,18 @@ EditorBridges editorBridges;
EditorPaths editorPaths;
EditorPathSprites editorPathSprites;
EditorStageMarkers editorStageMarkers;
EditorStructures editorStructures;
EditorTileType editorTileType;
EditorVehicleBoundaries editorVehicleBoundaries;
EditorWater editorWater;
EditorWarps editorWarps;
constexpr std::array<EditorBase*, 9> allEditors {
constexpr std::array<EditorBase*, 10> allEditors {
&editorBackground,
&editorBridges,
&editorPaths,
&editorPathSprites,
&editorStageMarkers,
&editorStructures,
&editorTileType,
&editorVehicleBoundaries,
&editorWarps,
Expand Down Expand Up @@ -1002,7 +1004,8 @@ int main(int argc, char* argv[])
break;

case EDITOR_STRUCTUREFOREGROUND:
state = editor_structureforeground();
state = enterEditor(editorStructures);
edit_mode = 1;
break;

case EDITOR_BRIDGES:
Expand Down Expand Up @@ -1972,78 +1975,6 @@ int enterEditor(EditorBase& editor)
return EDITOR_QUIT;
}

int editor_structureforeground()
{
bool done = false;

while (!done) {
int framestart = SDL_GetTicks();

// handle messages
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
done = true;
break;
}

case SDL_KEYDOWN: {
edit_mode = 1;
return EDITOR_EDIT;

break;
}

case SDL_MOUSEBUTTONDOWN: {
if (event.button.button == SDL_BUTTON_LEFT) {
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;

if (iButtonY >= 0 && iButtonY < 15) {
if (iButtonX >= 0 && iButtonX < 12) {
set_tile = WORLD_FOREGROUND_SPRITE_OFFSET + iButtonX + iButtonY * 12;

ignoreclick = true;
edit_mode = 1;
return EDITOR_EDIT;
} else if (iButtonX >= 12 && iButtonX < 14) {
set_tile = WORLD_FOREGROUND_SPRITE_ANIMATED_OFFSET + iButtonY + (iButtonX - 12) * 15;

ignoreclick = true;
edit_mode = 1;
return EDITOR_EDIT;
}
}
}

break;
}

default:
break;
}
}

SDL_FillRect(screen, NULL, 0x0);

rm->spr_worldforeground[0].draw(0, 0, 0, 0, 416, 480);
rm->spr_worldforeground[0].draw(416, 0, 512, 0, 32, 480);

DrawMessage();
gfx_flipscreen();

int delay = WAITTIME - (SDL_GetTicks() - framestart);
if (delay < 0)
delay = 0;
else if (delay > WAITTIME)
delay = WAITTIME;

SDL_Delay(delay);
}

return EDITOR_QUIT;
}

// Display stages over vehicles
// allow setting of stages on vehicles

Expand Down

0 comments on commit 023b9cc

Please sign in to comment.