1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[C++] DirectX Button Class

Discussion in 'Eigene Programmierung' started by Cre3per, Mar 5, 2014.

  1. Cre3per

    Cre3per Member

    Joined:
    Oct 22, 2012
    Messages:
    764
    Likes Received:
    2
    Gerade ausgegraben. Es sollte einfach sein sie als nicht DirectX Klasse umzuschreiben
    //Edit: Ich merke gerade, dass man mit GetCursorPos nicht mehr als 60fps bekommt. Also vielleicht nicht die beste Lösung
    Code:
    #include <windows.h>
    #include <d3dx9.h>
    #include <time.h>
    
    class Button
    {
    private:
        D3DXVECTOR4 Dimensions; //Button X, Y, Width, Height
        RECT Window;            //Window X, Y, Width, Height
        int Border_Top;            //Size of the window's titlebar
        int Border_Left;        //Size of the left side of the window
    
        DWORD Color_Default;    //Nomal button Color
        DWORD Color_Hover;        //Color when mouse hovers
        DWORD Color_Pressed;    //Color when the button is pressed
    
        time_t lastClick;        //When did the button get clicked the last time? Value in (double)seconds since 1970
        double clickSpeed;        //Minimum delay between each click
    
        char* Text;                //The text
    public:
        Button() //This Function(The Constructor) gets called when the class in created
        {
            //Set the Dimnesions to Zero, because NULL sucks
            Dimensions.x = 0;    //X
            Dimensions.y = 0;    //Y
            Dimensions.w = 0;    //Width
            Dimensions.z = 0;    //Height
    
            //Set the Colors in hexadecimal ARGB
            Color_Default = 0xFFFFFFFF;
            Color_Hover = 0xFFEEEEEE;
            Color_Pressed = 0xFFDDDDDD;
    
            //Set the Text
            Text = "Button";
            clickSpeed = 0.2l; //0x2l = 200ms
            lastClick = time(0); //NULL still sucks; time(0) gets the current time in seconds
    
            //Gets the size of the Top- and Sidebarusing GetSystemMetrics
            Border_Left = GetSystemMetrics(SM_CXBORDER) + GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXEDGE);
            Border_Top = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYEDGE);
        }
        ~Button()
        {
    
        }
    
        D3DXVECTOR4 GetDimensions()
        {
            return Dimensions;
        }
        void SetDimensions(int x, int y, int width, int height)
        {
            Dimensions.x = float(x);
            Dimensions.y = float(y);
            Dimensions.w = float(width);
            Dimensions.z = float(height);
        }
        BOOL MouseHovers(POINT _CursorPos)
        {
            ZeroMemory(&Window, sizeof(Window));
            GetWindowRect(GetActiveWindow(), &Window);
    
            //If Cursor is too far to the left
            if (_CursorPos.x < (Window.left + Border_Left + Dimensions.x))
                return false;
            //If the Cursor is to far to the right
            if (_CursorPos.x >(Window.left + Border_Left + Dimensions.x + Dimensions.w))
                return false;
            //If the Cursor is above
            if (_CursorPos.y < (Window.top + Border_Top + Dimensions.y))
                return false;
            //If the Cursor is below
            if (_CursorPos.y >(Window.top + Border_Top + Dimensions.y + Dimensions.z))
                return false;
    
            return true;
        }
        int IsPressed(POINT _CursorPos)
        {
            if (!MouseHovers(_CursorPos))
                return 0;
            else
            {
                if (GetAsyncKeyState(MK_LBUTTON))
                    return 1;
                else if (GetAsyncKeyState(MK_RBUTTON))
                    return 2;
                else if (GetAsyncKeyState(0x4))
                    return 3;
            }
    
            return 0;
        }
        DWORD GetCurrentColor(POINT _CursorPos)
        {
            if (MouseHovers(_CursorPos))
            if (IsPressed(_CursorPos))
                return Color_Pressed;
            else
                return Color_Hover;
            else
                return Color_Default;
        }
        void SetColor(DWORD Default, DWORD Hover, DWORD Pressed)
        {
            if (Default != -1)
                Color_Default = Default;
            if (Hover != -1)
                Color_Hover = Hover;
            if (Pressed != -1)
                Color_Pressed = Pressed;
        }
        char* GetText()
        {
            return Text;
        }
        void SetText(char* _Text)
        {
            Text = _Text;
        }
        int getBorderLeft()
        {
            return Border_Left;
        }
        int getBorderTop()
        {
            return Border_Top;
        }
        RECT getWindow()
        {
            return Window;
        }
        bool isClickable()
        {
            if (difftime(time(0), lastClick) >= this->clickSpeed)
                return true;
            return false;
        }
        void setClickSpeed(double _clickSpeed)
        {
            this->clickSpeed = _clickSpeed;
        }
        double getClickSpeed()
        {
            return this->clickSpeed;
        }
        //click has to be called every time the button gets clicked. It restets the time since the last click('lastClick')
        void click()
        {
            this->lastClick = time(0);
        }
    };
    Benutzung:
    Code:
    #include "Button.h" //Klasse ist ein einem eigenen Header
    
    #define BtnCount 1337
    Button Btns[1337];
    POINT cursorPos;
    
    void InitButtons()
    {
        for(int i = 0; i < BtnCount; i++)
        {
            Btns[i].setDimension(5, 5 + i * 30, 50, 20);
            Btns[i].setText("Hallo Welt!");
            Btns[i].setColor(0xFF00FF00, 0xFF00DD00, 0xFF000000);
            //...
        }
    }
    
    void HandleButtonClicks()
    {
        GetCursorPos(&cursorPos);
    
        for(int i = 0; i < BtnCount; i++) 
          {
            if(Btns[i].IsPressed(cursorPos) == 1 && Btns[i].isClickable())
            {
                //Mach was
                Btns[i].click();
            }
        }
    }
    
    void Render()
    {
    //Renderzeug
    for(int i = 0; i < BtnCount; i++) 
    {
    //Lasst eure Fantasie spielen
    DrawRectangle(Btns[i].GetDimensions().x, Btns[i].GetDimensions().y, Btns[i].GetDimensions().w, Btns[i].GetDimensions().z);
    }
    }
    
    void MainLoop()
    {
        while(1)
        {
            HandleButtonClicks();
    Render();
        }
    }
    
    int main(int argc, char* argv[])
    {
        initButtons();
        MainLoop();
    }
    
     
  2. krusty

    krusty New Member

    Joined:
    Sep 8, 2012
    Messages:
    182
    Likes Received:
    0
    ganz nett, ich hätte die Renderfunktion in den Button geschmissen, sodass man dann nur noch Button.draw() aufrufen muss.
    Ist aber Geschmackssache.