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