Browse code

Add the progress bar layer from Pebble UI Pattern examples

Natasha Kerensikova authored on18/03/2016 20:15:10
Showing2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,109 @@
1
+/*
2
+ * This is copied dirictly without any modification other than the addition
3
+ * of this header, from "UI Pattern" project in Pebble examples
4
+ * https://github.com/pebble-examples/ui-patterns
5
+ * It is covered by the MIT licence detailed below:
6
+ *
7
+ * Copyright (c) 2015 Pebble Examples
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in all
17
+ * copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+#include "progress_layer.h"
28
+
29
+#define MIN(a,b) (((a)<(b))?(a):(b))
30
+
31
+typedef struct {
32
+  int16_t progress_percent;
33
+  int16_t corner_radius;
34
+  GColor foreground_color;
35
+  GColor background_color;
36
+} ProgressLayerData;
37
+
38
+static int16_t scale_progress_bar_width_px(unsigned int progress_percent, int16_t rect_width_px) {
39
+  return ((progress_percent * (rect_width_px)) / 100);
40
+}
41
+
42
+static void progress_layer_update_proc(ProgressLayer* progress_layer, GContext* ctx) {
43
+  ProgressLayerData *data = (ProgressLayerData *)layer_get_data(progress_layer);
44
+  GRect bounds = layer_get_bounds(progress_layer);
45
+
46
+  int16_t progress_bar_width_px = scale_progress_bar_width_px(data->progress_percent, bounds.size.w);
47
+  GRect progress_bar = GRect(bounds.origin.x, bounds.origin.y, progress_bar_width_px, bounds.size.h);
48
+
49
+  graphics_context_set_fill_color(ctx, data->background_color);
50
+  graphics_fill_rect(ctx, bounds, data->corner_radius, GCornersAll);
51
+
52
+  graphics_context_set_fill_color(ctx, data->foreground_color);
53
+  graphics_fill_rect(ctx, progress_bar, data->corner_radius, GCornersAll);
54
+
55
+#ifdef PBL_PLATFORM_APLITE
56
+  graphics_context_set_stroke_color(ctx, data->background_color);
57
+  graphics_draw_rect(ctx, progress_bar);
58
+#endif
59
+}
60
+
61
+ProgressLayer* progress_layer_create(GRect frame) {
62
+  ProgressLayer *progress_layer = layer_create_with_data(frame, sizeof(ProgressLayerData));
63
+  layer_set_update_proc(progress_layer, progress_layer_update_proc);
64
+  layer_mark_dirty(progress_layer);
65
+
66
+  ProgressLayerData *data = (ProgressLayerData *)layer_get_data(progress_layer);
67
+  data->progress_percent = 0;
68
+  data->corner_radius = 1;
69
+  data->foreground_color = GColorBlack;
70
+  data->background_color = GColorWhite;
71
+
72
+  return progress_layer;
73
+}
74
+
75
+void progress_layer_destroy(ProgressLayer* progress_layer) {
76
+  if (progress_layer) {
77
+    layer_destroy(progress_layer);
78
+  }
79
+}
80
+
81
+void progress_layer_increment_progress(ProgressLayer* progress_layer, int16_t progress) {
82
+  ProgressLayerData *data = (ProgressLayerData *)layer_get_data(progress_layer);
83
+  data->progress_percent = MIN(100, data->progress_percent + progress);
84
+  layer_mark_dirty(progress_layer);
85
+}
86
+
87
+void progress_layer_set_progress(ProgressLayer* progress_layer, int16_t progress_percent) {
88
+  ProgressLayerData *data = (ProgressLayerData *)layer_get_data(progress_layer);
89
+  data->progress_percent = MIN(100, progress_percent);
90
+  layer_mark_dirty(progress_layer);
91
+}
92
+
93
+void progress_layer_set_corner_radius(ProgressLayer* progress_layer, uint16_t corner_radius) {
94
+  ProgressLayerData *data = (ProgressLayerData *)layer_get_data(progress_layer);
95
+  data->corner_radius = corner_radius;
96
+  layer_mark_dirty(progress_layer);
97
+}
98
+
99
+void progress_layer_set_foreground_color(ProgressLayer* progress_layer, GColor color) {
100
+  ProgressLayerData *data = (ProgressLayerData *)layer_get_data(progress_layer);
101
+  data->foreground_color = color;
102
+  layer_mark_dirty(progress_layer);
103
+}
104
+
105
+void progress_layer_set_background_color(ProgressLayer* progress_layer, GColor color) {
106
+  ProgressLayerData *data = (ProgressLayerData *)layer_get_data(progress_layer);
107
+  data->background_color = color;
108
+  layer_mark_dirty(progress_layer);
109
+}
0 110
new file mode 100644
... ...
@@ -0,0 +1,39 @@
1
+/*
2
+ * This is copied dirictly without any modification other than the addition
3
+ * of this header, from "UI Pattern" project in Pebble examples
4
+ * https://github.com/pebble-examples/ui-patterns
5
+ * It is covered by the MIT licence detailed below:
6
+ *
7
+ * Copyright (c) 2015 Pebble Examples
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in all
17
+ * copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+#pragma once
28
+
29
+#include <pebble.h>
30
+
31
+typedef Layer ProgressLayer;
32
+
33
+ProgressLayer* progress_layer_create(GRect frame);
34
+void progress_layer_destroy(ProgressLayer* progress_layer);
35
+void progress_layer_increment_progress(ProgressLayer* progress_layer, int16_t progress);
36
+void progress_layer_set_progress(ProgressLayer* progress_layer, int16_t progress_percent);
37
+void progress_layer_set_corner_radius(ProgressLayer* progress_layer, uint16_t corner_radius);
38
+void progress_layer_set_foreground_color(ProgressLayer* progress_layer, GColor color);
39
+void progress_layer_set_background_color(ProgressLayer* progress_layer, GColor color);