cargodest_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "window_gui.h"
00014 #include "gfx_func.h"
00015 #include "strings_func.h"
00016 #include "cargodest_base.h"
00017 #include "cargodest_gui.h"
00018 #include "town.h"
00019 #include "industry.h"
00020 #include "string_func.h"
00021 #include "gui.h"
00022 #include "viewport_func.h"
00023 
00024 #include "table/strings.h"
00025 
00026 static const CargoSourceSink *_cur_cargo_source;
00027 
00028 int CDECL CargoLinkSorter(const GUICargoLink *a, const GUICargoLink *b)
00029 {
00030   /* Sort by cargo type. */
00031   if (a->cid != b->cid) return a->cid < b->cid ? -1 : +1;
00032 
00033   /* Sort unspecified destination links always last. */
00034   if (a->link->dest == NULL) return +1;
00035   if (b->link->dest == NULL) return -1;
00036 
00037   /* Sort link with the current source as destination first. */
00038   if (a->link->dest == _cur_cargo_source) return -1;
00039   if (b->link->dest == _cur_cargo_source) return +1;
00040 
00041   /* Sort towns before industries. */
00042   if (a->link->dest->GetType() != b->link->dest->GetType()) {
00043     return a->link->dest->GetType() < b->link->dest->GetType() ? +1 : -1;
00044   }
00045 
00046   /* Sort by name. */
00047   static const CargoLink *last_b = NULL;
00048   static char last_name[128];
00049 
00050   char name[128];
00051   SetDParam(0, a->link->dest->GetID());
00052   GetString(name, a->link->dest->GetType() == ST_TOWN ? STR_TOWN_NAME : STR_INDUSTRY_NAME, lastof(name));
00053 
00054   /* Cache name lookup of 'b', as the sorter is often called
00055    * multiple times with the same 'b'. */
00056   if (b->link != last_b) {
00057     last_b = b->link;
00058 
00059     SetDParam(0, b->link->dest->GetID());
00060     GetString(last_name, b->link->dest->GetType() == ST_TOWN ? STR_TOWN_NAME : STR_INDUSTRY_NAME, lastof(last_name));
00061   }
00062 
00063   return strcmp(name, last_name);
00064 }
00065 
00066 CargoDestinationList::CargoDestinationList(const CargoSourceSink *o) : obj(o)
00067 {
00068   this->InvalidateData();
00069 }
00070 
00072 void CargoDestinationList::RebuildList()
00073 {
00074   if (!this->link_list.NeedRebuild()) return;
00075 
00076   this->link_list.Clear();
00077   for (CargoID i = 0; i < lengthof(this->obj->cargo_links); i++) {
00078     for (const CargoLink *l = this->obj->cargo_links[i].Begin(); l != this->obj->cargo_links[i].End(); l++) {
00079       *this->link_list.Append() = GUICargoLink(i, l);
00080     }
00081   }
00082 
00083   this->link_list.Compact();
00084   this->link_list.RebuildDone();
00085 }
00086 
00088 void CargoDestinationList::SortList()
00089 {
00090   _cur_cargo_source = this->obj;
00091   this->link_list.Sort(&CargoLinkSorter);
00092 }
00093 
00095 void CargoDestinationList::InvalidateData()
00096 {
00097   this->link_list.ForceRebuild();
00098   this->RebuildList();
00099   this->SortList();
00100 }
00101 
00103 void CargoDestinationList::Resort()
00104 {
00105   this->link_list.ForceResort();
00106   this->SortList();
00107 }
00108 
00114 uint CargoDestinationList::GetListHeight() const
00115 {
00116   uint lines = 1 + this->link_list.Length();
00117   return lines > 1 ? WD_PAR_VSEP_WIDE + lines * FONT_HEIGHT_NORMAL : 0;
00118 }
00119 
00127 uint CargoDestinationList::DrawList(uint left, uint right, uint y) const
00128 {
00129   if (this->link_list.Length() == 0) return y;
00130 
00131   DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += WD_PAR_VSEP_WIDE + FONT_HEIGHT_NORMAL, STR_VIEW_CARGO_LAST_MONTH_OUT);
00132 
00133   for (const GUICargoLink *l = this->link_list.Begin(); l != this->link_list.End(); l++) {
00134     SetDParam(0, l->cid);
00135     SetDParam(1, l->link->amount.old_act);
00136     SetDParam(2, l->cid);
00137     SetDParam(3, l->link->amount.old_max);
00138 
00139     /* Select string according to the destination type. */
00140     if (l->link->dest == NULL) {
00141       DrawString(left + 2 * WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, STR_VIEW_CARGO_LAST_MONTH_OTHER);
00142     } else if (l->link->dest == obj) {
00143       DrawString(left + 2 * WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, STR_VIEW_CARGO_LAST_MONTH_LOCAL);
00144     } else {
00145       SetDParam(4, l->link->dest->GetID());
00146       DrawString(left + 2 * WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, l->link->dest->GetType() == ST_TOWN ? STR_VIEW_CARGO_LAST_MONTH_TOWN : STR_VIEW_CARGO_LAST_MONTH_INDUSTRY);
00147     }
00148   }
00149 
00150   return y + FONT_HEIGHT_NORMAL;
00151 }
00152 
00157 void CargoDestinationList::OnClick(uint y) const
00158 {
00159   /* Subtract caption height. */
00160   y -= WD_PAR_VSEP_WIDE + 2 * FONT_HEIGHT_NORMAL;
00161 
00162   /* Calculate line from click pos. */
00163   y /= FONT_HEIGHT_NORMAL;
00164   if (y >= this->link_list.Length()) return;
00165 
00166   /* Move viewpoint to the position of the destination. */
00167   const CargoLink *l = this->link_list[y].link;
00168   if (l->dest == NULL) return;
00169 
00170   TileIndex xy = l->dest->GetType() == ST_TOWN ? static_cast<const Town *>(l->dest)->xy : static_cast<const Industry *>(l->dest)->location.tile;
00171   if (_ctrl_pressed) {
00172     ShowExtraViewPortWindow(xy);
00173   } else {
00174     ScrollMainWindowToTile(xy);
00175   }
00176 }

Generated on Fri Jun 3 05:18:49 2011 for OpenTTD by  doxygen 1.6.1