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

Commit

Permalink
util/smis -> util/shim/sfml
Browse files Browse the repository at this point in the history
+ Refined Geometry.hpp
  • Loading branch information
xparq committed Jun 27, 2023
1 parent 259ac2e commit a0fa65d
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 21 deletions.
63 changes: 57 additions & 6 deletions include/sfw/Geometry.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
#ifndef SFW_GEOMETRY_HPP
#define SFW_GEOMETRY_HPP

#include "sfw/util/generic.hpp" // using sfw::Unknown, sfw::UseDefault;

namespace sfw
{
/*
2D (on-screen) orientation of geometric entities
enum Orientation
{
Horizontal,
Vertical
};
- The interpretation of Horizontal|Vertical combinations as "diagonal"
is intentionally undefined here.)
*/
struct Orientation // `enum class Orientation` would forbid all the num. ops. (despite the :int)!
//!! This is the best trade-off between the namespace pollution of plain
//!! enums and the overly restrictive & cumbersome enum classes. This way
//!! there's no mandatory `Orientation::` qualifiers, but only for selected
//!! names, where that makes sense. Also, implicit num. conversions work.
{
enum _symbolic_ {
Unknown = sfw::Unknown,
UseDefault = sfw::UseDefault,
// `Default` would be hopelessly ambiguous: should it directly
// encode a value (like `Horizontal`), and then remain hardcoded
// + possibly too context-specific forever, or should it have a
// dedicated pseudo-value meaning "use the actually configured
// default for the given usage context"?...

Horizontal = 1,
Vertical = Horizontal << 1, // Support Horizontal|Vertical

Directed = 1 << 4,
Leftward = Directed | Horizontal,
Rightward = Directed | Horizontal | (Directed << 1),
Upward = Directed | Vertical,
Downward = Directed | Vertical | (Directed << 1),
};

Orientation() : value(Unknown) {}
Orientation(_symbolic_ x) : value(x) {}

operator _symbolic_() const { return value; }
//!!Unfortunately, this doesn't alleviate the need for an op= and op!=:
// operator int() { return value; }
bool operator ==(_symbolic_ x) const { return x == value; }
bool operator !=(_symbolic_ x) const { return x != value; }

_symbolic_ value;
};

// Emulate (only the "good parts"!) of a plain enum version...
// (Note: an enum class can't be `using`ed into scope all at once,
// only its values one by one. :-/ )
using Orientation::Horizontal, Orientation::Vertical,
//Orientation::Directed, // <- This one's kinda internal.
Orientation::Leftward, Orientation::Rightward, Orientation::Upward, Orientation::Downward;

// Sanity chk.:
static_assert((int)Unknown != UseDefault);
static_assert((int)Unknown != Horizontal);
static_assert((int)UseDefault != Horizontal);
static_assert(Orientation::Directed > (Horizontal|Vertical));

}
} // namespace

#endif // SFW_GEOMETRY_HPP
2 changes: 1 addition & 1 deletion include/sfw/Gfx/_backend-specific/SFML/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define SFW_GFX_TEXT_SFML_HPP

#include <SFML/Graphics/Text.hpp>
#include "sfw/util/shims.hpp"
#include "sfw/util/shim/sfml.hpp"

namespace sfw
{
Expand Down
2 changes: 1 addition & 1 deletion include/sfw/Widgets/OptionsBox.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "sfw/Theme.hpp"
#include "sfw/util/shims.hpp" // std::string <-> sf::String conv.
#include "sfw/util/shim/sfml.hpp" // std::string <-> sf::String conv.
#include "sfw/util/diagnostics.hpp"

#include <algorithm>
Expand Down
13 changes: 13 additions & 0 deletions include/sfw/util/generic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SFW_GENERIC_HPP
#define SFW_GENERIC_HPP

namespace sfw
{
enum : int
{
Unknown = 0,
UseDefault = -1,
};
}

#endif // SFW_GENERIC_HPP
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/Layout.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "sfw/Layout.hpp"
#include "sfw/Theme.hpp"
#include "sfw/Gfx/Render.hpp"
#include "sfw/util/shims.hpp" // for sf::Event::KeyEvent::==
#include "sfw/util/shim/sfml.hpp" // for sf::Event::KeyEvent::==

#ifdef DEBUG
# include "sfw/GUI-main.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/WidgetContainer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "sfw/WidgetContainer.hpp"

#include "sfw/GUI-main.hpp"
#include "sfw/util/shims.hpp"
#include "sfw/util/shim/sfml.hpp"

#include <cassert>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Widgets/Button.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sfw/Widgets/Button.hpp"
#include "sfw/Theme.hpp"
#include "sfw/util/shims.hpp"
#include "sfw/util/shim/sfml.hpp"

#include <algorithm>
using std::max;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Widgets/Label.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sfw/Widgets/Label.hpp"
#include "sfw/Theme.hpp"
#include "sfw/util/shims.hpp"
#include "sfw/util/shim/sfml.hpp"

#include <SFML/Graphics/RenderTarget.hpp>

Expand Down
13 changes: 4 additions & 9 deletions src/lib/Widgets/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "sfw/Theme.hpp"
#include "sfw/GUI-main.hpp"
#include "sfw/util/utf8.hpp"
#include "sfw/util/shims.hpp"
#include "sfw/util/shim/sfml.hpp"
#include "sfw/util/diagnostics.hpp"

#include <SFML/Graphics/RenderTarget.hpp>
Expand Down Expand Up @@ -58,13 +58,8 @@ TextBox::TextBox(float pxWidth, CursorStyle style):
TextBox* TextBox::set(const std::string& content)
{
m_text.set(sfw::utf8_substr(content, 0, m_maxLength)); // Limit the length

setCursorPos(length());

//!! This (i.e. calling the user callback also on set...()) should be consistent across all the widgets! -> #257
//!! Doing this prematurely (before attaching to the GUI) is not yet clearly warned about (or prevented)! -> #109, #271

//!!onUpdate(); //!! <- Can't do it yet, as the demo app relies on it not happening! :-/
//!! changed(); //! Will become more sophisticated later (-> Undo/Redo)
return this;
}

Expand Down Expand Up @@ -273,7 +268,7 @@ void TextBox::Paste()
std::string newcontent = get();
auto u8bpos = utf8_bsize(newcontent, m_cursorPos);
newcontent.insert(u8bpos, clip);
m_text.set(newcontent); //! not this->set(), to preserve m_cursorPos
m_text.set(newcontent); //! not this->set(), to preserve the cursor pos.
// Go to the end of the inserted part (or EOS)
setCursorPos(m_cursorPos + cliplen);
}
Expand Down Expand Up @@ -502,7 +497,7 @@ void TextBox::onKeyPressed(const sf::Event::KeyEvent& key)

// "Apply"
case sf::Keyboard::Enter:
onUpdate();
onUpdate(); //!!TBD: Should instead call update(), but that's a NOOP if !changed()
break;

// Ctrl+A: Select All
Expand Down

0 comments on commit a0fa65d

Please sign in to comment.