Skip to content

Commit

Permalink
Add drop shadow to placefile text
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulat committed Oct 10, 2023
1 parent cfa62d5 commit 4916dfe
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
36 changes: 28 additions & 8 deletions scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <scwx/qt/gl/draw/placefile_text.hpp>
#include <scwx/qt/manager/font_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/tooltip.hpp>
#include <scwx/util/logger.hpp>
Expand Down Expand Up @@ -39,7 +40,6 @@ class PlacefileText::Impl
const std::string& text,
const std::string& hoverText,
boost::gil::rgba8_pixel_t color,
std::size_t fontNumber,
float x,
float y);

Expand Down Expand Up @@ -157,13 +157,36 @@ void PlacefileText::Impl::RenderTextDrawItem(
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,
di->text_,
di->hoverText_,
di->color_,
std::clamp<std::size_t>(di->fontNumber_, 1, 8),
rotatedX + di->x_ + halfWidth_,
rotatedY + di->y_ + halfHeight_);

// Reset the font
ImGui::PopFont();
}
}

Expand All @@ -172,7 +195,6 @@ void PlacefileText::Impl::RenderText(
const std::string& text,
const std::string& hoverText,
boost::gil::rgba8_pixel_t color,
std::size_t fontNumber,
float x,
float y)
{
Expand All @@ -192,12 +214,10 @@ void PlacefileText::Impl::RenderText(
ImGuiWindowFlags_NoBackground);

// Render text
ImGui::PushFont(fonts_[fontNumber - 1]->font());
ImGui::PushStyleColor(ImGuiCol_Text,
IM_COL32(color[0], color[1], color[2], color[3]));
ImGui::TextUnformatted(text.c_str());
ImGui::PopStyleColor();
ImGui::PopFont();

// Store hover text for mouse picking pass
if (!hoverText.empty() && ImGui::IsItemHovered())
Expand Down Expand Up @@ -248,10 +268,10 @@ void PlacefileText::SetFonts(
auto defaultFont = manager::FontManager::Instance().GetImGuiFont(
types::FontCategory::Default);

// Valid font numbers are from 1 to 8, place in 0-based font vector
for (std::size_t i = 1; i <= 8; ++i)
// Valid font numbers are from 1 to 8, use 0 for the default font
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())
{
p->newFonts_.push_back(it->second);
Expand Down
15 changes: 14 additions & 1 deletion scwx-qt/source/scwx/qt/settings/text_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TextSettings::Impl
hoverTextWrap_.SetDefault(80);
hoverTextWrap_.SetMinimum(0);
hoverTextWrap_.SetMaximum(999);
placefileTextDropShadowEnabled_.SetDefault(true);
tooltipMethod_.SetDefault(defaultTooltipMethodValue);

tooltipMethod_.SetValidator(
Expand Down Expand Up @@ -93,12 +94,17 @@ class TextSettings::Impl

SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"};
SettingsVariable<std::string> tooltipMethod_ {"tooltip_method"};

SettingsVariable<bool> placefileTextDropShadowEnabled_ {
"placefile_text_drop_shadow_enabled"};
};

TextSettings::TextSettings() :
SettingsCategory("text"), p(std::make_unique<Impl>(this))
{
RegisterVariables({&p->hoverTextWrap_, &p->tooltipMethod_});
RegisterVariables({&p->hoverTextWrap_,
&p->placefileTextDropShadowEnabled_,
&p->tooltipMethod_});
SetDefaults();
}
TextSettings::~TextSettings() = default;
Expand Down Expand Up @@ -162,6 +168,11 @@ SettingsVariable<std::int64_t>& TextSettings::hover_text_wrap() const
return p->hoverTextWrap_;
}

SettingsVariable<bool>& TextSettings::placefile_text_drop_shadow_enabled() const
{
return p->placefileTextDropShadowEnabled_;
}

SettingsVariable<std::string>& TextSettings::tooltip_method() const
{
return p->tooltipMethod_;
Expand All @@ -177,6 +188,8 @@ bool operator==(const TextSettings& lhs, const TextSettings& rhs)
{
return (lhs.p->fontData_ == rhs.p->fontData_ &&
lhs.p->hoverTextWrap_ == rhs.p->hoverTextWrap_ &&
lhs.p->placefileTextDropShadowEnabled_ ==
rhs.p->placefileTextDropShadowEnabled_ &&
lhs.p->tooltipMethod_ == rhs.p->tooltipMethod_);
}

Expand Down
1 change: 1 addition & 0 deletions scwx-qt/source/scwx/qt/settings/text_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TextSettings : public SettingsCategory
font_point_size(types::FontCategory fontCategory) const;

SettingsVariable<std::int64_t>& hover_text_wrap() const;
SettingsVariable<bool>& placefile_text_drop_shadow_enabled() const;
SettingsVariable<std::string>& tooltip_method() const;

static TextSettings& Instance();
Expand Down
9 changes: 8 additions & 1 deletion scwx-qt/source/scwx/qt/ui/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class SettingsDialogImpl
&updateNotificationsEnabled_,
&debugEnabled_,
&hoverTextWrap_,
&tooltipMethod_}}
&tooltipMethod_,
&placefileTextDropShadowEnabled_}}
{
// Configure default alert phenomena colors
auto& paletteSettings = settings::PaletteSettings::Instance();
Expand Down Expand Up @@ -198,6 +199,7 @@ class SettingsDialogImpl

settings::SettingsInterface<std::int64_t> hoverTextWrap_ {};
settings::SettingsInterface<std::string> tooltipMethod_ {};
settings::SettingsInterface<bool> placefileTextDropShadowEnabled_ {};

std::vector<settings::SettingsInterfaceBase*> settings_;
};
Expand Down Expand Up @@ -808,6 +810,11 @@ void SettingsDialogImpl::SetupTextTab()
});
tooltipMethod_.SetEditWidget(self_->ui->tooltipMethodComboBox);
tooltipMethod_.SetResetButton(self_->ui->resetTooltipMethodButton);

placefileTextDropShadowEnabled_.SetSettingsVariable(
textSettings.placefile_text_drop_shadow_enabled());
placefileTextDropShadowEnabled_.SetEditWidget(
self_->ui->placefileTextDropShadowCheckBox);
}

QImage SettingsDialogImpl::GenerateColorTableImage(
Expand Down
25 changes: 16 additions & 9 deletions scwx-qt/source/scwx/qt/ui/settings_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -626,20 +626,16 @@
<property name="bottomMargin">
<number>0</number>
</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">
<widget class="QSpinBox" name="hoverTextWrapSpinBox">
<property name="maximum">
<number>999</number>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="tooltipMethodComboBox"/>
</item>
<item row="1" column="2">
<widget class="QToolButton" name="resetHoverTextWrapButton">
<property name="text">
Expand All @@ -658,8 +654,12 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="tooltipMethodComboBox"/>
<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="0" column="2">
<widget class="QToolButton" name="resetTooltipMethodButton">
Expand All @@ -672,6 +672,13 @@
</property>
</widget>
</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>
</widget>
</item>
Expand Down

0 comments on commit 4916dfe

Please sign in to comment.