viewButtonArea.cpp 2.33 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 35 36
	int w = m_tex->size.width();
	int h = m_tex->size.height();
	glBindTexture(GL_TEXTURE_2D, m_tex->texID);
37 38 39 40
	glBegin (GL_QUADS);
		glTexCoord2i(0, 1);
		glVertex2i(x, y);
		glTexCoord2i(0, 0);
41
		glVertex2i(x, y + h);
42
		glTexCoord2i(1, 0);
43
		glVertex2i(x + w, y + h);
44
		glTexCoord2i(1, 1);
45
		glVertex2i(x + w, y);
46 47 48
	glEnd();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
49 50


51 52 53

void ViewButtonArea::addButton(ViewButton* button)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	if(!l_buttons.contains(button))
55 56 57 58 59 60 61 62 63
	{
		QSize b_size = button->getSize();

		m_form.setWidth(m_form.width() + b_size.width() + 3);
		m_form.moveTopLeft(QPoint(m_form.x() - b_size.width() - 3, m_form.y()));

		if(b_size.height() > m_form.height())
			m_form.setHeight(b_size.height() + 6);

Pierre Kraemer's avatar
Pierre Kraemer committed
64
		l_buttons.push_back(button);
65 66 67
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
68
void ViewButtonArea::removeButton(ViewButton* button)
69
{
Pierre Kraemer's avatar
Pierre Kraemer committed
70
	if(l_buttons.removeOne(button))
71 72 73 74 75 76
	{
		QSize b_size = button->getSize();

		m_form.setWidth(m_form.width() - b_size.width() - 3);
		m_form.moveTopLeft(QPoint(m_form.x() + b_size.width() + 3, m_form.y()));

Pierre Kraemer's avatar
Pierre Kraemer committed
77 78
		int h_max = l_buttons.first()->getSize().height();
		foreach(ViewButton* b, l_buttons)
79 80
		{
			int h = b->getSize().height();
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82
			if(h > h_max)
				h_max = h;
83
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
84
		m_form.setHeight(h_max + 6);
85 86 87
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
88
bool ViewButtonArea::isClicked(int x, int y)
89
{
Pierre Kraemer's avatar
Pierre Kraemer committed
90
	return m_form.contains(x, y);
91 92
}

93
void ViewButtonArea::clickButton(int x, int y, int globalX, int globalY)
94 95 96
{
	QPoint p = m_form.topLeft();
	p.setY(p.y() + 3);
Pierre Kraemer's avatar
Pierre Kraemer committed
97
	foreach(ViewButton* b, l_buttons)
98
	{
99
		if(QRect(p, b->getSize()).contains(x, y))
100
		{
101
			b->click(x, y, globalX, globalY);
102
			return;
103
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
104
		p.setX(p.x() + 3 + b->getSize().width());
105 106 107 108 109 110 111 112 113 114
	}
}

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

void ViewButtonArea::draw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117 118
	int p_x = m_form.x();
	int p_y = m_form.y();

	foreach(ViewButton* b, l_buttons)
119
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
		b->drawAt(p_x, p_y + 3);
		p_x += b->getSize().width() + 3;
122 123
	}
}