Subversion Repositories tpanel

Rev

Rev 446 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 446 Rev 486
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (C) 2021 by Andreas Theofilu <andreas@theosys.at>
2
 * Copyright (C) 2021 to 2025 by Andreas Theofilu <andreas@theosys.at>
3
 *
3
 *
4
 * This program is free software; you can redistribute it and/or modify
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
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
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
7
 * (at your option) any later version.
Line 19... Line 19...
19
#include <include/core/SkBitmap.h>
19
#include <include/core/SkBitmap.h>
20
 
20
 
21
#include "tdrawimage.h"
21
#include "tdrawimage.h"
22
#include "tbutton.h"
22
#include "tbutton.h"
23
#include "terror.h"
23
#include "terror.h"
-
 
24
#include "ttpinit.h"
-
 
25
#include "timgcache.h"
24
#include <qglobal.h>
26
#include <qglobal.h>
25
 
27
 
26
extern bool prg_stopped;
28
extern bool prg_stopped;
27
 
29
 
28
using std::string;
30
using std::string;
Line 34... Line 36...
34
}
36
}
35
 
37
 
36
TDrawImage::~TDrawImage()
38
TDrawImage::~TDrawImage()
37
{
39
{
38
    DECL_TRACER("TDrawImage::~TDrawImage()");
40
    DECL_TRACER("TDrawImage::~TDrawImage()");
-
 
41
 
-
 
42
    if (TTPInit::isTP5() && !mBitmapStack.empty())
-
 
43
        mBitmapStack.clear();
-
 
44
}
-
 
45
 
-
 
46
void TDrawImage::setImageBm(SkBitmap& bm)
-
 
47
{
-
 
48
    DECL_TRACER("TDrawImage::setImageBm(SkBitmap& bm)");
-
 
49
 
-
 
50
    if (TTPInit::isTP5())
-
 
51
    {
-
 
52
        int width = bm.info().width();
-
 
53
        int height = bm.info().height();
-
 
54
        size_t index = mBitmapStack.empty() ? 0 : (mBitmapStack.size() - 1);
-
 
55
 
-
 
56
        mBitmapStack.push_back(bm);         // Put image on the stack
-
 
57
 
-
 
58
        if (imageBm.empty())                // If this is the 1st image, allocate the space for it
-
 
59
        {
-
 
60
            imageBm.allocN32Pixels(width, height);
-
 
61
            imageBm.eraseColor(SK_ColorTRANSPARENT);
-
 
62
        }
-
 
63
 
-
 
64
        MSG_DEBUG("Image info: Width: " << width << ", Height: " << height << ", Instance: " << mInstance << ", Index: " << index);
-
 
65
        Button::POSITION_t position = calcImagePosition(width, height, mInstance, index);
-
 
66
 
-
 
67
        if (!position.valid)
-
 
68
        {
-
 
69
            MSG_ERROR("Error calculating the position of the image!");
-
 
70
            TError::setError();
-
 
71
            return;
-
 
72
        }
-
 
73
        // Draw the image to the target. If there was a previous one, the new one is blended over
-
 
74
        SkPaint paint;
-
 
75
        SkCanvas can(imageBm, SkSurfaceProps(1, kUnknown_SkPixelGeometry));
-
 
76
        paint.setBlendMode(SkBlendMode::kSrcOver);
-
 
77
 
-
 
78
        if (mSr[mInstance].sb == 0)
-
 
79
        {
-
 
80
            sk_sp<SkImage> _image = SkImages::RasterFromBitmap(bm);
-
 
81
            can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
-
 
82
        }
-
 
83
        else    // Scale to fit
-
 
84
        {
-
 
85
            SkRect rect = SkRect::MakeXYWH(position.left, position.top, position.width, position.height);
-
 
86
            sk_sp<SkImage> im = SkImages::RasterFromBitmap(bm);
-
 
87
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
-
 
88
        }
-
 
89
    }
-
 
90
    else
-
 
91
        imageBm = bm;
-
 
92
}
-
 
93
 
-
 
94
SkBitmap& TDrawImage::getImageBm(size_t index)
-
 
95
{
-
 
96
    DECL_TRACER("TDrawImage::getImageBm(size_t index)");
-
 
97
 
-
 
98
    if (TTPInit::isTP5())
-
 
99
    {
-
 
100
        if (index >= mBitmapStack.size())
-
 
101
            return imageBm;
-
 
102
 
-
 
103
        return mBitmapStack[index];
-
 
104
    }
-
 
105
 
-
 
106
    return imageBm;
39
}
107
}
40
 
108
 
41
bool TDrawImage::drawImage(SkBitmap* bm)
109
bool TDrawImage::drawImage(SkBitmap* bm)
42
{
110
{
43
    DECL_TRACER("TDrawImage::drawImage(SkBitmap* bm)");
111
    DECL_TRACER("TDrawImage::drawImage(SkBitmap* bm)");
Line 68... Line 136...
68
    {
136
    {
69
        MSG_TRACE("Chameleon image ...");
137
        MSG_TRACE("Chameleon image ...");
70
        SkBitmap imgRed(imageMi);
138
        SkBitmap imgRed(imageMi);
71
        SkBitmap imgMask;
139
        SkBitmap imgMask;
72
 
140
 
73
        if (!imageBm.empty() && !mSr[instance].bm.empty())
141
        if ((!TTPInit::isTP5() && !imageBm.empty() && !mSr[instance].bm.empty()) || (TTPInit::isTP5() && !imageBm.empty()))
74
            imgMask.installPixels(imageBm.pixmap());
142
            imgMask.installPixels(imageBm.pixmap());
75
        else
143
        else
76
        {
144
        {
77
            imgMask.allocN32Pixels(mSr[instance].mi_width, mSr[instance].mi_height);
145
            imgMask.allocN32Pixels(mSr[instance].mi_width, mSr[instance].mi_height);
78
            imgMask.eraseColor(SK_ColorTRANSPARENT);
146
            imgMask.eraseColor(SK_ColorTRANSPARENT);
Line 92... Line 160...
92
        SkPaint paint;
160
        SkPaint paint;
93
        paint.setBlendMode(SkBlendMode::kSrcOver);
161
        paint.setBlendMode(SkBlendMode::kSrcOver);
94
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgMask);
162
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgMask);
95
        ctx.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
163
        ctx.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
96
 
164
 
97
        Button::POSITION_t position = calcImagePosition(mSr[instance].mi_width, mSr[instance].mi_height, instance);
165
        Button::POSITION_t position = calcImagePosition(mSr[instance].mi_width, mSr[instance].mi_height, instance, mBitmapStack.size() - 1);
98
 
166
 
99
        if (!position.valid)
167
        if (!position.valid)
100
        {
168
        {
101
            MSG_ERROR("Error calculating the position of the image!");
169
            MSG_ERROR("Error calculating the position of the image!");
102
            TError::setError();
170
            TError::setError();
Line 116... Line 184...
116
            SkRect rect = SkRect::MakeXYWH(position.left, position.top, position.width, position.height);
184
            SkRect rect = SkRect::MakeXYWH(position.left, position.top, position.width, position.height);
117
            sk_sp<SkImage> im = SkImages::RasterFromBitmap(img);
185
            sk_sp<SkImage> im = SkImages::RasterFromBitmap(img);
118
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
186
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
119
        }
187
        }
120
    }
188
    }
121
    else if (!imageBm.empty() && !mSr[instance].bm.empty())
189
    else if ((!TTPInit::isTP5() &&!imageBm.empty() && !mSr[instance].bm.empty()) || (TTPInit::isTP5() &&!imageBm.empty()))
122
    {
190
    {
123
        MSG_TRACE("Drawing normal image ...");
191
        MSG_TRACE("Drawing normal image ...");
124
        SkBitmap image = imageBm;
192
        SkBitmap image = imageBm;
125
 
193
 
126
        if (image.empty())
194
        if (image.empty())
127
        {
195
        {
-
 
196
            if (TTPInit::isTP5())
-
 
197
            {
-
 
198
                MSG_ERROR("Error creating the image \"" << mSr[instance].bitmaps[mBitmapStack.size()-1].fileName << "\"!");
-
 
199
            }
-
 
200
            else
-
 
201
            {
128
            MSG_ERROR("Error creating the image \"" << mSr[instance].bm << "\"!");
202
                MSG_ERROR("Error creating the image \"" << mSr[instance].bm << "\"!");
-
 
203
            }
-
 
204
 
129
            TError::setError();
205
            TError::setError();
130
            return false;
206
            return false;
131
        }
207
        }
132
 
208
 
-
 
209
        Button::POSITION_t position;
-
 
210
 
-
 
211
        if (!TTPInit::isTP5())
133
        Button::POSITION_t position = calcImagePosition(mSr[instance].bm_width, mSr[instance].bm_height, instance);
212
            position = calcImagePosition(mSr[instance].bm_width, mSr[instance].bm_height, instance);
-
 
213
        else
-
 
214
            position = calcImagePosition(imageBm.info().width(), imageBm.info().height(), instance);
134
 
215
 
135
        if (!position.valid)
216
        if (!position.valid)
136
        {
217
        {
137
            MSG_ERROR("Error calculating the position of the image.");
218
            MSG_ERROR("Error calculating the position of the image.");
138
            TError::setError();
219
            TError::setError();
Line 144... Line 225...
144
        paint.setBlendMode(SkBlendMode::kSrcOver);
225
        paint.setBlendMode(SkBlendMode::kSrcOver);
145
        SkCanvas can(*bm, SkSurfaceProps(1, kUnknown_SkPixelGeometry));
226
        SkCanvas can(*bm, SkSurfaceProps(1, kUnknown_SkPixelGeometry));
146
 
227
 
147
        if (mSr[instance].sb == 0)
228
        if (mSr[instance].sb == 0)
148
        {
229
        {
-
 
230
            size_t index = mBitmapStack.size() > 0 ? (mBitmapStack.size() - 1) : 0;
-
 
231
            Button::BITMAPS_t bitmap;
-
 
232
 
-
 
233
            if (mSr[instance].bitmaps.size() > index)
-
 
234
                bitmap = mSr[instance].bitmaps[index];
-
 
235
 
149
            if ((mSr[instance].jb == 0 && mSr[instance].bx >= 0 && mSr[instance].by >= 0) || mSr[instance].jb != 0)  // Draw the full image
236
            if ((!TTPInit::isTP5() && ((mSr[instance].jb == 0 && mSr[instance].bx >= 0 && mSr[instance].by >= 0) || mSr[instance].jb != 0)) ||  // Draw the full image
-
 
237
                (TTPInit::isTP5() && ((bitmap.justification == 0 && bitmap.offsetX >= 0 && bitmap.offsetY >= 0) || bitmap.justification != 0)))
150
            {
238
            {
151
                MSG_DEBUG("Drawing full image ...");
239
                MSG_DEBUG("Drawing full image ...");
152
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(image);
240
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(image);
153
                can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
241
                can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
154
            }
242
            }
Line 304... Line 392...
304
        return col2;
392
        return col2;
305
 
393
 
306
    return SK_ColorTRANSPARENT; // transparent pixel
394
    return SK_ColorTRANSPARENT; // transparent pixel
307
}
395
}
308
 
396
 
309
Button::POSITION_t TDrawImage::calcImagePosition(int width, int height, int number)
397
Button::POSITION_t TDrawImage::calcImagePosition(int width, int height, int number, size_t index)
310
{
398
{
311
    DECL_TRACER("TDrawImage::calcImagePosition(int with, int height, CENTER_CODE code, int number)");
399
    DECL_TRACER("TDrawImage::calcImagePosition(int with, int height, CENTER_CODE code, int number, size_t index)");
312
 
400
 
313
    Button::SR_T act_sr;
401
    Button::SR_T act_sr;
314
    Button::POSITION_t position;
402
    Button::POSITION_t position;
315
    int ix, iy;
403
    int ix, iy;
316
 
404
 
317
    if (mSr.size() == 0)
405
    if (mSr.size() == 0)
-
 
406
    {
-
 
407
        MSG_WARNING("Found no stage element! Can't calculate position.");
318
        return position;
408
        return position;
-
 
409
    }
319
 
410
 
320
    if (number <= 0)
411
    if (number <= 0)
321
        act_sr = mSr.at(0);
412
        act_sr = mSr.at(0);
322
    else if ((size_t)number < mSr.size())
413
    else if ((size_t)number < mSr.size())
323
        act_sr = mSr.at(number);
414
        act_sr = mSr.at(number);
Line 329... Line 420...
329
    int border_size = mBorderSize;
420
    int border_size = mBorderSize;
330
    int code, border = border_size;
421
    int code, border = border_size;
331
    string dbgCC;
422
    string dbgCC;
332
    int rwt = 0, rht = 0;
423
    int rwt = 0, rht = 0;
333
 
424
 
-
 
425
    if (TTPInit::isTP5())
-
 
426
    {
-
 
427
        if (index < act_sr.bitmaps.size())
-
 
428
        {
-
 
429
            code = act_sr.bitmaps[index].justification;
-
 
430
            ix = act_sr.bitmaps[index].offsetX;
-
 
431
            iy = act_sr.bitmaps[index].offsetY;
-
 
432
        }
-
 
433
        else
-
 
434
        {
-
 
435
            code = 5;
-
 
436
            ix = iy = 0;
-
 
437
        }
-
 
438
    }
-
 
439
    else
-
 
440
    {
334
    code = act_sr.jb;
441
        code = act_sr.jb;
335
    ix = act_sr.bx;
442
        ix = act_sr.bx;
336
    iy = act_sr.by;
443
        iy = act_sr.by;
-
 
444
    }
-
 
445
 
337
    rwt = std::min(mWidth - border * 2, width);
446
    rwt = std::min(mWidth - border * 2, width);
338
    rht = std::min(mHeight - border_size * 2, height);
447
    rht = std::min(mHeight - border_size * 2, height);
339
 
448
 
340
    if (width > rwt || height > rht)
449
    if (width > rwt || height > rht)
341
        position.overflow = true;
450
        position.overflow = true;