Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
castle055 committed Feb 1, 2024
2 parents 85728d0 + 2582637 commit 725d762
Show file tree
Hide file tree
Showing 1,595 changed files with 48,008 additions and 17,778 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ cmake-build-release/
.cache/

cmake-build-debug-clang/
cmake-build-debug-gcc-experimental/
cmake-build-release-gcc-experimental/
70 changes: 63 additions & 7 deletions dist/include/core/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ namespace cydui::components {
virtual void configure_event_handler() = 0;
virtual void subscribe_events() = 0;
virtual void clear_children() = 0;
virtual attrs_component<>* attrs() = 0;
virtual std::string name() = 0;

virtual event_handler_t* event_handler() = 0;

Expand Down Expand Up @@ -143,7 +145,7 @@ namespace cydui::components {
};

void configure_event_handler() override {
EVH * evh = event_handler_.operator->();
EVH* evh = event_handler_.operator->();
if (parent.has_value()) {
evh->parent = parent.value()->event_handler();
} else {
Expand All @@ -153,10 +155,11 @@ namespace cydui::components {
evh->props = &(((T*) this)->props);
evh->attrs = (attrs_component<T>*) this;
evh->get_dim = [this] {return get_dimensional_relations();};
evh->$children = [this] {return children;};
}
void subscribe_events() override {
clear_subscribed_listeners();
EVH * evh = event_handler_.operator->();
EVH* evh = event_handler_.operator->();
add_event_listeners(evh->get_event_listeners());
}
void clear_children() override {
Expand All @@ -165,6 +168,14 @@ namespace cydui::components {
}
children.clear();
}
attrs_component<>* attrs() override {
// Yes, the order of casting matters here because a conversion from `this` to
// `(attrs_component<>*)` does not work since that type is not a base of this
// class. So we need to cast to the base class first and then to its `void`
// specialization.
return (attrs_component<>*) (attrs_component<T>*) this;
}


component_state_t* create_state_instance() override {
return new typename T::state_t;
Expand All @@ -174,13 +185,54 @@ namespace cydui::components {
}

void redraw(cydui::layout::Layout* layout) override {
std::vector<component_holder_t> new_children = this->_content();
std::vector<component_holder_t> redraw_children = event_handler_->on_redraw();
for (auto &item: redraw_children) {
new_children.push_back(item);
std::vector<component_holder_t> content_children = this->_content();
std::string content_id_prefix = "content:";
std::size_t id_i = 0;
for (auto &item: content_children) {
for (auto &component_pair: item.get_components()) {
auto [id_, component] = component_pair;
std::string id = content_id_prefix;
id.append(id_);
id.append(":");
id.append(std::to_string(id_i));

// Get or Create state for component
component_state_t* child_state;
if (state.value()->children_states.contains(id)) {
child_state = state.value()->children_states[id];
} else {
child_state = component->create_state_instance();
state.value()->children_states[id] = child_state;
}

// Set child's variables
component->state = child_state;
child_state->win = state.value()->win;
child_state->parent = state.value();
child_state->component_instance = component;
component->parent = this;
children.push_back(component);
//printf("CHILDREN LEN: %d\n", children.size());

// Configure event handler
component->configure_event_handler();

// Subscribe child events
component->subscribe_events();
// Redraw child
//component->redraw(layout);

}
++id_i;
}

std::size_t id_i = 0;
std::vector<component_holder_t> new_children = event_handler_->on_redraw();
for (auto &item: children) {
// Redraw content child
item->redraw(layout);
}

id_i = 0;
for (auto &item: new_children) {
for (auto &component_pair: item.get_components()) {
auto [id_, component] = component_pair;
Expand Down Expand Up @@ -295,8 +347,12 @@ namespace cydui::components {
};
}
};

}

template<typename C>
struct component_state_template: public cydui::components::component_state_t { };

//#include "../graphics/vg.h"
//
//struct CanvasState: public component_state_t {
Expand Down
59 changes: 49 additions & 10 deletions dist/include/core/component_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,56 @@ struct attribute_i {
#define COMPONENT_ATTRIBUTE(TYPE, NAME, DEFAULT) \
template<typename E> \
struct attr_##NAME: public attribute_i { \
TYPE _##NAME = DEFAULT; \
~attr_##NAME() override = default; \
inline E &NAME(auto& _##NAME##_) { \
template<typename S = E, typename = std::enable_if_t<!std::is_void_v<S>>> \
inline S& NAME(auto& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
return *(E*)this; \
} \
inline E &NAME(auto&& _##NAME##_) { \
template<typename S = E, typename = std::enable_if_t<!std::is_void_v<S>>> \
inline S& NAME(auto&& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
return *(E*)this; \
} \
TYPE _##NAME = DEFAULT; \
template<typename S = E, typename = std::enable_if_t<std::is_void_v<S>>> \
inline void NAME(auto& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
} \
template<typename S = E, typename = std::enable_if_t<std::is_void_v<S>>> \
inline void NAME(auto&& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
} \
}

#define COMPONENT_ATTRIBUTE_W_MONITOR(TYPE, NAME, DEFAULT) \
template<typename E> \
struct attr_##NAME: public attribute_i { \
TYPE _##NAME = DEFAULT; \
bool _##NAME##_has_changed = false; \
~attr_##NAME() override = default; \
inline E &NAME(auto& _##NAME##_) { \
template<typename S = E, typename = std::enable_if_t<!std::is_void_v<S>>> \
inline S& NAME(auto& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
this->_##NAME##_has_changed = true; \
return *(E*)this; \
} \
inline E &NAME(auto&& _##NAME##_) { \
template<typename S = E, typename = std::enable_if_t<!std::is_void_v<S>>> \
inline S& NAME(auto&& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
this->_##NAME##_has_changed = true; \
return *(E*)this; \
} \
TYPE _##NAME = DEFAULT; \
bool _##NAME##_has_changed = false; \
template<typename S = E, typename = std::enable_if_t<std::is_void_v<S>>> \
inline void NAME(auto& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
this->_##NAME##_has_changed = true; \
} \
template<typename S = E, typename = std::enable_if_t<std::is_void_v<S>>> \
inline void NAME(auto&& _##NAME##_) { \
this->_##NAME = _##NAME##_; \
this->_##NAME##_has_changed = true; \
} \
}

COMPONENT_ATTRIBUTE(cydui::dimensions::dimension_t, x, 0);
Expand All @@ -63,14 +85,24 @@ using content = std::vector<cydui::components::component_holder_t>;

template<typename E>
struct attr_content: public attribute_i {
inline E &operator()(auto &&_content_) {
template<typename S = E, typename = std::enable_if_t<!std::is_void_v<S>>>
inline S &operator()(auto &&_content_) {
this->_content = _content_;
return *(E*) this;
}
inline E &operator()(auto &_content_) {
template<typename S = E, typename = std::enable_if_t<!std::is_void_v<S>>>
inline S &operator()(auto &_content_) {
this->_content = _content_;
return *(E*) this;
}
template<typename S = E, typename = std::enable_if_t<std::is_void_v<S>>>
inline void operator()(auto &&_content_) {
this->_content = _content_;
}
template<typename S = E, typename = std::enable_if_t<std::is_void_v<S>>>
inline void operator()(auto &_content_) {
this->_content = _content_;
}
std::function<content()> _content = [] -> content {return {};};
};

Expand Down Expand Up @@ -98,13 +130,20 @@ struct attrs_dimensions
attr_h<T> {
};

template<typename T>
template<typename T = void>
struct attrs_component
: attrs_dimensions<T>,
attrs_margin<T>,
attrs_padding<T>,
attr_content<T> {
};
//template<>
//struct attrs_component<void>
// : attrs_dimensions<void>,
// attrs_margin<void>,
// attrs_padding<void>,
// attr_content<void> {
//};

#undef COMPONENT_ATTRIBUTE
#undef COMPONENT_ATTRIBUTE_W_MONITOR
Expand Down
1 change: 1 addition & 0 deletions dist/include/core/component_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace cydui::components {
event_handler_t* parent;

std::function<component_dimensional_relations_t()> get_dim;
std::function<std::vector<component_base_t*>()> $children;
CYDUI_INTERNAL_EV_HANDLER_DIMENSION_ACCESSOR(x)
CYDUI_INTERNAL_EV_HANDLER_DIMENSION_ACCESSOR(y)
CYDUI_INTERNAL_EV_HANDLER_DIMENSION_ACCESSOR(w)
Expand Down
6 changes: 6 additions & 0 deletions dist/include/core/component_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ namespace cydui::components {
const auto &get_components() const {
return components;
}

// Cannot work, since we would need to recursively copy any `component_holder_t` in the props
// of each component.
//component_holder_t clone() const {
//
//}
private:
std::unordered_map<std::string, component_base_t*> components {};
};
Expand Down
20 changes: 12 additions & 8 deletions dist/include/core/component_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
#define CYDUI_COMPONENT_METADATA(NAME_) \
logging::logger log{.name = #NAME_};\
static constexpr const char* NAME = #NAME_ ; \


std::string name() override { return std::string {NAME}; }

// ? Macros for declaring component classes
// ?>
#define STATE(NAME) \
struct CYDUI_STATE_NAME(NAME): public cydui::components::component_state_t

struct NAME; \
template<typename T>\
struct component_state_template; \
template<> \
struct component_state_template<NAME>: public cydui::components::component_state_t

// ?>
#define COMPONENT(NAME, ...) \
struct NAME; \
using CYDUI_STATE_NAME(NAME) = component_state_template<NAME>; \
struct CYDUI_EV_HANDLER_NAME(NAME); \
struct NAME: \
public cydui::components::component_t<CYDUI_EV_HANDLER_NAME(NAME),NAME> \
Expand All @@ -35,8 +39,8 @@ struct NAME: \
using state_t = CYDUI_STATE_NAME(NAME); \
using event_handler_t = CYDUI_EV_HANDLER_NAME(NAME); \
struct props_t \
__VA_ARGS__ \
props; \
__VA_ARGS__; \
props_t props; \
NAME() = default; \
explicit NAME(props_t props) \
: cydui::components::component_t<CYDUI_EV_HANDLER_NAME(NAME),NAME>()\
Expand Down Expand Up @@ -76,8 +80,8 @@ struct NAME: \
using state_t = CYDUI_STATE_NAME(NAME) SET_COMPONENT_TEMPLATE_SHORT; \
using event_handler_t = CYDUI_EV_HANDLER_NAME(NAME) SET_COMPONENT_TEMPLATE_SHORT; \
struct props_t \
__VA_ARGS__ \
props; \
__VA_ARGS__; \
props_t props; \
NAME() = default; \
explicit NAME(props_t props) \
: cydui::components::component_t< \
Expand Down
4 changes: 4 additions & 0 deletions dist/include/core/dimensions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ namespace cydui::dimensions {
return this->binding();
}

dimension_value_t val() const {
return this->binding();
}

//dimensional_relation_t(// NOLINT(google-explicit-constructor)
// dimensional_relation_t const &rel
//) = default;
Expand Down
3 changes: 3 additions & 0 deletions dist/include/core/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace cydui::window {
prof::context_t profiling_ctx {};

void terminate();

std::pair<int, int> get_position() const;
std::pair<int, int> get_size() const;
};

CWindow* create(
Expand Down
4 changes: 4 additions & 0 deletions dist/include/graphics/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ namespace cydui::graphics {

unsigned long get_id(window_t* win);

std::pair<int, int> get_position(window_t* win);

std::pair<int, int> get_size(window_t* win);

void terminate(window_t* win);
}// namespace cydui::graphics

Expand Down

0 comments on commit 725d762

Please sign in to comment.