-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReachingWidget.hpp
136 lines (112 loc) · 3.73 KB
/
ReachingWidget.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
#ifndef REACHINGWIDGET_H
#define REACHINGWIDGET_H
#include <MultiWidgets/ImageWidget.hpp>
#include <Radiant/Grid.hpp>
#include <Luminous/Utils.hpp>
#include <Box2D/Box2D.h>
#include <VacuumWidget.hpp>
#include <map>
struct Particle {
float age;
float max_age;
float brightness;
Nimble::Vector2 pos;
bool dead() { return age > max_age; }
Particle(float brightness=1, float max_age=2, Nimble::Vector2 pos=Nimble::Vector2(0,0)) :
age(0),
max_age(max_age),
brightness(brightness),
pos(pos)
{
}
};
namespace {
float BOX2D_SCALE = 50.0f;
}
class ReachingWidget : public MultiWidgets::Widget {
// ensure appropriate scale for box2d
protected:
static Nimble::Vector2 fromBox2D(const b2Vec2 & v) { return Nimble::Vector2(v.x, v.y)*BOX2D_SCALE; }
static b2Vec2 toBox2D(const Nimble::Vector2 & v) { return b2Vec2(v.x/BOX2D_SCALE, v.y/BOX2D_SCALE); }
static float toBox2D(float v) { return v/BOX2D_SCALE; }
bool isLeftOfLine(Nimble::Vector2 a, Nimble::Vector2 b, Nimble::Vector2 c);
public:
enum FeatureFlags {
FEATURE_ARROWS = 1 << 0,
FEATURE_PARTICLES = 1 << 1,
FEATURE_VELOCITY_FIELD = 1 << 2,
FEATURE_GRAVITY = 1 << 3
};
ReachingWidget(MultiWidgets::Widget * parent = 0);
virtual void deleteChild(Widget *w);
void setFeatureFlags(uint32_t flags);
void resize(int width, int height);
//virtual void update(float dt);
void updateParticles(float dt);
virtual void decayVectorField(Radiant::MemGrid32f (&field)[2], float dt) = 0;
virtual void ensureWidgetsHaveBodies() = 0;
void ensureGroundInitialized();
virtual void applyForceToBodies(float dt) = 0;
void updateBodiesToWidgets();
Nimble::Vector2 vectorValue(Nimble::Vector2 v);
Nimble::Vector2 vectorOffset(Nimble::Vector2 pos);
void blur(float ratio);
Nimble::Vector2 mapInput(Nimble::Vector2 v);
MultiWidgets::Widget * findChildInside(Luminous::Transformer & tr, Nimble::Vector2f loc, MultiWidgets::Widget * parent);
void resetVectorField();
virtual void resetAndClear() = 0;
virtual void input(MultiWidgets::GrabManager & gm, float dt) = 0;
virtual void render(Luminous::RenderContext & r) = 0;
virtual void isReachingActive(bool active) = 0;
int w, h;
Radiant::MemGrid32f m_vectorFields[2];
struct GLData;
std::vector<Particle> m_particles;
std::vector<int> m_free_particles;
std::set<long> m_currentFingerIds;
Valuable::ValueIntT<uint32_t> m_featureFlags;
b2World m_world;
std::map<void*, b2Body*> m_bodies;
Valuable::ValueFloat m_bg_alpha;
Valuable::ValueFloat m_idle_timeout;
Valuable::ValueFloat m_idle_ok;
Valuable::ValueFloat m_input_pull_intensity;
Valuable::ValueFloat m_input_pull_angle;
Valuable::ValueInt m_input_pull_type;
Valuable::ValueVector2f m_gravity;
Valuable::ValueFloat m_tubeWidth;
Valuable::ValueFloat m_decayPerSec;
Valuable::ValueFloat m_blurFactor;
float m_particleAcc;
float m_blurAcc;
b2Body * groundBody;
MultiWidgets::Widget * wordGameWidget;
};
struct ReachingWidget::GLData : public Luminous::GLResource {
GLData(Luminous::GLResources* res) :
Luminous::GLResource(res),
m_shader(0),
m_vbo(0),
m_fbo(0)
//m_particleShader(0)
{
m_shader = Luminous::GLSLProgramObject::fromFiles(0, "distort.frag");
//m_particleShader = Luminous::GLSLProgramObject::fromFiles("particles.vert", "particles.frag");
m_fbo = new Luminous::Framebuffer();
m_vbo = new Luminous::VertexBuffer();
}
virtual ~GLData()
{
delete m_shader;
//delete m_particleShader;
delete m_fbo;
}
Luminous::GLSLProgramObject* m_shader;
Luminous::VertexBuffer * m_vbo;
Luminous::Framebuffer * m_fbo;
Luminous::GLSLProgramObject * m_particleShader;
// x, y & read buffer
Luminous::ContextVariableT<Luminous::Texture2D> m_tex[3];
Luminous::ContextVariableT<Luminous::Texture2D> m_particleTexture;
};
#endif // REACHINGWIDGET_H