Added direct theme support and will slowly move more things into this struct for configuring the look.

main
Zed A. Shaw 2 weeks ago
parent b9deb3a0de
commit a18d60dcb0
  1. 1
      include/guecs/sfml/backend.hpp
  2. 15
      include/guecs/sfml/color.hpp
  3. 36
      include/guecs/sfml/components.hpp
  4. 20
      include/guecs/ui.hpp
  5. 1
      meson.build
  6. 17
      src/guecs/sfml/backend.cpp
  7. 6
      src/guecs/sfml/components.cpp
  8. 11
      src/guecs/theme.cpp
  9. 8
      src/guecs/ui.cpp

@ -12,5 +12,6 @@ namespace sfml {
void sound_stop(const string& name); void sound_stop(const string& name);
std::shared_ptr<sf::Shader> shader_get(const std::string& name); std::shared_ptr<sf::Shader> shader_get(const std::string& name);
bool shader_updated(); bool shader_updated();
guecs::Theme theme();
}; };
} }

@ -1,15 +0,0 @@
#pragma once
#include <SFML/Graphics/Color.hpp>
namespace ColorValue {
constexpr const sf::Color BLACK{0, 0, 0};
constexpr const sf::Color DARK_DARK{10, 10, 10};
constexpr const sf::Color DARK_MID{30, 30, 30};
constexpr const sf::Color DARK_LIGHT{60, 60, 60};
constexpr const sf::Color MID{100, 100, 100};
constexpr const sf::Color LIGHT_DARK{150, 150, 150};
constexpr const sf::Color LIGHT_MID{200, 200, 200};
constexpr const sf::Color LIGHT_LIGHT{230, 230, 230};
constexpr const sf::Color WHITE{255, 255, 255};
constexpr const sf::Color TRANSPARENT = sf::Color::Transparent;
}

@ -1,31 +1,21 @@
#pragma once #pragma once
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "guecs/dbc.hpp" #include "guecs/dbc.hpp"
#include "guecs/sfml/color.hpp"
#include "guecs/lel.hpp" #include "guecs/lel.hpp"
#include <string> #include <string>
#include <memory> #include <memory>
#include <functional> #include <functional>
#include <any> #include <any>
#include "guecs/theme.hpp"
namespace guecs { namespace guecs {
using std::shared_ptr, std::wstring, std::string; using std::shared_ptr, std::wstring, std::string;
constexpr const int PADDING = 3;
constexpr const int BORDER_PX = 1;
constexpr const int TEXT_SIZE = 30;
constexpr const int LABEL_SIZE = 20;
constexpr const sf::Color FILL_COLOR = ColorValue::DARK_MID;
constexpr const sf::Color TEXT_COLOR = ColorValue::LIGHT_LIGHT;
constexpr const sf::Color BG_COLOR = ColorValue::MID;
constexpr const sf::Color BORDER_COLOR = ColorValue::MID;
constexpr const char *FONT_FILE_NAME="assets/text.otf";
struct Textual { struct Textual {
std::wstring content; std::wstring content;
unsigned int size = TEXT_SIZE; unsigned int size = THEME.TEXT_SIZE;
sf::Color color = TEXT_COLOR; sf::Color color = THEME.TEXT_COLOR;
int padding = PADDING; int padding = THEME.PADDING;
bool centered = false; bool centered = false;
shared_ptr<sf::Font> font = nullptr; shared_ptr<sf::Font> font = nullptr;
shared_ptr<sf::Text> text = nullptr; shared_ptr<sf::Text> text = nullptr;
@ -39,7 +29,7 @@ namespace guecs {
Label(Args... args) : Textual(args...) Label(Args... args) : Textual(args...)
{ {
centered = true; centered = true;
size = LABEL_SIZE; size = THEME.LABEL_SIZE;
} }
Label() { Label() {
@ -55,7 +45,7 @@ namespace guecs {
// or there's a static config function you call once, // or there's a static config function you call once,
// that's passed an object with all the necessary gear // that's passed an object with all the necessary gear
string name; string name;
int padding = PADDING; int padding = THEME.PADDING;
std::shared_ptr<sf::Sprite> sprite = nullptr; std::shared_ptr<sf::Sprite> sprite = nullptr;
void init(lel::Cell &cell); void init(lel::Cell &cell);
@ -63,10 +53,10 @@ namespace guecs {
}; };
struct Rectangle { struct Rectangle {
int padding = PADDING; int padding = THEME.PADDING;
sf::Color color = FILL_COLOR; sf::Color color = THEME.FILL_COLOR;
sf::Color border_color = BORDER_COLOR; sf::Color border_color = THEME.BORDER_COLOR;
int border_px = BORDER_PX; int border_px = THEME.BORDER_PX;
shared_ptr<sf::RectangleShape> shape = nullptr; shared_ptr<sf::RectangleShape> shape = nullptr;
void init(lel::Cell& cell); void init(lel::Cell& cell);
@ -74,7 +64,7 @@ namespace guecs {
struct Meter { struct Meter {
float percent = 1.0f; float percent = 1.0f;
sf::Color color = ColorValue::BLACK; sf::Color color = THEME.BG_COLOR_DARK;
Rectangle bar; Rectangle bar;
void init(lel::Cell& cell); void init(lel::Cell& cell);
@ -108,10 +98,10 @@ namespace guecs {
float y = 0.0f; float y = 0.0f;
float w = 0.0f; float w = 0.0f;
float h = 0.0f; float h = 0.0f;
sf::Color color = BG_COLOR; sf::Color color = THEME.BG_COLOR;
shared_ptr<sf::RectangleShape> shape = nullptr; shared_ptr<sf::RectangleShape> shape = nullptr;
Background(lel::Parser& parser, sf::Color bg_color=BG_COLOR) : Background(lel::Parser& parser, sf::Color bg_color=THEME.BG_COLOR) :
x(parser.grid_x), x(parser.grid_x),
y(parser.grid_y), y(parser.grid_y),
w(parser.grid_w), w(parser.grid_w),

@ -8,6 +8,7 @@
#include <queue> #include <queue>
#include <typeindex> #include <typeindex>
#include <unordered_map> #include <unordered_map>
#include "guecs/theme.hpp"
#include "guecs/sfml/components.hpp" #include "guecs/sfml/components.hpp"
namespace guecs { namespace guecs {
@ -38,25 +39,6 @@ namespace guecs {
string name; string name;
}; };
struct SpriteTexture {
std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr;
};
class Backend {
public:
virtual SpriteTexture texture_get(const string& name) = 0;
virtual void sound_play(const string& name) = 0;
virtual void sound_stop(const string& name) = 0;
virtual std::shared_ptr<sf::Shader> shader_get(const std::string& name) = 0;
virtual bool shader_updated() = 0;
};
void init(Backend* backend);
class UI { class UI {
public: public:

@ -73,6 +73,7 @@ sources = [
'src/guecs/dbc.cpp', 'src/guecs/dbc.cpp',
'src/guecs/ui.cpp', 'src/guecs/ui.cpp',
'src/guecs/lel.cpp', 'src/guecs/lel.cpp',
'src/guecs/theme.cpp',
'src/guecs/sfml/components.cpp', 'src/guecs/sfml/components.cpp',
] ]

@ -35,4 +35,21 @@ namespace sfml {
return false; return false;
} }
} }
guecs::Theme Backend::theme() {
guecs::Theme theme;
theme.PADDING = 3;
theme.BORDER_PX = 1;
theme.TEXT_SIZE = 30;
theme.LABEL_SIZE = 20;
theme.FILL_COLOR = theme.DARK_MID;
theme.TEXT_COLOR = theme.LIGHT_LIGHT;
theme.BG_COLOR = theme.MID;
theme.BORDER_COLOR = theme.LIGHT_DARK;
theme.BG_COLOR_DARK = theme.BLACK;
theme.FONT_FILE_NAME = "assets/text.otf";
return theme;
}
} }

@ -2,14 +2,8 @@
#include "guecs/sfml/backend.hpp" #include "guecs/sfml/backend.hpp"
namespace guecs { namespace guecs {
static Backend* BACKEND = nullptr;
using std::make_shared; using std::make_shared;
void init(Backend* backend) {
BACKEND = backend;
}
void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) { void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
dbc::check(font_ptr != nullptr, "you failed to initialize this WideText"); dbc::check(font_ptr != nullptr, "you failed to initialize this WideText");
if(font == nullptr) font = font_ptr; if(font == nullptr) font = font_ptr;

@ -0,0 +1,11 @@
#include "guecs/theme.hpp"
namespace guecs {
Backend* BACKEND = nullptr;
Theme THEME;
void init(Backend* backend) {
BACKEND = backend;
THEME = BACKEND->theme();
}
}

@ -5,7 +5,7 @@ namespace guecs {
using std::make_shared; using std::make_shared;
UI::UI() { UI::UI() {
$font = make_shared<sf::Font>(FONT_FILE_NAME); $font = make_shared<sf::Font>(THEME.FONT_FILE_NAME);
} }
void UI::position(int x, int y, int width, int height) { void UI::position(int x, int y, int width, int height) {
@ -180,7 +180,7 @@ namespace guecs {
tc->text->setString(content); tc->text->setString(content);
} else { } else {
auto &cell = cell_for(ent); auto &cell = cell_for(ent);
Textual to_set{content, TEXT_SIZE}; Textual to_set{content, THEME.TEXT_SIZE};
to_set.init(cell, $font); to_set.init(cell, $font);
set<Textual>(ent, to_set); set<Textual>(ent, to_set);
} }
@ -214,9 +214,9 @@ namespace guecs {
tc->text->setString(content); tc->text->setString(content);
} else { } else {
auto &cell = cell_for(ent); auto &cell = cell_for(ent);
Label to_set{content, LABEL_SIZE}; Label to_set{content, THEME.LABEL_SIZE};
to_set.init(cell, $font); to_set.init(cell, $font);
to_set.text->setFillColor(TEXT_COLOR); to_set.text->setFillColor(THEME.TEXT_COLOR);
set<Label>(ent, to_set); set<Label>(ent, to_set);
} }
} }

Loading…
Cancel
Save