git repos / blockattack-game

blame: source/code/ShowFileState.cpp

normal view · raw

b7550ffe sago007 2018-05-19 10:08 1
/*
b7550ffe sago007 2018-05-19 10:08 2
===========================================================================
b7550ffe sago007 2018-05-19 10:08 3
blockattack - Block Attack - Rise of the Blocks
b7550ffe sago007 2018-05-19 10:08 4
Copyright (C) 2005-2018 Poul Sander
b7550ffe sago007 2018-05-19 10:08 5
This program is free software: you can redistribute it and/or modify
b7550ffe sago007 2018-05-19 10:08 6
it under the terms of the GNU General Public License as published by
b7550ffe sago007 2018-05-19 10:08 7
the Free Software Foundation, either version 2 of the License, or
b7550ffe sago007 2018-05-19 10:08 8
(at your option) any later version.
b7550ffe sago007 2018-05-19 10:08 9
This program is distributed in the hope that it will be useful,
b7550ffe sago007 2018-05-19 10:08 10
but WITHOUT ANY WARRANTY; without even the implied warranty of
b7550ffe sago007 2018-05-19 10:08 11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b7550ffe sago007 2018-05-19 10:08 12
GNU General Public License for more details.
b7550ffe sago007 2018-05-19 10:08 13
You should have received a copy of the GNU General Public License
b7550ffe sago007 2018-05-19 10:08 14
along with this program.  If not, see http://www.gnu.org/licenses/
b7550ffe sago007 2018-05-19 10:08 15
Source information and contacts persons can be found at
b7550ffe sago007 2018-05-19 10:08 16
https://blockattack.net
b7550ffe sago007 2018-05-19 10:08 17
===========================================================================
b7550ffe sago007 2018-05-19 10:08 18
*/
b7550ffe sago007 2018-05-19 10:08 19
d7f4a888 sago007 2020-06-28 09:22 20
#include "DialogBox.hpp"
46404443 sago007 2020-06-28 08:53 21
#include "HelpCommon.hpp"
b7550ffe sago007 2018-05-19 10:08 22
#include "ShowFileState.hpp"
b7550ffe sago007 2018-05-19 10:08 23
#include "global.hpp"
b7550ffe sago007 2018-05-19 10:08 24
#include "common.h"
b7550ffe sago007 2018-05-19 10:08 25
#include "MenuSystem.h"
16476649 Poul Sander 2022-01-09 10:29 26
#include <fmt/core.h>
b7550ffe sago007 2018-05-19 10:08 27
b7550ffe sago007 2018-05-19 10:08 28
const int buttonOffset = 160;
b7550ffe sago007 2018-05-19 10:08 29
extern sago::SagoSprite bExit;
b7550ffe sago007 2018-05-19 10:08 30
c2086a7e sago007 2018-05-19 14:16 31
static void setHelpGamepadFont(const sago::SagoDataHolder* holder, sago::SagoTextField& field, const char* text) {
c2086a7e sago007 2018-05-19 14:16 32
	field.SetHolder(holder);
c2086a7e sago007 2018-05-19 14:16 33
	field.SetFont("freeserif");
c2086a7e sago007 2018-05-19 14:16 34
	field.SetColor({255,255,255,255});
c2086a7e sago007 2018-05-19 14:16 35
	field.SetFontSize(30);
c2086a7e sago007 2018-05-19 14:16 36
	field.SetOutline(1, {128,128,128,255});
c2086a7e sago007 2018-05-19 14:16 37
	field.SetText(text);
c2086a7e sago007 2018-05-19 14:16 38
}
c2086a7e sago007 2018-05-19 14:16 39
b7550ffe sago007 2018-05-19 10:08 40
ShowFileState::ShowFileState() {
c2086a7e sago007 2018-05-19 14:16 41
	setHelpGamepadFont(&globalData.spriteHolder->GetDataHolder(), titleField, "");
46404443 sago007 2020-06-28 08:53 42
	setHelpBoxFont(&globalData.spriteHolder->GetDataHolder(), infoBox, "");
46404443 sago007 2020-06-28 08:53 43
	setHelpBoxFont(&globalData.spriteHolder->GetDataHolder(), filenameField, "");
b7550ffe sago007 2018-05-19 10:08 44
}
b7550ffe sago007 2018-05-19 10:08 45
b7550ffe sago007 2018-05-19 10:08 46
ShowFileState::~ShowFileState() {
b7550ffe sago007 2018-05-19 10:08 47
}
b7550ffe sago007 2018-05-19 10:08 48
16476649 Poul Sander 2022-01-09 10:29 49
void ShowFileState::SetData(const std::string& filename, const std::string& header) {
16476649 Poul Sander 2022-01-09 10:29 50
	titleField.SetText(header);
16476649 Poul Sander 2022-01-09 10:29 51
	infoBox.SetText(sago::GetFileContent(filename));
16476649 Poul Sander 2022-01-09 10:29 52
	filenameField.SetText(fmt::format(_("Showing content of: {}"), filename));
16476649 Poul Sander 2022-01-09 10:29 53
}
16476649 Poul Sander 2022-01-09 10:29 54
b7550ffe sago007 2018-05-19 10:08 55
bool ShowFileState::IsActive() {
b7550ffe sago007 2018-05-19 10:08 56
	return isActive;
b7550ffe sago007 2018-05-19 10:08 57
}
b7550ffe sago007 2018-05-19 10:08 58
b7550ffe sago007 2018-05-19 10:08 59
void ShowFileState::ProcessInput(const SDL_Event& event, bool& processed) {
b7550ffe sago007 2018-05-19 10:08 60
b7550ffe sago007 2018-05-19 10:08 61
	UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
b7550ffe sago007 2018-05-19 10:08 62
b7550ffe sago007 2018-05-19 10:08 63
	if (isConfirmEvent(event) || isEscapeEvent(event)) {
b7550ffe sago007 2018-05-19 10:08 64
		isActive = false;
b7550ffe sago007 2018-05-19 10:08 65
		processed = true;
b7550ffe sago007 2018-05-19 10:08 66
	}
b7550ffe sago007 2018-05-19 10:08 67
}
b7550ffe sago007 2018-05-19 10:08 68
b7550ffe sago007 2018-05-19 10:08 69
void ShowFileState::Draw(SDL_Renderer* target) {
b7550ffe sago007 2018-05-19 10:08 70
	DrawBackground(target);
c2086a7e sago007 2018-05-19 14:16 71
	titleField.Draw(target, 50, 50);
c2086a7e sago007 2018-05-19 14:16 72
	DrawRectYellow(target, 40, 90, 600, 900);
c2086a7e sago007 2018-05-19 14:16 73
	infoBox.SetMaxWidth(850);
c2086a7e sago007 2018-05-19 14:16 74
	infoBox.Draw(target, 50, 100);
c2086a7e sago007 2018-05-19 14:16 75
	DrawRectYellow(target, 40, 700, 50, 900);
c2086a7e sago007 2018-05-19 14:16 76
	filenameField.Draw(target, 50, 715);
4b4edc1a Poul Sander 2019-04-24 17:02 77
	bExit.Draw(globalData.screen, SDL_GetTicks(), globalData.xsize-buttonOffset, globalData.ysize-buttonOffset);
b7550ffe sago007 2018-05-19 10:08 78
#if DEBUG
b7550ffe sago007 2018-05-19 10:08 79
	static sago::SagoTextField mousePos;
b7550ffe sago007 2018-05-19 10:08 80
	mousePos.SetHolder(&globalData.spriteHolder->GetDataHolder());
b7550ffe sago007 2018-05-19 10:08 81
	mousePos.SetFontSize(16);
b7550ffe sago007 2018-05-19 10:08 82
	mousePos.SetOutline(1, {128,128,128,255});
b7550ffe sago007 2018-05-19 10:08 83
	mousePos.SetText(std::string("Mouse position: ")+std::to_string(globalData.mousex)+std::string(", ")+std::to_string(globalData.mousey));
b7550ffe sago007 2018-05-19 10:08 84
	mousePos.Draw(target, 0,0);
b7550ffe sago007 2018-05-19 10:08 85
#endif
b7550ffe sago007 2018-05-19 10:08 86
}
b7550ffe sago007 2018-05-19 10:08 87
b7550ffe sago007 2018-05-19 10:08 88
void ShowFileState::Update() {
b7550ffe sago007 2018-05-19 10:08 89
	// If the mouse button is released, make bMouseUp equal true
b7550ffe sago007 2018-05-19 10:08 90
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
b7550ffe sago007 2018-05-19 10:08 91
		bMouseUp=true;
b7550ffe sago007 2018-05-19 10:08 92
	}
b7550ffe sago007 2018-05-19 10:08 93
b7550ffe sago007 2018-05-19 10:08 94
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
b7550ffe sago007 2018-05-19 10:08 95
		bMouseUp = false;
b7550ffe sago007 2018-05-19 10:08 96
b7550ffe sago007 2018-05-19 10:08 97
		//The Score button:
4b4edc1a Poul Sander 2019-04-24 17:02 98
		if ((globalData.mousex>globalData.xsize-buttonOffset) && (globalData.mousex<globalData.xsize-buttonOffset+bExit.GetWidth())
4b4edc1a Poul Sander 2019-04-24 17:02 99
		        && (globalData.mousey>globalData.ysize-buttonOffset) && (globalData.mousey<globalData.ysize-buttonOffset+bExit.GetHeight())) {
b7550ffe sago007 2018-05-19 10:08 100
			isActive = false;
b7550ffe sago007 2018-05-19 10:08 101
		}
b7550ffe sago007 2018-05-19 10:08 102
b7550ffe sago007 2018-05-19 10:08 103
	}
46404443 sago007 2020-06-28 08:53 104
}
1970-01-01 00:00 105