summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2012-09-19 22:10:48 +0000
committerfrosch <frosch@openttd.org>2012-09-19 22:10:48 +0000
commita4f22a2de145f26cdd633970c85d83ee19739538 (patch)
tree45f700db1260cd5360d2627943e00257332505ff
parent7251fbb514e90739553d4892b9dccda13d290cba (diff)
downloadopenttd-a4f22a2de145f26cdd633970c85d83ee19739538.tar.xz
(svn r24533) -Codechange: Move position determination on minimap legend to separate function.
-rw-r--r--src/smallmap_gui.cpp38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp
index e80cb63d2..d28d95144 100644
--- a/src/smallmap_gui.cpp
+++ b/src/smallmap_gui.cpp
@@ -1255,6 +1255,27 @@ public:
this->SetDirty();
}
+ /**
+ * Determines the mouse position on the legend.
+ * @param pt Mouse position.
+ * @return Legend item under the mouse.
+ */
+ int GetPositionOnLegend(Point pt)
+ {
+ const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
+ uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
+ uint columns = this->GetNumberColumnsLegend(wi->current_x);
+ uint number_of_rows = max(CeilDiv(max(_smallmap_company_count, _smallmap_industry_count), columns), this->min_number_of_fixed_rows);
+ if (line >= number_of_rows) return -1;
+
+ bool rtl = _current_text_dir == TD_RTL;
+ int x = pt.x - wi->pos_x;
+ if (rtl) x = wi->current_x - x;
+ uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
+
+ return (column * number_of_rows) + line;
+ }
+
virtual void OnClick(Point pt, int widget, int click_count)
{
/* User clicked something, notify the industry chain window to stop sending newly selected industries. */
@@ -1322,22 +1343,11 @@ public:
case WID_SM_LEGEND: // Legend
if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER) {
- const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND); // Label panel
- uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
- uint columns = this->GetNumberColumnsLegend(wi->current_x);
- uint number_of_rows = max(CeilDiv(max(_smallmap_company_count, _smallmap_industry_count), columns), this->min_number_of_fixed_rows);
- if (line >= number_of_rows) break;
-
- bool rtl = _current_text_dir == TD_RTL;
- int x = pt.x - wi->pos_x;
- if (rtl) x = wi->current_x - x;
- uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
-
/* If industry type small map*/
if (this->map_type == SMT_INDUSTRY) {
/* If click on industries label, find right industry type and enable/disable it. */
- int industry_pos = (column * number_of_rows) + line;
- if (industry_pos < _smallmap_industry_count) {
+ int industry_pos = GetPositionOnLegend(pt);
+ if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
if (_ctrl_pressed) {
/* Disable all, except the clicked one. */
bool changes = false;
@@ -1360,7 +1370,7 @@ public:
}
} else if (this->map_type == SMT_OWNER) {
/* If click on companies label, find right company and enable/disable it. */
- int company_pos = (column * number_of_rows) + line;
+ int company_pos = GetPositionOnLegend(pt);
if (company_pos < NUM_NO_COMPANY_ENTRIES) break;
if (company_pos < _smallmap_company_count) {
if (_ctrl_pressed) {