Skip to content

Commit

Permalink
Small refactoring and bugfixing of GuiElement
Browse files Browse the repository at this point in the history
  • Loading branch information
lhog committed Dec 21, 2021
1 parent e909643 commit e816806
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
28 changes: 11 additions & 17 deletions rts/aGui/GuiElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "GuiElement.h"

#include "Rendering/GL/myGL.h"
#include "System/SafeUtil.h"

namespace agui
{
Expand All @@ -21,29 +22,26 @@ GuiElement::GuiElement(GuiElement* _parent) : parent(_parent), fixedSize(false),

GuiElement::~GuiElement()
{
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
{
if (*it == nullptr) //can be nullptr when spring errors out with some message
delete* it;
}
for (auto& ch: children)
spring::SafeDelete(ch);
}

void GuiElement::Draw()
{
DrawSelf();
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
for (auto ch: children)
{
(*it)->Draw();
ch->Draw();
}
}

bool GuiElement::HandleEvent(const SDL_Event& ev)
{
if (HandleEventSelf(ev))
return true;
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
for (auto ch : children)
{
if ((*it)->HandleEvent(ev))
if (ch->HandleEvent(ev))
return true;
}
return false;
Expand Down Expand Up @@ -114,10 +112,8 @@ void GuiElement::SetSize(float x, float y, bool fixed)
void GuiElement::GeometryChange()
{
GeometryChangeSelf();
for (ChildList::iterator it = children.begin(); it != children.end(); ++it)
{
(*it)->GeometryChange();
}
for (auto ch : children)
ch->GeometryChange();
}

float GuiElement::DefaultOpacity() const
Expand All @@ -129,10 +125,8 @@ 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);
}
for (auto ch : children)
ch->Move(x, y);
}

void GuiElement::DrawBox(int how)
Expand Down
24 changes: 12 additions & 12 deletions rts/aGui/GuiElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef GUIELEMENT_H
#define GUIELEMENT_H

#include <list>
#include <vector>
#include <SDL_events.h>

namespace agui
Expand All @@ -12,22 +12,22 @@ namespace agui
class GuiElement
{
public:
GuiElement(GuiElement* parent = NULL);
GuiElement(GuiElement* parent = nullptr);
virtual ~GuiElement();

void Draw();
bool HandleEvent(const SDL_Event& ev);
bool MouseOver(int x, int y) const;
bool MouseOver(float x, float y) const;

static void UpdateDisplayGeo(int x, int y, int offsetX, int offsetY);
static float PixelToGlX(int x);
static float PixelToGlY(int y);
static float GlToPixelX(float x);
static float GlToPixelY(float y);

virtual void AddChild(GuiElement* elem);

void SetPos(float x, float y);
void SetSize(float x, float y, bool fixed = false);
bool SizeFixed() const
Expand All @@ -50,7 +50,7 @@ class GuiElement
{
return weight;
};

void GeometryChange();

float GetMidY() const
Expand Down Expand Up @@ -80,20 +80,20 @@ class GuiElement

protected:
GuiElement* parent;
typedef std::list<GuiElement*> ChildList;

using ChildList = std::vector<GuiElement*>;
ChildList children;

virtual void DrawSelf() {};
virtual bool HandleEventSelf(const SDL_Event& ev)
{
return false;
};
virtual void GeometryChangeSelf() {};

static int screensize[2];
static int screenoffset[2];

bool fixedSize;
float pos[2];
float size[2];
Expand Down

0 comments on commit e816806

Please sign in to comment.