Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
austinmhorn committed Jan 13, 2024
1 parent 2e21bdf commit a8a2f66
Show file tree
Hide file tree
Showing 21 changed files with 373 additions and 311 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ if( WIN32 )
set( LIB_PATH "lib" )
elseif( APPLE )
find_library( FOUNDATION_LIBRARY Foundation REQUIRED )
find_library( COREGRAPHICS_LIBRARY CoreGraphics REQUIRED )
find_library( COREGRAPHICS_LIBRARY CoreGraphics REQUIRE D)

target_link_libraries( ${TARGET} PUBLIC ${FOUNDATION_LIBRARY} ${COREGRAPHICS_LIBRARY} )
set( SHARE_PATH "${CMAKE_INSTALL_PREFIX}/share/leafy" )
Expand Down
39 changes: 18 additions & 21 deletions examples/Checkbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ using namespace leafy;

#include <iostream>

void printCheckboxSelections(Checkbox& checkbox, const std::string& caption)
{
// Print choice string of active selection for each Checkbox object
std::cout << caption << std::endl;
for ( auto &s : checkbox.getSelection() )
std::cout << " - " << s << std::endl;
}

int main(int argc, char const **argv)
{
sf::RenderWindow window = sf::RenderWindow{sf::VideoMode(800, 600), "Checkbox Demo", sf::Style::Close};
Expand Down Expand Up @@ -41,32 +49,21 @@ int main(int argc, char const **argv)
{
case sf::Event::Closed:
window.close();
break;
break;

case sf::Event::MouseButtonReleased:
std::cout << "\n*********************************" << std::endl;
printCheckboxSelections(checkbox1, "Checkbox #1");
printCheckboxSelections(checkbox2, "Checkbox #2");
printCheckboxSelections(checkbox3, "Checkbox #3");
std::cout << "*********************************\n" << std::endl;
break;

default:
break;
break;
}
}

static const auto clock = sf::Clock{};
static auto last_frame_time = sf::Time{};
const auto delta_time = clock.getElapsedTime() - last_frame_time;
last_frame_time = clock.getElapsedTime();

// Print choice string of active selection for each Checkbox object
for ( auto &s : checkbox1.getSelection() )
{
std::cout << "checkbox1: " << s << std::endl;
}
for ( auto &s : checkbox2.getSelection() )
{
std::cout << "checkbox2: " << s << std::endl;
}
for ( auto &s : checkbox3.getSelection() )
{
std::cout << "checkbox3: " << s << std::endl;
}

window.clear();
window.draw(checkbox1);
window.draw(checkbox2);
Expand Down
17 changes: 8 additions & 9 deletions examples/Dropdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ int main(int argc, char const **argv)
const auto window_size = sf::Vector2f{ window.getSize() };

Dropdown dropdown1({120.f, 20.f});
Dropdown dropdown2({200.f, 30.f});
Dropdown dropdown3({350.f, 40.f});
//Dropdown dropdown2({200.f, 30.f});
//Dropdown dropdown3({350.f, 40.f});

dropdown1.setPosition({window_size.x/2.f - dropdown1.getSize().x/2.f, window_size.y/2.f - 100.f});
dropdown2.setPosition({window_size.x/2.f - dropdown2.getSize().x/2.f, window_size.y/2.f - dropdown2.getSize().y/2.f});
dropdown3.setPosition({window_size.x/2.f - dropdown3.getSize().x/2.f, window_size.y/2.f + 100.f});
//dropdown2.setPosition({window_size.x/2.f - dropdown2.getSize().x/2.f, window_size.y/2.f - dropdown2.getSize().y/2.f});
//dropdown3.setPosition({window_size.x/2.f - dropdown3.getSize().x/2.f, window_size.y/2.f + 100.f});

// Start the game loop
while (window.isOpen())
{
for (auto event = sf::Event{}; window.pollEvent(event);)
{

// Update the Dropdown with event and window
dropdown1.handleEvent(window, event);
dropdown2.handleEvent(window, event);
dropdown3.handleEvent(window, event);
//dropdown2.handleEvent(window, event);
//dropdown3.handleEvent(window, event);

switch(event.type)
{
Expand All @@ -39,8 +38,8 @@ int main(int argc, char const **argv)

window.clear();
window.draw(dropdown1);
window.draw(dropdown2);
window.draw(dropdown3);
//window.draw(dropdown2);
//window.draw(dropdown3);
window.display();
}

Expand Down
54 changes: 27 additions & 27 deletions include/leafy/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
namespace leafy
{

template <typename T>
template <typename _Shape>
class LEAFY_API Button
: public UIElement
{
public:

// Verify T is a derived sf::Shape object; required [even if not drawing shape] to deduce T
static_assert(std::is_base_of<sf::Shape, typename std::remove_reference<T>::type>::value,
"T must be derived from sf::Shape.");
// Verify _Shape is a derived sf::Shape object; required [even if not drawing shape] to deduce _Shape
static_assert(std::is_base_of<sf::Shape, typename std::remove_reference<_Shape>::type>::value,
"_Shape must be derived from sf::Shape.");

/// @brief Represents response capability to an event type
enum class Function : std::size_t {
Expand All @@ -63,18 +63,16 @@ namespace leafy

/////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Constructor
/// \param shape Derived sf::Shape object; required [even if shape is disabled] to deduce T
/// \param shape Derived sf::Shape object; required [even if shape is disabled] to deduce _Shape
/////////////////////////////////////////////////////////////////////////////////////////////
explicit Button(const T& shape = T());
explicit Button(const _Shape& shape = _Shape());

//////////////////////////
/// \brief Destructor
//////////////////////////
~Button() override = default;



void setWillDrawShape(bool willDraw);
void setDrawShape(bool willDraw);

/////////////////////////////////////////////////
/// @brief Set the position of Button
Expand Down Expand Up @@ -121,32 +119,33 @@ namespace leafy

bool contains(const sf::Vector2f& point) const override;
void handleEvent(sf::RenderWindow& window, sf::Event event) override;
void update(sf::Time delta_time) override;

/////////////////////////////////////////////////////////////////////
/// @brief Set size for shapes with rectangular geometry
/// @param size New size width and height
/// @details Enabled if T is equivalent to any of the following types
/// @details Enabled if _Shape is equivalent to any of the following types
/// - RectangleShape
/// - RoundedRectangleShape
/// - StadiumShape
/////////////////////////////////////////////////////////////////////
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::RectangleShape>::value, int>::type = 0>
void setSize(const sf::Vector2f& size)
{
UIElement::setSize( size );
m_shape.setSize( m_size );
updateAlignment();
}
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::RoundedRectangleShape>::value, int>::type = 0>
void setSize(const sf::Vector2f& size)
{
UIElement::setSize( size );
m_shape.setSize( m_size );
updateAlignment();
}
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::StadiumShape>::value, int>::type = 0>
void setSize(const sf::Vector2f& size)
{
Expand All @@ -158,27 +157,27 @@ namespace leafy
/////////////////////////////////////////////////////////////////////////
/// @brief Get size of rectangular button
/// @return Width and height of button
/// @details Enabled if T is equivalent to any of the following types
/// @details Enabled if _Shape is equivalent to any of the following types
/// - RectangleShape
/// - RoundedRectangleShape
/// - StadiumShape
/////////////////////////////////////////////////////////////////////////
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::RectangleShape>::value, int>::type = 0>
const sf::Vector2f& getSize() const { return UIElement::getSize(); }
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::RoundedRectangleShape>::value, int>::type = 0>
const sf::Vector2f& getSize() const { return UIElement::getSize(); }
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::StadiumShape>::value, int>::type = 0>
const sf::Vector2f& getSize() const { return UIElement::getSize(); }

/////////////////////////////////////////////////////////////////////
/// @brief Set radius for shapes with symmetric circular geometry
/// @param radius New radius as float
/// @details Enabled if type T is a sf::CircleShape
/// @details Enabled if type _Shape is a sf::CircleShape
/////////////////////////////////////////////////////////////////////
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::CircleShape>::value, int>::type = 0>
void setRadius(float radius)
{
Expand All @@ -190,18 +189,18 @@ namespace leafy
/////////////////////////////////////////////////////////////////////
/// @brief Get radius of circular button
/// @return Radius of button
/// @details Enabled if type T is a sf::CircleShape
/// @details Enabled if type _Shape is a sf::CircleShape
/////////////////////////////////////////////////////////////////////
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::CircleShape>::value, int>::type = 0>
float getRadius() const { return UIElement::getRadius(); }

/////////////////////////////////////////////////////////////////////
/// @brief Set X and Y radius for shapes with convex polygon geometry
/// @param radius New X and Y radius
/// @details Enabled if type T is a sf::PolygonShape
/// @details Enabled if type _Shape is a sf::PolygonShape
/////////////////////////////////////////////////////////////////////
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::PolygonShape>::value, int>::type = 0>
void setPolygonRadius(const sf::Vector2f& radius)
{
Expand All @@ -213,9 +212,9 @@ namespace leafy
/////////////////////////////////////////////////////////////////////
/// @brief Get X and Y radius of convex polygon button
/// @return X and Y radius of
/// @details Enabled if type T is a sf::PolygonShape
/// @details Enabled if type _Shape is a sf::PolygonShape
/////////////////////////////////////////////////////////////////////
template <typename type_t = T,
template <typename type_t = _Shape,
typename std::enable_if<std::is_same<type_t, sf::PolygonShape>::value, int>::type = 0>
const sf::Vector2f& getPolygonRadius() const
{ return UIElement::getPolygonRadius(); }
Expand All @@ -229,11 +228,12 @@ namespace leafy

void mouseEnter() override;
void mouseLeave() override;
void mouseClick() override;
void pressed() override;
void clicked() override;

void draw(sf::RenderTarget& target, sf::RenderStates states) const override;

T m_shape; // T is a derived sf::Shape
_Shape m_shape; // _Shape is a derived sf::Shape
sf::Text m_label; // Text label of button
bool m_drawShape;
bool m_translucentHover;
Expand Down
3 changes: 2 additions & 1 deletion include/leafy/Checkbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class LEAFY_API Checkbox

virtual void mouseEnter() override;
virtual void mouseLeave() override;
virtual void mouseClick() override;
virtual void pressed() override;
virtual void clicked() override;
virtual bool contains(const sf::Vector2f& point) const override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;

Expand Down
3 changes: 2 additions & 1 deletion include/leafy/Dropdown.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class LEAFY_API Dropdown

void mouseEnter() override;
void mouseLeave() override;
void mouseClick() override;
void pressed() override;
void clicked() override;

private:

Expand Down
23 changes: 15 additions & 8 deletions include/leafy/Engine/Resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#ifndef Assets_hpp
#define Assets_hpp

#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Color.hpp>

#include <iostream> // std::cin, std::cout
#include <new> // std::bad_alloc
#include <cmath> // std::ceil, std::log
#include <map>
#include <cassert> // assert
#include <string>
//#include <new> // std::bad_alloc
//#include <cmath> // std::ceil, std::log
//#include <cassert> // assert

#include <leafy/SmartMouse.hpp>

Expand All @@ -38,20 +40,25 @@ namespace leafy
namespace Textures
{

static sf::Texture __load_domino()
static sf::Texture __load_texture(const std::string& filepath)
{
static sf::Texture texture;
try
{
if ( !texture.loadFromFile(__filepath_domino) )
if ( !texture.loadFromFile(filepath) )
throw std::runtime_error("Failed loading texture: ");
}
catch(const std::runtime_error &e)
{
std::cerr << "std::runtime_error::what(): " << e.what() << __filepath_domino << std::endl;
std::cerr << "std::runtime_error::what(): " << e.what() << filepath << std::endl;
}
return texture;
}

static const sf::Texture __load_domino()
{
return __load_texture(__filepath_domino);
}
} // MARK: End of namespace 'Textures'

struct Resources
Expand Down
4 changes: 2 additions & 2 deletions include/leafy/Engine/SystemInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <windows.h>
#elif LINUX || linux || __linux__
#include <X11/Xlib.h>
#elif APPLE || MACH
#elif APPLE || MACH || __APPLE__ || __MACH__
#include <CoreGraphics/CGDisplayConfiguration.h>
#else
#endif
Expand All @@ -26,7 +26,7 @@ static void getScreenResolution(unsigned int& width, unsigned int& height) {
Screen* s = DefaultScreenOfDisplay(d);
width = s->width;
height = s->height;
#elif APPLE || MACH
#elif APPLE || MACH || __APPLE__ || __MACH__
auto mainDisplayId = CGMainDisplayID();
width = static_cast<unsigned int>(CGDisplayPixelsWide(mainDisplayId));
height = static_cast<unsigned int>(CGDisplayPixelsHigh(mainDisplayId));
Expand Down
Loading

0 comments on commit a8a2f66

Please sign in to comment.