git repos / blockattack-game

blame: source/code/MenuSystem.h

normal view · raw

f3ef3495 sago007 2008-11-13 20:00 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 Poul Sander
f3ef3495 sago007 2008-11-13 20:00 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
f3ef3495 sago007 2008-11-13 20:00 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
f3ef3495 sago007 2008-11-13 20:00 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
f3ef3495 sago007 2008-11-13 20:00 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
021de090 sago007 2015-12-30 18:56 20
http://blockattack.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
f3ef3495 sago007 2008-11-13 20:00 22
*/
f3ef3495 sago007 2008-11-13 20:00 23
f3ef3495 sago007 2008-11-13 20:00 24
f3ef3495 sago007 2008-11-13 20:00 25
#ifndef _MENUSYSTEM_H
f3ef3495 sago007 2008-11-13 20:00 26
#define	_MENUSYSTEM_H
f3ef3495 sago007 2008-11-13 20:00 27
f3ef3495 sago007 2008-11-13 20:00 28
#include <string>
f3ef3495 sago007 2008-11-13 20:00 29
#include "SDL.h"
d91e7363 sago007 2018-03-26 13:09 30
#include <map>
f3ef3495 sago007 2008-11-13 20:00 31
#include <vector>
1d2e2129 sago007 2015-12-23 15:41 32
#include "sago/SagoSprite.hpp"
00e866da sago007 2016-10-31 20:47 33
#include "sago/GameStateInterface.hpp"
f3a1637d sago007 2015-11-14 21:32 34
#include <memory>
d91e7363 sago007 2018-03-26 13:09 35
#include "sago/SagoTextField.hpp"
f3ef3495 sago007 2008-11-13 20:00 36
f3ef3495 sago007 2008-11-13 20:00 37
//The ButtonGfx object hold common media for all buttons, so we can reskin them by only changeing one pointer
78ab168e sago007 2015-11-14 22:02 38
struct ButtonGfx
c53e6443 sago007 2012-04-17 11:04 39
{
c53e6443 sago007 2012-04-17 11:04 40
	//The size of the buttons, so we don't have to ask w and h from the SDL Surfaces each time
8ff26872 sago007 2016-03-25 09:46 41
	int xsize = 0;
8ff26872 sago007 2016-03-25 09:46 42
	int ysize = 0;
60b2bac0 Poul Sander 2023-11-10 20:43 43
	sago::SagoTextField* getLabel(const std::string& text, bool marked) const;
094b2dfe sago007 2016-06-12 10:50 44
	void setSurfaces();
60b2bac0 Poul Sander 2023-11-10 20:43 45
	std::string menu_marked = "menu_marked";
60b2bac0 Poul Sander 2023-11-10 20:43 46
	std::string menu_unmarked = "menu_unmarked";
d91e7363 sago007 2018-03-26 13:09 47
private:
60b2bac0 Poul Sander 2023-11-10 20:43 48
	mutable std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels;
60b2bac0 Poul Sander 2023-11-10 20:43 49
	mutable std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels_marked;
f3ef3495 sago007 2008-11-13 20:00 50
};
f3ef3495 sago007 2008-11-13 20:00 51
f3ef3495 sago007 2008-11-13 20:00 52
//A button
c53e6443 sago007 2012-04-17 11:04 53
class Button
c53e6443 sago007 2012-04-17 11:04 54
{
c53e6443 sago007 2012-04-17 11:04 55
private:
c53e6443 sago007 2012-04-17 11:04 56
	//Pointer to a callback function.
7f03a124 Poul Sander 2021-09-24 13:46 57
	void (*action)(void) = nullptr;
b4f1d3cf sago007 2012-08-20 21:28 58
67a32395 sago007 2012-04-19 18:40 59
	//If true the menu should also be closed then the button is clicked
8ff26872 sago007 2016-03-25 09:46 60
	bool popOnRun = false;
b4f1d3cf sago007 2012-08-20 21:28 61
c0a3362c sago007 2016-10-07 20:23 62
	//The label. This is written on the button
c0a3362c sago007 2016-10-07 20:23 63
	std::string label;
c99935b0 Poul Sander 2021-08-28 13:13 64
public:
c53e6443 sago007 2012-04-17 11:04 65
	//Is the button marked?
733af903 sago007 2016-03-24 14:58 66
	bool marked = false;
c53e6443 sago007 2012-04-17 11:04 67
	//Where is the button on the screen
733af903 sago007 2016-03-24 14:58 68
	int x = 0;
733af903 sago007 2016-03-24 14:58 69
	int y = 0;
60b2bac0 Poul Sander 2023-11-10 20:43 70
	ButtonGfx standardButton;
c53e6443 sago007 2012-04-17 11:04 71
792dafd6 sago007 2020-07-01 19:00 72
	Button() = default;
c53e6443 sago007 2012-04-17 11:04 73
	Button(const Button& b);
5db03af6 Poul Sander 2018-11-07 19:39 74
	Button& operator=(const Button& other);
792dafd6 sago007 2020-07-01 19:00 75
	virtual ~Button() = default;
c53e6443 sago007 2012-04-17 11:04 76
c53e6443 sago007 2012-04-17 11:04 77
c53e6443 sago007 2012-04-17 11:04 78
	//Set the text to write on the button
33f4e975 sago007 2015-11-14 21:49 79
	void setLabel(const std::string& text);
c53e6443 sago007 2012-04-17 11:04 80
	//Set the action to run
29edda2d sago007 2016-10-08 18:06 81
	void setAction(void (*action2run)(void));
c53e6443 sago007 2012-04-17 11:04 82
c53e6443 sago007 2012-04-17 11:04 83
	virtual void doAction(); //Run the callback function
517ed886 Poul Sander 2025-12-14 14:38 84
	virtual void doLeft() {}; //Handle left input (for adjustment buttons)
517ed886 Poul Sander 2025-12-14 14:38 85
	virtual void doRight() {}; //Handle right input (for adjustment buttons)
b4f1d3cf sago007 2012-08-20 21:28 86
	void setPopOnRun(bool popOnRun);
b4f1d3cf sago007 2012-08-20 21:28 87
	bool isPopOnRun() const;
c99935b0 Poul Sander 2021-08-28 13:13 88
	virtual const std::string& getLabel() const {return this->label; };
517ed886 Poul Sander 2025-12-14 14:38 89
	virtual bool hasAdjustmentButtons() const { return false; };
517ed886 Poul Sander 2025-12-14 14:38 90
	virtual void drawToScreen(SDL_Renderer* target) const;
517ed886 Poul Sander 2025-12-14 14:38 91
	virtual bool isClickedAdjustLeft(int /*x*/, int /*y*/) const { return false; };
517ed886 Poul Sander 2025-12-14 14:38 92
	virtual bool isClickedAdjustRight(int /*x*/, int /*y*/) const { return false; };
517ed886 Poul Sander 2025-12-14 14:38 93
	virtual int getTotalWidth() const { return standardButton.xsize; };
b4f1d3cf sago007 2012-08-20 21:28 94
67a32395 sago007 2012-04-19 18:40 95
	//May hold any other information the callback might need
733af903 sago007 2016-03-24 14:58 96
	int iGeneric1 = 0;
f3ef3495 sago007 2008-11-13 20:00 97
};
f3ef3495 sago007 2008-11-13 20:00 98
517ed886 Poul Sander 2025-12-14 14:38 99
//A button with left/right adjustment controls
517ed886 Poul Sander 2025-12-14 14:38 100
class ButtonWithAdjustment : public Button
517ed886 Poul Sander 2025-12-14 14:38 101
{
517ed886 Poul Sander 2025-12-14 14:38 102
public:
517ed886 Poul Sander 2025-12-14 14:38 103
	ButtonWithAdjustment() = default;
517ed886 Poul Sander 2025-12-14 14:38 104
	virtual ~ButtonWithAdjustment() = default;
517ed886 Poul Sander 2025-12-14 14:38 105
517ed886 Poul Sander 2025-12-14 14:38 106
	virtual bool hasAdjustmentButtons() const override { return true; };
517ed886 Poul Sander 2025-12-14 14:38 107
	virtual void drawToScreen(SDL_Renderer* target) const override;
517ed886 Poul Sander 2025-12-14 14:38 108
	virtual bool isClickedAdjustLeft(int x, int y) const override;
517ed886 Poul Sander 2025-12-14 14:38 109
	virtual bool isClickedAdjustRight(int x, int y) const override;
517ed886 Poul Sander 2025-12-14 14:38 110
	virtual int getTotalWidth() const override {
517ed886 Poul Sander 2025-12-14 14:38 111
		int adjustSize = getAdjustButtonSize();
517ed886 Poul Sander 2025-12-14 14:38 112
		return adjustSize + 10 + standardButton.xsize + 10 + adjustSize;
517ed886 Poul Sander 2025-12-14 14:38 113
	};
517ed886 Poul Sander 2025-12-14 14:38 114
517ed886 Poul Sander 2025-12-14 14:38 115
	int getAdjustButtonSize() const { return standardButton.ysize / 2; };
517ed886 Poul Sander 2025-12-14 14:38 116
};
517ed886 Poul Sander 2025-12-14 14:38 117
26ddb424 sago007 2011-06-09 20:06 118
00e866da sago007 2016-10-31 20:47 119
class Menu : public sago::GameStateInterface
c53e6443 sago007 2012-04-17 11:04 120
{
c53e6443 sago007 2012-04-17 11:04 121
private:
8ff26872 sago007 2016-03-25 09:46 122
	bool isSubmenu = false; //True if the menu is a submenu
8ff26872 sago007 2016-03-25 09:46 123
	int marked = 0; //The index of the marked button (for keyboard up/down)
733af903 sago007 2016-03-24 14:58 124
	bool running = true; //The menu is running. The menu will terminate then this is false
8ff26872 sago007 2016-03-25 09:46 125
	SDL_Renderer *screen = nullptr; //Pointer to the screen to draw to
33f4e975 sago007 2015-11-14 21:49 126
	std::string title;
50fd2c5f sago007 2016-11-12 16:05 127
	bool bMouseUp = false;
c53e6443 sago007 2012-04-17 11:04 128
public:
c53e6443 sago007 2012-04-17 11:04 129
	//numberOfItems is the expected numberOfItems for vector initialization
c53e6443 sago007 2012-04-17 11:04 130
	//SubMenu is true by default
1d2e2129 sago007 2015-12-23 15:41 131
	Menu(SDL_Renderer *screen,bool isSubmenu);
32754436 Poul Sander 2020-08-15 15:12 132
	explicit Menu(SDL_Renderer *screen);
1d2e2129 sago007 2015-12-23 15:41 133
	Menu(SDL_Renderer *screen, const std::string& title, bool isSubmenu);
00e866da sago007 2016-10-31 20:47 134
	virtual ~Menu() {}
792dafd6 sago007 2020-07-01 19:00 135
c53e6443 sago007 2012-04-17 11:04 136
	//Add a button to the menu
c53e6443 sago007 2012-04-17 11:04 137
	void addButton(Button *b);
792dafd6 sago007 2020-07-01 19:00 138
00e866da sago007 2016-10-31 20:47 139
	bool IsActive() override;
00e866da sago007 2016-10-31 20:47 140
	void Draw(SDL_Renderer* target) override;
00e866da sago007 2016-10-31 20:47 141
	void ProcessInput(const SDL_Event& event, bool &processed) override;
50fd2c5f sago007 2016-11-12 16:05 142
	void Update() override;
ddceb7e8 Poul Sander 2023-10-18 11:02 143
	void drawSelf(SDL_Renderer* target);        //Private function to draw the screen
ddceb7e8 Poul Sander 2023-10-18 11:02 144
	virtual void placeButtons(); //Rearanges the buttons to the correct place.
ddceb7e8 Poul Sander 2023-10-18 11:02 145
ddceb7e8 Poul Sander 2023-10-18 11:02 146
	std::vector<Button*> buttons; //Vector holder the buttons
ddceb7e8 Poul Sander 2023-10-18 11:02 147
	Button exit; //The exit button is special since it does not have a callback function
f3ef3495 sago007 2008-11-13 20:00 148
};
f3ef3495 sago007 2008-11-13 20:00 149
c53e6443 sago007 2012-04-17 11:04 150
class FileMenu
c53e6443 sago007 2012-04-17 11:04 151
{
cc3ef158 sago007 2011-07-03 16:13 152
private:
33f4e975 sago007 2015-11-14 21:49 153
	std::string pm_path;
33f4e975 sago007 2015-11-14 21:49 154
	std::string pm_fileending;
8ff26872 sago007 2016-03-25 09:46 155
	bool pm_hidden_files = false;
cc3ef158 sago007 2011-07-03 16:13 156
public:
33f4e975 sago007 2015-11-14 21:49 157
	FileMenu(const std::string& path, const std::string& fileending, bool hidden_files = false);
c53e6443 sago007 2012-04-17 11:04 158
33f4e975 sago007 2015-11-14 21:49 159
	std::string getFile(SDL_Surface **screen);
cc3ef158 sago007 2011-07-03 16:13 160
};
cc3ef158 sago007 2011-07-03 16:13 161
d21782d4 sago007 2016-03-13 11:17 162
bool isUpEvent(const SDL_Event& event);
d21782d4 sago007 2016-03-13 11:17 163
d21782d4 sago007 2016-03-13 11:17 164
bool isDownEvent(const SDL_Event& event);
d21782d4 sago007 2016-03-13 11:17 165
d21782d4 sago007 2016-03-13 11:17 166
bool isLeftEvent(const SDL_Event& event);
d21782d4 sago007 2016-03-13 11:17 167
d21782d4 sago007 2016-03-13 11:17 168
bool isRightEvent(const SDL_Event& event);
d21782d4 sago007 2016-03-13 11:17 169
d21782d4 sago007 2016-03-13 11:17 170
bool isEscapeEvent(const SDL_Event& event);
d21782d4 sago007 2016-03-13 11:17 171
d21782d4 sago007 2016-03-13 11:17 172
bool isConfirmEvent(const SDL_Event& event);
d21782d4 sago007 2016-03-13 11:17 173
f3ef3495 sago007 2008-11-13 20:00 174
#endif	/* _MENUSYSTEM_H */
f3ef3495 sago007 2008-11-13 20:00 175
1970-01-01 00:00 176