Skip to content

Commit 3340bbf

Browse files
committed
Add support for help texts for editor buttons
1 parent e66e109 commit 3340bbf

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/editor/button_widget.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "supertux/gameconfig.hpp"
2020
#include "supertux/globals.hpp"
21+
#include "supertux/resources.hpp"
22+
#include "util/log.hpp"
2123
#include "video/viewport.hpp"
2224
#include "video/video_system.hpp"
2325

@@ -28,7 +30,9 @@ ButtonWidget::ButtonWidget(SpritePtr sprite, const Vector& pos,
2830
static_cast<float>(m_sprite->get_height()))),
2931
m_grab(false),
3032
m_hover(false),
31-
m_sig_click(std::move(sig_click))
33+
m_sig_click(std::move(sig_click)),
34+
m_mouse_pos(),
35+
m_help_text()
3236
{
3337
}
3438

@@ -49,6 +53,16 @@ ButtonWidget::draw(DrawingContext& context)
4953
context.color().draw_filled_rect(m_rect, g_config->editorhovercolor, 4.0f,
5054
LAYER_GUI-5);
5155
}
56+
57+
if (m_hover && !m_help_text.empty())
58+
{
59+
const auto& font = Resources::console_font;
60+
const auto text_height = font->get_text_height(m_help_text);
61+
const auto text_width = font->get_text_width(m_help_text);
62+
const auto text_rect = Rectf(m_mouse_pos + Vector(32, 32), m_mouse_pos + Vector(32, 32) + Vector(text_width, text_height));
63+
context.color().draw_filled_rect(text_rect, Color::BLACK, LAYER_GUI - 5);
64+
context.color().draw_text(font, m_help_text, m_mouse_pos + Vector(32, 32), FontAlignment::ALIGN_LEFT, LAYER_GUI - 5);
65+
}
5266
}
5367

5468
void
@@ -71,10 +85,10 @@ ButtonWidget::on_mouse_button_up(const SDL_MouseButtonEvent& button)
7185
{
7286
if (button.button != SDL_BUTTON_LEFT) return false;
7387

74-
Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(button.x, button.y);
88+
m_mouse_pos = VideoSystem::current()->get_viewport().to_logical(button.x, button.y);
7589

7690
if (m_grab) {
77-
if (m_rect.contains(mouse_pos)) {
91+
if (m_rect.contains(m_mouse_pos)) {
7892
if (m_sig_click) {
7993
m_sig_click();
8094
}
@@ -92,9 +106,9 @@ ButtonWidget::on_mouse_button_down(const SDL_MouseButtonEvent& button)
92106
{
93107
if (button.button != SDL_BUTTON_LEFT) return false;
94108

95-
Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(button.x, button.y);
109+
m_mouse_pos = VideoSystem::current()->get_viewport().to_logical(button.x, button.y);
96110

97-
if (m_rect.contains(mouse_pos)) {
111+
if (m_rect.contains(m_mouse_pos)) {
98112
m_hover = true;
99113
m_grab = true;
100114
return true;
@@ -107,12 +121,12 @@ ButtonWidget::on_mouse_button_down(const SDL_MouseButtonEvent& button)
107121
bool
108122
ButtonWidget::on_mouse_motion(const SDL_MouseMotionEvent& motion)
109123
{
110-
Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(motion.x, motion.y);
124+
m_mouse_pos = VideoSystem::current()->get_viewport().to_logical(motion.x, motion.y);
111125

112126
if (m_grab) {
113-
m_hover = m_rect.contains(mouse_pos);
127+
m_hover = m_rect.contains(m_mouse_pos);
114128
return true;
115-
} else if (m_rect.contains(mouse_pos)) {
129+
} else if (m_rect.contains(m_mouse_pos)) {
116130
m_hover = true;
117131
return false;
118132
} else {
@@ -134,4 +148,10 @@ ButtonWidget::set_sprite(const std::string& path)
134148
set_sprite(SpriteManager::current()->create(path));
135149
}
136150

151+
void
152+
ButtonWidget::set_help_text(const std::string& help_text)
153+
{
154+
m_help_text = help_text;
155+
}
156+
137157
/* EOF */

src/editor/button_widget.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,16 @@ class ButtonWidget : public Widget
4848
void set_sprite(const std::string& path);
4949
void set_sprite(SpritePtr sprite);
5050

51+
void set_help_text(const std::string& help_text);
52+
5153
private:
5254
SpritePtr m_sprite;
5355
Rectf m_rect;
5456
bool m_grab;
5557
bool m_hover;
5658
std::function<void()> m_sig_click;
59+
Vector m_mouse_pos;
60+
std::string m_help_text;
5761

5862
private:
5963
ButtonWidget(const ButtonWidget&) = delete;

src/editor/editor.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,23 @@ Editor::Editor() :
163163
else
164164
snap_grid_size--;
165165
});
166-
166+
167+
grid_size_widget->set_help_text(_("Change / Toggle grid size"));
167168
m_grid_size_widget = grid_size_widget.get();
168169

169170
m_widgets.insert(m_widgets.begin() + 2, std::move(grid_size_widget));
170171

171172
auto play_button = std::make_unique<ButtonWidget>("images/engine/editor/play_button.png",
172173
Vector(96, 0), [this] { m_test_request = true; });
174+
play_button->set_help_text(_("Test level"));
173175

174176
m_play_widget = play_button.get();
175177

176178
m_widgets.insert(m_widgets.begin() + 3, std::move(play_button));
177179

178180
auto save_button = std::make_unique<ButtonWidget>("images/engine/editor/save.png",
179181
Vector(128, 0), [this] { save_level(); });
182+
save_button->set_help_text(_("Save level"));
180183

181184
m_save_widget = save_button.get();
182185

@@ -191,6 +194,7 @@ Editor::Editor() :
191194
else
192195
select_objectgroup(0);
193196
});
197+
mode_button->set_help_text(_("Toggle between object and tile mode"));
194198

195199
m_widgets.insert(m_widgets.begin() + 5, std::move(mode_button));
196200
}
@@ -1114,8 +1118,10 @@ Editor::retoggle_undo_tracking()
11141118
// Add undo/redo button widgets.
11151119
auto undo_button_widget = std::make_unique<ButtonWidget>("images/engine/editor/undo.png",
11161120
Vector(0, 0), [this]{ undo(); });
1121+
undo_button_widget->set_help_text(_("Undo"));
11171122
auto redo_button_widget = std::make_unique<ButtonWidget>("images/engine/editor/redo.png",
11181123
Vector(32, 0), [this]{ redo(); });
1124+
redo_button_widget->set_help_text(_("Redo"));
11191125

11201126
m_undo_widget = undo_button_widget.get();
11211127
m_redo_widget = redo_button_widget.get();

0 commit comments

Comments
 (0)