Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. #include <pebble.h>
  2.  
  3. static BitmapLayer *s_background_layer;
  4.  
  5. static GBitmap *s_background_bitmap;
  6.  
  7. static Window *s_main_window;
  8.  
  9. static TextLayer *s_time_layer, *s_date_layer;
  10.  
  11. static GFont s_time_font;
  12. static GFont s_date_font;  
  13.  
  14.  
  15. static void update_time() {
  16.   // Get a tm structure
  17.   time_t temp = time(NULL);
  18.   struct tm *tick_time = localtime(&temp);
  19.  
  20.   // Write the current hours and minutes into a buffer
  21.   static char s_buffer[8];
  22.  if (clock_is_24h_style()) {
  23.   strftime(s_buffer, sizeof(s_buffer), "%H:%M", tick_time);
  24. } else {
  25.   strftime(s_buffer, sizeof(s_buffer), "%I:%M%p", tick_time);
  26. }
  27.  
  28.   // Display this time on the TextLayer
  29.   text_layer_set_text(s_time_layer, s_buffer);
  30.  
  31.   // Copy date into buffer from tm structure
  32. static char date_buffer[16];
  33. strftime(date_buffer, sizeof(date_buffer), "DAY %d", tick_time);
  34.  
  35. // Show the date
  36. text_layer_set_text(s_date_layer, date_buffer);
  37. }
  38.  
  39.  
  40.  
  41.  
  42. static void main_window_load(Window *window) {
  43.    
  44.  // Get information about the Window
  45.  Layer *window_layer = window_get_root_layer(window);
  46.  GRect bounds = layer_get_bounds(window_layer);
  47.    
  48.  // Create GBitmap
  49.  s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_firewatchptr);
  50.  
  51.  // Create BitmapLayer to display the GBitmap
  52.  s_background_layer = bitmap_layer_create(bounds);
  53.  
  54.  // Set the bitmap onto the layer and add to the window
  55.  bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
  56.  layer_add_child(window_layer, bitmap_layer_get_layer(s_background_layer));
  57.  
  58.  // Create the TextLayer with specific bounds
  59.  s_time_layer = text_layer_create(
  60.     GRect(0, PBL_IF_ROUND_ELSE(52, 52), bounds.size.w, 50));
  61.  
  62.  // Improve the layout to be more like a watchface
  63.  text_layer_set_background_color(s_time_layer, GColorClear);
  64.  text_layer_set_text_color(s_time_layer, GColorBlack);
  65.  text_layer_set_text(s_time_layer, "00:00");
  66.  text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
  67.  
  68.   // Add it as a child layer to the Window's root layer
  69.   layer_add_child(window_layer, text_layer_get_layer(s_time_layer));
  70.    
  71.   // Create date TextLayer
  72.  s_date_layer = text_layer_create(GRect(0, 89, 185, 45));
  73.  text_layer_set_text_color(s_date_layer, GColorBlack);
  74.  text_layer_set_background_color(s_date_layer, GColorClear);
  75.  text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
  76.  
  77.    
  78.  // Add to Window
  79.  layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_date_layer));
  80.  
  81.  
  82.  // Create GFont
  83.  s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_MYRIAD_PRO_40));
  84.  s_date_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_MYRIAD_PRO_30));
  85.  
  86.  // Apply to TextLayer
  87.  text_layer_set_font(s_time_layer, s_time_font);
  88.    
  89.  text_layer_set_font(s_date_layer, s_date_font);
  90.    
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. static void main_window_unload(Window *window) {
  99.  
  100.  // Destroy TextLayer
  101.  text_layer_destroy(s_time_layer);
  102.  
  103.  
  104.  static GFont s_time_font;
  105.  static GFont s_date_font;
  106.    
  107.  // Unload GFont
  108.  fonts_unload_custom_font(s_time_font);
  109.  fonts_unload_custom_font(s_date_font);
  110.  
  111.  
  112.  // Destroy GBitmap
  113.  gbitmap_destroy(s_background_bitmap);
  114.  
  115.  // Destroy BitmapLayer
  116.  bitmap_layer_destroy(s_background_layer);
  117.  text_layer_destroy(s_date_layer);
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  125.   update_time();
  126. }
  127.  
  128. static void init() {
  129.  
  130.   // Create main Window element and assign to pointer
  131.   s_main_window = window_create();
  132.  
  133.   // Set handlers to manage the elements inside the Window
  134.   window_set_window_handlers(s_main_window, (WindowHandlers) {
  135.     .load = main_window_load,
  136.     .unload = main_window_unload
  137.   });
  138.  
  139.   // Show the Window on the watch, with animated=true
  140.   window_stack_push(s_main_window, true);
  141.  
  142.   // Register with TickTimerService
  143.   tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
  144.  
  145.   // Make sure the time is displayed from the start
  146.   update_time();
  147. }
  148.  
  149.  
  150. static void deinit() {
  151.  
  152.   // Destroy Window
  153.   window_destroy(s_main_window);
  154. }
  155.  
  156. int main(void) {
  157.   init();
  158.   app_event_loop();
  159.   deinit();
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement