GTK窗口移动动画? [英] GTK window motion animation?

查看:243
本文介绍了GTK窗口移动动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要自动将我在屏幕上GTK_WINDOW。目前,我有它以平局/移动循环,但这是非常不稳定。我很新的GTK程序(和一般的GUI编程)。我缺少什么?

I want to move my GTK_WINDOW across the screen automatically. Currently I have it in a draw/move loop, but that's terribly choppy. I'm very new to GTK programming (and gui programming in general). What am I missing?

推荐答案

您还没有说你想要的窗口,要遵循什么样的道路。如果路径是时间一些简单的功能 - 也就是说,如果你有办法计算,你希望窗口在任何给定的时间 - 你可以尝试在以下code所示的方法。对于本例中的相当简单的菜单,它的工作原理确定我的Linux系统上,并产生相当平滑的运动。

You haven't said what sort of path you want the window to follow. If the path is some simple function of time -- that is, if you have a way to compute where you want the window to be at any given time -- you could try the method illustrated in following code. For the quite-simple menu in the example, it works ok on my Linux system and produces fairly smooth motion.

要该方法的关键是,而不是移动窗口每个定时器事件的给定距离,它找到了当前时间和窗口移至它应该在的位置,在那个时候。从而,运动的速度的时间导数应该是恒定的,这避免了即使定时器事件不规则地出现参差不齐的或不连贯的运动。 (如<著名href=\"http://developer.gnome.org/glib/unstable/glib-The-Main-Event-Loop.html#g-timeout-add\">g-timeout-add()说明,极易发生不规则。)

The key to the method is that instead of moving the window a given distance per timer event, it finds out the current time and moves the window to the location it should be at, at that time. Thus, the time derivative of speed of motion should be constant, which avoids ragged or choppy motion even if timer events occur irregularly. (As noted in g-timeout-add() description, irregularity can easily occur.)

在这个例子中,该路径是从窗口的左上方向左下方和背面,反复进行。常量'半场'在timerEvent()控制需要多长时间从角落移到角落。在g_timeout_add()调用的常数3计时器的时间间隔设置为0.003秒,每秒约333移动(MPS)。 (您可能想尝试更合理的价格,如20,30,40,等MPS;我用3号,因为我没有抬头看<一个href=\"http://developer.gnome.org/glib/unstable/glib-The-Main-Event-Loop.html#g-timeout-add\">g-timeout-add()在使用它之前,并承担延迟是秒hundreths,约33 MPS,而不是毫秒,约333 MPS)。如果你的窗口内容是相当复杂的,更少的MPS将是实用。另外,我尝试了一些速度较慢,并得到了更多的抖动现象的IM pression的。

In this example, the path is from top left of window to bottom left and back, repeatedly. The constant 'HalfTime' in timerEvent() controls how long it takes to move from corner to corner. The constant 3 in the g_timeout_add() call sets the timer interval to 0.003 seconds, or about 333 moves per second (MPS). (You may want to try more-reasonable rates, such as 20, 30, 40, etc MPS; I used the number 3 because I didn't look up g-timeout-add() before using it, and assumed the delay was hundreths of seconds, for about 33 MPS, rather than milliseconds, for about 333 MPS.) If your window contents are quite complex, fewer MPS will be practical. Also, I tried some slower rates and got more of an impression of choppiness.

/* $Id: app12.c $
 Re: animating position of a top-level Gtk window
 jiw July 2011 -- Offered without warranty under GPL v3
 terms per http://www.gnu.org/licenses/gpl.html  */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <gtk/gtk.h>
typedef struct DATA { GTimer *timer; GtkWidget *window; int w, h; }
  DataStruct;

gboolean timerEvent(void *dataset) {
  enum { HalfTime=8, CycTime=2*HalfTime };
  gulong micros;
  DataStruct *data =dataset;
  double t = fabs(fmod (g_timer_elapsed (data->timer, &micros), CycTime));
  int x = (t*data->w)/HalfTime, y = (t*data->h)/HalfTime;
  gtk_window_move (GTK_WINDOW(data->window),
       t<HalfTime? x : 2*data->w-x, t<HalfTime? y : 2*data->h-y);
  return TRUE; /* Keep timeout running */
}

int main(int argc, char **argv) {
  GtkWidget *vbox, *b;
  GdkScreen *gds;
  DataStruct data;
  data.timer = g_timer_new();
  gtk_init (&argc, &argv);
  data.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW(data.window), 200, 150);
  g_signal_connect (G_OBJECT(data.window), "destroy",
            G_CALLBACK(gtk_main_quit), NULL);
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER(data.window), vbox);
  b = gtk_button_new_with_label ("Click to Exit");
  gtk_box_pack_start (GTK_BOX(vbox), b, TRUE, TRUE, TRUE);
  g_signal_connect (b, "clicked", G_CALLBACK(gtk_main_quit), NULL);
  gtk_widget_show_all (data.window);

  gds = gdk_screen_get_default ();  /* Get pointer to screen  */
  data.w = gdk_screen_get_width (gds);  /* Find out screen width  */
  data.h = gdk_screen_get_height (gds); /* Find out screen height */
  printf ("Screen size = %d by %d\n", data.w, data.h); fflush(stdout);
  g_timeout_add(3, timerEvent, &data);  /* Create .003 sec timer   */
  gtk_main();
  return (0);
}

这篇关于GTK窗口移动动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆