4

我正在尝试在goocanvas位于 a 内的 a内实现对指针/光标的缩放scrolledwindow。不幸的是,有关使用的坐标系的文档不是最佳的。

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

便利结构

 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 画布比例无关。在将比例应用于画布之前之后,根据指针位置(以像素为单位)计算 goocanvas 位置。不同之处在于指针在缩放后的位置和它应该在的位置之间的偏移量(我们希望夹在画布位置)。在画布坐标中获取画布的左上角,应用偏移量并滚动到新的左上角位置(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 问题,但指针并未固定在 goo 坐标上。所以我的猜测是,我需要一个辅助校正因子,或者我在现有代码中有一个错误。

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

 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 示例的指针。

4

1 回答 1

0

我最终通过查看 goocanvas 源来修复它,看看他们是如何通过实际滚动来中心魔法的。据此,我将画布指针位置移动到中心,缩放并将其移回。这里重要的是,向后移动只是像素位置(这里:只是开始时指针和中心之间的像素差异!)除以累积的缩放因子!(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.csheet_change_zoom上的函数

于 2013-03-10T01:36:35.500 回答