Created
December 20, 2015 18:23
-
-
Save ajweeks/a09ef233ae7a6141af6d to your computer and use it in GitHub Desktop.
Classes2Home1 Slider class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include "Slider.h" | |
#define GAME_ENGINE (GameEngine::GetSingleton()) | |
Slider::Slider(const DOUBLE2& posRef, int minValue, int maxValue) | |
: Slider(posRef, minValue, maxValue, String("value")) | |
{ | |
} | |
Slider::Slider(const DOUBLE2& posRef, int minValue, int maxValue, const String& textRef) : | |
m_Pos(posRef), m_MinValue(minValue), m_MaxValue(maxValue), m_Text(textRef) | |
{ | |
m_Width = 200; | |
m_Height = 30; | |
m_Color = COLOR(200, 200, 200); | |
m_BackColor = COLOR(100, 100, 100); | |
m_Value = minValue + (maxValue - minValue) / 2; | |
} | |
Slider::~Slider() | |
{ | |
} | |
void Slider::Tick(double deltaTime) | |
{ | |
DOUBLE2 mousePos = GAME_ENGINE->GetMousePosition(); | |
if (mousePos.x > m_Pos.x && mousePos.x < m_Pos.x + m_Width && | |
mousePos.y > m_Pos.y && mousePos.y < m_Pos.y + m_Height) | |
{ | |
if (GAME_ENGINE->IsMouseButtonPressed(VK_LBUTTON)) | |
{ | |
m_Value = m_MinValue; | |
} | |
else if (GAME_ENGINE->IsMouseButtonDown(VK_MBUTTON)) | |
{ | |
m_Value = m_MinValue + (mousePos.x - m_Pos.x) * (m_MaxValue - m_MinValue) / m_Width; | |
} | |
else if (GAME_ENGINE->IsMouseButtonPressed(VK_RBUTTON)) | |
{ | |
m_Value = m_MaxValue; | |
} | |
} | |
} | |
void Slider::Paint() | |
{ | |
GAME_ENGINE->SetDefaultFont(); | |
GAME_ENGINE->SetColor(m_BackColor); | |
GAME_ENGINE->FillRect(m_Pos, m_Pos + DOUBLE2(m_Width, m_Height)); | |
GAME_ENGINE->SetColor(m_Color); | |
GAME_ENGINE->FillRect(m_Pos.x, m_Pos.y, m_Pos.x + (m_Value-m_MinValue) * (m_Width / (m_MaxValue - m_MinValue)), m_Pos.y + m_Height); | |
GAME_ENGINE->SetColor(COLOR(0, 0, 0)); | |
GAME_ENGINE->DrawRect(m_Pos, m_Pos + DOUBLE2(m_Width, m_Height)); | |
GAME_ENGINE->DrawString(String(m_MinValue), m_Pos.x, m_Pos.y + 30); | |
GAME_ENGINE->DrawString(String(m_MaxValue), m_Pos.x + m_Width, m_Pos.y + 30); | |
GAME_ENGINE->DrawString(String(m_Text) + String(": ") + String(m_Value), m_Pos.x, m_Pos.y + 45); | |
DOUBLE2 mousePos = GAME_ENGINE->GetMousePosition(); | |
if (mousePos.x > m_Pos.x && mousePos.x < m_Pos.x + m_Width && | |
mousePos.y > m_Pos.y && mousePos.y < m_Pos.y + m_Height) | |
{ | |
GAME_ENGINE->SetColor(COLOR(200, 165, 120)); | |
GAME_ENGINE->FillRect(RECT2(mousePos.x + 15, mousePos.y, mousePos.x + 130, mousePos.y + 50)); | |
GAME_ENGINE->SetColor(COLOR(0, 0, 0)); | |
GAME_ENGINE->DrawRect(RECT2(mousePos.x + 15, mousePos.y, mousePos.x + 130, mousePos.y + 50)); | |
int x = mousePos.x + 15 + 5; | |
int y = mousePos.y + 3; | |
GAME_ENGINE->DrawString(String("Min value: LMB"), x, y); y += 15; | |
GAME_ENGINE->DrawString(String("Max value: RMB"), x, y); y += 15; | |
GAME_ENGINE->DrawString(String("Mouse value: MMB"), x, y); | |
} | |
} | |
void Slider::SetText(const String& textRef) | |
{ | |
m_Text = textRef; | |
} | |
void Slider::SetWidth(double width) | |
{ | |
m_Width = width; | |
} | |
void Slider::SetHeight(double height) | |
{ | |
m_Height = height; | |
} | |
void Slider::SetValueColor(const COLOR& colorRef) | |
{ | |
m_Color = colorRef; | |
} | |
void Slider::SetBackColor(const COLOR& colorRef) | |
{ | |
m_BackColor = colorRef; | |
} | |
double Slider::GetValue() | |
{ | |
return m_Value; | |
} | |
RECT2 Slider::GetBounds() | |
{ | |
return RECT2(m_Pos.x, m_Pos.y, m_Pos.x + m_Width, m_Pos.y + m_Height); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment