diff --git a/main.c b/main.c index 5d9d003..d4f0790 100644 --- a/main.c +++ b/main.c @@ -56,7 +56,7 @@ U64 file_size(FILE *f) { fseek(f, 0, SEEK_END); 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; } @@ -65,16 +65,21 @@ U64 now() 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; + // WARNING: Substracting from font size like that is unsafe. F32 subtitle_font_size = font_size - 8.0f; if(subtitle_font_size <= 0.0f) subtitle_font_size = font_size; - Vector2 title_font_d = MeasureTextEx(state.font, title, (float)font_size, 2); - Vector2 subtitle_font_d = MeasureTextEx(state.font, subtitle, (float)subtitle_font_size, 2); + Vector2 title_font_d = + 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(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); DrawTextEx(state.font, title, - (Vector2){ - (rect.x + rect.width / 2.0f - title_font_d.x / 2.0f), - (rect.y + rect.height / 2.0f - title_font_d.y / 2.0f) - }, font_size, 2, font_color); + (Vector2){ + (rect.x + rect.width / 2.0f - title_font_d.x / 2.0f), + (rect.y + rect.height / 2.0f - title_font_d.y / 2.0f) + }, font_size, 2, font_color); DrawTextEx(state.font, subtitle, - (Vector2){ - (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 - }, subtitle_font_size, 2, font_color); // WARNING: Substracting from font size like that is unsafe. + (Vector2){ + (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 + }, subtitle_font_size, 2, font_color); return button_pressed; } @@ -150,7 +155,8 @@ Hours break_time(U64 seconds) Hours hours; hours.hours = seconds / 60 / 60; 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; } @@ -190,8 +196,10 @@ int main(int argc, char *argv[]) char file_buffer[256]; strftime(date_buffer, sizeof(date_buffer), "%Y_%m_%d", t); - snprintf(dir_buffer, sizeof(dir_buffer), "%s/.timetracker", get_home_dir()); - snprintf(file_buffer, sizeof(file_buffer), "%s/%s", dir_buffer, date_buffer); + snprintf(dir_buffer, sizeof(dir_buffer), "%s/.timetracker", + get_home_dir()); + snprintf(file_buffer, sizeof(file_buffer), "%s/%s", dir_buffer, + date_buffer); if(!is_directory(dir_buffer)) { mkdir(dir_buffer, 0700); // WARNING: Might fail, what then? @@ -203,7 +211,8 @@ int main(int argc, char *argv[]) if(path_exists(file_buffer)) { f = fopen(file_buffer, "r"); 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; } } else { @@ -221,7 +230,8 @@ int main(int argc, char *argv[]) char activity_name[64]; U64 lower; 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) continue; @@ -243,13 +253,12 @@ int main(int argc, char *argv[]) continue; 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].ended = upper; current_activity++; - - printf("Added: %s\n", activity_name); } } @@ -271,7 +280,8 @@ int main(int argc, char *argv[]) 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)) { fprintf(stderr, "Unable to load font\n"); return 1; @@ -300,6 +310,7 @@ int main(int argc, char *argv[]) state.mouse_pos = GetMousePosition(); + // TODO: Switch activities with number keys. if(IsKeyPressed(KEY_Q)) break; @@ -314,32 +325,47 @@ int main(int argc, char *argv[]) DrawRectangle(0, 0, window_w, 40, GRAY); 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 = floor((F32)(activities[i].began - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s) * (F32)window_w); - F32 end_x = floor(((F32)(activities[i].ended - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s)) * window_w); - // DrawRectangleRounded((Rectangle){ start_x, 0, end_x - start_x, 40}, 0.0f, 100, activities[i].color); - DrawRectangle(start_x, 0, end_x - start_x, 40, activities[i].color); - // } + F32 start_x = + floor((F32)(activities[i].began - lower_bound_s) / + (F32)(upper_bound_s - lower_bound_s) * (F32)window_w); + F32 end_x = + 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. - 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++) { - 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 ++) { 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) }; - 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])) { + Rectangle rect = + { (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) { current_activity++; 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].ended = now_s; } @@ -368,7 +394,9 @@ int main(int argc, char *argv[]) activities[i].ended); 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"); } }