Some more cleanup and cap line length to 75 character column.
This commit is contained in:
96
main.c
96
main.c
@@ -56,7 +56,7 @@ U64 file_size(FILE *f)
|
|||||||
{
|
{
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
U64 size = ftell(f);
|
U64 size = ftell(f);
|
||||||
fseek(f, 0, SEEK_SET); // TODO: NOTE: Save the current position, instead?
|
fseek(f, 0, SEEK_SET); // TODO: Save the current position, instead?
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,16 +65,21 @@ U64 now()
|
|||||||
return (U64)time(NULL);
|
return (U64)time(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
B32 activity_button(Rectangle rect, char *title, char *subtitle, F32 font_size, Color font_color, Color background_color)
|
B32 activity_button(Rectangle rect, char *title, char *subtitle,
|
||||||
|
F32 font_size, Color font_color,
|
||||||
|
Color background_color)
|
||||||
{
|
{
|
||||||
B32 button_pressed = false;
|
B32 button_pressed = false;
|
||||||
|
|
||||||
|
// WARNING: Substracting from font size like that is unsafe.
|
||||||
F32 subtitle_font_size = font_size - 8.0f;
|
F32 subtitle_font_size = font_size - 8.0f;
|
||||||
if(subtitle_font_size <= 0.0f)
|
if(subtitle_font_size <= 0.0f)
|
||||||
subtitle_font_size = font_size;
|
subtitle_font_size = font_size;
|
||||||
|
|
||||||
Vector2 title_font_d = MeasureTextEx(state.font, title, (float)font_size, 2);
|
Vector2 title_font_d =
|
||||||
Vector2 subtitle_font_d = MeasureTextEx(state.font, subtitle, (float)subtitle_font_size, 2);
|
MeasureTextEx(state.font, title, (float)font_size, 2);
|
||||||
|
Vector2 subtitle_font_d =
|
||||||
|
MeasureTextEx(state.font, subtitle, (float)subtitle_font_size, 2);
|
||||||
|
|
||||||
if(CheckCollisionPointRec(state.mouse_pos, rect)) {
|
if(CheckCollisionPointRec(state.mouse_pos, rect)) {
|
||||||
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||||
@@ -83,15 +88,15 @@ B32 activity_button(Rectangle rect, char *title, char *subtitle, F32 font_size,
|
|||||||
|
|
||||||
DrawRectangleRounded(rect, 0.2f, 100, background_color);
|
DrawRectangleRounded(rect, 0.2f, 100, background_color);
|
||||||
DrawTextEx(state.font, title,
|
DrawTextEx(state.font, title,
|
||||||
(Vector2){
|
(Vector2){
|
||||||
(rect.x + rect.width / 2.0f - title_font_d.x / 2.0f),
|
(rect.x + rect.width / 2.0f - title_font_d.x / 2.0f),
|
||||||
(rect.y + rect.height / 2.0f - title_font_d.y / 2.0f)
|
(rect.y + rect.height / 2.0f - title_font_d.y / 2.0f)
|
||||||
}, font_size, 2, font_color);
|
}, font_size, 2, font_color);
|
||||||
DrawTextEx(state.font, subtitle,
|
DrawTextEx(state.font, subtitle,
|
||||||
(Vector2){
|
(Vector2){
|
||||||
(rect.x + rect.width / 2.0f - subtitle_font_d.x / 2.0f),
|
(rect.x + rect.width / 2.0f - subtitle_font_d.x / 2.0f),
|
||||||
(rect.y + rect.height / 2.0f - subtitle_font_d.y / 2.0f) + 20.0f
|
(rect.y + rect.height / 2.0f - subtitle_font_d.y / 2.0f) + 20.0f
|
||||||
}, subtitle_font_size, 2, font_color); // WARNING: Substracting from font size like that is unsafe.
|
}, subtitle_font_size, 2, font_color);
|
||||||
|
|
||||||
return button_pressed;
|
return button_pressed;
|
||||||
}
|
}
|
||||||
@@ -150,7 +155,8 @@ Hours break_time(U64 seconds)
|
|||||||
Hours hours;
|
Hours hours;
|
||||||
hours.hours = seconds / 60 / 60;
|
hours.hours = seconds / 60 / 60;
|
||||||
hours.minutes = seconds / 60 - (60 * hours.hours);
|
hours.minutes = seconds / 60 - (60 * hours.hours);
|
||||||
hours.seconds = seconds - ((hours.hours * 60 * 60) + (hours.minutes * 60));
|
hours.seconds =
|
||||||
|
seconds - ((hours.hours * 60 * 60) + (hours.minutes * 60));
|
||||||
return hours;
|
return hours;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,8 +196,10 @@ int main(int argc, char *argv[])
|
|||||||
char file_buffer[256];
|
char file_buffer[256];
|
||||||
|
|
||||||
strftime(date_buffer, sizeof(date_buffer), "%Y_%m_%d", t);
|
strftime(date_buffer, sizeof(date_buffer), "%Y_%m_%d", t);
|
||||||
snprintf(dir_buffer, sizeof(dir_buffer), "%s/.timetracker", get_home_dir());
|
snprintf(dir_buffer, sizeof(dir_buffer), "%s/.timetracker",
|
||||||
snprintf(file_buffer, sizeof(file_buffer), "%s/%s", dir_buffer, date_buffer);
|
get_home_dir());
|
||||||
|
snprintf(file_buffer, sizeof(file_buffer), "%s/%s", dir_buffer,
|
||||||
|
date_buffer);
|
||||||
|
|
||||||
if(!is_directory(dir_buffer)) {
|
if(!is_directory(dir_buffer)) {
|
||||||
mkdir(dir_buffer, 0700); // WARNING: Might fail, what then?
|
mkdir(dir_buffer, 0700); // WARNING: Might fail, what then?
|
||||||
@@ -203,7 +211,8 @@ int main(int argc, char *argv[])
|
|||||||
if(path_exists(file_buffer)) {
|
if(path_exists(file_buffer)) {
|
||||||
f = fopen(file_buffer, "r");
|
f = fopen(file_buffer, "r");
|
||||||
if(!f) {
|
if(!f) {
|
||||||
fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer, strerror(errno));
|
fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer,
|
||||||
|
strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -221,7 +230,8 @@ int main(int argc, char *argv[])
|
|||||||
char activity_name[64];
|
char activity_name[64];
|
||||||
U64 lower;
|
U64 lower;
|
||||||
U64 upper;
|
U64 upper;
|
||||||
int l = sscanf(line, "%s %llu %llu", activity_name, &lower, &upper);
|
int l =
|
||||||
|
sscanf(line, "%s %llu %llu", activity_name, &lower, &upper);
|
||||||
if(l != 3)
|
if(l != 3)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -243,13 +253,12 @@ int main(int argc, char *argv[])
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
activities[current_activity].type = type;
|
activities[current_activity].type = type;
|
||||||
activities[current_activity].color = activity_type_color_representation[type];
|
activities[current_activity].color =
|
||||||
|
activity_type_color_representation[type];
|
||||||
activities[current_activity].began = lower;
|
activities[current_activity].began = lower;
|
||||||
activities[current_activity].ended = upper;
|
activities[current_activity].ended = upper;
|
||||||
|
|
||||||
current_activity++;
|
current_activity++;
|
||||||
|
|
||||||
printf("Added: %s\n", activity_name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,7 +280,8 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
SetTargetFPS(30);
|
SetTargetFPS(30);
|
||||||
|
|
||||||
state.font = LoadFontFromMemory(".ttf", sourcecodepro_ttf, sourcecodepro_ttf_len, 96, NULL, 0);
|
state.font = LoadFontFromMemory(".ttf", sourcecodepro_ttf,
|
||||||
|
sourcecodepro_ttf_len, 96, NULL, 0);
|
||||||
if(!IsFontValid(state.font)) {
|
if(!IsFontValid(state.font)) {
|
||||||
fprintf(stderr, "Unable to load font\n");
|
fprintf(stderr, "Unable to load font\n");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -300,6 +310,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
state.mouse_pos = GetMousePosition();
|
state.mouse_pos = GetMousePosition();
|
||||||
|
|
||||||
|
// TODO: Switch activities with number keys.
|
||||||
if(IsKeyPressed(KEY_Q))
|
if(IsKeyPressed(KEY_Q))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -314,32 +325,47 @@ int main(int argc, char *argv[])
|
|||||||
DrawRectangle(0, 0, window_w, 40, GRAY);
|
DrawRectangle(0, 0, window_w, 40, GRAY);
|
||||||
|
|
||||||
for(U32 i = 0; i <= current_activity; i++) {
|
for(U32 i = 0; i <= current_activity; i++) {
|
||||||
// if((activities[i].ended_on - started_on_ms) <= (ended_on_ms - started_on_ms)) {
|
F32 start_x =
|
||||||
F32 start_x = floor((F32)(activities[i].began - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s) * (F32)window_w);
|
floor((F32)(activities[i].began - lower_bound_s) /
|
||||||
F32 end_x = floor(((F32)(activities[i].ended - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s)) * window_w);
|
(F32)(upper_bound_s - lower_bound_s) * (F32)window_w);
|
||||||
// DrawRectangleRounded((Rectangle){ start_x, 0, end_x - start_x, 40}, 0.0f, 100, activities[i].color);
|
F32 end_x =
|
||||||
DrawRectangle(start_x, 0, end_x - start_x, 40, activities[i].color);
|
floor(((F32)(activities[i].ended - lower_bound_s) /
|
||||||
// }
|
(F32)(upper_bound_s - lower_bound_s)) * window_w);
|
||||||
|
DrawRectangle(start_x, 0, end_x - start_x, 40,
|
||||||
|
activities[i].color);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: CLEAN UP.
|
// TODO: CLEAN UP.
|
||||||
F32 width = (window_w-padding_x*(float)(activity_type_COUNT+1)) / (float)activity_type_COUNT;
|
F32 width =
|
||||||
|
(window_w-padding_x*(float)(activity_type_COUNT+1)) /
|
||||||
|
(float)activity_type_COUNT;
|
||||||
|
|
||||||
for(U32 i = 0; i <= current_activity; i++) {
|
for(U32 i = 0; i <= current_activity; i++) {
|
||||||
activities_stats[activities[i].type].total_seconds += activities[i].ended - activities[i].began;
|
activities_stats[activities[i].type].total_seconds +=
|
||||||
|
activities[i].ended - activities[i].began;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(U32 i = 0; i < activity_type_COUNT; i ++) {
|
for(U32 i = 0; i < activity_type_COUNT; i ++) {
|
||||||
Hours time = break_time(activities_stats[i].total_seconds);
|
Hours time = break_time(activities_stats[i].total_seconds);
|
||||||
snprintf(activities_stats[i].seconds_str, sizeof(activities_stats[i].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);
|
snprintf(activities_stats[i].seconds_str,
|
||||||
|
sizeof(activities_stats[i].seconds_str),
|
||||||
|
"%02llu:%02llu:%02llu",
|
||||||
|
time.hours, time.minutes, time.seconds);
|
||||||
|
|
||||||
Rectangle rect = { (padding_x*(i+1))+(width*i), 60, width, (window_h-70) };
|
Rectangle rect =
|
||||||
if(activity_button(rect, activity_type_string_representation[i], activities_stats[i].seconds_str, font_size, (activities[current_activity].type == i ? WHITE : BLACK), activity_type_color_representation[i])) {
|
{ (padding_x*(i+1))+(width*i), 60, width, (window_h-70) };
|
||||||
|
if(activity_button(rect,
|
||||||
|
activity_type_string_representation[i],
|
||||||
|
activities_stats[i].seconds_str, font_size,
|
||||||
|
(activities[current_activity].type == i ? WHITE : BLACK),
|
||||||
|
activity_type_color_representation[i]))
|
||||||
|
{
|
||||||
if(activities[current_activity].type != i) {
|
if(activities[current_activity].type != i) {
|
||||||
current_activity++;
|
current_activity++;
|
||||||
|
|
||||||
activities[current_activity].type = i;
|
activities[current_activity].type = i;
|
||||||
activities[current_activity].color = activity_type_color_representation[i];
|
activities[current_activity].color =
|
||||||
|
activity_type_color_representation[i];
|
||||||
activities[current_activity].began = now_s;
|
activities[current_activity].began = now_s;
|
||||||
activities[current_activity].ended = now_s;
|
activities[current_activity].ended = now_s;
|
||||||
}
|
}
|
||||||
@@ -368,7 +394,9 @@ int main(int argc, char *argv[])
|
|||||||
activities[i].ended);
|
activities[i].ended);
|
||||||
|
|
||||||
size_t write_buffer_len = strlen(write_buffer);
|
size_t write_buffer_len = strlen(write_buffer);
|
||||||
if(fwrite(write_buffer, 1, write_buffer_len, f) != write_buffer_len) {
|
if(fwrite(write_buffer, 1, write_buffer_len, f) !=
|
||||||
|
write_buffer_len)
|
||||||
|
{
|
||||||
perror("fwrite");
|
perror("fwrite");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user