Skip to content

Commit

Permalink
Add search bar for gp editor
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingJellyfish committed Sep 19, 2024
1 parent 5a0b15a commit 35967b6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 17 deletions.
12 changes: 8 additions & 4 deletions data/gui/screens/edit_track.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
<stkgui>
<div x="2%" y="1%" width="96%" height="98%" layout="vertical-row" padding="10">

<header id="selected_track" height="8%" width="80%"
I18N="No neeed to translate this, it will be overwritten by the track name"
text="" align="center" text_align="center" />
<div height="8%" width="80%" layout="horizontal-row" align="center">
<header id="selected_track" height="8%" width="50%"
I18N="No neeed to translate this, it will be overwritten by the track name"
text="" align="center" text_align="center" />
<spacer width="5%" height="100%" />
<textbox width="45%" id="search" align="center"/>
</div>

<spacer height="3%" />
<spacer height="1%" />

<!-- Track selection -->
<box proportion="3" width="100%" layout="vertical-row">
Expand Down
85 changes: 73 additions & 12 deletions src/states_screens/edit_track_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#include "states_screens/edit_track_screen.hpp"

#include "config/player_manager.hpp"
#include "graphics/stk_tex_manager.hpp"

#include "guiengine/widgets/button_widget.hpp"
#include "guiengine/widgets/check_box_widget.hpp"
#include "guiengine/widgets/dynamic_ribbon_widget.hpp"
Expand Down Expand Up @@ -92,6 +92,8 @@ void EditTrackScreen::loadedFromFile()
DynamicRibbonWidget* tracks_widget = getWidget<DynamicRibbonWidget>("tracks");
assert(tracks_widget != NULL);
tracks_widget->setMaxLabelLength(MAX_LABEL_LENGTH);
// Avoid too many items shown at the same time
tracks_widget->setItemCountHint(std::min((int)track_manager->getNumberOfTracks()+1, 30));

m_screenshot = getWidget<IconButtonWidget>("screenshot");
m_screenshot->setFocusable(false);
Expand All @@ -101,6 +103,9 @@ void EditTrackScreen::loadedFromFile()
// -----------------------------------------------------------------------------
void EditTrackScreen::beforeAddingWidget()
{
track_manager->setFavoriteTrackStatus(
PlayerManager::getCurrentPlayer()->getFavoriteTrackStatus());

RibbonWidget* tabs = getWidget<RibbonWidget>("trackgroups");
assert (tabs != NULL);

Expand All @@ -118,6 +123,11 @@ void EditTrackScreen::beforeAddingWidget()
// -----------------------------------------------------------------------------
void EditTrackScreen::init()
{
m_search_box = getWidget<TextBoxWidget>("search");
assert(m_search_box != NULL);
m_search_box->clearListeners();
m_search_box->addListener(this);

RibbonWidget* tabs = getWidget<RibbonWidget>("trackgroups");
assert (tabs != NULL);
SpinnerWidget* laps = getWidget<SpinnerWidget>("laps");
Expand Down Expand Up @@ -183,24 +193,75 @@ void EditTrackScreen::eventCallback(GUIEngine::Widget* widget, const std::string
// -----------------------------------------------------------------------------
void EditTrackScreen::loadTrackList()
{
track_manager->setFavoriteTrackStatus(
PlayerManager::getCurrentPlayer()->getFavoriteTrackStatus());

DynamicRibbonWidget* tracks_widget = getWidget<DynamicRibbonWidget>("tracks");
assert(tracks_widget != NULL);

tracks_widget->clearItems();

for (unsigned int i = 0; i < track_manager->getNumberOfTracks(); i++)
// First build a list of all tracks to be displayed
// (e.g. exclude arenas, ...)
PtrVector<Track, REF> tracks;

if (m_track_group == ALL_TRACKS_GROUP_ID)
{
const int track_amount = (int)track_manager->getNumberOfTracks();
for (int n = 0; n < track_amount; n++)
{
Track* curr = track_manager->getTrack(n);
if (curr->isArena() || curr->isSoccer() || curr->isInternal()) continue;
if (RaceManager::get()->getMinorMode() == RaceManager::MINOR_MODE_EASTER_EGG
&& !curr->hasEasterEggs())
continue;
core::stringw search_text = m_search_box->getText();
search_text.make_lower();
if (!search_text.empty() &&
curr->getName().make_lower().find(search_text.c_str()) == -1)
continue;

tracks.push_back(curr);
} // for n<track_amount
}
else
{
const std::vector<int>& curr_tracks = track_manager->getTracksInGroup(m_track_group);
const int track_amount = (int)curr_tracks.size();

for (int n = 0; n < track_amount; n++)
{
Track* curr = track_manager->getTrack(curr_tracks[n]);
if (curr->isArena() || curr->isSoccer() || curr->isInternal()) continue;
if (RaceManager::get()->getMinorMode() == RaceManager::MINOR_MODE_EASTER_EGG
&& !curr->hasEasterEggs())
continue;
core::stringw search_text = m_search_box->getText();
search_text.make_lower();
if (!search_text.empty() &&
curr->getName().make_lower().find(search_text.c_str()) == -1)
continue;

tracks.push_back(curr);
} // for n<track_amount
}

tracks.insertionSort();

for (unsigned int i = 0; i < tracks.size(); i++)
{
Track* t = track_manager->getTrack(i);
bool belongs_to_group = (m_track_group.empty() ||
m_track_group == ALL_TRACKS_GROUP_ID ||
t->isInGroup(m_track_group) );
if (!t->isArena() && !t->isSoccer() &&
!t->isInternal() && belongs_to_group )
Track *curr = tracks.get(i);
if (PlayerManager::getCurrentPlayer()->isFavoriteTrack(curr->getIdent()))
{
tracks_widget->addItem(curr->getName(), curr->getIdent(),
curr->getScreenshotFile(), HEART_BADGE,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
}
else
{
tracks_widget->addItem(t->getName(),
t->getIdent(),
t->getScreenshotFile(), 0,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
tracks_widget->addItem(curr->getName(), curr->getIdent(),
curr->getScreenshotFile(), 0,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/states_screens/edit_track_screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define HEADER_EDIT_TRACK_SCREEN_HPP

#include "guiengine/screen.hpp"
#include "guiengine/widgets/text_box_widget.hpp"

namespace GUIEngine { class IconButtonWidget; class Widget; }

Expand All @@ -32,7 +33,8 @@ class Track;
*/
class EditTrackScreen :
public GUIEngine::Screen,
public GUIEngine::ScreenSingleton<EditTrackScreen>
public GUIEngine::ScreenSingleton<EditTrackScreen>,
public GUIEngine::ITextBoxWidgetListener
{
friend class GUIEngine::ScreenSingleton<EditTrackScreen>;

Expand All @@ -51,6 +53,8 @@ class EditTrackScreen :
bool m_result;

GUIEngine::IconButtonWidget* m_screenshot;

GUIEngine::TextBoxWidget *m_search_box;

public:

Expand All @@ -74,6 +78,15 @@ class EditTrackScreen :

/** \brief implement callback from parent class GUIEngine::Screen */
virtual void init() OVERRIDE;

/** Rebuild the list of tracks based on search text */
virtual void onTextUpdated() OVERRIDE
{
loadTrackList();
// After buildTrackList the m_search_box may be unfocused
m_search_box->focused(0);
}

};

#endif

0 comments on commit 35967b6

Please sign in to comment.