Commit 70169516 authored by Francois Techene's avatar Francois Techene
Browse files

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 *
......
......@@ -33,91 +33,141 @@
<property name="can_focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="vexpand">True</property>
<property name="hexpand">True</property>
<property name="spacing">24</property>
<child>
<object class="GtkLabel" id="boolean_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes"></property>
<property name="justify">center</property>
<property name="wrap">True</property>
<property name="opacity">1</property>
<attributes>
<attribute name="weight" value="bold"/>
<attribute name="scale" value="2"/>
</attributes>
</object>
</child>
<child>
<object class="GtkLabel" id="boolean_description_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Yes or No ?</property>
<property name="justify">center</property>
<property name="wrap">True</property>
<property name="opacity">0.5</property>
<attributes>
<attribute name="weight" value="normal"/>
<attribute name="scale" value="1"/>
</attributes>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Decide</property>
<property name="use-underline">True</property>
<signal name="clicked" handler="boolean_randomize_clicked_cb" swapped="no"/>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
</object>
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="vexpand">True</property>
<property name="hexpand">True</property>
<property name="spacing">24</property>
<child>
<object class="GtkLabel" id="boolean_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes"></property>
<property name="justify">center</property>
<property name="wrap">True</property>
<property name="opacity">1</property>
<attributes>
<attribute name="weight" value="bold"/>
<attribute name="scale" value="2"/>
</attributes>
</object>
</child>
<child>
<object class="GtkLabel" id="boolean_description_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Yes or No ?</property>
<property name="justify">center</property>
<property name="wrap">True</property>
<property name="opacity">0.5</property>
<attributes>
<attribute name="weight" value="normal"/>
<attribute name="scale" value="1"/>
</attributes>
</object>
</child>
<child>
<object class="GtkButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Decide</property>
<property name="use-underline">True</property>
<signal name="clicked" handler="boolean_randomize_clicked_cb" swapped="no"/>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
</object>
<packing>
<property name="name">page1</property>
<property name="title" translatable="yes">Yes/No</property>
<property name="icon_name">dialog-question-symbolic</property>
</packing>
</child>
<!-- Dices Tab -->
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="vexpand">True</property>
<property name="hexpand">True</property>
<property name="spacing">24</property>
<child>
<object class="GtkImage" id="dice_image">
<property name="visible">True</property>
<property name="pixel-size">128</property>
</object>
</child>
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="vexpand">True</property>
<property name="hexpand">True</property>
<property name="spacing">24</property>
<!-- Dices Images -->
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="orientation">horizontal</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="vexpand">True</property>
<property name="hexpand">True</property>
<property name="spacing">24</property>
<child>
<object class="GtkImage" id="dice_image">
<property name="icon_name">dice-1</property>
<property name="visible">True</property>
<property name="pixel-size">128</property>
</object>
</child>
<child>
<object class="GtkImage" id="dice_image_2">
<property name="icon_name">dice-1</property>
<property name="visible">True</property>
<property name="pixel-size">128</property>
</object>
</child>
</object>
</child>
<!-- End Dices Images -->
<child>
<object class="GtkButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Throw the dice</property>
<property name="use-underline">True</property>
<signal name="clicked" handler="dice_randomize_clicked_cb" swapped="no"/>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
</object>
<!-- Dices Button -->
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="vexpand">True</property>
<property name="hexpand">True</property>
<property name="spacing">24</property>
<child>
<object class="GtkButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Roll</property>
<property name="use-underline">True</property>
<signal name="clicked" handler="dice_randomize_clicked_cb" swapped="no"/>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
<child>
<object class="GtkSpinButton">
<property name="adjustment">dice_number_adjustment</property>
<property name="valign">center</property>
<property name="visible">True</property>
<signal name="value-changed" handler="dice_number_changed_cb" swapped="no"/>
</object>
</child>
</object>
</child>
<!-- End Dices Button -->
</object>
<packing>
<property name="name">page2</property>
<property name="title" translatable="yes">Dice</property>
......@@ -125,6 +175,8 @@
<property name="position">1</property>
</packing>
</child>
<!-- End Dices Tab -->
</object>
<packing>
<property name="expand">True</property>
......@@ -142,4 +194,12 @@
</object>
</child>
</template>
<object class="GtkAdjustment" id="dice_number_adjustment">
<property name="lower">1</property>
<property name="upper">2</property>
<property name="value">1</property>
<property name="page-increment">1</property>
<property name="step-increment">1</property>
</object>
</interface>
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