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