viewButtonArea.cpp 2.3 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 11 12 13 14 15
namespace CGoGN
{

namespace SCHNApps
{

Pierre Kraemer's avatar
Pierre Kraemer committed
16
ViewButton::ViewButton(const QString& image, View* view) :
17
	m_img(image),
Pierre Kraemer's avatar
Pierre Kraemer committed
18
	m_view(view)
19
{
20
	m_tex = m_view->getWindow()->getTexture(m_img);
21 22
}

Pierre Kraemer's avatar
Pierre Kraemer committed
23 24
ViewButton::~ViewButton()
{
25 26 27 28 29 30
	m_view->getWindow()->releaseTexture(m_img);
}

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

33
void ViewButton::click(int x, int y, int globalX, int globalY)
Pierre Kraemer's avatar
Pierre Kraemer committed
34
{
35
	emit clicked(x, y, globalX, globalY);
Pierre Kraemer's avatar
Pierre Kraemer committed
36 37
}

38 39
void ViewButton::drawAt(int x, int y)
{
40
	glBindTexture(GL_TEXTURE_2D, m_tex->texID);
41 42 43 44
	glBegin (GL_QUADS);
		glTexCoord2i(0, 1);
		glVertex2i(x, y);
		glTexCoord2i(0, 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
45
		glVertex2i(x, y + SIZE);
46
		glTexCoord2i(1, 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
47
		glVertex2i(x + SIZE, y + SIZE);
48
		glTexCoord2i(1, 1);
Pierre Kraemer's avatar
Pierre Kraemer committed
49
		glVertex2i(x + SIZE, y);
50 51 52
	glEnd();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
53 54


55 56 57

void ViewButtonArea::addButton(ViewButton* button)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	if(!l_buttons.contains(button))
59
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
60 61 62
		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);
63

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
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
72 73 74
		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);
75 76 77
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
78
bool ViewButtonArea::isClicked(int x, int y)
79
{
Pierre Kraemer's avatar
Pierre Kraemer committed
80
	return m_form.contains(x, y);
81 82
}

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

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

void ViewButtonArea::draw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107 108
	int p_x = m_form.x();
	int p_y = m_form.y();

	foreach(ViewButton* b, l_buttons)
109
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111
		b->drawAt(p_x, p_y + ViewButton::SPACE);
		p_x += ViewButton::SIZE + ViewButton::SPACE;
112 113
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115 116 117

} // namespace SCHNApps

} // namespace CGoGN