viewButtonArea.cpp 2.38 KB
Newer Older
1 2 3 4
#include "viewButtonArea.h"

#include "camera.h"
#include "scene.h"
5
//#include "context.h"
6 7 8

#include <iostream>

Pierre Kraemer's avatar
Pierre Kraemer committed
9 10
ViewButton::ViewButton(const QString& image, View* view) :
	m_view(view)
11 12 13 14 15 16
{
	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
17
		m_texID = m_view->bindTexture(m_GLimg, GL_TEXTURE_2D, GL_RGBA);
18 19 20
	}
}

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

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

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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
47 48


49 50 51

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

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

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

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

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