Browse Source

Moved some code from OptionTab to separate class, OptionTabBase

Ivan Savenko 1 year ago
parent
commit
3268ebe27c

+ 2 - 0
client/CMakeLists.txt

@@ -52,6 +52,7 @@ set(client_SRCS
 	lobby/CScenarioInfoScreen.cpp
 	lobby/CSelectionBase.cpp
 	lobby/OptionsTab.cpp
+	lobby/OptionsTabBase.cpp
 	lobby/RandomMapTab.cpp
 	lobby/SelectionTab.cpp
 
@@ -212,6 +213,7 @@ set(client_HEADERS
 	lobby/CScenarioInfoScreen.h
 	lobby/CSelectionBase.h
 	lobby/OptionsTab.h
+	lobby/OptionsTabBase.h
 	lobby/RandomMapTab.h
 	lobby/SelectionTab.h
 

+ 4 - 215
client/lobby/OptionsTab.cpp

@@ -42,164 +42,10 @@
 #include "../../lib/mapping/CMapInfo.h"
 #include "../../lib/mapping/CMapHeader.h"
 
-OptionsTab::OptionsTab() : humanPlayers(0)
+OptionsTab::OptionsTab()
+	: OptionsTabBase(JsonPath::builtin("config/widgets/optionsTab.json"))
+	, humanPlayers(0)
 {
-	recActions = 0;
-		
-	addCallback("setTimerPreset", [&](int index){
-		if(!variables["timerPresets"].isNull())
-		{
-			auto tpreset = variables["timerPresets"].Vector().at(index).Vector();
-			TurnTimerInfo tinfo;
-			tinfo.baseTimer = tpreset.at(0).Integer() * 1000;
-			tinfo.turnTimer = tpreset.at(1).Integer() * 1000;
-			tinfo.battleTimer = tpreset.at(2).Integer() * 1000;
-			tinfo.creatureTimer = tpreset.at(3).Integer() * 1000;
-			CSH->setTurnTimerInfo(tinfo);
-		}
-	});
-
-	addCallback("setSimturnDuration", [&](int index){
-		SimturnsInfo info;
-		info.optionalTurns = index;
-		CSH->setSimturnsInfo(info);
-	});
-	
-	//helper function to parse string containing time to integer reflecting time in seconds
-	//assumed that input string can be modified by user, function shall support user's intention
-	// normal: 2:00, 12:30
-	// adding symbol: 2:005 -> 2:05, 2:305 -> 23:05,
-	// adding symbol (>60 seconds): 12:095 -> 129:05
-	// removing symbol: 129:0 -> 12:09, 2:0 -> 0:20, 0:2 -> 0:02
-	auto parseTimerString = [](const std::string & str) -> int
-	{
-		auto sc = str.find(":");
-		if(sc == std::string::npos)
-			return str.empty() ? 0 : std::stoi(str);
-		
-		auto l = str.substr(0, sc);
-		auto r = str.substr(sc + 1, std::string::npos);
-		if(r.length() == 3) //symbol added
-		{
-			l.push_back(r.front());
-			r.erase(r.begin());
-		}
-		else if(r.length() == 1) //symbol removed
-		{
-			r.insert(r.begin(), l.back());
-			l.pop_back();
-		}
-		else if(r.empty())
-			r = "0";
-		
-		int sec = std::stoi(r);
-		if(sec >= 60)
-		{
-			if(l.empty()) //9:00 -> 0:09
-				return sec / 10;
-			
-			l.push_back(r.front()); //0:090 -> 9:00
-			r.erase(r.begin());
-		}
-		else if(l.empty())
-			return sec;
-		
-		return std::stoi(l) * 60 + std::stoi(r);
-	};
-	
-	addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){
-		int time = parseTimerString(str) * 1000;
-		if(time >= 0)
-		{
-			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
-			tinfo.baseTimer = time;
-			CSH->setTurnTimerInfo(tinfo);
-		}
-	});
-	addCallback("parseAndSetTimer_turn", [parseTimerString](const std::string & str){
-		int time = parseTimerString(str) * 1000;
-		if(time >= 0)
-		{
-			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
-			tinfo.turnTimer = time;
-			CSH->setTurnTimerInfo(tinfo);
-		}
-	});
-	addCallback("parseAndSetTimer_battle", [parseTimerString](const std::string & str){
-		int time = parseTimerString(str) * 1000;
-		if(time >= 0)
-		{
-			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
-			tinfo.battleTimer = time;
-			CSH->setTurnTimerInfo(tinfo);
-		}
-	});
-	addCallback("parseAndSetTimer_creature", [parseTimerString](const std::string & str){
-		int time = parseTimerString(str) * 1000;
-		if(time >= 0)
-		{
-			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
-			tinfo.creatureTimer = time;
-			CSH->setTurnTimerInfo(tinfo);
-		}
-	});
-	
-	const JsonNode config(JsonPath::builtin("config/widgets/optionsTab.json"));
-	build(config);
-	
-	//set timers combo box callbacks
-	if(auto w = widget<ComboBox>("timerModeSwitch"))
-	{
-		w->onConstructItems = [&](std::vector<const void *> & curItems){
-			if(variables["timers"].isNull())
-				return;
-			
-			for(auto & p : variables["timers"].Vector())
-			{
-				curItems.push_back(&p);
-			}
-		};
-		
-		w->onSetItem = [&](const void * item){
-			if(item)
-			{
-				if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
-				{
-					for(auto wname : (*tObj)["hideWidgets"].Vector())
-					{
-						if(auto w = widget<CIntObject>(wname.String()))
-							w->setEnabled(false);
-					}
-					for(auto wname : (*tObj)["showWidgets"].Vector())
-					{
-						if(auto w = widget<CIntObject>(wname.String()))
-							w->setEnabled(true);
-					}
-					if((*tObj)["default"].isVector())
-					{
-						TurnTimerInfo tinfo;
-						tinfo.baseTimer = (*tObj)["default"].Vector().at(0).Integer() * 1000;
-						tinfo.turnTimer = (*tObj)["default"].Vector().at(1).Integer() * 1000;
-						tinfo.battleTimer = (*tObj)["default"].Vector().at(2).Integer() * 1000;
-						tinfo.creatureTimer = (*tObj)["default"].Vector().at(3).Integer() * 1000;
-						CSH->setTurnTimerInfo(tinfo);
-					}
-				}
-				redraw();
-			}
-		};
-		
-		w->getItemText = [this](int idx, const void * item){
-			if(item)
-			{
-				if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
-					return readText((*tObj)["text"]);
-			}
-			return std::string("");
-		};
-		
-		w->setItem(0);
-	}
 }
 
 void OptionsTab::recreate()
@@ -221,64 +67,7 @@ void OptionsTab::recreate()
 		entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
 	}
 
-	//Simultaneous turns
-	if(auto turnSlider = widget<CSlider>("labelSimturnsDurationValue"))
-		turnSlider->scrollTo(SEL->getStartInfo()->simturnsInfo.optionalTurns);
-
-	if(auto w = widget<CLabel>("labelSimturnsDurationValue"))
-	{
-		MetaString message;
-		message.appendRawString("Simturns: up to %d days");
-		message.replaceNumber(SEL->getStartInfo()->simturnsInfo.optionalTurns);
-		w->setText(message.toString());
-	}
-	
-	const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo;
-	
-	//classic timer
-	if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
-	{
-		if(!variables["timerPresets"].isNull() && !turnTimerRemote.battleTimer && !turnTimerRemote.creatureTimer && !turnTimerRemote.baseTimer)
-		{
-			for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
-			{
-				auto & tpreset = variables["timerPresets"].Vector()[idx];
-				if(tpreset.Vector().at(1).Integer() == turnTimerRemote.turnTimer / 1000)
-				{
-					turnSlider->scrollTo(idx);
-					if(auto w = widget<CLabel>("labelTurnDurationValue"))
-						w->setText(CGI->generaltexth->turnDurations[idx]);
-				}
-			}
-		}
-	}
-	
-	//chess timer
-	auto timeToString = [](int time) -> std::string
-	{
-		std::stringstream ss;
-		ss << time / 1000 / 60 << ":" << std::setw(2) << std::setfill('0') << time / 1000 % 60;
-		return ss.str();
-	};
-	
-	if(auto ww = widget<CTextInput>("chessFieldBase"))
-		ww->setText(timeToString(turnTimerRemote.baseTimer), false);
-	if(auto ww = widget<CTextInput>("chessFieldTurn"))
-		ww->setText(timeToString(turnTimerRemote.turnTimer), false);
-	if(auto ww = widget<CTextInput>("chessFieldBattle"))
-		ww->setText(timeToString(turnTimerRemote.battleTimer), false);
-	if(auto ww = widget<CTextInput>("chessFieldCreature"))
-		ww->setText(timeToString(turnTimerRemote.creatureTimer), false);
-	
-	if(auto w = widget<ComboBox>("timerModeSwitch"))
-	{
-		if(turnTimerRemote.battleTimer || turnTimerRemote.creatureTimer || turnTimerRemote.baseTimer)
-		{
-			if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
-				if(turnSlider->isActive())
-					w->setItem(1);
-		}
-	}
+	OptionsTabBase::recreate();
 }
 
 size_t OptionsTab::CPlayerSettingsHelper::getImageIndex(bool big)

+ 2 - 2
client/lobby/OptionsTab.h

@@ -9,9 +9,9 @@
  */
 #pragma once
 
+#include "OptionsTabBase.h"
 #include "../windows/CWindowObject.h"
 #include "../widgets/Scrollable.h"
-#include "../gui/InterfaceObjectConfigurable.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 struct PlayerSettings;
@@ -30,7 +30,7 @@ class CButton;
 class FilledTexturePlayerColored;
 
 /// The options tab which is shown at the map selection phase.
-class OptionsTab : public InterfaceObjectConfigurable
+class OptionsTab : public OptionsTabBase
 {
 	struct PlayerOptionsEntry;
 	

+ 244 - 0
client/lobby/OptionsTabBase.cpp

@@ -0,0 +1,244 @@
+/*
+ * OptionsTabBase.cpp, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+#include "StdInc.h"
+#include "OptionsTabBase.h"
+#include "CSelectionBase.h"
+
+#include "../widgets/ComboBox.h"
+#include "../widgets/Slider.h"
+#include "../widgets/TextControls.h"
+#include "../CServerHandler.h"
+#include "../CGameInfo.h"
+
+#include "../../lib/StartInfo.h"
+#include "../../lib/MetaString.h"
+#include "../../lib/CGeneralTextHandler.h"
+
+OptionsTabBase::OptionsTabBase(const JsonPath & configPath)
+{
+	recActions = 0;
+
+	addCallback("setTimerPreset", [&](int index){
+		if(!variables["timerPresets"].isNull())
+		{
+			auto tpreset = variables["timerPresets"].Vector().at(index).Vector();
+			TurnTimerInfo tinfo;
+			tinfo.baseTimer = tpreset.at(0).Integer() * 1000;
+			tinfo.turnTimer = tpreset.at(1).Integer() * 1000;
+			tinfo.battleTimer = tpreset.at(2).Integer() * 1000;
+			tinfo.creatureTimer = tpreset.at(3).Integer() * 1000;
+			CSH->setTurnTimerInfo(tinfo);
+		}
+	});
+
+	addCallback("setSimturnDuration", [&](int index){
+		SimturnsInfo info;
+		info.optionalTurns = index;
+		CSH->setSimturnsInfo(info);
+	});
+
+	//helper function to parse string containing time to integer reflecting time in seconds
+	//assumed that input string can be modified by user, function shall support user's intention
+	// normal: 2:00, 12:30
+	// adding symbol: 2:005 -> 2:05, 2:305 -> 23:05,
+	// adding symbol (>60 seconds): 12:095 -> 129:05
+	// removing symbol: 129:0 -> 12:09, 2:0 -> 0:20, 0:2 -> 0:02
+	auto parseTimerString = [](const std::string & str) -> int
+	{
+		auto sc = str.find(":");
+		if(sc == std::string::npos)
+			return str.empty() ? 0 : std::stoi(str);
+
+		auto l = str.substr(0, sc);
+		auto r = str.substr(sc + 1, std::string::npos);
+		if(r.length() == 3) //symbol added
+		{
+			l.push_back(r.front());
+			r.erase(r.begin());
+		}
+		else if(r.length() == 1) //symbol removed
+		{
+			r.insert(r.begin(), l.back());
+			l.pop_back();
+		}
+		else if(r.empty())
+			r = "0";
+
+		int sec = std::stoi(r);
+		if(sec >= 60)
+		{
+			if(l.empty()) //9:00 -> 0:09
+				return sec / 10;
+
+			l.push_back(r.front()); //0:090 -> 9:00
+			r.erase(r.begin());
+		}
+		else if(l.empty())
+			return sec;
+
+		return std::stoi(l) * 60 + std::stoi(r);
+	};
+
+	addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){
+		int time = parseTimerString(str) * 1000;
+		if(time >= 0)
+		{
+			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
+			tinfo.baseTimer = time;
+			CSH->setTurnTimerInfo(tinfo);
+		}
+	});
+	addCallback("parseAndSetTimer_turn", [parseTimerString](const std::string & str){
+		int time = parseTimerString(str) * 1000;
+		if(time >= 0)
+		{
+			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
+			tinfo.turnTimer = time;
+			CSH->setTurnTimerInfo(tinfo);
+		}
+	});
+	addCallback("parseAndSetTimer_battle", [parseTimerString](const std::string & str){
+		int time = parseTimerString(str) * 1000;
+		if(time >= 0)
+		{
+			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
+			tinfo.battleTimer = time;
+			CSH->setTurnTimerInfo(tinfo);
+		}
+	});
+	addCallback("parseAndSetTimer_creature", [parseTimerString](const std::string & str){
+		int time = parseTimerString(str) * 1000;
+		if(time >= 0)
+		{
+			TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
+			tinfo.creatureTimer = time;
+			CSH->setTurnTimerInfo(tinfo);
+		}
+	});
+
+	const JsonNode config(configPath);
+	build(config);
+
+	//set timers combo box callbacks
+	if(auto w = widget<ComboBox>("timerModeSwitch"))
+	{
+		w->onConstructItems = [&](std::vector<const void *> & curItems){
+			if(variables["timers"].isNull())
+				return;
+
+			for(auto & p : variables["timers"].Vector())
+			{
+				curItems.push_back(&p);
+			}
+		};
+
+		w->onSetItem = [&](const void * item){
+			if(item)
+			{
+				if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
+				{
+					for(auto wname : (*tObj)["hideWidgets"].Vector())
+					{
+						if(auto w = widget<CIntObject>(wname.String()))
+							w->setEnabled(false);
+					}
+					for(auto wname : (*tObj)["showWidgets"].Vector())
+					{
+						if(auto w = widget<CIntObject>(wname.String()))
+							w->setEnabled(true);
+					}
+					if((*tObj)["default"].isVector())
+					{
+						TurnTimerInfo tinfo;
+						tinfo.baseTimer = (*tObj)["default"].Vector().at(0).Integer() * 1000;
+						tinfo.turnTimer = (*tObj)["default"].Vector().at(1).Integer() * 1000;
+						tinfo.battleTimer = (*tObj)["default"].Vector().at(2).Integer() * 1000;
+						tinfo.creatureTimer = (*tObj)["default"].Vector().at(3).Integer() * 1000;
+						CSH->setTurnTimerInfo(tinfo);
+					}
+				}
+				redraw();
+			}
+		};
+
+		w->getItemText = [this](int idx, const void * item){
+			if(item)
+			{
+				if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
+					return readText((*tObj)["text"]);
+			}
+			return std::string("");
+		};
+
+		w->setItem(0);
+	}
+}
+
+void OptionsTabBase::recreate()
+{
+	//Simultaneous turns
+	if(auto turnSlider = widget<CSlider>("labelSimturnsDurationValue"))
+		turnSlider->scrollTo(SEL->getStartInfo()->simturnsInfo.optionalTurns);
+
+	if(auto w = widget<CLabel>("labelSimturnsDurationValue"))
+	{
+		MetaString message;
+		message.appendRawString("Simturns: up to %d days");
+		message.replaceNumber(SEL->getStartInfo()->simturnsInfo.optionalTurns);
+		w->setText(message.toString());
+	}
+
+	const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo;
+
+	//classic timer
+	if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
+	{
+		if(!variables["timerPresets"].isNull() && !turnTimerRemote.battleTimer && !turnTimerRemote.creatureTimer && !turnTimerRemote.baseTimer)
+		{
+			for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
+			{
+				auto & tpreset = variables["timerPresets"].Vector()[idx];
+				if(tpreset.Vector().at(1).Integer() == turnTimerRemote.turnTimer / 1000)
+				{
+					turnSlider->scrollTo(idx);
+					if(auto w = widget<CLabel>("labelTurnDurationValue"))
+						w->setText(CGI->generaltexth->turnDurations[idx]);
+				}
+			}
+		}
+	}
+
+	//chess timer
+	auto timeToString = [](int time) -> std::string
+	{
+		std::stringstream ss;
+		ss << time / 1000 / 60 << ":" << std::setw(2) << std::setfill('0') << time / 1000 % 60;
+		return ss.str();
+	};
+
+	if(auto ww = widget<CTextInput>("chessFieldBase"))
+		ww->setText(timeToString(turnTimerRemote.baseTimer), false);
+	if(auto ww = widget<CTextInput>("chessFieldTurn"))
+		ww->setText(timeToString(turnTimerRemote.turnTimer), false);
+	if(auto ww = widget<CTextInput>("chessFieldBattle"))
+		ww->setText(timeToString(turnTimerRemote.battleTimer), false);
+	if(auto ww = widget<CTextInput>("chessFieldCreature"))
+		ww->setText(timeToString(turnTimerRemote.creatureTimer), false);
+
+	if(auto w = widget<ComboBox>("timerModeSwitch"))
+	{
+		if(turnTimerRemote.battleTimer || turnTimerRemote.creatureTimer || turnTimerRemote.baseTimer)
+		{
+			if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
+				if(turnSlider->isActive())
+					w->setItem(1);
+		}
+	}
+}

+ 22 - 0
client/lobby/OptionsTabBase.h

@@ -0,0 +1,22 @@
+/*
+ * OptionsTabBase.h, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+#pragma once
+
+#include "../gui/InterfaceObjectConfigurable.h"
+#include "../../lib/filesystem/ResourcePath.h"
+
+/// The options tab which is shown at the map selection phase.
+class OptionsTabBase : public InterfaceObjectConfigurable
+{
+public:
+	OptionsTabBase(const JsonPath & configPath);
+
+	void recreate();
+};