1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "GuiElement.h"
#include "Rendering/GL/myGL.h"
namespace agui
{
int GuiElement::screensize[2];
int GuiElement::screenoffset[2];
GuiElement::GuiElement(GuiElement* _parent) : parent(_parent), fixedSize(false), weight(1)
{
size[0] = size[1] = 0.0f;
pos[0] = pos[1] = 0.0f;
if (parent)
parent->AddChild(this);
}
GuiElement::~GuiElement()
{
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
{
delete *it;
}
}
void GuiElement::Draw()
{
DrawSelf();
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
{
(*it)->Draw();
}
}
bool GuiElement::HandleEvent(const SDL_Event& ev)
{
if (HandleEventSelf(ev))
return true;
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
{
if ((*it)->HandleEvent(ev))
return true;
}
return false;
}
bool GuiElement::MouseOver(int x, int y) const
{
float mouse[2] = {PixelToGlX(x), PixelToGlY(y)};
return (mouse[0] >= pos[0] && mouse[0] <= pos[0]+size[0]) && (mouse[1] >= pos[1] && mouse[1] <= pos[1]+size[1]);
}
bool GuiElement::MouseOver(float x, float y) const
{
return (x >= pos[0] && x <= pos[0]+size[0]) && (y >= pos[1] && y <= pos[1]+size[1]);
}
void GuiElement::UpdateDisplayGeo(int x, int y, int xOffset, int yOffset)
{
screensize[0] = x;
screensize[1] = y;
screenoffset[0] = xOffset;
screenoffset[1] = yOffset;
}
float GuiElement::PixelToGlX(int x)
{
return float(x - screenoffset[0])/float(screensize[0]);
}
float GuiElement::PixelToGlY(int y)
{
return 1.0f - float(y - screenoffset[1])/float(screensize[1]);
}
float GuiElement::GlToPixelX(float x)
{
return x*float(screensize[0]) + float(screenoffset[0]);
}
float GuiElement::GlToPixelY(float y)
{
return y*float(screensize[1]) + float(screenoffset[1]);
}
void GuiElement::AddChild(GuiElement* elem)
{
children.push_back(elem);
elem->SetPos(pos[0], pos[1]);
elem->SetSize(size[0], size[1]);
GeometryChange();
}
void GuiElement::SetPos(float x, float y)
{
pos[0] = x;
pos[1] = y;
GeometryChange();
}
void GuiElement::SetSize(float x, float y, bool fixed)
{
size[0] = x;
size[1] = y;
fixedSize = fixed;
GeometryChange();
}
void GuiElement::GeometryChange()
{
GeometryChangeSelf();
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
{
(*it)->GeometryChange();
}
}
float GuiElement::DefaultOpacity() const
{
return 0.8f;
}
void GuiElement::Move(float x, float y)
{
pos[0] += x;
pos[1] += y;
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
{
(*it)->Move(x, y);
}
}
void GuiElement::DrawBox(int how)
{
glBegin(how);
glVertex2f(pos[0], pos[1]);
glVertex2f(pos[0], pos[1]+size[1]);
glVertex2f(pos[0]+size[0], pos[1]+size[1]);
glVertex2f(pos[0]+size[0], pos[1]);
glEnd();
}
}
|