gpt4 book ai didi

c - goocanvas - 坐标系和缩放到光标

转载 作者:太空狗 更新时间:2023-10-29 15:42:10 25 4
gpt4 key购买 nike

我正在尝试在位于 scrolledwindow 中的 goocanvas 中实现对指针/光标的缩放。不幸的是,有关所用坐标系的文档并不理想。

那么有人能解释一下为什么下面的概念代码应该工作吗? (详见内联代码注释)

便利结构

 typedef struct {
gdouble x;
gdouble y;
} Coords;
typedef Sheet GooCanvas; // actually a subclass, but anyways

只是一个方便的辅助函数(剥离测试)

gboolean sheet_get_adjustments (const Sheet *sheet, GtkAdjustment **hadj, GtkAdjustment **vadj)
{
GtkWidget *parent;
GtkScrolledWindow *scrolled;
parent = gtk_widget_get_parent (GTK_WIDGET (sheet));
scrolled = GTK_SCROLLED_WINDOW (parent);
*hadj = gtk_scrolled_window_get_hadjustment (scrolled);
*vadj = gtk_scrolled_window_get_vadjustment (scrolled);
return TRUE;
}

以像素为单位获取指针位置(已验证,确实按预期工作)

gboolean sheet_get_pointer_pixel (Sheet *sheet, gdouble *x, gdouble *y)
{
GtkAdjustment *hadj, *vadj;
gdouble x1, y1;
gint _x, _y;
GdkDeviceManager *device_manager;
GdkDevice *device_pointer;
GdkRectangle allocation;

device_manager = gdk_display_get_device_manager (
gtk_widget_get_display (GTK_WIDGET (sheet)));
device_pointer = gdk_device_manager_get_client_pointer (device_manager);
gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (sheet)),
device_pointer,
&_x, &_y, NULL);

gtk_widget_get_allocation (GTK_WIDGET (sheet), &allocation);

_x -= allocation.x;
_y -= allocation.y;

x1 = (gdouble) _x;
y1 = (gdouble) _y;

if (!sheet_get_adjustments (sheet, &hadj, &vadj))
return FALSE;

x1 += gtk_adjustment_get_value (hadj);
y1 += gtk_adjustment_get_value (vadj);

*x = x1;
*y = y1;
return TRUE;
}

对于实际的缩放校正,我们采用以像素为单位的指针位置,它是恒定的并且与 goo Canvas 比例无关。将比例应用于 Canvas 之前之后,根据以像素为单位的指针位置计算 goocanvas 位置。不同之处在于指针在缩放后的位置与它应该在的位置之间的偏移量(我们希望钳制到 Canvas 位置)。在 Canvas 坐标中获取 Canvas 的左上角,应用偏移量并滚动到新的左上角位置(goo_canvas_scroll_to 始终向左上角滚动)

void change_zoom(Sheet *sheet, gdouble f)
{
Coords a;
Coords pixel_pointer;
Coords goo_pointer_before, goo_pointer_after;

GtkWidget *w;
GtkAdjustment *hadj, *vadj;

if (sheet_get_adjustments (sheet, &hadj, &vadj)) {
a.x = gtk_adjustment_get_value (hadj);
a.y = gtk_adjustment_get_value (vadj);
} else {
a.x = a.y = 0.;
}

w = GTK_WIDGET (sheet);

//top left, not used, just to "visualize" the visible canvas range
Coords goo_tl = {a.x, a.y};
Coords pixel_tl = goo_tl;
goo_canvas_convert_to_pixels (GOO_CANVAS (sheet), &pixel_tl.x, &pixel_tl.y);
NG_DEBUG ("### top left [pixel] %lf,%lf", pixel_tl.x, pixel_tl.y);


sheet_get_pointer_pixel (sheet, &pixel_pointer.x, &pixel_pointer.y);
goo_pointer_before = pixel_pointer;
goo_canvas_convert_from_pixels (GOO_CANVAS (sheet), &goo_pointer_before.x, &goo_pointer_before.y);
NG_DEBUG ("### pointer before scale[canvas] %lf,%lf", goo_pointer_before.x, goo_pointer_before.y);
////////////////////////////////////////////////////////////////////////////////////////////
goo_canvas_set_scale (GOO_CANVAS (sheet), f * goo_canvas_get_scale (GOO_CANVAS (sheet)));
////////////////////////////////////////////////////////////////////////////////////////////
goo_pointer_after = pixel_pointer;
goo_canvas_convert_from_pixels (GOO_CANVAS (sheet), &goo_pointer_after.x, &goo_pointer_after.y);
NG_DEBUG ("### pointer after scale[canvas] %lf,%lf", goo_pointer_after.x, goo_pointer_after.y);

// this needs to be redone, as scaling might have changed the bar positions
if (sheet_get_adjustments (sheet, &hadj, &vadj)) {
a.x = gtk_adjustment_get_value (hadj);
a.y = gtk_adjustment_get_value (vadj);
} else {
a.x = a.y = 0.;
}
Coords goo_tl2 = {a.x, a.y}; //top left, after scroll (in pixel coords)
goo_canvas_convert_from_pixels (GOO_CANVAS (sheet), &goo_tl2.x, &goo_tl2.y); // to goo canvas coords
NG_DEBUG ("### top left after zoom[canvas] %lf,%lf", goo_tl2.x, goo_tl2.y);

// the delta in goo canvas coords
const Coords goo_delta = {goo_pointer_before.x - goo_pointer_after.x, goo_pointer_before.y - goo_pointer_after.y};
NG_DEBUG ("### delta[canvas] %lf,%lf", goo_delta.x, goo_delta.y)

//apply the offset to the _current_ top left corner
goo_tl2.x -= goo_delta.x;
goo_tl2.y -= goo_delta.y;
goo_canvas_scroll_to (GOO_CANVAS (sheet), goo_tl2.x, goo_tl2.y);
NG_DEBUG ("### top left after scroll[canvas] %lf,%lf", goo_tl2.x, goo_tl2.y);

NG_DEBUG ("\n\n\n");
return;
}

问题是,尽管显示的所有内容,goo_delta something 都是错误的,指针未固定到 goo 坐标。所以我的猜测是,我需要一个辅助校正因子,或者我在现有代码中存在错误。

示例(f=1.1,光标靠近 Canvas 的左上角)

 sheet_change_zoom +++ ### top left [pixel]  0.000000,0.000000
sheet_change_zoom +++ ### pointer before scale[canvas] 7.000000,12.000000
sheet_change_zoom +++ ### pointer after scale[canvas] 6.363636,10.909091
sheet_change_zoom +++ ### top left after zoom[canvas] 42.045455,30.318182
sheet_change_zoom +++ ### delta[canvas] 0.636364,1.090909
sheet_change_zoom +++ ### top left after scroll[canvas] 41.409091,29.227273
sheet_change_zoom +++



sheet_change_zoom +++ ### top left [pixel] 50.105000,35.365000
sheet_change_zoom +++ ### pointer before scale[canvas] 47.772727,40.136364
sheet_change_zoom +++ ### pointer after scale[canvas] 43.429752,36.487603
sheet_change_zoom +++ ### top left after zoom[canvas] 79.632231,56.789256
sheet_change_zoom +++ ### delta[canvas] 4.342975,3.648760
sheet_change_zoom +++ ### top left after scroll[canvas] 75.289256,53.140496
sheet_change_zoom +++



sheet_change_zoom +++ ### top left [pixel] 110.231000,77.803000
sheet_change_zoom +++ ### pointer before scale[canvas] 81.074380,63.057851
sheet_change_zoom +++ ### pointer after scale[canvas] 73.703982,57.325319
sheet_change_zoom +++ ### top left after zoom[canvas] 110.037566,78.196844
sheet_change_zoom +++ ### delta[canvas] 7.370398,5.732532
sheet_change_zoom +++ ### top left after scroll[canvas] 102.667168,72.464313
sheet_change_zoom +++

任何类型的提示,指向涵盖此特定问题的 goocanvas 示例的指针。

最佳答案

我最终通过查看 goocanvas 源代码并查看他们如何实现实际的滚动到中心魔法来修复它。据此,我将 Canvas 指针位置移动到中心,缩放并将其移回。这里重要的是,向后移动只是像素位置(这里:只是开始时指针和中心之间的像素差异!)除以累积的缩放因子! (使用goo_canvas_get_scale获取)

注意:以上描述不包含对基于钳位的问题的更正。

伪代码:

delta = pointer_pos_in_pixel - center_pos_in_pixel
top_left_in_pixel read
top_left_in_pixel -= delta
convert_to_goo_coords (top_left_in_pixel → top_left_in_goo)
scroll_to (top_left_in_goo)

scale (f)

delta /= get_absolute_scale()
top_left_in_pixel read
top_left_in_pixel -= delta
convert_to_goo_coords (top_left_in_pixel → top_left_in_goo)
scroll_to (top_left_in_goo)

注意:要以像素为单位计算小部件的中心,请使用 gtk_adjustment_get_page_size 而不是 gtk_widget_get_allocation


添加:

我为牛至实现了它,所以对于那些喜欢真正源代码 (GPLv2.0) 的人,请参阅 https://github.com/drahnr/oregano/blob/master/src/sheet/sheet.c 上的函数 sheet_change_zoom

关于c - goocanvas - 坐标系和缩放到光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15212219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com