forked from 3snowp7im/urn
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimer.c
115 lines (96 loc) · 3.41 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "urn-component.h"
typedef struct _UrnTimer {
UrnComponent base;
GtkWidget *time;
GtkWidget *time_seconds;
GtkWidget *time_millis;
} UrnTimer;
extern UrnComponentOps urn_timer_operations;
UrnComponent *urn_component_timer_new() {
UrnTimer *self;
GtkWidget *spacer;
self = malloc(sizeof(UrnTimer));
if (!self) return NULL;
self->base.ops = &urn_timer_operations;
self->time = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
add_class(self->time, "timer");
add_class(self->time, "time");
gtk_widget_show(self->time);
spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_set_hexpand(spacer, TRUE);
gtk_container_add(GTK_CONTAINER(self->time), spacer);
gtk_widget_show(spacer);
self->time_seconds = gtk_label_new(NULL);
add_class(self->time_seconds, "timer-seconds");
gtk_widget_set_valign(self->time_seconds, GTK_ALIGN_BASELINE);
gtk_container_add(GTK_CONTAINER(self->time), self->time_seconds);
gtk_widget_show(self->time_seconds);
spacer = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_valign(spacer, GTK_ALIGN_END);
gtk_container_add(GTK_CONTAINER(self->time), spacer);
gtk_widget_show(spacer);
self->time_millis = gtk_label_new(NULL);
add_class(self->time_millis, "timer-millis");
gtk_widget_set_valign(self->time_millis, GTK_ALIGN_BASELINE);
gtk_container_add(GTK_CONTAINER(spacer), self->time_millis);
gtk_widget_show(self->time_millis);
return (UrnComponent *)self;
}
// Avoid collision with timer_delete of time.h
static void urn_timer_delete(UrnComponent *self) {
free(self);
}
static GtkWidget *timer_widget(UrnComponent *self) {
return ((UrnTimer *)self)->time;
}
static void timer_clear_game(UrnComponent *self_) {
UrnTimer *self = (UrnTimer *)self_;
gtk_label_set_text(GTK_LABEL(self->time_seconds), "");
gtk_label_set_text(GTK_LABEL(self->time_millis), "");
remove_class(self->time, "behind");
remove_class(self->time, "losing");
}
static void timer_draw(UrnComponent *self_, urn_game *game, urn_timer *timer) {
UrnTimer *self = (UrnTimer *)self_;
char str[256], millis[256];
int curr;
curr = timer->curr_split;
if (curr == game->split_count) {
--curr;
}
remove_class(self->time, "delay");
remove_class(self->time, "behind");
remove_class(self->time, "losing");
remove_class(self->time, "best-split");
if (curr == game->split_count) {
curr = game->split_count - 1;
}
if (timer->time <= 0) {
add_class(self->time, "delay");
} else {
if (timer->curr_split == game->split_count
&& timer->split_info[curr]
& URN_INFO_BEST_SPLIT) {
add_class(self->time, "best-split");
} else{
if (timer->split_info[curr]
& URN_INFO_BEHIND_TIME) {
add_class(self->time, "behind");
}
if (timer->split_info[curr]
& URN_INFO_LOSING_TIME) {
add_class(self->time, "losing");
}
}
}
urn_time_millis_string(str, &millis[1], timer->time);
millis[0] = '.';
gtk_label_set_text(GTK_LABEL(self->time_seconds), str);
gtk_label_set_text(GTK_LABEL(self->time_millis), millis);
}
UrnComponentOps urn_timer_operations = {
.delete = urn_timer_delete,
.widget = timer_widget,
.clear_game = timer_clear_game,
.draw = timer_draw
};