-
Notifications
You must be signed in to change notification settings - Fork 1
/
Button.hpp
193 lines (168 loc) · 5.47 KB
/
Button.hpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once
#include <array>
#include <string>
#include "Transformation.hpp"
#include "Region.hpp"
#include "Camera.hpp"
#include "Sprite.hpp"
#include "ShaderProgram.hpp"
#include "Shader.hpp"
#include "ShaderUniform.hpp"
#include "ShaderAttrib.hpp"
#include "Texture.hpp"
#include "Text.hpp"
#include "StrConst.hpp"
namespace ui {
template <typename BUTTON_FILENAME, typename FONT_FILENAME>
struct Button {
std::string dir;
// workaround for "error: cannot compile this scalar expression yet" from clang
const std::string btnquadv = sys::Path("shaders"s) / sys::Path("btn_quad.vert"s);
const std::string btntextv = sys::Path("shaders"s) / sys::Path("btn_text.vert"s);
const std::string btnquadf = sys::Path("shaders"s) / sys::Path("btn_quad.frag"s);
const std::string btntextf = sys::Path("shaders"s) / sys::Path("btn_text.frag"s);
using self_t = Button<BUTTON_FILENAME, FONT_FILENAME>;
Sprite<ui::Font, self_t> *font;
ui::Text label;
gl::Texture btnTx;
static constexpr int DEFAULT_STATE = 0;
static constexpr int POINTED_STATE = 1;
static constexpr int SELECTED_STATE = 2;
int state = DEFAULT_STATE;
gl::Uniform<gl::UniformType::INTEGER> uState;
gl::ShaderProgram<
gl::VertexShader,
gl::FragmentShader
> quadProgram;
gl::ShaderProgram<
gl::VertexShader,
gl::FragmentShader
> textProgram;
gl::Buffer<GL_ARRAY_BUFFER, gl::BufferElementType::VEC2> buf;
gl::Attrib<decltype(buf)> attrVertex;
gl::VertexArray<decltype(attrVertex)> vao;
Region region;
using ShaderBuffer = decltype(buf);
using ShaderAttrib = decltype(attrVertex);
using ShaderProgramQuad = decltype(quadProgram);
using ShaderProgramText = decltype(textProgram);
using VertexArray = decltype(vao);
Button(const std::string &dir, Region region=Region(glm::vec2(-1,1), glm::vec2(-1,1))):
dir(dir),
font(Sprite<ui::Font, self_t>::create(sys::Path(dir) / sys::Path(FONT_FILENAME::c_str))),
label(font->object),
btnTx("btn"),
uState("state"),
quadProgram({
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("btn_quad.vert"s),
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("btn_quad.frag"s),
}),
textProgram({
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("btn_text.vert"s),
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("btn_text.frag"s),
}),
attrVertex("vertex", buf),
vao(attrVertex),
region(region)
{}
void init() {
ShaderBuffer::init(buf);
buf.allocate<GL_STREAM_DRAW>(std::vector<float>{
1,1, -1,1, -1,-1,
-1,-1, 1,-1, 1,1,
});
VertexArray::init(vao);
attrVertex.select_buffer(buf);
vao.enable(attrVertex);
vao.set_access(attrVertex, 0);
ShaderProgramQuad::init(quadProgram, vao);
font->init();
label.init(textProgram);
btnTx.init(sys::Path(dir) / sys::Path(BUTTON_FILENAME::c_str));
btnTx.uSampler.set_id(quadProgram.id());
uState.set_id(quadProgram.id());
}
void setx(float l, float r) {
if(l > r)std::swap(l, r);
region.xs = glm::vec2(l, r);
}
void sety(float t, float b) {
if(t > b)std::swap(t, b);
region.ys = glm::vec2(t, b);
}
void mouse(float m_x, float m_y) {
float s_x = 2 * m_x - 1;
float s_y = 2 * m_y - 1;
/* printf("button mouse: %f %f\n", m_x, m_y); */
/* printf("xs: %f %f\n", region.x1(), region.x2()); */
/* printf("ys: %f %f\n", region.y1(), region.y2()); */
/* printf("contains: %d\n", region.contains(m_x, m_y)); */
/* printf("contains x: %d\n", region.x1() <= m_x && m_x <= region.x2()); */
/* printf("contains y: %d\n", region.y1() <= m_y && m_y <= region.y2()); */
if(state == DEFAULT_STATE && region.contains(s_x, s_y)) {
state = POINTED_STATE;
} else if(state == POINTED_STATE && !region.contains(s_x, s_y)) {
state = DEFAULT_STATE;
}
}
bool clicked = false;
void mouse_click(int button, int action) {
if(button == GLFW_MOUSE_BUTTON_LEFT) {
if(action == GLFW_PRESS) {
if(state == POINTED_STATE) {
state = SELECTED_STATE;
}
} else if(action == GLFW_RELEASE) {
if(state == SELECTED_STATE) {
state = POINTED_STATE;
clicked = true;
}
}
} else if(button == GLFW_MOUSE_BUTTON_RIGHT) {
if(action == GLFW_PRESS && state == SELECTED_STATE) {
state = POINTED_STATE;
}
}
}
template <typename F>
void action_on_click(F func) {
if(clicked) {
func();
clicked = false;
}
}
void display() {
label.transform.SetScale((region.x2()-region.x1())/2, (region.y2()-region.y1())/2, 1.);
label.pos = { region.center().x, region.center().y };
// display quad
ShaderProgramQuad::use(quadProgram);
gl::Texture::set_active(0);
btnTx.bind();
btnTx.set_data(0);
uState.set_data(state);
VertexArray::bind(vao);
auto m = label.transform.get_matrix();
label.uTransform.set_id(quadProgram.id());
label.uTransform.set_data(m);
glDrawArrays(GL_TRIANGLES, 0, 6); GLERROR
label.uTransform.unset_id();
VertexArray::unbind();
gl::Texture::unbind();
ShaderProgramQuad::unuse();
// display text
/* label.transform.SetScale(1, 1, 1); */
label.color = glm::vec3(1, 1, 1);
label.display(textProgram);
}
void clear() {
ShaderAttrib::clear(attrVertex);
ShaderBuffer::clear(buf);
VertexArray::clear(vao);
btnTx.clear();
label.clear();
ShaderProgramQuad::clear(quadProgram);
ShaderProgramText::clear(textProgram);
font->clear();
}
};
} // namespace ui