git repos / blockattack-game

blame: source/code/DialogBox.cpp

normal view · raw

59dcb571 sago007 2016-02-14 15:34 1
/*
7a956470 sago007 2016-02-14 17:09 2
===========================================================================
7a956470 sago007 2016-02-14 17:09 3
blockattack - Block Attack - Rise of the Blocks
7a956470 sago007 2016-02-14 17:09 4
Copyright (C) 2005-2016 Poul Sander
7a956470 sago007 2016-02-14 17:09 5
7a956470 sago007 2016-02-14 17:09 6
This program is free software: you can redistribute it and/or modify
7a956470 sago007 2016-02-14 17:09 7
it under the terms of the GNU General Public License as published by
7a956470 sago007 2016-02-14 17:09 8
the Free Software Foundation, either version 2 of the License, or
7a956470 sago007 2016-02-14 17:09 9
(at your option) any later version.
7a956470 sago007 2016-02-14 17:09 10
7a956470 sago007 2016-02-14 17:09 11
This program is distributed in the hope that it will be useful,
7a956470 sago007 2016-02-14 17:09 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
7a956470 sago007 2016-02-14 17:09 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7a956470 sago007 2016-02-14 17:09 14
GNU General Public License for more details.
7a956470 sago007 2016-02-14 17:09 15
7a956470 sago007 2016-02-14 17:09 16
You should have received a copy of the GNU General Public License
7a956470 sago007 2016-02-14 17:09 17
along with this program.  If not, see http://www.gnu.org/licenses/
7a956470 sago007 2016-02-14 17:09 18
7a956470 sago007 2016-02-14 17:09 19
Source information and contacts persons can be found at
7a956470 sago007 2016-02-14 17:09 20
http://www.blockattack.net
7a956470 sago007 2016-02-14 17:09 21
===========================================================================
7a956470 sago007 2016-02-14 17:09 22
*/
59dcb571 sago007 2016-02-14 15:34 23
59dcb571 sago007 2016-02-14 15:34 24
#include "DialogBox.hpp"
59dcb571 sago007 2016-02-14 15:34 25
#include "global.hpp"
59dcb571 sago007 2016-02-14 15:34 26
#include "common.h"
59dcb571 sago007 2016-02-14 15:34 27
#include "ReadKeyboard.h"
59dcb571 sago007 2016-02-14 15:34 28
34f6ec6d sago007 2018-03-29 16:17 29
static void setButtonFont(const sago::SagoDataHolder* holder, sago::SagoTextField& field, const char* text){
34f6ec6d sago007 2018-03-29 16:17 30
	field.SetHolder(holder);
34f6ec6d sago007 2018-03-29 16:17 31
	field.SetFont("freeserif");
34f6ec6d sago007 2018-03-29 16:17 32
	field.SetColor({255,255,255,255});
34f6ec6d sago007 2018-03-29 16:17 33
	field.SetFontSize(24);
34f6ec6d sago007 2018-03-29 16:17 34
	field.SetOutline(1, {0,0,0,255});
34f6ec6d sago007 2018-03-29 16:17 35
	field.SetText(text);
34f6ec6d sago007 2018-03-29 16:17 36
}
34f6ec6d sago007 2018-03-29 16:17 37
59dcb571 sago007 2016-02-14 15:34 38
static void NFont_Write(SDL_Renderer* target, int x, int y, const std::string& text) {
e1b2e653 sago007 2017-11-28 18:10 39
	globalData.standard_blue_font.draw(target, x, y, text);
59dcb571 sago007 2016-02-14 15:34 40
}
59dcb571 sago007 2016-02-14 15:34 41
41f037b2 sago007 2016-04-17 16:37 42
static void DrawRect(SDL_Renderer* target, int topx, int topy, int height, int width, const std::string& name) {
daa660ab sago007 2016-04-16 18:13 43
	const int size = 32;
daa660ab sago007 2016-04-16 18:13 44
	SDL_Rect bounds_ns = {topx, topy+size, width, height-2*size};  //bounds for south
daa660ab sago007 2016-04-16 18:13 45
	SDL_Rect bounds_e = {topx, topy, width-size, height};
72383a9e sago007 2016-10-30 11:56 46
	const sago::SagoSprite& n = globalData.spriteHolder->GetSprite(name+"n");
72383a9e sago007 2016-10-30 11:56 47
	const sago::SagoSprite& s = globalData.spriteHolder->GetSprite(name+"s");
72383a9e sago007 2016-10-30 11:56 48
	const sago::SagoSprite& e = globalData.spriteHolder->GetSprite(name+"e");
72383a9e sago007 2016-10-30 11:56 49
	const sago::SagoSprite& w = globalData.spriteHolder->GetSprite(name+"w");
72383a9e sago007 2016-10-30 11:56 50
	const sago::SagoSprite& fill = globalData.spriteHolder->GetSprite(name+"fill");
daa660ab sago007 2016-04-16 18:13 51
	for (int i = 1; i < width/size; ++i) {
daa660ab sago007 2016-04-16 18:13 52
		n.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy, bounds_e);
daa660ab sago007 2016-04-16 18:13 53
		for (int j = 1; j < height/size; ++j) {
daa660ab sago007 2016-04-16 18:13 54
			w.DrawBounded(target, SDL_GetTicks(), topx, topy+j*size, bounds_ns);
daa660ab sago007 2016-04-16 18:13 55
			fill.Draw(target, SDL_GetTicks(),topx+i*size, topy+j*size);
daa660ab sago007 2016-04-16 18:13 56
			e.DrawBounded(target, SDL_GetTicks(), topx+width-size, topy+j*size, bounds_ns);
daa660ab sago007 2016-04-16 18:13 57
		}
daa660ab sago007 2016-04-16 18:13 58
		s.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy+height-size, bounds_e);
daa660ab sago007 2016-04-16 18:13 59
	}
daa660ab sago007 2016-04-16 18:13 60
	//Corners
72383a9e sago007 2016-10-30 11:56 61
	const sago::SagoSprite& nw = globalData.spriteHolder->GetSprite(name+"nw");
72383a9e sago007 2016-10-30 11:56 62
	const sago::SagoSprite& ne = globalData.spriteHolder->GetSprite(name+"ne");
72383a9e sago007 2016-10-30 11:56 63
	const sago::SagoSprite& se = globalData.spriteHolder->GetSprite(name+"se");
72383a9e sago007 2016-10-30 11:56 64
	const sago::SagoSprite& sw = globalData.spriteHolder->GetSprite(name+"sw");
daa660ab sago007 2016-04-16 18:13 65
	nw.Draw(target, SDL_GetTicks(), topx, topy);
daa660ab sago007 2016-04-16 18:13 66
	ne.Draw(target, SDL_GetTicks(), topx+width-size, topy);
daa660ab sago007 2016-04-16 18:13 67
	se.Draw(target, SDL_GetTicks(), topx+width-size, topy+height-size);
daa660ab sago007 2016-04-16 18:13 68
	sw.Draw(target, SDL_GetTicks(), topx, topy+height-size);
41f037b2 sago007 2016-04-17 16:37 69
}
41f037b2 sago007 2016-04-17 16:37 70
41f037b2 sago007 2016-04-17 16:37 71
static void DrawRectWhite(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 72
	std::string name = "ui_rect_white_";
41f037b2 sago007 2016-04-17 16:37 73
	DrawRect(target, topx, topy, height, width, name);
41f037b2 sago007 2016-04-17 16:37 74
}
41f037b2 sago007 2016-04-17 16:37 75
41f037b2 sago007 2016-04-17 16:37 76
static void DrawRectYellow(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 77
	std::string name = "ui_rect_yellow_";
41f037b2 sago007 2016-04-17 16:37 78
	DrawRect(target, topx, topy, height, width, name);
daa660ab sago007 2016-04-16 18:13 79
}
daa660ab sago007 2016-04-16 18:13 80
96e759d9 sago007 2016-04-17 18:45 81
bool OpenDialogbox(int x, int y, std::string& name, const std::string& header) {
96e759d9 sago007 2016-04-17 18:45 82
	DialogBox d(x, y, name, header);
59dcb571 sago007 2016-02-14 15:34 83
	RunGameState(d);
59dcb571 sago007 2016-02-14 15:34 84
	if (d.IsUpdated()) {
59dcb571 sago007 2016-02-14 15:34 85
		name = d.GetName();
59dcb571 sago007 2016-02-14 15:34 86
		return true;
59dcb571 sago007 2016-02-14 15:34 87
	}
59dcb571 sago007 2016-02-14 15:34 88
	return false;
59dcb571 sago007 2016-02-14 15:34 89
}
59dcb571 sago007 2016-02-14 15:34 90
96e759d9 sago007 2016-04-17 18:45 91
DialogBox::DialogBox(int x, int y, const std::string& name, const std::string& header) : header(header) {
59dcb571 sago007 2016-02-14 15:34 92
	this->x = x;
59dcb571 sago007 2016-02-14 15:34 93
	this->y = y;
59dcb571 sago007 2016-02-14 15:34 94
	SetName(name);
34f6ec6d sago007 2018-03-29 16:17 95
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), headerLabel, header.c_str());
34f6ec6d sago007 2018-03-29 16:17 96
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), enterLabel, _("Enter to accept"));
34f6ec6d sago007 2018-03-29 16:17 97
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), cancelLabel, _("Esc to cancel"));
59dcb571 sago007 2016-02-14 15:34 98
}
59dcb571 sago007 2016-02-14 15:34 99
59dcb571 sago007 2016-02-14 15:34 100
59dcb571 sago007 2016-02-14 15:34 101
DialogBox::~DialogBox() {
59dcb571 sago007 2016-02-14 15:34 102
}
59dcb571 sago007 2016-02-14 15:34 103
59dcb571 sago007 2016-02-14 15:34 104
bool DialogBox::IsActive() {
59dcb571 sago007 2016-02-14 15:34 105
	return isActive;
59dcb571 sago007 2016-02-14 15:34 106
}
59dcb571 sago007 2016-02-14 15:34 107
59dcb571 sago007 2016-02-14 15:34 108
59dcb571 sago007 2016-02-14 15:34 109
void DialogBox::Draw(SDL_Renderer* target) {
72383a9e sago007 2016-10-30 11:56 110
	DrawBackground(globalData.screen);
72383a9e sago007 2016-10-30 11:56 111
	this->x = globalData.xsize/2-300;
72383a9e sago007 2016-10-30 11:56 112
	this->y = globalData.ysize/2-100;
647f12da sago007 2016-09-12 20:07 113
	DrawRectYellow(target, x, y, 200, 600);
34f6ec6d sago007 2018-03-29 16:17 114
	headerLabel.Draw(target, x+300, y+20, sago::SagoTextField::Alignment::center);
34f6ec6d sago007 2018-03-29 16:17 115
	enterLabel.Draw(target, x+150, y+140, sago::SagoTextField::Alignment::center);
34f6ec6d sago007 2018-03-29 16:17 116
	cancelLabel.Draw(target, x+450, y+140, sago::SagoTextField::Alignment::center);
647f12da sago007 2016-09-12 20:07 117
	DrawRectWhite(target, x+26, y+64, 54, 600-2*26);
647f12da sago007 2016-09-12 20:07 118
	NFont_Write(target, x+40, y+76,rk->GetString());
59dcb571 sago007 2016-02-14 15:34 119
	std::string strHolder = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 120
	strHolder.erase((int)rk->CharsBeforeCursor());
59dcb571 sago007 2016-02-14 15:34 121
59dcb571 sago007 2016-02-14 15:34 122
	if (((SDL_GetTicks()/600)%2)==1) {
e1b2e653 sago007 2017-11-28 18:10 123
		NFont_Write(target, x+40+globalData.standard_blue_font.getWidth( strHolder),y+76,"|");
59dcb571 sago007 2016-02-14 15:34 124
	}
59dcb571 sago007 2016-02-14 15:34 125
}
59dcb571 sago007 2016-02-14 15:34 126
c7f2082f sago007 2016-03-13 11:48 127
void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
59dcb571 sago007 2016-02-14 15:34 128
	if (event.type == SDL_TEXTINPUT) {
72383a9e sago007 2016-10-30 11:56 129
		if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 130
			Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 131
		}
59dcb571 sago007 2016-02-14 15:34 132
	}
59dcb571 sago007 2016-02-14 15:34 133
59dcb571 sago007 2016-02-14 15:34 134
	if ( event.type == SDL_KEYDOWN ) {
59dcb571 sago007 2016-02-14 15:34 135
		if ( (event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER) ) {
59dcb571 sago007 2016-02-14 15:34 136
			name = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 137
			updated = true;
59dcb571 sago007 2016-02-14 15:34 138
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 139
		}
59dcb571 sago007 2016-02-14 15:34 140
		else if ( (event.key.keysym.sym == SDLK_ESCAPE) ) {
59dcb571 sago007 2016-02-14 15:34 141
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 142
		}
59dcb571 sago007 2016-02-14 15:34 143
		else {
72383a9e sago007 2016-10-30 11:56 144
			if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 145
				Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 146
			}
59dcb571 sago007 2016-02-14 15:34 147
		}
59dcb571 sago007 2016-02-14 15:34 148
	}
7a956470 sago007 2016-02-14 17:09 149
	processed = true;
59dcb571 sago007 2016-02-14 15:34 150
}
59dcb571 sago007 2016-02-14 15:34 151
59dcb571 sago007 2016-02-14 15:34 152
void DialogBox::SetName(const std::string& name) {
59dcb571 sago007 2016-02-14 15:34 153
	this->name = name;
59dcb571 sago007 2016-02-14 15:34 154
	rk = std::make_shared<ReadKeyboard>(name.c_str());
59dcb571 sago007 2016-02-14 15:34 155
}
59dcb571 sago007 2016-02-14 15:34 156
59dcb571 sago007 2016-02-14 15:34 157
std::string DialogBox::GetName() const {
59dcb571 sago007 2016-02-14 15:34 158
	return name;
59dcb571 sago007 2016-02-14 15:34 159
}
59dcb571 sago007 2016-02-14 15:34 160
59dcb571 sago007 2016-02-14 15:34 161
bool DialogBox::IsUpdated() const {
59dcb571 sago007 2016-02-14 15:34 162
	return updated;
b1a7bd3e Poul Sander 2016-12-19 21:53 163
}
1970-01-01 00:00 164