viewButtonArea.cpp 2.34 KB
Newer Older
1 2 3 4 5 6
#include "viewButtonArea.h"

#include "camera.h"

#include <iostream>

Pierre Kraemer's avatar
Pierre Kraemer committed
7 8
ViewButton::ViewButton(const QString& image, View* view) :
	m_view(view)
9 10 11 12 13 14
{
	m_GLimg.load(image);
	if(!m_GLimg.isNull())
	{
		m_size = m_GLimg.size();
		m_GLimg = QGLWidget::convertToGLFormat(m_GLimg);
Pierre Kraemer's avatar
Pierre Kraemer committed
15
		m_texID = m_view->bindTexture(m_GLimg, GL_TEXTURE_2D, GL_RGBA);
16 17 18
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
19 20 21 22 23 24 25 26 27 28
ViewButton::~ViewButton()
{
	m_view->deleteTexture(m_texID);
}

void ViewButton::click()
{
	emit clicked();
}

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
void ViewButton::drawAt(int x, int y)
{
	glBindTexture(GL_TEXTURE_2D, m_texID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_GLimg.bits());
	glBegin (GL_QUADS);
		glTexCoord2i(0, 1);
		glVertex2i(x, y);
		glTexCoord2i(0, 0);
		glVertex2i(x, y + m_size.height());
		glTexCoord2i(1, 0);
		glVertex2i(x + m_size.width(), y + m_size.height());
		glTexCoord2i(1, 1);
		glVertex2i(x + m_size.width(), y);
	glEnd();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
45 46


47 48 49

void ViewButtonArea::addButton(ViewButton* button)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	if(!l_buttons.contains(button))
51 52 53 54 55 56 57 58 59
	{
		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
60
		l_buttons.push_back(button);
61 62 63
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
64
void ViewButtonArea::removeButton(ViewButton* button)
65
{
Pierre Kraemer's avatar
Pierre Kraemer committed
66
	if(l_buttons.removeOne(button))
67 68 69 70 71 72
	{
		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
73 74
		int h_max = l_buttons.first()->getSize().height();
		foreach(ViewButton* b, l_buttons)
75 76
		{
			int h = b->getSize().height();
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78
			if(h > h_max)
				h_max = h;
79
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
80
		m_form.setHeight(h_max + 6);
81 82 83
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
84
bool ViewButtonArea::isClicked(int x, int y)
85
{
Pierre Kraemer's avatar
Pierre Kraemer committed
86
	return m_form.contains(x, y);
87 88
}

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

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

void ViewButtonArea::draw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115
	int p_x = m_form.x();
	int p_y = m_form.y();

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