efl

Форк
0
/
efl_canvas_textblock_obstacles_example.c 
273 строки · 8.0 Кб
1
#define EFL_BETA_API_SUPPORT 1
2

3
#include <Efl_Ui.h>
4

5
 /**
6
 * Example of canvas textblock obstacles.
7
 *
8
 * You start with two registered obstacle objects. They are not visible
9
 * at first, so the canvas textblock simply shows the text that has been set to it.
10
 * Once the obstacle is visible (show/hide keys in the example), the text will
11
 * wrap around it.
12
 * This example allows you to test two obstacles registered to the same
13
 * canvas textblock object. Also, you can play with size and position for each.
14
 * Use the 'h' key to show the provided options for this test.
15
 *
16
 * @verbatim
17
 * gcc -g efl_canvas_textblock_obstacles_example.c -o efl_canvas_textblock_obstacles_example `pkg-config --cflags --libs elementary`
18
 * @endverbatim
19
 */
20

21
#define WIDTH  (360)
22
#define HEIGHT (240)
23

24
#define POINTER_CYCLE(_ptr, _array)                             \
25
  do                                                            \
26
    {                                                           \
27
       if ((unsigned int)(((unsigned char *)(_ptr)) - ((unsigned char *)(_array))) >= \
28
           sizeof(_array))                                      \
29
         _ptr = _array;                                         \
30
    }                                                           \
31
  while(0)
32

33
static const char *commands = \
34
  "commands are:\n"
35
  "\tt - change currently controlled obstacle\n"
36
  "\tv - show/hide current obstacle\n"
37
  "\ts - cycle current obstacle's size\n"
38
  "\tp - change current obstacle's position (random)\n"
39
  "\tw - cycle text wrapping modes (none/word/char/mixed)\n"
40
  "\th - print help\n";
41

42
struct text_preset_data
43
{
44
   const char        **font_ptr;
45
   const char         *font[3];
46

47
   const char        **wrap_ptr;
48
   const char         *wrap[4];
49

50
   int                *obs_size_ptr;
51
   int                 obs_size[3];
52

53
   Eo **obs_ptr; /* pointer to the currently controlled obstacle object */
54
   Eo *obs[2];
55
};
56

57
struct test_data
58
{
59
   Eo                     *win, *box, *bg, *text;
60
   struct text_preset_data t_data;
61
   Eina_Size2D             size;
62
};
63

64
static struct test_data d = {0};
65

66
static unsigned int
67
_getrand(unsigned int low, unsigned int high)
68
{
69
   return (rand() % (high - low)) + low;
70
}
71

72
static void
73
_style_set(const char *wrap)
74
{
75
   char buf[2000];
76
   snprintf(buf,
77
         2000,
78
         "font=Sans font_size=16 color=#000 wrap=%s",
79
         wrap);
80

81
   efl_canvas_textblock_style_apply(d.text, buf);
82
}
83

84
static void
85
_key_down(void *data EINA_UNUSED, const Efl_Event *ev)
86
{
87
   const char *key = efl_input_key_string_get(ev->info);
88
   if (!key)
89
     return;
90

91
   if (strcmp(key, "h") == 0) /* print help */
92
     {
93
        printf("%s\n", commands);
94
        return;
95
     }
96

97
   if (strcmp(key, "t") == 0) /* change obstacle type */
98
     {
99
        (d.t_data.obs_ptr)++;
100
        POINTER_CYCLE(d.t_data.obs_ptr, d.t_data.obs);
101

102
        printf("Now controlling obstacle: %p\n", *d.t_data.obs_ptr);
103

104
        return;
105
     }
106

107
   if (strcmp(key, "v") == 0) /* change obstacle visibility */
108
     {
109
        Eo *obj = *d.t_data.obs_ptr;
110
        if (efl_gfx_entity_visible_get(obj))
111
           efl_gfx_entity_visible_set(obj, EINA_FALSE);
112
        else
113
           efl_gfx_entity_visible_set(obj, EINA_TRUE);
114

115
        printf("Show/hide toggle for obstacle %p\n",
116
               *d.t_data.obs_ptr);
117

118
        efl_canvas_textblock_obstacles_update(d.text);
119

120
        return;
121
     }
122

123
   if (strcmp(key, "s") == 0) /* change obstacle size */
124
     {
125
        (d.t_data.obs_size_ptr)++;
126
        POINTER_CYCLE(d.t_data.obs_size_ptr, d.t_data.obs_size);
127

128
        efl_gfx_entity_size_set(*d.t_data.obs_ptr, EINA_SIZE2D(*d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr));
129

130
        efl_canvas_textblock_obstacles_update(d.text);
131

132
        printf("Changing obstacle size to: %d,%d\n",
133
               *d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr);
134

135
        return;
136
     }
137

138
   if (strcmp(key, "p") == 0) /* change obstacle position */
139
     {
140
        int  x, y;
141
        x = _getrand(0, d.size.w);
142
        y = _getrand(0, d.size.h);
143

144
        efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));
145
        efl_canvas_textblock_obstacles_update(d.text);
146

147
        printf("Changing obstacles position\n");
148
        efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));
149

150
        Eina_Position2D r_rec = efl_gfx_entity_position_get(d.t_data.obs[0]);
151
        Eina_Position2D g_rec = efl_gfx_entity_position_get(d.t_data.obs[1]);
152

153
        printf("Obstacle #1 (red)  : [%d,%d]\n", r_rec.x, r_rec.y);
154
        printf("Obstacle #2 (green): [%d,%d]\n", g_rec.x, g_rec.y);
155

156
        return;
157
     }
158

159
   if (strcmp(key, "w") == 0) /* change obstacle position */
160
     {
161
        (d.t_data.wrap_ptr)++;
162
        POINTER_CYCLE(d.t_data.wrap_ptr, d.t_data.wrap);
163
        printf("Changing wrap mode to: %s\n",
164
               *d.t_data.wrap_ptr);
165
        _style_set(*d.t_data.wrap_ptr);
166
        efl_canvas_textblock_obstacles_update(d.text);
167

168
        return;
169
     }
170
}
171

172
static void
173
_win_resize(void *data EINA_UNUSED, const Efl_Event *ev)
174
{
175
   Eina_Size2D sz;
176

177
   sz = efl_gfx_entity_size_get(ev->object);
178
   efl_gfx_entity_size_set(d.bg, sz);
179
   efl_gfx_entity_size_set(d.text, sz);
180

181
   d.size = sz;
182
}
183

184
static void
185
_text_init()
186
{
187
   _style_set("word");
188

189
   efl_text_markup_set(d.text,
190
         "This example text demonstrates the textblock object"
191
         " with obstacle objects support."
192
         " Any evas object <item size=72x16></item>can register itself as an obstacle to the textblock"
193
         " object. Upon regi<color=#0ff>stering, it aff</color>ects the layout of the text in"
194
         " certain situations. Usually, when the obstacle shows above the text"
195
         " area, it will cause the layout of the text to split and move"
196
         " parts of it, so that all text area is apparent."
197
         );
198
}
199

200
static void
201
_gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
202
{
203
   efl_exit(0);
204
}
205

206
static void
207
_gui_setup()
208
{
209
   /* init values one is going to cycle through while running this
210
    * example */
211
   struct text_preset_data init_data =
212
   {
213
      .font = {"DejaVu", "Courier", "Utopia"},
214
      .wrap = {"word", "char", "mixed", "none"},
215
      .obs_size = {50, 70, 100},
216
      .obs = {NULL, NULL},
217
   };
218

219
   d.t_data = init_data;
220
   d.t_data.font_ptr = d.t_data.font;
221
   d.t_data.obs_size_ptr = d.t_data.obs_size;
222
   d.t_data.obs_ptr = d.t_data.obs;
223

224
   d.win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(),
225
                 efl_text_set(efl_added, "Obstacles Example"),
226
                 efl_ui_win_autodel_set(efl_added, EINA_TRUE));
227

228
   efl_gfx_entity_size_set(d.win, EINA_SIZE2D(WIDTH, HEIGHT));
229
   printf("Window size set to [%d,%d]\n", WIDTH, HEIGHT);
230

231
   efl_event_callback_add(d.win, EFL_UI_WIN_EVENT_DELETE_REQUEST, _gui_quit_cb, NULL);
232
   efl_event_callback_add(d.win, EFL_GFX_ENTITY_EVENT_SIZE_CHANGED, _win_resize, NULL);
233
   efl_event_callback_add(d.win, EFL_EVENT_KEY_DOWN, _key_down, NULL);
234

235
   d.bg = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
236
           efl_gfx_color_set(efl_added, 255, 255, 255, 255));
237

238
   efl_gfx_entity_size_set(d.bg, EINA_SIZE2D(WIDTH, HEIGHT));
239
   efl_gfx_entity_position_set(d.bg, EINA_POSITION2D(0, 0));
240

241
   d.text = efl_add(EFL_CANVAS_TEXTBLOCK_CLASS, d.win,
242
           efl_text_multiline_set(efl_added, EINA_TRUE));
243

244
   _text_init();
245
   efl_gfx_entity_size_set(d.text, EINA_SIZE2D(WIDTH, HEIGHT));
246
   efl_gfx_entity_position_set(d.text, EINA_POSITION2D(0, 0));
247

248
   d.size.w = WIDTH;
249
   d.size.h = HEIGHT;
250

251
   /* init obstacles */
252
   d.t_data.obs[0] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
253
           efl_gfx_color_set(efl_added, 255, 0, 0, 255));
254

255
   efl_gfx_entity_size_set(d.t_data.obs[0], EINA_SIZE2D(50,50));
256

257
   d.t_data.obs[1] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
258
           efl_gfx_color_set(efl_added, 0, 255, 0, 255));
259

260
   efl_gfx_entity_size_set(d.t_data.obs[1], EINA_SIZE2D(50,50));
261

262
   efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[0]);
263
   efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[1]);
264

265
   printf("%s\n", commands);
266
}
267

268
EAPI_MAIN void
269
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
270
{
271
   _gui_setup();
272
}
273
EFL_MAIN()
274

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.