Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if( SFGUI_INCLUDE_FONT )
target_compile_definitions( ${TARGET} PRIVATE SFGUI_INCLUDE_FONT )
endif()

target_link_libraries( ${TARGET} PUBLIC SFML::Graphics OpenGL::GL )
target_link_libraries( ${TARGET} PUBLIC SFML::Graphics OpenGL::GL tinyxml2 )

# Tell the compiler to export when necessary.
set_target_properties( ${TARGET} PROPERTIES DEFINE_SYMBOL SFGUI_EXPORTS )
Expand Down Expand Up @@ -122,7 +122,7 @@ elseif( "${CMAKE_SYSTEM_NAME}" MATCHES "Linux" )
find_package( X11 REQUIRED )
target_link_libraries( ${TARGET} PUBLIC X11::X11 )
set( SHARE_PATH "${CMAKE_INSTALL_PREFIX}/share/SFGUI" )

if( LIB_SUFFIX )
set( LIB_PATH "lib${LIB_SUFFIX}" )
else()
Expand Down
8 changes: 6 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
cmake_minimum_required( VERSION 3.22 )

# the flag -rdynamic are required to XMLLoader parser string callback in xml
set(CMAKE_CXX_FLAGS "-O2 -rdynamic")

function( build_example SAMPLE_NAME SOURCES )
add_executable( ${SAMPLE_NAME} ${SOURCES} )
target_link_libraries( ${SAMPLE_NAME} PRIVATE SFGUI::SFGUI)
Expand Down Expand Up @@ -38,6 +41,7 @@ build_example( "ProgressBar" "ProgressBar.cpp" )
build_example( "SpinButton" "SpinButton.cpp" )
build_example( "Canvas" "Canvas.cpp" )
build_example( "CustomWidget" "CustomWidget.cpp" )
build_example( "XMLLoader" "XMLLoader.cpp" )
build_example( "SFGUI-Test" "Test.cpp" )

# Copy data directory to build cache directory to be able to run examples from
Expand All @@ -49,13 +53,13 @@ if( NOT ( "${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}" ) )
COMMAND "${CMAKE_COMMAND}"
ARGS -E copy_directory "${PROJECT_SOURCE_DIR}/examples/data" "${PROJECT_BINARY_DIR}/examples/data"
)

add_custom_command(
TARGET "Image" POST_BUILD
COMMAND "${CMAKE_COMMAND}"
ARGS -E copy_directory "${PROJECT_SOURCE_DIR}/examples/data" "${PROJECT_BINARY_DIR}/examples/data"
)

add_custom_command(
TARGET "Canvas" POST_BUILD
COMMAND "${CMAKE_COMMAND}"
Expand Down
50 changes: 50 additions & 0 deletions examples/XMLLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <SFML/Graphics.hpp>
#include <SFGUI/SFGUI.hpp>
#include <SFGUI/Widgets.hpp>
#include <SFGUI/UI/XMLLoader.hpp>

extern "C" void button_click_event(sfg::Widget::Ptr widget, sf::RenderWindow* render){
puts("Clicked");
}

extern "C" void on_tab_change_event(sfg::Notebook::Ptr notebook, sf::RenderWindow* render){
puts("Tab changed!");
}

extern "C" void config_windo_load_event_for_widget(sfg::Notebook::Ptr notebook, sf::RenderWindow* render){
puts("Window as created!");
}

int main() {
sf::RenderWindow appWindow(sf::VideoMode({800, 600}), "Exemplo SFGUI");
appWindow.setFramerateLimit(60);
appWindow.resetGLStates();

sfg::SFGUI sfgui;
sfg::Desktop desktop;

sfg::ui::XMLLoader::Ptr widget = sfg::ui::XMLLoader::Create();
widget->loadFromFile("data/window.xml");
desktop.Add(std::dynamic_pointer_cast<sfg::Window>(widget->getWidget()));

desktop.LoadThemeFromFile("data/xmltheme.theme");

while (appWindow.isOpen()) {
while (auto event = appWindow.pollEvent()) {
desktop.HandleEvent(*event);

if (event->is<sf::Event::Closed>()) {
appWindow.close();
}
}

desktop.Update(0.0f);

appWindow.clear(sf::Color(0xffffffff));
sfgui.Display(appWindow);

appWindow.display();
}

return 0;
}
Binary file added examples/data/Courier Prime Bold.ttf
Binary file not shown.
Binary file added examples/data/maid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions examples/data/window.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<Window title="Loaded from éXML" style="SHADOW,CLOSE,BACKGROUND,TITLEBAR" pos="10 10" size="640 680" on-create="config_windo_load_event_for_widget">
<Notebook on-tab-change="on_tab_change_event">
<Box orientation="VERTICAL" label="Views">
<Label>This is an label!</Label>
<ProgressBar value="0.3" orientation="HORIZONTAL"/>
<Scrollbar orientation="HORIZONTAL"/>
<Separator orientation="HORIZONTAL"/>
<Spinner size="40 40" pos="0 0"/>
<ScrolledWindow size="400 200">
<Image src="data/maid.png"/>
</ScrolledWindow>
</Box>
<Box orientation="VERTICAL" label="Events" spacing="2">
<Button class="success">Success</Button>
<Button class="warning">Warning</Button>
<Button class="danger">Danger</Button>
<Button class="" on-left-click="button_click_event">Normal</Button>

<ToggleButton>ToggleButton</ToggleButton>
</Box>
<Box orientation="VERTICAL" label="Inputs">
<Box orientation="" label="Inputs">
<Frame label="CheckButton" size="200 120">
<Box orientation="VERTICAL">
<CheckButton label="CheckButton 1"/>
<CheckButton label="CheckButton 2"/>
<CheckButton label="CheckButton 3"/>
</Box>
</Frame>
<Frame label="RadioButton">
<Box orientation="VERTICAL">
<Frame label="Group 1">
<Box orientation="VERTICAL">
<RadioButton group="test" label="RadioButton 1"/>
<RadioButton group="test" label="RadioButton 2"/>
<RadioButton group="test" label="RadioButton 3"/>
</Box>
</Frame>
<Frame label="Group 2">
<Box orientation="VERTICAL">
<RadioButton group="next" label="RadioButton 4"/>
<RadioButton group="next" label="RadioButton 5"/>
<RadioButton group="next" label="RadioButton 6"/>
</Box>
</Frame>
</Box>
</Frame>
</Box>
<Entry id="entry_id" value="Value"/>
<Scale min="0" max="10" step="1" value="3"/>
<ComboBox selected="2">
<Item>Item 1</Item>
<Item>Item 2</Item>
<Item>Item 3</Item>
<Item>Item 4</Item>
<Item>Item 5</Item>
</ComboBox>
<SpinButton min="0" max="10" step="2" value="4"/>
</Box>
<Fixed label="Fixed Layout">
<Button pos="30 50">Fixed Button</Button>
<Label pos="100 10">Fixed Label</Label>
</Fixed>
<Table label="Table" spacing="10">
<Row>
<Col>
<Button>{0, 0} - {1, 1}</Button>
</Col>
<Col>
<Button rows="2">{1, 0} - {1, 2}</Button>
</Col>
</Row>
<Row>
<Col>
<Button>{0, 1} - {1, 1}</Button>
</Col>
</Row>
<Row>
<Col>
<Button cols="2">{0, 2} - {2, 1}</Button>
</Col>
</Row>
</Table>
</Notebook>
</Window>
53 changes: 53 additions & 0 deletions examples/data/xmltheme.theme
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
*{
FontName: data/Courier Prime Bold.ttf;
}

Window{
BackgroundColor: #22262aff;
BorderColor: #00000000;
}

Button {
BackgroundColor: #6497e4ff;
BorderColor: #1e56abff;
Color: #FFFFFFFF;
}

Button:prelight {
BorderColor: #1e56abff;
BackgroundColor: #256ddaff;
}

Button:active {
BorderColor: #6497e4ff;
BackgroundColor: #1e56abff;
}

Button.danger {
BackgroundColor: #d45e5eFF;
BorderColor: #8d2626ff;
}

Button.danger:active {
BorderColor: #d45e5eFF;
BackgroundColor: #8d2626ff;
}

Button.warning {
BackgroundColor: #e6ba6aff;
BorderColor: #b37e1eff;
}

Button.success {
BackgroundColor: #44af7fff;
BorderColor: #20533cff;
}

Entry {
BackgroundColor: #C8C8C8FF;
Color: #000000FF;
}

Entry#my_entry {
FontSize: 14;
}
88 changes: 88 additions & 0 deletions include/SFGUI/UI/XMLLoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#pragma once

#include <tinyxml2.h>

#include <SFGUI/Widgets.hpp>
#include <string>

namespace sfg {
namespace ui {
const std::string OnStateChange ("on-state-change"); // Widget event selector
const std::string OnGainFocus ("on-gain-focus"); // Widget event selector
const std::string OnLostFocus ("on-lost-focus"); // Widget event selector
const std::string OnExpose ("on-expose"); // Widget event selector
const std::string OnSizeAllocate ("on-size-allocate"); // Widget event selector
const std::string OnSizeRequest ("on-size-request"); // Widget event selector
const std::string OnMouseEnter ("on-mouse-enter"); // Widget event selector
const std::string OnMouseLeave ("on-mouse-leave"); // Widget event selector
const std::string OnMouseMove ("on-mouse-move"); // Widget event selector
const std::string OnMouseLeftPress ("on-mouse-left-press"); // Widget event selector
const std::string OnMouseRightPress ("on-mouse-right-press"); // Widget event selector
const std::string OnMouseLeftRelease ("on-mouse-left-release"); // Widget event selector
const std::string OnMouseRightRelease ("on-mouse-right-release"); // Widget event selector
const std::string OnLeftClick ("on-left-click"); // Widget event selector
const std::string OnRightClick ("on-right-click"); // Widget event selector
const std::string OnKeyPress ("on-key-press"); // Widget event selector
const std::string OnKeyRelease ("on-key-release"); // Widget event selector
const std::string OnText ("on-text"); // Widget event selector
const std::string OnSelect ("on-select"); // ComboBox event selector
const std::string OnOpen ("on-open"); // ComboBox event selector
const std::string OnTextChanged ("on-text-changed"); // Entry event selector
const std::string OnTabChange ("on-tab-change"); // Notebook event selector
const std::string OnValueChanged ("on-value-changed"); // SpinButton event selector
const std::string OnToggle ("on-toggle"); // ToggleButton event selector
const std::string OnCloseButton ("on-close-button"); // Window event selector

const std::string OnCreate ("on-create"); // this event dont need signal

class SFGUI_API XMLLoader : protected ::tinyxml2::XMLDocument {
public:
typedef std::shared_ptr<XMLLoader> Ptr; //!< Shared pointer.
typedef std::shared_ptr<const XMLLoader> PtrConst; //!< Shared pointer.

static std::map<std::string, sfg::Signal::SignalID*> SIGNAL;

////////////////////////////////////////////////////////////
/// \brief Create XMLLoader object
///
/// \return Shared pointer of XMLLoader
///
////////////////////////////////////////////////////////////
static Ptr Create();

////////////////////////////////////////////////////////////
/// \brief Load XML file
///
/// This function read a XML file to generate widget structures
///
/// \param fileName File name or file path
/// \param render Pointer of sf::RenderWindow (used to param in callbacks event defined in xml file)
///
/// \return true if success and false if fail
///
////////////////////////////////////////////////////////////
bool loadFromFile(std::string fileName, sf::RenderWindow* render = nullptr);

////////////////////////////////////////////////////////////
/// \brief Get widet
///
/// This function return the root widget from loaded file
/// the returned widget can be box, buttons or any where widget defined in xml file
///
/// \return Return shared pointer from sfg::Widget
///
////////////////////////////////////////////////////////////
std::shared_ptr<sfg::Widget> getWidget();

protected:
XMLLoader();

private:
sfg::Widget::Ptr m_widget;
sf::RenderWindow* m_render;
bool m_reload;

bool parser();
};
} // namespace ui
} // namespace sfg
Loading
Loading