viewButtonArea.cpp 2.21 KB
Newer Older
1 2
#include "viewButtonArea.h"

3 4 5
#include "window.h"
#include "view.h"
#include "texture.h"
6 7 8 9
#include "camera.h"

#include <iostream>

Pierre Kraemer's avatar
Pierre Kraemer committed
10
ViewButton::ViewButton(const QString& image, View* view) :
11
	m_img(image),
Pierre Kraemer's avatar
Pierre Kraemer committed
12
	m_view(view)
13
{
14
	m_tex = m_view->getWindow()->getTexture(m_img);
15 16
}

Pierre Kraemer's avatar
Pierre Kraemer committed
17 18
ViewButton::~ViewButton()
{
19 20 21 22 23 24
	m_view->getWindow()->releaseTexture(m_img);
}

QSize ViewButton::getSize()
{
	return m_tex->size;
Pierre Kraemer's avatar
Pierre Kraemer committed
25 26
}

27
void ViewButton::click(int x, int y, int globalX, int globalY)
Pierre Kraemer's avatar
Pierre Kraemer committed
28
{
29
	emit clicked(x, y, globalX, globalY);
Pierre Kraemer's avatar
Pierre Kraemer committed
30 31
}

32 33
void ViewButton::drawAt(int x, int y)
{
34
	glBindTexture(GL_TEXTURE_2D, m_tex->texID);
35 36 37 38
	glBegin (GL_QUADS);
		glTexCoord2i(0, 1);
		glVertex2i(x, y);
		glTexCoord2i(0, 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
39
		glVertex2i(x, y + SIZE);
40
		glTexCoord2i(1, 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
41
		glVertex2i(x + SIZE, y + SIZE);
42
		glTexCoord2i(1, 1);
Pierre Kraemer's avatar
Pierre Kraemer committed
43
		glVertex2i(x + SIZE, y);
44 45 46
	glEnd();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
47 48


49 50 51

void ViewButtonArea::addButton(ViewButton* button)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
52
	if(!l_buttons.contains(button))
53
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55 56
		m_form.setWidth(m_form.width() + ViewButton::SIZE + ViewButton::SPACE);
		m_form.moveTopLeft(QPoint(m_form.x() - ViewButton::SIZE - ViewButton::SPACE, m_form.y()));
		m_form.setHeight(ViewButton::SIZE + 2 * ViewButton::SPACE);
57

Pierre Kraemer's avatar
Pierre Kraemer committed
58
		l_buttons.push_back(button);
59 60 61
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
62
void ViewButtonArea::removeButton(ViewButton* button)
63
{
Pierre Kraemer's avatar
Pierre Kraemer committed
64
	if(l_buttons.removeOne(button))
65
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
66 67 68
		m_form.setWidth(m_form.width() - ViewButton::SIZE - ViewButton::SPACE);
		m_form.moveTopLeft(QPoint(m_form.x() + ViewButton::SIZE + ViewButton::SPACE, m_form.y()));
		m_form.setHeight(ViewButton::SIZE + 2 * ViewButton::SPACE);
69 70 71
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
72
bool ViewButtonArea::isClicked(int x, int y)
73
{
Pierre Kraemer's avatar
Pierre Kraemer committed
74
	return m_form.contains(x, y);
75 76
}

77
void ViewButtonArea::clickButton(int x, int y, int globalX, int globalY)
78 79
{
	QPoint p = m_form.topLeft();
Pierre Kraemer's avatar
Pierre Kraemer committed
80
	p.setY(p.y() + ViewButton::SPACE);
Pierre Kraemer's avatar
Pierre Kraemer committed
81
	foreach(ViewButton* b, l_buttons)
82
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
83
		if(QRect(p, QSize(ViewButton::SIZE, ViewButton::SIZE)).contains(x, y))
84
		{
85
			b->click(x, y, globalX, globalY);
86
			return;
87
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
88
		p.setX(p.x() + ViewButton::SPACE + ViewButton::SIZE);
89 90 91 92 93 94 95 96 97 98
	}
}

void ViewButtonArea::setTopRightPosition(int x, int y)
{
	m_form.moveTopRight(QPoint(x,y));
}

void ViewButtonArea::draw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
99 100 101 102
	int p_x = m_form.x();
	int p_y = m_form.y();

	foreach(ViewButton* b, l_buttons)
103
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
104 105
		b->drawAt(p_x, p_y + ViewButton::SPACE);
		p_x += ViewButton::SIZE + ViewButton::SPACE;
106 107
	}
}