Custom style created for buttons
[apps/native/gear-racing-controller.git] / src / view / view_base.c
1 /*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://floralicense.org/license/
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <system_info.h>
18 #include <assert.h>
19 #include "gear-racing-controller.h"
20 #include "view/view_base.h"
21
22 void view_base_get_resource(const char *edj_file_in, char *edj_path_out)
23 {
24         char *res_path = app_get_resource_path();
25         if (res_path) {
26                 snprintf(edj_path_out, PATH_MAX, "%s%s", res_path, edj_file_in);
27                 free(res_path);
28         }
29 }
30
31 Evas_Object *view_base_create_layout(Evas_Object *parent, char *edj_file, char *group)
32 {
33         char edj_path[PATH_MAX] = { 0 };
34         view_base_get_resource(edj_file, edj_path);
35         Evas_Object *layout = elm_layout_add(parent);
36         if (!layout) {
37                 assert(!"Failed to create controller");
38         }
39
40         if (!elm_layout_file_set(layout, edj_path, GRP_MAIN)) {
41                 assert(!"Failed to load file");
42         }
43
44         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
45 //      elm_object_content_set(parent, layout);
46
47         return layout;
48 }
49
50 Evas_Object *view_base_create_image(Evas_Object *parent, char *path)
51 {
52         char edj_path[PATH_MAX] = { 0 };
53         view_base_get_resource(path, edj_path);
54
55         Evas_Object *image = elm_image_add(parent);
56         evas_object_size_hint_weight_set(parent, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
57
58         if (!elm_image_file_set(image, edj_path, NULL)) {
59                 dlog_print(DLOG_ERROR, LOG_TAG, "Failed to load file: %s", path);
60         }
61
62         return image;
63 }
64
65 Evas_Object *view_base_create_button(Evas_Object *parent, char *part, char *text, char *image_path, char *style, Evas_Smart_Cb callback, void *data)
66 {
67         Evas_Object *button = elm_button_add(parent);
68         if (!button) {
69                 assert(!"Failed to create button");
70         }
71
72         evas_object_size_hint_weight_set(button, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
73
74         if (style && !elm_object_style_set(button, style)) {
75                 dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set style: %s", style);
76         }
77
78         if (text) {
79                 elm_object_text_set(button, text);
80         }
81
82         if (image_path) {
83                 Evas_Object *image = view_base_create_image(button, image_path);
84                 elm_object_content_set(button, image);
85         }
86
87         if (callback) {
88                 evas_object_smart_callback_add(button, "clicked", callback, data);
89         }
90
91         if (part) {
92                 elm_layout_content_set(parent, part, button);
93         }
94
95         return button;
96 }
97
98 void get_screen_size(int *width, int *height)
99 {
100         system_info_get_platform_int("http://tizen.org/feature/screen.width", width);
101         system_info_get_platform_int("http://tizen.org/feature/screen.height", height);
102 }
103
104 void view_base_set_position(Evas_Object *obj, int pos_x, int pos_y,
105                 e_horizontal_align horizontal_align, e_vertical_align vertical_align)
106 {
107         int width = 0;
108         int height = 0;
109         int x = 0;
110         int y = 0;
111
112         if (horizontal_align != HORIZONTAL_ALIGN_LEFT || vertical_align != VERTICAL_ALIGN_TOP) {
113                 evas_object_geometry_get(obj, NULL, NULL, &width, &height);
114         }
115
116         switch (horizontal_align) {
117                 case HORIZONTAL_ALIGN_LEFT:
118                         x = pos_x;
119                         break;
120                 case HORIZONTAL_ALIGN_CENTER:
121                         x = pos_x - width / 2;
122                         break;
123                 case HORIZONTAL_ALIGN_RIGHT:
124                         x = pos_x - width;
125                         break;
126                 default:
127                         break;
128         }
129
130         switch (vertical_align) {
131                 case VERTICAL_ALIGN_TOP:
132                         y = pos_y;
133                         break;
134                 case VERTICAL_ALIGN_CENTER:
135                         y = pos_y - height / 2;
136                         break;
137                 case VERTICAL_ALIGN_BOTTOM:
138                         y = pos_y - height;
139                         break;
140                 default:
141                         break;
142         }
143
144         evas_object_move(obj, x, y);
145 }
146
147 void view_base_set_angle(Evas_Object *image, float angle, float rotation_center_x, float rotation_center_y)
148 {
149         Evas_Map *map = evas_map_new(4);
150         evas_map_util_points_populate_from_object(map, image);
151         evas_map_util_rotate(map, angle, rotation_center_x, rotation_center_y);
152         evas_object_map_set(image, map);
153         evas_object_map_enable_set(image, true);
154         evas_map_free(map);
155 }
156
157 void view_base_show(s_view_base *view)
158 {
159         if (view && view->show_cb) {
160                 view->show_cb();
161         }
162 }
163
164 void view_base_hide(s_view_base *view)
165 {
166         if (view && view->hide_cb) {
167                 view->hide_cb();
168         }
169 }
170
171 void view_base_update(s_view_base *view)
172 {
173         if (view && view->update_cb) {
174                 view->update_cb();
175         }
176 }
177