Number5
Visualisierung 2 Project - Florian Schober (0828151, f.schober@live.com), Andreas Walch (0926780, walch.andreas89@gmail.com)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OwnCursor.cpp
Go to the documentation of this file.
1 #include "Application.hpp"
2 
4  : m_app(app)
5  , m_tex_arrow(app->getTextures().get("hand.gif"))
6  , m_tex_hand(app->getTextures().get("arrow.gif"))
7  , m_lastMeasure(0)
8  , m_lastSpF(0)
9  , m_active_hand(true)
10  , m_scale_linear(true)
11  , m_scale(1.0f)
12  , m_minScale(0.4f)
13  , m_maxScale(1.2f)
14  , m_blink(true)
15  , m_cushion_factor(0.8f)
16 {
17 }
18 
20 {
22 }
23 
25 {
27 }
28 
30 {
31  if((m_cushion_factor > 0.4 && value < 0) || (m_cushion_factor < 2 && value > 0)) m_cushion_factor += value;
32 }
33 
34 void OwnCursor::onKeyStateChanged(int key, bool pressed)
35 {
36  if (!pressed) return;
37 
38  if (key == GLFW_KEY_F5) toggleScaleFunction();
39 
40  if (key == GLFW_KEY_F6) toggleImage();
41 
42  if (key == GLFW_KEY_F7) updateCushionFactor(-0.05f);
43 
44  if (key == GLFW_KEY_F8) updateCushionFactor(0.05f);
45 }
46 
47 
48 vec2 OwnCursor::calculateCursorPosInVisualSpace(vec2 cursorPosBeforeMove, vec2 const & deltaMotorSpace)
49 {
50  auto& wnd = m_app->getWindow();
51 
52  auto deltaLength = glm::length(deltaMotorSpace);
53 
54  if ( deltaLength > glm::epsilon<float>()) // only calculate if cursor was moved
55  {
56  auto deltaDir = deltaMotorSpace/deltaLength;
57 
58  while( deltaLength > glm::epsilon<float>() ) // move by distance 1 at once, then again by 1 until whole cursor-delta is passed
59  {
60  auto length = glm::min( deltaLength, 1.0f );
61 
62  cursorPosBeforeMove = calculateCursorMoveDeltaInVisualSpace(cursorPosBeforeMove, deltaDir*length);
63 
64  deltaLength -= length;
65  }
66  }
67 
68  return cursorPosBeforeMove;
69 }
70 
71 vec2 OwnCursor::calculateCursorMoveDeltaInVisualSpace(vec2 const & cursorPosBeforeMove, vec2 const & deltaMotorSpace)
72 {
73  // calculate scale
74  {
75  auto D = m_app->getPicker().getFocusDistance(); // C...cutoff distance
76  auto C = m_app->getPicker().maxDistance(); // D...distnace to nearest target
77 
78  float t = 1.0f;
79 
80  if(m_scale_linear)
81  {
82  // linear scaling function
83  t = D/C;
84  }
85  else
86  {
87  // invers scaling function
88  float s = 0.5f; // scaling constant (emperically chosen)
89  t = 1.0f - pow((D+1),-s);
90  }
91 
92  m_scale = glm::mix(m_minScale,
93  m_maxScale,
94  glm::clamp(t,
95  0.0f,
96  1.0f));
97  }
98 
99  // update scale-factor by cushion-factor
100  float scaleCushion = m_scale*m_cushion_factor;
101 
102  // return new cursor-pos
103  return cursorPosBeforeMove + deltaMotorSpace * scaleCushion;
104 }
105 
106 void OwnCursor::update(double time, double timeDelta)
107 {
108  const double updateDelta = 0.3;
109 
110  if (time > (m_lastMeasure + updateDelta))
111  {
112  m_lastMeasure = time;
113  m_lastSpF = timeDelta;
114  m_blink = !m_blink;
115  }
116 }
117 
118 void OwnCursor::draw(double time, double timeDelta)
119 {
120  auto& wnd = m_app->getWindow();
121  auto& wndSize = m_app->getWindow().getSize();
122  auto& sprite = m_app->getSprites();
123 
124  auto cursorPos = wnd.getCursor();
125 
126  auto px = [wndSize]
127  (float x, float y)
128  {
129  return vec2(x / wndSize.x, y / wndSize.y);
130  };
131 
132  sprite.begin2D();
133  {
134  sprite.begin();
135  {
136  if(m_active_hand)
137  m_tex_hand->bind();
138  else
139  m_tex_arrow->bind();
140 
141  // hand
142  {
143  int tex_size_x;
144  int tex_size_y;
145 
146  if(m_active_hand){
147  tex_size_x= m_tex_hand->getSize().x;
148  tex_size_y= m_tex_hand->getSize().y;
149  }else{
150  tex_size_x= m_tex_arrow->getSize().x;
151  tex_size_y= m_tex_arrow->getSize().y;
152  }
153 
154  // red-white color-sheme
155  float g = (m_scale-m_minScale)*(1.0f/(m_maxScale-m_minScale)); // min_scale...max_scale auf 0..1 mappen
156  float b = g;
157  float r = 1.0f;
158 
159  // blinking
160  if((m_scale-m_minScale) <= 0.01 && m_blink){
161  r = 0.8f;
162  g = 0.0f;
163  b = 0.0f;
164  }
165  if((m_scale-m_minScale) <= 0.01 && !m_blink){
166  r = 1.0f;
167  g = 1.0f;
168  b = 1.0f;
169  }
170 
171  // cursor scaling
172  float cursor_scale = 0.7f+0.4f*(m_scale-m_minScale)*(1.0f/(m_maxScale-m_minScale)); // map scale to 0.7...1.1 for icon-scaling
173 
174  sprite.draw(vec4(r, g, b, 1.0f), px(cursorPos.x, wndSize.y-cursorPos.y), px(tex_size_x*cursor_scale, tex_size_y*cursor_scale), vec2(1,-1), HAlign::Left, VAlign::Top);
175  }
176  }
177  sprite.end();
178  }
179  sprite.end2D();
180 }
vec2 calculateCursorPosInVisualSpace(vec2 cursorPosBeforeMove, vec2 const &deltaMotorSpace)
Definition: OwnCursor.cpp:48
void toggleScaleFunction()
Definition: OwnCursor.cpp:24
float m_maxScale
Definition: OwnCursor.hpp:20
void updateCushionFactor(float value)
Definition: OwnCursor.cpp:29
uint maxDistance() const
Definition: Picker.hpp:54
bool m_active_hand
Definition: OwnCursor.hpp:16
SpriteRenderer & getSprites()
Definition: Application.hpp:81
bool m_blink
Definition: OwnCursor.hpp:21
float getFocusDistance() const
Definition: Picker.hpp:44
float m_minScale
Definition: OwnCursor.hpp:19
float m_cushion_factor
Definition: OwnCursor.hpp:22
bool m_scale_linear
Definition: OwnCursor.hpp:17
double m_lastSpF
Definition: OwnCursor.hpp:14
vec2 calculateCursorMoveDeltaInVisualSpace(vec2 const &cursorPosBeforeMove, vec2 const &deltaMotorSpace)
Definition: OwnCursor.cpp:71
float m_scale
Definition: OwnCursor.hpp:18
uvec2 & getSize()
Definition: Texture.hpp:24
Texture * m_tex_hand
Definition: OwnCursor.hpp:13
Window & getWindow()
Definition: Application.hpp:41
void update(double time, double timeDelta)
Definition: OwnCursor.cpp:106
OwnCursor(Application *app)
Definition: OwnCursor.cpp:3
vec2 const & getSize() const
Definition: Window.hpp:45
Picker & getPicker()
Definition: Application.hpp:91
void toggleImage()
Definition: OwnCursor.cpp:19
Application * m_app
Definition: OwnCursor.hpp:11
Texture * m_tex_arrow
Definition: OwnCursor.hpp:12
double m_lastMeasure
Definition: OwnCursor.hpp:15
void bind(uint target=0)
Definition: Texture.cpp:54
void onKeyStateChanged(int key, bool pressed)
Definition: OwnCursor.cpp:34
void draw(double time, double timeDelta)
Definition: OwnCursor.cpp:118