git repos / blockattack-game

blame: source/code/ModConfigMenuState.cpp

normal view · raw

3affd2d6 Poul Sander 2020-12-27 15:56 1
/*
3affd2d6 Poul Sander 2020-12-27 15:56 2
===========================================================================
3affd2d6 Poul Sander 2020-12-27 15:56 3
blockattack - Block Attack - Rise of the Blocks
3affd2d6 Poul Sander 2020-12-27 15:56 4
Copyright (C) 2005-2020 Poul Sander
3affd2d6 Poul Sander 2020-12-27 15:56 5
3affd2d6 Poul Sander 2020-12-27 15:56 6
This program is free software: you can redistribute it and/or modify
3affd2d6 Poul Sander 2020-12-27 15:56 7
it under the terms of the GNU General Public License as published by
3affd2d6 Poul Sander 2020-12-27 15:56 8
the Free Software Foundation, either version 2 of the License, or
3affd2d6 Poul Sander 2020-12-27 15:56 9
(at your option) any later version.
3affd2d6 Poul Sander 2020-12-27 15:56 10
3affd2d6 Poul Sander 2020-12-27 15:56 11
This program is distributed in the hope that it will be useful,
3affd2d6 Poul Sander 2020-12-27 15:56 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
3affd2d6 Poul Sander 2020-12-27 15:56 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3affd2d6 Poul Sander 2020-12-27 15:56 14
GNU General Public License for more details.
3affd2d6 Poul Sander 2020-12-27 15:56 15
3affd2d6 Poul Sander 2020-12-27 15:56 16
You should have received a copy of the GNU General Public License
3affd2d6 Poul Sander 2020-12-27 15:56 17
along with this program.  If not, see http://www.gnu.org/licenses/
3affd2d6 Poul Sander 2020-12-27 15:56 18
3affd2d6 Poul Sander 2020-12-27 15:56 19
Source information and contacts persons can be found at
3affd2d6 Poul Sander 2020-12-27 15:56 20
https://blockattack.net
3affd2d6 Poul Sander 2020-12-27 15:56 21
===========================================================================
3affd2d6 Poul Sander 2020-12-27 15:56 22
*/
3affd2d6 Poul Sander 2020-12-27 15:56 23
3affd2d6 Poul Sander 2020-12-27 15:56 24
#include "ModConfigMenuState.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 25
#include "MenuSystem.h"
3affd2d6 Poul Sander 2020-12-27 15:56 26
#include "global.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 27
#include "MenuSystem.h"
3affd2d6 Poul Sander 2020-12-27 15:56 28
#include "sago/SagoMisc.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 29
#include "os.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 30
#include <boost/algorithm/string/predicate.hpp>
3affd2d6 Poul Sander 2020-12-27 15:56 31
3affd2d6 Poul Sander 2020-12-27 15:56 32
const int buttonOffset = 160;
3affd2d6 Poul Sander 2020-12-27 15:56 33
extern sago::SagoSprite bExit;
3affd2d6 Poul Sander 2020-12-27 15:56 34
3affd2d6 Poul Sander 2020-12-27 15:56 35
bool sort_mods_enabled_order (const Mod& i,const Mod& j) {
3affd2d6 Poul Sander 2020-12-27 15:56 36
	if (i.enabled && !j.enabled) {
3affd2d6 Poul Sander 2020-12-27 15:56 37
		//Enabled mods always goes before disabled ones
3affd2d6 Poul Sander 2020-12-27 15:56 38
		return true;
3affd2d6 Poul Sander 2020-12-27 15:56 39
	}
3affd2d6 Poul Sander 2020-12-27 15:56 40
	if (i.enabled) {
3affd2d6 Poul Sander 2020-12-27 15:56 41
		return i.order < j.order;
3affd2d6 Poul Sander 2020-12-27 15:56 42
	}
3affd2d6 Poul Sander 2020-12-27 15:56 43
	return i.name < j.name;
3affd2d6 Poul Sander 2020-12-27 15:56 44
}
3affd2d6 Poul Sander 2020-12-27 15:56 45
3affd2d6 Poul Sander 2020-12-27 15:56 46
static void initMods(std::vector<Mod>& mod_list) {
3affd2d6 Poul Sander 2020-12-27 15:56 47
	for (size_t i=0; i < globalData.modList.size(); ++i) {
3affd2d6 Poul Sander 2020-12-27 15:56 48
		for (Mod& m : mod_list) {
3affd2d6 Poul Sander 2020-12-27 15:56 49
			if (m.name == globalData.modList[i]) {
3affd2d6 Poul Sander 2020-12-27 15:56 50
				m.order = i;
3affd2d6 Poul Sander 2020-12-27 15:56 51
				m.enabled = true;
3affd2d6 Poul Sander 2020-12-27 15:56 52
			}
3affd2d6 Poul Sander 2020-12-27 15:56 53
		}
3affd2d6 Poul Sander 2020-12-27 15:56 54
	}
3affd2d6 Poul Sander 2020-12-27 15:56 55
	std::sort(mod_list.begin(), mod_list.end(), sort_mods_enabled_order);
3affd2d6 Poul Sander 2020-12-27 15:56 56
}
3affd2d6 Poul Sander 2020-12-27 15:56 57
3affd2d6 Poul Sander 2020-12-27 15:56 58
static void appendMods(const std::vector<std::string>& mod_files, const std::string& dir, std::vector<Mod>& mods_available) {
3affd2d6 Poul Sander 2020-12-27 15:56 59
	for (const std::string& mod : mod_files) {
3affd2d6 Poul Sander 2020-12-27 15:56 60
		if (!boost::ends_with(mod, ".data")) {
3affd2d6 Poul Sander 2020-12-27 15:56 61
			continue;
3affd2d6 Poul Sander 2020-12-27 15:56 62
		}
3affd2d6 Poul Sander 2020-12-27 15:56 63
		Mod m;
3affd2d6 Poul Sander 2020-12-27 15:56 64
		m.name = mod.substr(0, mod.length()-5);
3affd2d6 Poul Sander 2020-12-27 15:56 65
		m.filename = dir + "/" + mod;
3affd2d6 Poul Sander 2020-12-27 15:56 66
		mods_available.push_back(m);
3affd2d6 Poul Sander 2020-12-27 15:56 67
	}
3affd2d6 Poul Sander 2020-12-27 15:56 68
}
3affd2d6 Poul Sander 2020-12-27 15:56 69
3affd2d6 Poul Sander 2020-12-27 15:56 70
3affd2d6 Poul Sander 2020-12-27 15:56 71
ModConfigMenuState::ModConfigMenuState() {
3affd2d6 Poul Sander 2020-12-27 15:56 72
	mods_available.clear();
3affd2d6 Poul Sander 2020-12-27 15:56 73
	std::string baseMods = std::string(PHYSFS_getBaseDir())+ "/mods";
3affd2d6 Poul Sander 2020-12-27 15:56 74
	std::vector<std::string> baseModFiles = OsGetDirFileList(baseMods);
3affd2d6 Poul Sander 2020-12-27 15:56 75
	appendMods(baseModFiles, baseMods, mods_available);
cb995956 Poul Sander 2022-01-15 14:06 76
	std::string sharedMods = std::string(SHAREDIR)+ "/mods";
cb995956 Poul Sander 2022-01-15 14:06 77
	std::vector<std::string> sharedModFiles = OsGetDirFileList(sharedMods);
cb995956 Poul Sander 2022-01-15 14:06 78
	appendMods(sharedModFiles, sharedMods, mods_available);
3affd2d6 Poul Sander 2020-12-27 15:56 79
	std::string userMods = getPathToSaveFiles()+"/mods";
3affd2d6 Poul Sander 2020-12-27 15:56 80
	std::vector<std::string> userModFiles = OsGetDirFileList(userMods);
3affd2d6 Poul Sander 2020-12-27 15:56 81
	appendMods(userModFiles, userMods, mods_available);
3affd2d6 Poul Sander 2020-12-27 15:56 82
	initMods(mods_available);
3affd2d6 Poul Sander 2020-12-27 15:56 83
}
3affd2d6 Poul Sander 2020-12-27 15:56 84
3affd2d6 Poul Sander 2020-12-27 15:56 85
ModConfigMenuState::~ModConfigMenuState() {}
3affd2d6 Poul Sander 2020-12-27 15:56 86
3affd2d6 Poul Sander 2020-12-27 15:56 87
bool ModConfigMenuState::IsActive() {
3affd2d6 Poul Sander 2020-12-27 15:56 88
	return isActive;
3affd2d6 Poul Sander 2020-12-27 15:56 89
}
3affd2d6 Poul Sander 2020-12-27 15:56 90
3affd2d6 Poul Sander 2020-12-27 15:56 91
void ModConfigMenuState::Draw(SDL_Renderer* target) {
3affd2d6 Poul Sander 2020-12-27 15:56 92
	DrawBackground(target);
3affd2d6 Poul Sander 2020-12-27 15:56 93
	standardButton.getLabel(_("Mod config"))->Draw(target, 50, 50);
3affd2d6 Poul Sander 2020-12-27 15:56 94
	for (size_t i = 0; i < mods_available.size(); ++i) {
3affd2d6 Poul Sander 2020-12-27 15:56 95
		standardButton.getLabel(mods_available[i].name)->Draw(target, 60, 160+30*i);
3affd2d6 Poul Sander 2020-12-27 15:56 96
		standardButton.getLabel(mods_available[i].enabled ? _("Yes") : _("No"))->Draw(target, 600, 160+30*i);
3affd2d6 Poul Sander 2020-12-27 15:56 97
		if (mods_available[i].enabled) {
3affd2d6 Poul Sander 2020-12-27 15:56 98
			standardButton.getLabel(std::to_string(mods_available[i].order))->Draw(target, 700, 160+30*i);
3affd2d6 Poul Sander 2020-12-27 15:56 99
		}
3affd2d6 Poul Sander 2020-12-27 15:56 100
	}
3affd2d6 Poul Sander 2020-12-27 15:56 101
	bExit.Draw(globalData.screen, SDL_GetTicks(), globalData.xsize-buttonOffset, globalData.ysize-buttonOffset);
3affd2d6 Poul Sander 2020-12-27 15:56 102
}
3affd2d6 Poul Sander 2020-12-27 15:56 103
3affd2d6 Poul Sander 2020-12-27 15:56 104
void ModConfigMenuState::ProcessInput(const SDL_Event& event, bool& processed) {
3affd2d6 Poul Sander 2020-12-27 15:56 105
	if (isEscapeEvent(event)) {
3affd2d6 Poul Sander 2020-12-27 15:56 106
		isActive = false;
3affd2d6 Poul Sander 2020-12-27 15:56 107
		processed = true;
3affd2d6 Poul Sander 2020-12-27 15:56 108
	}
3affd2d6 Poul Sander 2020-12-27 15:56 109
}
3affd2d6 Poul Sander 2020-12-27 15:56 110
3affd2d6 Poul Sander 2020-12-27 15:56 111
void ModConfigMenuState::Update() {
3affd2d6 Poul Sander 2020-12-27 15:56 112
	// If the mouse button is released, make bMouseUp equal true
3affd2d6 Poul Sander 2020-12-27 15:56 113
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
3affd2d6 Poul Sander 2020-12-27 15:56 114
		bMouseUp=true;
3affd2d6 Poul Sander 2020-12-27 15:56 115
	}
3affd2d6 Poul Sander 2020-12-27 15:56 116
3affd2d6 Poul Sander 2020-12-27 15:56 117
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
3affd2d6 Poul Sander 2020-12-27 15:56 118
		bMouseUp = false;
3affd2d6 Poul Sander 2020-12-27 15:56 119
3affd2d6 Poul Sander 2020-12-27 15:56 120
		//The Score button:
3affd2d6 Poul Sander 2020-12-27 15:56 121
		if ((globalData.mousex>globalData.xsize-buttonOffset) && (globalData.mousex<globalData.xsize-buttonOffset+bExit.GetWidth())
3affd2d6 Poul Sander 2020-12-27 15:56 122
		        && (globalData.mousey>globalData.ysize-buttonOffset) && (globalData.mousey<globalData.ysize-buttonOffset+bExit.GetHeight())) {
3affd2d6 Poul Sander 2020-12-27 15:56 123
			isActive = false;
3affd2d6 Poul Sander 2020-12-27 15:56 124
		}
3affd2d6 Poul Sander 2020-12-27 15:56 125
3affd2d6 Poul Sander 2020-12-27 15:56 126
	}
3affd2d6 Poul Sander 2020-12-27 15:56 127
}
1970-01-01 00:00 128