mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Added callbacks for preset dropdowns
This commit is contained in:
		| @@ -26,18 +26,35 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath) | ||||
| { | ||||
| 	recActions = 0; | ||||
|  | ||||
| 	addCallback("setTimerPreset", [&](int index){ | ||||
| 	auto setTimerPresetCallback = [this](int index){ | ||||
| 		if(!variables["timerPresets"].isNull()) | ||||
| 		{ | ||||
| 			auto tpreset = variables["timerPresets"].Vector().at(index).Vector(); | ||||
| 			TurnTimerInfo tinfo; | ||||
| 			TurnTimerInfo tinfo =  SEL->getStartInfo()->turnTimerInfo; | ||||
| 			tinfo.baseTimer = tpreset.at(0).Integer() * 1000; | ||||
| 			tinfo.turnTimer = tpreset.at(1).Integer() * 1000; | ||||
| 			tinfo.battleTimer = tpreset.at(2).Integer() * 1000; | ||||
| 			tinfo.unitTimer = tpreset.at(3).Integer() * 1000; | ||||
| 			tinfo.accumulatingTurnTimer = tpreset.at(4).Bool(); | ||||
| 			tinfo.accumulatingUnitTimer = tpreset.at(5).Bool(); | ||||
| 			CSH->setTurnTimerInfo(tinfo); | ||||
| 		} | ||||
| 	}); | ||||
| 	}; | ||||
|  | ||||
| 	auto setSimturnsPresetCallback = [this](int index){ | ||||
| 		if(!variables["simturnsPresets"].isNull()) | ||||
| 		{ | ||||
| 			auto tpreset = variables["simturnsPresets"].Vector().at(index).Vector(); | ||||
| 			SimturnsInfo tinfo = SEL->getStartInfo()->simturnsInfo; | ||||
| 			tinfo.optionalTurns = tpreset.at(0).Integer(); | ||||
| 			tinfo.requiredTurns = tpreset.at(1).Integer(); | ||||
| 			tinfo.allowHumanWithAI = tpreset.at(2).Bool(); | ||||
| 			CSH->setSimturnsInfo(tinfo); | ||||
| 		} | ||||
| 	}; | ||||
|  | ||||
| 	addCallback("setTimerPreset", setTimerPresetCallback); | ||||
| 	addCallback("setSimturnPreset", setSimturnsPresetCallback); | ||||
|  | ||||
| 	addCallback("setSimturnDurationMin", [&](int index){ | ||||
| 		SimturnsInfo info = SEL->getStartInfo()->simturnsInfo; | ||||
| @@ -59,6 +76,18 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath) | ||||
| 		CSH->setSimturnsInfo(info); | ||||
| 	}); | ||||
|  | ||||
| 	addCallback("setTurnTimerAccumulate", [&](int index){ | ||||
| 		TurnTimerInfo info = SEL->getStartInfo()->turnTimerInfo; | ||||
| 		info.accumulatingTurnTimer = index; | ||||
| 		CSH->setTurnTimerInfo(info); | ||||
| 	}); | ||||
|  | ||||
| 	addCallback("setUnitTimerAccumulate", [&](int index){ | ||||
| 		TurnTimerInfo info = SEL->getStartInfo()->turnTimerInfo; | ||||
| 		info.accumulatingUnitTimer = index; | ||||
| 		CSH->setTurnTimerInfo(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 | ||||
| @@ -194,6 +223,34 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath) | ||||
|  | ||||
| 		w->setItem(0); | ||||
| 	} | ||||
|  | ||||
| 	if(auto w = widget<ComboBox>("simturnsPresetSelector")) | ||||
| 	{ | ||||
| 		w->onConstructItems = [this](std::vector<const void *> & curItems) | ||||
| 		{ | ||||
| 			for (size_t i = 0; i < variables["simturnsPresets"].Vector().size(); ++i) | ||||
| 				curItems.push_back((void*)i); | ||||
| 		}; | ||||
|  | ||||
| 		w->onSetItem = [setSimturnsPresetCallback](const void * item){ | ||||
| 			size_t itemIndex = (size_t)item; | ||||
| 			setSimturnsPresetCallback(itemIndex); | ||||
| 		}; | ||||
| 	} | ||||
|  | ||||
| 	if(auto w = widget<ComboBox>("timerPresetSelector")) | ||||
| 	{ | ||||
| 		w->onConstructItems = [this](std::vector<const void *> & curItems) | ||||
| 		{ | ||||
| 			for (size_t i = 0; i < variables["timerPresets"].Vector().size(); ++i) | ||||
| 				curItems.push_back((void*)i); | ||||
| 		}; | ||||
|  | ||||
| 		w->onSetItem = [setTimerPresetCallback](const void * item){ | ||||
| 			size_t itemIndex = (size_t)item; | ||||
| 			setTimerPresetCallback(itemIndex); | ||||
| 		}; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void OptionsTabBase::recreate() | ||||
| @@ -246,6 +303,12 @@ void OptionsTabBase::recreate() | ||||
| 	if(auto buttonSimturnsAI = widget<CToggleButton>("buttonSimturnsAI")) | ||||
| 		buttonSimturnsAI->setSelectedSilent(SEL->getStartInfo()->simturnsInfo.allowHumanWithAI); | ||||
|  | ||||
| 	if(auto buttonTurnTimerAccumulate = widget<CToggleButton>("buttonTurnTimerAccumulate")) | ||||
| 		buttonTurnTimerAccumulate->setSelectedSilent(SEL->getStartInfo()->turnTimerInfo.accumulatingTurnTimer); | ||||
|  | ||||
| 	if(auto buttonUnitTimerAccumulate = widget<CToggleButton>("buttonUnitTimerAccumulate")) | ||||
| 		buttonUnitTimerAccumulate->setSelectedSilent(SEL->getStartInfo()->turnTimerInfo.accumulatingUnitTimer); | ||||
|  | ||||
| 	const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo; | ||||
|  | ||||
| 	//classic timer | ||||
|   | ||||
| @@ -33,9 +33,9 @@ ComboBox::DropDown::Item::Item(const JsonNode & config, ComboBox::DropDown & _dr | ||||
|  | ||||
| void ComboBox::DropDown::Item::updateItem(int idx, const void * _item) | ||||
| { | ||||
| 	item = _item; | ||||
| 	if(auto w = widget<CLabel>("labelName")) | ||||
| 	{ | ||||
| 		item = _item; | ||||
| 		if(dropDown.comboBox.getItemText) | ||||
| 			w->setText(dropDown.comboBox.getItemText(idx, item)); | ||||
| 	} | ||||
| @@ -130,20 +130,21 @@ void ComboBox::DropDown::clickPressed(const Point & cursorPosition) | ||||
|  | ||||
| void ComboBox::DropDown::updateListItems() | ||||
| { | ||||
| 	int elemIdx = 0; | ||||
|  | ||||
| 	if(auto w = widget<CSlider>("slider")) | ||||
| 		elemIdx = w->getValue(); | ||||
|  | ||||
| 	for(auto item : items) | ||||
| 	{ | ||||
| 		int elemIdx = w->getValue(); | ||||
| 		for(auto item : items) | ||||
| 		if(elemIdx < curItems.size()) | ||||
| 		{ | ||||
| 			if(elemIdx < curItems.size()) | ||||
| 			{ | ||||
| 				item->updateItem(elemIdx, curItems[elemIdx]); | ||||
| 				elemIdx++; | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				item->updateItem(elemIdx); | ||||
| 			} | ||||
| 			item->updateItem(elemIdx, curItems[elemIdx]); | ||||
| 			elemIdx++; | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			item->updateItem(elemIdx); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -167,7 +168,9 @@ ComboBox::ComboBox(Point position, const AnimationPath & defName, const std::pai | ||||
|  | ||||
| void ComboBox::setItem(const void * item) | ||||
| { | ||||
| 	if(auto w = std::dynamic_pointer_cast<CLabel>(overlay); getItemText) | ||||
| 	auto w = std::dynamic_pointer_cast<CLabel>(overlay); | ||||
|  | ||||
| 	if( w && getItemText) | ||||
| 		addTextOverlay(getItemText(0, item), w->font, w->color); | ||||
| 	 | ||||
| 	if(onSetItem) | ||||
|   | ||||
| @@ -37,12 +37,13 @@ class ComboBox : public CButton | ||||
| 		bool receiveEvent(const Point & position, int eventType) const override; | ||||
| 		void clickPressed(const Point & cursorPosition) override; | ||||
| 		void setItem(const void *); | ||||
|  | ||||
| 		void updateListItems(); | ||||
| 			 | ||||
| 	private: | ||||
| 		std::shared_ptr<DropDown::Item> buildItem(const JsonNode & config); | ||||
| 		 | ||||
| 		void sliderMove(int slidPos); | ||||
| 		void updateListItems(); | ||||
| 		 | ||||
| 		ComboBox & comboBox; | ||||
| 		std::vector<std::shared_ptr<Item>> items; | ||||
| @@ -66,4 +67,6 @@ public: | ||||
| 	std::function<std::string(int, const void *)> getItemText; | ||||
| 	 | ||||
| 	void setItem(int id); | ||||
|  | ||||
| 	void updateListItems(); | ||||
| }; | ||||
|   | ||||
| @@ -113,17 +113,17 @@ | ||||
| 	{ | ||||
| 		"timerPresets" : | ||||
| 		[ | ||||
| 			[0, 60, 0, 0], | ||||
| 			[0, 120, 0, 0], | ||||
| 			[0, 240, 0, 0], | ||||
| 			[0, 360, 0, 0], | ||||
| 			[0, 480, 0, 0], | ||||
| 			[0, 600, 0, 0], | ||||
| 			[0, 900, 0, 0], | ||||
| 			[0, 1200, 0, 0], | ||||
| 			[0, 1500, 0, 0], | ||||
| 			[0, 1800, 0, 0], | ||||
| 			[0, 0, 0, 0], | ||||
| 			[0,   60, 0, 0, false, false], | ||||
| 			[0,  120, 0, 0, false, false], | ||||
| 			[0,  240, 0, 0, false, false], | ||||
| 			[0,  360, 0, 0, false, false], | ||||
| 			[0,  480, 0, 0, false, false], | ||||
| 			[0,  600, 0, 0, false, false], | ||||
| 			[0,  900, 0, 0, false, false], | ||||
| 			[0, 1200, 0, 0, false, false], | ||||
| 			[0, 1500, 0, 0, false, false], | ||||
| 			[0, 1800, 0, 0, false, false], | ||||
| 			[0,    0, 0, 0, false, false], | ||||
| 		] | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -92,17 +92,30 @@ | ||||
| 	{ | ||||
| 		"timerPresets" : | ||||
| 		[ | ||||
| 			[0, 60, 0, 0], | ||||
| 			[0, 120, 0, 0], | ||||
| 			[0, 240, 0, 0], | ||||
| 			[0, 360, 0, 0], | ||||
| 			[0, 480, 0, 0], | ||||
| 			[0, 600, 0, 0], | ||||
| 			[0, 900, 0, 0], | ||||
| 			[0, 1200, 0, 0], | ||||
| 			[0, 1500, 0, 0], | ||||
| 			[0, 1800, 0, 0], | ||||
| 			[0, 0, 0, 0], | ||||
| 			[    0,    0,   0, 0, false, false], | ||||
| 			[    0,   60,   0, 0, false, false], | ||||
| 			[    0,  120,   0, 0, false, false], | ||||
| 			[    0,  300,   0, 0, false, false], | ||||
| 			[    0,  600,   0, 0, false, false], | ||||
| 			[    0, 1200,   0, 0, false, false], | ||||
| 			[    0, 1800,   0, 0, false, false], | ||||
| 			[  960,  480, 120, 0, true, false], | ||||
| 			[  960,  480,  75, 0, true, false], | ||||
| 			[  480,  240,  60, 0, true, false], | ||||
| 			[  120,   90,  60, 0, true, false], | ||||
| 			[  120,   60,  15, 0, true, false], | ||||
| 			[   60,   60,   0, 0, true, false] | ||||
| 		], | ||||
| 		"simturnsPresets" : | ||||
| 		[ | ||||
| 			[   0,  0, false], | ||||
| 			[ 999,  0, false], | ||||
| 			[   7,  0, false], | ||||
| 			[  14,  0, false], | ||||
| 			[  28,  0, false], | ||||
| 			[   7,  7, false], | ||||
| 			[  14, 14, false], | ||||
| 			[  28, 28, false] | ||||
| 		] | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -173,13 +173,15 @@ | ||||
| 			"name": "buttonTurnTimerAccumulate", | ||||
| 			"position": {"x": 160, "y": 195}, | ||||
| 			"type": "toggleButton", | ||||
| 			"image": "lobby/checkbox" | ||||
| 			"image": "lobby/checkbox", | ||||
| 			"callback" : "setSimturnAI" | ||||
| 		}, | ||||
| 		{ | ||||
| 			"name": "buttonUnitTimerAccumulate", | ||||
| 			"position": {"x": 160, "y": 327}, | ||||
| 			"type": "toggleButton", | ||||
| 			"image": "lobby/checkbox" | ||||
| 			"image": "lobby/checkbox", | ||||
| 			"callback" : "setSimturnAI" | ||||
| 		}, | ||||
| 		 | ||||
| 		{ | ||||
| @@ -297,7 +299,6 @@ | ||||
| 			"position": {"x": 278, "y": 478} | ||||
| 		}, | ||||
| 		{ | ||||
| 			"type" : "label", | ||||
| 			"text": "vcmi.optionsTab.simturnsMin.help", | ||||
| 			"type": "multiLineLabel", | ||||
| 			"font": "tiny", | ||||
| @@ -306,7 +307,6 @@ | ||||
| 			"rect": {"x": 70, "y": 430, "w": 300, "h": 40} | ||||
| 		}, | ||||
| 		{ | ||||
| 			"type" : "label", | ||||
| 			"text": "vcmi.optionsTab.simturnsMax.help", | ||||
| 			"type": "multiLineLabel", | ||||
| 			"font": "tiny", | ||||
| @@ -318,7 +318,8 @@ | ||||
| 			"name": "buttonSimturnsAI", | ||||
| 			"position": {"x": 70, "y": 535}, | ||||
| 			"type": "toggleButton", | ||||
| 			"image": "lobby/checkbox" | ||||
| 			"image": "lobby/checkbox", | ||||
| 			"callback" : "setSimturnAI" | ||||
| 		}, | ||||
| 		{ | ||||
| 			"name": "labelSimturnsAI", | ||||
| @@ -329,5 +330,36 @@ | ||||
| 			"text": "vcmi.optionsTab.simturnsAI.hover", | ||||
| 			"position": {"x": 110, "y": 540} | ||||
| 		} | ||||
| 	] | ||||
| 	], | ||||
| 	 | ||||
| 	"variables": | ||||
| 	{ | ||||
| 		"timerPresets" : | ||||
| 		[ | ||||
| 			[    0,    0,   0, 0, false, false], | ||||
| 			[    0,   60,   0, 0, false, false], | ||||
| 			[    0,  120,   0, 0, false, false], | ||||
| 			[    0,  300,   0, 0, false, false], | ||||
| 			[    0,  600,   0, 0, false, false], | ||||
| 			[    0, 1200,   0, 0, false, false], | ||||
| 			[    0, 1800,   0, 0, false, false], | ||||
| 			[  960,  480, 120, 0, true, false], | ||||
| 			[  960,  480,  75, 0, true, false], | ||||
| 			[  480,  240,  60, 0, true, false], | ||||
| 			[  120,   90,  60, 0, true, false], | ||||
| 			[  120,   60,  15, 0, true, false], | ||||
| 			[   60,   60,   0, 0, true, false] | ||||
| 		], | ||||
| 		"simturnsPresets" : | ||||
| 		[ | ||||
| 			[   0,  0, false], | ||||
| 			[ 999,  0, false], | ||||
| 			[   7,  0, false], | ||||
| 			[  14,  0, false], | ||||
| 			[  28,  0, false], | ||||
| 			[   7,  7, false], | ||||
| 			[  14, 14, false], | ||||
| 			[  28, 28, false] | ||||
| 		] | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -74,7 +74,7 @@ void TurnTimerHandler::onPlayerGetTurn(PlayerColor player) | ||||
| 		{ | ||||
| 			endTurnAllowed[player] = true; | ||||
| 			auto & timer = timers[player]; | ||||
| 			if(si->turnTimerInfo.accumulatingTurnTimer > 0) | ||||
| 			if(si->turnTimerInfo.accumulatingTurnTimer) | ||||
| 				timer.baseTimer += timer.turnTimer; | ||||
| 			timer.turnTimer = si->turnTimerInfo.turnTimer; | ||||
| 			 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user