OSDN Git Service

f11c7b5dba8e82200d1a4d77c088bcf01036a632
[radegast/radegast.git] / Radegast / GUI / Consoles / Assets / Landmark.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2013, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
9 //     * Redistributions of source code must retain the above copyright notice,
10 //       this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above copyright
12 //       notice, this list of conditions and the following disclaimer in the
13 //       documentation and/or other materials provided with the distribution.
14 //     * Neither the name of the application "Radegast", nor the names of its
15 //       contributors may be used to endorse or promote products derived from
16 //       this software without specific prior written permission.
17 // 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Windows.Forms;
33 using OpenMetaverse;
34 using OpenMetaverse.Assets;
35
36 namespace Radegast
37 {
38     public partial class Landmark : DettachableControl
39     {
40         private RadegastInstance instance;
41         private GridClient client { get { return instance.Client; } }
42         private InventoryLandmark landmark;
43         private AssetLandmark decodedLandmark;
44         private UUID parcelID;
45         private ParcelInfo parcel;
46         private Vector3 localPosition;
47         private bool parcelLocation = false;
48
49         public Landmark(RadegastInstance instance, InventoryLandmark landmark)
50         {
51             this.landmark = landmark;
52             Init(instance);
53             client.Assets.RequestAsset(landmark.AssetUUID, landmark.AssetType, true, Assets_OnAssetReceived);
54         }
55
56         public Landmark(RadegastInstance instance, UUID parcelID)
57         {
58             this.parcelID = parcelID;
59             Init(instance);
60             parcelLocation = true;
61             client.Parcels.RequestParcelInfo(parcelID);
62         }
63
64         void Init(RadegastInstance instance)
65         {
66             InitializeComponent();
67             Disposed += new EventHandler(Landmark_Disposed);
68
69             this.instance = instance;
70
71             // Callbacks
72             client.Grid.RegionHandleReply += new EventHandler<RegionHandleReplyEventArgs>(Grid_RegionHandleReply);
73             client.Parcels.ParcelInfoReply += new EventHandler<ParcelInfoReplyEventArgs>(Parcels_ParcelInfoReply);
74         }
75
76         void Landmark_Disposed(object sender, EventArgs e)
77         {
78             client.Grid.RegionHandleReply -= new EventHandler<RegionHandleReplyEventArgs>(Grid_RegionHandleReply);
79             client.Parcels.ParcelInfoReply -= new EventHandler<ParcelInfoReplyEventArgs>(Parcels_ParcelInfoReply);
80         }
81
82         void Parcels_ParcelInfoReply(object sender, ParcelInfoReplyEventArgs e)
83         {
84             if (e.Parcel.ID != parcelID) return;
85
86             if (InvokeRequired)
87             {
88                 if (!instance.MonoRuntime || IsHandleCreated)
89                     BeginInvoke(new MethodInvoker(() => Parcels_ParcelInfoReply(sender, e)));
90                 return;
91             }
92
93             this.parcel = e.Parcel;
94
95             pnlDetail.Visible = true;
96             if (parcel.SnapshotID != UUID.Zero)
97             {
98                 SLImageHandler img = new SLImageHandler(instance, parcel.SnapshotID, "");
99                 img.Dock = DockStyle.Fill;
100                 pnlDetail.Controls.Add(img);
101                 pnlDetail.Disposed += (object senderx, EventArgs ex) =>
102                 {
103                     img.Dispose();
104                 };
105                 img.BringToFront();
106             }
107
108             btnTeleport.Enabled = true;
109             btnShowOnMap.Enabled = true;
110
111             if (parcelLocation)
112             {
113                 localPosition = new Vector3();
114                 localPosition.X = parcel.GlobalX % 256;
115                 localPosition.Y = parcel.GlobalY % 256;
116                 localPosition.Z = parcel.GlobalZ;
117             }
118
119             if (decodedLandmark == null)
120             {
121                 txtParcelName.Text = string.Format("{0} - {1} ", parcel.Name, parcel.SimName);
122             }
123             else
124             {
125                 txtParcelName.Text = string.Format("{0} - {1} ({2}, {3}, {4}) ", parcel.Name, parcel.SimName, (int)decodedLandmark.Position.X, (int)decodedLandmark.Position.Y, (int)decodedLandmark.Position.Z);
126             }
127
128             txtParcelDescription.Text = parcel.Description;
129         }
130
131         void Grid_RegionHandleReply(object sender, RegionHandleReplyEventArgs e)
132         {
133             if (decodedLandmark == null || decodedLandmark.RegionID != e.RegionID) return;
134
135             parcelID = client.Parcels.RequestRemoteParcelID(decodedLandmark.Position, e.RegionHandle, e.RegionID);
136             if (parcelID != UUID.Zero)
137             {
138                 client.Parcels.RequestParcelInfo(parcelID);
139             }
140         }
141
142         void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)
143         {
144             if (transfer.Success && asset.AssetType == AssetType.Landmark)
145             {
146                 decodedLandmark = (AssetLandmark)asset;
147                 decodedLandmark.Decode();
148                 localPosition = decodedLandmark.Position;
149                 client.Grid.RequestRegionHandle(decodedLandmark.RegionID);
150             }
151         }
152
153         private void btnTeleport_Click(object sender, EventArgs e)
154         {
155             instance.MainForm.WorldMap.DisplayLocation(parcel.SimName,
156                 (int)localPosition.X,
157                 (int)localPosition.Y,
158                 (int)localPosition.Z);
159             instance.MainForm.WorldMap.DoTeleport();
160         }
161
162         private void btnShowOnMap_Click(object sender, EventArgs e)
163         {
164             instance.MainForm.WorldMap.Show();
165             instance.MainForm.WorldMap.DisplayLocation(parcel.SimName, 
166                 (int)localPosition.X,
167                 (int)localPosition.Y,
168                 (int)localPosition.Z);
169         }
170     }
171 }