From 3a1451644a0eae226545818adbde8a8a8c45eab7 Mon Sep 17 00:00:00 2001 From: alberth Date: Mon, 26 Jul 2010 13:02:28 +0000 Subject: [PATCH] (svn r20221) -Codechange: Move unscrolled row calculation into a function. --- src/music_gui.cpp | 4 ++-- src/town_gui.cpp | 3 +-- src/window.cpp | 16 ++++++++++++++++ src/window_gui.h | 1 + 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/music_gui.cpp b/src/music_gui.cpp index c141f4e1f0..380ba09ced 100644 --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -401,7 +401,7 @@ struct MusicTrackSelectionWindow : public Window { { switch (widget) { case MTSW_LIST_LEFT: { // add to playlist - int y = (pt.y - this->GetWidget(widget)->pos_y) / FONT_HEIGHT_SMALL; + int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL); if (msf.playlist < 4) return; if (!IsInsideMM(y, 0, BaseMusic::GetUsedSet()->num_available)) return; @@ -425,7 +425,7 @@ struct MusicTrackSelectionWindow : public Window { } break; case MTSW_LIST_RIGHT: { // remove from playlist - int y = (pt.y - this->GetWidget(widget)->pos_y) / FONT_HEIGHT_SMALL; + int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL); if (msf.playlist < 4) return; if (!IsInsideMM(y, 0, NUM_SONGS_PLAYLIST)) return; diff --git a/src/town_gui.cpp b/src/town_gui.cpp index bcab4560f7..dc6864f205 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -259,8 +259,7 @@ public: { switch (widget) { case TWA_COMMAND_LIST: { - int y = (pt.y - this->GetWidget(TWA_COMMAND_LIST)->pos_y - 1) / FONT_HEIGHT_NORMAL; - + int y = this->GetRowFromWidget(pt.y, TWA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL); if (!IsInsideMM(y, 0, 5)) return; y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_company, this->town), y + this->vscroll.GetPosition() - 1); diff --git a/src/window.cpp b/src/window.cpp index da6a1cc18c..c3cde4d5ab 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -81,6 +81,22 @@ WindowDesc::~WindowDesc() { } +/** + * Compute the row of a widget that a user clicked in. + * @param clickpos Vertical position of the mouse click. + * @param widget Widget number of the widget clicked in. + * @param padding Amount of empty space between the widget edge and the top of the first row. + * @param line_height Height of a single row. + * @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned. + * @note The widget does not know where a list printed at the widget ends, so below a list is not a wrong position. + */ +int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const +{ + const NWidgetBase *wid = this->GetWidget(widget); + if (clickpos < (int)wid->pos_y + padding) return INT_MAX; + return (clickpos - (int)wid->pos_y - padding) / line_height; +} + /** * Set capacity of visible elements from the size and resize properties of a widget. * @param w Window. diff --git a/src/window_gui.h b/src/window_gui.h index 83c7e3bb98..d01e8e4af2 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -528,6 +528,7 @@ public: bool SetFocusedWidget(byte widget_index); void HandleButtonClick(byte widget); + int GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const; void RaiseButtons(bool autoraise = false); void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);