-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRectangle.h
88 lines (73 loc) · 2.13 KB
/
Rectangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <SDL.h>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <tuple>
#include <vector>
#include "Common.h"
#include "Item.h"
template<int TId, typename TUserState>
struct RectangleState : public DrawableControl
{
STATE_PROPERTY(glm::vec2, size)
STATE_PROPERTY(glm::vec2, position)
STATE_PROPERTY(uint, color)
STATE_PROPERTY(std::vector<DrawCommand>, draw_commands)
};
template<Operation TOperation>
struct RectangleLogic
{
template<typename TContext, typename ...TProperties>
static auto invoke(const TContext &context, const std::tuple<TProperties...> &)
{
const auto &rectangle = read_control_state<RectangleState>(context);
return repack(context,
rectangle.with_draw_commands({})
);
}
};
template<>
struct RectangleLogic<Operation::Initialize>
{
template<typename TContext, typename ...TProperties>
static auto invoke(const TContext &context, const std::tuple<TProperties...> &)
{
return context_prepend(RectangleState<get_level_v<TContext>, get_user_state_t<TContext>>(), context);
}
};
template<>
struct RectangleLogic<Operation::Update>
{
template<typename TContext, typename ...TProperties>
static auto invoke(const TContext &context, const std::tuple<TProperties...> &properties)
{
const auto &rectangle = read_control_state<RectangleState>(context);
return repack(context,
apply_properties(properties, rectangle)
);
}
};
template<>
struct RectangleLogic<Operation::Draw>
{
template<typename TContext, typename ...TProperties>
static auto invoke(const TContext &context, const std::tuple<TProperties...> &)
{
const auto &rectangle = read_control_state<RectangleState>(context);
const auto &draw_command = DrawCommand()
.with_matrix(glm::scale(glm::translate(glm::mat3(1.0f), rectangle.position), rectangle.size))
.with_color(rectangle.color);
return repack(context
, rectangle.with_draw_commands({ draw_command })
);
}
};
template<typename ...TParameters>
struct Rectangle : public Item<RectangleLogic, TParameters...>
{
Rectangle(const TParameters &...parameters)
: Item<RectangleLogic, TParameters...>(parameters...)
{
}
};
#endif // RECTANGLE_H