protected member value not changing

Started by
3 comments, last by JPulham 15 years, 10 months ago
I have IGUIButton deriving from IGUIWidget. IGUIWidget has a protected boolean called '_hover'. I know that Hover is changing, but the member function 'draw' in IGUIButton just isn't seeing the value change. help?!?

virtual void draw()
{
    if(_hover)
    {
        glBegin(GL_POLYGON);
        glColor4f(_fillColor[0],_fillColor[1],_fillColor[2],0.2f);
        glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.UpperLeft.y);
        glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.UpperLeft.y);

        // bevel
        glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.LowerRight.y - _vbevel);
        glVertex2f(_absoluteRect.LowerRight.x - _hbevel,_absoluteRect.LowerRight.y);

        glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.LowerRight.y);
        glEnd();
        // printf - the greatest debugging tool EVER!
        printf("draw hover\n");
    }

    glBegin(GL_LINE_LOOP);
    glColor4f(_edgeColor[0],_edgeColor[1],_edgeColor[2],0.5f);
    glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.UpperLeft.y);
    glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.UpperLeft.y);

    // bevel
    glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.LowerRight.y - _vbevel);
    glVertex2f(_absoluteRect.LowerRight.x - _hbevel,_absoluteRect.LowerRight.y);

    glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.LowerRight.y);
    glEnd();

    // draw children
    IGUIWidget::draw();
}

pushpork
Advertisement
You didn't post enough code. Post the entire class definition as well as its base classes.
of course.. sorry, did it in a rush ;)

this is just the beginning of an implementation... and failing already

IGUIWidget.h
#ifndef __I_GUI_WIDGET_H__#define __I_GUI_WIDGET_H__#include "RefCount.h"#include <list>#include "rect.h"namespace SilverNova{class IGUIWidget : public RefCount{        friend class CGUISurface;        typedef std::list<IGUIWidget*> WidgetList;    public:        IGUIWidget(IOpenGLContext *glC, IGUIWidget *parent, int id = -1)         : _glContext(glC), _parent(parent), _id(id), _visible(true), _hover(false), _focus(false)        {            if(parent)parent->addChild(this);            _fillColor[0] = _fillColor[1] = _fillColor[2] = 0.5f;            _edgeColor[0] = _edgeColor[1] = _edgeColor[2] = 1.0f;        }        virtual ~IGUIWidget()        {            WidgetList::iterator i = _children.begin();            for(; i != _children.end(); i++)            {                (*i)->_parent = 0;                (*i)->drop();            }        }        virtual bool OnEvent(const SEvent& evt)        {            switch(evt.event_type)            {                case EET_GUI:                    switch(evt.gui_event.type)                    {                        case EGE_HOVER_OVER:                            _hover = true;                            return false;                        case EGE_HOVER_OUT:                            _hover = false;                            return false;                        default:                            break;                    }                    break;                default:                    break;            }            return _parent ? _parent->OnEvent(evt) : false;        }        virtual void addChild(IGUIWidget *child)        {            if(child)            {                child->grab();                child->remove();                child->updateRect();                child->_parent = this;                _children.push_back(child);            }        }        virtual void removeChild(IGUIWidget* child)        {            WidgetList::iterator i = _children.begin();            for(; i != _children.end(); i++)            {                if((*i) == child)                {                    (*i)->_parent = 0;                    (*i)->drop();                    _children.erase(i);                    return;                }            }        }        virtual void remove()        {            if(_parent)_parent->removeChild(this);        }        virtual void draw()        {            if(!_visible)return;            for(WidgetList::iterator i = _children.begin();i != _children.end();i++)                (*i)->draw();        }        virtual bool isVisible() const        {            return _visible;        }        virtual void setVisible(bool vis)        {            _visible = vis;        }        virtual void setFillColor(float* col)        {            _fillColor[0] = col[0];            _fillColor[1] = col[1];            _fillColor[2] = col[2];        }        virtual void setEdgeColor(float* col)        {            _edgeColor[0] = col[0];            _edgeColor[1] = col[1];            _edgeColor[2] = col[2];        }        virtual void setRectangle(rect r)        {            _relativeRect = r;            updateRect();        }        virtual void updateRect()        {            if(_parent)                _absoluteRect =  _relativeRect + _parent->_absoluteRect.UpperLeft;            else                _absoluteRect = _relativeRect;        }        virtual IGUIWidget *getWidgetFromPoint(const point& pt)        {            IGUIWidget *target = 0;            if(_visible)            {                WidgetList::reverse_iterator i = _children.rbegin();                while(i != _children.rend())                {                    target = (*i)->getWidgetFromPoint(pt);                    if(target)                        return target;                    i++;                }                if(isPointInside(pt))                    target = this;            }            return target;        }        virtual bool isPointInside(const point& pt) const        {            return _absoluteRect.isPointInside(pt);        }    protected:        IOpenGLContext *_glContext;        IGUIWidget *_parent;        WidgetList _children;        int _id;        bool _visible;        bool _hover;        bool _focus;        rect _relativeRect;        rect _absoluteRect;        float _fillColor[3];        float _edgeColor[3];};}#endif


CGUIBButton.h
#ifndef __C_GUI_BUTTON_H__#define __C_GUI_BUTTON_H__#include "IGUIWidget.h"namespace SilverNova{class CGUIButton : public IGUIWidget{    public:        CGUIButton(IOpenGLContext *glC, IGUIWidget *parent, int id = -1)         : IGUIWidget(glC, parent,id)        {            setBevel(0.02f);        }        virtual void draw()        {            if(_hover)            {                glBegin(GL_POLYGON);                glColor4f(_fillColor[0],_fillColor[1],_fillColor[2],0.2f);                glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.UpperLeft.y);                glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.UpperLeft.y);                // bevel                glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.LowerRight.y - _vbevel);                glVertex2f(_absoluteRect.LowerRight.x - _hbevel,_absoluteRect.LowerRight.y);                glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.LowerRight.y);                glEnd();                //printf("draw hover\n");            }            glBegin(GL_LINE_LOOP);            glColor4f(_edgeColor[0],_edgeColor[1],_edgeColor[2],0.5f);            glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.UpperLeft.y);            glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.UpperLeft.y);            // bevel            glVertex2f(_absoluteRect.LowerRight.x,_absoluteRect.LowerRight.y - _vbevel);            glVertex2f(_absoluteRect.LowerRight.x - _hbevel,_absoluteRect.LowerRight.y);            glVertex2f(_absoluteRect.UpperLeft.x,_absoluteRect.LowerRight.y);            glEnd();            IGUIWidget::draw();        }        virtual void setBevel(float bev)        {            _vbevel = bev * _glContext->getAspect();            _hbevel = bev;        }    protected:        float _vbevel;        float _hbevel;};}#endif
pushpork
Maybe the scene is not refreshed...

Try calling draw() at the end of OnEvent(...) to reflect your state change.
Put a breakpoint in draw() to see the value of _hover and see if you actually reach that codepoint. Aren't you really debugging anything?

PS: You should separate the declarations and the definitions in .h and .cpp files to avoid huge compile time as your application is growing!
fixed!
was using relative sizes ([0..1]) but changed to pixel values and problem disappears ... yey! must of been a floating point rounding thing.
pushpork

This topic is closed to new replies.

Advertisement