-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from BinaryDrag0n/master
#168 GUI: Checkbox Pull Request
- Loading branch information
Showing
11 changed files
with
231 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#include "checkbox_widget.h" | ||
|
||
#include "../component/rectangle_component.h" | ||
#include "../component/text_component.h" | ||
#include "widget_helper.h" | ||
|
||
namespace gui { | ||
|
||
CheckBoxWidget::CheckBoxWidget(RectangleComponent* rectComponent, TextComponent* label) | ||
: mp_rectangle(rectComponent) | ||
, mp_label(label) | ||
, m_checked(false) | ||
{ | ||
componentList.push_back(mp_rectangle); | ||
componentList.push_back(mp_label); | ||
|
||
mp_label->setFontSize(40); | ||
} | ||
|
||
void CheckBoxWidget::setPosition(const GuiDimension& position) | ||
{ | ||
mp_rectangle->setPosition(position); | ||
} | ||
|
||
void CheckBoxWidget::setSize(const GuiDimension& size) | ||
{ | ||
mp_rectangle->setSize(size); | ||
} | ||
|
||
void CheckBoxWidget::setColour(float r, float g, float b) | ||
{ | ||
mp_rectangle->colour = {r, g, b}; | ||
} | ||
|
||
void CheckBoxWidget::setImage(int Image) | ||
{ | ||
mp_rectangle->setTexture(Image); | ||
} | ||
|
||
void CheckBoxWidget::setUncheckedImage(int Image) | ||
{ | ||
uncheckedTexture = Image; | ||
} | ||
|
||
void CheckBoxWidget::setCheckedImage(int Image) | ||
{ | ||
checkedTexture = Image; | ||
} | ||
|
||
void CheckBoxWidget::setText(const std::string& text) | ||
{ | ||
mp_label->setText(text); | ||
} | ||
|
||
void CheckBoxWidget::setTextSize(unsigned size) | ||
{ | ||
mp_label->setFontSize(size); | ||
} | ||
|
||
void CheckBoxWidget::setChecked(bool checked) | ||
{ | ||
m_checked = checked; | ||
} | ||
|
||
void CheckBoxWidget::handleClick(sf::Mouse::Button button, float mx, float my) | ||
{ | ||
if (mp_rectangle->isInBounds(mx, my) && button == sf::Mouse::Left) { | ||
onClick(); | ||
} | ||
} | ||
|
||
void CheckBoxWidget::handleMouseMove(float mx, float my) | ||
{ | ||
if (mp_rectangle->isInBounds(mx, my)) { | ||
if (m_onMoveOver.valid()) { | ||
m_onMoveOver(); | ||
} | ||
} | ||
else { | ||
if (m_onMoveOver.valid()) { | ||
m_onMouseOff(); | ||
} | ||
} | ||
} | ||
|
||
void CheckBoxWidget::onClick() | ||
{ | ||
m_checked = !m_checked; | ||
if (m_checked) | ||
setImage(checkedTexture); | ||
else | ||
setImage(uncheckedTexture); | ||
} | ||
|
||
void CheckBoxWidget::setOnMouseOver(sol::function function) | ||
{ | ||
m_onMoveOver = function; | ||
} | ||
|
||
void CheckBoxWidget::setOnMouseOff(sol::function function) | ||
{ | ||
m_onMouseOff = function; | ||
} | ||
|
||
void CheckBoxWidget::prepareRender() | ||
{ | ||
GuiDimension pos = centerText(mp_rectangle->getBounds(), *mp_label); | ||
pos.offset.x -= mp_rectangle->getBounds().width / 6; | ||
|
||
mp_label->setPosition(pos); | ||
} | ||
|
||
bool CheckBoxWidget::getChecked() | ||
{ | ||
return m_checked; | ||
} | ||
|
||
|
||
} // namespace gui |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include "widget.h" | ||
|
||
#include <sol/sol.hpp> | ||
|
||
namespace gui { | ||
|
||
class CheckBoxWidget final : public Widget { | ||
public: | ||
CheckBoxWidget(RectangleComponent* mp_rectangle, TextComponent* label); | ||
void setPosition(const GuiDimension& position) final override; | ||
void setSize(const GuiDimension& size) final override; | ||
|
||
void setColour(float r, float g, float b) final override; | ||
void setImage(int Image); | ||
void setUncheckedImage(int Image); | ||
void setCheckedImage(int Image); | ||
|
||
void setText(const std::string& text); | ||
void setTextSize(unsigned size); | ||
|
||
void setChecked(bool check); | ||
|
||
void handleClick(sf::Mouse::Button, float, float) final override; | ||
void handleMouseMove(float, float) final override; | ||
|
||
|
||
void onClick(); | ||
void setOnMouseOver(sol::function function); | ||
void setOnMouseOff(sol::function function); | ||
|
||
void prepareRender() final override; | ||
|
||
bool getChecked(); | ||
|
||
private: | ||
|
||
int checkedTexture; | ||
int uncheckedTexture; | ||
|
||
bool m_checked; | ||
RectangleComponent* mp_rectangle = nullptr; | ||
TextComponent* mp_label = nullptr; | ||
|
||
sol::function m_onMoveOver; | ||
sol::function m_onMouseOff; | ||
}; | ||
|
||
} // namespace gui |
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