summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralberth <alberth@openttd.org>2010-07-26 13:02:28 +0000
committeralberth <alberth@openttd.org>2010-07-26 13:02:28 +0000
commit3a1451644a0eae226545818adbde8a8a8c45eab7 (patch)
tree130026ba4b8d6f2d094d756f434671a4cbc52569 /src
parent80cc566553ca1eb33566e0b05a49b7112d98718d (diff)
downloadopenttd-3a1451644a0eae226545818adbde8a8a8c45eab7.tar.xz
(svn r20221) -Codechange: Move unscrolled row calculation into a function.
Diffstat (limited to 'src')
-rw-r--r--src/music_gui.cpp4
-rw-r--r--src/town_gui.cpp3
-rw-r--r--src/window.cpp16
-rw-r--r--src/window_gui.h1
4 files changed, 20 insertions, 4 deletions
diff --git a/src/music_gui.cpp b/src/music_gui.cpp
index c141f4e1f..380ba09ce 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<NWidgetBase>(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<NWidgetBase>(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 bcab4560f..dc6864f20 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<NWidgetBase>(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 da6a1cc18..c3cde4d5a 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -82,6 +82,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<NWidgetBase>(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.
* @param widget Widget with size and resize properties.
diff --git a/src/window_gui.h b/src/window_gui.h
index 83c7e3bb9..d01e8e4af 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, ...);