Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
99 andreas 2
 * Copyright (C) 2020 to 2022 by Andreas Theofilu <andreas@theosys.at>
2 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
#ifndef __TBUTTON_H__
19
#define __TBUTTON_H__
20
 
21
#include <string>
22
#include <vector>
3 andreas 23
#include <map>
6 andreas 24
#include <functional>
15 andreas 25
#include <thread>
6 andreas 26
 
4 andreas 27
#include <include/core/SkImage.h>
28
#include <include/core/SkCanvas.h>
6 andreas 29
#include <include/core/SkBitmap.h>
30
 
76 andreas 31
#include "texpat++.h"
4 andreas 32
#include "tpalette.h"
33
#include "tcolor.h"
7 andreas 34
#include "tfont.h"
15 andreas 35
#include "tamxnet.h"
36
#include "ttimer.h"
21 andreas 37
#include "timagerefresh.h"
2 andreas 38
 
8 andreas 39
#define ORD_ELEM_COUNT  5
40
 
21 andreas 41
extern bool prg_stopped;
97 andreas 42
extern bool _restart_;
21 andreas 43
 
8 andreas 44
class SkFont;
66 andreas 45
class SkTextBlob;
46
 
21 andreas 47
struct RESOURCE_T;
8 andreas 48
 
2 andreas 49
namespace Button
50
{
15 andreas 51
    typedef enum BUTTONTYPE
52
    {
53
        NONE,
54
        GENERAL,
55
        MULTISTATE_GENERAL,
56
        BARGRAPH,
57
        MULTISTATE_BARGRAPH,
58
        JOISTICK,
59
        TEXT_INPUT,
60
        COMPUTER_CONTROL,
61
        TAKE_NOTE,
62
        SUBPAGE_VIEW
63
    } BUTTONTYPE;
64
 
4 andreas 65
    typedef struct SYSBORDER_t
66
    {
81 andreas 67
        int id{0};                  // Internal unique ID number
68
        char *name{nullptr};        // Name of the border
69
        int number{0};              // AMX number
70
        char *style{nullptr};       // Style to use if dynamicaly calculated
71
        int width{0};               // The width of the border
72
        int radius{0};              // Radius for rounded corners
73
        bool calc{false};           // TRUE = Calculated inside, FALSE = Read from images
4 andreas 74
    }SYSBORDER_t;
75
 
15 andreas 76
    typedef struct SYSBUTTONS_t
77
    {
78
        int channel{0};             // Channel number
79
        BUTTONTYPE type{NONE};      // Type of button
80
        int states{0};              // Maximum number of states
81
        int ll{0};                  // Level low range
82
        int lh{0};                  // Level high range
83
    }SYSBUTTONS_t;
84
 
3 andreas 85
    typedef enum TEXT_ORIENTATION
86
    {
87
        ORI_ABSOLUT,
88
        ORI_TOP_LEFT,
89
        ORI_TOP_MIDDLE,
90
        ORI_TOP_RIGHT,
91
        ORI_CENTER_LEFT,
92
        ORI_CENTER_MIDDLE,		// default
93
        ORI_CENTER_RIGHT,
94
        ORI_BOTTOM_LEFT,
95
        ORI_BOTTOM_MIDDLE,
96
        ORI_BOTTOM_RIGHT
97
    } TEXT_ORIENTATION;
2 andreas 98
 
7 andreas 99
    typedef enum TEXT_EFFECT
100
    {
101
        EFFECT_NONE,
102
        EFFECT_OUTLINE_S,
103
        EFFECT_OUTLINE_M,
104
        EFFECT_OUTLINE_L,
105
        EFFECT_OUTLINE_X,
106
        EFFECT_GLOW_S,
107
        EFFECT_GLOW_M,
108
        EFFECT_GLOW_L,
109
        EFFECT_GLOW_X,
110
        EFFECT_SOFT_DROP_SHADOW_1,
111
        EFFECT_SOFT_DROP_SHADOW_2,
112
        EFFECT_SOFT_DROP_SHADOW_3,
113
        EFFECT_SOFT_DROP_SHADOW_4,
114
        EFFECT_SOFT_DROP_SHADOW_5,
115
        EFFECT_SOFT_DROP_SHADOW_6,
116
        EFFECT_SOFT_DROP_SHADOW_7,
117
        EFFECT_SOFT_DROP_SHADOW_8,
118
        EFFECT_MEDIUM_DROP_SHADOW_1,
119
        EFFECT_MEDIUM_DROP_SHADOW_2,
120
        EFFECT_MEDIUM_DROP_SHADOW_3,
121
        EFFECT_MEDIUM_DROP_SHADOW_4,
122
        EFFECT_MEDIUM_DROP_SHADOW_5,
123
        EFFECT_MEDIUM_DROP_SHADOW_6,
124
        EFFECT_MEDIUM_DROP_SHADOW_7,
125
        EFFECT_MEDIUM_DROP_SHADOW_8,
126
        EFFECT_HARD_DROP_SHADOW_1,
127
        EFFECT_HARD_DROP_SHADOW_2,
128
        EFFECT_HARD_DROP_SHADOW_3,
129
        EFFECT_HARD_DROP_SHADOW_4,
130
        EFFECT_HARD_DROP_SHADOW_5,
131
        EFFECT_HARD_DROP_SHADOW_6,
132
        EFFECT_HARD_DROP_SHADOW_7,
133
        EFFECT_HARD_DROP_SHADOW_8,
134
        EFFECT_SOFT_DROP_SHADOW_1_WITH_OUTLINE,
135
        EFFECT_SOFT_DROP_SHADOW_2_WITH_OUTLINE,
136
        EFFECT_SOFT_DROP_SHADOW_3_WITH_OUTLINE,
137
        EFFECT_SOFT_DROP_SHADOW_4_WITH_OUTLINE,
138
        EFFECT_SOFT_DROP_SHADOW_5_WITH_OUTLINE,
139
        EFFECT_SOFT_DROP_SHADOW_6_WITH_OUTLINE,
140
        EFFECT_SOFT_DROP_SHADOW_7_WITH_OUTLINE,
141
        EFFECT_SOFT_DROP_SHADOW_8_WITH_OUTLINE,
142
        EFFECT_MEDIUM_DROP_SHADOW_1_WITH_OUTLINE,
143
        EFFECT_MEDIUM_DROP_SHADOW_2_WITH_OUTLINE,
144
        EFFECT_MEDIUM_DROP_SHADOW_3_WITH_OUTLINE,
145
        EFFECT_MEDIUM_DROP_SHADOW_4_WITH_OUTLINE,
146
        EFFECT_MEDIUM_DROP_SHADOW_5_WITH_OUTLINE,
147
        EFFECT_MEDIUM_DROP_SHADOW_6_WITH_OUTLINE,
148
        EFFECT_MEDIUM_DROP_SHADOW_7_WITH_OUTLINE,
149
        EFFECT_MEDIUM_DROP_SHADOW_8_WITH_OUTLINE,
150
        EFFECT_HARD_DROP_SHADOW_1_WITH_OUTLINE,
151
        EFFECT_HARD_DROP_SHADOW_2_WITH_OUTLINE,
152
        EFFECT_HARD_DROP_SHADOW_3_WITH_OUTLINE,
153
        EFFECT_HARD_DROP_SHADOW_4_WITH_OUTLINE,
154
        EFFECT_HARD_DROP_SHADOW_5_WITH_OUTLINE,
155
        EFFECT_HARD_DROP_SHADOW_6_WITH_OUTLINE,
156
        EFFECT_HARD_DROP_SHADOW_7_WITH_OUTLINE,
157
        EFFECT_HARD_DROP_SHADOW_8_WITH_OUTLINE
158
    }TEXT_EFFECT;
159
 
8 andreas 160
    typedef enum DRAW_ORDER
161
    {
162
        ORD_ELEM_NONE,
163
        ORD_ELEM_FILL,
164
        ORD_ELEM_BITMAP,
165
        ORD_ELEM_ICON,
166
        ORD_ELEM_TEXT,
167
        ORD_ELEM_BORDER
168
    }DRAW_ORDER;
169
 
43 andreas 170
    typedef enum FEEDBACK
171
    {
172
        FB_NONE,
173
        FB_CHANNEL,
174
        FB_INV_CHANNEL,     // inverted channel
175
        FB_ALWAYS_ON,
176
        FB_MOMENTARY,
177
        FB_BLINK
178
    } FEEDBACK;
179
 
3 andreas 180
    typedef struct SR_T
181
    {
182
        int number{0};
6 andreas 183
        std::string _do;        // Order on how to show a multistate bargraph (010203...)
184
        std::string bs;         // Frame type (circle, ...)
185
        std::string mi;         // Chameleon image
186
        int mi_width{0};        // Width of image
187
        int mi_height{0};       // Height of image
188
        std::string cb;         // Border color
189
        std::string cf;         // Fill color
190
        std::string ct;         // Text Color
191
        std::string ec;         // Text effect color
192
        std::string bm;         // bitmap file name
193
        std::string sd;         // Sound file to play
194
        int bm_width{0};        // Width of image
195
        int bm_height{0};       // Height of image
196
        bool dynamic{false};    // TRUE = moving image
197
        int sb{0};              // Index to external graphics download
198
        int ii{0};              // Icon index number
10 andreas 199
        int ix{0};              // Icon X position
200
        int iy{0};              // Icon Y position
7 andreas 201
        int ji{5};              // Icon style / position like "jt", default 5 = center+middle
202
        int jb{5};              // Image position (center, left, ...), default 5 = center+middle
10 andreas 203
        int bx{0};              // Absolute image position x
204
        int by{0};              // Absolute image position y
205
        int fi{0};              // Font index
6 andreas 206
        std::string te;         // Text
207
        TEXT_ORIENTATION jt{ORI_CENTER_MIDDLE}; // Text orientation
208
        int tx{0};              // Text X position
209
        int ty{0};              // Text Y position
210
        int ww{0};              // line break when 1
211
        int et{0};              // Text effect (^TEF)
8 andreas 212
        int oo{-1};             // Over all opacity
3 andreas 213
    } SR_T;
2 andreas 214
 
43 andreas 215
    typedef struct EXTBUTTON_t
3 andreas 216
    {
43 andreas 217
        BUTTONTYPE type;
218
        int bi{0};              // button ID
219
        std::string na;         // name
220
        int lt{0};              // pixel from left
221
        int tp{0};              // pixel from top
222
        int wt{0};              // width
223
        int ht{0};              // height
224
        int zo{0};              // Z-Order
225
        std::string hs;         // bounding, ...
226
        std::string bs;         // Border style (circle, ...)
227
        FEEDBACK fb{FB_NONE};   // Feedback type (momentary, ...)
228
        int ap{1};              // Address port (default: 1)
229
        int ad{0};              // Address channel
230
        int lp{1};              // Level port (default: 1)
231
        int lv{0};              // Level code
232
        std::string dr;         // Level "horizontal" or "vertical"
233
        int lu{0};              // Animate time up (Bargraph)
234
        int ld{0};              // Animate time down (Bargraph)
235
        int rl{0};              // Range low
236
        int rh{0};              // Range high
237
        int rn{0};              // Bargraph: Range drag increment
238
        std::string sc;         // Color of slider (for bargraph)
239
        std::vector<SR_T> sr;
240
    }EXTBUTTON_t;
2 andreas 241
 
3 andreas 242
    typedef struct PUSH_FUNC
243
    {
6 andreas 244
        std::string pfType; // command to execute when button was pushed
245
        std::string pfName; // Name of popup
3 andreas 246
    } PUSH_FUNC_T;
2 andreas 247
 
4 andreas 248
    typedef enum CENTER_CODE
249
    {
250
        SC_ICON = 0,
251
        SC_BITMAP,
252
        SC_TEXT
253
    }CENTER_CODE;
254
 
255
    typedef struct POSITION_t
256
    {
257
        int width{0};
258
        int height{0};
10 andreas 259
        int left{1};
260
        int top{1};
17 andreas 261
        bool overflow{false};
4 andreas 262
        bool valid{false};
263
    }POSITION_t;
264
 
99 andreas 265
    typedef struct IMAGE_SIZE_t
266
    {
267
        int width{0};
268
        int height{0};
269
    }IMAGE_SIZE_t;
270
 
21 andreas 271
    typedef struct THR_REFRESH_t
272
    {
273
        ulong handle{0};
274
        ulong parent{0};
275
        int bi{0};
276
        TImageRefresh *mImageRefresh{nullptr};
277
        THR_REFRESH_t *next{nullptr};
278
    }THR_REFRESH_t;
279
 
50 andreas 280
    typedef struct BITMAP_t
281
    {
52 andreas 282
        unsigned char *buffer{nullptr};
283
        int left{0};
284
        int top{0};
285
        int width{0};
286
        int height{0};
287
        size_t rowBytes{0};
50 andreas 288
    }BITMAP_t;
289
 
94 andreas 290
    typedef struct BITMAP_CACHE
291
    {
292
        ulong handle{0};
293
        ulong parent{0};
294
        int left{0};
295
        int top{0};
296
        int width{0};
297
        int height{0};
298
        int bi{0};
97 andreas 299
        bool show{false};
94 andreas 300
        bool ready{false};
301
        SkBitmap bitmap;
302
    }BITMAP_CACHE;
303
 
3 andreas 304
    class TButton
305
    {
306
        public:
307
            TButton();
308
            ~TButton();
2 andreas 309
 
14 andreas 310
            /**
311
             * The following function parses the parameters of a particular
312
             * button and creates a new button. This function is called either
313
             * from class TPage or TSubPage when a page or subpage is created.
314
             *
23 andreas 315
             * @param xml     A pointer to the XML reader
316
             * @param node    A pointer to the actual node in the XML tree.
76 andreas 317
             * @return On success the last index processed. On error
318
             * TExpat::npos is returned.
14 andreas 319
             */
76 andreas 320
            size_t initialize(Expat::TExpat *xml, size_t index);
2 andreas 321
 
14 andreas 322
            /**
323
             * Returns the button index. This is a unique number inside a page
324
             * or subpage.
23 andreas 325
             *
326
             * @return return the button index number as defined in the
327
             * configuration file.
14 andreas 328
             */
3 andreas 329
            int getButtonIndex() { return bi; }
14 andreas 330
            /**
331
             * Returns the name of the button.
332
             */
3 andreas 333
            std::string& getButtonName() { return na; }
14 andreas 334
            /**
335
             * Returns the description of the button, if there is one.
336
             */
3 andreas 337
            std::string& getButtonDescription() { return bd; }
14 andreas 338
            /**
339
             * Returns the width of the button in pixels.
340
             */
3 andreas 341
            int getWidth() { return wt; }
14 andreas 342
            /**
343
             * Returns the height of the button in pixels.
344
             */
3 andreas 345
            int getHeight() { return ht; }
14 andreas 346
            /**
347
             * Returns the left position in pixels.
348
             */
3 andreas 349
            int getLeftPosition() { return lt; }
14 andreas 350
            /**
351
             * Returns the top position of the button in pixels.
352
             */
3 andreas 353
            int getTopPosition() { return tp; }
14 andreas 354
            /**
355
             * Returns the Z-order. This number marks the order the buttons
356
             * are drawed on the screen. Inside a page or subpage the buttons
357
             * are always sorted.
358
             */
3 andreas 359
            int getZOrder() { return zo; }
14 andreas 360
            /**
361
             * Returns the type of the button.
362
             */
363
            BUTTONTYPE getButtonType() { return type; }
15 andreas 364
 
365
            int getRangeLow() { return rl; }
366
            int getRangeHigh() { return rh; }
367
            int getStateCount() { return stateCount; }
14 andreas 368
            int getAddressPort() { return ap; }
369
            int getAddressChannel() { return ad; }
370
            int getChannelNumber() { return ch; }
371
            int getChannelPort() { return cp; }
372
            int getLevelPort() { return lp; }
373
            int getLevelValue() { return lv; }
53 andreas 374
            std::string getText(int inst=0) { return sr[inst].te; }
375
            std::string getTextColor(int inst=0) { return sr[inst].ct; }
376
            std::string getTextEffectColor(int inst=0) { return sr[inst].ec; }
377
            std::string getFillColor(int inst=0) { return sr[inst].cf; }
82 andreas 378
            std::string getBitmapName(int inst=0) { return sr[inst].bm; }
51 andreas 379
            bool isSingleLine() { return ( dt.compare("multiple") != 0); }
380
            bool isMultiLine() { return ( dt.compare("multiple") == 0); }
381
            int getTextMaxChars() { return mt; }
53 andreas 382
            bool getTextWordWrap(int inst=0) { return (sr[inst].ww == 1); }
14 andreas 383
            int getNumberInstances() { return (int)sr.size(); }
15 andreas 384
            int getActiveInstance() { return mActInstance; }
16 andreas 385
            ulong getHandle() { return mHandle; }
50 andreas 386
            ulong getParent() { return (mHandle & 0xffff0000); }
51 andreas 387
            void setActiveInstance(int inst);
16 andreas 388
            void setEnable(bool en) { mEnabled = en; }
389
            bool isEnabled() { return mEnabled; }
26 andreas 390
            void setHandle(ulong handle) { mHandle = handle; };
4 andreas 391
            void setPalette(TPalette *pal) { mPalette = pal; }
6 andreas 392
            void setParentWidth(int width) { mParentWidth = width; }
393
            void setParentHeight(int height) { mParentHeight = height; }
394
            void setParentSize(int width, int height) { mParentWidth = width; mParentHeight = height; }
7 andreas 395
            void setFonts(TFont *ft) { mFonts = ft; }
10 andreas 396
            void setGlobalOpacity(int oo) { if (oo >= 0 && oo <= 255) mGlobalOO = oo; }
100 andreas 397
            void setVisible(bool v) { visible = v; hd = (v ? 0 : 1); }
97 andreas 398
            bool isVisible() { return visible; }
38 andreas 399
 
400
            /**
401
             * @brief setBitmap Sets a new bitmap to the button
402
             * If there was already a bitmap on this button and if this bitmap
403
             * is different from the one in \p file, then it is erased.
404
             * The new bitmap file name is set and it will be loaded and created.
405
             *
406
             * @param file      File name of a bitmap file.
407
             * @param instance  The instance where to put the new bitmap. If
408
             *                  this is 0, the bitmap is set on all instances.
409
             * @return TRUE if no errors occures, otherwise FALSE.
410
             */
16 andreas 411
            bool setBitmap(const std::string& file, int instance);
38 andreas 412
            /**
413
             * @brief setOpacity Sets the opacity of this button
414
             *
415
             * @param op        The opacity in a reange of 0 to 255.
416
             * @param instance  The instance where to put the new bitmap. If
417
             *                  this is 0, the bitmap is set on all instances.
418
             * @return TRUE if no errors occures, otherwise FALSE.
419
             */
16 andreas 420
            bool setOpacity(int op, int instance);
421
            bool setWorWrap(bool state, int instance);
422
            bool setFont(int id, int instance);
423
            void setTop(int top);
424
            void setLeft(int left);
425
            void setLeftTop(int left, int top);
21 andreas 426
            void setResourceName(const std::string& name, int instance);
38 andreas 427
            bool startAnimation(int start, int end, int time);
428
            /**
429
             * @brief registerSystemButton registers the button as a system button.
430
             *
431
             * If the button is a system button, than it has special functions.
432
             * The action of the button depends on the kind of system button.
433
             */
15 andreas 434
            void registerSystemButton();
435
            bool isSystemButton();
38 andreas 436
            void addPushFunction(std::string& func, std::string& page);
16 andreas 437
            void clearPushFunctions() { pushFunc.clear(); }
438
            void clearPushFunction(const std::string& action);
439
            void refresh();
3 andreas 440
            /**
14 andreas 441
             * Sets a particular instance of the button active. This implies
442
             * a redraw of the button in case the instance is different from
443
             * the one already visible.
444
             *
445
             * @param instance
446
             * The instance of the button to be activated.
447
             *
448
             * @return
449
             * On error returns FALSE.
450
             */
451
            bool setActive(int instance);
452
            /**
453
             * Sets an Icon on the button. This implies a redraw of the button
454
             * in case the instance is different from the one already visible.
455
             *
456
             * @param id
457
             * The id number of the icon.
458
             *
459
             * @param instance
460
             * The instance where the icon should be drawed
461
             *
462
             * @return On error returns FALSE.
463
             */
464
            bool setIcon(int id, int instance);
465
            /**
466
             * Sets an Icon on the button. This implies a redraw of the button
467
             * in case the instance is different from the one already visible.
468
             *
469
             * @param icon
470
             * The file name of the icon.
471
             *
472
             * @param instance
473
             * The instance where the icon should be drawed
474
             *
475
             * @return
476
             * On error returns FALSE.
477
             */
478
            bool setIcon(const std::string& icon, int instance);
479
            /**
480
             * Removes an icon from a button. This implies a redraw of the
481
             * button.
482
             *
483
             * @param instance
484
             * The instance number involved.
485
             *
486
             * @return
487
             * On error returns FALSE.
488
             */
489
            bool revokeIcon(int instance);
490
            /**
491
             * Set a string to a button.
492
             *
493
             * @param txt
494
             * The text to write on top of  a button.
495
             *
496
             * @param instance
497
             * The instance number involved.
498
             *
499
             * @return
500
             * On error returns FALSE.
501
             */
502
            bool setText(const std::string& txt, int instance);
503
            /**
51 andreas 504
             * Set a string to a button. This method does not trigger a new
505
             * drawing of the element.
506
             *
507
             * @param txt
508
             * The text to write on top of  a button.
509
             *
510
             * @param instance
511
             * The instance number involved.
512
             *
513
             * @return
514
             * On error returns FALSE.
515
             */
516
            bool setTextOnly(const std::string& txt, int instance);
517
            /**
43 andreas 518
             * @brief appendText    Append non-unicode text.
519
             * @param txt
520
             * The text to write on top of  a button.
521
             * @param instance
522
             * The instance number involved.
523
             * @return
524
             * On error returns FALSE.
525
             */
526
            bool appendText(const std::string& txt, int instance);
527
            /**
528
             * @brief setBorderColor Set the border color.
529
             * Set the border color to the specified color. Only if the
530
             * specified border color is not the same as the current color.
531
             * Note: Color can be assigned by color name (without spaces),
532
             * number or R,G,B value (RRGGBB or RRGGBBAA).
533
             * @param color     the color
534
             * @param instance
535
             * The instance number involved.
536
             * @return
537
             * On error returns FALSE.
538
             */
539
            bool setBorderColor(const std::string& color, int instance);
540
            /**
82 andreas 541
             * @brief retrieves the current border color.
542
             * Determines the current border color of the button and returns
543
             * the color as a string.
544
             * @param instance  The instance number of the button.
545
             * @return
546
             * If everything went well it returns the color string of the button
547
             * instance. On error an empty string is returned.
548
             */
549
            std::string getBorderColor(int instance);
550
            /**
60 andreas 551
             * @brief setFillColor Set the fill color.
552
             * Set the fill color to the specified color. Only if the
553
             * specified fill color is not the same as the current color.
554
             * Note: Color can be assigned by color name (without spaces),
555
             * number or R,G,B value (RRGGBB or RRGGBBAA).
556
             * @param color     the color
557
             * @param instance
558
             * The instance number involved.
559
             * @return
560
             * On error returns FALSE.
561
             */
562
            bool setFillColor(const std::string& color, int instance);
563
            /**
564
             * @brief setTextColor Set the text color.
565
             * Set the text color to the specified color. Only if the
566
             * specified text color is not the same as the current color.
567
             * Note: Color can be assigned by color name (without spaces),
568
             * number or R,G,B value (RRGGBB or RRGGBBAA).
569
             * @param color     the color
570
             * @param instance
571
             * The instance number involved.
572
             * @return
573
             * On error returns FALSE.
574
             */
575
            bool setTextColor(const std::string& color, int instance);
576
            /**
577
             * @brief setDrawOrder - Set the button draw order.
578
             * Determines what order each layer of the button is drawn.
579
             * @param order     the draw order
580
             * @param instance
581
             * The instance number involved.
582
             * @return
583
             * On error returns FALSE.
584
             */
585
            bool setDrawOrder(const std::string& order, int instance);
586
            /**
587
             * @brief setFeedback - Set the feedback type of the button.
588
             * ONLY works on General-type buttons.
589
             * @param fb    The feedback type
590
             * @return On error returns FALSE.
591
             */
592
            bool setFeedback(FEEDBACK feedback);
593
            /**
594
             * Set a border to a specific border style associated with a border
595
             * value for those buttons with a defined address range.
596
             * @param style     The name of the border style
597
             * @param instance  -1 = style for all instances
598
             * >= 0 means the style is valid only for this instance.
599
             */
600
            bool setBorderStyle(const std::string& style, int instance=-1);
601
            /**
602
             * Set the bargraph upper limit. Range = 1 to 65535.
603
             * @param limit the new limit
604
             * @return TRUE on success.
605
             */
606
            bool setBargraphUpperLimit(int limit);
607
            /**
608
             * Set the bargraph lower limit. Range = 1 to 65535.
609
             * @param limit the new limit
610
             * @return TRUE on success.
611
             */
612
            bool setBargraphLowerLimit(int limit);
613
            /**
3 andreas 614
             * Read the images, if any, from files and create an image. If there
615
             * are no files, draw the image as defined.
14 andreas 616
             *
40 andreas 617
             * @param force
618
             * This parameter forces the function to reload the image. This is
619
             * necessary if the image of the button was changed by a command.
620
             * This parameter is optional. Defaults to FALSE.
621
             *
14 andreas 622
             * @return
623
             * On error returns FALSE.
3 andreas 624
             */
40 andreas 625
            bool createButtons(bool force = false);
4 andreas 626
            /**
53 andreas 627
             * Register a callback function to display a ready image. This
628
             * function is used for nearly every kind of button or bargraph.
629
             * It is up to the surface to bring the buttons to screen.
4 andreas 630
             */
6 andreas 631
            void registerCallback(std::function<void (ulong handle, ulong parent, unsigned char *buffer, int width, int height, int pixline, int left, int top)> displayButton)
4 andreas 632
            {
633
                _displayButton = displayButton;
634
            }
53 andreas 635
            /**
636
             * Register a callback function to display a video on a special
637
             * button. It is up to the surface to display a video. So the
638
             * callback passes only the size, position and the url together
639
             * with a user name and password, if there is one.
640
             */
21 andreas 641
            void regCallPlayVideo(std::function<void (ulong handle, ulong parent, int left, int top, int width, int height, const std::string& url, const std::string& user, const std::string& pw)> playVideo) { _playVideo = playVideo; };
7 andreas 642
 
4 andreas 643
            /**
644
             * Make a pixel array and call the callback function to display the
645
             * image. If there is no callback function registered, nothing
646
             * happens.
15 andreas 647
             * This method draws a general button. It is used for most
648
             * specialized buttons too.
14 andreas 649
             *
15 andreas 650
             * @param instance
651
             * Optional. The instance of the button to draw. If the base
652
             * instance should be drawn, the parameter can be omitted.
653
             *
654
             * @param show
655
             * Optional. By defualt TRUE. If set to FALSE, the button image is
656
             * not send to GUI. The image is available in the variable
657
             * "mLastImage" immediately after ending the method.
658
             *
14 andreas 659
             * @return
660
             * On error returns FALSE.
4 andreas 661
             */
50 andreas 662
            bool drawButton(int instance=0, bool show=true);
6 andreas 663
            /**
50 andreas 664
             * Creates a pixel array and holds it for access through a callback
665
             * function which receives this class as a whole. The callback
666
             * function displays a keyboard and handles input to this text area.
667
             * The created image can be obtained by calling the function
668
             * getLastImage().
669
             *
670
             * @param instance
671
             * Optional. The instance of the button to draw. If the base
672
             * instance should be drawn, the parameter can be omitted.
673
             *
674
             * @param show
675
             * Optional. By defualt TRUE. If set to FALSE, the button image is
676
             * not send to GUI. The image is available in the variable
677
             * "mLastImage" immediately after ending the method.
678
             *
679
             * @return
680
             * On error returns FALSE.
681
             */
682
            bool drawTextArea(int instance=0);
683
            /**
15 andreas 684
             * Method to draw a multistate animated button. It creates a thread
685
             * with a timer and starts an animation by drawing every instance in
686
             * the defined order.
687
             *
688
             * @return
689
             * On error returns FALSE.
690
             */
691
            bool drawButtonMultistateAni();
692
            /**
693
             * Draws a normal bargraph with an ON and OFF state. Takes care
694
             * about cameleon images. If registered, it calls a callback
695
             * function to the GUI to display the ready image.
696
             *
697
             * @param instance
698
             * The instance of the bargraph to draw. This value must be in the
699
             * range of 0 to "stateCount".
700
             *
701
             * @param level
702
             * This is the level to show on the bargraph. It must be a value
703
             * the range of "rl" (range low) and "rh" (range high). A lower
704
             * value than "rl" is interpreted as "rl" and a value higher than
705
             * "rh" is interpreted as "rh".
706
             *
707
             * @param show
708
             * Optional. By default TRUE. If set to FALSE, the bargraph image is
709
             * not send to GUI. The image is available in the variable
710
             * "mLastImage" immediately after ending the method.
711
             *
712
             * @return
713
             * On error returns FALSE.
714
             */
715
            bool drawBargraph(int instance, int level, bool show=true);
716
            /**
38 andreas 717
             * @brief drawMultistateBargraph draws a bargraph comparable to a button.
718
             * This method draws a multistate bargraph. This is like many
719
             * buttons in one. But they behave like a bargraph. This means, that
720
             * it depends on the level how many buttons are ON and which are OFF.
721
             *
722
             * @param level The level. This defines how many buttons are at ON state.
723
             * @param show  If this is false, the button is invisible.
724
             * @return If everything went well, TRUE is returned. If an error
725
             * occurred it returns FALSE.
726
             */
727
            bool drawMultistateBargraph(int level, bool show=true);
728
            /**
6 andreas 729
             * Show the button with it's current state.
730
             */
731
            void show();
11 andreas 732
            /**
15 andreas 733
             * Hide the button. In case of an animated button, the animation is
734
             * stopped.
735
             *
736
             * @param total
737
             * Optional. When set to TRUE, a transparent button is displayed.
738
             */
739
            void hide(bool total=false);
740
            /**
741
             * This method sends the image together with all data to the GUI,
742
             * where it will be shown. The variable mLastImage must not be
743
             * empty for the method to succeed.
744
             */
745
            void showLastButton();
746
            /**
11 andreas 747
             * Handle a mouse click.
15 andreas 748
             *
38 andreas 749
             * @param x
750
             * The x coordinate of the mouse press
751
             *
752
             * @param y
753
             * The y coordinate of the mouse press
754
             *
15 andreas 755
             * @param pressed
756
             * TRUE = Button was pressed, FALSE = Button was released.
757
             *
758
             * @return
759
             * TRUE = Button was clickable and processed. FALSE = Button was not
760
             * clickable.
11 andreas 761
             */
15 andreas 762
            bool doClick(int x, int y, bool pressed);
43 andreas 763
            /**
764
             * Creates a button but uses the informations in the structure
765
             * instead of reading it from a file.
766
             *
767
             * @param bt    A structure containing all informations to create a button.
768
             * @return On success returns TRUE, else FALSE.
769
             */
770
            bool createSoftButton(const EXTBUTTON_t& bt);
50 andreas 771
            /**
772
             * Returns the image in mLastImage as a pixel array with the defined
773
             * dimensions.
774
             *
775
             * @return A typedef IMAGE_t containing the ingredentials of the
776
             * image. It contains a pointer with allocated memory holding the
777
             * image. This buffer must NOT be freed!
778
             */
779
            BITMAP_t getLastImage();
780
            /**
781
             * Returns the fint the button uses.
782
             * @return A structure containing the informations for the font
783
             * to load.
784
             */
785
            FONT_T getFont();
786
            /**
787
             * Returns the style of the font.
788
             * @return The font style.
789
             */
51 andreas 790
            FONT_STYLE getFontStyle();
3 andreas 791
 
792
        protected:
793
            BUTTONTYPE getButtonType(const std::string& bt);
794
            FEEDBACK getButtonFeedback(const std::string& fb);
10 andreas 795
            SkBitmap drawImageButton(SkBitmap& imgRed, SkBitmap& imgMask, int width, int height, SkColor col1, SkColor col2);
99 andreas 796
            SkBitmap combineImages(SkBitmap& base, SkBitmap& alpha, SkColor col);
3 andreas 797
 
15 andreas 798
            void funcTimer(const amx::ANET_BLINK& blink);
799
            void funcNetwork(int state);
94 andreas 800
            void funcResource(const RESOURCE_T *resource, const std::string& url, BITMAP_CACHE bc, int instance);
38 andreas 801
            void funcBattery(int level, bool charging, int chargeType);
36 andreas 802
            void funcNetworkState(int level);
15 andreas 803
 
3 andreas 804
        private:
805
            typedef struct IMAGE_t
806
            {
807
                int number{0};
6 andreas 808
                SkBitmap imageMi;
809
                SkBitmap imageBm;
3 andreas 810
 
811
                void clear()
812
                {
813
                    number = 0;
6 andreas 814
                    imageMi.reset();
3 andreas 815
                    imageBm.reset();
816
                }
817
            }IMAGE_t;
818
 
6 andreas 819
            std::function<void (ulong handle, ulong parent, unsigned char *buffer, int width, int height, int pixline, int left, int top)> _displayButton{nullptr};
21 andreas 820
            std::function<void (ulong handle, ulong parent, int left, int top, int width, int height, const std::string& url, const std::string& user, const std::string& pw)> _playVideo{nullptr};
6 andreas 821
 
40 andreas 822
            POSITION_t calcImagePosition(int width, int height, CENTER_CODE cc, int number, int line = 0);
99 andreas 823
            IMAGE_SIZE_t calcImageSize(int imWidth, int imHeight, int instance, bool aspect=false);
4 andreas 824
            int getBorderSize(const std::string& name);
825
            void calcImageSizePercent(int imWidth, int imHeight, int btWidth, int btHeight, int btFrame, int *realX, int *realY);
10 andreas 826
            SkColor baseColor(SkColor basePix, SkColor maskPix, SkColor col1, SkColor col2);
7 andreas 827
            TEXT_EFFECT textEffect(const std::string& effect);
8 andreas 828
            int numberLines(const std::string& str);
829
            SkRect calcRect(int width, int height, int pen);
15 andreas 830
            void runAnimation();    // Method started as thread for button animation
38 andreas 831
            void runAnimationRange(int start, int end, ulong step);     // Method started as thread for a limited time
99 andreas 832
            bool drawAlongOrder(SkBitmap *imgButton, int instance);
3 andreas 833
 
8 andreas 834
            void getDrawOrder(const std::string& sdo, DRAW_ORDER *order);
835
            bool buttonFill(SkBitmap *bm, int instance);
836
            bool buttonBitmap(SkBitmap *bm, int instance);
97 andreas 837
            bool buttonDynamic(SkBitmap *bm, int instance, bool show, bool *state=nullptr);
8 andreas 838
            bool buttonIcon(SkBitmap *bm, int instance);
839
            bool buttonText(SkBitmap *bm, int instance);
840
            bool buttonBorder(SkBitmap *bm, int instance);
15 andreas 841
            bool isClickable();
842
            bool isPixelTransparent(int x, int y);
843
            bool barLevel(SkBitmap *bm, int instance, int level);
844
            bool makeElement(int instance=-1);
21 andreas 845
            bool loadImage(SkBitmap *bm, SkBitmap& image, int instance);
15 andreas 846
            void _TimerCallback(ulong counter);
21 andreas 847
            void _imageRefresh(const std::string& url);
848
            static THR_REFRESH_t *_addResource(TImageRefresh *refr, ulong handle, ulong parent, int bi);
849
            static THR_REFRESH_t *_findResource(ulong handle, ulong parent, int bi);
69 andreas 850
            int calcLineHeight(const std::string& text, SkFont& font);
66 andreas 851
            bool textEffect(SkCanvas *canvas, sk_sp<SkTextBlob>& blob, SkScalar startX, SkScalar startY, int instance);
69 andreas 852
            std::string getFormatString(TEXT_ORIENTATION to);
71 andreas 853
            bool checkForSound();
79 andreas 854
            bool scaleImage(SkBitmap *bm, double scaleWidth, double scaleHeight);
855
            bool stretchImageWidth(SkBitmap *bm, int width);
856
            bool stretchImageHeight(SkBitmap *bm, int height);
81 andreas 857
            bool colorImage(SkBitmap *img, int width, int height, SkColor col, SkColor bg=0, bool useBG=false);
99 andreas 858
            bool retrieveImage(const std::string& path, SkBitmap *image);
859
            SkBitmap drawSliderButton(const std::string& slider, SkColor col);
8 andreas 860
 
94 andreas 861
            void addToBitmapCache(BITMAP_CACHE& bc);
862
            BITMAP_CACHE& getBCentryByHandle(ulong handle, ulong parent);
863
            BITMAP_CACHE& getBCentryByBI(int bIdx);
864
            void removeBCentry(std::vector<BITMAP_CACHE>::iterator *elem);
865
            void setReady(ulong handle);
97 andreas 866
            void setInvalid(ulong handle);
94 andreas 867
            void setBCBitmap(ulong handle, SkBitmap& bm);
868
            void showBitmapCache();
869
 
3 andreas 870
            BUTTONTYPE type;
871
            int bi{0};              // button ID
872
            std::string na;         // name
873
            std::string bd;         // Description --> ignored
874
            int lt{0};              // pixel from left
875
            int tp{0};              // pixel from top
876
            int wt{0};              // width
877
            int ht{0};              // height
878
            int zo{0};              // Z-Order
879
            std::string hs;         // bounding, ...
880
            std::string bs;         // Border style (circle, ...)
41 andreas 881
            FEEDBACK fb{FB_NONE};   // Feedback type (momentary, ...)
14 andreas 882
            int ap{1};              // Address port (default: 1)
3 andreas 883
            int ad{0};              // Address channel
884
            int ch{0};              // Channel number
14 andreas 885
            int cp{1};              // Channel port (default: 1)
886
            int lp{1};              // Level port (default: 1)
3 andreas 887
            int lv{0};              // Level code
888
            std::string dr;         // Level "horizontal" or "vertical"
889
            int va{0};
890
            int stateCount{0};      // State count with multistate buttons
891
            int rm{0};              // State count with multistate buttons?
15 andreas 892
            int nu{2};              // Animate time up
893
            int nd{2};              // Animate time down
3 andreas 894
            int ar{0};              // Auto repeat (1 = true)
895
            int ru{0};              // Animate time up (bargraph)
896
            int rd{0};              // Animate time down (bargraph)
897
            int lu{0};              // Animate time up (Bargraph)
898
            int ld{0};              // Animate time down (Bargraph)
899
            int rv{0};
900
            int rl{0};              // Range low
901
            int rh{0};              // Range high
902
            int ri{0};              // Bargraph inverted (0 = normal, 1 = inverted)
903
            int rn{0};              // Bargraph: Range drag increment
51 andreas 904
            int ac_di{0};           // ???
905
            int hd{0};              // 1 = Hidden, 0 = Normal visible
906
            int da{0};              // 1 = Disabled, 0 = Normal active
49 andreas 907
            std::string lf;         // Bargraph function: empty = display only, active, active centering, drag, drag centering
3 andreas 908
            std::string sd;         // Name/Type of slider for a bargraph
909
            std::string sc;         // Color of slider (for bargraph)
910
            int mt{0};              // Length of text area (0 = 2000)
911
            std::string dt;         // "multiple" textarea has multiple lines, else single line
912
            std::string im;         // Input mask of a text area
51 andreas 913
            std::string pc;         // Password character for text area
3 andreas 914
            std::string op;         // String the button send
14 andreas 915
            bool visible{true};     // TRUE=Button is visible
3 andreas 916
            std::vector<PUSH_FUNC_T> pushFunc;  // Push functions: This are executed on button press
917
            std::vector<SR_T> sr;   // The elements the button consists of
41 andreas 918
 
7 andreas 919
            TPalette *mPalette{nullptr}; // The color palette
3 andreas 920
            // Image management
921
            std::map<int, IMAGE_t> mImages; // Contains the images in ready to use format, if there any
6 andreas 922
            SkBitmap mLastImage;    // The last calculated image
5 andreas 923
            ulong mHandle{0};       // internal used handle to identify button
7 andreas 924
            int mParentHeight{0};   // The height of the parent page / subpage
925
            int mParentWidth{0};    // The width of the parent page / subpage
16 andreas 926
            bool mEnabled{true};    // By default a button is enabled (TRUE); FALSE = Button disabled
7 andreas 927
            TFont *mFonts{nullptr}; // The font table
10 andreas 928
            int mGlobalOO{-1};      // Opacity of the whole subpage, if any
14 andreas 929
            int mActInstance{0};    // Active instance
8 andreas 930
            DRAW_ORDER mDOrder[ORD_ELEM_COUNT];  // The order to draw the elements of a button
15 andreas 931
            std::thread mThrAni;    // Thread handle for animation
21 andreas 932
            std::thread mThrRes;    // A resouce (download of a remote image/video) running in background
38 andreas 933
            std::atomic<bool> mAniRunning{false}; // TRUE = Animation is running
93 andreas 934
            std::atomic<bool> mAniStop{false};  // If TRUE, the running animation will stop
15 andreas 935
            int mLastLevel{0};      // The last level value for a bargraph
936
            bool mSystemReg{false}; // TRUE = registered as system button
937
            amx::ANET_BLINK mLastBlink; // This is used for the system clock buttons
21 andreas 938
            TTimer *mTimer{nullptr};    // This is for buttons displaying the time or a date. It's a thread running in background.
939
            static THR_REFRESH_t *mThrRefresh;  // If  we have a source to reread periodicaly, this starts a thread to do that.
38 andreas 940
            ulong mAniRunTime{0};   // The time in milliseconds an animation should run. 0 = run forever.
94 andreas 941
            BITMAP_CACHE mBCDummy;  // A dummy retuned in case no cache exists or the element was not found.
3 andreas 942
    };
943
 
944
    typedef struct BUTTONS_T
945
    {
946
        TButton *button{nullptr};
947
        BUTTONS_T *previous{nullptr};
948
        BUTTONS_T *next{nullptr};
949
    } BUTTONS_T;
2 andreas 950
}
951
 
952
#endif