Add drop shadow to placefile text

This commit is contained in:
Dan Paulat 2023-10-09 22:01:58 -05:00
parent cfa62d5fbc
commit 4916dfe85a
6 changed files with 68 additions and 20 deletions

View file

@ -1,6 +1,7 @@
#include <scwx/qt/gl/draw/placefile_text.hpp> #include <scwx/qt/gl/draw/placefile_text.hpp>
#include <scwx/qt/manager/font_manager.hpp> #include <scwx/qt/manager/font_manager.hpp>
#include <scwx/qt/manager/placefile_manager.hpp> #include <scwx/qt/manager/placefile_manager.hpp>
#include <scwx/qt/settings/text_settings.hpp>
#include <scwx/qt/util/maplibre.hpp> #include <scwx/qt/util/maplibre.hpp>
#include <scwx/qt/util/tooltip.hpp> #include <scwx/qt/util/tooltip.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
@ -39,7 +40,6 @@ public:
const std::string& text, const std::string& text,
const std::string& hoverText, const std::string& hoverText,
boost::gil::rgba8_pixel_t color, boost::gil::rgba8_pixel_t color,
std::size_t fontNumber,
float x, float x,
float y); float y);
@ -157,13 +157,36 @@ void PlacefileText::Impl::RenderTextDrawItem(
screenCoordinates.y * mapBearingCos_; screenCoordinates.y * mapBearingCos_;
} }
// Clamp font number to 0-8
std::size_t fontNumber = std::clamp<std::size_t>(di->fontNumber_, 0, 8);
// Set the font for the drop shadow and text
ImGui::PushFont(fonts_[fontNumber]->font());
if (settings::TextSettings::Instance()
.placefile_text_drop_shadow_enabled()
.GetValue())
{
// Draw a drop shadow 1 pixel to the lower right, in black, with the
// original transparency level
RenderText(params,
di->text_,
{},
boost::gil::rgba8_pixel_t {0, 0, 0, di->color_[3]},
rotatedX + di->x_ + halfWidth_ + 1.0f,
rotatedY + di->y_ + halfHeight_ - 1.0f);
}
// Draw the text
RenderText(params, RenderText(params,
di->text_, di->text_,
di->hoverText_, di->hoverText_,
di->color_, di->color_,
std::clamp<std::size_t>(di->fontNumber_, 1, 8),
rotatedX + di->x_ + halfWidth_, rotatedX + di->x_ + halfWidth_,
rotatedY + di->y_ + halfHeight_); rotatedY + di->y_ + halfHeight_);
// Reset the font
ImGui::PopFont();
} }
} }
@ -172,7 +195,6 @@ void PlacefileText::Impl::RenderText(
const std::string& text, const std::string& text,
const std::string& hoverText, const std::string& hoverText,
boost::gil::rgba8_pixel_t color, boost::gil::rgba8_pixel_t color,
std::size_t fontNumber,
float x, float x,
float y) float y)
{ {
@ -192,12 +214,10 @@ void PlacefileText::Impl::RenderText(
ImGuiWindowFlags_NoBackground); ImGuiWindowFlags_NoBackground);
// Render text // Render text
ImGui::PushFont(fonts_[fontNumber - 1]->font());
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::PushStyleColor(ImGuiCol_Text,
IM_COL32(color[0], color[1], color[2], color[3])); IM_COL32(color[0], color[1], color[2], color[3]));
ImGui::TextUnformatted(text.c_str()); ImGui::TextUnformatted(text.c_str());
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::PopFont();
// Store hover text for mouse picking pass // Store hover text for mouse picking pass
if (!hoverText.empty() && ImGui::IsItemHovered()) if (!hoverText.empty() && ImGui::IsItemHovered())
@ -248,10 +268,10 @@ void PlacefileText::SetFonts(
auto defaultFont = manager::FontManager::Instance().GetImGuiFont( auto defaultFont = manager::FontManager::Instance().GetImGuiFont(
types::FontCategory::Default); types::FontCategory::Default);
// Valid font numbers are from 1 to 8, place in 0-based font vector // Valid font numbers are from 1 to 8, use 0 for the default font
for (std::size_t i = 1; i <= 8; ++i) for (std::size_t i = 0; i <= 8; ++i)
{ {
auto it = fonts.find(i); auto it = (i > 0) ? fonts.find(i) : fonts.cend();
if (it != fonts.cend()) if (it != fonts.cend())
{ {
p->newFonts_.push_back(it->second); p->newFonts_.push_back(it->second);

View file

@ -49,6 +49,7 @@ public:
hoverTextWrap_.SetDefault(80); hoverTextWrap_.SetDefault(80);
hoverTextWrap_.SetMinimum(0); hoverTextWrap_.SetMinimum(0);
hoverTextWrap_.SetMaximum(999); hoverTextWrap_.SetMaximum(999);
placefileTextDropShadowEnabled_.SetDefault(true);
tooltipMethod_.SetDefault(defaultTooltipMethodValue); tooltipMethod_.SetDefault(defaultTooltipMethodValue);
tooltipMethod_.SetValidator( tooltipMethod_.SetValidator(
@ -93,12 +94,17 @@ public:
SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"}; SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"};
SettingsVariable<std::string> tooltipMethod_ {"tooltip_method"}; SettingsVariable<std::string> tooltipMethod_ {"tooltip_method"};
SettingsVariable<bool> placefileTextDropShadowEnabled_ {
"placefile_text_drop_shadow_enabled"};
}; };
TextSettings::TextSettings() : TextSettings::TextSettings() :
SettingsCategory("text"), p(std::make_unique<Impl>(this)) SettingsCategory("text"), p(std::make_unique<Impl>(this))
{ {
RegisterVariables({&p->hoverTextWrap_, &p->tooltipMethod_}); RegisterVariables({&p->hoverTextWrap_,
&p->placefileTextDropShadowEnabled_,
&p->tooltipMethod_});
SetDefaults(); SetDefaults();
} }
TextSettings::~TextSettings() = default; TextSettings::~TextSettings() = default;
@ -162,6 +168,11 @@ SettingsVariable<std::int64_t>& TextSettings::hover_text_wrap() const
return p->hoverTextWrap_; return p->hoverTextWrap_;
} }
SettingsVariable<bool>& TextSettings::placefile_text_drop_shadow_enabled() const
{
return p->placefileTextDropShadowEnabled_;
}
SettingsVariable<std::string>& TextSettings::tooltip_method() const SettingsVariable<std::string>& TextSettings::tooltip_method() const
{ {
return p->tooltipMethod_; return p->tooltipMethod_;
@ -177,6 +188,8 @@ bool operator==(const TextSettings& lhs, const TextSettings& rhs)
{ {
return (lhs.p->fontData_ == rhs.p->fontData_ && return (lhs.p->fontData_ == rhs.p->fontData_ &&
lhs.p->hoverTextWrap_ == rhs.p->hoverTextWrap_ && lhs.p->hoverTextWrap_ == rhs.p->hoverTextWrap_ &&
lhs.p->placefileTextDropShadowEnabled_ ==
rhs.p->placefileTextDropShadowEnabled_ &&
lhs.p->tooltipMethod_ == rhs.p->tooltipMethod_); lhs.p->tooltipMethod_ == rhs.p->tooltipMethod_);
} }

View file

@ -34,6 +34,7 @@ public:
font_point_size(types::FontCategory fontCategory) const; font_point_size(types::FontCategory fontCategory) const;
SettingsVariable<std::int64_t>& hover_text_wrap() const; SettingsVariable<std::int64_t>& hover_text_wrap() const;
SettingsVariable<bool>& placefile_text_drop_shadow_enabled() const;
SettingsVariable<std::string>& tooltip_method() const; SettingsVariable<std::string>& tooltip_method() const;
static TextSettings& Instance(); static TextSettings& Instance();

View file

@ -102,7 +102,8 @@ public:
&updateNotificationsEnabled_, &updateNotificationsEnabled_,
&debugEnabled_, &debugEnabled_,
&hoverTextWrap_, &hoverTextWrap_,
&tooltipMethod_}} &tooltipMethod_,
&placefileTextDropShadowEnabled_}}
{ {
// Configure default alert phenomena colors // Configure default alert phenomena colors
auto& paletteSettings = settings::PaletteSettings::Instance(); auto& paletteSettings = settings::PaletteSettings::Instance();
@ -198,6 +199,7 @@ public:
settings::SettingsInterface<std::int64_t> hoverTextWrap_ {}; settings::SettingsInterface<std::int64_t> hoverTextWrap_ {};
settings::SettingsInterface<std::string> tooltipMethod_ {}; settings::SettingsInterface<std::string> tooltipMethod_ {};
settings::SettingsInterface<bool> placefileTextDropShadowEnabled_ {};
std::vector<settings::SettingsInterfaceBase*> settings_; std::vector<settings::SettingsInterfaceBase*> settings_;
}; };
@ -808,6 +810,11 @@ void SettingsDialogImpl::SetupTextTab()
}); });
tooltipMethod_.SetEditWidget(self_->ui->tooltipMethodComboBox); tooltipMethod_.SetEditWidget(self_->ui->tooltipMethodComboBox);
tooltipMethod_.SetResetButton(self_->ui->resetTooltipMethodButton); tooltipMethod_.SetResetButton(self_->ui->resetTooltipMethodButton);
placefileTextDropShadowEnabled_.SetSettingsVariable(
textSettings.placefile_text_drop_shadow_enabled());
placefileTextDropShadowEnabled_.SetEditWidget(
self_->ui->placefileTextDropShadowCheckBox);
} }
QImage SettingsDialogImpl::GenerateColorTableImage( QImage SettingsDialogImpl::GenerateColorTableImage(

View file

@ -626,13 +626,6 @@
<property name="bottomMargin"> <property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </property>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Hover text character wrap (0 to disable)</string>
</property>
</widget>
</item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QSpinBox" name="hoverTextWrapSpinBox"> <widget class="QSpinBox" name="hoverTextWrapSpinBox">
<property name="maximum"> <property name="maximum">
@ -640,6 +633,9 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QComboBox" name="tooltipMethodComboBox"/>
</item>
<item row="1" column="2"> <item row="1" column="2">
<widget class="QToolButton" name="resetHoverTextWrapButton"> <widget class="QToolButton" name="resetHoverTextWrapButton">
<property name="text"> <property name="text">
@ -658,8 +654,12 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="1" column="0">
<widget class="QComboBox" name="tooltipMethodComboBox"/> <widget class="QLabel" name="label_8">
<property name="text">
<string>Hover text character wrap (0 to disable)</string>
</property>
</widget>
</item> </item>
<item row="0" column="2"> <item row="0" column="2">
<widget class="QToolButton" name="resetTooltipMethodButton"> <widget class="QToolButton" name="resetTooltipMethodButton">
@ -672,6 +672,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="placefileTextDropShadowCheckBox">
<property name="text">
<string>Placefile Text Drop Shadow</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>

@ -1 +1 @@
Subproject commit 1685e4048ef4a9f34bc11ecbb8db4905dd0a2e19 Subproject commit 58d61ba37385c699df1eca547668ec3c2a93871e