Commit 70169516 authored by Francois Techene's avatar Francois Techene

Added a second dice

parent 7893df65
......@@ -44,7 +44,7 @@
"sources" : [
{
"type" : "git",
"url" : "file:///home/feeef/dev/randomizer"
"url" : "https://source.puri.sm/francois.techene/randomizer.git"
}
]
}
......
......@@ -18,6 +18,7 @@
#include "randomizer-config.h"
#include "randomizer-window.h"
#include <sys/time.h>
struct _RandomizerWindow
{
......@@ -27,19 +28,49 @@ struct _RandomizerWindow
HdyViewSwitcherTitle *switcher_title;
GtkLabel *boolean_label;
GtkImage *dice_image;
GtkImage *dice_image_2;
};
G_DEFINE_TYPE (RandomizerWindow, randomizer_window, HDY_TYPE_WINDOW)
/***********************************************************
* Randomize function
*
*/
static int
get_randomized_number_with_range (int lower,
int upper)
{
srand((unsigned)time(NULL));
int num = (rand() %(upper - lower + 1)) + lower;
struct timeval tm;
gettimeofday(&tm, NULL);
srandom(tm.tv_sec + tm.tv_usec * 1000000ul);
int num = (random() %(upper - lower + 1)) + lower;
return num;
}
static void
randomize_dice_image (GtkImage *dice_image)
{
int num = get_randomized_number_with_range(1,6);
char image_name[6];
sprintf(image_name, "dice-%d", num);
gtk_image_set_from_icon_name (dice_image,
image_name,
GTK_ICON_SIZE_INVALID);
}
/***********************************************************
* Callbacks
*
*/
static void
boolean_randomize_clicked_cb (GtkButton *btn,
......@@ -58,22 +89,31 @@ boolean_randomize_clicked_cb (GtkButton *btn,
static void
dice_randomize_clicked_cb (GtkButton *btn,
RandomizerWindow *self)
RandomizerWindow *self)
{
int num = get_randomized_number_with_range(1,6);
char image_name[6];
sprintf(image_name, "dice-%d", num);
randomize_dice_image(self->dice_image);
randomize_dice_image(self->dice_image_2);
}
gtk_image_set_from_icon_name (self->dice_image,
image_name,
GTK_ICON_SIZE_INVALID);
//gtk_label_set_text(self->dice_label, str);
static void
dice_number_changed_cb (GtkSpinButton *spinbutton,
RandomizerWindow *self)
{
int nb_dices = gtk_spin_button_get_value_as_int (spinbutton);
if (nb_dices > 1) {
gtk_widget_show( GTK_WIDGET(self->dice_image_2) );
}
else {
gtk_widget_hide( GTK_WIDGET(self->dice_image_2) );
}
}
/***********************************************************
* Init
*
*/
static void
randomizer_window_class_init (RandomizerWindowClass *klass)
......@@ -84,14 +124,18 @@ randomizer_window_class_init (RandomizerWindowClass *klass)
gtk_widget_class_bind_template_child (widget_class, RandomizerWindow, switcher_title);
gtk_widget_class_bind_template_child (widget_class, RandomizerWindow, boolean_label);
gtk_widget_class_bind_template_child (widget_class, RandomizerWindow, dice_image);
gtk_widget_class_bind_template_child (widget_class, RandomizerWindow, dice_image_2);
gtk_widget_class_bind_template_callback (widget_class, boolean_randomize_clicked_cb);
gtk_widget_class_bind_template_callback (widget_class, dice_randomize_clicked_cb);
gtk_widget_class_bind_template_callback (widget_class, dice_number_changed_cb);
}
static void
randomizer_window_init (RandomizerWindow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
gtk_widget_hide( GTK_WIDGET(self->dice_image_2) );
}
RandomizerWindow *
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment