ItemRelativeGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003-2005  The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "pent_include.h"
00020 #include "ItemRelativeGump.h"
00021 #include "GameMapGump.h"
00022 #include "Item.h"
00023 #include "Container.h"
00024 #include "ShapeInfo.h"
00025 #include "getObject.h"
00026 #include "IDataSource.h"
00027 #include "ODataSource.h"
00028 
00029 DEFINE_RUNTIME_CLASSTYPE_CODE(ItemRelativeGump,Gump);
00030 
00031 ItemRelativeGump::ItemRelativeGump()
00032         : Gump(), ix(0), iy(0)
00033 {
00034 }
00035 
00036 ItemRelativeGump::ItemRelativeGump(int x, int y, int width, int height,
00037                                                                    uint16 owner, uint32 _Flags, sint32 _Layer)
00038         : Gump(x, y, width, height, owner, _Flags, _Layer), ix(0), iy(0)
00039 {
00040 }
00041 
00042 ItemRelativeGump::~ItemRelativeGump(void)
00043 {
00044 }
00045 
00046 void ItemRelativeGump::InitGump(Gump* newparent, bool take_focus)
00047 {
00048         Gump::InitGump(newparent, take_focus);
00049 
00050         GetItemLocation(0);
00051 
00052         if (!newparent && parent)
00053                 MoveOnScreen();
00054 }
00055 
00056 void ItemRelativeGump::MoveOnScreen()
00057 {
00058         assert(parent);
00059         Pentagram::Rect sd, gd;
00060         parent->GetDims(sd);
00061 
00062         // first move back to our desired location
00063         x = 0;
00064         y = 0;
00065 
00066         // get rectangle that gump occupies in scalerGump's coordinate space
00067         sint32 left,right,top,bottom;
00068         left = -dims.x;
00069         right = left + dims.w;
00070         top = -dims.y;
00071         bottom = top + dims.h;
00072         GumpToParent(left,top);
00073         GumpToParent(right,bottom);
00074 
00075         sint32 movex = 0, movey = 0;
00076 
00077         if (left < -sd.x)
00078                 movex = -sd.x - left;
00079         else if (right > -sd.x + sd.w)
00080                 movex = -sd.x + sd.w - right;
00081 
00082         if (top < -sd.y)
00083                 movey = -sd.y - top;
00084         else if (bottom > -sd.y + sd.h)
00085                 movey = -sd.y + sd.h - bottom;
00086 
00087         Move(left+movex, top+movey);
00088 }
00089 
00090 // Paint the Gump (RenderSurface is relative to parent).
00091 // Calls PaintThis and PaintChildren
00092 void ItemRelativeGump::Paint(RenderSurface*surf, sint32 lerp_factor, bool scaled)
00093 {
00094         GetItemLocation(lerp_factor);
00095         Gump::Paint(surf,lerp_factor, scaled);
00096 }
00097 
00098 
00099 // Convert a parent relative point to a gump point
00100 void ItemRelativeGump::ParentToGump(int &px, int &py, PointRoundDir r)
00101 {
00102         px -= ix; 
00103         py -= iy;
00104         Gump::ParentToGump(px,py,r);
00105 }
00106 
00107 // Convert a gump point to parent relative point
00108 void ItemRelativeGump::GumpToParent(int &gx, int &gy, PointRoundDir r)
00109 {
00110         Gump::GumpToParent(gx,gy,r);
00111         gx += ix;
00112         gy += iy;
00113 }
00114 
00115 void ItemRelativeGump::GetItemLocation(sint32 lerp_factor)
00116 {
00117         Item *it = 0;
00118         Item *next = 0;
00119         Item *prev = 0;
00120         Gump *gump = 0;
00121 
00122         it = getItem(owner);
00123 
00124         if (!it) {
00125                 // This shouldn't ever happen, the GumpNotifyProcess should
00126                 // close us before we get here
00127                 Close();
00128                 return;
00129         }
00130 
00131         while ((next = it->getParentAsContainer()) != 0)
00132         {
00133                 prev = it;
00134                 it = next;
00135                 gump = getGump(it->getGump());
00136                 if (gump) break;
00137         }
00138 
00139         int gx, gy;
00140 
00141         if (!gump)
00142         {
00143                 gump = GetRootGump()->FindGump(GameMapGump::ClassType);
00144 
00145                 if (!gump) {
00146                         perr << "ItemRelativeGump::GetItemLocation(): "
00147                                  << "Unable to find GameMapGump!?!?" << std::endl;
00148                         return;
00149                 }
00150 
00151                 gump->GetLocationOfItem(owner, gx, gy, lerp_factor);
00152         }
00153         else
00154         {
00155                 gump->GetLocationOfItem(prev->getObjId(), gx, gy, lerp_factor);
00156         }
00157 
00158         // Convert the GumpSpaceCoord relative to the world/item gump
00159         // into screenspace coords
00160         gy = gy-it->getShapeInfo()->z*8-16;
00161         gump->GumpToScreenSpace(gx,gy);
00162 
00163         // Convert the screenspace coords into the coords of us
00164         if (parent) parent->ScreenSpaceToGump(gx,gy);
00165 
00166         // Set x and y, and center us over it
00167         ix = gx-dims.w/2;
00168 //      iy = gy-dims.h-it->getShapeInfo()->z*8-16;
00169         iy = gy-dims.h;
00170 
00171 
00172         if (flags & FLAG_KEEP_VISIBLE)
00173                 MoveOnScreen();
00174 }
00175 
00176 void ItemRelativeGump::Move(int x_, int y_)
00177 {
00178         ParentToGump(x_, y_);
00179         x += x_;
00180         y += y_;
00181 }
00182 
00183 void ItemRelativeGump::saveData(ODataSource* ods)
00184 {
00185         Gump::saveData(ods);
00186 }
00187 
00188 bool ItemRelativeGump::loadData(IDataSource* ids, uint32 version)
00189 {
00190         if (!Gump::loadData(ids, version)) return false;
00191 
00192         return true;
00193 }
00194 
00195 // Colourless Protection

Generated on Fri Jul 27 22:27:22 2007 for pentagram by  doxygen 1.4.7