… | |
… | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $ROOT; |
12 | our $ROOT; |
|
|
13 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
14 | our $BUTTON_STATE; |
|
|
15 | |
|
|
16 | sub check_tooltip { |
|
|
17 | if (!$GRAB) { |
|
|
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
19 | if (length $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_markup ($widget->{tooltip}); |
|
|
29 | |
|
|
30 | $TOOLTIP->show; |
|
|
31 | |
|
|
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
|
|
33 | |
|
|
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
|
|
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->move ($x, $y); |
|
|
39 | } |
|
|
40 | |
|
|
41 | return; |
|
|
42 | } |
|
|
43 | } |
|
|
44 | } |
|
|
45 | |
|
|
46 | $TOOLTIP->hide; |
|
|
47 | delete $TOOLTIP->{owner}; |
|
|
48 | } |
14 | |
49 | |
15 | # class methods for events |
50 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
51 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
52 | $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) |
|
|
53 | if $FOCUS; |
18 | } |
54 | } |
19 | |
55 | |
20 | sub feed_sdl_key_up_event { |
56 | sub feed_sdl_key_up_event { |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
57 | $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) |
|
|
58 | if $FOCUS; |
22 | } |
59 | } |
23 | |
60 | |
24 | sub feed_sdl_button_down_event { |
61 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
62 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
63 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
64 | |
28 | if (!$BUTTON_STATE) { |
65 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
66 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
67 | |
31 | $GRAB = $widget; |
68 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
69 | $GRAB->update if $GRAB; |
33 | } |
|
|
34 | |
70 | |
|
|
71 | check_tooltip; |
|
|
72 | } |
|
|
73 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
74 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
75 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
76 | if ($GRAB) { |
|
|
77 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
78 | $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y); |
|
|
79 | } |
38 | } |
80 | } |
39 | |
81 | |
40 | sub feed_sdl_button_up_event { |
82 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
83 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
84 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
85 | |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
86 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
87 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
88 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
89 | |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
90 | if ($GRAB) { |
|
|
91 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
92 | $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y); |
|
|
93 | } |
49 | |
94 | |
50 | if (!$BUTTON_STATE) { |
95 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
96 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
97 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
98 | $GRAB->update if $GRAB; |
|
|
99 | |
|
|
100 | check_tooltip; |
54 | } |
101 | } |
55 | } |
102 | } |
56 | |
103 | |
57 | sub feed_sdl_motion_event { |
104 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
105 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
106 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
107 | |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
108 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
109 | |
63 | if ($widget != $HOVER) { |
110 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
111 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
112 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
113 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
114 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
|
|
69 | |
115 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
116 | check_tooltip; |
|
|
117 | } |
|
|
118 | |
|
|
119 | if ($HOVER) { |
|
|
120 | ($x, $y) = $HOVER->coord2local ($x, $y); |
|
|
121 | $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); |
|
|
122 | } |
71 | } |
123 | } |
72 | |
124 | |
73 | # convert position array to integers |
125 | # convert position array to integers |
74 | sub harmonize { |
126 | sub harmonize { |
75 | my ($vals) = @_; |
127 | my ($vals) = @_; |
76 | |
128 | |
77 | my $rem = 0; |
129 | my $rem = 0; |
78 | |
130 | |
79 | for ($vals) { |
131 | for (@$vals) { |
80 | my $i = int $_ + $rem; |
132 | my $i = int $_ + $rem; |
81 | $rem += $_ - $i; |
133 | $rem += $_ - $i; |
82 | $_ = $i; |
134 | $_ = $i; |
83 | } |
135 | } |
84 | } |
136 | } |
… | |
… | |
87 | |
139 | |
88 | package CFClient::UI::Base; |
140 | package CFClient::UI::Base; |
89 | |
141 | |
90 | use strict; |
142 | use strict; |
91 | |
143 | |
92 | use SDL::OpenGL; |
144 | use CFClient::OpenGL; |
93 | |
145 | |
94 | sub new { |
146 | sub new { |
95 | my $class = shift; |
147 | my $class = shift; |
96 | |
148 | |
97 | my $self = bless { |
149 | my $self = bless { |
98 | x => 0, |
150 | x => 0, |
99 | y => 0, |
151 | y => 0, |
100 | z => 0, |
152 | z => 0, |
101 | w => -1, |
153 | can_events => 1, |
102 | h => -1, |
|
|
103 | @_ |
154 | @_ |
104 | }, $class; |
155 | }, $class; |
105 | |
156 | |
106 | for (keys %$self) { |
157 | for (keys %$self) { |
107 | if (/^connect_(.*)$/) { |
158 | if (/^connect_(.*)$/) { |
… | |
… | |
110 | } |
161 | } |
111 | |
162 | |
112 | $self |
163 | $self |
113 | } |
164 | } |
114 | |
165 | |
|
|
166 | sub destroy { |
|
|
167 | my ($self) = @_; |
|
|
168 | |
|
|
169 | $self->hide; |
|
|
170 | %$self = (); |
|
|
171 | } |
|
|
172 | |
|
|
173 | sub show { |
|
|
174 | my ($self) = @_; |
|
|
175 | |
|
|
176 | return if $self->{parent}; |
|
|
177 | |
|
|
178 | $CFClient::UI::ROOT->add ($self); |
|
|
179 | } |
|
|
180 | |
|
|
181 | sub hide { |
|
|
182 | my ($self) = @_; |
|
|
183 | |
|
|
184 | undef $GRAB if $GRAB == $self; |
|
|
185 | undef $HOVER if $HOVER == $self; |
|
|
186 | |
|
|
187 | $self->{parent}->remove ($self) |
|
|
188 | if $self->{parent}; |
|
|
189 | } |
|
|
190 | |
115 | sub move { |
191 | sub move { |
116 | my ($self, $x, $y, $z) = @_; |
192 | my ($self, $x, $y, $z) = @_; |
|
|
193 | |
117 | $self->{x} = $x; |
194 | $self->{x} = int $x; |
118 | $self->{y} = $y; |
195 | $self->{y} = int $y; |
119 | $self->{z} = $z if defined $z; |
196 | $self->{z} = $z if defined $z; |
120 | } |
|
|
121 | |
197 | |
122 | sub needs_redraw { |
198 | $self->update; |
123 | 0 |
199 | } |
|
|
200 | |
|
|
201 | sub set_size { |
|
|
202 | my ($self, $w, $h) = @_; |
|
|
203 | |
|
|
204 | $self->{user_w} = $w; |
|
|
205 | $self->{user_h} = $h; |
|
|
206 | |
|
|
207 | $self->check_size; |
124 | } |
208 | } |
125 | |
209 | |
126 | sub size_request { |
210 | sub size_request { |
127 | require Carp; |
211 | require Carp; |
128 | Carp::confess "size_request is abtract"; |
212 | Carp::confess "size_request is abstract"; |
129 | } |
213 | } |
130 | |
214 | |
131 | sub _size_allocate { |
215 | sub configure { |
132 | my ($self, $x, $y, $w, $h) = @_; |
216 | my ($self, $x, $y, $w, $h) = @_; |
133 | |
217 | |
|
|
218 | if ($self->{aspect}) { |
|
|
219 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
220 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
221 | |
|
|
222 | # use alignment to adjust x, y |
|
|
223 | |
|
|
224 | $x += int +($w - $w2) * 0.5; |
|
|
225 | $y += int +($h - $h2) * 0.5; |
|
|
226 | |
|
|
227 | ($w, $h) = ($w2, $h2); |
|
|
228 | } |
|
|
229 | |
|
|
230 | if ($self->{x} != $x || $self->{y} != $y) { |
134 | $self->{x} = $x; |
231 | $self->{x} = $x; |
135 | $self->{y} = $y; |
232 | $self->{y} = $y; |
|
|
233 | $self->update; |
|
|
234 | } |
136 | |
235 | |
137 | return unless $self->{w} != $w || $self->{h} != $h; |
236 | if ($self->{w} != $w || $self->{h} != $h) { |
138 | |
237 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; |
139 | $self->{w} = $w; |
|
|
140 | $self->{h} = $h; |
|
|
141 | |
|
|
142 | 1 |
238 | } |
143 | } |
239 | } |
144 | |
240 | |
145 | sub size_allocate { |
241 | sub size_allocate { |
146 | my ($self, $x, $y, $w, $h) = @_; |
242 | # nothing to be done |
147 | |
|
|
148 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
149 | } |
243 | } |
150 | |
244 | |
151 | # return top left coordinates |
245 | sub children { |
152 | sub _topleft { |
246 | } |
|
|
247 | |
|
|
248 | # call when resolution changes etc. |
|
|
249 | sub reconfigure { |
|
|
250 | my ($self) = @_; |
|
|
251 | |
|
|
252 | $_->reconfigure |
|
|
253 | for $self->children; |
|
|
254 | |
|
|
255 | $self->check_size; |
|
|
256 | $self->size_allocate ($self->{w}, $self->{h}); |
|
|
257 | $self->update; |
|
|
258 | } |
|
|
259 | |
|
|
260 | sub set_max_size { |
153 | my ($self, $x, $y) = @_; |
261 | my ($self, $w, $h) = @_; |
154 | |
262 | |
155 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
263 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
264 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
156 | } |
265 | } |
157 | |
266 | |
158 | # translate global coordinates to local coordinate system |
267 | # translate global coordinates to local coordinate system |
159 | sub coord2local { |
268 | sub coord2local { |
160 | my ($self, $x, $y) = @_; |
269 | my ($self, $x, $y) = @_; |
161 | |
270 | |
162 | my ($X, $Y) = $self->_topleft; |
271 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
163 | ($x - $X, $y - $Y) |
|
|
164 | } |
272 | } |
165 | |
273 | |
166 | # translate local coordinates to global coordinate system |
274 | # translate local coordinates to global coordinate system |
167 | sub coord2global { |
275 | sub coord2global { |
168 | my ($self, $x, $y) = @_; |
276 | my ($self, $x, $y) = @_; |
169 | |
277 | |
170 | my ($X, $Y) = $self->_topleft; |
278 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
171 | ($x + $X, $y + $Y) |
|
|
172 | } |
279 | } |
173 | |
280 | |
174 | sub focus_in { |
281 | sub focus_in { |
175 | my ($self) = @_; |
282 | my ($self) = @_; |
176 | |
283 | |
177 | return if $FOCUS == $self; |
284 | return if $FOCUS == $self; |
178 | return unless $self->{can_focus}; |
285 | return unless $self->{can_focus}; |
179 | |
286 | |
180 | my $focus = $FOCUS; $FOCUS = $self; |
287 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
288 | |
|
|
289 | $self->emit (focus_in => $focus); |
|
|
290 | |
181 | $focus->update if $focus; |
291 | $focus->update if $focus; |
182 | $FOCUS->update; |
292 | $FOCUS->update; |
183 | } |
293 | } |
184 | |
294 | |
185 | sub focus_out { |
295 | sub focus_out { |
186 | my ($self) = @_; |
296 | my ($self) = @_; |
187 | |
297 | |
188 | return unless $FOCUS == $self; |
298 | return unless $FOCUS == $self; |
189 | |
299 | |
190 | my $focus = $FOCUS; undef $FOCUS; |
300 | my $focus = $FOCUS; undef $FOCUS; |
|
|
301 | |
|
|
302 | $self->emit (focus_out => $focus); |
|
|
303 | |
191 | $focus->update if $focus; #? |
304 | $focus->update if $focus; #? |
192 | } |
305 | } |
193 | |
306 | |
194 | sub mouse_motion { } |
307 | sub mouse_motion { } |
195 | sub button_up { } |
308 | sub button_up { } |
… | |
… | |
219 | glPopMatrix; |
332 | glPopMatrix; |
220 | |
333 | |
221 | if ($self == $HOVER && $self->{can_hover}) { |
334 | if ($self == $HOVER && $self->{can_hover}) { |
222 | my ($x, $y) = @$self{qw(x y)}; |
335 | my ($x, $y) = @$self{qw(x y)}; |
223 | |
336 | |
224 | glColor 0, 0, 1, 0.2; |
337 | glColor 1, 0.8, 0.5, 0.2; |
225 | glEnable GL_BLEND; |
338 | glEnable GL_BLEND; |
226 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
339 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
227 | glBegin GL_QUADS; |
340 | glBegin GL_QUADS; |
228 | glVertex $x , $y; |
341 | glVertex $x , $y; |
229 | glVertex $x + $self->{w}, $y; |
342 | glVertex $x + $self->{w}, $y; |
230 | glVertex $x + $self->{w}, $y + $self->{h}; |
343 | glVertex $x + $self->{w}, $y + $self->{h}; |
231 | glVertex $x , $y + $self->{h}; |
344 | glVertex $x , $y + $self->{h}; |
232 | glEnd; |
345 | glEnd; |
233 | glDisable GL_BLEND; |
346 | glDisable GL_BLEND; |
234 | } |
347 | } |
|
|
348 | |
|
|
349 | if ($ENV{PCLIENT_DEBUG}) { |
|
|
350 | glPushMatrix; |
|
|
351 | glColor 1, 1, 0, 1; |
|
|
352 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
|
|
353 | glBegin GL_LINE_LOOP; |
|
|
354 | glVertex 0 , 0; |
|
|
355 | glVertex $self->{w}, 0; |
|
|
356 | glVertex $self->{w}, $self->{h}; |
|
|
357 | glVertex 0 , $self->{h}; |
|
|
358 | glEnd; |
|
|
359 | glPopMatrix; |
|
|
360 | CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
|
|
361 | } |
235 | } |
362 | } |
236 | |
363 | |
237 | sub _draw { |
364 | sub _draw { |
238 | my ($self) = @_; |
365 | my ($self) = @_; |
239 | |
366 | |
240 | warn "no draw defined for $self\n"; |
367 | warn "no draw defined for $self\n"; |
241 | } |
368 | } |
242 | |
369 | |
243 | sub find_widget { |
370 | sub find_widget { |
244 | my ($self, $x, $y) = @_; |
371 | my ($self, $x, $y) = @_; |
|
|
372 | |
|
|
373 | return () unless $self->{can_events}; |
245 | |
374 | |
246 | return $self |
375 | return $self |
247 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
376 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
248 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
377 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
249 | |
378 | |
… | |
… | |
251 | } |
380 | } |
252 | |
381 | |
253 | sub set_parent { |
382 | sub set_parent { |
254 | my ($self, $parent) = @_; |
383 | my ($self, $parent) = @_; |
255 | |
384 | |
|
|
385 | $self->{_tree_depth} = $parent->{_tree_depth} + 1; |
256 | Scalar::Util::weaken ($self->{parent} = $parent); |
386 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
387 | } |
|
|
388 | |
|
|
389 | sub check_size { |
|
|
390 | my ($self) = @_; |
|
|
391 | |
|
|
392 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
257 | } |
393 | } |
258 | |
394 | |
259 | sub update { |
395 | sub update { |
260 | my ($self) = @_; |
396 | my ($self) = @_; |
261 | |
397 | |
… | |
… | |
264 | } |
400 | } |
265 | |
401 | |
266 | sub connect { |
402 | sub connect { |
267 | my ($self, $signal, $cb) = @_; |
403 | my ($self, $signal, $cb) = @_; |
268 | |
404 | |
269 | push @{ $self->{cb}{$signal} }, $cb; |
405 | push @{ $self->{signal_cb}{$signal} }, $cb; |
270 | } |
406 | } |
271 | |
407 | |
272 | sub emit { |
408 | sub emit { |
273 | my ($self, $signal, @args) = @_; |
409 | my ($self, $signal, @args) = @_; |
274 | |
410 | |
275 | $_->($self, @args) |
411 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
276 | for @{$self->{cb}{$signal} || []}; |
|
|
277 | } |
412 | } |
278 | |
413 | |
279 | sub DESTROY { |
414 | sub DESTROY { |
280 | my ($self) = @_; |
415 | my ($self) = @_; |
281 | |
416 | |
… | |
… | |
287 | package CFClient::UI::DrawBG; |
422 | package CFClient::UI::DrawBG; |
288 | |
423 | |
289 | our @ISA = CFClient::UI::Base::; |
424 | our @ISA = CFClient::UI::Base::; |
290 | |
425 | |
291 | use strict; |
426 | use strict; |
292 | use SDL::OpenGL; |
427 | use CFClient::OpenGL; |
293 | |
428 | |
294 | sub new { |
429 | sub new { |
295 | my $class = shift; |
430 | my $class = shift; |
296 | |
431 | |
297 | # range [value, low, high, page] |
432 | # range [value, low, high, page] |
… | |
… | |
326 | |
461 | |
327 | package CFClient::UI::Empty; |
462 | package CFClient::UI::Empty; |
328 | |
463 | |
329 | our @ISA = CFClient::UI::Base::; |
464 | our @ISA = CFClient::UI::Base::; |
330 | |
465 | |
|
|
466 | sub new { |
|
|
467 | my ($class, %arg) = @_; |
|
|
468 | $class->SUPER::new (can_events => 0, %arg); |
|
|
469 | } |
|
|
470 | |
331 | sub size_request { |
471 | sub size_request { |
332 | (0, 0) |
472 | (0, 0) |
333 | } |
473 | } |
334 | |
474 | |
335 | sub draw { } |
475 | sub draw { } |
… | |
… | |
343 | sub new { |
483 | sub new { |
344 | my ($class, %arg) = @_; |
484 | my ($class, %arg) = @_; |
345 | |
485 | |
346 | my $children = delete $arg{children} || []; |
486 | my $children = delete $arg{children} || []; |
347 | |
487 | |
348 | my $self = $class->SUPER::new (children => [], %arg); |
488 | my $self = $class->SUPER::new ( |
|
|
489 | children => [], |
|
|
490 | can_events => 0, |
|
|
491 | %arg, |
|
|
492 | ); |
349 | $self->add ($_) for @$children; |
493 | $self->add ($_) for @$children; |
350 | |
494 | |
351 | $self |
495 | $self |
352 | } |
496 | } |
353 | |
497 | |
354 | sub add { |
498 | sub add { |
355 | my ($self, $child) = @_; |
499 | my ($self, @widgets) = @_; |
356 | |
500 | |
357 | $child->set_parent ($self); |
501 | $_->set_parent ($self) |
|
|
502 | for @widgets; |
358 | |
503 | |
359 | use sort 'stable'; |
504 | use sort 'stable'; |
360 | |
505 | |
361 | $self->{children} = [ |
506 | $self->{children} = [ |
362 | sort { $a->{z} <=> $b->{z} } |
507 | sort { $a->{z} <=> $b->{z} } |
363 | @{$self->{children}}, $child |
508 | @{$self->{children}}, @widgets |
364 | ]; |
509 | ]; |
365 | |
510 | |
366 | $self->{w} = $self->{h} = -1; |
511 | $_->check_size |
|
|
512 | for @widgets; |
|
|
513 | |
367 | $self->update; |
514 | $self->update; |
368 | } |
515 | } |
369 | |
516 | |
|
|
517 | sub children { |
|
|
518 | @{ $_[0]{children} } |
|
|
519 | } |
|
|
520 | |
370 | sub remove { |
521 | sub remove { |
371 | my ($self, $widget) = @_; |
522 | my ($self, $child) = @_; |
372 | |
523 | |
|
|
524 | delete $child->{parent}; |
|
|
525 | $child->hide; |
|
|
526 | |
373 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
527 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
374 | |
528 | |
375 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
529 | $self->check_size; |
|
|
530 | $self->update; |
|
|
531 | } |
|
|
532 | |
|
|
533 | sub clear { |
|
|
534 | my ($self) = @_; |
|
|
535 | |
|
|
536 | my $children = delete $self->{children}; |
|
|
537 | $self->{children} = []; |
|
|
538 | |
|
|
539 | for (@$children) { |
|
|
540 | delete $_->{parent}; |
|
|
541 | $_->hide; |
|
|
542 | } |
|
|
543 | |
|
|
544 | $self->check_size; |
|
|
545 | $self->update; |
376 | } |
546 | } |
377 | |
547 | |
378 | sub find_widget { |
548 | sub find_widget { |
379 | my ($self, $x, $y) = @_; |
549 | my ($self, $x, $y) = @_; |
380 | |
550 | |
… | |
… | |
410 | |
580 | |
411 | $class->SUPER::new (children => [$child], %arg) |
581 | $class->SUPER::new (children => [$child], %arg) |
412 | } |
582 | } |
413 | |
583 | |
414 | sub add { |
584 | sub add { |
415 | my ($self, $widget) = @_; |
585 | my ($self, $child) = @_; |
416 | |
586 | |
417 | $self->{children} = []; |
587 | $self->{children} = []; |
418 | |
588 | |
419 | $self->SUPER::add ($widget); |
589 | $self->SUPER::add ($child); |
420 | } |
590 | } |
421 | |
591 | |
422 | sub remove { |
592 | sub remove { |
423 | my ($self, $widget) = @_; |
593 | my ($self, $widget) = @_; |
424 | |
594 | |
… | |
… | |
433 | sub size_request { |
603 | sub size_request { |
434 | $_[0]{children}[0]->size_request |
604 | $_[0]{children}[0]->size_request |
435 | } |
605 | } |
436 | |
606 | |
437 | sub size_allocate { |
607 | sub size_allocate { |
438 | my ($self, $x, $y, $w, $h) = @_; |
608 | my ($self, $w, $h) = @_; |
439 | |
609 | |
440 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
441 | |
|
|
442 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
610 | $self->{children}[0]->configure (0, 0, $w, $h); |
443 | } |
611 | } |
444 | |
612 | |
445 | ############################################################################# |
613 | ############################################################################# |
446 | |
614 | |
447 | package CFClient::UI::Window; |
615 | package CFClient::UI::Window; |
448 | |
616 | |
449 | our @ISA = CFClient::UI::Bin::; |
617 | our @ISA = CFClient::UI::Bin::; |
450 | |
618 | |
451 | use SDL::OpenGL; |
619 | use CFClient::OpenGL; |
452 | |
620 | |
453 | sub new { |
621 | sub new { |
454 | my ($class, %arg) = @_; |
622 | my ($class, %arg) = @_; |
455 | |
623 | |
456 | my $self = $class->SUPER::new (%arg); |
624 | my $self = $class->SUPER::new (%arg); |
457 | } |
625 | } |
458 | |
626 | |
459 | sub update { |
627 | sub update { |
460 | my ($self) = @_; |
628 | my ($self) = @_; |
461 | |
629 | |
462 | # we want to do this delayed... |
630 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
463 | $self->render_chld; |
|
|
464 | $self->SUPER::update; |
631 | $self->SUPER::update; |
465 | } |
632 | } |
466 | |
633 | |
|
|
634 | sub size_allocate { |
|
|
635 | my ($self, $w, $h) = @_; |
|
|
636 | |
|
|
637 | $self->SUPER::size_allocate ($w, $h); |
|
|
638 | $self->update; |
|
|
639 | } |
|
|
640 | |
|
|
641 | sub _render { |
|
|
642 | $_[0]{children}[0]->draw; |
|
|
643 | } |
|
|
644 | |
467 | sub render_chld { |
645 | sub render_child { |
468 | my ($self) = @_; |
646 | my ($self) = @_; |
469 | |
647 | |
470 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
648 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
471 | glClearColor 0, 0, 0, 1; |
649 | glClearColor 0, 0, 0, 0; |
472 | glClear GL_COLOR_BUFFER_BIT; |
650 | glClear GL_COLOR_BUFFER_BIT; |
473 | $self->child->draw; |
651 | |
|
|
652 | $self->_render; |
|
|
653 | # glColorMask 1, 1, 1, 0; |
|
|
654 | # glEnable GL_BLEND; |
|
|
655 | # glBlendFunc GL_SRC_ALPHA, GL_ZERO; |
|
|
656 | # glRasterPos 0, 0; |
|
|
657 | # glCopyPixels 0, 0, $self->{w}, $self->{h}; |
|
|
658 | # glDisable GL_BLEND; |
|
|
659 | # glColorMask 1, 1, 1, 1; |
474 | }; |
660 | }; |
475 | } |
|
|
476 | |
|
|
477 | sub size_allocate { |
|
|
478 | my ($self, $x, $y, $w, $h) = @_; |
|
|
479 | |
|
|
480 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
481 | |
|
|
482 | $self->child->size_allocate (0, 0, $w, $h); |
|
|
483 | |
|
|
484 | $self->render_chld; |
|
|
485 | } |
661 | } |
486 | |
662 | |
487 | sub _draw { |
663 | sub _draw { |
488 | my ($self) = @_; |
664 | my ($self) = @_; |
489 | |
665 | |
490 | my ($w, $h) = ($self->w, $self->h); |
666 | my ($w, $h) = ($self->w, $self->h); |
491 | |
667 | |
492 | my $tex = $self->{texture} |
668 | my $tex = $self->{texture} |
493 | or return; |
669 | or return; |
|
|
670 | |
|
|
671 | glEnable GL_BLEND; |
|
|
672 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
673 | glEnable GL_TEXTURE_2D; |
|
|
674 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
675 | glColor 0, 0, 0, 1; |
|
|
676 | |
|
|
677 | $tex->draw_quad (0, 0, $w, $h); |
|
|
678 | |
|
|
679 | glDisable GL_BLEND; |
|
|
680 | glDisable GL_TEXTURE_2D; |
|
|
681 | } |
|
|
682 | |
|
|
683 | ############################################################################# |
|
|
684 | |
|
|
685 | package CFClient::UI::ViewPort; |
|
|
686 | |
|
|
687 | our @ISA = CFClient::UI::Window::; |
|
|
688 | |
|
|
689 | sub size_request { |
|
|
690 | my ($self) = @_; |
|
|
691 | |
|
|
692 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
|
|
693 | $self->child->configure (0, 0, @$self{qw(child_w child_h)}); |
|
|
694 | |
|
|
695 | @$self{qw(child_w child_h)} |
|
|
696 | } |
|
|
697 | |
|
|
698 | sub size_allocate { |
|
|
699 | my ($self, $w, $h) = @_; |
|
|
700 | |
|
|
701 | $self->update; |
|
|
702 | } |
|
|
703 | |
|
|
704 | sub set_offset { |
|
|
705 | my ($self, $x, $y) = @_; |
|
|
706 | |
|
|
707 | $self->{view_x} = int $x; |
|
|
708 | $self->{view_y} = int $y; |
|
|
709 | |
|
|
710 | $self->update; |
|
|
711 | } |
|
|
712 | |
|
|
713 | # hmm, this does not work for topleft of $self... but we should not ask for that |
|
|
714 | sub coord2local { |
|
|
715 | my ($self, $x, $y) = @_; |
|
|
716 | |
|
|
717 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
718 | } |
|
|
719 | |
|
|
720 | sub coord2global { |
|
|
721 | my ($self, $x, $y) = @_; |
|
|
722 | |
|
|
723 | $x = List::Util::min $self->{w}, $x - $self->{view_x}; |
|
|
724 | $y = List::Util::min $self->{h}, $y - $self->{view_y}; |
|
|
725 | |
|
|
726 | $self->SUPER::coord2global ($x, $y) |
|
|
727 | } |
|
|
728 | |
|
|
729 | sub find_widget { |
|
|
730 | my ($self, $x, $y) = @_; |
|
|
731 | |
|
|
732 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
733 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
|
|
734 | ) { |
|
|
735 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
736 | } else { |
|
|
737 | $self->CFClient::UI::Base::find_widget ($x, $y) |
|
|
738 | } |
|
|
739 | } |
|
|
740 | |
|
|
741 | sub _render { |
|
|
742 | my ($self) = @_; |
|
|
743 | |
|
|
744 | CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
|
|
745 | |
|
|
746 | $self->SUPER::_render; |
|
|
747 | } |
|
|
748 | |
|
|
749 | ############################################################################# |
|
|
750 | |
|
|
751 | package CFClient::UI::ScrolledWindow; |
|
|
752 | |
|
|
753 | our @ISA = CFClient::UI::HBox::; |
|
|
754 | |
|
|
755 | sub new { |
|
|
756 | my $class = shift; |
|
|
757 | |
|
|
758 | my $self; |
|
|
759 | |
|
|
760 | my $slider = new CFClient::UI::Slider |
|
|
761 | vertical => 1, |
|
|
762 | range => [0, 0, 1, 0.01], # HACK fix |
|
|
763 | connect_changed => sub { |
|
|
764 | $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); |
|
|
765 | }, |
|
|
766 | ; |
|
|
767 | |
|
|
768 | $self = $class->SUPER::new ( |
|
|
769 | vp => (new CFClient::UI::ViewPort), |
|
|
770 | slider => $slider, |
|
|
771 | @_, |
|
|
772 | ); |
|
|
773 | |
|
|
774 | $self->{vp}->add ($self->{scrolled}); |
|
|
775 | $self->add ($self->{vp}); |
|
|
776 | $self->add ($self->{slider}); |
|
|
777 | |
|
|
778 | $self |
|
|
779 | } |
|
|
780 | |
|
|
781 | #TODO# update range on size_allocate depeneing on child |
|
|
782 | # update viewport offset on scroll |
|
|
783 | |
|
|
784 | ############################################################################# |
|
|
785 | |
|
|
786 | package CFClient::UI::Frame; |
|
|
787 | |
|
|
788 | our @ISA = CFClient::UI::Bin::; |
|
|
789 | |
|
|
790 | use CFClient::OpenGL; |
|
|
791 | |
|
|
792 | sub new { |
|
|
793 | my $class = shift; |
|
|
794 | |
|
|
795 | my $self = $class->SUPER::new ( |
|
|
796 | bg => [1, 1, 1, 1], |
|
|
797 | border_bg => [1, 1, 1, 1], |
|
|
798 | border => 0.8, |
|
|
799 | @_ |
|
|
800 | ); |
|
|
801 | |
|
|
802 | $self |
|
|
803 | } |
|
|
804 | |
|
|
805 | sub _draw { |
|
|
806 | my ($self) = @_; |
|
|
807 | |
|
|
808 | my ($w, $h) = ($self->{w}, $self->{h}); |
494 | |
809 | |
495 | glEnable GL_BLEND; |
810 | glEnable GL_BLEND; |
496 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
811 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
497 | glEnable GL_TEXTURE_2D; |
812 | glEnable GL_TEXTURE_2D; |
498 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
813 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
499 | |
814 | |
500 | $tex->draw_quad (0, 0, $w, $h); |
815 | # glBegin GL_QUADS; |
|
|
816 | # glColor 0, 0, 0, 0; |
|
|
817 | # glVertex 0 , 0; |
|
|
818 | # glVertex 0 , $h; |
|
|
819 | # glVertex $w, $h; |
|
|
820 | # glVertex $w, 0; |
|
|
821 | # glEnd; |
501 | |
822 | |
|
|
823 | |
|
|
824 | $self->child->draw; |
502 | glDisable GL_BLEND; |
825 | glDisable GL_BLEND; |
503 | glDisable GL_TEXTURE_2D; |
826 | glDisable GL_TEXTURE_2D; |
504 | } |
827 | } |
505 | |
828 | |
506 | ############################################################################# |
829 | ############################################################################# |
507 | |
830 | |
508 | package CFClient::UI::Frame; |
831 | package CFClient::UI::FancyFrame; |
509 | |
832 | |
510 | our @ISA = CFClient::UI::Bin::; |
833 | our @ISA = CFClient::UI::Bin::; |
511 | |
834 | |
512 | use SDL::OpenGL; |
835 | use CFClient::OpenGL; |
513 | |
|
|
514 | sub size_request { |
|
|
515 | my ($self) = @_; |
|
|
516 | my $chld = $self->child |
|
|
517 | or return (0, 0); |
|
|
518 | |
|
|
519 | $chld->move (2, 2); |
|
|
520 | |
|
|
521 | map { $_ + 4 } $chld->size_request; |
|
|
522 | } |
|
|
523 | |
|
|
524 | sub size_allocate { |
|
|
525 | my ($self, $x, $y, $w, $h) = @_; |
|
|
526 | |
|
|
527 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
528 | |
|
|
529 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
|
|
530 | } |
|
|
531 | |
|
|
532 | sub _draw { |
|
|
533 | my ($self) = @_; |
|
|
534 | |
|
|
535 | my $chld = $self->child; |
|
|
536 | |
|
|
537 | my ($w, $h) = $chld->size_request; |
|
|
538 | |
|
|
539 | glBegin GL_QUADS; |
|
|
540 | glColor 0, 0, 0; |
|
|
541 | glVertex 0 , 0; |
|
|
542 | glVertex 0 , $h + 4; |
|
|
543 | glVertex $w + 4 , $h + 4; |
|
|
544 | glVertex $w + 4 , 0; |
|
|
545 | glEnd; |
|
|
546 | |
|
|
547 | $chld->draw; |
|
|
548 | } |
|
|
549 | |
|
|
550 | ############################################################################# |
|
|
551 | |
|
|
552 | package CFClient::UI::FancyFrame; |
|
|
553 | |
|
|
554 | our @ISA = CFClient::UI::Bin::; |
|
|
555 | |
|
|
556 | use SDL::OpenGL; |
|
|
557 | |
836 | |
558 | my @tex = |
837 | my @tex = |
559 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
838 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
560 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
839 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
561 | |
840 | |
562 | sub new { |
841 | sub new { |
563 | my $class = shift; |
842 | my $class = shift; |
564 | |
843 | |
565 | # TODO: user_x, user_y, overwrite moveto? |
844 | # TODO: user_x, user_y, overwrite moveto? |
566 | |
845 | |
567 | $class->SUPER::new ( |
846 | my $self = $class->SUPER::new ( |
568 | bg => [1, 1, 1, 1], |
847 | bg => [1, 1, 1, 1], |
569 | border_bg => [1, 1, 1, 1], |
848 | border_bg => [1, 1, 1, 1], |
570 | border => $::FONTSIZE * 0.8, |
849 | border => 0.6, |
|
|
850 | can_events => 1, |
571 | @_ |
851 | @_ |
|
|
852 | ); |
|
|
853 | |
|
|
854 | $self->{title} &&= new CFClient::UI::Label |
|
|
855 | align => 0, |
|
|
856 | valign => 1, |
|
|
857 | text => $self->{title}, |
|
|
858 | fontsize => $self->{border}; |
|
|
859 | |
|
|
860 | $self |
|
|
861 | } |
|
|
862 | |
|
|
863 | sub border { |
|
|
864 | int $_[0]{border} * $::FONTSIZE |
|
|
865 | } |
|
|
866 | |
|
|
867 | sub size_request { |
|
|
868 | my ($self) = @_; |
|
|
869 | |
|
|
870 | my ($w, $h) = $self->SUPER::size_request; |
|
|
871 | |
|
|
872 | ( |
|
|
873 | $w + $self->border * 2, |
|
|
874 | $h + $self->border * 2, |
572 | ) |
875 | ) |
573 | } |
876 | } |
574 | |
877 | |
575 | sub size_request { |
|
|
576 | my ($self) = @_; |
|
|
577 | |
|
|
578 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
579 | |
|
|
580 | my ($w, $h) = $self->SUPER::size_request; |
|
|
581 | |
|
|
582 | ( |
|
|
583 | $w + $self->{border} * 2, |
|
|
584 | $h + $self->{border} * 2, |
|
|
585 | ) |
|
|
586 | } |
|
|
587 | |
|
|
588 | sub size_allocate { |
878 | sub size_allocate { |
589 | my ($self, $x, $y, $w, $h) = @_; |
879 | my ($self, $w, $h) = @_; |
590 | |
880 | |
591 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
592 | |
|
|
593 | $h -= List::Util::max 0, $self->{border} * 2; |
881 | $h -= List::Util::max 0, $self->border * 2; |
594 | $w -= List::Util::max 0, $self->{border} * 2; |
882 | $w -= List::Util::max 0, $self->border * 2; |
595 | |
883 | |
|
|
884 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
885 | if $self->{title}; |
|
|
886 | |
596 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
887 | $self->child->configure ($self->border, $self->border, $w, $h); |
597 | } |
888 | } |
598 | |
889 | |
599 | sub button_down { |
890 | sub button_down { |
600 | my ($self, $ev, $x, $y) = @_; |
891 | my ($self, $ev, $x, $y) = @_; |
601 | |
892 | |
602 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
893 | my ($w, $h) = @$self{qw(w h)}; |
603 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
894 | my $border = $self->border; |
604 | |
895 | |
|
|
896 | my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); |
|
|
897 | my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); |
|
|
898 | |
|
|
899 | if ($lr & $td) { |
|
|
900 | my ($wx, $wy) = ($self->{x}, $self->{y}); |
605 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
901 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
606 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
902 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
903 | |
|
|
904 | my $mx = $x < $border; |
|
|
905 | my $my = $y < $border; |
607 | |
906 | |
608 | $self->{motion} = sub { |
907 | $self->{motion} = sub { |
609 | my ($ev, $x, $y) = @_; |
908 | my ($ev, $x, $y) = @_; |
610 | |
909 | |
611 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
910 | my $dx = $ev->{x} - $ox; |
|
|
911 | my $dy = $ev->{y} - $oy; |
612 | |
912 | |
613 | $self->{user_w} = $bw + $x - $ox; |
913 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
614 | $self->{user_h} = $bh + $y - $oy; |
914 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
615 | $self->update; |
915 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
|
|
916 | $self->check_size; |
616 | }; |
917 | }; |
617 | |
918 | |
618 | } elsif ($x >= 0 && $x < $self->{w} |
919 | } elsif ($lr ^ $td) { |
619 | && $y >= 0 && $y < $self->{border}) { |
|
|
620 | |
|
|
621 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
920 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
622 | my ($bx, $by) = ($self->{x}, $self->{y}); |
921 | my ($bx, $by) = ($self->{x}, $self->{y}); |
623 | |
922 | |
624 | $self->{motion} = sub { |
923 | $self->{motion} = sub { |
625 | my ($ev, $x, $y) = @_; |
924 | my ($ev, $x, $y) = @_; |
626 | |
925 | |
627 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
926 | ($x, $y) = ($ev->{x}, $ev->{y}); |
628 | |
927 | |
629 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
928 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
630 | $self->update; |
929 | $self->update; |
631 | }; |
930 | }; |
632 | } |
931 | } |
… | |
… | |
653 | glEnable GL_BLEND; |
952 | glEnable GL_BLEND; |
654 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
953 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
655 | glEnable GL_TEXTURE_2D; |
954 | glEnable GL_TEXTURE_2D; |
656 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
955 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
657 | |
956 | |
|
|
957 | my $border = $self->border; |
|
|
958 | |
658 | glColor @{ $self->{border_bg} }; |
959 | glColor @{ $self->{border_bg} }; |
659 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
960 | $tex[1]->draw_quad (0, 0, $w, $border); |
660 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
961 | $tex[3]->draw_quad (0, $border, $border, $ch); |
661 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
962 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
662 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
963 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
663 | |
964 | |
|
|
965 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
664 | my $bg = $tex[0]; |
966 | my $bg = $tex[0]; |
665 | |
967 | |
666 | # TODO: repeat texture not scale |
968 | # TODO: repeat texture not scale |
667 | my $rep_x = $cw / $bg->{w}; |
969 | my $rep_x = $cw / $bg->{w}; |
668 | my $rep_y = $ch / $bg->{h}; |
970 | my $rep_y = $ch / $bg->{h}; |
669 | |
971 | |
670 | glColor @{ $self->{bg} }; |
972 | glColor @{ $self->{bg} }; |
671 | |
973 | |
672 | $bg->{s} = $rep_x; |
974 | $bg->{s} = $rep_x; |
673 | $bg->{t} = $rep_y; |
975 | $bg->{t} = $rep_y; |
674 | $bg->{wrap_mode} = 1; |
976 | $bg->{wrap_mode} = 1; |
675 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
977 | $bg->draw_quad ($border, $border, $cw, $ch); |
676 | |
978 | |
677 | glDisable GL_TEXTURE_2D; |
979 | glDisable GL_TEXTURE_2D; |
678 | glDisable GL_BLEND; |
980 | glDisable GL_BLEND; |
|
|
981 | } |
|
|
982 | |
|
|
983 | $self->{title}->draw if $self->{title}; |
679 | |
984 | |
680 | $self->child->draw; |
985 | $self->child->draw; |
681 | } |
986 | } |
682 | |
987 | |
683 | ############################################################################# |
988 | ############################################################################# |
… | |
… | |
686 | |
991 | |
687 | our @ISA = CFClient::UI::Base::; |
992 | our @ISA = CFClient::UI::Base::; |
688 | |
993 | |
689 | use List::Util qw(max sum); |
994 | use List::Util qw(max sum); |
690 | |
995 | |
691 | use SDL::OpenGL; |
996 | use CFClient::OpenGL; |
692 | |
997 | |
693 | sub new { |
998 | sub new { |
694 | my $class = shift; |
999 | my $class = shift; |
695 | |
1000 | |
696 | $class->SUPER::new ( |
1001 | $class->SUPER::new ( |
… | |
… | |
703 | my ($self, $x, $y, $child) = @_; |
1008 | my ($self, $x, $y, $child) = @_; |
704 | |
1009 | |
705 | $child->set_parent ($self); |
1010 | $child->set_parent ($self); |
706 | $self->{children}[$y][$x] = $child; |
1011 | $self->{children}[$y][$x] = $child; |
707 | |
1012 | |
708 | $self->{w} = $self->{h} = -1; |
1013 | $child->check_size; |
709 | $self->update; |
1014 | } |
|
|
1015 | |
|
|
1016 | sub children { |
|
|
1017 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
710 | } |
1018 | } |
711 | |
1019 | |
712 | # TODO: move to container class maybe? send childs a signal on removal? |
1020 | # TODO: move to container class maybe? send childs a signal on removal? |
713 | sub clear { |
1021 | sub clear { |
714 | my ($self) = @_; |
1022 | my ($self) = @_; |
715 | |
1023 | |
|
|
1024 | my @children = $self->children; |
716 | delete $self->{children}; |
1025 | delete $self->{children}; |
|
|
1026 | |
|
|
1027 | for (@children) { |
|
|
1028 | delete $_->{parent}; |
|
|
1029 | $_->hide; |
|
|
1030 | } |
|
|
1031 | |
717 | $self->update; |
1032 | $self->update; |
718 | } |
1033 | } |
719 | |
1034 | |
720 | sub get_wh { |
1035 | sub get_wh { |
721 | my ($self) = @_; |
1036 | my ($self) = @_; |
… | |
… | |
727 | or next; |
1042 | or next; |
728 | |
1043 | |
729 | for my $x (0 .. $#$row) { |
1044 | for my $x (0 .. $#$row) { |
730 | my $widget = $row->[$x] |
1045 | my $widget = $row->[$x] |
731 | or next; |
1046 | or next; |
732 | my ($w, $h) = $widget->size_request; |
1047 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
733 | |
1048 | |
734 | $w[$x] = max $w[$x], $w; |
1049 | $w[$x] = max $w[$x], $w; |
735 | $h[$y] = max $h[$y], $h; |
1050 | $h[$y] = max $h[$y], $h; |
736 | } |
1051 | } |
737 | } |
1052 | } |
… | |
… | |
749 | (sum @$hs), |
1064 | (sum @$hs), |
750 | ) |
1065 | ) |
751 | } |
1066 | } |
752 | |
1067 | |
753 | sub size_allocate { |
1068 | sub size_allocate { |
754 | my ($self, $x, $y, $w, $h) = @_; |
1069 | my ($self, $w, $h) = @_; |
755 | |
|
|
756 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
757 | |
1070 | |
758 | my ($ws, $hs) = $self->get_wh; |
1071 | my ($ws, $hs) = $self->get_wh; |
759 | |
1072 | |
760 | my $req_w = sum @$ws; |
1073 | my $req_w = sum @$ws; |
761 | my $req_h = sum @$hs; |
1074 | my $req_h = sum @$hs; |
… | |
… | |
783 | |
1096 | |
784 | for my $c (0 .. $#$row) { |
1097 | for my $c (0 .. $#$row) { |
785 | my $col_w = $ws->[$c]; |
1098 | my $col_w = $ws->[$c]; |
786 | |
1099 | |
787 | if (my $widget = $row->[$c]) { |
1100 | if (my $widget = $row->[$c]) { |
788 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
1101 | $widget->configure ($x, $y, $col_w, $row_h); |
789 | } |
1102 | } |
790 | |
1103 | |
791 | $x += $col_w; |
1104 | $x += $col_w; |
792 | } |
1105 | } |
793 | |
1106 | |
… | |
… | |
838 | (List::Util::max map $_->[1], @alloc), |
1151 | (List::Util::max map $_->[1], @alloc), |
839 | ) |
1152 | ) |
840 | } |
1153 | } |
841 | |
1154 | |
842 | sub size_allocate { |
1155 | sub size_allocate { |
843 | my ($self, $x, $y, $w, $h) = @_; |
1156 | my ($self, $w, $h) = @_; |
844 | |
|
|
845 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
846 | |
1157 | |
847 | ($h, $w) = ($w, $h); |
1158 | ($h, $w) = ($w, $h); |
848 | |
1159 | |
849 | my $children = $self->{children}; |
1160 | my $children = $self->{children}; |
850 | |
1161 | |
851 | my @h = map +($_->size_request)[0], @$children; |
1162 | my @h = map $_->{req_w}, @$children; |
852 | |
1163 | |
853 | my $req_h = List::Util::sum @h; |
1164 | my $req_h = List::Util::sum @h; |
854 | |
1165 | |
855 | if ($req_h > $h) { |
1166 | if ($req_h > $h) { |
856 | # ah well, not enough space |
1167 | # ah well, not enough space |
… | |
… | |
872 | |
1183 | |
873 | my $y = 0; |
1184 | my $y = 0; |
874 | for (0 .. $#$children) { |
1185 | for (0 .. $#$children) { |
875 | my $child = $children->[$_]; |
1186 | my $child = $children->[$_]; |
876 | my $h = $h[$_]; |
1187 | my $h = $h[$_]; |
877 | $child->size_allocate ($y, 0, $h, $w); |
1188 | $child->configure ($y, 0, $h, $w); |
878 | |
1189 | |
879 | $y += $h; |
1190 | $y += $h; |
880 | } |
1191 | } |
|
|
1192 | |
|
|
1193 | 1 |
881 | } |
1194 | } |
882 | |
1195 | |
883 | ############################################################################# |
1196 | ############################################################################# |
884 | |
1197 | |
885 | package CFClient::UI::VBox; |
1198 | package CFClient::UI::VBox; |
… | |
… | |
898 | (List::Util::sum map $_->[1], @alloc), |
1211 | (List::Util::sum map $_->[1], @alloc), |
899 | ) |
1212 | ) |
900 | } |
1213 | } |
901 | |
1214 | |
902 | sub size_allocate { |
1215 | sub size_allocate { |
903 | my ($self, $x, $y, $w, $h) = @_; |
1216 | my ($self, $w, $h) = @_; |
904 | |
|
|
905 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
906 | |
1217 | |
907 | my $children = $self->{children}; |
1218 | my $children = $self->{children}; |
908 | |
1219 | |
909 | my @h = map +($_->size_request)[1], @$children; |
1220 | my @h = map $_->{req_h}, @$children; |
910 | |
1221 | |
911 | my $req_h = List::Util::sum @h; |
1222 | my $req_h = List::Util::sum @h; |
912 | |
1223 | |
913 | if ($req_h > $h) { |
1224 | if ($req_h > $h) { |
914 | # ah well, not enough space |
1225 | # ah well, not enough space |
… | |
… | |
928 | |
1239 | |
929 | my $y = 0; |
1240 | my $y = 0; |
930 | for (0 .. $#$children) { |
1241 | for (0 .. $#$children) { |
931 | my $child = $children->[$_]; |
1242 | my $child = $children->[$_]; |
932 | my $h = $h[$_]; |
1243 | my $h = $h[$_]; |
933 | $child->size_allocate (0, $y, $w, $h); |
1244 | $child->configure (0, $y, $w, $h); |
934 | |
1245 | |
935 | $y += $h; |
1246 | $y += $h; |
936 | } |
1247 | } |
|
|
1248 | |
|
|
1249 | 1 |
937 | } |
1250 | } |
938 | |
1251 | |
939 | ############################################################################# |
1252 | ############################################################################# |
940 | |
1253 | |
941 | package CFClient::UI::Label; |
1254 | package CFClient::UI::Label; |
942 | |
1255 | |
943 | our @ISA = CFClient::UI::Base::; |
1256 | our @ISA = CFClient::UI::Base::; |
944 | |
1257 | |
945 | use SDL::OpenGL; |
1258 | use CFClient::OpenGL; |
946 | |
1259 | |
947 | sub new { |
1260 | sub new { |
948 | my ($class, %arg) = @_; |
1261 | my ($class, %arg) = @_; |
949 | |
1262 | |
950 | my $self = $class->SUPER::new ( |
1263 | my $self = $class->SUPER::new ( |
951 | fg => [1, 1, 1], |
1264 | fg => [1, 1, 1], |
952 | fontsize => $::FONTSIZE, |
1265 | #font => default_font |
|
|
1266 | fontsize => 1, |
953 | text => "", |
1267 | text => "", |
954 | align => -1, |
1268 | align => -1, |
|
|
1269 | valign => -1, |
955 | padding => 2, |
1270 | padding => 2, |
956 | layout => new CFClient::Layout, |
1271 | layout => new CFClient::Layout, |
|
|
1272 | can_events => 0, |
957 | %arg |
1273 | %arg |
958 | ); |
1274 | ); |
959 | |
1275 | |
960 | $self->set_text ($self->{text}); |
1276 | if (exists $self->{template}) { |
|
|
1277 | my $layout = new CFClient::Layout; |
|
|
1278 | $layout->set_text (delete $self->{template}); |
|
|
1279 | $self->{template} = $layout; |
|
|
1280 | } |
|
|
1281 | |
|
|
1282 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
|
|
1283 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
961 | |
1284 | |
962 | $self |
1285 | $self |
963 | } |
1286 | } |
964 | |
1287 | |
965 | sub escape_text { |
1288 | sub escape { |
966 | local $_ = $_[1]; |
1289 | local $_ = $_[1]; |
967 | |
1290 | |
968 | s/&/&/g; |
1291 | s/&/&/g; |
969 | s/>/>/g; |
1292 | s/>/>/g; |
970 | s/</</g; |
1293 | s/</</g; |
971 | |
1294 | |
972 | $_[1] |
1295 | $_[1] |
973 | } |
1296 | } |
974 | |
1297 | |
|
|
1298 | sub update { |
|
|
1299 | my ($self) = @_; |
|
|
1300 | |
|
|
1301 | delete $self->{texture}; |
|
|
1302 | $self->SUPER::update; |
|
|
1303 | } |
|
|
1304 | |
975 | sub set_text { |
1305 | sub set_text { |
976 | my ($self, $text) = @_; |
1306 | my ($self, $text) = @_; |
977 | |
1307 | |
|
|
1308 | return if $self->{text} eq "T$text"; |
978 | $self->{text} = $text; |
1309 | $self->{text} = "T$text"; |
|
|
1310 | |
979 | $self->{layout}->set_markup ($text); |
1311 | $self->{layout}->set_text ($text); |
980 | |
1312 | |
981 | delete $self->{texture}; |
|
|
982 | # $self->{w} = $self->{h} = -1; |
|
|
983 | $self->update; |
1313 | $self->update; |
|
|
1314 | $self->check_size; |
984 | } |
1315 | } |
985 | |
1316 | |
986 | sub get_text { |
1317 | sub set_markup { |
987 | my ($self, $text) = @_; |
1318 | my ($self, $markup) = @_; |
988 | |
1319 | |
989 | $self->{text} |
1320 | return if $self->{text} eq "M$markup"; |
|
|
1321 | $self->{text} = "M$markup"; |
|
|
1322 | |
|
|
1323 | $self->{layout}->set_markup ($markup); |
|
|
1324 | |
|
|
1325 | $self->update; |
|
|
1326 | $self->check_size; |
990 | } |
1327 | } |
991 | |
1328 | |
992 | sub size_request { |
1329 | sub size_request { |
993 | my ($self) = @_; |
1330 | my ($self) = @_; |
994 | |
1331 | |
995 | $self->{layout}->set_width; |
1332 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1333 | $self->{layout}->set_width ($self->{max_w} || -1); |
996 | $self->{layout}->set_height ($self->{fontsize}); |
1334 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1335 | |
997 | my ($w, $h) = $self->{layout}->size; |
1336 | my ($w, $h) = $self->{layout}->size; |
|
|
1337 | |
|
|
1338 | if (exists $self->{template}) { |
|
|
1339 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1340 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1341 | |
|
|
1342 | my ($w2, $h2) = $self->{template}->size; |
|
|
1343 | |
|
|
1344 | $w = List::Util::max $w, $w2; |
|
|
1345 | $h = List::Util::max $h, $h2; |
|
|
1346 | } |
998 | |
1347 | |
999 | ( |
1348 | ( |
1000 | $w + $self->{padding} * 2, |
1349 | $w + $self->{padding} * 2, |
1001 | $h + $self->{padding} * 2, |
1350 | $h + $self->{padding} * 2, |
1002 | ) |
1351 | ) |
1003 | } |
1352 | } |
1004 | |
1353 | |
1005 | sub size_allocate { |
1354 | sub size_allocate { |
1006 | my ($self, $x, $y, $w, $h) = @_; |
1355 | my ($self, $w, $h) = @_; |
1007 | |
|
|
1008 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
1009 | |
1356 | |
1010 | delete $self->{texture}; |
1357 | delete $self->{texture}; |
1011 | } |
1358 | } |
1012 | |
1359 | |
1013 | sub update { |
1360 | sub set_fontsize { |
1014 | my ($self) = @_; |
1361 | my ($self, $fontsize) = @_; |
1015 | |
1362 | |
|
|
1363 | $self->{fontsize} = $fontsize; |
1016 | delete $self->{texture}; |
1364 | delete $self->{texture}; |
|
|
1365 | |
1017 | $self->SUPER::update; |
1366 | $self->update; |
|
|
1367 | $self->check_size; |
1018 | } |
1368 | } |
1019 | |
1369 | |
1020 | sub _draw { |
1370 | sub _draw { |
1021 | my ($self) = @_; |
1371 | my ($self) = @_; |
1022 | |
1372 | |
1023 | my $tex = $self->{texture} ||= do { |
1373 | my $tex = $self->{texture} ||= do { |
|
|
1374 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1024 | $self->{layout}->set_width ($self->{w}); |
1375 | $self->{layout}->set_width ($self->{w}); |
1025 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
1376 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1026 | new_from_layout CFClient::Texture $self->{layout} |
1377 | new_from_layout CFClient::Texture $self->{layout} |
1027 | }; |
1378 | }; |
1028 | |
1379 | |
1029 | glEnable GL_BLEND; |
1380 | glEnable GL_BLEND; |
1030 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1381 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1031 | glEnable GL_TEXTURE_2D; |
1382 | glEnable GL_TEXTURE_2D; |
1032 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1383 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1033 | |
1384 | |
1034 | glColor @{$self->{fg}}; |
1385 | glColor @{$self->{fg}}; |
1035 | |
1386 | |
1036 | my $x = |
1387 | $self->{ox} = int ( |
1037 | $self->{align} < 0 ? $self->{padding} |
1388 | $self->{align} < 0 ? $self->{padding} |
1038 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1389 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1039 | : ($self->{w} - $tex->{w}) * 0.5; |
1390 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1391 | ); |
1040 | |
1392 | |
1041 | $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); |
1393 | $self->{oy} = int ( |
|
|
1394 | $self->{valign} < 0 ? $self->{padding} |
|
|
1395 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1396 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1397 | ); |
|
|
1398 | |
|
|
1399 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1042 | |
1400 | |
1043 | glDisable GL_TEXTURE_2D; |
1401 | glDisable GL_TEXTURE_2D; |
1044 | glDisable GL_BLEND; |
1402 | glDisable GL_BLEND; |
1045 | } |
1403 | } |
1046 | |
1404 | |
1047 | ############################################################################# |
1405 | ############################################################################# |
1048 | |
1406 | |
1049 | package CFClient::UI::Entry; |
1407 | package CFClient::UI::EntryBase; |
1050 | |
1408 | |
1051 | our @ISA = CFClient::UI::Label::; |
1409 | our @ISA = CFClient::UI::Label::; |
1052 | |
1410 | |
1053 | use SDL; |
1411 | use CFClient::OpenGL; |
1054 | use SDL::OpenGL; |
|
|
1055 | |
1412 | |
1056 | sub new { |
1413 | sub new { |
1057 | my $class = shift; |
1414 | my $class = shift; |
1058 | |
1415 | |
1059 | $class->SUPER::new ( |
1416 | $class->SUPER::new ( |
1060 | fg => [1, 1, 1], |
1417 | fg => [1, 1, 1], |
1061 | bg => [0, 0, 0, 0.2], |
1418 | bg => [0, 0, 0, 0.2], |
1062 | active_bg => [1, 1, 1, 0.5], |
1419 | active_bg => [1, 1, 1, 0.5], |
1063 | active_fg => [0, 0, 0], |
1420 | active_fg => [0, 0, 0], |
1064 | can_hover => 1, |
1421 | can_hover => 1, |
1065 | can_focus => 1, |
1422 | can_focus => 1, |
|
|
1423 | valign => 0, |
|
|
1424 | can_events => 1, |
1066 | @_ |
1425 | @_ |
1067 | ) |
1426 | ) |
1068 | } |
1427 | } |
1069 | |
1428 | |
1070 | sub _set_text { |
1429 | sub _set_text { |
1071 | my ($self, $text) = @_; |
1430 | my ($self, $text) = @_; |
1072 | |
1431 | |
1073 | my $old_text = $self->{text}; |
1432 | delete $self->{cur_h}; |
|
|
1433 | |
|
|
1434 | return if $self->{text} eq $text; |
|
|
1435 | |
|
|
1436 | delete $self->{texture}; |
1074 | |
1437 | |
1075 | $self->{last_activity} = $::NOW; |
1438 | $self->{last_activity} = $::NOW; |
1076 | |
|
|
1077 | $self->{text} = $text; |
1439 | $self->{text} = $text; |
1078 | $self->{layout}->set_width ($self->{w}); |
|
|
1079 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
|
|
1080 | |
1440 | |
1081 | $text =~ s/./*/g if $self->{hidden}; |
1441 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1442 | $self->{layout}->set_text ("$text "); |
1082 | |
1443 | |
1083 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1444 | $self->emit (changed => $self->{text}); |
|
|
1445 | } |
1084 | |
1446 | |
1085 | $text = substr $text, 0, $self->{cursor}; |
1447 | sub get_text { |
1086 | utf8::encode $text; |
1448 | $_[0]{text} |
1087 | |
|
|
1088 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1089 | |
|
|
1090 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1091 | if $old_text ne $self->{text}; |
|
|
1092 | } |
1449 | } |
1093 | |
1450 | |
1094 | sub size_request { |
1451 | sub size_request { |
1095 | my ($self) = @_; |
1452 | my ($self) = @_; |
1096 | |
1453 | |
… | |
… | |
1098 | |
1455 | |
1099 | ($w + 1, $h) # add 1 for cursor |
1456 | ($w + 1, $h) # add 1 for cursor |
1100 | } |
1457 | } |
1101 | |
1458 | |
1102 | sub size_allocate { |
1459 | sub size_allocate { |
1103 | my ($self, $x, $y, $w, $h) = @_; |
1460 | my ($self, $w, $h) = @_; |
1104 | |
1461 | |
1105 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1462 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1106 | |
|
|
1107 | $self->_set_text ($self->{text}); |
|
|
1108 | } |
1463 | } |
1109 | |
1464 | |
1110 | sub set_text { |
1465 | sub set_text { |
1111 | my ($self, $text) = @_; |
1466 | my ($self, $text) = @_; |
1112 | |
1467 | |
… | |
… | |
1116 | } |
1471 | } |
1117 | |
1472 | |
1118 | sub key_down { |
1473 | sub key_down { |
1119 | my ($self, $ev) = @_; |
1474 | my ($self, $ev) = @_; |
1120 | |
1475 | |
1121 | my $mod = $ev->key_mod; |
1476 | my $mod = $ev->{mod}; |
1122 | my $sym = $ev->key_sym; |
1477 | my $sym = $ev->{sym}; |
1123 | |
|
|
1124 | my $uni = $ev->key_unicode; |
1478 | my $uni = $ev->{unicode}; |
1125 | |
1479 | |
1126 | my $text = $self->get_text; |
1480 | my $text = $self->get_text; |
1127 | |
1481 | |
1128 | if ($sym == SDLK_BACKSPACE) { |
1482 | if ($sym == 8) { |
1129 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1483 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1130 | } elsif ($sym == SDLK_DELETE) { |
1484 | } elsif ($sym == 127) { |
1131 | substr $text, $self->{cursor}, 1, ""; |
1485 | substr $text, $self->{cursor}, 1, ""; |
1132 | } elsif ($sym == SDLK_LEFT) { |
1486 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1133 | --$self->{cursor} if $self->{cursor}; |
1487 | --$self->{cursor} if $self->{cursor}; |
1134 | } elsif ($sym == SDLK_RIGHT) { |
1488 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1135 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1489 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1136 | } elsif ($sym == SDLK_HOME) { |
1490 | } elsif ($sym == CFClient::SDLK_HOME) { |
1137 | $self->{cursor} = 0; |
1491 | $self->{cursor} = 0; |
1138 | } elsif ($sym == SDLK_END) { |
1492 | } elsif ($sym == CFClient::SDLK_END) { |
1139 | $self->{cursor} = length $text; |
1493 | $self->{cursor} = length $text; |
1140 | } elsif ($sym == SDLK_ESCAPE) { |
1494 | } elsif ($sym == 27) { |
1141 | $self->emit ('escape'); |
1495 | $self->emit ('escape'); |
1142 | } elsif ($uni) { |
1496 | } elsif ($uni) { |
1143 | substr $text, $self->{cursor}++, 0, chr $uni; |
1497 | substr $text, $self->{cursor}++, 0, chr $uni; |
1144 | } |
1498 | } |
1145 | |
1499 | |
… | |
… | |
1200 | |
1554 | |
1201 | $self->SUPER::_draw; |
1555 | $self->SUPER::_draw; |
1202 | |
1556 | |
1203 | #TODO: force update every cursor change :( |
1557 | #TODO: force update every cursor change :( |
1204 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1558 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1559 | |
|
|
1560 | unless (exists $self->{cur_h}) { |
|
|
1561 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1562 | utf8::encode $text; |
|
|
1563 | |
|
|
1564 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1565 | } |
|
|
1566 | |
1205 | glColor @{$self->{fg}}; |
1567 | glColor @{$self->{fg}}; |
1206 | glBegin GL_LINES; |
1568 | glBegin GL_LINES; |
1207 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1208 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1569 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1570 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1209 | glEnd; |
1571 | glEnd; |
1210 | } |
1572 | } |
1211 | } |
1573 | } |
1212 | |
1574 | |
1213 | package CFClient::UI::LineEntry; |
1575 | package CFClient::UI::Entry; |
1214 | |
1576 | |
1215 | our @ISA = CFClient::UI::Entry::; |
1577 | our @ISA = CFClient::UI::EntryBase::; |
1216 | |
1578 | |
1217 | use SDL; |
1579 | use CFClient::OpenGL; |
1218 | use SDL::OpenGL; |
|
|
1219 | |
1580 | |
1220 | sub key_down { |
1581 | sub key_down { |
1221 | my ($self, $ev) = @_; |
1582 | my ($self, $ev) = @_; |
1222 | |
1583 | |
1223 | my $sym = $ev->key_sym; |
1584 | my $sym = $ev->{sym}; |
1224 | |
1585 | |
1225 | if ($sym == SDLK_RETURN) { |
1586 | if ($sym == 13) { |
|
|
1587 | unshift @{$self->{history}}, |
|
|
1588 | my $txt = $self->get_text; |
|
|
1589 | $self->{history_pointer} = -1; |
|
|
1590 | $self->{history_saveback} = ''; |
1226 | $self->emit (activate => $self->get_text); |
1591 | $self->emit (activate => $txt); |
1227 | $self->update; |
1592 | $self->update; |
|
|
1593 | |
|
|
1594 | } elsif ($sym == CFClient::SDLK_UP) { |
|
|
1595 | if ($self->{history_pointer} < 0) { |
|
|
1596 | $self->{history_saveback} = $self->get_text; |
|
|
1597 | } |
|
|
1598 | if (@{$self->{history} || []} > 0) { |
|
|
1599 | $self->{history_pointer}++; |
|
|
1600 | if ($self->{history_pointer} >= @{$self->{history} || []}) { |
|
|
1601 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
|
|
1602 | } |
|
|
1603 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
|
|
1604 | } |
|
|
1605 | |
|
|
1606 | } elsif ($sym == CFClient::SDLK_DOWN) { |
|
|
1607 | $self->{history_pointer}--; |
|
|
1608 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
|
|
1609 | |
|
|
1610 | if ($self->{history_pointer} >= 0) { |
|
|
1611 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
|
|
1612 | } else { |
|
|
1613 | $self->set_text ($self->{history_saveback}); |
|
|
1614 | } |
1228 | |
1615 | |
1229 | } else { |
1616 | } else { |
1230 | $self->SUPER::key_down ($ev); |
1617 | $self->SUPER::key_down ($ev); |
1231 | } |
1618 | } |
1232 | |
1619 | |
… | |
… | |
1236 | |
1623 | |
1237 | package CFClient::UI::Button; |
1624 | package CFClient::UI::Button; |
1238 | |
1625 | |
1239 | our @ISA = CFClient::UI::Label::; |
1626 | our @ISA = CFClient::UI::Label::; |
1240 | |
1627 | |
1241 | use SDL; |
1628 | use CFClient::OpenGL; |
1242 | use SDL::OpenGL; |
|
|
1243 | |
1629 | |
1244 | my @tex = |
1630 | my @tex = |
1245 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1631 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1246 | qw(b1_button_active.png); |
1632 | qw(b1_button_active.png); |
1247 | |
1633 | |
1248 | sub new { |
1634 | sub new { |
1249 | my $class = shift; |
1635 | my $class = shift; |
1250 | |
1636 | |
1251 | $class->SUPER::new ( |
1637 | $class->SUPER::new ( |
1252 | padding => 4, |
1638 | padding => 4, |
1253 | fg => [1, 1, 1], |
1639 | fg => [1, 1, 1], |
1254 | bg => [1, 1, 1, 0.2], |
1640 | bg => [1, 1, 1, 0.2], |
1255 | active_fg => [1, 1, 0], |
1641 | active_fg => [0, 0, 1], |
1256 | can_hover => 1, |
1642 | can_hover => 1, |
|
|
1643 | align => 0, |
|
|
1644 | valign => 0, |
|
|
1645 | can_events => 1, |
1257 | @_ |
1646 | @_ |
1258 | ) |
1647 | ) |
1259 | } |
1648 | } |
1260 | |
1649 | |
1261 | sub button_up { |
1650 | sub button_up { |
… | |
… | |
1269 | |
1658 | |
1270 | sub _draw { |
1659 | sub _draw { |
1271 | my ($self) = @_; |
1660 | my ($self) = @_; |
1272 | |
1661 | |
1273 | local $self->{fg} = $self->{fg}; |
1662 | local $self->{fg} = $self->{fg}; |
1274 | my $tex = $tex[0]; |
|
|
1275 | |
|
|
1276 | glEnable GL_BLEND; |
|
|
1277 | glEnable GL_TEXTURE_2D; |
|
|
1278 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1279 | |
1663 | |
1280 | if ($GRAB == $self) { |
1664 | if ($GRAB == $self) { |
1281 | $self->{fg} = $self->{active_fg}; |
1665 | $self->{fg} = $self->{active_fg}; |
1282 | } |
1666 | } |
1283 | |
1667 | |
1284 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1668 | glEnable GL_BLEND; |
|
|
1669 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1670 | glEnable GL_TEXTURE_2D; |
1285 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1671 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1672 | glColor 0, 0, 0, 1; |
1286 | |
1673 | |
1287 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1674 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1288 | |
1675 | |
1289 | glDisable GL_TEXTURE_2D; |
1676 | glDisable GL_TEXTURE_2D; |
1290 | glDisable GL_BLEND; |
1677 | glDisable GL_BLEND; |
1291 | |
1678 | |
1292 | $self->SUPER::_draw; |
1679 | $self->SUPER::_draw; |
… | |
… | |
1297 | package CFClient::UI::CheckBox; |
1684 | package CFClient::UI::CheckBox; |
1298 | |
1685 | |
1299 | our @ISA = CFClient::UI::DrawBG::; |
1686 | our @ISA = CFClient::UI::DrawBG::; |
1300 | |
1687 | |
1301 | my @tex = |
1688 | my @tex = |
1302 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1689 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1303 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1690 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1304 | |
1691 | |
1305 | use SDL; |
1692 | use CFClient::OpenGL; |
1306 | use SDL::OpenGL; |
|
|
1307 | |
1693 | |
1308 | sub new { |
1694 | sub new { |
1309 | my $class = shift; |
1695 | my $class = shift; |
1310 | |
1696 | |
1311 | $class->SUPER::new ( |
1697 | $class->SUPER::new ( |
… | |
… | |
1322 | my ($self) = @_; |
1708 | my ($self) = @_; |
1323 | |
1709 | |
1324 | ($self->{padding} * 2 + 6) x 2 |
1710 | ($self->{padding} * 2 + 6) x 2 |
1325 | } |
1711 | } |
1326 | |
1712 | |
1327 | sub size_allocate { |
|
|
1328 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1329 | |
|
|
1330 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1331 | } |
|
|
1332 | |
|
|
1333 | sub button_down { |
1713 | sub button_down { |
1334 | my ($self, $ev, $x, $y) = @_; |
1714 | my ($self, $ev, $x, $y) = @_; |
1335 | |
1715 | |
1336 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1716 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1337 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1717 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1363 | glDisable GL_BLEND; |
1743 | glDisable GL_BLEND; |
1364 | } |
1744 | } |
1365 | |
1745 | |
1366 | ############################################################################# |
1746 | ############################################################################# |
1367 | |
1747 | |
|
|
1748 | package CFClient::UI::Image; |
|
|
1749 | |
|
|
1750 | our @ISA = CFClient::UI::Base::; |
|
|
1751 | |
|
|
1752 | use CFClient::OpenGL; |
|
|
1753 | use Carp qw/confess/; |
|
|
1754 | |
|
|
1755 | our %loaded_images; |
|
|
1756 | |
|
|
1757 | sub new { |
|
|
1758 | my $class = shift; |
|
|
1759 | |
|
|
1760 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1761 | |
|
|
1762 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1763 | |
|
|
1764 | $loaded_images{$self->{image}} ||= |
|
|
1765 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1766 | |
|
|
1767 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1768 | |
|
|
1769 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1770 | |
|
|
1771 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1772 | |
|
|
1773 | $self |
|
|
1774 | } |
|
|
1775 | |
|
|
1776 | sub size_request { |
|
|
1777 | my ($self) = @_; |
|
|
1778 | |
|
|
1779 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1780 | } |
|
|
1781 | |
|
|
1782 | sub _draw { |
|
|
1783 | my ($self) = @_; |
|
|
1784 | |
|
|
1785 | my $tex = $self->{tex}; |
|
|
1786 | |
|
|
1787 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1788 | |
|
|
1789 | if ($self->{rot90}) { |
|
|
1790 | glRotate 90, 0, 0, 1; |
|
|
1791 | glTranslate 0, -$self->{w}, 0; |
|
|
1792 | |
|
|
1793 | ($w, $h) = ($h, $w); |
|
|
1794 | } |
|
|
1795 | |
|
|
1796 | glEnable GL_BLEND; |
|
|
1797 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1798 | glEnable GL_TEXTURE_2D; |
|
|
1799 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1800 | |
|
|
1801 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1802 | |
|
|
1803 | glDisable GL_BLEND; |
|
|
1804 | glDisable GL_TEXTURE_2D; |
|
|
1805 | } |
|
|
1806 | |
|
|
1807 | ############################################################################# |
|
|
1808 | |
|
|
1809 | package CFClient::UI::VGauge; |
|
|
1810 | |
|
|
1811 | our @ISA = CFClient::UI::Base::; |
|
|
1812 | |
|
|
1813 | use List::Util qw(min max); |
|
|
1814 | |
|
|
1815 | use CFClient::OpenGL; |
|
|
1816 | |
|
|
1817 | my %tex = ( |
|
|
1818 | food => [ |
|
|
1819 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1820 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1821 | ], |
|
|
1822 | grace => [ |
|
|
1823 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1824 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
|
|
1825 | ], |
|
|
1826 | hp => [ |
|
|
1827 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1828 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1829 | ], |
|
|
1830 | mana => [ |
|
|
1831 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1832 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
|
|
1833 | ], |
|
|
1834 | ); |
|
|
1835 | |
|
|
1836 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1837 | sub new { |
|
|
1838 | my $class = shift; |
|
|
1839 | |
|
|
1840 | my $self = $class->SUPER::new ( |
|
|
1841 | type => 'food', |
|
|
1842 | @_ |
|
|
1843 | ); |
|
|
1844 | |
|
|
1845 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1846 | |
|
|
1847 | $self |
|
|
1848 | } |
|
|
1849 | |
|
|
1850 | sub size_request { |
|
|
1851 | my ($self) = @_; |
|
|
1852 | |
|
|
1853 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1854 | #@$tex{qw(w h)} |
|
|
1855 | (0, 0) |
|
|
1856 | } |
|
|
1857 | |
|
|
1858 | sub set_max { |
|
|
1859 | my ($self, $max) = @_; |
|
|
1860 | |
|
|
1861 | return if $self->{max_val} == $max; |
|
|
1862 | |
|
|
1863 | $self->{max_val} = $max; |
|
|
1864 | $self->update; |
|
|
1865 | } |
|
|
1866 | |
|
|
1867 | sub set_value { |
|
|
1868 | my ($self, $val, $max) = @_; |
|
|
1869 | |
|
|
1870 | $self->set_max ($max) |
|
|
1871 | if defined $max; |
|
|
1872 | |
|
|
1873 | return if $self->{val} == $val; |
|
|
1874 | |
|
|
1875 | $self->{val} = $val; |
|
|
1876 | $self->update; |
|
|
1877 | } |
|
|
1878 | |
|
|
1879 | sub _draw { |
|
|
1880 | my ($self) = @_; |
|
|
1881 | |
|
|
1882 | my $tex = $tex{$self->{type}}; |
|
|
1883 | my ($t1, $t2, $t3) = @$tex; |
|
|
1884 | |
|
|
1885 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1886 | |
|
|
1887 | if ($self->{vertical}) { |
|
|
1888 | glRotate 90, 0, 0, 1; |
|
|
1889 | glTranslate 0, -$self->{w}, 0; |
|
|
1890 | |
|
|
1891 | ($w, $h) = ($h, $w); |
|
|
1892 | } |
|
|
1893 | |
|
|
1894 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1895 | |
|
|
1896 | my $ycut1 = max 0, min 1, $ycut; |
|
|
1897 | my $ycut2 = max 0, min 1, $ycut - 1; |
|
|
1898 | |
|
|
1899 | my $h1 = $self->{h} * (1 - $ycut1); |
|
|
1900 | my $h2 = $self->{h} * (1 - $ycut2); |
|
|
1901 | |
|
|
1902 | glEnable GL_BLEND; |
|
|
1903 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1904 | glEnable GL_TEXTURE_2D; |
|
|
1905 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1906 | |
|
|
1907 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1908 | glBegin GL_QUADS; |
|
|
1909 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1910 | glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1911 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1912 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1913 | glEnd; |
|
|
1914 | |
|
|
1915 | my $ycut1 = List::Util::min 1, $ycut; |
|
|
1916 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1917 | glBegin GL_QUADS; |
|
|
1918 | glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1919 | glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1920 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1921 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1922 | glEnd; |
|
|
1923 | |
|
|
1924 | if ($t3) { |
|
|
1925 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
|
|
1926 | glBegin GL_QUADS; |
|
|
1927 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1928 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
|
|
1929 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
|
|
1930 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1931 | glEnd; |
|
|
1932 | } |
|
|
1933 | |
|
|
1934 | glDisable GL_BLEND; |
|
|
1935 | glDisable GL_TEXTURE_2D; |
|
|
1936 | } |
|
|
1937 | |
|
|
1938 | ############################################################################# |
|
|
1939 | |
|
|
1940 | package CFClient::UI::Gauge; |
|
|
1941 | |
|
|
1942 | our @ISA = CFClient::UI::VBox::; |
|
|
1943 | |
|
|
1944 | sub new { |
|
|
1945 | my ($class, %arg) = @_; |
|
|
1946 | |
|
|
1947 | my $self = $class->SUPER::new ( |
|
|
1948 | tooltip => $arg{type}, |
|
|
1949 | can_hover => 1, |
|
|
1950 | can_events => 1, |
|
|
1951 | %arg, |
|
|
1952 | ); |
|
|
1953 | |
|
|
1954 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); |
|
|
1955 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
|
|
1956 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); |
|
|
1957 | |
|
|
1958 | $self |
|
|
1959 | } |
|
|
1960 | |
|
|
1961 | sub set_fontsize { |
|
|
1962 | my ($self, $fsize) = @_; |
|
|
1963 | |
|
|
1964 | $self->{value}->set_fontsize ($fsize); |
|
|
1965 | $self->{max} ->set_fontsize ($fsize); |
|
|
1966 | } |
|
|
1967 | |
|
|
1968 | sub set_max { |
|
|
1969 | my ($self, $max) = @_; |
|
|
1970 | |
|
|
1971 | $self->{gauge}->set_max ($max); |
|
|
1972 | $self->{max}->set_text ($max); |
|
|
1973 | } |
|
|
1974 | |
|
|
1975 | sub set_value { |
|
|
1976 | my ($self, $val, $max) = @_; |
|
|
1977 | |
|
|
1978 | $self->set_max ($max) |
|
|
1979 | if defined $max; |
|
|
1980 | |
|
|
1981 | $self->{gauge}->set_value ($val, $max); |
|
|
1982 | $self->{value}->set_text ($val); |
|
|
1983 | } |
|
|
1984 | |
|
|
1985 | ############################################################################# |
|
|
1986 | |
1368 | package CFClient::UI::Slider; |
1987 | package CFClient::UI::Slider; |
1369 | |
1988 | |
1370 | use strict; |
1989 | use strict; |
1371 | |
1990 | |
1372 | use SDL::OpenGL; |
1991 | use CFClient::OpenGL; |
1373 | |
1992 | |
1374 | our @ISA = CFClient::UI::DrawBG::; |
1993 | our @ISA = CFClient::UI::DrawBG::; |
1375 | |
1994 | |
1376 | my @tex = |
1995 | my @tex = |
1377 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1996 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1387 | # TODO: calculations are off |
2006 | # TODO: calculations are off |
1388 | my $self = $class->SUPER::new ( |
2007 | my $self = $class->SUPER::new ( |
1389 | fg => [1, 1, 1], |
2008 | fg => [1, 1, 1], |
1390 | active_fg => [0, 0, 0], |
2009 | active_fg => [0, 0, 0], |
1391 | range => [0, 0, 100, 10], |
2010 | range => [0, 0, 100, 10], |
1392 | req_w => 40, |
2011 | req_w => $::WIDTH / 80, |
1393 | req_h => 13, |
2012 | req_h => $::WIDTH / 80, |
1394 | vertical => 0, |
2013 | vertical => 0, |
1395 | can_hover => 1, |
2014 | can_hover => 1, |
1396 | inner_pad => 5, |
2015 | inner_pad => 5, |
1397 | @_ |
2016 | @_ |
1398 | ); |
2017 | ); |
… | |
… | |
1502 | |
2121 | |
1503 | package CFClient::UI::TextView; |
2122 | package CFClient::UI::TextView; |
1504 | |
2123 | |
1505 | our @ISA = CFClient::UI::HBox::; |
2124 | our @ISA = CFClient::UI::HBox::; |
1506 | |
2125 | |
1507 | use SDL::OpenGL; |
2126 | use CFClient::OpenGL; |
1508 | |
2127 | |
1509 | sub new { |
2128 | sub new { |
1510 | my $class = shift; |
2129 | my $class = shift; |
1511 | |
2130 | |
1512 | my $self = $class->SUPER::new ( |
2131 | my $self = $class->SUPER::new ( |
1513 | req_w => $::WIDTH / 6, |
2132 | fontsize => 1, |
1514 | req_h => $::HEIGHT / 6, |
2133 | can_events => 0, |
1515 | fontsize => $::FONTSIZE, |
2134 | #font => default_font |
1516 | @_, |
2135 | @_, |
1517 | |
2136 | |
1518 | layout => (new CFClient::Layout), |
2137 | layout => (new CFClient::Layout), |
1519 | par => [], |
2138 | par => [], |
1520 | height => 0, |
2139 | height => 0, |
1521 | children => [ |
2140 | children => [ |
1522 | (new CFClient::UI::Empty expand => 1), |
2141 | (new CFClient::UI::Empty expand => 1), |
1523 | (new CFClient::UI::Slider vertical => 1), |
2142 | (new CFClient::UI::Slider vertical => 1), |
1524 | ], |
2143 | ], |
1525 | ); |
2144 | ); |
1526 | |
2145 | |
1527 | $self->{children}[1]->connect (changed => sub { |
2146 | $self->{children}[1]->connect (changed => sub { $self->update }); |
1528 | $self->update; |
|
|
1529 | }); |
|
|
1530 | |
2147 | |
1531 | $self |
2148 | $self |
1532 | } |
2149 | } |
1533 | |
2150 | |
1534 | sub set_fontsize { |
2151 | sub set_fontsize { |
… | |
… | |
1541 | sub text_height { |
2158 | sub text_height { |
1542 | my ($self, $text) = @_; |
2159 | my ($self, $text) = @_; |
1543 | |
2160 | |
1544 | my $layout = $self->{layout}; |
2161 | my $layout = $self->{layout}; |
1545 | |
2162 | |
1546 | $layout->set_height ($self->{fontsize}); |
2163 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1547 | $layout->set_width ($self->{w}); |
2164 | $layout->set_width ($self->{children}[0]{w}); |
1548 | $layout->set_text ($text); |
2165 | $layout->set_text ($text); |
1549 | |
2166 | |
1550 | ($layout->size)[1] |
2167 | ($layout->size)[1] |
1551 | } |
2168 | } |
1552 | |
2169 | |
… | |
… | |
1555 | |
2172 | |
1556 | $self->{need_reflow}++; |
2173 | $self->{need_reflow}++; |
1557 | $self->update; |
2174 | $self->update; |
1558 | } |
2175 | } |
1559 | |
2176 | |
1560 | sub size_request { |
|
|
1561 | my ($self) = @_; |
|
|
1562 | |
|
|
1563 | ($self->{req_w}, $self->{req_h}) |
|
|
1564 | } |
|
|
1565 | |
|
|
1566 | sub size_allocate { |
2177 | sub size_allocate { |
1567 | my ($self, $x, $y, $w, $h) = @_; |
2178 | my ($self, $w, $h) = @_; |
1568 | |
2179 | |
1569 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
2180 | $self->SUPER::size_allocate ($w, $h); |
1570 | |
2181 | |
|
|
2182 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1571 | $self->{layout}->set_height ($self->{fontsize}); |
2183 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1572 | $self->{layout}->set_width ($self->{w}); |
2184 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1573 | |
2185 | |
1574 | $self->reflow; |
2186 | $self->reflow; |
1575 | } |
2187 | } |
1576 | |
2188 | |
1577 | sub add_paragraph { |
2189 | sub add_paragraph { |
… | |
… | |
1610 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
2222 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
1611 | |
2223 | |
1612 | delete $self->{texture}; |
2224 | delete $self->{texture}; |
1613 | } |
2225 | } |
1614 | |
2226 | |
1615 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
2227 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
1616 | glClearColor 0, 0, 0, 1; |
2228 | glClearColor 0, 0, 0, 0; |
1617 | glClear GL_COLOR_BUFFER_BIT; |
2229 | glClear GL_COLOR_BUFFER_BIT; |
1618 | |
2230 | |
1619 | glEnable GL_BLEND; |
2231 | glEnable GL_BLEND; |
1620 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
2232 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1621 | glEnable GL_TEXTURE_2D; |
2233 | glEnable GL_TEXTURE_2D; |
… | |
… | |
1628 | |
2240 | |
1629 | my $y = 0; |
2241 | my $y = 0; |
1630 | |
2242 | |
1631 | my $layout = $self->{layout}; |
2243 | my $layout = $self->{layout}; |
1632 | |
2244 | |
|
|
2245 | $layout->set_font ($self->{font}) if $self->{font}; |
|
|
2246 | |
1633 | for my $par (@{$self->{par}}) { |
2247 | for my $par (@{$self->{par}}) { |
1634 | my $h = $par->[0]; |
2248 | my $h = $par->[0]; |
1635 | |
2249 | |
1636 | if ($y0 < $y + $h && $y < $y1) { |
2250 | if ($y0 < $y + $h && $y < $y1) { |
1637 | $layout->set_text ($par->[2]); |
2251 | $layout->set_text ($par->[2]); |
… | |
… | |
1651 | } |
2265 | } |
1652 | |
2266 | |
1653 | sub _draw { |
2267 | sub _draw { |
1654 | my ($self) = @_; |
2268 | my ($self) = @_; |
1655 | |
2269 | |
1656 | if ($self->{texture}) { |
2270 | glEnable GL_BLEND; |
|
|
2271 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1657 | glEnable GL_TEXTURE_2D; |
2272 | glEnable GL_TEXTURE_2D; |
1658 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2273 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2274 | glColor 1, 1, 1, 1; |
1659 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
2275 | $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
1660 | glDisable GL_TEXTURE_2D; |
2276 | glDisable GL_TEXTURE_2D; |
1661 | } |
2277 | glDisable GL_BLEND; |
1662 | |
2278 | |
1663 | $self->{children}[1]->draw; |
2279 | $self->{children}[1]->draw; |
1664 | |
2280 | |
1665 | } |
2281 | } |
1666 | |
2282 | |
1667 | ############################################################################# |
2283 | ############################################################################# |
1668 | |
2284 | |
1669 | package CFClient::UI::MapWidget; |
|
|
1670 | |
|
|
1671 | use strict; |
|
|
1672 | |
|
|
1673 | use List::Util qw(min max); |
|
|
1674 | |
|
|
1675 | use SDL; |
|
|
1676 | use SDL::OpenGL; |
|
|
1677 | |
|
|
1678 | our @ISA = CFClient::UI::Base::; |
|
|
1679 | |
|
|
1680 | sub new { |
|
|
1681 | my $class = shift; |
|
|
1682 | |
|
|
1683 | $class->SUPER::new ( |
|
|
1684 | z => -1, |
|
|
1685 | can_focus => 1, |
|
|
1686 | list => (glGenLists 1), |
|
|
1687 | @_ |
|
|
1688 | ) |
|
|
1689 | } |
|
|
1690 | |
|
|
1691 | sub key_down { |
|
|
1692 | print "MAPKEYDOWN\n"; |
|
|
1693 | } |
|
|
1694 | |
|
|
1695 | sub key_up { |
|
|
1696 | } |
|
|
1697 | |
|
|
1698 | sub button_down { |
|
|
1699 | my ($self, $ev, $x, $y) = @_; |
|
|
1700 | |
|
|
1701 | $self->focus_in; |
|
|
1702 | |
|
|
1703 | if ($ev->button == 2) { |
|
|
1704 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1705 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1706 | |
|
|
1707 | $self->{motion} = sub { |
|
|
1708 | my ($ev, $x, $y) = @_; |
|
|
1709 | |
|
|
1710 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1711 | |
|
|
1712 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1713 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1714 | |
|
|
1715 | $self->update; |
|
|
1716 | }; |
|
|
1717 | } |
|
|
1718 | } |
|
|
1719 | |
|
|
1720 | sub button_up { |
|
|
1721 | my ($self, $ev, $x, $y) = @_; |
|
|
1722 | |
|
|
1723 | delete $self->{motion}; |
|
|
1724 | } |
|
|
1725 | |
|
|
1726 | sub mouse_motion { |
|
|
1727 | my ($self, $ev, $x, $y) = @_; |
|
|
1728 | |
|
|
1729 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1730 | } |
|
|
1731 | |
|
|
1732 | sub size_request { |
|
|
1733 | ( |
|
|
1734 | 1 + 32 * int $::WIDTH / 32, |
|
|
1735 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1736 | ) |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | sub update { |
|
|
1740 | my ($self) = @_; |
|
|
1741 | |
|
|
1742 | $self->{need_update} = 1; |
|
|
1743 | $self->SUPER::update; |
|
|
1744 | } |
|
|
1745 | |
|
|
1746 | sub draw { |
|
|
1747 | my ($self) = @_; |
|
|
1748 | |
|
|
1749 | if (delete $self->{need_update}) { |
|
|
1750 | glNewList $self->{list}, GL_COMPILE; |
|
|
1751 | |
|
|
1752 | if ($::MAP) { |
|
|
1753 | my $sw = int $::WIDTH / 32; |
|
|
1754 | my $sh = int $::HEIGHT / 32; |
|
|
1755 | |
|
|
1756 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1757 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1758 | |
|
|
1759 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1760 | |
|
|
1761 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1762 | |
|
|
1763 | if ($::CFG->{fow_enable}) { |
|
|
1764 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1765 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1766 | glConvolutionFilter2D |
|
|
1767 | GL_CONVOLUTION_2D, |
|
|
1768 | GL_ALPHA, |
|
|
1769 | 3, 3, |
|
|
1770 | GL_ALPHA, GL_FLOAT, |
|
|
1771 | pack "f*", |
|
|
1772 | 0.1, 0.1, 0.1, |
|
|
1773 | 0.1, 0.2, 0.1, |
|
|
1774 | 0.1, 0.1, 0.1, |
|
|
1775 | ; |
|
|
1776 | glEnable GL_CONVOLUTION_2D; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | my $tex = new CFClient::Texture |
|
|
1780 | w => $w, |
|
|
1781 | h => $h, |
|
|
1782 | data => $data, |
|
|
1783 | internalformat => GL_ALPHA, |
|
|
1784 | format => GL_ALPHA; |
|
|
1785 | |
|
|
1786 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1787 | |
|
|
1788 | glEnable GL_BLEND; |
|
|
1789 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1790 | glEnable GL_TEXTURE_2D; |
|
|
1791 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1792 | |
|
|
1793 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1794 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1795 | |
|
|
1796 | glDisable GL_TEXTURE_2D; |
|
|
1797 | glDisable GL_BLEND; |
|
|
1798 | } |
|
|
1799 | |
|
|
1800 | # HACK BEGIN |
|
|
1801 | { |
|
|
1802 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1803 | my ($w, $h) = (250, 250); |
|
|
1804 | |
|
|
1805 | glEnable GL_BLEND; |
|
|
1806 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1807 | glEnable GL_TEXTURE_2D; |
|
|
1808 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1809 | |
|
|
1810 | CFClient::Texture->new ( |
|
|
1811 | w => $w, |
|
|
1812 | h => $h, |
|
|
1813 | data => $::MAP->mapmap ($w, $h), |
|
|
1814 | type => GL_UNSIGNED_INT_8_8_8_8_REV |
|
|
1815 | )->draw_quad (100, 100); |
|
|
1816 | |
|
|
1817 | glDisable GL_TEXTURE_2D; |
|
|
1818 | glDisable GL_BLEND; |
|
|
1819 | } |
|
|
1820 | # HACK END |
|
|
1821 | } |
|
|
1822 | |
|
|
1823 | glEndList; |
|
|
1824 | } |
|
|
1825 | |
|
|
1826 | glPushMatrix; |
|
|
1827 | glCallList $self->{list}; |
|
|
1828 | glPopMatrix; |
|
|
1829 | |
|
|
1830 | if ($FOCUS != $self) { |
|
|
1831 | glColor 64/255, 64/255, 64/255; |
|
|
1832 | glLogicOp GL_AND; |
|
|
1833 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1834 | glBegin GL_QUADS; |
|
|
1835 | glVertex 0, 0; |
|
|
1836 | glVertex 0, $::HEIGHT; |
|
|
1837 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1838 | glVertex $::WIDTH, 0; |
|
|
1839 | glEnd; |
|
|
1840 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1841 | } |
|
|
1842 | } |
|
|
1843 | |
|
|
1844 | my %DIR = ( |
|
|
1845 | SDLK_KP8, [1, "north"], |
|
|
1846 | SDLK_KP9, [2, "northeast"], |
|
|
1847 | SDLK_KP6, [3, "east"], |
|
|
1848 | SDLK_KP3, [4, "southeast"], |
|
|
1849 | SDLK_KP2, [5, "south"], |
|
|
1850 | SDLK_KP1, [6, "southwest"], |
|
|
1851 | SDLK_KP4, [7, "west"], |
|
|
1852 | SDLK_KP7, [8, "northwest"], |
|
|
1853 | |
|
|
1854 | SDLK_UP, [1, "north"], |
|
|
1855 | SDLK_RIGHT, [3, "east"], |
|
|
1856 | SDLK_DOWN, [5, "south"], |
|
|
1857 | SDLK_LEFT, [7, "west"], |
|
|
1858 | ); |
|
|
1859 | |
|
|
1860 | sub key_down { |
|
|
1861 | my ($self, $ev) = @_; |
|
|
1862 | |
|
|
1863 | my $mod = $ev->key_mod; |
|
|
1864 | my $sym = $ev->key_sym; |
|
|
1865 | |
|
|
1866 | if ($sym == SDLK_KP5) { |
|
|
1867 | $::CONN->user_send ("command stay fire"); |
|
|
1868 | } elsif ($sym == SDLK_a) { |
|
|
1869 | $::CONN->user_send ("command apply"); |
|
|
1870 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1871 | $self->emit ('activate_console'); |
|
|
1872 | } elsif ($sym == SDLK_SLASH) { |
|
|
1873 | $self->emit ('activate_console' => '/'); |
|
|
1874 | } elsif (exists $DIR{$sym}) { |
|
|
1875 | if ($mod & KMOD_SHIFT) { |
|
|
1876 | $self->{shft}++; |
|
|
1877 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1878 | } elsif ($mod & KMOD_CTRL) { |
|
|
1879 | $self->{ctrl}++; |
|
|
1880 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1881 | } else { |
|
|
1882 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1883 | } |
|
|
1884 | } |
|
|
1885 | } |
|
|
1886 | |
|
|
1887 | sub key_up { |
|
|
1888 | my ($self, $ev) = @_; |
|
|
1889 | |
|
|
1890 | my $mod = $ev->key_mod; |
|
|
1891 | my $sym = $ev->key_sym; |
|
|
1892 | |
|
|
1893 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1894 | $::CONN->user_send ("command fire_stop"); |
|
|
1895 | } |
|
|
1896 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1897 | $::CONN->user_send ("command run_stop"); |
|
|
1898 | } |
|
|
1899 | } |
|
|
1900 | |
|
|
1901 | ############################################################################# |
|
|
1902 | |
|
|
1903 | package CFClient::UI::Animator; |
2285 | package CFClient::UI::Animator; |
1904 | |
2286 | |
1905 | use SDL::OpenGL; |
2287 | use CFClient::OpenGL; |
1906 | |
2288 | |
1907 | our @ISA = CFClient::UI::Bin::; |
2289 | our @ISA = CFClient::UI::Bin::; |
1908 | |
2290 | |
1909 | sub moveto { |
2291 | sub moveto { |
1910 | my ($self, $x, $y) = @_; |
2292 | my ($self, $x, $y) = @_; |
… | |
… | |
1948 | |
2330 | |
1949 | sub new { |
2331 | sub new { |
1950 | my $class = shift; |
2332 | my $class = shift; |
1951 | |
2333 | |
1952 | my $self = $class->SUPER::new ( |
2334 | my $self = $class->SUPER::new ( |
1953 | state => 0, |
2335 | state => 0, |
1954 | connect_activate => \&toggle_flopper, |
2336 | connect_activate => \&toggle_flopper, |
1955 | @_ |
2337 | @_ |
1956 | ); |
2338 | ); |
1957 | |
2339 | |
1958 | if ($self->{state}) { |
2340 | if ($self->{state}) { |
… | |
… | |
1979 | $self->emit (changed => $self->{state}); |
2361 | $self->emit (changed => $self->{state}); |
1980 | } |
2362 | } |
1981 | |
2363 | |
1982 | ############################################################################# |
2364 | ############################################################################# |
1983 | |
2365 | |
|
|
2366 | package CFClient::UI::Tooltip; |
|
|
2367 | |
|
|
2368 | our @ISA = CFClient::UI::Bin::; |
|
|
2369 | |
|
|
2370 | use CFClient::OpenGL; |
|
|
2371 | |
|
|
2372 | sub new { |
|
|
2373 | my $class = shift; |
|
|
2374 | |
|
|
2375 | $class->SUPER::new ( |
|
|
2376 | @_, |
|
|
2377 | can_events => 0, |
|
|
2378 | ) |
|
|
2379 | } |
|
|
2380 | |
|
|
2381 | sub set_markup { |
|
|
2382 | my ($self, $text) = @_; |
|
|
2383 | |
|
|
2384 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2385 | $self->{label}->set_markup ($text); |
|
|
2386 | $self->add ($self->{label}); |
|
|
2387 | } |
|
|
2388 | |
|
|
2389 | sub size_request { |
|
|
2390 | my ($self) = @_; |
|
|
2391 | |
|
|
2392 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2393 | |
|
|
2394 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2395 | |
|
|
2396 | ($w + 4, $h + 4) |
|
|
2397 | } |
|
|
2398 | |
|
|
2399 | sub size_allocate { |
|
|
2400 | my ($self, $w, $h) = @_; |
|
|
2401 | |
|
|
2402 | $self->SUPER::size_allocate ($w - 4, $h - 4); |
|
|
2403 | } |
|
|
2404 | |
|
|
2405 | sub _draw { |
|
|
2406 | my ($self) = @_; |
|
|
2407 | |
|
|
2408 | glPushMatrix; |
|
|
2409 | glTranslate 0.375, 0.375; |
|
|
2410 | |
|
|
2411 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2412 | |
|
|
2413 | glColor 1, 0.8, 0.4; |
|
|
2414 | glBegin GL_QUADS; |
|
|
2415 | glVertex 0 , 0; |
|
|
2416 | glVertex 0 , $h; |
|
|
2417 | glVertex $w, $h; |
|
|
2418 | glVertex $w, 0; |
|
|
2419 | glEnd; |
|
|
2420 | |
|
|
2421 | glColor 0, 0, 0; |
|
|
2422 | glBegin GL_LINE_LOOP; |
|
|
2423 | glVertex 0 , 0; |
|
|
2424 | glVertex 0 , $h; |
|
|
2425 | glVertex $w, $h; |
|
|
2426 | glVertex $w, 0; |
|
|
2427 | glEnd; |
|
|
2428 | |
|
|
2429 | glPopMatrix; |
|
|
2430 | |
|
|
2431 | glTranslate 2, 2; |
|
|
2432 | $self->SUPER::_draw; |
|
|
2433 | } |
|
|
2434 | |
|
|
2435 | ############################################################################# |
|
|
2436 | |
|
|
2437 | package CFClient::UI::Face; |
|
|
2438 | |
|
|
2439 | our @ISA = CFClient::UI::Base::; |
|
|
2440 | |
|
|
2441 | use CFClient::OpenGL; |
|
|
2442 | |
|
|
2443 | sub new { |
|
|
2444 | my $class = shift; |
|
|
2445 | |
|
|
2446 | $class->SUPER::new ( |
|
|
2447 | aspect => 1, |
|
|
2448 | @_, |
|
|
2449 | ) |
|
|
2450 | } |
|
|
2451 | |
|
|
2452 | sub size_request { |
|
|
2453 | (32, 8) |
|
|
2454 | } |
|
|
2455 | |
|
|
2456 | sub _draw { |
|
|
2457 | my ($self) = @_; |
|
|
2458 | |
|
|
2459 | return unless $::CONN;#d# manage and cache textures differently |
|
|
2460 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
|
|
2461 | |
|
|
2462 | # TODO animation |
|
|
2463 | if ($tex) { |
|
|
2464 | glEnable GL_BLEND; |
|
|
2465 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2466 | glEnable GL_TEXTURE_2D; |
|
|
2467 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2468 | glColor 1, 1, 1, 1; |
|
|
2469 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
2470 | glDisable GL_TEXTURE_2D; |
|
|
2471 | glDisable GL_BLEND; |
|
|
2472 | } |
|
|
2473 | } |
|
|
2474 | |
|
|
2475 | ############################################################################# |
|
|
2476 | |
|
|
2477 | package CFClient::UI::InventoryItem; |
|
|
2478 | |
|
|
2479 | our @ISA = CFClient::UI::HBox::; |
|
|
2480 | |
|
|
2481 | sub new { |
|
|
2482 | my $class = shift; |
|
|
2483 | |
|
|
2484 | my %args = @_; |
|
|
2485 | |
|
|
2486 | my $item = $args{item}; |
|
|
2487 | |
|
|
2488 | my $desc = $item->{nrof} < 2 |
|
|
2489 | ? $item->{name} |
|
|
2490 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2491 | |
|
|
2492 | |
|
|
2493 | my $self = $class->SUPER::new ( |
|
|
2494 | can_hover => 1, |
|
|
2495 | can_events => 1, |
|
|
2496 | tooltip => (CFClient::UI::Label->escape ($desc) |
|
|
2497 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2498 | connect_button_down => sub { |
|
|
2499 | my ($self, $ev, $x, $y) = @_; |
|
|
2500 | |
|
|
2501 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2502 | if ($ev->{button} == 1) { |
|
|
2503 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
|
|
2504 | } elsif ($ev->{button} == 2) { |
|
|
2505 | $::CONN->send ("apply $item->{tag}"); |
|
|
2506 | } elsif ($ev->{button} == 3) { |
|
|
2507 | CFClient::UI::Menu->new ( |
|
|
2508 | items => [ |
|
|
2509 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2510 | [ |
|
|
2511 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
|
|
2512 | sub { $::CONN->send ("lock $item->{tag}") }, |
|
|
2513 | ], |
|
|
2514 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2515 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2516 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2517 | ], |
|
|
2518 | )->popup ($ev); |
|
|
2519 | } |
|
|
2520 | |
|
|
2521 | 1 |
|
|
2522 | }, |
|
|
2523 | %args |
|
|
2524 | ); |
|
|
2525 | $self->add(new CFClient::UI::Face |
|
|
2526 | can_events => 0, |
|
|
2527 | face => $item->{face}, |
|
|
2528 | anim => $item->{anim}, |
|
|
2529 | animspeed => $item->{animspeed}); |
|
|
2530 | $self->add(new CFClient::UI::Label |
|
|
2531 | can_events => 0, |
|
|
2532 | text => $desc); |
|
|
2533 | |
|
|
2534 | $self |
|
|
2535 | } |
|
|
2536 | |
|
|
2537 | ############################################################################# |
|
|
2538 | |
|
|
2539 | package CFClient::UI::Inventory; |
|
|
2540 | |
|
|
2541 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2542 | |
|
|
2543 | sub new { |
|
|
2544 | my $class = shift; |
|
|
2545 | |
|
|
2546 | my $self = $class->SUPER::new ( |
|
|
2547 | scrolled => (new CFClient::UI::VBox), |
|
|
2548 | @_, |
|
|
2549 | ); |
|
|
2550 | |
|
|
2551 | $self |
|
|
2552 | } |
|
|
2553 | |
|
|
2554 | sub set_items { |
|
|
2555 | my ($self, $items) = @_; |
|
|
2556 | |
|
|
2557 | $self->{scrolled}->clear; |
|
|
2558 | return unless $items; |
|
|
2559 | |
|
|
2560 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
|
|
2561 | |
|
|
2562 | $self->{real_items} = \@items; |
|
|
2563 | |
|
|
2564 | for my $item (@items) { |
|
|
2565 | my $desc = $item->{nrof} < 2 |
|
|
2566 | ? $item->{name} |
|
|
2567 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2568 | |
|
|
2569 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
|
|
2570 | } |
|
|
2571 | |
|
|
2572 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2573 | } |
|
|
2574 | |
|
|
2575 | sub size_request { |
|
|
2576 | my ($self) = @_; |
|
|
2577 | ($self->{req_w}, $self->{req_h}); |
|
|
2578 | } |
|
|
2579 | |
|
|
2580 | ############################################################################# |
|
|
2581 | |
|
|
2582 | package CFClient::UI::Menu; |
|
|
2583 | |
|
|
2584 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2585 | |
|
|
2586 | use CFClient::OpenGL; |
|
|
2587 | |
|
|
2588 | sub new { |
|
|
2589 | my $class = shift; |
|
|
2590 | |
|
|
2591 | my $self = $class->SUPER::new ( |
|
|
2592 | items => [], |
|
|
2593 | z => 100, |
|
|
2594 | @_, |
|
|
2595 | ); |
|
|
2596 | |
|
|
2597 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2598 | |
|
|
2599 | for my $item (@{ $self->{items} }) { |
|
|
2600 | my ($widget, $cb) = @$item; |
|
|
2601 | |
|
|
2602 | # handle various types of items, only text for now |
|
|
2603 | if (!ref $widget) { |
|
|
2604 | $widget = new CFClient::UI::Label |
|
|
2605 | can_hover => 1, |
|
|
2606 | can_events => 1, |
|
|
2607 | text => $widget; |
|
|
2608 | } |
|
|
2609 | |
|
|
2610 | $self->{item}{$widget} = $item; |
|
|
2611 | |
|
|
2612 | $self->{vbox}->add ($widget); |
|
|
2613 | } |
|
|
2614 | |
|
|
2615 | $self |
|
|
2616 | } |
|
|
2617 | |
|
|
2618 | # popup given the event (must be a mouse button down event currently) |
|
|
2619 | sub popup { |
|
|
2620 | my ($self, $ev) = @_; |
|
|
2621 | |
|
|
2622 | $self->emit ("popdown"); |
|
|
2623 | |
|
|
2624 | # maybe save $GRAB? must be careful about events... |
|
|
2625 | $GRAB = $self; |
|
|
2626 | $self->{button} = $ev->{button}; |
|
|
2627 | |
|
|
2628 | $self->show; |
|
|
2629 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2630 | } |
|
|
2631 | |
|
|
2632 | sub mouse_motion { |
|
|
2633 | my ($self, $ev, $x, $y) = @_; |
|
|
2634 | |
|
|
2635 | # TODO: should use vbox->find_widget or so |
|
|
2636 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2637 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2638 | } |
|
|
2639 | |
|
|
2640 | sub button_up { |
|
|
2641 | my ($self, $ev, $x, $y) = @_; |
|
|
2642 | |
|
|
2643 | if ($ev->{button} == $self->{button}) { |
|
|
2644 | undef $GRAB; |
|
|
2645 | $self->hide; |
|
|
2646 | |
|
|
2647 | $self->emit ("popdown"); |
|
|
2648 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2649 | } |
|
|
2650 | } |
|
|
2651 | |
|
|
2652 | ############################################################################# |
|
|
2653 | |
1984 | package CFClient::UI::Root; |
2654 | package CFClient::UI::Root; |
1985 | |
2655 | |
1986 | our @ISA = CFClient::UI::Container::; |
2656 | our @ISA = CFClient::UI::Container::; |
1987 | |
2657 | |
1988 | use SDL::OpenGL; |
2658 | use CFClient::OpenGL; |
|
|
2659 | |
|
|
2660 | sub new { |
|
|
2661 | my $class = shift; |
|
|
2662 | |
|
|
2663 | $class->SUPER::new ( |
|
|
2664 | @_, |
|
|
2665 | ) |
|
|
2666 | } |
|
|
2667 | |
|
|
2668 | sub configure { |
|
|
2669 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2670 | |
|
|
2671 | $self->{w} = $w; |
|
|
2672 | $self->{h} = $h; |
|
|
2673 | } |
|
|
2674 | |
|
|
2675 | sub check_size { |
|
|
2676 | my ($self) = @_; |
|
|
2677 | |
|
|
2678 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2679 | if $self->{w}; |
|
|
2680 | } |
1989 | |
2681 | |
1990 | sub size_request { |
2682 | sub size_request { |
1991 | ($::WIDTH, $::HEIGHT) |
2683 | my ($self) = @_; |
|
|
2684 | |
|
|
2685 | ($self->{w}, $self->{h}) |
1992 | } |
2686 | } |
1993 | |
2687 | |
1994 | sub size_allocate { |
2688 | sub size_allocate { |
1995 | my ($self, $x, $y, $w, $h) = @_; |
2689 | my ($self, $w, $h) = @_; |
1996 | |
2690 | |
1997 | $self->_size_allocate ($x, $y, $w, $h); |
2691 | my $old_w = $self->{old_w}; |
|
|
2692 | my $old_h = $self->{old_h}; |
1998 | |
2693 | |
1999 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
2694 | if ($old_w && $old_h) { |
2000 | for @{$self->{children}}; |
2695 | for my $child ($self->children) { |
2001 | } |
2696 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2697 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2698 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2699 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2700 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2701 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2702 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2703 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2704 | } |
|
|
2705 | } |
2002 | |
2706 | |
2003 | sub _topleft { |
2707 | for my $child ($self->children) { |
|
|
2708 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2709 | |
|
|
2710 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2711 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2712 | $child->configure ($X, $Y, $W, $H); |
|
|
2713 | } |
|
|
2714 | |
|
|
2715 | $self->{old_w} = $w; |
|
|
2716 | $self->{old_h} = $h; |
|
|
2717 | } |
|
|
2718 | |
|
|
2719 | sub coord2local { |
2004 | my ($self, $x, $y) = @_; |
2720 | my ($self, $x, $y) = @_; |
2005 | |
2721 | |
2006 | ($x, $y) |
2722 | ($x, $y) |
2007 | } |
2723 | } |
2008 | |
2724 | |
|
|
2725 | sub coord2global { |
|
|
2726 | my ($self, $x, $y) = @_; |
|
|
2727 | |
|
|
2728 | ($x, $y) |
|
|
2729 | } |
|
|
2730 | |
2009 | sub update { |
2731 | sub update { |
2010 | my ($self) = @_; |
2732 | my ($self) = @_; |
2011 | |
2733 | |
2012 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2734 | $self->check_size; |
2013 | ::refresh (); |
2735 | $::WANT_REFRESH++; |
2014 | } |
2736 | } |
2015 | |
2737 | |
2016 | sub add { |
2738 | sub add { |
2017 | my ($self, $widget) = @_; |
2739 | my ($self, $child) = @_; |
2018 | |
2740 | |
|
|
2741 | # integerise window positions |
|
|
2742 | $child->{x} = int $child->{x}; |
|
|
2743 | $child->{y} = int $child->{y}; |
|
|
2744 | |
2019 | $self->SUPER::add ($widget); |
2745 | $self->SUPER::add ($child); |
2020 | |
|
|
2021 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
2022 | } |
2746 | } |
2023 | |
2747 | |
2024 | sub on_refresh { |
2748 | sub on_refresh { |
2025 | my ($self, $id, $cb) = @_; |
2749 | my ($self, $id, $cb) = @_; |
2026 | |
2750 | |
2027 | $self->{refresh_hook}{$id} = $cb; |
2751 | $self->{refresh_hook}{$id} = $cb; |
2028 | } |
2752 | } |
2029 | |
2753 | |
2030 | sub draw { |
2754 | sub draw { |
2031 | my ($self) = @_; |
2755 | my ($self) = @_; |
2032 | |
2756 | |
|
|
2757 | if ($self->{check_size}) { |
|
|
2758 | my @queue = ([], []); |
|
|
2759 | |
|
|
2760 | for (;;) { |
|
|
2761 | if ($self->{check_size}) { |
|
|
2762 | # heuristic: check containers last |
|
|
2763 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2764 | for values %{delete $self->{check_size}} |
|
|
2765 | } |
|
|
2766 | |
|
|
2767 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2768 | |
|
|
2769 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2770 | ? @$widget{qw(user_w user_h)} |
|
|
2771 | : $widget->size_request; |
|
|
2772 | |
|
|
2773 | if ($w != $widget->{req_w} || $h != $widget->{req_h}) { |
|
|
2774 | $widget->{req_w} = $w; |
|
|
2775 | $widget->{req_h} = $h; |
|
|
2776 | |
|
|
2777 | $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; |
|
|
2778 | |
|
|
2779 | $widget->{parent}->check_size |
|
|
2780 | if $widget->{parent}; |
|
|
2781 | } |
|
|
2782 | } |
|
|
2783 | } |
|
|
2784 | |
|
|
2785 | while ($self->{size_alloc}) { |
|
|
2786 | for (values %{delete $self->{size_alloc}}) { |
|
|
2787 | my ($widget, $w, $h) = @$_; |
|
|
2788 | |
|
|
2789 | $widget->{w} = $w; |
|
|
2790 | $widget->{h} = $h; |
|
|
2791 | $widget->size_allocate ($w, $h); |
|
|
2792 | $widget->emit (size_allocate => $w, $h); |
|
|
2793 | } |
|
|
2794 | } |
|
|
2795 | |
2033 | while (my $rcb = delete $self->{refresh_hook}) { |
2796 | while ($self->{refresh_hook}) { |
2034 | $_->() for values %$rcb; |
2797 | $_->() |
|
|
2798 | for values %{delete $self->{refresh_hook}}; |
2035 | } |
2799 | } |
2036 | |
2800 | |
2037 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2801 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2038 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2802 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2039 | glClear GL_COLOR_BUFFER_BIT; |
2803 | glClear GL_COLOR_BUFFER_BIT; |
… | |
… | |
2050 | ############################################################################# |
2814 | ############################################################################# |
2051 | |
2815 | |
2052 | package CFClient::UI; |
2816 | package CFClient::UI; |
2053 | |
2817 | |
2054 | $ROOT = new CFClient::UI::Root; |
2818 | $ROOT = new CFClient::UI::Root; |
|
|
2819 | $TOOLTIP = new CFClient::UI::Tooltip; |
2055 | |
2820 | |
2056 | 1 |
2821 | 1 |
2057 | |
2822 | |