Skip to content

Commit 9750a3f

Browse files
authored
Game Mode Switcher on World Creation Screen (#446)
1 parent 26924b6 commit 9750a3f

File tree

3 files changed

+96
-30
lines changed

3 files changed

+96
-30
lines changed

source/client/gui/screens/CreateWorldScreen.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static char g_CreateWorldFilterArray[] = { '/','\n','\r','\x09','\0','\xC','`','
1616
CreateWorldScreen::CreateWorldScreen() :
1717
m_textName(this, 1, 0, 0, 0, 0, "", "Unnamed world"),
1818
m_textSeed(this, 2, 0, 0, 0, 0, ""),
19+
m_btnGameMode(5, "Game Mode"),
1920
m_btnBack(3, "Cancel"),
2021
m_btnCreate(4, "Create New World")
2122
{
@@ -84,31 +85,40 @@ void CreateWorldScreen::_buttonClicked(Button* pButton)
8485
seed = Util::hashCode(seedThing);
8586
}
8687

87-
LevelSettings levelSettings(seed);
88+
LevelSettings levelSettings(seed, m_gameMode);
8889
m_pMinecraft->selectLevel(levelUniqueName, levelNickname, levelSettings);
8990
}
91+
92+
if (pButton->getId() == m_btnGameMode.getId())
93+
m_gameMode = static_cast<GameType>((static_cast<int>(m_gameMode) + 1) % (GAME_TYPES_MAX + 1));
9094
}
9195

92-
#define CRAMPED() (100 + 32 + 58 > m_height)
96+
#define CRAMPED() (100 + 32 + 58 + 16 > m_height)
9397

9498
void CreateWorldScreen::init()
9599
{
96-
m_textName.m_width = m_textSeed.m_width = 200;
100+
bool touchscreen = m_pMinecraft->isTouchscreen();
101+
97102
m_textName.m_height = m_textSeed.m_height = 20;
103+
104+
// provide thicker buttons for pressing on touchscreen and match the rest of the ui
105+
if (!touchscreen)
106+
m_btnBack.m_height = m_btnCreate.m_height = m_btnGameMode.m_height = m_textName.m_height = m_textSeed.m_height = 20;
107+
108+
m_textName.m_width = m_textSeed.m_width = 200;
98109
m_textName.m_xPos = m_textSeed.m_xPos = (m_width - 200) / 2;
99110
m_textName.m_yPos = 60;
100111
m_textSeed.m_yPos = 100;
101-
112+
m_btnGameMode.m_yPos = 140;
113+
m_btnGameMode.m_width = 150;
102114
m_btnCreate.m_yPos = m_height - 56;
103-
m_btnBack.m_yPos = m_height - 30;
104-
m_btnBack.m_width = m_btnCreate.m_width = 200;
105-
m_btnBack.m_height = m_btnCreate.m_height = 20;
106-
107-
m_btnBack.m_xPos = m_width / 2 - 200 / 2;
108-
m_btnCreate.m_xPos = m_width / 2 - 200 / 2;
115+
m_btnCreate.m_yPos = m_btnBack.m_yPos = m_height - 30 + ((touchscreen) ? 2 : 0); // on touchscreens, align the create/cancel buttons with previous screen's Y values
116+
m_btnBack.m_xPos = (m_width / 2) + 5;
117+
m_btnBack.m_width = m_btnCreate.m_width = 150;
109118

110119
_addElement(m_textName);
111120
_addElement(m_textSeed);
121+
_addElement(m_btnGameMode);
112122
_addElement(m_btnCreate);
113123
_addElement(m_btnBack);
114124
m_textName.init(m_pFont);
@@ -117,26 +127,46 @@ void CreateWorldScreen::init()
117127
// 100 - yPos of textSeed
118128
// 32 - offset + height of "Leave blank for random" text
119129
// 58 - approximately the Y position of the create button
130+
// 16 - extra offset for gamemode text to make sure it never overlaps with create new world / cancel
120131
bool crampedMode = CRAMPED();
121132
if (crampedMode)
122133
{
123134
// crush everything down as much as we can
124-
m_textName.m_yPos = 40;
125-
m_textSeed.m_yPos = 80;
126-
m_btnCreate.m_yPos += 10;
127-
m_btnBack.m_yPos += 5;
135+
m_textName.m_yPos = 34;
136+
m_textSeed.m_yPos = 69;
137+
m_btnGameMode.m_yPos = m_textSeed.m_yPos + 24;
138+
m_btnCreate.m_width -= 50;
139+
m_btnBack.m_width -= 50;
128140
}
141+
142+
// calculate after crush
143+
m_btnGameMode.m_xPos = m_width / 2 - m_btnGameMode.m_width / 2;
144+
m_btnCreate.m_xPos = m_width / 2 - m_btnCreate.m_width - 5;
145+
146+
m_gameMode = GAME_TYPE_SURVIVAL;
129147
}
130148

131149
void CreateWorldScreen::render(float f)
132150
{
133151
renderBackground();
134152
Screen::render(f);
135153

154+
const std::string gameTypeStr = GameTypeConv::GameTypeDescriptionToNonLocString(m_gameMode);
155+
m_btnGameMode.setMessage("Game Mode: " + GameTypeConv::GameTypeToNonLocString(m_gameMode));
156+
136157
drawCenteredString(*m_pFont, "Create New World", m_width / 2, CRAMPED() ? 10 : 30, 0xFFFFFF);
137158
drawString(*m_pFont, "World name", m_textName.m_xPos, m_textName.m_yPos - 10, 0xDDDDDD);
138159
drawString(*m_pFont, "Seed for the World Generator", m_textSeed.m_xPos, m_textSeed.m_yPos - 10, 0xDDDDDD);
160+
161+
bool crampedMode = CRAMPED();
162+
163+
// Extra text not necessarily needed
164+
if (crampedMode)
165+
return;
166+
139167
drawString(*m_pFont, "Leave blank for a random seed", m_textSeed.m_xPos, m_textSeed.m_yPos + 22, 0x999999);
168+
int textCenteredX = m_btnGameMode.m_xPos + (m_btnGameMode.m_width / 2) - (m_pFont->width(gameTypeStr.c_str()) / 2);
169+
drawString(*m_pFont, gameTypeStr.c_str(), textCenteredX, m_btnGameMode.m_yPos + m_btnGameMode.m_height + 3, 0x999999);
140170
}
141171

142172
bool CreateWorldScreen::handleBackEvent(bool b)
@@ -147,4 +177,4 @@ bool CreateWorldScreen::handleBackEvent(bool b)
147177
}
148178

149179
return true;
150-
}
180+
}

source/client/gui/screens/CreateWorldScreen.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class CreateWorldScreen : public Screen
3030
public:
3131
TextBox m_textName;
3232
TextBox m_textSeed;
33+
Button m_btnGameMode;
3334
Button m_btnBack;
3435
Button m_btnCreate;
36+
GameType m_gameMode;
3537
};
3638

3739
#endif

source/world/gamemode/GameType.hpp

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <string>
4+
#include "common/Util.hpp"
45

56
enum GameType
67
{
@@ -15,20 +16,53 @@ enum GameType
1516
class GameTypeConv
1617
{
1718
public:
18-
static std::string GameTypeToNonLocString(GameType type)
19-
{
20-
switch (type)
21-
{
22-
case GAME_TYPE_SURVIVAL:
23-
return "Survival";
24-
case GAME_TYPE_CREATIVE:
25-
return "Creative";
26-
case GAME_TYPE_ADVENTURE:
27-
return "Adventure";
28-
case GAME_TYPE_SPECTATOR:
29-
return "Spectator";
30-
default:
31-
return "Undefined";
32-
}
19+
static const std::string& GameTypeToNonLocString(GameType type)
20+
{
21+
switch (type)
22+
{
23+
case GAME_TYPE_SURVIVAL:
24+
{
25+
static const std::string survival = "Survival";
26+
return survival;
27+
}
28+
case GAME_TYPE_CREATIVE:
29+
{
30+
static const std::string creative = "Creative";
31+
return creative;
32+
}
33+
case GAME_TYPE_ADVENTURE:
34+
{
35+
static const std::string adventure = "Adventure";
36+
return adventure;
37+
}
38+
case GAME_TYPE_SPECTATOR:
39+
{
40+
static const std::string spectator = "Spectator";
41+
return spectator;
42+
}
43+
default:
44+
return Util::EMPTY_STRING;
45+
}
46+
}
47+
48+
static const std::string& GameTypeDescriptionToNonLocString(GameType type)
49+
{
50+
switch (type)
51+
{
52+
case GAME_TYPE_SURVIVAL:
53+
{
54+
static const std::string survival = "Mobs, health and gather resources";
55+
return survival;
56+
}
57+
case GAME_TYPE_CREATIVE:
58+
{
59+
static const std::string creative = "Unlimited resources, flying";
60+
return creative;
61+
}
62+
case GAME_TYPE_ADVENTURE:
63+
case GAME_TYPE_SPECTATOR:
64+
default:
65+
return Util::EMPTY_STRING;
3366
}
67+
}
3468
};

0 commit comments

Comments
 (0)