Creating a custom LED indicator

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
Fenhasan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Aug 02, 2020 10:57 am

Creating a custom LED indicator

Post by Fenhasan » Sat Apr 17, 2021 2:03 pm

Hi guys:

I want to make a LED indicator, but I've had no luck in making it modular. Let me explain: I managed to draw the LED, but when i tried to make it a module, to make it portable, it only shows a white square, and nothing else. My test code is the following:

led_widget.h

Code: Select all

#include <wx/wx.h>
#include <wx/graphics.h>

class Led : public wxWindow
{
    public:
        Led(wxFrame *parent, wxWindowID id);
        wxFrame *m_parent;
        int x0=100,y0=100,x1=0,y1=0,x2=0,y2=0,D1=40,D2=60;
        bool FlagStatus = false;

        void SetStatus(bool status);
        void OnPaint(wxPaintEvent& event);
};
led_wigdet.cpp

Code: Select all

#include "led_widget.h"

Led::Led(wxFrame *parent, wxWindowID id)
    : wxWindow(parent, id, wxPoint(200,10), wxSize(60,60), wxNO_BORDER)
{
    m_parent = parent;
    Connect(wxEVT_PAINT, wxPaintEventHandler(Led::OnPaint));
}

void Led::SetStatus(bool status)
{
    FlagStatus = status;
    Refresh();
}

void Led::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);

    //dc.SetBrush(wxColor(0,255,0));
    //dc.DrawCircle(220, 30, 15);

    wxGraphicsContext *gc = wxGraphicsContext::Create(dc);

    // Generar paradas de la gradiente
    wxGraphicsGradientStops stops;

    if(FlagStatus)
    {
        stops.Add(wxColor(255,255,255),0.0f);
        stops.Add(wxColor(255,0,0),1.0f);
    }
    else
    {
        stops.Add(wxColor(128,0,0),0.0f);
        stops.Add(wxColor(0,0,0),1.0f);
    }

    // Dibujar led

    D2 = D1*1.5;
    x1 = x0-D1/2;
    y1 = y0-D1/2;
    x2 = x0-D2/2;
    y2 = y0-D2/2;

    gc->SetPen(wxPen(wxColor(0,0,0), 1, wxTRANSPARENT));
    gc->SetBrush(gc->CreateLinearGradientBrush(x2,y2,x2+D2,y2+D2,wxColor(35,35,35),wxColor(180,180,180)));
    gc->DrawEllipse(x2,y2,D2,D2);
    gc->SetBrush(gc->CreateRadialGradientBrush(x0,y0,x0,y0,D1/2,stops));
    gc->DrawEllipse(x1,y1,D1,D1);
}

Fenhasan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Aug 02, 2020 10:57 am

Re: Creating a custom LED indicator

Post by Fenhasan » Sat Apr 17, 2021 2:06 pm

TestDCMain.h

Code: Select all

#ifndef TESTDCMAIN_H
#define TESTDCMAIN_H

//(*Headers(TestDCFrame)
#include <wx/button.h>
#include <wx/frame.h>
#include <wx/menu.h>
#include <wx/radiobox.h>
#include <wx/slider.h>
#include <wx/stattext.h>
#include <wx/statusbr.h>
//*)

#include <wx/wx.h>
//#include <wx/graphics.h>

#include "led_widget.h"

class TestDCFrame: public wxFrame
{
    public:

        TestDCFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~TestDCFrame();

        Led *test_led;

        int GradRadius = 10;
        int LedRadius = 20;
        int LedSize = 20;
        int LedXcoord = 50;
        int LedYcoord = 50;
        bool FlagTxOn = false;
        bool FlagRxOn = false;
        int EllipseXcoord = 200;
        int EllipseYcoord = 100;
        int EllipseRadius = 15;
        int x0=100,y0=150,x1=0,y1=0,x2=0,y2=0,D1=40,D2=60;

        void PaintCircles(wxDC& dc)
        {
            /** LED TX (rojo) **/

            if(FlagTxOn)
            {
                dc.SetBrush(wxColor(255,0,0));
                dc.DrawCircle(LedXcoord, LedYcoord, LedSize);
            }
            else
            {
                dc.SetBrush(wxColor(128,0,0));
                dc.DrawCircle(LedXcoord, LedYcoord, LedSize);
            }


            /** LED RX (verde) **/

            if(FlagRxOn)
            {
                dc.SetBrush(wxColor(0,255,0));
                dc.DrawCircle(LedXcoord+50, LedYcoord, LedSize);
            }
            else
            {
                dc.SetBrush(wxColor(0,128,0));
                dc.DrawCircle(LedXcoord+50, LedYcoord, LedSize);
            }
        }


    private:

        //(*Handlers(TestDCFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnResize(wxSizeEvent& event);
        void OnClose(wxCloseEvent& event);
        void OnPaint(wxPaintEvent& event);
        void OnSlider1CmdSliderUpdated(wxScrollEvent& event);
        void OnSlider2CmdSliderUpdated(wxScrollEvent& event);
        void OnRadioBox1Select(wxCommandEvent& event);
        //*)

        //(*Identifiers(TestDCFrame)
        static const long ID_BUTTON1;
        static const long ID_STATICTEXT1;
        static const long ID_SLIDER1;
        static const long ID_RADIOBOX1;
        static const long idMenuQuit;
        static const long idMenuAbout;
        static const long ID_STATUSBAR1;
        //*)

        //(*Declarations(TestDCFrame)
        wxButton* Button1;
        wxRadioBox* RadioBox1;
        wxSlider* Slider1;
        wxStaticText* StaticText1;
        wxStatusBar* StatusBar1;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // TESTDCMAIN_H
TestDCMain.cpp

Code: Select all

#include "TestDCMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(TestDCFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(TestDCFrame)
const long TestDCFrame::ID_BUTTON1 = wxNewId();
const long TestDCFrame::ID_STATICTEXT1 = wxNewId();
const long TestDCFrame::ID_SLIDER1 = wxNewId();
const long TestDCFrame::ID_RADIOBOX1 = wxNewId();
const long TestDCFrame::idMenuQuit = wxNewId();
const long TestDCFrame::idMenuAbout = wxNewId();
const long TestDCFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(TestDCFrame,wxFrame)
    //(*EventTable(TestDCFrame)
    //*)
END_EVENT_TABLE()

TestDCFrame::TestDCFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(TestDCFrame)
    wxMenu* Menu1;
    wxMenu* Menu2;
    wxMenuBar* MenuBar1;
    wxMenuItem* MenuItem1;
    wxMenuItem* MenuItem2;

    Create(parent, id, _("TestDC"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(500,500));
    Button1 = new wxButton(this, ID_BUTTON1, _("Salir"), wxPoint(392,400), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, wxEmptyString, wxPoint(136,376), wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    Slider1 = new wxSlider(this, ID_SLIDER1, 20, 10, 100, wxPoint(80,232), wxDefaultSize, 0, wxDefaultValidator, _T("ID_SLIDER1"));
    wxString __wxRadioBoxChoices_1[2] =
    {
    	_("OFF"),
    	_("ON")
    };
    RadioBox1 = new wxRadioBox(this, ID_RADIOBOX1, _("Status"), wxPoint(248,136), wxSize(56,80), 2, __wxRadioBoxChoices_1, 1, 0, wxDefaultValidator, _T("ID_RADIOBOX1"));
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TestDCFrame::OnQuit);
    Connect(ID_SLIDER1,wxEVT_COMMAND_SLIDER_UPDATED,(wxObjectEventFunction)&TestDCFrame::OnSlider1CmdSliderUpdated);
    Connect(ID_RADIOBOX1,wxEVT_COMMAND_RADIOBOX_SELECTED,(wxObjectEventFunction)&TestDCFrame::OnRadioBox1Select);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TestDCFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TestDCFrame::OnAbout);
    Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&TestDCFrame::OnClose);
    Connect(wxEVT_PAINT,(wxObjectEventFunction)&TestDCFrame::OnPaint);
    Connect(wxEVT_SIZE,(wxObjectEventFunction)&TestDCFrame::OnResize);
    //*)

    Connect(wxEVT_PAINT, wxPaintEventHandler(TestDCFrame::OnPaint));

    test_led = new Led(this,wxID_ANY);
}

TestDCFrame::~TestDCFrame()
{
    //(*Destroy(TestDCFrame)
    //*)
}

void TestDCFrame::OnQuit(wxCommandEvent& event)
{
    Destroy();
}

void TestDCFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void TestDCFrame::OnResize(wxSizeEvent& event)
{
    SetMaxSize(GetSize());
    SetMinSize(GetSize());
}

void TestDCFrame::OnClose(wxCloseEvent& event)
{
    Destroy();
}

void TestDCFrame::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    wxGraphicsContext *gc = wxGraphicsContext::Create(dc);

    // Dibujar LED

    D2 = D1*1.5;
    x1 = x0-D1/2;
    y1 = y0-D1/2;
    x2 = x0-D2/2;
    y2 = y0-D2/2;
    wxGraphicsGradientStops stops;

    if(RadioBox1->GetSelection())
    {
        stops.Add(wxColor(255,255,255),0.0f);
        stops.Add(wxColor(255,0,0),1.0f);
    }
    else
    {
        stops.Add(wxColor(128,0,0),0.0f);
        stops.Add(wxColor(0,0,0),1.0f);
    }

    gc->SetPen(wxPen(wxColor(0,0,0), 1, wxTRANSPARENT));
    gc->SetBrush(gc->CreateLinearGradientBrush(x2,y2,x2+D2,y2+D2,wxColor(35,35,35),wxColor(180,180,180)));
    gc->DrawEllipse(x2,y2,D2,D2);
    gc->SetBrush(gc->CreateRadialGradientBrush(x0,y0,x0,y0,D1/2,stops));
    gc->DrawEllipse(x1,y1,D1,D1);

    // Another type old LED

    if(RadioBox1->GetSelection())
    {
        FlagTxOn = true;
        FlagRxOn = true;
    }
    else
    {
        FlagTxOn = false;
        FlagRxOn = false;
    }

    PaintCircles(dc);
}

void TestDCFrame::OnSlider1CmdSliderUpdated(wxScrollEvent& event)
{
    D1 = Slider1->GetValue();
    Refresh();
}

void TestDCFrame::OnSlider2CmdSliderUpdated(wxScrollEvent& event)
{

}

void TestDCFrame::OnRadioBox1Select(wxCommandEvent& event)
{
    test_led->SetStatus(RadioBox1->GetSelection());
    test_led->Refresh();
    Refresh();
}

Fenhasan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Aug 02, 2020 10:57 am

Re: Creating a custom LED indicator

Post by Fenhasan » Sat Apr 17, 2021 2:12 pm

Sorry for triple posting, but when i tried to post all 4 files in one post, the system flagged it as spam...

Anyway, here is a picture of the test program running, showing the blank square at the top, where another LED should appear:
Test.jpg
Test program running
Test.jpg (13.5 KiB) Viewed 941 times
My idea is to make a LED with some gradient fill, which is directly drawn and doesn't require an additional bitmap, as i tried before. It sorta works, but i have to paste the code in my program, so it's rather cumbersome to use; my idea is to make it modular, so i can include it in other programs. The green and red circles at the top are a simpler LED, it also works, but it's not as pretty... :)

I'd like to ask for your help, I have no idea what i'm doing wrong. Obviously is the first time i'm trying to write a graphic component, so i expected some setbacks... Your help is very appreciated.

Best regards,
Fenhasan

User avatar
doublemax
Moderator
Moderator
Posts: 15915
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating a custom LED indicator

Post by doublemax » Sat Apr 17, 2021 4:58 pm

Code: Select all

int x0=100,y0=100,x1=0,y1=0,x2=0,y2=0,D1=40,D2=60;
I suspect these hard-coded coordinates to be responsible. Inside a paint event handler for a specific window, the coordinates are relative to the window position, i.e. they always start at (0,0) in the upper left corner. I guess you're drawing outside the visible window.

A few other remarks:
- the parent of the custom control should be wxWindow*, not wxFrame*
- don't use hard-coded values for position and size, use GetClientSize() at runtime and adjust the drawing code accordingly.
- make only the class members public where it's necessary

And in the test frame, remove (or disable) the custom paint event handler.
Use the source, Luke!

Fenhasan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Aug 02, 2020 10:57 am

Re: Creating a custom LED indicator

Post by Fenhasan » Sat Apr 17, 2021 5:38 pm

Thanks for your suggestions, doublemax, I'll give them a try and report the results.

Best regards,
Fenhasan

Fenhasan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Aug 02, 2020 10:57 am

Re: Creating a custom LED indicator

Post by Fenhasan » Sun Apr 18, 2021 2:18 am

So I made a few changes, and i got this result:
Test01.jpg
Test01.jpg (17.96 KiB) Viewed 896 times
I could draw the Led, but i get those white areas... is there any way to make the component background transparent?

The code now is:

led_widget.h

Code: Select all

#include <wx/wx.h>
#include <wx/graphics.h>

class Led : public wxWindow
{
    public:
        Led(wxWindow *parent, wxWindowID id, wxPoint Position, int Size, char color);
        wxWindow *m_parent;

        char col;
        int x0,y0,x1,y1,D1,D2;
        bool FlagStatus = false;

        void SetStatus(bool status);
        void OnPaint(wxPaintEvent& event);
        void SetColor(char color);
};
led_widget.cpp

Code: Select all

#include "led_widget.h"

Led::Led(wxWindow *parent, wxWindowID id, wxPoint Position, int Size, char color)
    : wxWindow(parent, id, Position, wxSize(Size,Size))
{
    m_parent = parent;
    Connect(wxEVT_PAINT, wxPaintEventHandler(Led::OnPaint));
    col = color;
}

void Led::SetStatus(bool status)
{
    FlagStatus = status;
    Refresh();
}

void Led::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);

    wxGraphicsContext *gc = wxGraphicsContext::Create(dc);

    D1 = GetClientSize().GetX();

    // Generar paradas de la gradiente
    wxGraphicsGradientStops stops;

    if(FlagStatus)
    {
        switch(col)
        {
            case 'r':
            case 'R':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(255,0,0),1.0f);
                        break;

            case 'g':
            case 'G':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(0,255,0),1.0f);
                        break;

            case 'b':
            case 'B':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(0,0,255),1.0f);
                        break;

            case 'y':
            case 'Y':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(255,255,0),1.0f);
                        break;

            default :   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(255,0,0),1.0f);
                        break;
        }
    }
    else
    {
        switch(col)
        {
            case 'r':
            case 'R':   stops.Add(wxColor(128,0,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            case 'g':
            case 'G':   stops.Add(wxColor(0,128,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            case 'b':
            case 'B':   stops.Add(wxColor(0,0,128),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            case 'y':
            case 'Y':   stops.Add(wxColor(128,128,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            default :   stops.Add(wxColor(128,0,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;
        }
    }

    // Dibujar led
    gc->SetPen(wxPen(wxColor(0,0,0), 1, wxTRANSPARENT));
    gc->SetBrush(gc->CreateLinearGradientBrush(0,0,D1,D1,wxColor(35,35,35),wxColor(180,180,180)));
    gc->DrawEllipse(0,0,D1,D1);

    D2 = (2*D1)/3;
    x1 = D1/6;
    y1 = x1;

    gc->SetBrush(gc->CreateRadialGradientBrush(D1/2,D1/2,D1/2,D1/2,D2/2,stops));
    gc->DrawEllipse(x1,y1,D2,D2);

    //wxMessageBox(wxString::Format("x1: %d y1: %d D1: %d D2: %d",x1,y1,D1,D2),"Info");
}

void Led::SetColor(char color)
{
    col = color;
    Refresh();
}
Now I don't think it's possible to change the size or position of the Led on the fly, since those are now parameters, but I guess it's not that big of a deal. The color of the Led can be changed on the fly now. I would like to remove those white areas though, they look really bad. Is there a way to do that?

Thanks and best regards,
Fenhasan

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 382
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Creating a custom LED indicator

Post by New Pagodi » Sun Apr 18, 2021 6:13 am

Fenhasan wrote:
Sun Apr 18, 2021 2:18 am

I could draw the Led, but i get those white areas... is there any way to make the component background transparent?
I think calling Clear for the paint dc should fill the background in appropriately. Like so:

Code: Select all

void Led::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    dc.Clear();
....

User avatar
doublemax
Moderator
Moderator
Posts: 15915
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating a custom LED indicator

Post by doublemax » Sun Apr 18, 2021 8:15 am

Add the wxTRANSPARENT_WINDOW style flag.
Call SetBackgroundStyle(wxBG_STYLE_PAINT); in the ctor, so that its background does not get cleared.

Memory leak: You need to call "delete gc;" at the end of the paint event handler.
Use the source, Luke!

Fenhasan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Aug 02, 2020 10:57 am

Re: Creating a custom LED indicator

Post by Fenhasan » Sun Apr 18, 2021 8:43 pm

doublemax:

How do you set the wxTRANSPARENT_WINDOW flag? I tried adding this in the ctor:

Code: Select all

Led::Led(wxWindow *parent, wxWindowID id, wxPoint Position, int Size, char color)
    : wxWindow(parent, id, Position, wxSize(Size,Size),'R')
{
    m_parent = parent;
    Connect(wxEVT_PAINT, wxPaintEventHandler(Led::OnPaint));
    col = color;
    SetWindowStyleFlag(wxTRANSPARENT_WINDOW);
    SetBackgroundStyle(wxBG_STYLE_PAINT);
}
but it didn't make a difference, i get the same result. Am i doing it wrong?

Best regards,
Fenhasan

User avatar
doublemax
Moderator
Moderator
Posts: 15915
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating a custom LED indicator

Post by doublemax » Sun Apr 18, 2021 8:57 pm

Pass as "style" to the wxWindow ctor.

Code: Select all

Led::Led(wxWindow *parent, wxWindowID id, wxPoint Position, int Size, char color)
    : wxWindow(parent, id, Position, wxSize(Size,Size), wxTRANSPARENT_WINDOW )
And must not have the "dc.Clear();" in the paint event handler.
Use the source, Luke!

Fenhasan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Aug 02, 2020 10:57 am

Re: Creating a custom LED indicator

Post by Fenhasan » Mon Apr 19, 2021 4:07 am

As usual, thanks doublemax for providing the solution!

The Led looks like this now :D :
Test02.jpg
Test02.jpg (14.49 KiB) Viewed 822 times
Color can be changed on the fly.

Thank you very much doublemax, I'm really grateful!

Best regards,
Fenhasan

PD: This is the working code, in case anyone would like to use it.

led_widget.h

Code: Select all

#include <wx/wx.h>
#include <wx/graphics.h>

class Led : public wxWindow
{
    public:
        Led(wxWindow *parent, wxWindowID id, wxPoint Position, int Size, char color);
        wxWindow *m_parent;

        char col;
        int x0,y0,x1,y1,D1,D2;
        bool FlagStatus = false;

        void SetStatus(bool status);
        void OnPaint(wxPaintEvent& event);
        void SetColor(char color);
};
led_widget.cpp

Code: Select all

#include "led_widget.h"

Led::Led(wxWindow *parent, wxWindowID id, wxPoint Position, int Size, char color)
    : wxWindow(parent, id, Position, wxSize(Size,Size),wxTRANSPARENT_WINDOW,'R')
{
    m_parent = parent;
    Connect(wxEVT_PAINT, wxPaintEventHandler(Led::OnPaint));
    col = color;
    SetWindowStyleFlag(wxTRANSPARENT_WINDOW);
    SetBackgroundStyle(wxBG_STYLE_PAINT);
}

void Led::SetStatus(bool status)
{
    FlagStatus = status;    
    Refresh();
}

void Led::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);

    wxGraphicsContext *gc = wxGraphicsContext::Create(dc);

    D1 = GetClientSize().GetX();

    // Generar paradas de la gradiente
    wxGraphicsGradientStops stops;

    if(FlagStatus)
    {
        switch(col)
        {
            case 'r':
            case 'R':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(255,0,0),1.0f);
                        break;

            case 'g':
            case 'G':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(0,255,0),1.0f);
                        break;

            case 'b':
            case 'B':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(0,0,255),1.0f);
                        break;

            case 'y':
            case 'Y':   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(255,255,0),1.0f);
                        break;

            default :   stops.Add(wxColor(255,255,255),0.0f);
                        stops.Add(wxColor(255,0,0),1.0f);
                        break;
        }
    }
    else
    {
        switch(col)
        {
            case 'r':
            case 'R':   stops.Add(wxColor(128,0,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            case 'g':
            case 'G':   stops.Add(wxColor(0,128,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            case 'b':
            case 'B':   stops.Add(wxColor(0,0,128),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            case 'y':
            case 'Y':   stops.Add(wxColor(128,128,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;

            default :   stops.Add(wxColor(128,0,0),0.0f);
                        stops.Add(wxColor(0,0,0),1.0f);
                        break;
        }
    }

    // Dibujar led
    gc->SetPen(wxPen(wxColor(0,0,0), 1, wxTRANSPARENT));
    gc->SetBrush(gc->CreateLinearGradientBrush(0,0,D1,D1,wxColor(35,35,35),wxColor(180,180,180)));
    gc->DrawEllipse(0,0,D1,D1);

    D2 = (2*D1)/3;
    x1 = D1/6;
    y1 = x1;

    gc->SetBrush(gc->CreateRadialGradientBrush(D1/2,D1/2,D1/2,D1/2,D2/2,stops));
    gc->DrawEllipse(x1,y1,D2,D2);

    delete gc;
}

void Led::SetColor(char color)
{
    col = color;
    Refresh();
}


Post Reply