Analog Watch Sample Overview
The Analog Watch sample demonstrates how you can create an analog watch application.
The following figure illustrates the main screen of the Analog Watch application and the UI component tree.
Figure: Analog Watch screen
Implementation
To implement the analog watch:
- Initialize the application by creating the window and layout.
The _create_analogwatch() function creates the window and layout, which are the basis of the analog watch application. After creating the window and layout, the _create_clock() function makes the detailed layout of the analog watch, and the _clock_set_info_time() function sets the time by retrieving the current time with the watch_time_get_current_time() function.
static void _create_analogwatch(appdata_s *ad) { // Window ad->win = _create_win(ad); ret_if(!ad->win); // Layout ad->layout = _create_layout(ad); goto_if(!ad->layout, ERROR); // Clock ad->clock = _create_clock(ad); goto_if(!ad->clock, ERROR); ret = watch_time_get_current_time(&watch_time); if (ret != APP_ERROR_NONE) { _E("failed to get current time. err = %d", ret); } _clock_set_info_time(ad, watch_time); return; }
- Create the detailed layout of the analog watch.
The _create_clock() function creates the detailed layout of the analog watch application: it makes a rectangle which is the background of the analog watch, and always sets the size of the rectangle to become a square. It also makes hands of the analog watch.
static Evas_Object * _create_clock(appdata_s *ad) { // Clock base clock = evas_object_rectangle_add(evas); retv_if(!clock, NULL); if (ad->w < ad->h) { evas_object_size_hint_min_set(clock, ad->w, ad->w); evas_object_resize(clock, ad->w, ad->w); } else { evas_object_size_hint_min_set(clock, ad->h, ad->h); evas_object_resize(clock, ad->h, ad->h); } evas_object_color_set(clock, 255, 255, 255, 255); elm_object_part_content_set(ad->layout, "clock", clock); evas_object_show(clock); // Hands of the clock ad->hour_needle = evas_object_line_add(evas); evas_object_color_set(ad->hour_needle, 0, 0, 0, 255); evas_object_show(ad->hour_needle); ad->min_needle = evas_object_line_add(evas); evas_object_color_set(ad->min_needle, 100, 100, 100, 255); evas_object_show(ad->min_needle); ad->sec_needle = evas_object_line_add(evas); evas_object_color_set(ad->sec_needle, 255, 0, 0, 255); evas_object_show(ad->sec_needle); return clock; }
- Renew the time once per second.
The app_time_tick() function is one of the watch_app_lifecycle_callback_s structure variables, and it called at least once per second. Watch applications can get the current time from the watch_time handle. Therefore, the _clock_set_info_time() function is also called once per second.
static void app_time_tick(watch_time_h watch_time, void* user_data) { appdata_s *ad = user_data; _clock_set_info_time(ad, watch_time); }
- Draw the hands of the analog watch to follow the course of time.
The _clock_set_info_time() function is called every second, and it gets the current time using the watch_time_get_hour24/minute/second() functions. It also draws the hands of the analog watch based on the time information.
static void _clock_set_info_time(void *data, watch_time_h watch_time) { watch_time_get_hour24(watch_time, &hour24); watch_time_get_minute(watch_time, &minute); watch_time_get_second(watch_time, &second); w = ad->w; h = ad->h; num = _get_radian((hour24%12) * HOUR_ANGLE); _D("Hour : %d", hour24%12); evas_object_line_xy_set(ad->hour_needle, (w/2), (h/2), (w/2) + HOUR_NEEDLE_SIZE*(sin(num)), (h/2) - HOUR_NEEDLE_SIZE*(cos(num))); num = _get_radian(minute * MIN_ANGLE); _D("Min : %d", minute); evas_object_line_xy_set(ad->min_needle, (w/2), (h/2), (w/2) + MIN_NEEDLE_SIZE*(sin(num)), (h/2) - MIN_NEEDLE_SIZE*(cos(num))); num = _get_radian(second * SEC_ANGLE); _D("Sec: %d", second); evas_object_line_xy_set(ad->sec_needle, (w/2), (h/2), (w/2) + SEC_NEEDLE_SIZE*(sin(num)), (h/2) - SEC_NEEDLE_SIZE*(cos(num))); }