Skip to content

Commit 71282cd

Browse files
committed
Moved the world tile type editing to a separate class
1 parent 44a353e commit 71282cd

File tree

5 files changed

+119
-136
lines changed

5 files changed

+119
-136
lines changed

src/worldeditor/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ add_executable(smw-worldedit WIN32
4545
EditorPaths.cpp
4646
EditorStageMarkers.h
4747
EditorStageMarkers.cpp
48+
EditorTileType.h
49+
EditorTileType.cpp
4850
EditorWater.h
4951
EditorWater.cpp
5052
Helpers.h

src/worldeditor/EditorBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class EditorBase {
2828
/// The editor is ready for editing the world map.
2929
bool isReady() const { return !newlyEntered; }
3030

31+
/// If transparent, will show the map behind the setup screen,
32+
/// otherwise it is filled with black.
33+
virtual bool isSetupTransparent() const { return false; }
34+
3135
virtual void loadAssets() {}
3236
virtual void onEnter();
3337
virtual bool onTileClicked(WorldMap& world, Vec2s pos, uint8_t button) = 0;

src/worldeditor/EditorTileType.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "EditorTileType.h"
2+
3+
#include "FileList.h"
4+
#include "ResourceManager.h"
5+
#include "world.h"
6+
7+
extern WorldList* worldlist; // from global.cpp
8+
9+
10+
void EditorTileType::onSetupKeypress(const SDL_KeyboardEvent& event)
11+
{
12+
switch (event.keysym.sym) {
13+
case SDLK_ESCAPE:
14+
newlyEntered = false;
15+
return;
16+
}
17+
}
18+
19+
20+
void EditorTileType::onSetupMouseClick(const SDL_MouseButtonEvent& event)
21+
{
22+
if (event.button != SDL_BUTTON_LEFT)
23+
return;
24+
25+
const short tileX = event.x / TILESIZE;
26+
const short tileY = event.y / TILESIZE;
27+
28+
if (0 <= tileX && tileX <= 5 && tileY == 0) {
29+
m_selectedTileId = tileX;
30+
newlyEntered = false;
31+
}
32+
}
33+
34+
35+
void EditorTileType::renderSetup(CResourceManager& rm)
36+
{
37+
rm.spr_worldforegroundspecial[0].draw(0, 0, 320, 128, 64, 32);
38+
rm.spr_worldforegroundspecial[0].draw(64, 0, 320, 192, 128, 32);
39+
rm.spr_worldforegroundspecial[0].draw(64, 0, 448, 64, 128, 32);
40+
rm.menu_font_small.drawRightJustified(640, 0, worldlist->currentPath().c_str());
41+
}
42+
43+
44+
bool EditorTileType::onTileClicked(WorldMap& world, Vec2s pos, uint8_t button)
45+
{
46+
WorldMapTile& tile = world.getTiles().at(pos.x, pos.y);
47+
bool changed = false;
48+
49+
if (button == SDL_BUTTON_LEFT) {
50+
if (m_selectedTileId <= 1) {
51+
if (tile.iForegroundSprite != m_selectedTileId + WORLD_START_SPRITE_OFFSET) {
52+
tile.iType = 1;
53+
tile.iForegroundSprite = m_selectedTileId + WORLD_START_SPRITE_OFFSET;
54+
changed = true;
55+
}
56+
} else if (m_selectedTileId <= 5) { // doors
57+
if (tile.iType != m_selectedTileId) {
58+
// if the door was placed on a start tile
59+
if (tile.iType == 1)
60+
tile.iForegroundSprite = 0;
61+
62+
tile.iType = m_selectedTileId;
63+
changed = true;
64+
}
65+
}
66+
} else if (button == SDL_BUTTON_RIGHT) {
67+
if (tile.iType == 1) {
68+
tile.iForegroundSprite = 0;
69+
changed = true;
70+
} else if (tile.iType <= 5) {
71+
tile.iType = 0;
72+
changed = true;
73+
}
74+
75+
tile.iType = 0;
76+
}
77+
78+
return changed;
79+
}

src/worldeditor/EditorTileType.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "EditorBase.h"
4+
5+
6+
class EditorTileType : public EditorBase {
7+
protected:
8+
bool isSetupTransparent() const override { return true; }
9+
10+
bool onTileClicked(WorldMap& world, Vec2s pos, uint8_t button) override;
11+
void renderSetup(CResourceManager& rm) override;
12+
13+
void onSetupKeypress(const SDL_KeyboardEvent& event) override;
14+
void onSetupMouseClick(const SDL_MouseButtonEvent& event) override;
15+
16+
private:
17+
short m_selectedTileId = 0;
18+
};

src/worldeditor/worldeditor.cpp

Lines changed: 16 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ extern "C" FILE* __cdecl __iob_func(void) { return _iob; }
7575
#include "EditorBackground.h"
7676
#include "EditorStageMarkers.h"
7777
#include "EditorPaths.h"
78+
#include "EditorTileType.h"
7879
#include "EditorWater.h"
7980
#include "Helpers.h"
8081

@@ -319,19 +320,20 @@ int editor_structureforeground();
319320
int editor_bridges();
320321
int editor_vehicles();
321322
int editor_pathsprite();
322-
int editor_type();
323323
int editor_stage();
324324
int editor_start_items();
325325

326326
EditorBackground editorBackground;
327327
EditorPaths editorPaths;
328328
EditorStageMarkers editorStageMarkers;
329+
EditorTileType editorTileType;
329330
EditorWater editorWater;
330331
EditorBase* currentEditor = nullptr;
331-
constexpr std::array<EditorBase*, 4> allEditors {
332+
constexpr std::array<EditorBase*, 5> allEditors {
332333
&editorBackground,
333334
&editorPaths,
334335
&editorStageMarkers,
336+
&editorTileType,
335337
&editorWater,
336338
};
337339
int enterEditor(EditorBase& editor);
@@ -1040,7 +1042,8 @@ int main(int argc, char* argv[])
10401042
break;
10411043

10421044
case EDITOR_TYPE:
1043-
state = editor_type();
1045+
state = enterEditor(editorTileType);
1046+
edit_mode = 3;
10441047
break;
10451048

10461049
case EDITOR_STAGE:
@@ -1403,25 +1406,7 @@ int editor_edit()
14031406
}
14041407

14051408
if (event.button.button == SDL_BUTTON_LEFT && !ignoreclick) {
1406-
if (edit_mode == 3) { // selected type
1407-
// start tiles
1408-
if (set_tile <= 1) {
1409-
if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != set_tile + WORLD_START_SPRITE_OFFSET) {
1410-
g_worldmap.tiles.at(iCol, iRow).iType = 1;
1411-
g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = set_tile + WORLD_START_SPRITE_OFFSET;
1412-
updateworldsurface();
1413-
}
1414-
} else if (set_tile <= 5) { // doors
1415-
if (g_worldmap.tiles.at(iCol, iRow).iType != set_tile) {
1416-
// if the door was placed on a start tile
1417-
if (g_worldmap.tiles.at(iCol, iRow).iType == 1)
1418-
g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0;
1419-
1420-
g_worldmap.tiles.at(iCol, iRow).iType = set_tile;
1421-
updateworldsurface();
1422-
}
1423-
}
1424-
} else if (edit_mode == 4) { // selected path sprite
1409+
if (edit_mode == 4) { // selected path sprite
14251410
short iAdjustedTile = AdjustForeground(set_tile, iCol, iRow);
14261411
bool fNeedUpdate = false;
14271412

@@ -1459,17 +1444,7 @@ int editor_edit()
14591444
g_worldmap.tiles.at(iCol, iRow).iType = set_tile;
14601445
}
14611446
} else if (event.button.button == SDL_BUTTON_RIGHT) {
1462-
if (edit_mode == 3) { // selected start/door
1463-
if (g_worldmap.tiles.at(iCol, iRow).iType == 1) {
1464-
g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0;
1465-
updateworldsurface();
1466-
} else if (g_worldmap.tiles.at(iCol, iRow).iType <= 5) {
1467-
g_worldmap.tiles.at(iCol, iRow).iType = 0;
1468-
updateworldsurface();
1469-
}
1470-
1471-
g_worldmap.tiles.at(iCol, iRow).iType = 0;
1472-
} else if (edit_mode == 4) {
1447+
if (edit_mode == 4) {
14731448
bool fNeedUpdate = false;
14741449

14751450
if (!fAutoPaint && g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != 0) {
@@ -1526,24 +1501,7 @@ int editor_edit()
15261501
}
15271502

15281503
if (event.motion.state == SDL_BUTTON(SDL_BUTTON_LEFT) && !ignoreclick) {
1529-
if (edit_mode == 3) { // selected stage/door
1530-
if (set_tile <= 1) {
1531-
if (g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != set_tile + WORLD_START_SPRITE_OFFSET) {
1532-
g_worldmap.tiles.at(iCol, iRow).iType = 1;
1533-
g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = set_tile + WORLD_START_SPRITE_OFFSET;
1534-
updateworldsurface();
1535-
}
1536-
} else if (set_tile <= 5) {
1537-
if (g_worldmap.tiles.at(iCol, iRow).iType != set_tile) {
1538-
// if the door was placed on a start tile
1539-
if (g_worldmap.tiles.at(iCol, iRow).iType == 1)
1540-
g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0;
1541-
1542-
g_worldmap.tiles.at(iCol, iRow).iType = set_tile;
1543-
updateworldsurface();
1544-
}
1545-
}
1546-
} else if (edit_mode == 4) {
1504+
if (edit_mode == 4) {
15471505
short iAdjustedTile = AdjustForeground(set_tile, iCol, iRow);
15481506
bool fNeedUpdate = false;
15491507

@@ -1579,17 +1537,7 @@ int editor_edit()
15791537
g_worldmap.tiles.at(iCol, iRow).iType = set_tile;
15801538
}
15811539
} else if (event.motion.state == SDL_BUTTON(SDL_BUTTON_RIGHT)) {
1582-
if (edit_mode == 3) {
1583-
if (g_worldmap.tiles.at(iCol, iRow).iType == 1) {
1584-
g_worldmap.tiles.at(iCol, iRow).iForegroundSprite = 0;
1585-
updateworldsurface();
1586-
} else if (g_worldmap.tiles.at(iCol, iRow).iType <= 5) {
1587-
g_worldmap.tiles.at(iCol, iRow).iType = 0;
1588-
updateworldsurface();
1589-
}
1590-
1591-
g_worldmap.tiles.at(iCol, iRow).iType = 0;
1592-
} else if (edit_mode == 4) {
1540+
if (edit_mode == 4) {
15931541
bool fNeedUpdate = false;
15941542

15951543
if (!fAutoPaint && g_worldmap.tiles.at(iCol, iRow).iForegroundSprite != 0) {
@@ -2415,79 +2363,6 @@ int editor_boundary()
24152363
return EDITOR_QUIT;
24162364
}
24172365

2418-
int editor_type()
2419-
{
2420-
bool done = false;
2421-
2422-
while (!done) {
2423-
int framestart = SDL_GetTicks();
2424-
2425-
// handle messages
2426-
while (SDL_PollEvent(&event)) {
2427-
switch (event.type) {
2428-
case SDL_QUIT: {
2429-
done = true;
2430-
break;
2431-
}
2432-
2433-
case SDL_KEYDOWN: {
2434-
edit_mode = 3; // change to edit mode using doors/start
2435-
return EDITOR_EDIT;
2436-
2437-
break;
2438-
}
2439-
2440-
case SDL_MOUSEBUTTONDOWN: {
2441-
if (event.button.button == SDL_BUTTON_LEFT) {
2442-
short iButtonX = bound_to_window_w(event.button.x) / TILESIZE;
2443-
short iButtonY = bound_to_window_h(event.button.y) / TILESIZE;
2444-
2445-
// Start and doors
2446-
if (iButtonX >= 0 && iButtonX <= 5 && iButtonY == 0) {
2447-
set_tile = iButtonX;
2448-
}
2449-
2450-
edit_mode = 3; // change to edit mode using warps
2451-
2452-
// The user must release the mouse button before trying to add a tile
2453-
ignoreclick = true;
2454-
2455-
return EDITOR_EDIT;
2456-
}
2457-
2458-
break;
2459-
}
2460-
2461-
default:
2462-
break;
2463-
}
2464-
}
2465-
2466-
2467-
drawmap(false, TILESIZE);
2468-
menu_shade.draw(0, 0);
2469-
2470-
rm->spr_worldforegroundspecial[0].draw(0, 0, 320, 128, 64, 32);
2471-
rm->spr_worldforegroundspecial[0].draw(64, 0, 320, 192, 128, 32);
2472-
2473-
rm->spr_worldforegroundspecial[0].draw(64, 0, 448, 64, 128, 32);
2474-
2475-
rm->menu_font_small.drawRightJustified(640, 0, worldlist->currentPath().c_str());
2476-
2477-
DrawMessage();
2478-
gfx_flipscreen();
2479-
2480-
int delay = WAITTIME - (SDL_GetTicks() - framestart);
2481-
if (delay < 0)
2482-
delay = 0;
2483-
else if (delay > WAITTIME)
2484-
delay = WAITTIME;
2485-
2486-
SDL_Delay(delay);
2487-
}
2488-
2489-
return EDITOR_QUIT;
2490-
}
24912366

24922367
int enterEditor(EditorBase& editor)
24932368
{
@@ -2510,7 +2385,12 @@ int enterEditor(EditorBase& editor)
25102385
if (editor.isReady())
25112386
return EDITOR_EDIT;
25122387

2513-
SDL_FillRect(screen, NULL, 0x0);
2388+
if (editor.isSetupTransparent()) {
2389+
drawmap(false, TILESIZE);
2390+
menu_shade.draw(0, 0);
2391+
} else {
2392+
SDL_FillRect(screen, NULL, 0x0);
2393+
}
25142394

25152395
editor.renderSetup(*rm);
25162396

0 commit comments

Comments
 (0)