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

Commit

Permalink
Changes to compile with SFML/SFML@698f265
Browse files Browse the repository at this point in the history
- Close #436: Sync to recent SFML changes in the Event API
- Other changes (in the SAL) to cope with further breaking changes
  (despite the SFML API freeze announced earlier)
  • Loading branch information
xparq committed Aug 26, 2024
1 parent e5a8870 commit 15b07dd
Show file tree
Hide file tree
Showing 36 changed files with 252 additions and 267 deletions.
46 changes: 24 additions & 22 deletions include/SAL/geometry/Rectangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ namespace SAL::geometry
public:
constexpr Rectangle_Interface() = default;

//!!?? Accept native vector types (our Vector auto-converts to those, so they work, too),
//!!?? Accept backend vector types (our Vector auto-converts to those, so they work, too),
//!!?? but that might not actually be a good thing -- could be confusingly permissive, and
//!!?? also untenable with more complex classes, which then would also be expected to support
//!!?? all sorts of other native types transparently (with minor combinatoric explosions)!
//!!?? all sorts of other backend types transparently (with minor combinatoric explosions)!
//!!?? (And the confusion part: generic APIs are not expected to work with "random" foreign
//!!?? types; however, supporting the types of the current backend could be excused.)
constexpr Rectangle_Interface(Impl::vector_type position, Impl::vector_type size)
: Impl(position.native(), size.native()) {}
: Impl(position.foreign(), size.foreign()) {}

constexpr Rectangle_Interface(Impl::vector_type size)
: Impl({0,0}, size.native()) {}

/*!! OLD:
// Convert from lval with different number type:
template <class OtherT>// requires (!std::is_same_v<typename OtherT::number_type, typename Impl::number_type>)
constexpr Rectangle_Interface(const OtherT& other) //! `const` would break rval inputs like auto r = fRect(), despite having an rval ctor, too! :-o
: Impl( {(typename Impl::number_type)other.x(), (typename Impl::number_type)other.y()},
{(typename Impl::number_type)other.width(), (typename Impl::number_type)other.height()} ) { static_assert(false, "HERE"); }
!!*/
: Impl({0,0}, size.foreign()) {}

//!! OLD:
// // Convert from lval with different number type:
// template <class OtherT>// requires (!std::is_same_v<typename OtherT::number_type, typename Impl::number_type>)
// constexpr Rectangle_Interface(const OtherT& other) //! `const` would break rval inputs like auto r = fRect(), despite having an rval ctor, too! :-o
// : Impl( {(typename Impl::number_type)other.x(), (typename Impl::number_type)other.y()},
// {(typename Impl::number_type)other.width(), (typename Impl::number_type)other.height()} ) { /*!!NOPE:static_!!*/assert(false, "HERE"); }
//!!
// Convert from rval with different number type:
//!! These versions both failed to enable this ctor for e.g. const iRect const_ir; auto cfr = fRect(const_ir4);
// template <Rectangle OtherT> Rectangle_Interface(OtherT&& other) ...
Expand All @@ -75,7 +75,7 @@ namespace SAL::geometry
>
constexpr Rectangle_Interface(OtherT&& other)
: Impl( {(typename Impl::number_type)std::forward<OtherT>(other).x(), (typename Impl::number_type)std::forward<OtherT>(other).y()},
{(typename Impl::number_type)std::forward<OtherT>(other).width(), (typename Impl::number_type)std::forward<OtherT>(other).height()} ) {} //!!{ static_assert(false, "HERE"); }
{(typename Impl::number_type)std::forward<OtherT>(other).width(), (typename Impl::number_type)std::forward<OtherT>(other).height()} ) {} //!!{ /*!!NOPE:static_!!*/assert(false, "HERE"); }

//!! Also: if the converting ctor above indeed transparently covers rvals/lvals & const/non-consts,
//!! then do the same for the plain (Other == Self) copy/move ctors below:
Expand All @@ -86,26 +86,28 @@ namespace SAL::geometry
//!! decay_t (or at least std::remove_cvref_t) is CRITICAL for accepting both const/non-const OtherT ctor args.! :-o
>
constexpr Rectangle_Interface(const SameT& other) : Impl(std::forward(other.native()))
{ //!!??static_
assert("FINALLY HERE!... :)" && false); } //!!?? WHY IS THIS NEVER ACTUALLY INSTANTIATED IN GCC, BUT IS IN MSVC?! :-o (No failed static_assert in GCC! :-o )
/*!! OLD:
//!!?? WHY ARE THESE NEVER ACTUALLY INSTANTIATED IN GCC, BUT ARE IN MSVC?! :-o (No failed static_assert in GCC! :-o )
{
assert("FINALLY HERE!... :)" && false); } //!!?? WHY IS THIS NEVER ACTUALLY INSTANTIATED IN GCC, BUT IS IN MSVC?! :-o (No failed assert in GCC! :-o )
//!! Oh, BTW, no, static_assert is useless for this purpose, however straightforward
//!! it would be...: https://groups.google.com/a/isocpp.org/g/std-proposals/c/d2ADcbygYn4/m/jNtpL2GpBgAJ
#if 0 //!! OLD:
//!!?? WHY ARE THESE NEVER ACTUALLY INSTANTIATED IN GCC, BUT ARE IN MSVC?! :-o (No failed asserts in GCC! :-o )
template <Rectangle SameT,
typename = std::enable_if_t< std::is_same_v<typename std::decay_t<SameT>::number_type, typename Impl::number_type> >
//!! decay_t (or at least std::remove_cvref_t) is CRITICAL for accepting both const/non-const OtherRect ctor args.! :-o
>
constexpr Rectangle_Interface(const SameT& other) : Impl(std::forward(other.native()))
{ //!!??static_
assert("SHOULDN'T BE HERE!" && false); }
{
/*!!NOPE:static_!!*/assert("SHOULDN'T BE HERE!" && false); }

// Copy:
template <class SameT> requires (std::is_same_v<typename SameT::number_type, typename Impl::number_type>)
constexpr Rectangle_Interface(const SameT& other) : Impl(other.native()) { static_assert(false, "HERE"); }
constexpr Rectangle_Interface(const SameT& other) : Impl(other.native()) { /*!!NOPE:static_!!*/assert(false, "HERE"); }

// Move (still should just be a copy though):
template <class SameT> requires (std::is_same_v<typename SameT::number_type, typename Impl::number_type>)
constexpr Rectangle_Interface(SameT&& other) : Impl(std::move(other.native())) { static_assert(false, "HERE"); }
!!*/
constexpr Rectangle_Interface(SameT&& other) : Impl(std::move(other.native())) { /*!!NOPE:static_!!*/assert(false, "HERE"); }
#endif
// All other signatures are passed to the native impl.:
using Impl::Impl; // Elevate the backend-specific ctors for implicit backend -> abstract conversions!

Expand Down
4 changes: 2 additions & 2 deletions include/SAL/geometry/SFML/Rectangle_Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace SFML
constexpr const auto& native() const { return _d; }

// Intercept our vectors:
constexpr Rectangle_Impl(Vec2<NumT> pos, Vec2<NumT> size) : _d(pos.native(), size.native()) {}
//!!?? OR: constexpr Rectangle_Impl(const Vec2<NumT>& pos, const Vec2<NumT>& size) : _d(pos.native(), size.native()) {}
constexpr Rectangle_Impl(Vec2<NumT> pos, Vec2<NumT> size) : _d(pos.foreign(), size.foreign()) {}
//!!?? OR: constexpr Rectangle_Impl(const Vec2<NumT>& pos, const Vec2<NumT>& size) : _d(pos.foreign(), size.foreign()) {}

// Forward the original signatures:
template <typename... T>
Expand Down
9 changes: 7 additions & 2 deletions include/SAL/gfx/element/Font/SFML/Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace SAL::gfx::adapter

namespace SFML
{
static_assert( ! std::is_default_constructible_v<sf::Font> );
//static_assert( ! std::is_default_constructible_v<sf::Font> );
// But it's copyable?! :-o WHY?! It's NOT just a handle!
//!!?! And how?! It doesn't have a copy ctor, only a user-defined custom one! :-o
static_assert( std::is_copy_constructible_v<sf::Font> );
Expand All @@ -38,7 +38,7 @@ class Font_Impl //!! Can't just derive from sf::Font, as it's not default-constr
using native_type = sf::Font;

bool load(std::string_view filename) {
_native_font = sf::Font::openFromFile(filename);
_native_font = sf::Font::createFromFile(filename);
return _valid();
}

Expand Down Expand Up @@ -119,6 +119,11 @@ class Font_Impl //!! Can't just derive from sf::Font, as it's not default-constr
return _native_font.value(); // Just a ref.
}

constexpr native_type& native() {
_err_if_null();
return _native_font.value(); // Just a ref.
}

}; // class Font_Impl


Expand Down
7 changes: 4 additions & 3 deletions include/SAL/gfx/element/Texture/SFML/Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ class Texture : public sf::Texture
Texture() : sf::Texture(sf::Texture::create({1, 1}).value()) {} //!! Assumed never to fail...

bool load(std::string_view filename, const geometry::iRect& r = {})
{ auto res = sf::Texture::loadFromFile(filename, false, r); // false: no sRTGB support in this adapter...
{ auto res = sf::Texture::createFromFile(filename, false, r); // false: no sRTGB support in this adapter...
if (res) { native() = res.value(); } return res.has_value(); }

bool set(const sf::Image& image, const geometry::iRect& r = {})
{ auto res = sf::Texture::loadFromImage(image, false, r); // false: no sRTGB support in this adapter...
if (res) { native() = res.value(); } return res.has_value(); }
{ auto res = native().loadFromImage(image, false, r); // false: no sRTGB support in this adapter...
//!!OLD: if (res) { native() = res.value(); } return res.has_value(); }
return res; }

iVec2 size() const { return iVec2(uVec2(native().getSize())); } // getSize() is unsigned!
//!! xparq/pocketvec#18:
Expand Down
33 changes: 0 additions & 33 deletions include/SAL/sfml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,6 @@
#define _LASKDRUHTNIUSCE5H7Y7N4W0987YW4U58MSDHFMYKJHN_


//!!----------------------------------------------------------------------------
//!! This should (also?) be moved to an SFML-backed Event adapter:

#include <SFML/Window/Event.hpp>

namespace sf //! Inject into sf:: (to let innocent client code actually find it via ADL)!
{
// FFS, still no default == (at least for PODs), even in c++20?! :-/
// (https://stackoverflow.com/a/27837789/1479945)
inline bool operator==(const sf::Event::KeyChanged& key1, const sf::Event::KeyChanged& key2)
{
return key1.code == key2.code
//&& key1.scancode == key2.scancode
&& key1.alt == key2.alt
&& key1.control == key2.control
&& key1.shift == key2.shift
&& key1.system == key2.system
;
}
} // namespace sf

inline bool SFML_keypress_has_modifiers(const sf::Event::KeyChanged& key)
// Is there a sane way already to check this?
{
return key.alt
|| key.control
|| key.shift
|| key.system
;
}


//----------------------------------------------------------------------------
#include <SFML/System/String.hpp>
#include <string>
#include <string_view>
Expand Down
6 changes: 3 additions & 3 deletions include/sfw/Layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Layout: public WidgetContainer
void onMousePressed(float x, float y) override;
void onMouseReleased(float x, float y) override;
void onMouseWheelMoved(int delta) override;
void onKeyPressed(const sf::Event::KeyChanged& key) override;
void onKeyReleased(const sf::Event::KeyChanged& key) override;
void onTextEntered(char32_t unichar) override;
void onKeyPressed(const event::KeyCombination& key) override;
void onKeyReleased(const event::KeyCombination& key) override;
void onTextEntered(char32_t codepoint) override;

private:
Widget* m_hoveredWidget;
Expand Down
8 changes: 4 additions & 4 deletions include/sfw/Theme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
//#include "SAL/geometry/Rectangle.hpp" // Definitely done already by the rectangular things above. ;)
//#include "sfw/gfx/Color.hpp" // Also this, by those colorful things above. :)

#include "sfw/event/Keyboard.hpp" // KeyState

#include <SFML/Window.hpp>
#include <SFML/Window/Event.hpp>

#include <map>
#include <string>
#include <string_view>

Expand Down Expand Up @@ -109,8 +109,8 @@ class Theme
static float PADDING; // Spacing inside widgets
static float MARGIN; // Spacing between widgets

static sf::Event::KeyChanged previousWidgetKey;
static sf::Event::KeyChanged nextWidgetKey;
static event::KeyState previousWidgetKey;
static event::KeyState nextWidgetKey;

static const sf::Cursor& mousePointer;

Expand Down
4 changes: 2 additions & 2 deletions include/sfw/widget/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class Button: public InputWidget<Button>
void onMouseMoved(float x, float y) override;
void onMousePressed(float x, float y) override;
void onMouseReleased(float x, float y) override;
void onKeyPressed(const sf::Event::KeyChanged& key) override;
void onKeyReleased(const sf::Event::KeyChanged& key) override;
void onKeyPressed(const event::KeyCombination& key) override;
void onKeyReleased(const event::KeyCombination& key) override;
void onThemeChanged() override;

ItemBox<gfx::Text> m_box;
Expand Down
2 changes: 1 addition & 1 deletion include/sfw/widget/CheckBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CheckBox: public InputWidget<CheckBox>
void onActivationChanged(ActivationState state) override;
void onThemeChanged() override;
void onMouseReleased(float x, float y) override;
void onKeyPressed(const sf::Event::KeyChanged& key) override;
void onKeyPressed(const event::KeyCombination& key) override;

// State
Box m_box;
Expand Down
4 changes: 2 additions & 2 deletions include/sfw/widget/ImageButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class ImageButton: public InputWidget<ImageButton>
void onMouseMoved(float x, float y) override;
void onMousePressed(float x, float y) override;
void onMouseReleased(float x, float y) override;
void onKeyPressed(const sf::Event::KeyChanged& key) override;
void onKeyReleased(const sf::Event::KeyChanged& key) override;
void onKeyPressed(const event::KeyCombination& key) override;
void onKeyReleased(const event::KeyCombination& key) override;

void centerText();
void press();
Expand Down
4 changes: 2 additions & 2 deletions include/sfw/widget/OptionsBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class OptionsBox: public InputWidget<OptionsBox<T>>
void onMouseReleased(float x, float y) override;
void onMouseWheelMoved(int delta) override;

void onKeyPressed(const sf::Event::KeyChanged& key) override;
void onKeyReleased(const sf::Event::KeyChanged& key) override;
void onKeyPressed(const event::KeyCombination& key) override;
void onKeyReleased(const event::KeyCombination& key) override;
void onThemeChanged() override;

// -------- Helpers...
Expand Down
32 changes: 16 additions & 16 deletions include/sfw/widget/OptionsBox.inl
Original file line number Diff line number Diff line change
Expand Up @@ -293,44 +293,44 @@ template <class T> void OptionsBox<T>::onMouseWheelMoved(int delta)
}


template <class T> void OptionsBox<T>::onKeyPressed(const sf::Event::KeyChanged& key)
template <class T> void OptionsBox<T>::onKeyPressed(const event::KeyCombination& key)
{
switch (key.code)
switch (key.code) //!!XLAT below!
{
case sf::Keyboard::Key::Left:
case sf::Keyboard::Key::Up:
case unsigned(sf::Keyboard::Key::Left):
case unsigned(sf::Keyboard::Key::Up):
selectPrevious();
m_arrowLeft.press();
break;
case sf::Keyboard::Key::Right:
case sf::Keyboard::Key::Down:
case unsigned(sf::Keyboard::Key::Right):
case unsigned(sf::Keyboard::Key::Down):
selectNext();
m_arrowRight.press();
break;
case sf::Keyboard::Key::Home:
case sf::Keyboard::Key::PageUp:
case unsigned(sf::Keyboard::Key::Home):
case unsigned(sf::Keyboard::Key::PageUp):
selectFirst();
break;
case sf::Keyboard::Key::End:
case sf::Keyboard::Key::PageDown:
case unsigned(sf::Keyboard::Key::End):
case unsigned(sf::Keyboard::Key::PageDown):
selectLast();
break;
default: ; // (Just for GCC to shut up...)
}
}

template <class T> void OptionsBox<T>::onKeyReleased(const sf::Event::KeyChanged& key)
template <class T> void OptionsBox<T>::onKeyReleased(const event::KeyCombination& key)
{
switch (key.code)
switch (key.code) //!!XLAT below!
{
case sf::Keyboard::Key::Left:
case sf::Keyboard::Key::Up:
case unsigned(sf::Keyboard::Key::Left):
case unsigned(sf::Keyboard::Key::Up):
m_arrowLeft.release();
// Without this the focus rect would be lost on the arrow (#137):
update_arrow_pressed_state(m_arrowLeft, -1, -1); // -1,-1 to avoid the "hover" state
break;
case sf::Keyboard::Key::Right:
case sf::Keyboard::Key::Down:
case unsigned(sf::Keyboard::Key::Right):
case unsigned(sf::Keyboard::Key::Down):
m_arrowRight.release();
// Without this the focus rect would be lost on the arrow (#137):
update_arrow_pressed_state(m_arrowRight, -1, -1); // -1,-1 to avoid the "hover" state
Expand Down
2 changes: 1 addition & 1 deletion include/sfw/widget/Slider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Slider: public InputWidget<Slider>
void draw(const gfx::RenderContext& ctx) const override;

// Callbacks
void onKeyPressed(const sf::Event::KeyChanged& key) override;
void onKeyPressed(const event::KeyCombination& key) override;
void onMousePressed(float x, float y) override;
void onMouseMoved(float x, float y) override;
void onMouseReleased(float x, float y) override;
Expand Down
10 changes: 5 additions & 5 deletions include/sfw/widget/TextBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


#include "sfw/InputWidget.hpp"
#include "sfw/TextSelection.hpp"
#include "sfw/TextSelection.hpp" //!!?? -> ::parts, or rather another -- abstract -- component lib!
#include "sfw/gfx/element/Text.hpp"
#include "sfw/gfx/element/Box.hpp"
#include "sfw/gfx/Color.hpp"
Expand Down Expand Up @@ -92,23 +92,23 @@ class TextBox: public InputWidget<TextBox>
void set_selection(size_t from, size_t to);
void clear_selection();
void delete_selected();
bool flip_selection(const sf::Event::KeyChanged& key, size_t from, size_t to);
bool flip_selection(const event::KeyCombination& key, size_t from, size_t to);
void update_view();
size_t pos_at_mouse(float mouse_x);

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

// Callbacks
void onKeyPressed(const sf::Event::KeyChanged& key) override;
void onKeyReleased(const sf::Event::KeyChanged& key) override;
void onKeyPressed(const event::KeyCombination& key) override;
void onKeyReleased(const event::KeyCombination& key) override;
void onMouseEnter() override;
void onMouseLeave() override;
void onMousePressed(float x, float y) override;
void onMouseReleased(float x, float y) override;
void onMouseMoved(float x, float y) override;
void onMouseWheelMoved(int delta) override;
void onTextEntered(char32_t unichar) override;
void onTextEntered(char32_t codepoint) override;
void onActivationChanged(ActivationState state) override;
void onThemeChanged() override;

Expand Down
2 changes: 1 addition & 1 deletion src/example/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ cerr << "font size: "<< themecfg.fontSize << endl; //!!#196
demo.process(event);

// Close on Esc:
if (event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Key::Escape)
if (event.is<sfw::event::KeyPressed>() && event.get_if<sfw::event::KeyPressed>()->code == unsigned(sf::Keyboard::Key::Escape))
demo.close();
}

Expand Down
Loading

0 comments on commit 15b07dd

Please sign in to comment.