File: Window.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (131 lines) | stat: -rw-r--r-- 2,837 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <cstring> // memcpy

#include "Window.h"
#include "Gui.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/VAO.h"
#include "Rendering/GL/VBO.h"
#include "Rendering/GL/VertexArrayTypes.h"
#include "Rendering/Fonts/glFont.h"

namespace agui
{

Window::Window(const std::string& _title, GuiElement* parent) : GuiElement(parent), title(_title)
{
	titleHeight = 0.05f;
	dragging = false;

	pos[0] = pos[1] = 0.2f;
	size[0] = size[1] = 0.3f;

	dragPos[0] = dragPos[1] = 0.0f;

	GeometryChangeSelf();
}

void Window::AddChild(GuiElement* elem)
{
	children.push_back(elem);

	elem->SetPos(pos[0], pos[1]);
	elem->SetSize(size[0], size[1] - titleHeight);
}



void Window::DrawSelf()
{
	const float opacity = Opacity();

	// background
	gui->SetDrawMode(Gui::DrawMode::COLOR);
	gui->SetColor(0.0f, 0.0f, 0.0f, opacity);
	DrawBox();
	// foreground
	gui->SetColor(0.7f, 0.7f, 0.7f, opacity);
	DrawBox(1);
	// edges
	gui->SetColor(1.0f, 1.0f, 1.0f, opacity);
	DrawOutline();

	// string content
	gui->SetDrawMode(Gui::DrawMode::FONT);
	font->SetTextColor(1.0f, 1.0f, 1.0f, opacity);
	font->SetOutlineColor(0.0f, 0.0f, 0.0f, opacity);
	font->SetTextDepth(depth);
	font->glPrint(pos[0] + 0.01f, pos[1] + size[1] - titleHeight * 0.5f, 1.0f, FONT_VCENTER | FONT_SCALE | FONT_SHADOW | FONT_NORM | FONT_BUFFERED, title);
	font->DrawBufferedGL4(gui->GetShader());
}


void Window::GeometryChangeSelf()
{
	GuiElement::GeometryChangeSelf();

	{
		VA_TYPE_T vaElems[4];
		for (int i = 0; i < 4; ++i) {
			const bool bottom = (i & 1) == 0;
			const bool right = (i & 2) == 0;
			vaElems[i].p.x = pos[0] + right * size[0];
			vaElems[i].p.y = pos[1] + size[1] - (!bottom) * titleHeight;
			vaElems[i].p.z = depth;
			vaElems[i].s = 0.0f;
			vaElems[i].t = 0.0f;
		}

		GeometryChangeSelfRaw(1, sizeof(vaElems), vaElems);
	}
}


bool Window::HandleEventSelf(const SDL_Event& ev)
{
	switch (ev.type) {
		case SDL_MOUSEBUTTONDOWN: {
			if (MouseOver(ev.button.x, ev.button.y)) {
				const float mouse[2] = {PixelToGlX(ev.button.x), PixelToGlY(ev.button.y)};

				if (mouse[1] > (pos[1] + size[1] - titleHeight)) {
					dragPos[0] = mouse[0] - pos[0];
					dragPos[1] = mouse[1] - pos[1];
					dragging = true;
					return true;
				};
			}
		} break;
		case SDL_MOUSEBUTTONUP: {
			if (dragging) {
				dragging = false;
				return true;
			}
		} break;
		case SDL_MOUSEMOTION: {
			if (dragging) {
				Move(PixelToGlX(ev.motion.xrel), PixelToGlY(ev.motion.yrel) - 1);
				return true;
			}
		} break;
		case SDL_KEYDOWN: {
			if (ev.key.keysym.sym == SDLK_ESCAPE) {
				WantClose.emit();
				return true;
			}
		} break;
	}

	return false;
}

float Window::Opacity() const
{
	if (dragging)
		return (GuiElement::Opacity() * 0.5f);

	return GuiElement::Opacity();
}

}