parent
6cdebb298c
commit
29ec390478
@ -0,0 +1,130 @@ |
|||||||
|
#include "guecs/lel.hpp" |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <numeric> |
||||||
|
#include <cassert> |
||||||
|
|
||||||
|
#include "./lel_parser.cpp" |
||||||
|
|
||||||
|
namespace lel { |
||||||
|
|
||||||
|
Parser::Parser(int x, int y, int width, int height) : |
||||||
|
grid_x(x), |
||||||
|
grid_y(y), |
||||||
|
grid_w(width), |
||||||
|
grid_h(height) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
void Parser::position(int x, int y, int width, int height) { |
||||||
|
grid_x = x; |
||||||
|
grid_y = y; |
||||||
|
grid_w = width; |
||||||
|
grid_h = height; |
||||||
|
|
||||||
|
// used in GUIs for setting backgrounds
|
||||||
|
main = { |
||||||
|
.x=grid_x, |
||||||
|
.y=grid_y, |
||||||
|
.w=grid_w, |
||||||
|
.h=grid_h, |
||||||
|
.mid_x=std::midpoint(grid_x, grid_x + grid_w), |
||||||
|
.mid_y=std::midpoint(grid_y, grid_y + grid_h), |
||||||
|
.max_w=grid_w, |
||||||
|
.max_h=grid_h |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
void Parser::id(const std::string& name) { |
||||||
|
if(name != "_") { |
||||||
|
assert(!cells.contains(name) && "duplicate cell name"); |
||||||
|
cells.insert_or_assign(name, cur); |
||||||
|
} |
||||||
|
|
||||||
|
cur = {.col=cur.col + 1, .row=cur.row}; |
||||||
|
auto& row = grid.back(); |
||||||
|
row.push_back(name); |
||||||
|
} |
||||||
|
|
||||||
|
void Parser::finalize() { |
||||||
|
size_t rows = grid.size(); |
||||||
|
int cell_height = grid_h / rows; |
||||||
|
|
||||||
|
for(auto& row : grid) { |
||||||
|
// BUG: see issue #16
|
||||||
|
size_t columns = row.size(); |
||||||
|
int cell_width = grid_w / columns; |
||||||
|
assert(cell_width > 0 && "invalid cell width calc"); |
||||||
|
assert(cell_height > 0 && "invalid cell height calc"); |
||||||
|
|
||||||
|
for(auto& name : row) { |
||||||
|
if(name == "_") continue; |
||||||
|
auto& cell = cells.at(name); |
||||||
|
|
||||||
|
// ZED: getting a bit hairy but this should work
|
||||||
|
if(cell.percent) { |
||||||
|
// when percent mode we have to take unset to 100%
|
||||||
|
if(cell.max_w == 0) cell.max_w = 100; |
||||||
|
if(cell.max_h == 0) cell.max_h = 100; |
||||||
|
cell.max_w *= cell_width * 0.01; |
||||||
|
cell.max_h *= cell_height * 0.01; |
||||||
|
} else { |
||||||
|
if(cell.max_w == 0) cell.max_w = cell_width; |
||||||
|
if(cell.max_h == 0) cell.max_h = cell_height; |
||||||
|
} |
||||||
|
|
||||||
|
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w); |
||||||
|
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h); |
||||||
|
|
||||||
|
assert(cell.h > 0 && "invalid height cell is <= 0"); |
||||||
|
assert(cell.w > 0 && "invalid width cell is <= 0"); |
||||||
|
|
||||||
|
cell.x = grid_x + (cell.col * cell_width); |
||||||
|
cell.y = grid_y + (cell.row * cell_height); |
||||||
|
|
||||||
|
// keep the midpoint since it is used a lot
|
||||||
|
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w); |
||||||
|
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h); |
||||||
|
|
||||||
|
// perform alignments
|
||||||
|
if(cell.right) cell.x += cell_width - cell.w; |
||||||
|
if(cell.bottom) cell.y += cell_height - cell.h; |
||||||
|
if(cell.center) { |
||||||
|
cell.x = cell.mid_x - cell.w / 2; |
||||||
|
cell.y = cell.mid_y - cell.h / 2; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Parser::reset() { |
||||||
|
cur = {.col=0, .row=0}; |
||||||
|
main = {.col=0, .row=0}; |
||||||
|
grid.clear(); |
||||||
|
cells.clear(); |
||||||
|
} |
||||||
|
|
||||||
|
std::optional<std::string> Parser::hit(int x, int y) { |
||||||
|
for(auto& [name, cell] : cells) { |
||||||
|
if(name != "_MAIN" && |
||||||
|
(x >= cell.x && x <= cell.x + cell.w) && |
||||||
|
(y >= cell.y && y <= cell.y + cell.h)) |
||||||
|
{ |
||||||
|
return name; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return std::nullopt; |
||||||
|
} |
||||||
|
|
||||||
|
Cell center(int width, int height, Cell &parent) { |
||||||
|
Cell copy = parent; |
||||||
|
|
||||||
|
// BUG: could I use std::midpoint here or am I even using this?
|
||||||
|
copy.x = parent.mid_x - width / 2; |
||||||
|
copy.y = parent.mid_y - height / 2; |
||||||
|
copy.w = width; |
||||||
|
copy.h = height; |
||||||
|
|
||||||
|
return copy; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,52 @@ |
|||||||
|
#pragma once |
||||||
|
#include <string> |
||||||
|
#include <unordered_map> |
||||||
|
#include <optional> |
||||||
|
#include <vector> |
||||||
|
|
||||||
|
namespace lel { |
||||||
|
|
||||||
|
struct Cell { |
||||||
|
int col = 0; |
||||||
|
int row = 0; |
||||||
|
int x = 0; |
||||||
|
int y = 0; |
||||||
|
int w = 0; |
||||||
|
int h = 0; |
||||||
|
int mid_x = 0; |
||||||
|
int mid_y = 0; |
||||||
|
int max_w = 0; |
||||||
|
int max_h = 0; |
||||||
|
bool right = false; |
||||||
|
bool bottom = false; |
||||||
|
bool expand = false; |
||||||
|
bool center = false; |
||||||
|
bool percent = false; |
||||||
|
}; |
||||||
|
|
||||||
|
using Row = std::vector<std::string>; |
||||||
|
using CellMap = std::unordered_map<std::string, Cell>; |
||||||
|
|
||||||
|
struct Parser { |
||||||
|
int grid_x = 0; |
||||||
|
int grid_y = 0; |
||||||
|
int grid_w = 0; |
||||||
|
int grid_h = 0; |
||||||
|
Cell cur; |
||||||
|
Cell main; |
||||||
|
std::vector<Row> grid; |
||||||
|
CellMap cells; |
||||||
|
|
||||||
|
Parser(int x, int y, int width, int height); |
||||||
|
Parser() : cur{.col=0, .row=0} {}; |
||||||
|
|
||||||
|
void position(int x, int y, int width, int height); |
||||||
|
void id(const std::string& name); |
||||||
|
void reset(); |
||||||
|
bool parse(const std::string& input); |
||||||
|
void finalize(); |
||||||
|
std::optional<std::string> hit(int x, int y); |
||||||
|
}; |
||||||
|
|
||||||
|
Cell center(int width, int height, Cell &parent); |
||||||
|
} |
||||||
@ -0,0 +1,261 @@ |
|||||||
|
|
||||||
|
#line 1 "src/guecs/lel_parser.rl" |
||||||
|
#include "guecs/lel.hpp" |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
namespace lel { |
||||||
|
|
||||||
|
|
||||||
|
#line 41 "src/guecs/lel_parser.rl" |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#line 10 "src/guecs/lel_parser.cpp" |
||||||
|
static const char _Parser_actions[] = { |
||||||
|
0, 1, 1, 1, 2, 1, 3, 1,
|
||||||
|
4, 1, 5, 1, 6, 1, 9, 1,
|
||||||
|
10, 1, 11, 1, 12, 1, 13, 2,
|
||||||
|
0, 7, 2, 0, 8, 2, 4, 1,
|
||||||
|
2, 4, 5 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_key_offsets[] = { |
||||||
|
0, 0, 4, 20, 33, 35, 39, 44,
|
||||||
|
47, 59, 64 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_trans_keys[] = { |
||||||
|
32, 91, 9, 13, 32, 37, 40, 42,
|
||||||
|
46, 61, 94, 95, 9, 13, 60, 62,
|
||||||
|
65, 90, 97, 122, 37, 40, 42, 46,
|
||||||
|
61, 94, 95, 60, 62, 65, 90, 97,
|
||||||
|
122, 48, 57, 41, 44, 48, 57, 32,
|
||||||
|
9, 13, 48, 57, 41, 48, 57, 32,
|
||||||
|
93, 95, 124, 9, 13, 48, 57, 65,
|
||||||
|
90, 97, 122, 32, 93, 124, 9, 13,
|
||||||
|
32, 91, 9, 13, 0 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_single_lengths[] = { |
||||||
|
0, 2, 8, 7, 0, 2, 1, 1,
|
||||||
|
4, 3, 2 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_range_lengths[] = { |
||||||
|
0, 1, 4, 3, 1, 1, 2, 1,
|
||||||
|
4, 1, 1 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_index_offsets[] = { |
||||||
|
0, 0, 4, 17, 28, 30, 34, 38,
|
||||||
|
41, 50, 55 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_indicies[] = { |
||||||
|
0, 2, 0, 1, 3, 4, 5, 6,
|
||||||
|
7, 9, 7, 10, 3, 8, 10, 10,
|
||||||
|
1, 4, 5, 6, 7, 9, 7, 10,
|
||||||
|
8, 10, 10, 1, 11, 1, 12, 13,
|
||||||
|
14, 1, 15, 15, 16, 1, 17, 18,
|
||||||
|
1, 19, 21, 20, 22, 19, 20, 20,
|
||||||
|
20, 1, 23, 24, 25, 23, 1, 26,
|
||||||
|
2, 26, 1, 0 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_trans_targs[] = { |
||||||
|
1, 0, 2, 2, 3, 4, 3, 3,
|
||||||
|
3, 3, 8, 5, 3, 6, 5, 6,
|
||||||
|
7, 3, 7, 9, 8, 10, 2, 9,
|
||||||
|
10, 2, 10 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_trans_actions[] = { |
||||||
|
0, 0, 3, 0, 17, 0, 13, 5,
|
||||||
|
11, 15, 21, 19, 23, 23, 0, 0,
|
||||||
|
19, 26, 0, 7, 0, 32, 29, 0,
|
||||||
|
9, 1, 0 |
||||||
|
}; |
||||||
|
|
||||||
|
static const int Parser_start = 1; |
||||||
|
static const int Parser_first_final = 10; |
||||||
|
static const int Parser_error = 0; |
||||||
|
|
||||||
|
static const int Parser_en_main = 1; |
||||||
|
|
||||||
|
|
||||||
|
#line 44 "src/guecs/lel_parser.rl" |
||||||
|
|
||||||
|
bool Parser::parse(const std::string& input) { |
||||||
|
reset(); |
||||||
|
int cs = 0; |
||||||
|
const char *start = nullptr; |
||||||
|
const char *begin = input.data(); |
||||||
|
const char *p = input.data(); |
||||||
|
const char *pe = p + input.size(); |
||||||
|
std::string tk; |
||||||
|
|
||||||
|
|
||||||
|
#line 91 "src/guecs/lel_parser.cpp" |
||||||
|
{ |
||||||
|
cs = Parser_start; |
||||||
|
} |
||||||
|
|
||||||
|
#line 55 "src/guecs/lel_parser.rl" |
||||||
|
|
||||||
|
#line 94 "src/guecs/lel_parser.cpp" |
||||||
|
{ |
||||||
|
int _klen; |
||||||
|
unsigned int _trans; |
||||||
|
const char *_acts; |
||||||
|
unsigned int _nacts; |
||||||
|
const char *_keys; |
||||||
|
|
||||||
|
if ( p == pe ) |
||||||
|
goto _test_eof; |
||||||
|
if ( cs == 0 ) |
||||||
|
goto _out; |
||||||
|
_resume: |
||||||
|
_keys = _Parser_trans_keys + _Parser_key_offsets[cs]; |
||||||
|
_trans = _Parser_index_offsets[cs]; |
||||||
|
|
||||||
|
_klen = _Parser_single_lengths[cs]; |
||||||
|
if ( _klen > 0 ) { |
||||||
|
const char *_lower = _keys; |
||||||
|
const char *_mid; |
||||||
|
const char *_upper = _keys + _klen - 1; |
||||||
|
while (1) { |
||||||
|
if ( _upper < _lower ) |
||||||
|
break; |
||||||
|
|
||||||
|
_mid = _lower + ((_upper-_lower) >> 1); |
||||||
|
if ( (*p) < *_mid ) |
||||||
|
_upper = _mid - 1; |
||||||
|
else if ( (*p) > *_mid ) |
||||||
|
_lower = _mid + 1; |
||||||
|
else { |
||||||
|
_trans += (unsigned int)(_mid - _keys); |
||||||
|
goto _match; |
||||||
|
} |
||||||
|
} |
||||||
|
_keys += _klen; |
||||||
|
_trans += _klen; |
||||||
|
} |
||||||
|
|
||||||
|
_klen = _Parser_range_lengths[cs]; |
||||||
|
if ( _klen > 0 ) { |
||||||
|
const char *_lower = _keys; |
||||||
|
const char *_mid; |
||||||
|
const char *_upper = _keys + (_klen<<1) - 2; |
||||||
|
while (1) { |
||||||
|
if ( _upper < _lower ) |
||||||
|
break; |
||||||
|
|
||||||
|
_mid = _lower + (((_upper-_lower) >> 1) & ~1); |
||||||
|
if ( (*p) < _mid[0] ) |
||||||
|
_upper = _mid - 2; |
||||||
|
else if ( (*p) > _mid[1] ) |
||||||
|
_lower = _mid + 2; |
||||||
|
else { |
||||||
|
_trans += (unsigned int)((_mid - _keys)>>1); |
||||||
|
goto _match; |
||||||
|
} |
||||||
|
} |
||||||
|
_trans += _klen; |
||||||
|
} |
||||||
|
|
||||||
|
_match: |
||||||
|
_trans = _Parser_indicies[_trans]; |
||||||
|
cs = _Parser_trans_targs[_trans]; |
||||||
|
|
||||||
|
if ( _Parser_trans_actions[_trans] == 0 ) |
||||||
|
goto _again; |
||||||
|
|
||||||
|
_acts = _Parser_actions + _Parser_trans_actions[_trans]; |
||||||
|
_nacts = (unsigned int) *_acts++; |
||||||
|
while ( _nacts-- > 0 ) |
||||||
|
{ |
||||||
|
switch ( *_acts++ ) |
||||||
|
{ |
||||||
|
case 0: |
||||||
|
#line 11 "src/guecs/lel_parser.rl" |
||||||
|
{tk = input.substr(start - begin, p - start); } |
||||||
|
break; |
||||||
|
case 1: |
||||||
|
#line 13 "src/guecs/lel_parser.rl" |
||||||
|
{} |
||||||
|
break; |
||||||
|
case 2: |
||||||
|
#line 14 "src/guecs/lel_parser.rl" |
||||||
|
{ grid.push_back(Row()); } |
||||||
|
break; |
||||||
|
case 3: |
||||||
|
#line 15 "src/guecs/lel_parser.rl" |
||||||
|
{ cur.bottom = (*p) == '.'; } |
||||||
|
break; |
||||||
|
case 4: |
||||||
|
#line 16 "src/guecs/lel_parser.rl" |
||||||
|
{ id(input.substr(start - begin, p - start)); } |
||||||
|
break; |
||||||
|
case 5: |
||||||
|
#line 17 "src/guecs/lel_parser.rl" |
||||||
|
{ cur.col = 0; cur.row++; } |
||||||
|
break; |
||||||
|
case 6: |
||||||
|
#line 18 "src/guecs/lel_parser.rl" |
||||||
|
{ cur.right = (*p) == '>'; } |
||||||
|
break; |
||||||
|
case 7: |
||||||
|
#line 19 "src/guecs/lel_parser.rl" |
||||||
|
{cur.max_w = std::stoi(tk); } |
||||||
|
break; |
||||||
|
case 8: |
||||||
|
#line 20 "src/guecs/lel_parser.rl" |
||||||
|
{ cur.max_h = std::stoi(tk); } |
||||||
|
break; |
||||||
|
case 9: |
||||||
|
#line 21 "src/guecs/lel_parser.rl" |
||||||
|
{ cur.expand = true; } |
||||||
|
break; |
||||||
|
case 10: |
||||||
|
#line 22 "src/guecs/lel_parser.rl" |
||||||
|
{ cur.center = true; } |
||||||
|
break; |
||||||
|
case 11: |
||||||
|
#line 23 "src/guecs/lel_parser.rl" |
||||||
|
{ cur.percent = true; } |
||||||
|
break; |
||||||
|
case 12: |
||||||
|
#line 33 "src/guecs/lel_parser.rl" |
||||||
|
{ start = p; } |
||||||
|
break; |
||||||
|
case 13: |
||||||
|
#line 36 "src/guecs/lel_parser.rl" |
||||||
|
{start = p;} |
||||||
|
break; |
||||||
|
#line 209 "src/guecs/lel_parser.cpp" |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
_again: |
||||||
|
if ( cs == 0 ) |
||||||
|
goto _out; |
||||||
|
if ( ++p != pe ) |
||||||
|
goto _resume; |
||||||
|
_test_eof: {} |
||||||
|
_out: {} |
||||||
|
} |
||||||
|
|
||||||
|
#line 56 "src/guecs/lel_parser.rl" |
||||||
|
|
||||||
|
bool good = pe - p == 0; |
||||||
|
if(good) { |
||||||
|
finalize(); |
||||||
|
} else { |
||||||
|
std::cout << "error at:"; |
||||||
|
std::cout << p; |
||||||
|
} |
||||||
|
return good; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,67 @@ |
|||||||
|
#include "guecs/lel.hpp" |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
namespace lel { |
||||||
|
|
||||||
|
%%{ |
||||||
|
machine Parser; |
||||||
|
alphtype char; |
||||||
|
|
||||||
|
action token {tk = input.substr(start - begin, fpc - start); } |
||||||
|
|
||||||
|
action col {} |
||||||
|
action ltab { grid.push_back(Row()); } |
||||||
|
action valign { cur.bottom = fc == '.'; } |
||||||
|
action id { id(input.substr(start - begin, fpc - start)); } |
||||||
|
action row { cur.col = 0; cur.row++; } |
||||||
|
action align { cur.right = fc == '>'; } |
||||||
|
action setwidth {cur.max_w = std::stoi(tk); } |
||||||
|
action setheight { cur.max_h = std::stoi(tk); } |
||||||
|
action expand { cur.expand = true; } |
||||||
|
action center { cur.center = true; } |
||||||
|
action percent { cur.percent = true; } |
||||||
|
|
||||||
|
col = "|" $col; |
||||||
|
ltab = "[" $ltab; |
||||||
|
rtab = "]" $row; |
||||||
|
valign = ("^" | ".") $valign; |
||||||
|
expand = "*" $expand; |
||||||
|
center = "=" $center; |
||||||
|
percent = "%" $percent; |
||||||
|
halign = ("<" | ">") $align; |
||||||
|
number = digit+ >{ start = fpc; } %token; |
||||||
|
setw = ("(" number %setwidth ("," space* number %setheight)? ")") ; |
||||||
|
modifiers = (percent | center | expand | valign | halign | setw); |
||||||
|
id = ((alpha | '_')+ :>> (alnum | '_')*) >{start = fpc;} %id; |
||||||
|
cell = modifiers* id; |
||||||
|
row = space* ltab space* cell space* (col space* cell space*)* space* rtab space*; |
||||||
|
|
||||||
|
main := row+; |
||||||
|
}%% |
||||||
|
|
||||||
|
%% write data; |
||||||
|
|
||||||
|
bool Parser::parse(const std::string& input) { |
||||||
|
reset(); |
||||||
|
int cs = 0; |
||||||
|
const char *start = nullptr; |
||||||
|
const char *begin = input.data(); |
||||||
|
const char *p = input.data(); |
||||||
|
const char *pe = p + input.size(); |
||||||
|
std::string tk; |
||||||
|
|
||||||
|
%% write init; |
||||||
|
%% write exec; |
||||||
|
|
||||||
|
bool good = pe - p == 0; |
||||||
|
if(good) { |
||||||
|
finalize(); |
||||||
|
} else { |
||||||
|
std::cout << "error at:"; |
||||||
|
std::cout << p; |
||||||
|
} |
||||||
|
return good; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,59 @@ |
|||||||
|
#include "guecs/sfml/backend.hpp" |
||||||
|
#include "guecs/sfml/shaders.hpp" |
||||||
|
#include "guecs/sfml/sound.hpp" |
||||||
|
#include "guecs/sfml/textures.hpp" |
||||||
|
|
||||||
|
namespace sfml { |
||||||
|
guecs::SpriteTexture Backend::get_sprite(const string& name) { |
||||||
|
auto sp = textures::get(name); |
||||||
|
return {sp.sprite, sp.texture, sp.frame_size}; |
||||||
|
} |
||||||
|
|
||||||
|
guecs::SpriteTexture Backend::get_icon(const string& name) { |
||||||
|
return get_sprite(name); |
||||||
|
} |
||||||
|
|
||||||
|
Backend::Backend() { |
||||||
|
sound::init(); |
||||||
|
shaders::init(); |
||||||
|
textures::init(); |
||||||
|
} |
||||||
|
|
||||||
|
void Backend::sound_play(const string& name) { |
||||||
|
sound::play(name); |
||||||
|
} |
||||||
|
|
||||||
|
void Backend::sound_stop(const string& name) { |
||||||
|
sound::stop(name); |
||||||
|
} |
||||||
|
|
||||||
|
std::shared_ptr<sf::Shader> Backend::get_shader(const std::string& name) { |
||||||
|
return shaders::get(name); |
||||||
|
} |
||||||
|
|
||||||
|
bool Backend::shader_updated() { |
||||||
|
if(shaders::updated($shaders_version)) { |
||||||
|
$shaders_version = shaders::version(); |
||||||
|
return true; |
||||||
|
} else { |
||||||
|
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.DARK_DARK; |
||||||
|
theme.BORDER_COLOR = theme.DARK_LIGHT; |
||||||
|
theme.BG_COLOR_DARK = theme.BLACK; |
||||||
|
theme.FONT_FILE_NAME = "assets/text.otf"; |
||||||
|
|
||||||
|
return theme; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
#pragma once |
||||||
|
#include "guecs/ui.hpp" |
||||||
|
|
||||||
|
namespace sfml { |
||||||
|
using std::string; |
||||||
|
|
||||||
|
class Backend : public guecs::Backend { |
||||||
|
int $shaders_version = 0; |
||||||
|
|
||||||
|
public: |
||||||
|
|
||||||
|
Backend(); |
||||||
|
guecs::SpriteTexture get_sprite(const string& name); |
||||||
|
guecs::SpriteTexture get_icon(const string& name); |
||||||
|
void sound_play(const string& name); |
||||||
|
void sound_stop(const string& name); |
||||||
|
std::shared_ptr<Shader> get_shader(const std::string& name); |
||||||
|
bool shader_updated(); |
||||||
|
guecs::Theme theme(); |
||||||
|
}; |
||||||
|
} |
||||||
@ -0,0 +1,196 @@ |
|||||||
|
#include "guecs/ui.hpp" |
||||||
|
#include "guecs/sfml/backend.hpp" |
||||||
|
#include <cassert> |
||||||
|
#include <iostream> |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <GLFW/glfw3.h> |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
using std::make_shared; |
||||||
|
|
||||||
|
inline SpriteTexture load_texture(const string& name, bool is_icon) { |
||||||
|
return {}; |
||||||
|
} |
||||||
|
|
||||||
|
void Text::init(lel::Cell &cell, const std::string& font_file) { |
||||||
|
// make the text here
|
||||||
|
|
||||||
|
if(centered || cell.center) { |
||||||
|
// get the text bounds and use lel::center
|
||||||
|
// auto bounds = text->getLocalBounds();
|
||||||
|
// auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell);
|
||||||
|
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
|
||||||
|
// text->setPosition({float(text_cell.x), float(text_cell.y) - text_cell.h / 2});
|
||||||
|
} else { |
||||||
|
// CHANGE ME
|
||||||
|
// text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)});
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Text::render() { |
||||||
|
} |
||||||
|
|
||||||
|
void Text::update(const wstring& new_content) { |
||||||
|
content = new_content; |
||||||
|
} |
||||||
|
|
||||||
|
void Sprite::update(const string& new_name) { |
||||||
|
if(new_name != name) { |
||||||
|
name = new_name; |
||||||
|
// CHANGE: get the image and save it
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Sprite::init(lel::Cell &cell) { |
||||||
|
if(cell.center) stretch = false; |
||||||
|
|
||||||
|
auto sprite_texture = load_texture(name, is_icon); |
||||||
|
// setup the image size and such
|
||||||
|
glm::vec2 bounds{0.0, 0.0}; // get this from the image
|
||||||
|
|
||||||
|
if(stretch) { |
||||||
|
// handle stretch
|
||||||
|
} else { |
||||||
|
float box_width = float(cell.w - padding * 2); |
||||||
|
float box_height = float(cell.h - padding * 2); |
||||||
|
float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y)); |
||||||
|
|
||||||
|
// no stretch so scale it
|
||||||
|
} |
||||||
|
|
||||||
|
// used the center helper here
|
||||||
|
} |
||||||
|
|
||||||
|
void Sprite::render() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void Rectangle::init(lel::Cell& cell) { |
||||||
|
glm::vec2 size{float(cell.w) - padding * 2.0f, float(cell.h) - padding * 2.0f}; |
||||||
|
update(); |
||||||
|
// sfml_center_helper(shape, cell, padding);
|
||||||
|
} |
||||||
|
|
||||||
|
void Rectangle::render() { |
||||||
|
} |
||||||
|
|
||||||
|
void Rectangle::update() { |
||||||
|
} |
||||||
|
|
||||||
|
void Meter::init(lel::Cell& cell) { |
||||||
|
$cell_x = cell.x; |
||||||
|
$cell_y = cell.y; |
||||||
|
$cell_w = cell.w; |
||||||
|
$cell_h = cell.h; |
||||||
|
bar.init(cell); |
||||||
|
update(); |
||||||
|
} |
||||||
|
|
||||||
|
void Meter::render() { |
||||||
|
} |
||||||
|
|
||||||
|
void Meter::update() { |
||||||
|
bar.color = color; |
||||||
|
bar.padding = padding; |
||||||
|
bar.update(); |
||||||
|
|
||||||
|
if(vertical) { |
||||||
|
float level = std::clamp(percent, 0.0f, 1.0f) * float($cell_h); |
||||||
|
glm::vec2 size{float($cell_w) - padding * 2.0f, std::max(level, 0.0f) - padding}; |
||||||
|
// set bar size and position
|
||||||
|
} else { |
||||||
|
float level = std::clamp(percent, 0.0f, 1.0f) * float($cell_w); |
||||||
|
// set bar size only
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Sound::play(bool hover) { |
||||||
|
if(!hover) { |
||||||
|
BACKEND->sound_play(on_click); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Sound::stop(bool hover) { |
||||||
|
if(!hover) { |
||||||
|
BACKEND->sound_stop(on_click); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Background::init() { |
||||||
|
assert(w > 0.0f && "Background.w must be > 0.0f. Forgot $gui.$parser?"); |
||||||
|
assert(h > 0.0f && "Background.h must be > 0.0f. Forgot $gui.$parser?"); |
||||||
|
|
||||||
|
glm::vec2 size{float(w), float(h)}; |
||||||
|
|
||||||
|
if(shape == nullptr) { |
||||||
|
// need a shape thing
|
||||||
|
} else { |
||||||
|
// need a shape thing
|
||||||
|
} |
||||||
|
// set the shape psitions and color
|
||||||
|
// shape->pos({float(x), float(y)});
|
||||||
|
// shape->color(color);
|
||||||
|
} |
||||||
|
|
||||||
|
void Background::set_color(Color& c) { |
||||||
|
color = c; |
||||||
|
} |
||||||
|
|
||||||
|
void Background::set_sprite(const std::string& name, bool stretch) { |
||||||
|
auto sprite_texture = BACKEND->get_sprite(name); |
||||||
|
|
||||||
|
// create the sprite and position/scale
|
||||||
|
|
||||||
|
if(stretch) { |
||||||
|
// if it's stretched then fill the cell
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Background::render() { |
||||||
|
} |
||||||
|
|
||||||
|
void Effect::init(lel::Cell &cell) { |
||||||
|
$shader = BACKEND->get_shader(name); |
||||||
|
$shader->setVec2("u_resolution", glm::vec2({float(cell.w), float(cell.h)})); |
||||||
|
// do I need a clock?
|
||||||
|
} |
||||||
|
|
||||||
|
void Effect::render() { |
||||||
|
double u_time = glfwGetTime(); // THIS IS WRONG, IT'S WALL TIME SINCE PROGRAM RUN
|
||||||
|
|
||||||
|
if(u_time < $u_time_end) { |
||||||
|
$shader->setFloat("u_duration", duration); |
||||||
|
$shader->setFloat("u_time_end", $u_time_end); |
||||||
|
$shader->setFloat("u_time", u_time); |
||||||
|
} else { |
||||||
|
$active = false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Effect::run() { |
||||||
|
$active = true; |
||||||
|
// THIS IS WRONG
|
||||||
|
double u_time = glfwGetTime(); |
||||||
|
$u_time_end = u_time + duration; |
||||||
|
} |
||||||
|
|
||||||
|
Shader* Effect::get_shader(bool is_shape) { |
||||||
|
Shader *shader_ptr = nullptr; |
||||||
|
|
||||||
|
if(BACKEND->shader_updated()) { |
||||||
|
$shader = BACKEND->get_shader(name); |
||||||
|
} |
||||||
|
|
||||||
|
if($active) { |
||||||
|
$shader->setBool("is_shape", is_shape); |
||||||
|
// NOTE: this is needed because SFML doesn't handle shared_ptr
|
||||||
|
shader_ptr = $shader.get(); |
||||||
|
} |
||||||
|
|
||||||
|
return shader_ptr; |
||||||
|
} |
||||||
|
|
||||||
|
void Effect::stop() { |
||||||
|
$active = false; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,136 @@ |
|||||||
|
#pragma once |
||||||
|
#include "guecs/lel.hpp" |
||||||
|
#include <string> |
||||||
|
#include <memory> |
||||||
|
#include <functional> |
||||||
|
#include <any> |
||||||
|
#include "guecs/theme.hpp" |
||||||
|
#include <glm/glm.hpp> |
||||||
|
#include "text.hpp" |
||||||
|
#include "shader.hpp" |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
using std::shared_ptr, std::wstring, std::string; |
||||||
|
|
||||||
|
struct Text { |
||||||
|
std::wstring content; |
||||||
|
unsigned int size = THEME.TEXT_SIZE; |
||||||
|
Color color = THEME.TEXT_COLOR; |
||||||
|
int padding = THEME.PADDING; |
||||||
|
bool centered = false; |
||||||
|
shared_ptr<Text> text = nullptr; |
||||||
|
|
||||||
|
void init(lel::Cell &cell, const std::string& font_file); |
||||||
|
void update(const std::wstring& new_content); |
||||||
|
void render(); |
||||||
|
}; |
||||||
|
|
||||||
|
struct Sprite { |
||||||
|
string name; |
||||||
|
int padding = THEME.PADDING; |
||||||
|
bool stretch = true; |
||||||
|
bool is_icon = false; |
||||||
|
std::shared_ptr<Image> image = nullptr; |
||||||
|
|
||||||
|
void init(lel::Cell &cell); |
||||||
|
void update(const string& new_name); |
||||||
|
void render(); |
||||||
|
}; |
||||||
|
|
||||||
|
struct Icon : public Sprite { |
||||||
|
template<typename... Args> |
||||||
|
Icon(Args... args) : Sprite(args...) |
||||||
|
{ |
||||||
|
stretch = false; |
||||||
|
is_icon = true; |
||||||
|
} |
||||||
|
|
||||||
|
Icon() { |
||||||
|
stretch = false; |
||||||
|
is_icon = true; |
||||||
|
}; |
||||||
|
}; |
||||||
|
|
||||||
|
struct Rectangle { |
||||||
|
// BUG: confirm that padding is being used correctly here
|
||||||
|
int padding = THEME.PADDING; |
||||||
|
Color color = THEME.FILL_COLOR; |
||||||
|
Color border_color = THEME.BORDER_COLOR; |
||||||
|
int border_px = THEME.BORDER_PX; |
||||||
|
void* shape = nullptr; // CHANGE ME BRO
|
||||||
|
|
||||||
|
void init(lel::Cell& cell); |
||||||
|
void render(); |
||||||
|
void update(); |
||||||
|
}; |
||||||
|
|
||||||
|
// BUG: meter needs a backing rectangle of one color for the full bar, then
|
||||||
|
// another rectangle for the current setting.
|
||||||
|
// BUG: the meter will be past the end, probably a floating point error
|
||||||
|
struct Meter { |
||||||
|
float percent = 1.0f; |
||||||
|
Color color = THEME.BG_COLOR_DARK; |
||||||
|
int padding = THEME.PADDING; |
||||||
|
bool vertical = false; |
||||||
|
Rectangle bar{padding, color}; |
||||||
|
|
||||||
|
// this is set automatically if a Rectangle is configured for the cell
|
||||||
|
size_t $cell_x = 0; |
||||||
|
size_t $cell_y = 0; |
||||||
|
size_t $cell_w = 0; |
||||||
|
size_t $cell_h = 0; |
||||||
|
|
||||||
|
void init(lel::Cell& cell); |
||||||
|
void render(); |
||||||
|
void update(); |
||||||
|
}; |
||||||
|
|
||||||
|
struct Effect { |
||||||
|
double duration = 0.1; |
||||||
|
string name{"ui_shader"}; |
||||||
|
double $u_time_end = 0.0; |
||||||
|
bool $active = false; |
||||||
|
std::shared_ptr<Shader> $shader = nullptr; |
||||||
|
int $shader_version = 0; |
||||||
|
|
||||||
|
void init(lel::Cell &cell); |
||||||
|
void run(); |
||||||
|
void stop(); |
||||||
|
void render(); |
||||||
|
Shader* get_shader(bool is_shape); |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
struct Sound { |
||||||
|
string on_click{"ui_click"}; |
||||||
|
void play(bool hover); |
||||||
|
void stop(bool hover); |
||||||
|
}; |
||||||
|
|
||||||
|
// BUG: it should be _way_ easier to set the background and change it
|
||||||
|
struct Background { |
||||||
|
float x = 0.0f; |
||||||
|
float y = 0.0f; |
||||||
|
float w = 0.0f; |
||||||
|
float h = 0.0f; |
||||||
|
Color color=THEME.BG_COLOR; |
||||||
|
void* shape = nullptr; // CHANGE ME BRO
|
||||||
|
shared_ptr<Image> sprite = nullptr; |
||||||
|
|
||||||
|
Background(lel::Parser& parser, Color& bg_color=THEME.BG_COLOR) : |
||||||
|
x(parser.grid_x), |
||||||
|
y(parser.grid_y), |
||||||
|
w(parser.grid_w), |
||||||
|
h(parser.grid_h), |
||||||
|
color(bg_color) |
||||||
|
{} |
||||||
|
|
||||||
|
Background() {} |
||||||
|
|
||||||
|
void init(); |
||||||
|
void render(); |
||||||
|
|
||||||
|
void set_color(Color& c); |
||||||
|
void set_sprite(const std::string& name, bool stretch=false); |
||||||
|
}; |
||||||
|
} |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
#include "guecs/sfml/config.hpp" |
||||||
|
#include <fmt/core.h> |
||||||
|
|
||||||
|
using nlohmann::json; |
||||||
|
using fmt::format; |
||||||
|
|
||||||
|
std::filesystem::path Config::BASE_DIR{"."}; |
||||||
|
|
||||||
|
Config::Config(const std::string src_path) : $src_path(src_path) { |
||||||
|
std::ifstream infile(Config::path_to($src_path)); |
||||||
|
$config = json::parse(infile); |
||||||
|
} |
||||||
|
|
||||||
|
json &Config::operator[](const std::string &key) { |
||||||
|
assert($config.contains(key) && |
||||||
|
"ERROR in config, key doesn't exist."); |
||||||
|
return $config[key]; |
||||||
|
} |
||||||
|
|
||||||
|
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) { |
||||||
|
assert($config.contains(main_key) && "ERROR wstring main/key in config, main_key doesn't exist."); |
||||||
|
assert($config[main_key].contains(sub_key) && "ERROR wstring in config, main_key/key doesn't exist."); |
||||||
|
|
||||||
|
const std::string& str_val = $config[main_key][sub_key]; |
||||||
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter; |
||||||
|
return $converter.from_bytes(str_val); |
||||||
|
} |
||||||
|
|
||||||
|
std::vector<std::string> Config::keys() { |
||||||
|
std::vector<std::string> the_fucking_keys; |
||||||
|
|
||||||
|
for(auto& [key, value] : $config.items()) { |
||||||
|
the_fucking_keys.push_back(key); |
||||||
|
} |
||||||
|
|
||||||
|
return the_fucking_keys; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
void Config::set_base_dir(const char *optarg) { |
||||||
|
Config::BASE_DIR.assign(optarg); |
||||||
|
} |
||||||
|
|
||||||
|
std::filesystem::path Config::path_to(const std::string& path) { |
||||||
|
return Config::BASE_DIR / path; |
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
#pragma once |
||||||
|
#include <nlohmann/json.hpp> |
||||||
|
#include <fstream> |
||||||
|
#include <codecvt> |
||||||
|
#include <filesystem> |
||||||
|
|
||||||
|
struct Config { |
||||||
|
static std::filesystem::path BASE_DIR; |
||||||
|
nlohmann::json $config; |
||||||
|
std::string $src_path; |
||||||
|
|
||||||
|
Config(const std::string src_path); |
||||||
|
|
||||||
|
Config(nlohmann::json config, std::string src_path) |
||||||
|
: $config(config), $src_path(src_path) {} |
||||||
|
|
||||||
|
nlohmann::json &operator[](const std::string &key); |
||||||
|
nlohmann::json &json() { return $config; }; |
||||||
|
std::wstring wstring(const std::string main_key, const std::string sub_key); |
||||||
|
std::vector<std::string> keys(); |
||||||
|
|
||||||
|
static void set_base_dir(const char *optarg); |
||||||
|
static std::filesystem::path path_to(const std::string& path); |
||||||
|
}; |
||||||
@ -0,0 +1,77 @@ |
|||||||
|
#include "guecs/sfml/shaders.hpp" |
||||||
|
#include "guecs/sfml/config.hpp" |
||||||
|
#include <SFML/Graphics/Image.hpp> |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <memory> |
||||||
|
|
||||||
|
namespace shaders { |
||||||
|
using std::shared_ptr, std::make_shared; |
||||||
|
|
||||||
|
static ShaderManager SMGR; |
||||||
|
static bool INITIALIZED = false; |
||||||
|
static int VERSION = 0; |
||||||
|
|
||||||
|
inline void configure_shader_defaults(std::shared_ptr<sf::Shader> ptr) { |
||||||
|
ptr->setUniform("source", sf::Shader::CurrentTexture); |
||||||
|
} |
||||||
|
|
||||||
|
bool load_shader(std::string name, nlohmann::json& settings) { |
||||||
|
auto file_name = settings["file_name"]; |
||||||
|
auto file_path = Config::path_to(file_name); |
||||||
|
auto ptr = std::make_shared<sf::Shader>(); |
||||||
|
bool good = ptr->loadFromFile(file_path, sf::Shader::Type::Fragment); |
||||||
|
|
||||||
|
if(good) { |
||||||
|
configure_shader_defaults(ptr); |
||||||
|
SMGR.shaders.try_emplace(name, name, file_name, ptr); |
||||||
|
} |
||||||
|
return good; |
||||||
|
} |
||||||
|
|
||||||
|
void init() { |
||||||
|
if(!INITIALIZED) { |
||||||
|
assert(sf::Shader::isAvailable() && "no shaders?!"); |
||||||
|
INITIALIZED = true; |
||||||
|
Config config("assets/shaders.json"); |
||||||
|
bool good = load_shader("ERROR", config["ERROR"]); |
||||||
|
assert(good && "Failed to load ERROR shader. Look in assets/shaders.json"); |
||||||
|
|
||||||
|
for(auto& [name, settings] : config.json().items()) { |
||||||
|
if(name == "ERROR") continue; |
||||||
|
|
||||||
|
assert(!SMGR.shaders.contains(name) && |
||||||
|
"shader name duplicated in assets/shaders.json"); |
||||||
|
good = load_shader(name, settings); |
||||||
|
|
||||||
|
if(!good) { |
||||||
|
fmt::println("[SOUND] failed to load shader {}", name); |
||||||
|
SMGR.shaders.insert_or_assign(name, SMGR.shaders.at("ERROR")); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
std::shared_ptr<sf::Shader> get(const std::string& name) { |
||||||
|
assert(INITIALIZED && "you forgot to shaders::init()"); |
||||||
|
assert(SMGR.shaders.contains(name) && |
||||||
|
"shader name not in assets/shaders.json"); |
||||||
|
auto& rec = SMGR.shaders.at(name); |
||||||
|
return rec.ptr; |
||||||
|
} |
||||||
|
|
||||||
|
int reload() { |
||||||
|
VERSION++; |
||||||
|
INITIALIZED = false; |
||||||
|
SMGR.shaders.clear(); |
||||||
|
init(); |
||||||
|
return VERSION; |
||||||
|
} |
||||||
|
|
||||||
|
bool updated(int my_version) { |
||||||
|
return my_version != VERSION; |
||||||
|
} |
||||||
|
|
||||||
|
int version() { |
||||||
|
return VERSION; |
||||||
|
} |
||||||
|
}; |
||||||
@ -0,0 +1,27 @@ |
|||||||
|
#pragma once |
||||||
|
#include <cstdint> |
||||||
|
#include <vector> |
||||||
|
#include <string> |
||||||
|
#include <SFML/Graphics.hpp> |
||||||
|
#include <unordered_map> |
||||||
|
#include <memory> |
||||||
|
#include <nlohmann/json.hpp> |
||||||
|
|
||||||
|
namespace shaders { |
||||||
|
struct Record { |
||||||
|
std::string name; |
||||||
|
std::string file_name; |
||||||
|
std::shared_ptr<sf::Shader> ptr = nullptr; |
||||||
|
}; |
||||||
|
|
||||||
|
struct ShaderManager { |
||||||
|
std::unordered_map<std::string, Record> shaders; |
||||||
|
}; |
||||||
|
|
||||||
|
std::shared_ptr<sf::Shader> get(const std::string& name); |
||||||
|
void init(); |
||||||
|
bool load_shader(std::string& name, nlohmann::json& settings); |
||||||
|
bool updated(int my_version); |
||||||
|
int reload(); |
||||||
|
int version(); |
||||||
|
} |
||||||
@ -0,0 +1,81 @@ |
|||||||
|
#include "guecs/sfml/sound.hpp" |
||||||
|
#include "guecs/sfml/config.hpp" |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <cassert> |
||||||
|
|
||||||
|
namespace sound { |
||||||
|
static SoundManager SMGR; |
||||||
|
static bool initialized = false; |
||||||
|
static bool muted = false; |
||||||
|
|
||||||
|
using namespace fmt; |
||||||
|
using std::make_shared; |
||||||
|
namespace fs = std::filesystem; |
||||||
|
|
||||||
|
SoundPair& get_sound_pair(const std::string& name) { |
||||||
|
assert(initialized && "You need to call sound::init() first"); |
||||||
|
|
||||||
|
if(SMGR.sounds.contains(name)) { |
||||||
|
// get the sound from the sound map
|
||||||
|
return SMGR.sounds.at(name); |
||||||
|
} else { |
||||||
|
fmt::println("[SOUND] Attempted to stop {} sound but not available.", name); |
||||||
|
return SMGR.sounds.at("blank"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
void init() { |
||||||
|
if(!initialized) { |
||||||
|
Config assets("assets/config.json"); |
||||||
|
|
||||||
|
for(auto& el : assets["sounds"].items()) { |
||||||
|
load(el.key(), Config::path_to(el.value())); |
||||||
|
} |
||||||
|
initialized = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void load(const std::string& name, const std::filesystem::path& sound_path) { |
||||||
|
assert(fs::exists(sound_path) && "sound file does not exist"); |
||||||
|
|
||||||
|
// create the buffer and keep in the buffer map
|
||||||
|
auto buffer = make_shared<sf::SoundBuffer>(sound_path); |
||||||
|
|
||||||
|
// set it on the sound and keep in the sound map
|
||||||
|
auto sound = make_shared<sf::Sound>(*buffer); |
||||||
|
sound->setRelativeToListener(false); |
||||||
|
sound->setPosition({0.0f, 0.0f, 1.0f}); |
||||||
|
|
||||||
|
SMGR.sounds.try_emplace(name, buffer, sound); |
||||||
|
} |
||||||
|
|
||||||
|
void play(const std::string& name, bool loop) { |
||||||
|
if(muted) return; |
||||||
|
auto& pair = get_sound_pair(name); |
||||||
|
pair.sound->setLooping(loop); |
||||||
|
// play it
|
||||||
|
pair.sound->play(); |
||||||
|
} |
||||||
|
|
||||||
|
void stop(const std::string& name) { |
||||||
|
auto& pair = get_sound_pair(name); |
||||||
|
pair.sound->stop(); |
||||||
|
} |
||||||
|
|
||||||
|
bool playing(const std::string& name) { |
||||||
|
auto& pair = get_sound_pair(name); |
||||||
|
auto status = pair.sound->getStatus(); |
||||||
|
return status == sf::SoundSource::Status::Playing; |
||||||
|
} |
||||||
|
|
||||||
|
void play_at(const std::string& name, float x, float y, float z) { |
||||||
|
auto& pair = get_sound_pair(name); |
||||||
|
pair.sound->setPosition({x, y, z}); |
||||||
|
pair.sound->play(); |
||||||
|
} |
||||||
|
|
||||||
|
void mute(bool setting) { |
||||||
|
muted = setting; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
#pragma once |
||||||
|
#include <string> |
||||||
|
#include <filesystem> |
||||||
|
#include <memory> |
||||||
|
#include <unordered_map> |
||||||
|
#include <SFML/Audio.hpp> |
||||||
|
|
||||||
|
namespace sound { |
||||||
|
struct SoundPair { |
||||||
|
std::shared_ptr<sf::SoundBuffer> buffer; |
||||||
|
std::shared_ptr<sf::Sound> sound; |
||||||
|
}; |
||||||
|
|
||||||
|
struct SoundManager { |
||||||
|
std::unordered_map<std::string, SoundPair> sounds; |
||||||
|
}; |
||||||
|
|
||||||
|
void init(); |
||||||
|
void load(const std::string& name, const std::filesystem::path& path); |
||||||
|
void play(const std::string& name, bool loop=false); |
||||||
|
void play_at(const std::string& name, float x, float y, float z); |
||||||
|
void stop(const std::string& name); |
||||||
|
void mute(bool setting); |
||||||
|
bool playing(const std::string& name); |
||||||
|
SoundPair& get_sound_pair(const std::string& name); |
||||||
|
} |
||||||
@ -0,0 +1,62 @@ |
|||||||
|
#include "guecs/sfml/textures.hpp" |
||||||
|
#include "guecs/sfml/config.hpp" |
||||||
|
#include <SFML/Graphics/Image.hpp> |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <memory> |
||||||
|
|
||||||
|
namespace textures { |
||||||
|
using std::shared_ptr, std::make_shared; |
||||||
|
|
||||||
|
static TextureManager TMGR; |
||||||
|
static bool initialized = false; |
||||||
|
|
||||||
|
void load_sprites() { |
||||||
|
Config assets("assets/config.json"); |
||||||
|
|
||||||
|
for(auto& [name, settings] : assets["sprites"].items()) { |
||||||
|
auto file_name = settings["path"]; |
||||||
|
auto file_path = Config::path_to(file_name); |
||||||
|
auto texture = make_shared<sf::Texture>(file_path); |
||||||
|
|
||||||
|
texture->setSmooth(assets["graphics"]["smooth_textures"]); |
||||||
|
auto sprite = make_shared<sf::Sprite>(*texture); |
||||||
|
|
||||||
|
int width = settings["frame_width"]; |
||||||
|
int height = settings["frame_height"]; |
||||||
|
|
||||||
|
sf::Vector2i frame_size{width, height}; |
||||||
|
sprite->setTextureRect({{0,0}, frame_size}); |
||||||
|
TMGR.sprite_textures.try_emplace(name, sprite, texture, frame_size); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void init() { |
||||||
|
if(!initialized) { |
||||||
|
load_sprites(); |
||||||
|
initialized = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
SpriteTexture get(const std::string& name) { |
||||||
|
assert(initialized && "you forgot to call textures::init()"); |
||||||
|
assert(TMGR.sprite_textures.contains(name) && |
||||||
|
"!!!!! texture pack does not contain sprite"); |
||||||
|
|
||||||
|
auto result = TMGR.sprite_textures.at(name); |
||||||
|
|
||||||
|
assert(result.sprite != nullptr && |
||||||
|
"bad sprite from textures::get named can't be null"); |
||||||
|
assert(result.texture != nullptr && |
||||||
|
"bad texture from textures::get named can't be null"); |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
sf::Image load_image(const std::string& filename) { |
||||||
|
sf::Image texture; |
||||||
|
auto file_path = Config::path_to(filename); |
||||||
|
bool good = texture.loadFromFile(file_path); |
||||||
|
assert(good && "failed to load image file"); |
||||||
|
return texture; |
||||||
|
} |
||||||
|
}; |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
#pragma once |
||||||
|
#include <cstdint> |
||||||
|
#include <vector> |
||||||
|
#include <string> |
||||||
|
#include <SFML/Graphics.hpp> |
||||||
|
#include <unordered_map> |
||||||
|
#include <memory> |
||||||
|
|
||||||
|
namespace textures { |
||||||
|
|
||||||
|
struct SpriteTexture { |
||||||
|
std::shared_ptr<sf::Sprite> sprite = nullptr; |
||||||
|
std::shared_ptr<sf::Texture> texture = nullptr; |
||||||
|
sf::Vector2i frame_size; |
||||||
|
}; |
||||||
|
|
||||||
|
struct TextureManager { |
||||||
|
std::vector<sf::Image> surfaces; |
||||||
|
std::unordered_map<std::string, SpriteTexture> sprite_textures; |
||||||
|
std::unordered_map<wchar_t, int> char_to_texture; |
||||||
|
}; |
||||||
|
|
||||||
|
void init(); |
||||||
|
|
||||||
|
SpriteTexture get(const std::string& name); |
||||||
|
|
||||||
|
sf::Image load_image(const std::string& filename); |
||||||
|
} |
||||||
@ -0,0 +1,11 @@ |
|||||||
|
#include "guecs/theme.hpp" |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
Backend* BACKEND = nullptr; |
||||||
|
Theme THEME; |
||||||
|
|
||||||
|
void init(Backend* backend) { |
||||||
|
BACKEND = backend; |
||||||
|
THEME = BACKEND->theme(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,57 @@ |
|||||||
|
#pragma once |
||||||
|
#include <memory> |
||||||
|
#include <string> |
||||||
|
#include <glm/glm.hpp> |
||||||
|
#include "document.hpp" |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
using std::string; |
||||||
|
|
||||||
|
struct Theme { |
||||||
|
Color BLACK{0, 0, 0}; |
||||||
|
Color DARK_DARK{10, 10, 10}; |
||||||
|
Color DARK_MID{30, 30, 30}; |
||||||
|
Color DARK_LIGHT{60, 60, 60}; |
||||||
|
Color MID{100, 100, 100}; |
||||||
|
Color LIGHT_DARK{150, 150, 150}; |
||||||
|
Color LIGHT_MID{200, 200, 200}; |
||||||
|
Color LIGHT_LIGHT{230, 230, 230}; |
||||||
|
Color WHITE{255, 255, 255}; |
||||||
|
Color TRANSPARENT = {0, 0, 0}; |
||||||
|
|
||||||
|
int PADDING = 3; |
||||||
|
int BORDER_PX = 1; |
||||||
|
unsigned int TEXT_SIZE = 30; |
||||||
|
int LABEL_SIZE = 20; |
||||||
|
Color FILL_COLOR{255,0,0,255}; |
||||||
|
Color TEXT_COLOR{255,0,0,255}; |
||||||
|
Color BG_COLOR{255,0,0,255}; |
||||||
|
Color BORDER_COLOR{255,0,0,255}; |
||||||
|
Color BG_COLOR_DARK{0,0,0,255}; |
||||||
|
std::string FONT_FILE_NAME{"you_forgot_to_init.otf"}; |
||||||
|
}; |
||||||
|
|
||||||
|
struct SpriteTexture { |
||||||
|
}; |
||||||
|
|
||||||
|
class Backend { |
||||||
|
public: |
||||||
|
virtual SpriteTexture get_sprite(const string& name) = 0; |
||||||
|
|
||||||
|
virtual SpriteTexture get_icon(const string& name) = 0; |
||||||
|
|
||||||
|
virtual void sound_play(const string& name) = 0; |
||||||
|
|
||||||
|
virtual void sound_stop(const string& name) = 0; |
||||||
|
|
||||||
|
virtual std::shared_ptr<Shader> get_shader(const std::string& name) = 0; |
||||||
|
|
||||||
|
virtual bool shader_updated() = 0; |
||||||
|
virtual Theme theme() = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
extern Backend* BACKEND; |
||||||
|
extern Theme THEME; |
||||||
|
|
||||||
|
void init(Backend* backend); |
||||||
|
} |
||||||
@ -0,0 +1,243 @@ |
|||||||
|
#include "guecs/ui.hpp" |
||||||
|
#include <typeinfo> |
||||||
|
#include <cassert> |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <iostream> |
||||||
|
#include <utf8.h> |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
using std::make_shared; |
||||||
|
|
||||||
|
UI::UI() { |
||||||
|
} |
||||||
|
|
||||||
|
void UI::position(int x, int y, int width, int height) { |
||||||
|
$parser.position(x, y, width, height); |
||||||
|
} |
||||||
|
|
||||||
|
glm::vec2 UI::get_position() { |
||||||
|
return {float($parser.grid_x), float($parser.grid_y)}; |
||||||
|
} |
||||||
|
|
||||||
|
glm::vec2 UI::get_size() { |
||||||
|
return {float($parser.grid_w), float($parser.grid_h)}; |
||||||
|
} |
||||||
|
|
||||||
|
void UI::layout(const string& grid) { |
||||||
|
$grid = grid; |
||||||
|
bool good = $parser.parse($grid); |
||||||
|
assert(good && "LEL parsing failed."); |
||||||
|
|
||||||
|
for(auto& [name, cell] : $parser.cells) { |
||||||
|
auto ent = init_entity(name); |
||||||
|
set<lel::Cell>(ent, cell); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Entity UI::init_entity(const string& name) { |
||||||
|
auto ent = entity(); |
||||||
|
// this lets you look up an entity by name
|
||||||
|
$name_ents.insert_or_assign(name, ent); |
||||||
|
// this lets you get a name by entity
|
||||||
|
$ents_name.insert_or_assign(ent, name); |
||||||
|
// this makes it easier to get the name during querying
|
||||||
|
set<CellName>(ent, {name}); |
||||||
|
return ent; |
||||||
|
} |
||||||
|
|
||||||
|
const std::string& UI::name_for(Entity gui_id) { |
||||||
|
assert($ents_name.contains(gui_id) && |
||||||
|
"Attempt to get name_for an Entity but that's not a valid ID."); |
||||||
|
return $ents_name.at(gui_id); |
||||||
|
} |
||||||
|
|
||||||
|
bool UI::contains(const string& name) { |
||||||
|
return $name_ents.contains(name); |
||||||
|
} |
||||||
|
|
||||||
|
Entity UI::entity(const string& name) { |
||||||
|
assert($name_ents.contains(name) && |
||||||
|
"GUECS entity does not exist. Mispelled cell name?"); |
||||||
|
return $name_ents.at(name); |
||||||
|
} |
||||||
|
|
||||||
|
Entity UI::entity(const string& name, int id) { |
||||||
|
return entity(fmt::format("{}{}", name, id)); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::init() { |
||||||
|
query<Background>([](auto, auto& bg) { |
||||||
|
bg.init(); |
||||||
|
}); |
||||||
|
|
||||||
|
query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) { |
||||||
|
rect.init(cell); |
||||||
|
}); |
||||||
|
|
||||||
|
query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) { |
||||||
|
shader.init(cell); |
||||||
|
}); |
||||||
|
|
||||||
|
query<lel::Cell, Meter>([this](auto ent, auto &cell, auto& meter) { |
||||||
|
meter.init(cell); |
||||||
|
}); |
||||||
|
|
||||||
|
query<lel::Cell, Text>([this](auto, auto& cell, auto& text) { |
||||||
|
// CHANGE ME font needs to be loaded
|
||||||
|
text.init(cell, $font); |
||||||
|
}); |
||||||
|
|
||||||
|
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) { |
||||||
|
sprite.init(cell); |
||||||
|
}); |
||||||
|
|
||||||
|
query<lel::Cell, Icon>([&](auto, auto &cell, auto &icon) { |
||||||
|
icon.init(cell); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::debug_layout() { |
||||||
|
query<lel::Cell>([&](const auto, auto &cell) { |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
Shader* UI::find_shader(Entity ent, bool is_shape) { |
||||||
|
if(auto shader = get_if<Effect>(ent)) { |
||||||
|
return shader->get_shader(is_shape); |
||||||
|
} else { |
||||||
|
return nullptr; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::update() { |
||||||
|
query<Rectangle>([&](auto ent, auto& rect) { |
||||||
|
rect.update(); |
||||||
|
}); |
||||||
|
|
||||||
|
query<Meter>([&](auto ent, auto &meter) { |
||||||
|
meter.update(); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
// BUG: either render detects that the things are initialized or there's
|
||||||
|
// another validator function I can call in debug modes to confirm they are
|
||||||
|
void UI::render() { |
||||||
|
if(auto bg = get_if<Background>(MAIN)) { |
||||||
|
bg->render(); |
||||||
|
} |
||||||
|
|
||||||
|
query<Effect>([&](auto, auto& shader) { |
||||||
|
if(shader.$active) shader.render(); |
||||||
|
}); |
||||||
|
|
||||||
|
query<Rectangle>([&](auto ent, auto& rect) { |
||||||
|
auto shader_ptr = find_shader(ent, true); |
||||||
|
rect.render(); |
||||||
|
}); |
||||||
|
|
||||||
|
query<Meter>([&](auto ent, auto &meter) { |
||||||
|
auto shader_ptr = find_shader(ent, true); |
||||||
|
meter.render(); |
||||||
|
}); |
||||||
|
|
||||||
|
query<Sprite>([&](auto ent, auto& sprite) { |
||||||
|
auto shader_ptr = find_shader(ent, false); |
||||||
|
sprite.render(); |
||||||
|
}); |
||||||
|
|
||||||
|
query<Icon>([&](auto ent, auto& icon) { |
||||||
|
auto shader_ptr = find_shader(ent, false); |
||||||
|
icon.render(); |
||||||
|
}); |
||||||
|
|
||||||
|
query<Text>([&](auto ent, auto& text) { |
||||||
|
auto shader_ptr = find_shader(ent, false); |
||||||
|
text.render(); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
bool UI::mouse(float x, float y, Modifiers mods) { |
||||||
|
int action_count = 0; |
||||||
|
bool hover = mods.test(size_t(ModBit::hover)); |
||||||
|
|
||||||
|
query<lel::Cell>([&](auto ent, auto& cell) { |
||||||
|
if((x >= cell.x && x <= cell.x + cell.w) && |
||||||
|
(y >= cell.y && y <= cell.y + cell.h)) |
||||||
|
{ |
||||||
|
do_if<Effect>(ent, [hover](auto& effect) { |
||||||
|
effect.$shader->setBool("hover", hover); |
||||||
|
effect.run(); |
||||||
|
}); |
||||||
|
|
||||||
|
do_if<Sound>(ent, [hover](auto& sound) { |
||||||
|
// here set that it played then only play once
|
||||||
|
sound.play(hover); |
||||||
|
}); |
||||||
|
|
||||||
|
if(hover) return; |
||||||
|
|
||||||
|
click_on(ent, mods); |
||||||
|
|
||||||
|
action_count++; |
||||||
|
} else { |
||||||
|
do_if<Effect>(ent, [](auto& effect) { |
||||||
|
effect.stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
do_if<Sound>(ent, [hover](auto& sound) { |
||||||
|
// here set that it played then only play once
|
||||||
|
sound.stop(hover); |
||||||
|
}); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
return action_count > 0; |
||||||
|
} |
||||||
|
|
||||||
|
void UI::show_sprite(const string& region, const string& sprite_name) { |
||||||
|
auto ent = entity(region); |
||||||
|
|
||||||
|
if(auto sprite = get_if<Sprite>(ent)) { |
||||||
|
sprite->update(sprite_name); |
||||||
|
} else { |
||||||
|
set_init<Sprite>(ent, {sprite_name}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::show_icon(const string& region, const string& sprite_name) { |
||||||
|
auto ent = entity(region); |
||||||
|
|
||||||
|
if(auto sprite = get_if<Icon>(ent)) { |
||||||
|
sprite->update(sprite_name); |
||||||
|
} else { |
||||||
|
set_init<Icon>(ent, {sprite_name}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::show_text(const string& region, const wstring& content) { |
||||||
|
auto ent = entity(region); |
||||||
|
// REWRITE ME
|
||||||
|
} |
||||||
|
|
||||||
|
void UI::click_on(const std::string& name, Modifiers mods) { |
||||||
|
click_on(entity(name), mods); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::click_on(Entity gui_id, Modifiers mods) { |
||||||
|
if(auto to_click = get_if<Clickable>(gui_id)) { |
||||||
|
to_click->action(mods); |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
wstring to_wstring(const string& str) { |
||||||
|
std::wstring result = L""; |
||||||
|
|
||||||
|
try { |
||||||
|
utf8::utf8to32(str.begin(), str.end(), std::back_inserter(result)); |
||||||
|
} catch(const utf8::exception& e) { |
||||||
|
std::cerr << e.what() << std::endl; |
||||||
|
} |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,238 @@ |
|||||||
|
#pragma once |
||||||
|
#include "guecs/lel.hpp" |
||||||
|
#include <string> |
||||||
|
#include <memory> |
||||||
|
#include <functional> |
||||||
|
#include <any> |
||||||
|
#include <queue> |
||||||
|
#include <typeindex> |
||||||
|
#include <unordered_map> |
||||||
|
#include "guecs/theme.hpp" |
||||||
|
#include "guecs/sfml/components.hpp" |
||||||
|
#include <cassert> |
||||||
|
#include <bitset> |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
using std::shared_ptr, std::wstring, std::string; |
||||||
|
|
||||||
|
using Entity = unsigned long; |
||||||
|
|
||||||
|
using EntityMap = std::unordered_map<Entity, size_t>; |
||||||
|
|
||||||
|
template <typename T> |
||||||
|
struct ComponentStorage { |
||||||
|
std::vector<T> data; |
||||||
|
std::queue<size_t> free_indices; |
||||||
|
}; |
||||||
|
|
||||||
|
namespace ModBit { |
||||||
|
enum { |
||||||
|
NONE=0, |
||||||
|
hover=1, |
||||||
|
left=2, |
||||||
|
right=3 |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
using Modifiers = std::bitset<16>; |
||||||
|
|
||||||
|
constexpr const Modifiers NO_MODS{0}; |
||||||
|
|
||||||
|
struct Clickable { |
||||||
|
/* This is actually called by UI::mouse and passed the entity ID of the
|
||||||
|
* button pressed so you can interact with it in the event handler. |
||||||
|
*/ |
||||||
|
std::function<void(Modifiers mods)> action; |
||||||
|
}; |
||||||
|
|
||||||
|
struct CellName { |
||||||
|
string name; |
||||||
|
}; |
||||||
|
|
||||||
|
class UI { |
||||||
|
public: |
||||||
|
Entity MAIN = 0; |
||||||
|
unsigned long entity_count = 1; |
||||||
|
std::unordered_map<std::type_index, EntityMap> $components; |
||||||
|
std::unordered_map<std::type_index, std::any> $component_storages; |
||||||
|
std::unordered_map<string, Entity> $name_ents; |
||||||
|
std::unordered_map<Entity, string> $ents_name; |
||||||
|
// change this to use the new text?
|
||||||
|
std::string $font = THEME.FONT_FILE_NAME; |
||||||
|
lel::Parser $parser; |
||||||
|
string $grid = ""; |
||||||
|
// BUG: check for this to prevent bugs, or find a way to shape the API so you
|
||||||
|
// simply can't get it wrong
|
||||||
|
bool initialized = false; |
||||||
|
|
||||||
|
UI(); |
||||||
|
|
||||||
|
void position(int x, int y, int width, int height); |
||||||
|
glm::vec2 get_position(); |
||||||
|
glm::vec2 get_size(); |
||||||
|
void layout(const string& grid); |
||||||
|
Entity init_entity(const string& name); |
||||||
|
bool contains(const string& name); |
||||||
|
Entity entity(const string& name); |
||||||
|
Entity entity(const string& name, int id); |
||||||
|
const std::string& name_for(Entity gui_id); |
||||||
|
|
||||||
|
inline lel::CellMap& cells() { |
||||||
|
return $parser.cells; |
||||||
|
} |
||||||
|
|
||||||
|
void init(); |
||||||
|
void render(); |
||||||
|
void update(); |
||||||
|
bool mouse(float x, float y, Modifiers mods); |
||||||
|
void click_on(const std::string& name, Modifiers mods=NO_MODS); |
||||||
|
void click_on(Entity slot_id, Modifiers mods=NO_MODS); |
||||||
|
void debug_layout(); |
||||||
|
|
||||||
|
Entity entity() { return ++entity_count; } |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
size_t make_component() { |
||||||
|
auto &storage = component_storage_for<Comp>(); |
||||||
|
size_t index; |
||||||
|
|
||||||
|
if(!storage.free_indices.empty()) { |
||||||
|
index = storage.free_indices.front(); |
||||||
|
storage.free_indices.pop(); |
||||||
|
} else { |
||||||
|
storage.data.emplace_back(); |
||||||
|
index = storage.data.size() - 1; |
||||||
|
} |
||||||
|
|
||||||
|
return index; |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
ComponentStorage<Comp> &component_storage_for() { |
||||||
|
auto type_index = std::type_index(typeid(Comp)); |
||||||
|
$component_storages.try_emplace(type_index, ComponentStorage<Comp>{}); |
||||||
|
return std::any_cast<ComponentStorage<Comp> &>( |
||||||
|
$component_storages.at(type_index)); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
EntityMap &entity_map_for() { |
||||||
|
return $components[std::type_index(typeid(Comp))]; |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
void set(Entity ent, Comp val) { |
||||||
|
EntityMap &map = entity_map_for<Comp>(); |
||||||
|
|
||||||
|
if(has<Comp>(ent)) { |
||||||
|
get<Comp>(ent) = val; |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
map.insert_or_assign(ent, make_component<Comp>()); |
||||||
|
get<Comp>(ent) = val; |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
Comp& get(Entity ent) { |
||||||
|
EntityMap &map = entity_map_for<Comp>(); |
||||||
|
auto &storage = component_storage_for<Comp>(); |
||||||
|
auto index = map.at(ent); |
||||||
|
return storage.data[index]; |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
Comp* get_if(Entity entity) { |
||||||
|
EntityMap &map = entity_map_for<Comp>(); |
||||||
|
auto &storage = component_storage_for<Comp>(); |
||||||
|
if(map.contains(entity)) { |
||||||
|
auto index = map.at(entity); |
||||||
|
return &storage.data[index]; |
||||||
|
} else { |
||||||
|
return nullptr; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
bool has(Entity ent) { |
||||||
|
EntityMap &map = entity_map_for<Comp>(); |
||||||
|
return map.contains(ent); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
void remove(Entity ent) { |
||||||
|
EntityMap &map = entity_map_for<Comp>(); |
||||||
|
|
||||||
|
if(map.contains(ent)) { |
||||||
|
size_t index = map.at(ent); |
||||||
|
component_storage_for<Comp>().free_indices.push(index); |
||||||
|
} |
||||||
|
|
||||||
|
map.erase(ent); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
void query(std::function<void(Entity, Comp &)> cb) { |
||||||
|
EntityMap &map = entity_map_for<Comp>(); |
||||||
|
|
||||||
|
for(auto &[entity, index] : map) { |
||||||
|
cb(entity, get<Comp>(entity)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
template <typename CompA, typename CompB> |
||||||
|
void query(std::function<void(Entity, CompA &, CompB &)> cb) { |
||||||
|
EntityMap &map_a = entity_map_for<CompA>(); |
||||||
|
EntityMap &map_b = entity_map_for<CompB>(); |
||||||
|
|
||||||
|
for(auto &[entity, index_a] : map_a) { |
||||||
|
if(map_b.contains(entity)) { |
||||||
|
cb(entity, get<CompA>(entity), get<CompB>(entity)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
void set_init(Entity ent, Comp val) { |
||||||
|
assert(has<lel::Cell>(ent) && "WRONG! slot is missing its cell?!"); |
||||||
|
auto& cell = get<lel::Cell>(ent); |
||||||
|
val.init(cell); |
||||||
|
set<Comp>(ent, val); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Comp> |
||||||
|
void do_if(Entity ent, std::function<void(Comp &)> cb) { |
||||||
|
if(has<Comp>(ent)) { |
||||||
|
cb(get<Comp>(ent)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
lel::Cell& cell_for(Entity ent) { |
||||||
|
return get<lel::Cell>(ent); |
||||||
|
} |
||||||
|
|
||||||
|
lel::Cell& cell_for(const string& name) { |
||||||
|
Entity ent = entity(name); |
||||||
|
return get<lel::Cell>(ent); |
||||||
|
} |
||||||
|
|
||||||
|
// BUG: close could just be remove with overload
|
||||||
|
template <typename Comp> |
||||||
|
void close(string region) { |
||||||
|
auto ent = entity(region); |
||||||
|
if(has<Comp>(ent)) { |
||||||
|
remove<Comp>(ent); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Shader* find_shader(Entity ent, bool is_shape); |
||||||
|
|
||||||
|
void show_sprite(const string& region, const string& sprite_name); |
||||||
|
void show_icon(const string& region, const string& sprite_name); |
||||||
|
void show_text(const string& region, const wstring& content); |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
wstring to_wstring(const string& str); |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,97 @@ |
|||||||
|
#include <guecs/ui.hpp> |
||||||
|
#include <flat_map> |
||||||
|
#include <cassert> |
||||||
|
|
||||||
|
namespace gui { |
||||||
|
template<typename SCREEN_TYPE> |
||||||
|
struct UIStack { |
||||||
|
using UIStackMap = std::flat_map<std::string, std::shared_ptr<SCREEN_TYPE>>; |
||||||
|
|
||||||
|
UIStackMap screens{}; |
||||||
|
bool visible = false; |
||||||
|
|
||||||
|
UIStackMap::iterator $current{screens.begin()}; |
||||||
|
SCREEN_TYPE* $active = nullptr; |
||||||
|
|
||||||
|
void add(const std::string& name, std::shared_ptr<SCREEN_TYPE> screen) { |
||||||
|
screens.insert_or_assign(name, screen); |
||||||
|
update_active(screens.begin()); |
||||||
|
} |
||||||
|
|
||||||
|
void update_active(const UIStackMap::iterator& itr) { |
||||||
|
$current = itr; |
||||||
|
$active = (*$current).second.get(); |
||||||
|
} |
||||||
|
|
||||||
|
void set_active(const std::string& name) { |
||||||
|
assert(screens.contains(name) && "no screen with that name"); |
||||||
|
update_active(screens.find(name)); |
||||||
|
} |
||||||
|
|
||||||
|
void set_visible(bool new_value) { |
||||||
|
visible = new_value; |
||||||
|
} |
||||||
|
|
||||||
|
bool is_visible() { |
||||||
|
return visible; |
||||||
|
} |
||||||
|
|
||||||
|
void render(sf::RenderTarget& target) { |
||||||
|
assert($active != nullptr && "you didn't set active"); |
||||||
|
$active->render(target); |
||||||
|
} |
||||||
|
|
||||||
|
void update() { |
||||||
|
assert($active != nullptr && "you didn't set active"); |
||||||
|
$active->update(); |
||||||
|
} |
||||||
|
|
||||||
|
bool mouse(float x, float y, guecs::Modifiers mods = guecs::NO_MODS) { |
||||||
|
assert($active != nullptr && "you didn't set active"); |
||||||
|
return $active->mouse(x, y, mods); |
||||||
|
} |
||||||
|
|
||||||
|
bool move(const UIStackMap::iterator& itr, const UIStackMap::iterator& avoid, const UIStackMap::iterator& result) { |
||||||
|
if(itr != avoid) { |
||||||
|
update_active(result); |
||||||
|
return true; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
bool next() { |
||||||
|
return move($current + 1, screens.end(), $current + 1); |
||||||
|
} |
||||||
|
|
||||||
|
bool prev() { |
||||||
|
return move($current, screens.begin(), $current - 1); |
||||||
|
} |
||||||
|
|
||||||
|
void first() { |
||||||
|
update_active(screens.begin()); |
||||||
|
} |
||||||
|
|
||||||
|
void last() { |
||||||
|
update_active(screens.end() - 1); |
||||||
|
} |
||||||
|
|
||||||
|
const std::string& active_name() { |
||||||
|
assert($active != nullptr && "you didn't set active"); |
||||||
|
return (*$current).first; |
||||||
|
} |
||||||
|
|
||||||
|
std::shared_ptr<SCREEN_TYPE> active_ui() { |
||||||
|
assert($active != nullptr && "you didn't set active"); |
||||||
|
return (*$current).second; |
||||||
|
} |
||||||
|
|
||||||
|
std::shared_ptr<SCREEN_TYPE> at(const std::string& name) { |
||||||
|
return screens.at(name); |
||||||
|
} |
||||||
|
|
||||||
|
const UIStackMap::key_container_type& names() { |
||||||
|
return screens.keys(); |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
sources = [ |
||||||
|
'src/glad/gl.cpp', |
||||||
|
'src/stb_image.cpp', |
||||||
|
'src/shader.cpp', |
||||||
|
'src/text.cpp', |
||||||
|
'src/stats.cpp', |
||||||
|
'src/dbc.cpp', |
||||||
|
'src/builder.cpp', |
||||||
|
'src/bezos.cpp', |
||||||
|
'src/document.cpp', |
||||||
|
'src/framebuffer.cpp', |
||||||
|
'src/screen.cpp', |
||||||
|
'src/engine.cpp', |
||||||
|
'src/image.cpp', |
||||||
|
'src/options.cpp', |
||||||
|
'src/guecs/ui.cpp', |
||||||
|
'src/guecs/lel.cpp', |
||||||
|
'src/guecs/theme.cpp', |
||||||
|
'src/guecs/sfml/components.cpp', |
||||||
|
] |
||||||
@ -0,0 +1,5 @@ |
|||||||
|
tests = [ |
||||||
|
'tests/main.cpp', |
||||||
|
'tests/sol2_test.cpp', |
||||||
|
'tests/builder_test.cpp', |
||||||
|
] |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
[wrap-file] |
||||||
|
directory = miniaudio-0.11.22 |
||||||
|
source_url = https://github.com/mackron/miniaudio/archive/refs/tags/0.11.22.tar.gz |
||||||
|
source_filename = miniaudio-0.11.22.tar.gz |
||||||
|
source_hash = bcb07bfb27e6fa94d34da73ba2d5642d4940b208ec2a660dbf4e52e6b7cd492f |
||||||
|
patch_filename = miniaudio_0.11.22-2_patch.zip |
||||||
|
patch_url = https://wrapdb.mesonbuild.com/v2/miniaudio_0.11.22-2/get_patch |
||||||
|
patch_hash = 345bc40914588b9901aff3d95213e25fdd678c24e976588a63442ad2c5171c77 |
||||||
|
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/miniaudio_0.11.22-2/miniaudio-0.11.22.tar.gz |
||||||
|
wrapdb_version = 0.11.22-2 |
||||||
|
|
||||||
|
[provide] |
||||||
|
dependency_names = miniaudio |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
[wrap-git] |
||||||
|
directory=utf8cpp-4.0.9 |
||||||
|
url=https://github.com/nemtrif/utfcpp.git |
||||||
|
revision=v4.0.9 |
||||||
|
depth=1 |
||||||
|
method=cmake |
||||||
|
|
||||||
|
[provide] |
||||||
|
utf8cpp = utf8cpp_dep |
||||||
Loading…
Reference in new issue