Skip to content

Commit

Permalink
Added support of labels without background image by specifying DispIm…
Browse files Browse the repository at this point in the history
…ageNum=0 GrandOrgue#1386
  • Loading branch information
oleg68 committed Feb 25, 2023
1 parent 672ed1e commit 27a0f3d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Added support of labels without a background image by specifying DispImageNum=0 https://github.com/GrandOrgue/grandorgue/issues/1386
- Added capability of overriding wav MIDIPitchFraction with the Pipe999MIDIPitchFraction key https://github.com/GrandOrgue/grandorgue/issues/1378
# 3.10.1 (2022-02-24)
- Fixed crash on loading an incorrect organ
Expand Down
5 changes: 3 additions & 2 deletions help/grandorgue.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8825,8 +8825,9 @@ group label font of the panel.
<term>DispImageNum</term>
<listitem>
<para>
(integer 1 - 12, default: 1) Builtin bitmap set to use.
</para>
(integer 0 - 12, default: 1) Builtin bitmap set to use. 0 means
the lable does not have a background image.
</para>
</listitem>
</varlistentry>
<varlistentry>
Expand Down
176 changes: 105 additions & 71 deletions src/grandorgue/gui/GOGUILabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "config/GOConfigReader.h"
#include "control/GOLabelControl.h"

#include "GOBitmap.h"
#include "GODC.h"
#include "GOGUIDisplayMetrics.h"
#include "GOGUILayoutEngine.h"
Expand All @@ -23,7 +24,7 @@ GOGUILabel::GOGUILabel(GOGUIPanel *panel, GOLabelControl *label)
m_DispXpos(0),
m_DispYpos(0),
m_Label(label),
m_Bitmap(),
m_PBackgroundBitmap(nullptr),
m_FontSize(0),
m_FontName(),
m_Text(),
Expand All @@ -33,6 +34,64 @@ GOGUILabel::GOGUILabel(GOGUIPanel *panel, GOLabelControl *label)
m_TileOffsetX(0),
m_TileOffsetY(0) {}

/**
* Deletes m_PBackgroundBitmap if it has been allocated
*/
void GOGUILabel::DestroyBackgroundBitmap() {
GOBitmap *pOldBackgroundBitmap = m_PBackgroundBitmap;

// reset the old background
if (pOldBackgroundBitmap) {
m_PBackgroundBitmap = nullptr;
delete pOldBackgroundBitmap;
}
}

static const wxString WX_BITMAP_LABEL_FMT = wxT(GOBitmapPrefix "label%02d");

/**
* Creates m_PBackgroundBitmap image from the imageFileName and imageNum and
* sets the default values for m_BoundingRect, m_TextRect, m_TextWidth,
* m_TileOffsetX, m_TileOffsetY, basing on the image size
* @param x - the x coordinate for m_BoundingRect
* @param x - the y coordinate for m_BoundingRect
* @param imageFileName the file name of the bitmap. If it is empty then
* it is calculated from imageNum
* @param imageNum if > 0 then the number of predefined images. If 0 then the
* background is transparent. This parameter is used only if imageFileName is
* empty.
* @param imageMaskFilename a filename of the image mask
*/
void GOGUILabel::InitBackgroundBitmap(
unsigned x,
unsigned y,
wxString imageFileName,
unsigned imageNum,
const wxString &imageMaskFilename) {
DestroyBackgroundBitmap();
if (imageFileName.IsEmpty() && imageNum)
// Calculate imageFileName from imageNum
imageFileName.Printf(WX_BITMAP_LABEL_FMT, imageNum);
if (!imageFileName.IsEmpty())
m_PBackgroundBitmap
= new GOBitmap(m_panel->LoadBitmap(imageFileName, imageMaskFilename));

unsigned backgroundWidth
= m_PBackgroundBitmap ? m_PBackgroundBitmap->GetWidth() : 80;
unsigned backgroundHeight
= m_PBackgroundBitmap ? m_PBackgroundBitmap->GetHeight() : 25;
unsigned textX = 1;
unsigned textY = 1;
unsigned textWidth = backgroundWidth - textX;
unsigned textHeigth = backgroundHeight - textY;

m_BoundingRect = wxRect(x, y, backgroundWidth, backgroundHeight);
m_TextRect = wxRect(textX, textY, textWidth, textHeigth);
m_TextWidth = textWidth;
m_TileOffsetX = 0;
m_TileOffsetY = 0;
}

void GOGUILabel::Init(
GOConfigReader &cfg,
wxString group,
Expand All @@ -47,34 +106,18 @@ void GOGUILabel::Init(
m_FontName = wxT("");
m_Text = name;

wxString image_file
= wxString::Format(wxT(GOBitmapPrefix "label%02d"), DispImageNum);
wxString image_mask_file = wxEmptyString;

m_Bitmap = m_panel->LoadBitmap(image_file, image_mask_file);

int x, y, w, h;
x = x_pos;
y = y_pos;
w = m_Bitmap.GetWidth();
h = m_Bitmap.GetHeight();
m_BoundingRect = wxRect(x, y, w, h);

m_TileOffsetX = 0;
m_TileOffsetY = 0;

x = 1;
y = 1;
w = m_BoundingRect.GetWidth() - x;
h = m_BoundingRect.GetHeight() - y;
m_TextRect = wxRect(x, y, w, h);
m_TextWidth = m_TextRect.GetWidth();
InitBackgroundBitmap(
x_pos, y_pos, wxEmptyString, DispImageNum, wxEmptyString);

m_Font = m_metrics->GetGroupLabelFont();
m_Font.SetName(m_FontName);
m_Font.SetPoints(m_FontSize);
}

static const wxString WX_IMAGE = wxT("Image");
static const wxString WX_DISP_IMAGE_NUM = wxT("DispImageNum");
static const wxString WX_MASK = wxT("Mask");

void GOGUILabel::Load(GOConfigReader &cfg, wxString group) {
GOGUIControl::Load(cfg, group);

Expand Down Expand Up @@ -134,31 +177,6 @@ void GOGUILabel::Load(GOConfigReader &cfg, wxString group) {
0);
}

m_TextColor = logicalToWxColour(cfg.ReadColor(
ODFSetting, group, wxT("DispLabelColour"), false, wxT("BLACK")));
m_FontSize = cfg.ReadFontSize(
ODFSetting, group, wxT("DispLabelFontSize"), false, wxT("normal"));
m_FontName = cfg.ReadStringTrim(
ODFSetting, group, wxT("DispLabelFontName"), false, wxT(""));
if (!m_Label || !m_panel->GetOrganFile()->GetSettings().ODFCheck())
m_Text
= cfg.ReadString(ODFSetting, group, wxT("Name"), false, wxEmptyString);

unsigned DispImageNum
= cfg.ReadInteger(ODFSetting, group, wxT("DispImageNum"), 1, 12, false, 1);

wxString image_file = cfg.ReadStringTrim(
ODFSetting,
group,
wxT("Image"),
false,
wxString::Format(wxT(GOBitmapPrefix "label%02d"), DispImageNum));
wxString image_mask_file
= cfg.ReadStringTrim(ODFSetting, group, wxT("Mask"), false, wxEmptyString);

m_Bitmap = m_panel->LoadBitmap(image_file, image_mask_file);

int w, h;
x = cfg.ReadInteger(
ODFSetting,
group,
Expand All @@ -175,38 +193,58 @@ void GOGUILabel::Load(GOConfigReader &cfg, wxString group) {
m_metrics->GetScreenHeight(),
false,
y);

m_TextColor = logicalToWxColour(cfg.ReadColor(
ODFSetting, group, wxT("DispLabelColour"), false, wxT("BLACK")));
m_FontSize = cfg.ReadFontSize(
ODFSetting, group, wxT("DispLabelFontSize"), false, wxT("normal"));
m_FontName = cfg.ReadStringTrim(
ODFSetting, group, wxT("DispLabelFontName"), false, wxT(""));
if (!m_Label || !m_panel->GetOrganFile()->GetSettings().ODFCheck())
m_Text
= cfg.ReadString(ODFSetting, group, wxT("Name"), false, wxEmptyString);

InitBackgroundBitmap(
x,
y,
cfg.ReadStringTrim(ODFSetting, group, WX_IMAGE, false),
cfg.ReadInteger(ODFSetting, group, WX_DISP_IMAGE_NUM, 0, 12, false, 1),
cfg.ReadStringTrim(ODFSetting, group, WX_MASK, false));

int w, h;

w = cfg.ReadInteger(
ODFSetting,
group,
wxT("Width"),
1,
m_metrics->GetScreenWidth(),
false,
m_Bitmap.GetWidth());
m_BoundingRect.width);
h = cfg.ReadInteger(
ODFSetting,
group,
wxT("Height"),
1,
m_metrics->GetScreenHeight(),
false,
m_Bitmap.GetHeight());
m_BoundingRect.height);
m_BoundingRect = wxRect(x, y, w, h);

m_TileOffsetX = cfg.ReadInteger(
ODFSetting,
group,
wxT("TileOffsetX"),
0,
m_Bitmap.GetWidth() - 1,
m_BoundingRect.width - 1,
false,
0);
m_TileOffsetY = cfg.ReadInteger(
ODFSetting,
group,
wxT("TileOffsetY"),
0,
m_Bitmap.GetHeight() - 1,
m_BoundingRect.height - 1,
false,
0);

Expand All @@ -215,42 +253,36 @@ void GOGUILabel::Load(GOConfigReader &cfg, wxString group) {
group,
wxT("TextRectLeft"),
0,
m_BoundingRect.GetWidth() - 1,
m_BoundingRect.width - 1,
false,
1);
m_TextRect.x);
y = cfg.ReadInteger(
ODFSetting,
group,
wxT("TextRectTop"),
0,
m_BoundingRect.GetHeight() - 1,
m_BoundingRect.height - 1,
false,
1);
m_TextRect.y);
w = cfg.ReadInteger(
ODFSetting,
group,
wxT("TextRectWidth"),
1,
m_BoundingRect.GetWidth() - x,
m_BoundingRect.width - x,
false,
m_BoundingRect.GetWidth() - x);
m_BoundingRect.width - x);
h = cfg.ReadInteger(
ODFSetting,
group,
wxT("TextRectHeight"),
1,
m_BoundingRect.GetHeight() - y,
m_BoundingRect.height - y,
false,
m_BoundingRect.GetHeight() - y);
m_BoundingRect.height - y);
m_TextRect = wxRect(x, y, w, h);
m_TextWidth = cfg.ReadInteger(
ODFSetting,
group,
wxT("TextBreakWidth"),
0,
m_TextRect.GetWidth(),
false,
m_TextRect.GetWidth());
m_TextWidth
= cfg.ReadInteger(ODFSetting, group, wxT("TextBreakWidth"), 0, w, false, w);

m_Font = m_metrics->GetGroupLabelFont();
m_Font.SetName(m_FontName);
Expand Down Expand Up @@ -278,15 +310,17 @@ void GOGUILabel::Layout() {
}

void GOGUILabel::PrepareDraw(double scale, GOBitmap *background) {
m_Bitmap.PrepareTileBitmap(
scale, m_BoundingRect, m_TileOffsetX, m_TileOffsetY, background);
if (m_PBackgroundBitmap)
m_PBackgroundBitmap->PrepareTileBitmap(
scale, m_BoundingRect, m_TileOffsetX, m_TileOffsetY, background);
}

void GOGUILabel::Draw(GODC &dc) {
if (m_Label)
m_Text = m_Label->GetContent();

dc.DrawBitmap(m_Bitmap, m_BoundingRect);
if (m_PBackgroundBitmap)
dc.DrawBitmap(*m_PBackgroundBitmap, m_BoundingRect);
if (m_TextWidth)
dc.DrawText(m_Text, m_TextRect, m_TextColor, m_Font, m_TextWidth);

Expand Down
13 changes: 11 additions & 2 deletions src/grandorgue/gui/GOGUILabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

#include <wx/colour.h>

#include "GOBitmap.h"
#include "GOFont.h"
#include "GOGUIControl.h"

class GOBitmap;
class GOLabelControl;

class GOGUILabel : public GOGUIControl {
private:
int m_DispXpos;
int m_DispYpos;
GOLabelControl *m_Label;
GOBitmap m_Bitmap;
GOBitmap *m_PBackgroundBitmap; // if nullptr then the label is transparent
unsigned m_FontSize;
wxString m_FontName;
GOFont m_Font;
Expand All @@ -32,8 +32,17 @@ class GOGUILabel : public GOGUIControl {
unsigned m_TileOffsetX;
unsigned m_TileOffsetY;

void DestroyBackgroundBitmap();
void InitBackgroundBitmap(
unsigned x,
unsigned y,
wxString imageFileName,
unsigned imageNum,
const wxString &imageMaskFileneme);

public:
GOGUILabel(GOGUIPanel *panel, GOLabelControl *label);
~GOGUILabel() override { DestroyBackgroundBitmap(); }
void Init(
GOConfigReader &cfg,
wxString group,
Expand Down

0 comments on commit 27a0f3d

Please sign in to comment.