|
Packit |
1470ea |
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
Packit |
1470ea |
#include <clutter/clutter.h>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
#define STAGE_WIDTH 800
|
|
Packit |
1470ea |
#define STAGE_HEIGHT 600
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
#define THUMBNAIL_SIZE 200
|
|
Packit |
1470ea |
#define ROW_COUNT (STAGE_HEIGHT / THUMBNAIL_SIZE)
|
|
Packit |
1470ea |
#define COL_COUNT (STAGE_WIDTH / THUMBNAIL_SIZE)
|
|
Packit |
1470ea |
#define THUMBNAIL_COUNT (ROW_COUNT * COL_COUNT)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
#define ANIMATION_DURATION_MS 500
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
#define IMAGE_DIR_PATH "./berlin_images/"
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
static GPtrArray *img_paths;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
static ClutterPoint unfocused_pos;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
static void
|
|
Packit |
1470ea |
load_image_path_names()
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
/* Ensure we can access the directory. */
|
|
Packit |
1470ea |
GError *error = NULL;
|
|
Packit |
1470ea |
GDir *dir = g_dir_open(IMAGE_DIR_PATH, 0, &error);
|
|
Packit |
1470ea |
if(error)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
g_warning("g_dir_open() failed with error: %s\n", error->message);
|
|
Packit |
1470ea |
g_clear_error(&error);
|
|
Packit |
1470ea |
return;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
img_paths = g_ptr_array_new_with_free_func (g_free);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const gchar *filename = g_dir_read_name(dir);
|
|
Packit |
1470ea |
while(filename)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
if(g_str_has_suffix(filename, ".jpg") || g_str_has_suffix(filename, ".png"))
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
gchar *path = g_build_filename(IMAGE_DIR_PATH, filename, NULL);
|
|
Packit |
1470ea |
g_ptr_array_add (img_paths, path);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
filename = g_dir_read_name(dir);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
static gboolean
|
|
Packit |
1470ea |
actor_clicked_cb(ClutterActor *actor,
|
|
Packit |
1470ea |
ClutterEvent *event,
|
|
Packit |
1470ea |
gpointer user_data)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
/* Flag to keep track of our state. */
|
|
Packit |
1470ea |
static gboolean is_focused = FALSE;
|
|
Packit |
1470ea |
ClutterActorIter iter;
|
|
Packit |
1470ea |
ClutterActor *child;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Reset the focus state on all the images */
|
|
Packit |
1470ea |
clutter_actor_iter_init (&iter, clutter_actor_get_parent(actor));
|
|
Packit |
1470ea |
while (clutter_actor_iter_next(&iter, &child))
|
|
Packit |
1470ea |
clutter_actor_set_reactive(child, is_focused);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
clutter_actor_save_easing_state(actor);
|
|
Packit |
1470ea |
clutter_actor_set_easing_duration(actor, ANIMATION_DURATION_MS);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if(is_focused)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
/* Restore the old location and size. */
|
|
Packit |
1470ea |
clutter_actor_set_position(actor, unfocused_pos.x, unfocused_pos.y);
|
|
Packit |
1470ea |
clutter_actor_set_size(actor, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
/* Save the current location before animating. */
|
|
Packit |
1470ea |
clutter_actor_get_position(actor, &unfocused_pos.x, &unfocused_pos.y);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Only the currently focused image should receive to events. */
|
|
Packit |
1470ea |
clutter_actor_set_reactive(actor, TRUE);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Put the focused image on top. */
|
|
Packit |
1470ea |
clutter_actor_set_child_above_sibling(clutter_actor_get_parent(actor), actor, NULL);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
clutter_actor_set_position(actor, (STAGE_WIDTH - STAGE_HEIGHT) / 2.0, 0);
|
|
Packit |
1470ea |
clutter_actor_set_size(actor, STAGE_HEIGHT, STAGE_HEIGHT);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
clutter_actor_restore_easing_state(actor);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Toggle our flag. */
|
|
Packit |
1470ea |
is_focused = !is_focused;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return TRUE;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* This function handles setting up and placing the rectangles. */
|
|
Packit |
1470ea |
static void
|
|
Packit |
1470ea |
initialize_actor(ClutterActor *actor, guint row, guint col)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
clutter_actor_set_size(actor, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
|
|
Packit |
1470ea |
clutter_actor_set_position(actor, col * THUMBNAIL_SIZE, row * THUMBNAIL_SIZE);
|
|
Packit |
1470ea |
clutter_actor_set_reactive(actor, TRUE);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
g_signal_connect(actor,
|
|
Packit |
1470ea |
"button-press-event",
|
|
Packit |
1470ea |
G_CALLBACK(actor_clicked_cb),
|
|
Packit |
1470ea |
NULL);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
int
|
|
Packit |
1470ea |
main(int argc, char *argv[])
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
ClutterColor stage_color = { 16, 16, 16, 255 };
|
|
Packit |
1470ea |
ClutterActor *stage = NULL;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
|
|
Packit |
1470ea |
return 1;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
stage = clutter_stage_new();
|
|
Packit |
1470ea |
clutter_actor_set_size(stage, STAGE_WIDTH, STAGE_HEIGHT);
|
|
Packit |
1470ea |
clutter_actor_set_background_color(stage, &stage_color);
|
|
Packit |
1470ea |
clutter_stage_set_title(CLUTTER_STAGE (stage), "Photo Wall");
|
|
Packit |
1470ea |
g_signal_connect(stage, "destroy", G_CALLBACK(clutter_main_quit), NULL);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
load_image_path_names();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
guint row = 0;
|
|
Packit |
1470ea |
guint col = 0;
|
|
Packit |
1470ea |
for(row=0; row < ROW_COUNT; ++row)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
for(col=0; col < COL_COUNT; ++col)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
const char *img_path = g_ptr_array_index(img_paths, (row * COL_COUNT) + col);
|
|
Packit |
1470ea |
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(img_path, STAGE_HEIGHT, STAGE_HEIGHT, NULL);
|
|
Packit |
1470ea |
ClutterContent *image = clutter_image_new ();
|
|
Packit |
1470ea |
ClutterActor *actor = clutter_actor_new ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (pixbuf != NULL)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
clutter_image_set_data(CLUTTER_IMAGE(image),
|
|
Packit |
1470ea |
gdk_pixbuf_get_pixels(pixbuf),
|
|
Packit |
1470ea |
gdk_pixbuf_get_has_alpha(pixbuf)
|
|
Packit |
1470ea |
? COGL_PIXEL_FORMAT_RGBA_8888
|
|
Packit |
1470ea |
: COGL_PIXEL_FORMAT_RGB_888,
|
|
Packit |
1470ea |
gdk_pixbuf_get_width(pixbuf),
|
|
Packit |
1470ea |
gdk_pixbuf_get_height(pixbuf),
|
|
Packit |
1470ea |
gdk_pixbuf_get_rowstride(pixbuf),
|
|
Packit |
1470ea |
NULL);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
clutter_actor_set_content(actor, image);
|
|
Packit |
1470ea |
g_object_unref(image);
|
|
Packit |
1470ea |
g_object_unref(pixbuf);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
initialize_actor(actor, row, col);
|
|
Packit |
1470ea |
clutter_actor_add_child(stage, actor);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Show the stage. */
|
|
Packit |
1470ea |
clutter_actor_show(stage);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Start the clutter main loop. */
|
|
Packit |
1470ea |
clutter_main();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
g_ptr_array_unref(img_paths);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return 0;
|
|
Packit |
1470ea |
}
|