Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
bubble_message.hpp
Go to the documentation of this file.
1#pragma once
2
25
26#include <algorithm>
27#include <functional>
28#include <string>
29
32
33namespace pulp::view {
34
35class View; // fwd
36
38public:
39 enum class Phase {
43 };
44
49 enum class Side {
54 };
55
57
66 void show_for(View* source, std::string text, float lifetime = 3.0f) {
67 source_ = source;
68 text_ = std::move(text);
69 lifetime_ = std::max(0.0f, lifetime);
70 elapsed_ = 0.0f;
72 if (phase_ != Phase::showing) {
73 phase_ = Phase::showing;
74 opacity_.set(0.0f);
75 opacity_.animate_to(1.0f, fade_duration_);
76 }
77 }
78
82 void move_to(Point anchor) {
83 explicit_anchor_ = anchor;
84 has_explicit_anchor_ = true;
85 position_ = compute_offset(anchor);
86 }
87
92
95 void hide() {
96 if (phase_ != Phase::showing) return;
97 opacity_.animate_to(0.0f, fade_duration_);
98 phase_ = Phase::hiding;
99 }
100
104 bool tick(float dt) {
105 switch (phase_) {
106 case Phase::idle:
107 return false;
108 case Phase::showing:
109 opacity_.advance(dt);
110 elapsed_ += dt;
111 if (lifetime_ > 0.0f && elapsed_ >= lifetime_) {
112 hide();
113 }
114 return true;
115 case Phase::hiding:
116 opacity_.advance(dt);
117 if (!opacity_.animating()) {
118 phase_ = Phase::idle;
119 source_ = nullptr; // Transient: forget the source.
120 has_explicit_anchor_ = false;
121 return false;
122 }
123 return true;
124 }
125 return false;
126 }
127
128 // ── State ────────────────────────────────────────────────────────
129
130 bool visible() const {
131 return phase_ == Phase::showing || phase_ == Phase::hiding;
132 }
133 Phase phase() const { return phase_; }
134 float opacity() const { return opacity_.value(); }
135 Point position() const { return position_; }
136 const std::string& text() const { return text_; }
137 View* source_view() const { return source_; }
138 float elapsed() const { return elapsed_; }
139
140 // ── Configuration ───────────────────────────────────────────────
141
142 void set_side(Side s) { side_ = s; }
143 Side side() const { return side_; }
144
147 void set_offset(Point offset) { offset_ = offset; }
148 Point offset() const { return offset_; }
149
152 void set_lifetime(float seconds) { lifetime_ = std::max(0.0f, seconds); }
153 float lifetime() const { return lifetime_; }
154
156 void set_fade_duration(float seconds) {
157 fade_duration_ = std::max(0.0f, seconds);
158 }
159 float fade_duration() const { return fade_duration_; }
160
162 std::function<void(Phase)> on_phase_change;
163
164private:
165 Point compute_offset(Point anchor) const {
166 return {anchor.x + offset_.x, anchor.y + offset_.y};
167 }
168
169 View* source_ = nullptr;
170 std::string text_;
171 Side side_ = Side::above;
172 Point position_{};
173 Point offset_{0, -8};
174 Point explicit_anchor_{};
175 bool has_explicit_anchor_ = false;
176
177 Phase phase_ = Phase::idle;
178 float lifetime_ = 3.0f;
179 float elapsed_ = 0.0f;
180 float fade_duration_ = 0.12f;
181 ValueAnimation opacity_{0.0f};
182};
183
184} // namespace pulp::view
Phase phase() const
Definition bubble_message.hpp:133
bool tick(float dt)
Advance time.
Definition bubble_message.hpp:104
bool visible() const
Definition bubble_message.hpp:130
Side
Anchor edge — where the bubble is placed relative to the source view's bounds.
Definition bubble_message.hpp:49
@ above
Bubble sits above the top edge of the source.
Definition bubble_message.hpp:50
@ below
Bubble sits below the bottom edge of the source.
Definition bubble_message.hpp:51
@ right
Bubble sits to the right of the source.
Definition bubble_message.hpp:53
@ left
Bubble sits to the left of the source.
Definition bubble_message.hpp:52
float fade_duration() const
Definition bubble_message.hpp:159
View * source_view() const
Definition bubble_message.hpp:137
void recompute_anchor()
Re-derive the position from source_view()->bounds() and the configured side().
Point offset() const
Definition bubble_message.hpp:148
std::function< void(Phase)> on_phase_change
Optional observer fired on every phase transition.
Definition bubble_message.hpp:162
float opacity() const
Definition bubble_message.hpp:134
void show_for(View *source, std::string text, float lifetime=3.0f)
Begin showing the bubble anchored to source.
Definition bubble_message.hpp:66
Phase
Definition bubble_message.hpp:39
@ hiding
Fade-out in progress.
Definition bubble_message.hpp:42
@ showing
Visible (possibly still fading in).
Definition bubble_message.hpp:41
@ idle
Not currently shown.
Definition bubble_message.hpp:40
void set_fade_duration(float seconds)
Fade-in / fade-out duration.
Definition bubble_message.hpp:156
void hide()
Begin fade-out.
Definition bubble_message.hpp:95
Point position() const
Definition bubble_message.hpp:135
float elapsed() const
Definition bubble_message.hpp:138
void set_lifetime(float seconds)
Time in seconds the bubble stays visible before auto-fade.
Definition bubble_message.hpp:152
void set_side(Side s)
Definition bubble_message.hpp:142
float lifetime() const
Definition bubble_message.hpp:153
void move_to(Point anchor)
Update the anchor explicitly (skip the source-view lookup).
Definition bubble_message.hpp:82
void set_offset(Point offset)
Pixel offset added to the computed anchor after the side projection.
Definition bubble_message.hpp:147
const std::string & text() const
Definition bubble_message.hpp:136
Side side() const
Definition bubble_message.hpp:143
Definition view.hpp:73
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ s
Definition input_events.hpp:41
Definition geometry.hpp:20