Explorar el Código

possibility to change name

Laserlicht hace 2 años
padre
commit
0c6b1ca3c8

+ 8 - 0
client/CServerHandler.cpp

@@ -499,6 +499,14 @@ void CServerHandler::setPlayer(PlayerColor color) const
 	sendLobbyPack(lsp);
 }
 
+void CServerHandler::setPlayerName(PlayerColor color, std::string name) const
+{
+	LobbySetPlayerName lspn;
+	lspn.color = color;
+	lspn.name = name;
+	sendLobbyPack(lspn);
+}
+
 void CServerHandler::setPlayerOption(ui8 what, int32_t value, PlayerColor player) const
 {
 	LobbyChangePlayerOption lcpo;

+ 2 - 0
client/CServerHandler.h

@@ -67,6 +67,7 @@ public:
 	virtual void setCampaignBonus(int bonusId) const = 0;
 	virtual void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const = 0;
 	virtual void setPlayer(PlayerColor color) const = 0;
+	virtual void setPlayerName(PlayerColor color, std::string name) const = 0;
 	virtual void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const = 0;
 	virtual void setDifficulty(int to) const = 0;
 	virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0;
@@ -152,6 +153,7 @@ public:
 	void setCampaignBonus(int bonusId) const override;
 	void setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts = {}) const override;
 	void setPlayer(PlayerColor color) const override;
+	void setPlayerName(PlayerColor color, std::string name) const override;
 	void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const override;
 	void setDifficulty(int to) const override;
 	void setTurnTimerInfo(const TurnTimerInfo &) const override;

+ 27 - 2
client/lobby/OptionsTab.cpp

@@ -1041,7 +1041,8 @@ void OptionsTab::SelectedBox::scrollBy(int distance)
 }
 
 OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
-	: pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
+	: CIntObject(KEYBOARD | TEXTINPUT)
+	, pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
 	, s(std::make_unique<PlayerSettings>(S))
 	, parentTab(parent)
 {
@@ -1081,7 +1082,13 @@ OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, con
 	}};
 
 	background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
-	labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, s->name);
+	if(!CSH->isMyColor(s->color) || CSH->isGuest())
+		labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, s->name);
+	else
+	{
+		labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, nullptr, false);
+		labelPlayerNameEdit->setText(s->name);
+	}
 	labelWhoCanPlay = std::make_shared<CMultiLineLabel>(Rect(6, 23, 45, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->arraytxt[206 + whoCanPlay]);
 
 	if(SEL->screenType == ESelectionScreen::newGame)
@@ -1115,6 +1122,24 @@ OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, con
 	bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
 }
 
+bool OptionsTab::PlayerOptionsEntry::captureThisKey(EShortcut key)
+{
+	return labelPlayerNameEdit && labelPlayerNameEdit->hasFocus() && key == EShortcut::GLOBAL_ACCEPT;
+}
+
+void OptionsTab::PlayerOptionsEntry::keyPressed(EShortcut key)
+{
+	if(labelPlayerNameEdit)
+	{
+		if(key == EShortcut::GLOBAL_ACCEPT)
+		{
+			CSH->setPlayerName(s->color, labelPlayerNameEdit->getText());
+			Settings name = settings.write["general"]["playerName"];
+			name->String() = labelPlayerNameEdit->getText();
+		}
+	}
+}
+
 void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
 {
 	if(ps.isControlledByAI() || humanPlayers > 0)

+ 3 - 0
client/lobby/OptionsTab.h

@@ -170,6 +170,7 @@ private:
 		std::unique_ptr<PlayerInfo> pi;
 		std::unique_ptr<PlayerSettings> s;
 		std::shared_ptr<CLabel> labelPlayerName;
+		std::shared_ptr<CTextInput> labelPlayerNameEdit;
 		std::shared_ptr<CMultiLineLabel> labelWhoCanPlay;
 		std::shared_ptr<CPicture> background;
 		std::shared_ptr<CButton> buttonTownLeft;
@@ -186,6 +187,8 @@ private:
 
 		PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parentTab);
 		void hideUnavailableButtons();
+		bool captureThisKey(EShortcut key) override;
+		void keyPressed(EShortcut key) override;
 
 	private:
 		const OptionsTab & parentTab;

+ 3 - 2
client/widgets/TextControls.cpp

@@ -532,7 +532,7 @@ Point CGStatusBar::getBorderSize()
 	return Point();
 }
 
-CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB)
+CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, bool giveFocusToInput)
 	: CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
 	cb(CB),
 	CFocusable(std::make_shared<CKeyboardFocusListener>(this))
@@ -544,7 +544,8 @@ CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(c
 	addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
 
 #if !defined(VCMI_MOBILE)
-	giveFocus();
+	if(giveFocusToInput)
+		giveFocus();
 #endif
 }
 

+ 1 - 1
client/widgets/TextControls.h

@@ -224,7 +224,7 @@ public:
 	void setText(const std::string & nText, bool callCb);
 	void setHelpText(const std::string &);
 
-	CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB);
+	CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, bool giveFocusToInput = true);
 	CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB);
 	CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
 

+ 1 - 0
lib/NetPackVisitor.h

@@ -159,6 +159,7 @@ public:
 	virtual void visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack) {}
 	virtual void visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack) {}
 	virtual void visitLobbySetPlayer(LobbySetPlayer & pack) {}
+	virtual void visitLobbySetPlayerName(LobbySetPlayerName & pack) {}
 	virtual void visitLobbySetSimturns(LobbySetSimturns & pack) {}
 	virtual void visitLobbySetTurnTime(LobbySetTurnTime & pack) {}
 	virtual void visitLobbySetDifficulty(LobbySetDifficulty & pack) {}

+ 5 - 0
lib/NetPacksLib.cpp

@@ -755,6 +755,11 @@ void LobbySetPlayer::visitTyped(ICPackVisitor & visitor)
 	visitor.visitLobbySetPlayer(*this);
 }
 
+void LobbySetPlayerName::visitTyped(ICPackVisitor & visitor)
+{
+	visitor.visitLobbySetPlayerName(*this);
+}
+
 void LobbySetSimturns::visitTyped(ICPackVisitor & visitor)
 {
 	visitor.visitLobbySetSimturns(*this);

+ 14 - 0
lib/NetPacksLobby.h

@@ -250,6 +250,20 @@ struct DLL_LINKAGE LobbySetPlayer : public CLobbyPackToServer
 	}
 };
 
+struct DLL_LINKAGE LobbySetPlayerName : public CLobbyPackToServer
+{
+	PlayerColor color = PlayerColor::CANNOT_DETERMINE;
+	std::string name = "";
+
+	virtual void visitTyped(ICPackVisitor & visitor) override;
+
+	template <typename Handler> void serialize(Handler &h, const int version)
+	{
+		h & color;
+		h & name;
+	}
+};
+
 struct DLL_LINKAGE LobbySetSimturns : public CLobbyPackToServer
 {
 	SimturnsInfo simturnsInfo;

+ 1 - 0
lib/registerTypes/RegisterTypes.h

@@ -399,6 +399,7 @@ void registerTypesLobbyPacks(Serializer &s)
 	s.template registerType<CLobbyPackToServer, LobbySetCampaignMap>();
 	s.template registerType<CLobbyPackToServer, LobbySetCampaignBonus>();
 	s.template registerType<CLobbyPackToServer, LobbySetPlayer>();
+	s.template registerType<CLobbyPackToServer, LobbySetPlayerName>();
 	s.template registerType<CLobbyPackToServer, LobbySetTurnTime>();
 	s.template registerType<CLobbyPackToServer, LobbySetSimturns>();
 	s.template registerType<CLobbyPackToServer, LobbySetDifficulty>();

+ 13 - 0
server/CVCMIServer.cpp

@@ -835,6 +835,19 @@ void CVCMIServer::setPlayer(PlayerColor clickedColor)
 	}
 }
 
+void CVCMIServer::setPlayerName(PlayerColor color, std::string name)
+{
+	PlayerSettings & player = si->playerInfos[color];
+
+	if(player.isControlledByHuman())
+	{
+		int nameID = *(player.connectedPlayerIDs.begin()); //if not AI - set appropiate ID
+
+		playerNames[nameID].name = name;
+		setPlayerConnectedId(player, nameID);
+	}
+}
+
 void CVCMIServer::optionNextCastle(PlayerColor player, int dir)
 {
 	PlayerSettings & s = si->playerInfos[player];

+ 1 - 0
server/CVCMIServer.h

@@ -107,6 +107,7 @@ public:
 
 	// Work with LobbyInfo
 	void setPlayer(PlayerColor clickedColor);
+	void setPlayerName(PlayerColor player, std::string name);
 	void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
 	void optionSetHero(PlayerColor player, HeroTypeID id);
 	HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);

+ 1 - 0
server/LobbyNetPackVisitors.h

@@ -86,6 +86,7 @@ public:
 	virtual void visitLobbyChangeHost(LobbyChangeHost & pack) override;
 	virtual void visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack) override;
 	virtual void visitLobbySetPlayer(LobbySetPlayer & pack) override;
+	virtual void visitLobbySetPlayerName(LobbySetPlayerName & pack) override;
 	virtual void visitLobbySetTurnTime(LobbySetTurnTime & pack) override;
 	virtual void visitLobbySetSimturns(LobbySetSimturns & pack) override;
 	virtual void visitLobbySetDifficulty(LobbySetDifficulty & pack) override;

+ 6 - 0
server/NetPacksLobbyServer.cpp

@@ -397,6 +397,12 @@ void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
 	result = true;
 }
 
+void ApplyOnServerNetPackVisitor::visitLobbySetPlayerName(LobbySetPlayerName & pack)
+{
+	srv.setPlayerName(pack.color, pack.name);
+	result = true;
+}
+
 void ApplyOnServerNetPackVisitor::visitLobbySetSimturns(LobbySetSimturns & pack)
 {
 	srv.si->simturnsInfo = pack.simturnsInfo;