GRAPHICS: add title to 9-patch image

This commit is contained in:
ysj1173886760 2021-03-31 23:01:33 +08:00 committed by Eugene Sandulenko
parent b2b8b17133
commit 179654939e
2 changed files with 32 additions and 16 deletions

View File

@ -61,11 +61,12 @@ NinePatchSide::~NinePatchSide() {
}
bool NinePatchSide::init(Graphics::TransparentSurface *bmp, bool vertical) {
bool NinePatchSide::init(Graphics::TransparentSurface *bmp, bool vertical, int titleIndex, int titleWidth) {
const uint len = vertical ? bmp->h : bmp->w;
uint i;
int s, t, z;
int index = 0;
_m.clear();
for (i = 1, s = -1, t = 0, z = -1; i < len; ++i) {
@ -97,7 +98,10 @@ bool NinePatchSide::init(Graphics::TransparentSurface *bmp, bool vertical) {
} else {
mrk->ratio = 0;
}
// if there is title, then we try to recalc the t, because we want to fix the title width
if (titleIndex > 0 && index == titleIndex) t -= mrk->length;
_m.push_back(mrk);
index++;
}
s = i;
z = zz;
@ -113,21 +117,29 @@ bool NinePatchSide::init(Graphics::TransparentSurface *bmp, bool vertical) {
return true;
}
void NinePatchSide::calcOffsets(int len) {
void NinePatchSide::calcOffsets(int len, int titleIndex, int titleWidth) {
uint i, j;
int dest_offset = 0;
int remaining_stretch = len - _fix;
// if we don't got titleIndex, then we better set titleWidth to 0
if (titleIndex == 0) titleWidth = 0;
int remaining_stretch = len - _fix - titleWidth;
// if titleWidth is too big, which may cause the remaining_stretch be 0, so we check it here
if (remaining_stretch < 0) remaining_stretch = 0;
for (i = 0, j = 0; i < _m.size(); ++i) {
_m[i]->dest_offset = dest_offset;
if (_m[i]->ratio == 0) {
_m[i]->dest_length = _m[i]->length;
} else {
_m[i]->dest_length = (len - _fix) * _m[i]->ratio;
remaining_stretch -= _m[i]->dest_length;
j = i;
}
if (titleIndex > 0 && i == (uint)titleIndex) {
_m[i]->dest_length = titleWidth;
} else {
if (_m[i]->ratio == 0) {
_m[i]->dest_length = _m[i]->length;
} else {
_m[i]->dest_length = (len - _fix - titleWidth) * _m[i]->ratio;
remaining_stretch -= _m[i]->dest_length;
j = i;
}
}
dest_offset += _m[i]->dest_length;
}
@ -138,7 +150,7 @@ void NinePatchSide::calcOffsets(int len) {
}
}
NinePatchBitmap::NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bitmap) {
NinePatchBitmap::NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bitmap, int titleIndex, int titleWidth) {
int i;
uint8 r, g, b, a;
@ -150,6 +162,8 @@ NinePatchBitmap::NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bi
_cached_dh = 0;
_width = bmp->w - 2;
_height = bmp->h - 2;
_titleIndex = titleIndex;
_titleWidth = titleWidth;
if (_width <= 0 || _height <= 0)
goto bad_bitmap;
@ -199,7 +213,7 @@ NinePatchBitmap::NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bi
++i;
}
if (!_h.init(bmp, false) || !_v.init(bmp, true)) {
if (!_h.init(bmp, false, titleIndex, titleWidth) || !_v.init(bmp, true)) {
bad_bitmap:
warning("NinePatchBitmap::NinePatchBitmap(): Bad bitmap");
@ -213,6 +227,7 @@ void NinePatchBitmap::blit(Graphics::Surface &target, int dx, int dy, int dw, in
if (dw < _h._fix || dh < _v._fix)
return;
if (dw < _h._fix + _titleWidth) dw = _h._fix + _titleWidth;
/* if the bitmap is the same size as the origin, then draw it as-is */
if (dw == _width && dh == _height) {
Common::Rect r(1, 1, dw, dh);
@ -223,7 +238,7 @@ void NinePatchBitmap::blit(Graphics::Surface &target, int dx, int dy, int dw, in
/* only recalculate the offsets if they have changed since the last draw */
if (_cached_dw != dw || _cached_dh != dh) {
_h.calcOffsets(dw);
_h.calcOffsets(dw, _titleIndex, _titleWidth);
_v.calcOffsets(dh);
_cached_dw = dw;

View File

@ -72,8 +72,8 @@ public:
NinePatchSide() : _fix(0) { _m.clear(); }
~NinePatchSide();
bool init(Graphics::TransparentSurface *bmp, bool vertical);
void calcOffsets(int len);
bool init(Graphics::TransparentSurface *bmp, bool vertical, int titleIndex = 0, int titleWidth = 0);
void calcOffsets(int len, int titleIndex = 0, int titleWidth = 0);
};
class NinePatchBitmap {
@ -83,10 +83,11 @@ class NinePatchBitmap {
bool _destroy_bmp;
int _width, _height;
int _cached_dw, _cached_dh;
int _titleIndex, _titleWidth;
Common::HashMap<uint32, int> _cached_colors;
public:
NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bitmap);
NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bitmap, int titleIndex = 0, int titleWidth = 0);
~NinePatchBitmap();
void blit(Graphics::Surface &target, int dx, int dy, int dw, int dh, byte *palette = NULL, int numColors = 0, MacWindowManager *wm = NULL, uint32 transColor = 0);