Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Close #339: Messing about with CheckBox([]{})...
Browse files Browse the repository at this point in the history
  • Loading branch information
xparq committed Oct 2, 2023
1 parent 1f5c084 commit a366d4a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 64 deletions.
58 changes: 29 additions & 29 deletions include/sfw/Widgets/CheckBox.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef SFW_CHECKBOX_HPP
#define SFW_CHECKBOX_HPP
#ifndef _SFW_CHECKBOX_HPP_
#define _SFW_CHECKBOX_HPP_

#include "sfw/InputWidget.hpp"
#include "sfw/Gfx/Elements/Box.hpp"
Expand All @@ -16,40 +16,40 @@ namespace sfw
class CheckBox: public InputWidget<CheckBox>
{
public:
CheckBox(bool checked = false);
CheckBox(std::function<void(CheckBox*)> callback, bool checked = false);
CheckBox(bool checked = false);
CheckBox(std::function<void(CheckBox*)> callback, bool checked = false);

// "Generic-input-level" abstract get/set
CheckBox* set(bool checked);
bool get() const { return m_checked; }
// "Generic-input-level" abstract get/set
CheckBox* set(bool checked);
bool get() const { return m_checked; }

// Widget-specific operations
CheckBox* check() { return update(true); }
CheckBox* uncheck() { return update(false); }
CheckBox* toggle() { return update(!checked()); }
// Widget-specific operations
CheckBox* check() { return update(true); }
CheckBox* uncheck() { return update(false); }
CheckBox* toggle() { return update(!checked()); }

// Widget-specific queries
operator bool() const { return checked(); }
bool checked() const { return get(); }
// Still keeping the legacy style, too:
bool isChecked() const { return checked(); }
// Widget-specific queries
operator bool() const { return checked(); }
bool checked() const { return get(); }
// Still keeping the legacy style, too:
bool isChecked() const { return checked(); }

private:
void draw(const gfx::RenderContext& ctx) const override;

// Callbacks
void onStateChanged(WidgetState state) override;
void onThemeChanged() override;
void onMouseReleased(float x, float y) override;
void onKeyPressed(const sf::Event::KeyEvent& key) override;

// State
Box m_box;
CheckMark m_checkmark;
bool m_checked;
void draw(const gfx::RenderContext& ctx) const override;

// Callbacks
void onStateChanged(WidgetState state) override;
void onThemeChanged() override;
void onMouseReleased(float x, float y) override;
void onKeyPressed(const sf::Event::KeyEvent& key) override;

// State
Box m_box;
CheckMark m_checkmark;
bool m_checked;
};


} // namespace

#endif // SFW_CHECKBOX_HPP
#endif // _SFW_CHECKBOX_HPP_
63 changes: 35 additions & 28 deletions src/lib/Widgets/CheckBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,32 @@
#include <SFML/Graphics/RenderTarget.hpp>

#include <algorithm>
using std::max;
using std::max;

namespace sfw
{

CheckBox::CheckBox(bool checked_state):
m_box(Box::Input)
m_box(Box::Input)
{
set(checked_state);

onThemeChanged(); //!!kludge to force geom. recalc.!
}

CheckBox::CheckBox(std::function<void(CheckBox*)> callback, bool checked):
CheckBox(checked)
CheckBox(checked)
{
setCallback(callback);
setCallback(callback);
}

/* See #339, why this couldn't be added...
CheckBox::CheckBox(std::function<void()> callback, bool checked):
CheckBox(checked)
{
setCallback(callback);
}
*/

CheckBox* CheckBox::set(bool checked)
{
Expand All @@ -35,52 +42,52 @@ CheckBox* CheckBox::set(bool checked)

void CheckBox::draw(const gfx::RenderContext& ctx) const
{
auto sfml_renderstates = ctx.props;
sfml_renderstates.transform *= getTransform();
ctx.target.draw(m_box, sfml_renderstates);
if (checked())
ctx.target.draw(m_checkmark, sfml_renderstates);
auto sfml_renderstates = ctx.props;
sfml_renderstates.transform *= getTransform();
ctx.target.draw(m_box, sfml_renderstates);
if (checked())
ctx.target.draw(m_checkmark, sfml_renderstates);
}


// callbacks -------------------------------------------------------------------

void CheckBox::onStateChanged(WidgetState state)
{
m_box.applyState(state);
m_box.applyState(state);
}


void CheckBox::onThemeChanged()
{
float chkmark_offset = Theme::PADDING + Theme::borderSize;
float box_size = m_checkmark.getSize().x + chkmark_offset * 2;
m_box.setSize(box_size, box_size);
setSize(max(box_size, (float)Theme::getLineSpacing() / 1.5f),
max(box_size, (float)Theme::getBoxHeight()) * 0.85f); //!! Heuristic quick-fix for #199...

m_box.setPosition((getSize().x - m_box.getSize().x) / 2,
(getSize().y - m_box.getSize().y) / 2);
m_checkmark.setPosition({m_box.getPosition().x + chkmark_offset,
m_box.getPosition().y + chkmark_offset});
float chkmark_offset = Theme::PADDING + Theme::borderSize;
float box_size = m_checkmark.getSize().x + chkmark_offset * 2;
m_box.setSize(box_size, box_size);
setSize(max(box_size, (float)Theme::getLineSpacing() / 1.5f),
max(box_size, (float)Theme::getBoxHeight()) * 0.85f); //!! Heuristic quick-fix for #199...

m_box.setPosition((getSize().x - m_box.getSize().x) / 2,
(getSize().y - m_box.getSize().y) / 2);
m_checkmark.setPosition({m_box.getPosition().x + chkmark_offset,
m_box.getPosition().y + chkmark_offset});
}


void CheckBox::onMouseReleased(float x, float y)
{
if (contains(sf::Vector2f(x, y)))
{
toggle();
}
if (contains(sf::Vector2f(x, y)))
{
toggle();
}
}


void CheckBox::onKeyPressed(const sf::Event::KeyEvent& key)
{
if (key.code == sf::Keyboard::Space)
{
toggle();
}
if (key.code == sf::Keyboard::Space)
{
toggle();
}
}

} // namespace
7 changes: 0 additions & 7 deletions src/test/issue/#2497-sfml-regression.cpp

This file was deleted.

7 changes: 7 additions & 0 deletions src/test/issue/#339-CheckBox-0-ary-callback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "sfw/GUI.hpp"

int main()
{
//!! Wow, this is ambiguous with CheckBox(bool)!... :-o Great job, C++! :)
//!!auto dummy = new sfw::CheckBox([]{});
}

0 comments on commit a366d4a

Please sign in to comment.