summaryrefslogtreecommitdiff
path: root/src/GipfelWidget.cxx
blob: 365def129301b95549fc7b3d4dd07da61b39297e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 
// "$Id: GipfelWidget.cxx,v 1.11 2005/04/24 09:51:17 hofmann Exp $"
//
// PSEditWidget routines.
//
// Copyright 2004 by Johannes Hofmann
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>

#include <FL/Fl.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_JPEG_Image.H>
#include <FL/fl_draw.H>
#include <FL/x.H>

#include "GipfelWidget.H"


GipfelWidget::GipfelWidget(int X,int Y,int W, int H): Fl_Widget(X, Y, W, H) {
  img = NULL;
  pan = new Panorama();
  cur_mountain = NULL;
  fl_register_images();
}

int
GipfelWidget::load_image(const char *file) {
  img = new Fl_JPEG_Image(file);
  
  if (img == NULL) {
    return 1;
  }

  w(img->w());
  h(img->h());
  return 0;
}

int
GipfelWidget::load_data(const char *file) {
  return pan->load_file(file);
}

int
GipfelWidget::set_viewpoint(const char *pos) {
  return pan->set_viewpoint(pos);
}

void 
GipfelWidget::draw() {
  Mountain *m;
  int center_x = w() / 2;
  int center_y = h() / 2;
  
  if (img == NULL) {
    return;
  }

  fl_push_clip(x(), y(), w(), h());
  img->draw(x(),y(),w(),h(),0,0);

  fl_font(FL_HELVETICA, 8);
  m = pan->get_visible_mountains();
  while (m) {
    if (m == cur_mountain) {
      fl_color(FL_RED);
    } else {
      fl_color(FL_BLACK);
    }
    
    fl_rectf(center_x + m->x + x() - 2, 
	     center_y + m->y + y() - 2,
	     4,
	     4);
      //    fl_line(center_x + m->x + x(), 0 + y(), center_x + m->x + x(), h() + y());
    fl_draw(m->name, 
	    center_x + m->x + x(), 
	    center_y + m->y + y());
    m = m->get_next_visible();
  }

  fl_pop_clip();
}

int
GipfelWidget::set_cur_mountain(int m_x, int m_y) {
  Mountain *m = pan->get_visible_mountains();
  int center = w() / 2;

  while (m) {
    if (m_x - center >= m->x - 2 && m_x - center < m->x + 2) {
      cur_mountain = m;
      redraw();
      return 0;
    }

    m = m->get_next_visible();
  }

  cur_mountain = NULL;
  redraw();
  return 1;
}

int
GipfelWidget::set_mountain(int m_x, int m_y) {
  int ret;
  int center_x = w() / 2;
  int center_y = h() / 2;

  if (cur_mountain == NULL) {
    return 1;
  }

  ret = pan->set_mountain(cur_mountain, m_x - center_x, m_y - center_y);
  
  redraw();
  return ret;
}

void
GipfelWidget::set_center_angle(double a) {
  pan->set_center_angle(a);
  redraw();
}

void
GipfelWidget::set_nick_angle(double a) {
  pan->set_nick_angle(a);
  redraw();
}

void
GipfelWidget::set_tilt_angle(double a) {
  pan->set_tilt_angle(a);
  redraw();
}

void
GipfelWidget::set_scale(double s) {
  pan->set_scale(s);
  redraw();
}

void
GipfelWidget::set_height_dist_ratio(double r) {
  pan->set_height_dist_ratio(r);
  redraw();
}

int
GipfelWidget::comp_params() {
  pan->comp_params();
  redraw();
}


int
GipfelWidget::newton() {
  pan->newton();
  redraw();
}

int
GipfelWidget::handle(int event) {
  int mark_x, mark_y;

  switch(event) {
  case FL_PUSH:    
    if (Fl::event_button() == 1) {
         
      mark_x = Fl::event_x()-x();
      mark_y = Fl::event_y()-y();
      fprintf(stderr, "x %d, y %d\n", mark_x, mark_y);
      set_cur_mountain(mark_x, mark_y);

      Fl::focus(this);
      return 1;
    }
    break;
  case FL_DRAG:
    set_mountain(Fl::event_x()-x(), Fl::event_y()-y());
    return 1;
    break;
  case FL_FOCUS:
    return 1;
    break;
  case FL_UNFOCUS:
    return 0;
    break;
  }
  return 0;
}