Develop and Download Open Source Software

Browse CVS Repository

Contents of /autocoast/src/gui/MForm.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Nov 24 13:13:33 2004 UTC (19 years, 4 months ago) by tmurakam
Branch: MAIN
CVS Tags: v0_3, v0_2, HEAD
Changes since 1.4: +7 -0 lines
File MIME type: text/x-c++src
misc

1 //---------------------------------------------------------------------------
2
3 #include <vcl.h>
4 #pragma hdrstop
5 #include <math.h>
6
7 #include "gshhs.hpp"
8 #include "bbox.hpp"
9 #include "cell.hpp"
10
11 #include "MForm.h"
12 //---------------------------------------------------------------------------
13 #pragma package(smart_init)
14 #pragma resource "*.dfm"
15
16 const crMove = 5;
17
18 TMainForm *MainForm;
19 //---------------------------------------------------------------------------
20 __fastcall TMainForm::TMainForm(TComponent* Owner)
21 : TForm(Owner)
22 {
23 min_lon = -180;
24 max_lat = 90;
25
26 mag = 12.0; //pixels/deg
27
28 isDragging = false;
29 MoveX = MoveY = false;
30 }
31 //---------------------------------------------------------------------------
32
33 void __fastcall TMainForm::LoadGSHHSdataClick(TObject *Sender)
34 {
35 if (!OpenDialog->Execute()) return;
36
37 BoundingBox bbox;
38 bbox.xmin = -180;
39 bbox.xmax = 360;
40 bbox.ymin = -90;
41 bbox.ymax = 90;
42
43 #if 0 // test
44 bbox.xmin = 125;
45 bbox.xmax = 145;
46 bbox.ymin = 25;
47 bbox.ymax = 50;
48 #endif
49
50 for (int level = 1; level <= 4; level++) {
51 polygon[level].LoadFromGshhsBinary(OpenDialog->FileName.c_str(),
52 level, bbox, -1);
53 }
54
55 PaintBox->Invalidate();
56 }
57 //---------------------------------------------------------------------------
58 void TMainForm::LonLat2Screen(double lon, double lat, int &x, int &y)
59 {
60 x = (int)((lon - min_lon) * mag);
61 y = (int)((max_lat - lat) * mag);
62 }
63
64 //---------------------------------------------------------------------------
65 void TMainForm::Screen2LonLat(int x, int y, double &lon, double &lat)
66 {
67 lon = x / mag + min_lon;
68 lat = max_lat - y / mag;
69 }
70
71 //---------------------------------------------------------------------------
72
73
74 void __fastcall TMainForm::PaintBoxPaint(TObject *Sender)
75 {
76 int i, x, y;
77
78 PaintBox->Width = 360 * mag;
79 PaintBox->Height = 180 * mag;
80
81 TCanvas *canvas = PaintBox->Canvas;
82
83 canvas->Brush->Color = clWhite;
84 canvas->Brush->Style = bsSolid;
85
86 canvas->Rectangle(0, 0, 360 * mag, 180 * mag);
87
88 canvas->Brush->Color = clBlue;
89
90 // draw grids
91 if (mag >= 16.0) {
92 canvas->Brush->Color = clLtGray;
93
94 for (i = 0; i < 768; i++) {
95 LonLat2Screen(i * 360.0 / 768.0 - 180, 90, x, y);
96 canvas->MoveTo(x, y);
97 LonLat2Screen(i * 360.0 / 768.0 - 180, -90, x, y);
98 canvas->LineTo(x, y);
99 }
100 for (i = 0; i < 512; i++) {
101 LonLat2Screen(-180, 90 - i * 180.0 / 512.0, x, y);
102 canvas->MoveTo(x, y);
103 LonLat2Screen( 180, 90 - i * 180.0 / 512.0, x, y);
104 canvas->LineTo(x, y);
105 }
106 }
107
108 // draw contuors
109 canvas->Brush->Color = clBlue;
110 for (int level = 1; level <= 4; level++) {
111 int nvlist = polygon[level].numVList();
112 for (int i = 0; i < nvlist; i++) {
113 GpcVertexList *vlist = polygon[level].getVList(i);
114
115 // draw contour
116 int n = vlist->numVertices();
117 double lat, lon;
118 double prev_lon = 0;
119
120 for (int j = 0; j < n; j++) {
121 lon = vlist->vx(j);
122 lat = vlist->vy(j);
123
124 if (lon > 180) lon -= 360;
125 if (lon < -180 || lon > 180) {
126 Application->MessageBoxA("ouch", "ouch");
127 }
128
129 // convert coordinates
130 LonLat2Screen(lon, lat, x, y);
131 if (x < 0 || 360 * mag < x) {
132 Application->MessageBox("ouch", "ouch");
133 }
134 if (j == 0) {
135 canvas->MoveTo(x, y);
136 } else if (fabs(lon - prev_lon) < 300) {
137 canvas->LineTo(x, y);
138 } else {
139 canvas->MoveTo(x, y);
140 }
141
142 prev_lon = lon;
143 }
144 }
145 }
146 }
147
148
149 //---------------------------------------------------------------------------
150 // Mouse Down : drag start
151 void __fastcall TMainForm::PaintBoxMouseDown(TObject *Sender,
152 TMouseButton Button, TShiftState Shift, int X, int Y)
153 {
154 // drag
155 isDragging = true;
156
157 // save drag start point
158 POINT p;
159 GetCursorPos(&p);
160 DragX = p.x;
161 DragY = p.y;
162
163 MoveX = MoveY = 0;
164 Timer->Enabled = true;
165
166 Screen->Cursor = crMove;
167 }
168
169 //---------------------------------------------------------------------------
170 // Mouse Up : stop drag
171 void __fastcall TMainForm::PaintBoxMouseUp(TObject *Sender,
172 TMouseButton Button, TShiftState Shift, int X, int Y)
173 {
174 if (isDragging) {
175 isDragging = false;
176 Timer->Enabled = false;
177 Screen->Cursor = crDefault;
178 }
179 }
180
181 //---------------------------------------------------------------------------
182 // Mouse move : dragging etc.
183 void __fastcall TMainForm::PaintBoxMouseMove(TObject *Sender,
184 TShiftState Shift, int X, int Y)
185 {
186 double lon, lat;
187
188 Screen2LonLat(X, Y, lon, lat);
189
190 int cx = Cell::lon2x(lon);
191 int cy = Cell::lat2y(lat);
192
193 AnsiString s;
194 s.sprintf("Lat:%f Lon:%f Cell(%d, %d)", lon, lat, cx, cy);
195 StatusBar->SimpleText = s;
196
197 if (isDragging) {
198 POINT p;
199 GetCursorPos(&p);
200 MoveX = p.x - DragX;
201 MoveY = p.y - DragY;
202 }
203 }
204 //---------------------------------------------------------------------------
205 // Scroll window
206 void __fastcall TMainForm::OnTimer(TObject *Sender)
207 {
208 if (!isDragging) return;
209
210 ScrollBox->HorzScrollBar->Position += MoveX * 0.5;
211 ScrollBox->VertScrollBar->Position += MoveY * 0.5;
212 }
213
214
215 //---------------------------------------------------------------------------
216
217
218 void __fastcall TMainForm::ZoomInClick(TObject *Sender)
219 {
220 SetZoom(2);
221 }
222 void __fastcall TMainForm::ZoomOutClick(TObject *Sender)
223 {
224 SetZoom(0.5);
225 }
226
227 //---------------------------------------------------------------------------
228 // �g���k��
229 void TMainForm::SetZoom(double m)
230 {
231 // display area size
232 int width = ScrollBox->Width;
233 int height = ScrollBox->Height;
234
235 // get current center
236 int cx = ScrollBox->HorzScrollBar->Position + width / 2;
237 int cy = ScrollBox->VertScrollBar->Position + height / 2;
238
239 // magnify
240 mag *= m;
241 cx *= m;
242 cy *= m;
243
244 // move to new center
245 ScrollBox->HorzScrollBar->Position = cx - width / 2;
246 ScrollBox->VertScrollBar->Position = cy - height / 2;
247
248 PaintBox->Invalidate();
249 }
250
251 //---------------------------------------------------------------------------
252
253

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26