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
59dcb571 sago007 2016-02-14 15:34 29
static void NFont_Write(SDL_Renderer* target, int x, int y, const std::string& text) {
59dcb571 sago007 2016-02-14 15:34 30
	nf_standard_blue_font.draw(target, x, y, "%s", text.c_str());
59dcb571 sago007 2016-02-14 15:34 31
}
59dcb571 sago007 2016-02-14 15:34 32
41f037b2 sago007 2016-04-17 16:37 33
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 34
	const int size = 32;
daa660ab sago007 2016-04-16 18:13 35
	SDL_Rect bounds_ns = {topx, topy+size, width, height-2*size};  //bounds for south
daa660ab sago007 2016-04-16 18:13 36
	SDL_Rect bounds_e = {topx, topy, width-size, height};
daa660ab sago007 2016-04-16 18:13 37
	const sago::SagoSprite& n = spriteHolder->GetSprite(name+"n");
daa660ab sago007 2016-04-16 18:13 38
	const sago::SagoSprite& s = spriteHolder->GetSprite(name+"s");
daa660ab sago007 2016-04-16 18:13 39
	const sago::SagoSprite& e = spriteHolder->GetSprite(name+"e");
daa660ab sago007 2016-04-16 18:13 40
	const sago::SagoSprite& w = spriteHolder->GetSprite(name+"w");
daa660ab sago007 2016-04-16 18:13 41
	const sago::SagoSprite& fill = spriteHolder->GetSprite(name+"fill");
daa660ab sago007 2016-04-16 18:13 42
	for (int i = 1; i < width/size; ++i) {
daa660ab sago007 2016-04-16 18:13 43
		n.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy, bounds_e);
daa660ab sago007 2016-04-16 18:13 44
		for (int j = 1; j < height/size; ++j) {
daa660ab sago007 2016-04-16 18:13 45
			w.DrawBounded(target, SDL_GetTicks(), topx, topy+j*size, bounds_ns);
daa660ab sago007 2016-04-16 18:13 46
			fill.Draw(target, SDL_GetTicks(),topx+i*size, topy+j*size);
daa660ab sago007 2016-04-16 18:13 47
			e.DrawBounded(target, SDL_GetTicks(), topx+width-size, topy+j*size, bounds_ns);
daa660ab sago007 2016-04-16 18:13 48
		}
daa660ab sago007 2016-04-16 18:13 49
		s.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy+height-size, bounds_e);
daa660ab sago007 2016-04-16 18:13 50
	}
daa660ab sago007 2016-04-16 18:13 51
	//Corners
daa660ab sago007 2016-04-16 18:13 52
	const sago::SagoSprite& nw = spriteHolder->GetSprite(name+"nw");
daa660ab sago007 2016-04-16 18:13 53
	const sago::SagoSprite& ne = spriteHolder->GetSprite(name+"ne");
daa660ab sago007 2016-04-16 18:13 54
	const sago::SagoSprite& se = spriteHolder->GetSprite(name+"se");
daa660ab sago007 2016-04-16 18:13 55
	const sago::SagoSprite& sw = spriteHolder->GetSprite(name+"sw");
daa660ab sago007 2016-04-16 18:13 56
	nw.Draw(target, SDL_GetTicks(), topx, topy);
daa660ab sago007 2016-04-16 18:13 57
	ne.Draw(target, SDL_GetTicks(), topx+width-size, topy);
daa660ab sago007 2016-04-16 18:13 58
	se.Draw(target, SDL_GetTicks(), topx+width-size, topy+height-size);
daa660ab sago007 2016-04-16 18:13 59
	sw.Draw(target, SDL_GetTicks(), topx, topy+height-size);
41f037b2 sago007 2016-04-17 16:37 60
}
41f037b2 sago007 2016-04-17 16:37 61
41f037b2 sago007 2016-04-17 16:37 62
static void DrawRectWhite(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 63
	std::string name = "ui_rect_white_";
41f037b2 sago007 2016-04-17 16:37 64
	DrawRect(target, topx, topy, height, width, name);
41f037b2 sago007 2016-04-17 16:37 65
}
41f037b2 sago007 2016-04-17 16:37 66
41f037b2 sago007 2016-04-17 16:37 67
static void DrawRectYellow(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 68
	std::string name = "ui_rect_yellow_";
41f037b2 sago007 2016-04-17 16:37 69
	DrawRect(target, topx, topy, height, width, name);
daa660ab sago007 2016-04-16 18:13 70
}
daa660ab sago007 2016-04-16 18:13 71
96e759d9 sago007 2016-04-17 18:45 72
bool OpenDialogbox(int x, int y, std::string& name, const std::string& header) {
96e759d9 sago007 2016-04-17 18:45 73
	DialogBox d(x, y, name, header);
59dcb571 sago007 2016-02-14 15:34 74
	RunGameState(d);
59dcb571 sago007 2016-02-14 15:34 75
	if (d.IsUpdated()) {
59dcb571 sago007 2016-02-14 15:34 76
		name = d.GetName();
59dcb571 sago007 2016-02-14 15:34 77
		return true;
59dcb571 sago007 2016-02-14 15:34 78
	}
59dcb571 sago007 2016-02-14 15:34 79
	return false;
59dcb571 sago007 2016-02-14 15:34 80
}
59dcb571 sago007 2016-02-14 15:34 81
96e759d9 sago007 2016-04-17 18:45 82
DialogBox::DialogBox(int x, int y, const std::string& name, const std::string& header) : header(header) {
59dcb571 sago007 2016-02-14 15:34 83
	this->x = x;
59dcb571 sago007 2016-02-14 15:34 84
	this->y = y;
59dcb571 sago007 2016-02-14 15:34 85
	SetName(name);
59dcb571 sago007 2016-02-14 15:34 86
}
59dcb571 sago007 2016-02-14 15:34 87
59dcb571 sago007 2016-02-14 15:34 88
59dcb571 sago007 2016-02-14 15:34 89
DialogBox::~DialogBox() {
59dcb571 sago007 2016-02-14 15:34 90
}
59dcb571 sago007 2016-02-14 15:34 91
59dcb571 sago007 2016-02-14 15:34 92
bool DialogBox::IsActive() {
59dcb571 sago007 2016-02-14 15:34 93
	return isActive;
59dcb571 sago007 2016-02-14 15:34 94
}
59dcb571 sago007 2016-02-14 15:34 95
59dcb571 sago007 2016-02-14 15:34 96
59dcb571 sago007 2016-02-14 15:34 97
void DialogBox::Draw(SDL_Renderer* target) {
59dcb571 sago007 2016-02-14 15:34 98
	backgroundImage.Draw(screen, SDL_GetTicks(), 0, 0);
7a792f2c sago007 2016-04-17 18:30 99
	DrawRectYellow(screen, x, y, 200, 600);
96e759d9 sago007 2016-04-17 18:45 100
	nf_button_font.draw(screen, x+300, y+20, NFont::CENTER, "%s", header.c_str());
7a792f2c sago007 2016-04-17 18:30 101
	nf_button_font.draw(screen, x+150, y+140, NFont::CENTER, _("Enter to accept"));
7a792f2c sago007 2016-04-17 18:30 102
	nf_button_font.draw(screen, x+450, y+140, NFont::CENTER, _("Esc to cancel"));
7a792f2c sago007 2016-04-17 18:30 103
	DrawRectWhite(screen, x+26, y+64, 54, 600-2*26);
7a792f2c sago007 2016-04-17 18:30 104
	NFont_Write(screen, x+40, y+76,rk->GetString());
59dcb571 sago007 2016-02-14 15:34 105
	std::string strHolder = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 106
	strHolder.erase((int)rk->CharsBeforeCursor());
59dcb571 sago007 2016-02-14 15:34 107
59dcb571 sago007 2016-02-14 15:34 108
	if (((SDL_GetTicks()/600)%2)==1) {
59dcb571 sago007 2016-02-14 15:34 109
		NFont_Write(screen, x+40+nf_standard_blue_font.getWidth( "%s", strHolder.c_str()),y+76,"|");
59dcb571 sago007 2016-02-14 15:34 110
	}
59dcb571 sago007 2016-02-14 15:34 111
}
59dcb571 sago007 2016-02-14 15:34 112
c7f2082f sago007 2016-03-13 11:48 113
void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
59dcb571 sago007 2016-02-14 15:34 114
	if (event.type == SDL_TEXTINPUT) {
59dcb571 sago007 2016-02-14 15:34 115
		if ((rk->ReadKey(event))&&(SoundEnabled)&&(!NoSound)) {
59dcb571 sago007 2016-02-14 15:34 116
			Mix_PlayChannel(1, typingChunk, 0);
59dcb571 sago007 2016-02-14 15:34 117
		}
59dcb571 sago007 2016-02-14 15:34 118
	}
59dcb571 sago007 2016-02-14 15:34 119
59dcb571 sago007 2016-02-14 15:34 120
	if ( event.type == SDL_KEYDOWN ) {
59dcb571 sago007 2016-02-14 15:34 121
		if ( (event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER) ) {
59dcb571 sago007 2016-02-14 15:34 122
			name = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 123
			updated = true;
59dcb571 sago007 2016-02-14 15:34 124
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 125
		}
59dcb571 sago007 2016-02-14 15:34 126
		else if ( (event.key.keysym.sym == SDLK_ESCAPE) ) {
59dcb571 sago007 2016-02-14 15:34 127
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 128
		}
59dcb571 sago007 2016-02-14 15:34 129
		else {
59dcb571 sago007 2016-02-14 15:34 130
			if ((rk->ReadKey(event))&&(SoundEnabled)&&(!NoSound)) {
59dcb571 sago007 2016-02-14 15:34 131
				Mix_PlayChannel(1,typingChunk,0);
59dcb571 sago007 2016-02-14 15:34 132
			}
59dcb571 sago007 2016-02-14 15:34 133
		}
59dcb571 sago007 2016-02-14 15:34 134
	}
7a956470 sago007 2016-02-14 17:09 135
	processed = true;
59dcb571 sago007 2016-02-14 15:34 136
}
59dcb571 sago007 2016-02-14 15:34 137
59dcb571 sago007 2016-02-14 15:34 138
void DialogBox::SetName(const std::string& name) {
59dcb571 sago007 2016-02-14 15:34 139
	this->name = name;
59dcb571 sago007 2016-02-14 15:34 140
	rk = std::make_shared<ReadKeyboard>(name.c_str());
59dcb571 sago007 2016-02-14 15:34 141
}
59dcb571 sago007 2016-02-14 15:34 142
59dcb571 sago007 2016-02-14 15:34 143
std::string DialogBox::GetName() const {
59dcb571 sago007 2016-02-14 15:34 144
	return name;
59dcb571 sago007 2016-02-14 15:34 145
}
59dcb571 sago007 2016-02-14 15:34 146
59dcb571 sago007 2016-02-14 15:34 147
bool DialogBox::IsUpdated() const {
59dcb571 sago007 2016-02-14 15:34 148
	return updated;
59dcb571 sago007 2016-02-14 15:34 149
}