43 lines
2.6 KiB
C++
43 lines
2.6 KiB
C++
#include "Hover_Tooltip.h"
|
|
#include "../Axis/Abs_Axis_p.h"
|
|
namespace renderive {
|
|
void Hover_Tooltip_State::draw_hover_tooltip(Canvas* canvas, Abs_Axis* domain_axis, Abs_Axis* value_axis, Hover_Tooltip_Provider* renderable, const Render_Frame_Snapshot* snapshot) {
|
|
Point pos = hover_position;
|
|
std::vector<std::string> lines = renderable->build_hover_lines(domain_axis, value_axis, pos, snapshot);
|
|
double max_width = 0;
|
|
double line_height = 0;
|
|
for (const std::string& s : lines) {
|
|
Text_Metrics metrics = canvas->measure_text(hover_info_font, s);
|
|
max_width = std::max(max_width, metrics.width);
|
|
line_height = std::max(line_height, metrics.line_height());
|
|
}
|
|
Rect rect(pos.x + hover_info_left, pos.y + hover_info_top,
|
|
static_cast<int>(max_width) + hover_info_right + hover_info_left,
|
|
static_cast<int>(line_height * lines.size()) + hover_info_bottom);
|
|
canvas->set_brush(tooltip_background_brush);
|
|
canvas->fill_rect(RectF{(double)rect.x, (double)rect.y, (double)rect.width, (double)rect.height});
|
|
canvas->set_pen(tooltip_text_pen);
|
|
canvas->set_font(hover_info_font);
|
|
int lh = static_cast<int>(line_height);
|
|
for (int i = 0; i < (int)lines.size(); ++i) {
|
|
RectF line_rect((double)rect.x, (double)(rect.y + i * lh), (double)rect.width, (double)lh);
|
|
canvas->draw_text(line_rect, Text_Align::Center, lines[i]);
|
|
}
|
|
}
|
|
bool Hover_Tooltip_State::can_show_hover(Renderable* able) const {
|
|
auto tmp = dynamic_cast<Hover_Tooltip_Provider*>(able);
|
|
return use_hover_tooltip && hover_info_active && tmp->hit_test_hover(hover_position);
|
|
}
|
|
std::vector<std::string> Hover_Tooltip_Provider::build_hover_lines(Abs_Axis* h, Abs_Axis* v, Point pos, const Render_Frame_Snapshot* snapshot) {
|
|
Axis_Mapping_2D mapping = snapshot ? Axis_Render_Access::mapping(h, v, *snapshot) : Axis_Render_Access::mapping(h, v);
|
|
PointF screen_point{static_cast<double>(pos.x), static_cast<double>(pos.y)};
|
|
double tick1 = mapping.domain.pixel_to_coord(mapping.domain_pixel(screen_point));
|
|
double tick2 = mapping.value.pixel_to_coord(mapping.value_pixel(screen_point));
|
|
std::string line1 = (snapshot ? Axis_Render_Access::tick_label(h, tick1, *snapshot) : Axis_Render_Access::tick_label(h, tick1))
|
|
+ (snapshot ? Axis_Render_Access::unit_text(h, *snapshot) : h->unit_text());
|
|
std::string line2 = (snapshot ? Axis_Render_Access::tick_label(v, tick2, *snapshot) : Axis_Render_Access::tick_label(v, tick2))
|
|
+ (snapshot ? Axis_Render_Access::unit_text(v, *snapshot) : v->unit_text());
|
|
return {line1, line2};
|
|
}
|
|
} // namespace renderive
|