ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.232 by root, Thu May 25 02:23:14 2006 UTC vs.
Revision 1.330 by root, Sun Jul 23 04:37:51 2006 UTC

3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8use Event;
8 9
9use CFClient; 10use CFClient;
11use CFClient::Texture;
10 12
11our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
12 14
15our $LAYOUT;
13our $ROOT; 16our $ROOT;
14our $TOOLTIP; 17our $TOOLTIP;
15our $BUTTON_STATE; 18our $BUTTON_STATE;
16 19
17our %WIDGET; # all widgets, weak-referenced 20our %WIDGET; # all widgets, weak-referenced
18 21
19sub check_tooltip { 22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
20 if (!$GRAB) { 23 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 25 if (length $widget->{tooltip}) {
23
24 if ($TOOLTIP->{owner} != $widget) { 26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->hide;
28
25 $TOOLTIP->{owner} = $widget; 29 $TOOLTIP->{owner} = $widget;
30
31 return if $ENV{CFPLUS_DEBUG} & 8;
26 32
27 my $tip = $widget->{tooltip}; 33 my $tip = $widget->{tooltip};
28 34
29 $tip = $tip->($widget) if CODE:: eq ref $tip; 35 $tip = $tip->($widget) if CODE:: eq ref $tip;
30 36
31 $TOOLTIP->set_tooltip_from ($widget); 37 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show; 38 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH;
38
39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
42 } 39 }
43 40
44 return; 41 return;
45 } 42 }
46 } 43 }
47 } 44 }
48 45
49 $TOOLTIP->hide; 46 $TOOLTIP->hide;
50 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
48});
49
50sub get_layout {
51 my $layout;
52
53 for (grep { $_->{name} } values %WIDGET) {
54 my $win = $layout->{$_->{name}} = { };
55
56 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
57 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
58 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
59 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
60
61 $win->{show} = $_->{visible} && $_->{is_toplevel};
62 }
63
64 $layout
65}
66
67sub set_layout {
68 my ($layout) = @_;
69
70 $LAYOUT = $layout;
51} 71}
52 72
53# class methods for events 73# class methods for events
54sub feed_sdl_key_down_event { 74sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) 75 $FOCUS->emit (key_down => $_[0])
59sub feed_sdl_key_up_event { 79sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) 80 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 81 if $FOCUS;
62} 82}
63 83
84sub check_hover {
85 my ($widget) = @_;
86
87 if ($widget != $HOVER) {
88 my $hover = $HOVER; $HOVER = $widget;
89
90 $hover->update if $hover && $hover->{can_hover};
91 $HOVER->update if $HOVER && $HOVER->{can_hover};
92
93 $TOOLTIP_WATCHER->start;
94 }
95}
96
64sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
65 my ($ev) = @_; 98 my ($ev) = @_;
66 my ($x, $y) = ($ev->{x}, $ev->{y}); 99 my ($x, $y) = ($ev->{x}, $ev->{y});
67 100
68 if (!$BUTTON_STATE) { 101 unless ($BUTTON_STATE) {
69 my $widget = $ROOT->find_widget ($x, $y); 102 my $widget = $ROOT->find_widget ($x, $y);
70 103
71 $GRAB = $widget; 104 $GRAB = $widget;
72 $GRAB->update if $GRAB; 105 $GRAB->update if $GRAB;
73 106
74 check_tooltip; 107 $TOOLTIP_WATCHER->cb->();
75 } 108 }
76 109
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 110 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 111
79 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) 112 if ($GRAB) {
80 if $GRAB; 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel
115 $ev->{dx} = 0;
116 $ev->{dy} = $ev->{button} * 2 - 9;
117 $GRAB->emit (mouse_wheel => $ev);
118 } else {
119 $GRAB->emit (button_down => $ev)
120 }
121 }
81} 122}
82 123
83sub feed_sdl_button_up_event { 124sub feed_sdl_button_up_event {
84 my ($ev) = @_; 125 my ($ev) = @_;
85 my ($x, $y) = ($ev->{x}, $ev->{y});
86 126
87 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 127 my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y});
88 128
89 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 129 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
90 130
91 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) 131 $GRAB->emit (button_up => $ev)
92 if $GRAB; 132 if $GRAB && $ev->{button} != 4 && $ev->{button} != 5;
93 133
94 if (!$BUTTON_STATE) { 134 unless ($BUTTON_STATE) {
95 my $grab = $GRAB; undef $GRAB; 135 my $grab = $GRAB; undef $GRAB;
96 $grab->update if $grab; 136 $grab->update if $grab;
97 $GRAB->update if $GRAB; 137 $GRAB->update if $GRAB;
98 138
99 check_tooltip; 139 check_hover $widget;
140 $TOOLTIP_WATCHER->cb->();
100 } 141 }
101} 142}
102 143
103sub feed_sdl_motion_event { 144sub feed_sdl_motion_event {
104 my ($ev) = @_; 145 my ($ev) = @_;
105 my ($x, $y) = ($ev->{x}, $ev->{y}); 146 my ($x, $y) = ($ev->{x}, $ev->{y});
106 147
107 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 148 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
108 149
109 if ($widget != $HOVER) { 150 check_hover $widget;
110 my $hover = $HOVER; $HOVER = $widget;
111 151
112 $hover->update if $hover && $hover->{can_hover}; 152 $HOVER->emit (mouse_motion => $ev)
113 $HOVER->update if $HOVER && $HOVER->{can_hover};
114
115 check_tooltip;
116 }
117
118 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
119 if $HOVER; 153 if $HOVER;
120} 154}
121 155
122# convert position array to integers 156# convert position array to integers
123sub harmonize { 157sub harmonize {
152sub rescale_widgets { 186sub rescale_widgets {
153 my ($sx, $sy) = @_; 187 my ($sx, $sy) = @_;
154 188
155 for my $widget (values %WIDGET) { 189 for my $widget (values %WIDGET) {
156 if ($widget->{is_toplevel}) { 190 if ($widget->{is_toplevel}) {
191 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
192 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
193
157 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 194 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
158 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 195 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
159 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 196 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
160 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
161 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 197 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
162 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 198 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
163 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 199 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
164 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; 200
201 $widget->{x} -= int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
202 $widget->{y} -= int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
203
165 } 204 }
166 } 205 }
167 206
168 reconfigure_widgets; 207 reconfigure_widgets;
208}
209
210#############################################################################
211
212package CFClient::UI::Event;
213
214sub xy {
215 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
169} 216}
170 217
171############################################################################# 218#############################################################################
172 219
173package CFClient::UI::Base; 220package CFClient::UI::Base;
178 225
179sub new { 226sub new {
180 my $class = shift; 227 my $class = shift;
181 228
182 my $self = bless { 229 my $self = bless {
183 x => 0, 230 x => "center",
184 y => 0, 231 y => "center",
185 z => 0, 232 z => 0,
233 w => undef,
234 h => undef,
186 can_events => 1, 235 can_events => 1,
187 @_ 236 @_
188 }, $class; 237 }, $class;
189 238
239 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
240
190 for (keys %$self) { 241 for (keys %$self) {
191 if (/^connect_(.*)$/) { 242 if (/^on_(.*)$/) {
192 $self->connect ($1 => delete $self->{$_}); 243 $self->connect ($1 => delete $self->{$_});
193 } 244 }
194 } 245 }
195 246
196 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 247 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
248 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{x};
249 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{y};
250 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{w};
251 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{h};
252
253 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
254 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
255
256 $self->show if $layout->{show};
257 }
197 258
198 $self 259 $self
199} 260}
200 261
201sub destroy { 262sub destroy {
205 %$self = (); 266 %$self = ();
206} 267}
207 268
208sub show { 269sub show {
209 my ($self) = @_; 270 my ($self) = @_;
271
210 return if $self->{parent}; 272 return if $self->{parent};
211 273
212 $CFClient::UI::ROOT->add ($self); 274 $CFClient::UI::ROOT->add ($self);
213} 275}
214 276
215sub show_centered { 277sub set_visible {
216 my ($self) = @_; 278 my ($self) = @_;
279
217 return if $self->{parent}; 280 return if $self->{visible};
218 281
219 $self->show; 282 $self->{root} = $self->{parent}{root};
283 $self->{visible} = $self->{parent}{visible} + 1;
220 284
221 $CFClient::UI::ROOT->on_post_alloc ( 285 $self->emit (visibility_change => 1);
222 "centered $self" => sub { 286
223 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 287 $self->realloc if !exists $self->{req_w};
224 }, 288
225 ); 289 $_->set_visible for $self->children;
226} 290}
227 291
228sub set_invisible { 292sub set_invisible {
229 my ($self) = @_; 293 my ($self) = @_;
230 294
231 # broken show/hide model 295 return unless $self->{visible};
296
297 $_->set_invisible for $self->children;
232 298
233 delete $self->{visible}; 299 delete $self->{visible};
300 delete $self->{root};
234 301
235 undef $GRAB if $GRAB == $self; 302 undef $GRAB if $GRAB == $self;
236 undef $HOVER if $HOVER == $self; 303 undef $HOVER if $HOVER == $self;
237 304
238 CFClient::UI::check_tooltip 305 $CFClient::UI::TOOLTIP_WATCHER->cb->()
239 if $CFClient::UI::TOOLTIP->{owner} == $self; 306 if $TOOLTIP->{owner} == $self;
240 307
241 $self->focus_out; 308 $self->emit ("focus_out");
309 $self->emit (visibility_change => 0);
310}
311
312sub set_visibility {
313 my ($self, $visible) = @_;
314
315 return if $self->{visible} == $visible;
316
317 $visible ? $self->hide
318 : $self->show;
319}
320
321sub toggle_visibility {
322 my ($self) = @_;
323
324 $self->{visible}
325 ? $self->hide
326 : $self->show;
242} 327}
243 328
244sub hide { 329sub hide {
245 my ($self) = @_; 330 my ($self) = @_;
246 331
248 333
249 $self->{parent}->remove ($self) 334 $self->{parent}->remove ($self)
250 if $self->{parent}; 335 if $self->{parent};
251} 336}
252 337
253sub move { 338sub move_abs {
254 my ($self, $x, $y, $z) = @_; 339 my ($self, $x, $y, $z) = @_;
255 340
256 $self->{x} = int $x; 341 $self->{x} = List::Util::max 0, List::Util::min $self->{root}{w} - $self->{w}, int $x;
257 $self->{y} = int $y; 342 $self->{y} = List::Util::max 0, List::Util::min $self->{root}{h} - $self->{h}, int $y;
258 $self->{z} = $z if defined $z; 343 $self->{z} = $z if defined $z;
259 344
260 $self->update; 345 $self->update;
261} 346}
262 347
263sub set_size { 348sub set_size {
264 my ($self, $w, $h) = @_; 349 my ($self, $w, $h) = @_;
265 350
266 $self->{user_w} = $w; 351 $self->{force_w} = $w;
267 $self->{user_h} = $h; 352 $self->{force_h} = $h;
268 353
269 $self->check_size; 354 $self->realloc;
270} 355}
271 356
272sub size_request { 357sub size_request {
273 require Carp; 358 require Carp;
274 Carp::confess "size_request is abstract"; 359 Carp::confess "size_request is abstract";
275} 360}
276 361
362sub baseline_shift {
363 0
364}
365
277sub configure { 366sub configure {
278 my ($self, $x, $y, $w, $h) = @_; 367 my ($self, $x, $y, $w, $h) = @_;
279 368
280 if ($self->{aspect}) { 369 if ($self->{aspect}) {
370 my ($ow, $oh) = ($w, $h);
371
281 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 372 $w = List::Util::min $w, int $h * $self->{aspect};
282 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 373 $h = List::Util::min $h, int $w / $self->{aspect};
283 374
284 # use alignment to adjust x, y 375 # use alignment to adjust x, y
285 376
286 $x += int +($w - $w2) * 0.5; 377 $x += int 0.5 * ($ow - $w);
287 $y += int +($h - $h2) * 0.5; 378 $y += int 0.5 * ($oh - $h);
288
289 ($w, $h) = ($w2, $h2);
290 } 379 }
291 380
292 if ($self->{x} != $x || $self->{y} != $y) { 381 if ($self->{x} ne $x || $self->{y} ne $y) {
293 $self->{x} = $x; 382 $self->{x} = $x;
294 $self->{y} = $y; 383 $self->{y} = $y;
295 $self->update; 384 $self->update;
296 } 385 }
297 386
298 if ($self->{w} != $w || $self->{h} != $h) { 387 if ($self->{alloc_w} != $w || $self->{alloc_h} != $h) {
299 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 388 return unless $self->{visible};
300 }
301}
302 389
303sub size_allocate { 390 $self->{alloc_w} = $w;
304 # nothing to be done 391 $self->{alloc_h} = $h;
305}
306 392
307sub reconfigure { 393 $self->{root}{size_alloc}{$self+0} = $self;
308 my ($self) = @_; 394 }
309
310 $self->check_size (1);
311 $self->update;
312} 395}
313 396
314sub children { 397sub children {
398 # nop
399}
400
401sub visible_children {
402 $_[0]->children
315} 403}
316 404
317sub set_max_size { 405sub set_max_size {
318 my ($self, $w, $h) = @_; 406 my ($self, $w, $h) = @_;
319 407
320 delete $self->{max_w}; $self->{max_w} = $w if $w; 408 $self->{max_w} = int $w if defined $w;
321 delete $self->{max_h}; $self->{max_h} = $h if $h; 409 $self->{max_h} = int $h if defined $h;
410
411 $self->realloc;
322} 412}
323 413
324sub set_tooltip { 414sub set_tooltip {
325 my ($self, $tooltip) = @_; 415 my ($self, $tooltip) = @_;
326 416
417 $tooltip =~ s/^\s+//;
418 $tooltip =~ s/\s+$//;
419
420 return if $self->{tooltip} eq $tooltip;
421
327 $self->{tooltip} = $tooltip; 422 $self->{tooltip} = $tooltip;
328 423
329 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 424 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
330 delete $CFClient::UI::TOOLTIP->{owner}; 425 delete $CFClient::UI::TOOLTIP->{owner};
331 CFClient::UI::check_tooltip; 426 $CFClient::UI::TOOLTIP_WATCHER->cb->();
332 } 427 }
333} 428}
334 429
335# translate global coordinates to local coordinate system 430# translate global coordinates to local coordinate system
336sub coord2local { 431sub coord2local {
344 my ($self, $x, $y) = @_; 439 my ($self, $x, $y) = @_;
345 440
346 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 441 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
347} 442}
348 443
349sub focus_in { 444sub invoke_focus_in {
350 my ($self) = @_; 445 my ($self) = @_;
351 446
352 return if $FOCUS == $self; 447 return if $FOCUS == $self;
353 return unless $self->{can_focus}; 448 return unless $self->{can_focus};
354 449
355 my $focus = $FOCUS; $FOCUS = $self; 450 $FOCUS = $self;
356 451
357 $self->_emit (focus_in => $focus); 452 $self->update;
358 453
359 $focus->update if $focus; 454 0
360 $FOCUS->update;
361} 455}
362 456
363sub focus_out { 457sub invoke_focus_out {
364 my ($self) = @_; 458 my ($self) = @_;
365 459
366 return unless $FOCUS == $self; 460 return unless $FOCUS == $self;
367 461
368 my $focus = $FOCUS; undef $FOCUS; 462 undef $FOCUS;
369 463
370 $self->_emit (focus_out => $focus); 464 $self->update;
371 465
372 $focus->update if $focus; #?
373
374 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus 466 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus
375 unless $FOCUS; 467 unless $FOCUS;
376}
377 468
469 0
470}
471
472sub grab_focus {
473 my ($self) = @_;
474
475 $FOCUS->emit ("focus_out") if $FOCUS;
476 $self->emit ("focus_in");
477}
478
378sub mouse_motion { } 479sub invoke_mouse_motion { 0 }
379sub button_up { } 480sub invoke_button_up { 0 }
380sub key_down { } 481sub invoke_key_down { 0 }
381sub key_up { } 482sub invoke_key_up { 0 }
483sub invoke_mouse_wheel { 0 }
382 484
383sub button_down { 485sub invoke_button_down {
384 my ($self, $ev, $x, $y) = @_; 486 my ($self, $ev, $x, $y) = @_;
385 487
386 $self->focus_in; 488 $self->grab_focus;
387}
388 489
389sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 490 0
390sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 491}
391sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 492
392sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 493sub connect {
393sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 494 my ($self, $signal, $cb) = @_;
495
496 push @{ $self->{signal_cb}{$signal} }, $cb;
497}
498
499sub emit {
500 my ($self, $signal, @args) = @_;
501
502 my @append
503 = ref $args[0] && $args[0]->isa ("CFClient::UI::Event")
504 ? $args[0]->xy ($self)
505 : ();
506
507 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
508
509 #d##TODO# stop propagating at first true, do not use sum
510 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before
511 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure
512 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
513}
514
515sub find_widget {
516 my ($self, $x, $y) = @_;
517
518 return () unless $self->{can_events};
519
520 return $self
521 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
522 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
523
524 ()
525}
526
527sub set_parent {
528 my ($self, $parent) = @_;
529
530 Scalar::Util::weaken ($self->{parent} = $parent);
531 $self->set_visible if $parent->{visible};
532}
533
534sub realloc {
535 my ($self) = @_;
536
537 if ($self->{visible}) {
538 return if $self->{root}{realloc}{$self+0};
539
540 $self->{root}{realloc}{$self+0} = $self;
541 $self->{root}->update;
542 } else {
543 delete $self->{req_w};
544 delete $self->{req_h};
545 }
546}
547
548sub update {
549 my ($self) = @_;
550
551 $self->{parent}->update
552 if $self->{parent};
553}
554
555sub reconfigure {
556 my ($self) = @_;
557
558 $self->realloc;
559 $self->update;
560}
561
562# using global variables seems a bit hacky, but passing through all drawing
563# functions seems pointless.
564our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
394 565
395sub draw { 566sub draw {
396 my ($self) = @_; 567 my ($self) = @_;
397 568
398 return unless $self->{h} && $self->{w}; 569 return unless $self->{h} && $self->{w};
570
571 # update screen rectangle
572 local $draw_x = $draw_x + $self->{x};
573 local $draw_y = $draw_y + $self->{y};
574
575 # skip widgets that are entirely outside the drawing area
576 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
577 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
399 578
400 glPushMatrix; 579 glPushMatrix;
401 glTranslate $self->{x}, $self->{y}, 0; 580 glTranslate $self->{x}, $self->{y}, 0;
402 $self->_draw;
403 glPopMatrix;
404 581
405 if ($self == $HOVER && $self->{can_hover}) { 582 if ($self == $HOVER && $self->{can_hover}) {
406 my ($x, $y) = @$self{qw(x y)};
407
408 glColor 1, 0.8, 0.5, 0.2; 583 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
409 glEnable GL_BLEND; 584 glEnable GL_BLEND;
410 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 585 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
411 glBegin GL_QUADS; 586 glBegin GL_QUADS;
412 glVertex $x , $y;
413 glVertex $x + $self->{w}, $y;
414 glVertex $x + $self->{w}, $y + $self->{h};
415 glVertex $x , $y + $self->{h};
416 glEnd;
417 glDisable GL_BLEND;
418 }
419
420 if ($ENV{PCLIENT_DEBUG}) {
421 glPushMatrix;
422 glColor 1, 1, 0, 1;
423 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
424 glBegin GL_LINE_LOOP;
425 glVertex 0 , 0; 587 glVertex 0 , 0;
426 glVertex $self->{w}, 0; 588 glVertex $self->{w}, 0;
427 glVertex $self->{w}, $self->{h}; 589 glVertex $self->{w}, $self->{h};
428 glVertex 0 , $self->{h}; 590 glVertex 0 , $self->{h};
429 glEnd; 591 glEnd;
592 glDisable GL_BLEND;
593 }
594
595 if ($ENV{CFPLUS_DEBUG} & 1) {
596 glPushMatrix;
597 glColor 1, 1, 0, 1;
598 glTranslate 0.375, 0.375;
599 glBegin GL_LINE_LOOP;
600 glVertex 0 , 0;
601 glVertex $self->{w} - 1, 0;
602 glVertex $self->{w} - 1, $self->{h} - 1;
603 glVertex 0 , $self->{h} - 1;
604 glEnd;
430 glPopMatrix; 605 glPopMatrix;
431 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 606 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
432 } 607 }
608
609 $self->_draw;
610 glPopMatrix;
433} 611}
434 612
435sub _draw { 613sub _draw {
436 my ($self) = @_; 614 my ($self) = @_;
437 615
438 warn "no draw defined for $self\n"; 616 warn "no draw defined for $self\n";
439} 617}
440 618
441sub find_widget {
442 my ($self, $x, $y) = @_;
443
444 return () unless $self->{can_events};
445
446 return $self
447 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
448 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
449
450 ()
451}
452
453sub set_parent {
454 my ($self, $parent) = @_;
455
456 Scalar::Util::weaken ($self->{parent} = $parent);
457
458 # TODO: req_w _does_change after ->reconfigure
459 $self->check_size
460 unless exists $self->{req_w};
461
462 $self->show;
463}
464
465sub check_size {
466 my ($self, $forced) = @_;
467
468 $self->{force_alloc} = 1 if $forced;
469 $CFClient::UI::ROOT->{check_size}{$self} = $self;
470}
471
472sub update {
473 my ($self) = @_;
474
475 $self->{parent}->update
476 if $self->{parent};
477}
478
479sub connect {
480 my ($self, $signal, $cb) = @_;
481
482 push @{ $self->{signal_cb}{$signal} }, $cb;
483}
484
485sub _emit {
486 my ($self, $signal, @args) = @_;
487
488 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
489}
490
491sub emit {
492 my ($self, $signal, @args) = @_;
493
494 $self->_emit ($signal, @args)
495 || $self->$signal (@args);
496}
497
498sub DESTROY { 619sub DESTROY {
499 my ($self) = @_; 620 my ($self) = @_;
500 621
622 return if CFClient::in_destruct;
623
501 delete $WIDGET{$self+0}; 624 delete $WIDGET{$self+0};
502 #$self->deactivate; 625
626 eval { $self->destroy };
627 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
503} 628}
504 629
505############################################################################# 630#############################################################################
506 631
507package CFClient::UI::DrawBG; 632package CFClient::UI::DrawBG;
532 657
533 if ($color && (@$color < 4 || $color->[3])) { 658 if ($color && (@$color < 4 || $color->[3])) {
534 my ($w, $h) = @$self{qw(w h)}; 659 my ($w, $h) = @$self{qw(w h)};
535 660
536 glEnable GL_BLEND; 661 glEnable GL_BLEND;
537 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 662 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
538 glColor @$color; 663 glColor_premultiply @$color;
539 664
540 glBegin GL_QUADS; 665 glBegin GL_QUADS;
541 glVertex 0 , 0; 666 glVertex 0 , 0;
542 glVertex 0 , $h; 667 glVertex 0 , $h;
543 glVertex $w, $h; 668 glVertex $w, $h;
558 my ($class, %arg) = @_; 683 my ($class, %arg) = @_;
559 $class->SUPER::new (can_events => 0, %arg); 684 $class->SUPER::new (can_events => 0, %arg);
560} 685}
561 686
562sub size_request { 687sub size_request {
563 (0, 0) 688 my ($self) = @_;
689
690 ($self->{w} + 0, $self->{h} + 0)
564} 691}
565 692
566sub draw { } 693sub draw { }
567 694
568############################################################################# 695#############################################################################
572our @ISA = CFClient::UI::Base::; 699our @ISA = CFClient::UI::Base::;
573 700
574sub new { 701sub new {
575 my ($class, %arg) = @_; 702 my ($class, %arg) = @_;
576 703
577 my $children = delete $arg{children} || []; 704 my $children = delete $arg{children};
578 705
579 my $self = $class->SUPER::new ( 706 my $self = $class->SUPER::new (
580 children => [], 707 children => [],
581 can_events => 0, 708 can_events => 0,
582 %arg, 709 %arg,
583 ); 710 );
711
584 $self->add ($_) for @$children; 712 $self->add (@$children)
713 if $children;
585 714
586 $self 715 $self
587} 716}
588 717
589sub add { 718sub add {
597 $self->{children} = [ 726 $self->{children} = [
598 sort { $a->{z} <=> $b->{z} } 727 sort { $a->{z} <=> $b->{z} }
599 @{$self->{children}}, @widgets 728 @{$self->{children}}, @widgets
600 ]; 729 ];
601 730
602 $self->check_size (1); 731 $self->realloc;
603 $self->update;
604} 732}
605 733
606sub children { 734sub children {
607 @{ $_[0]{children} } 735 @{ $_[0]{children} }
608} 736}
613 delete $child->{parent}; 741 delete $child->{parent};
614 $child->hide; 742 $child->hide;
615 743
616 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 744 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
617 745
618 $self->check_size; 746 $self->realloc;
619 $self->update;
620} 747}
621 748
622sub clear { 749sub clear {
623 my ($self) = @_; 750 my ($self) = @_;
624 751
628 for (@$children) { 755 for (@$children) {
629 delete $_->{parent}; 756 delete $_->{parent};
630 $_->hide; 757 $_->hide;
631 } 758 }
632 759
633 $self->check_size; 760 $self->realloc;
634 $self->update;
635} 761}
636 762
637sub find_widget { 763sub find_widget {
638 my ($self, $x, $y) = @_; 764 my ($self, $x, $y) = @_;
639 765
640 $x -= $self->{x}; 766 $x -= $self->{x};
641 $y -= $self->{y}; 767 $y -= $self->{y};
642 768
643 my $res; 769 my $res;
644 770
645 for (reverse @{ $self->{children} }) { 771 for (reverse $self->visible_children) {
646 $res = $_->find_widget ($x, $y) 772 $res = $_->find_widget ($x, $y)
647 and return $res; 773 and return $res;
648 } 774 }
649 775
650 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) 776 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
671} 797}
672 798
673sub add { 799sub add {
674 my ($self, $child) = @_; 800 my ($self, $child) = @_;
675 801
676 $self->{children} = []; 802 $self->SUPER::remove ($_) for @{ $self->{children} };
677
678 $self->SUPER::add ($child); 803 $self->SUPER::add ($child);
679} 804}
680 805
681sub remove { 806sub remove {
682 my ($self, $widget) = @_; 807 my ($self, $widget) = @_;
691 816
692sub size_request { 817sub size_request {
693 $_[0]{children}[0]->size_request 818 $_[0]{children}[0]->size_request
694} 819}
695 820
696sub size_allocate { 821sub invoke_size_allocate {
697 my ($self, $w, $h) = @_; 822 my ($self, $w, $h) = @_;
698 823
699 $self->{children}[0]->configure (0, 0, $w, $h); 824 $self->{children}[0]->configure (0, 0, $w, $h);
825
826 1
700} 827}
701 828
702############################################################################# 829#############################################################################
830
831# back-buffered drawing area
703 832
704package CFClient::UI::Window; 833package CFClient::UI::Window;
705 834
706our @ISA = CFClient::UI::Bin::; 835our @ISA = CFClient::UI::Bin::;
707 836
718 847
719 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 848 $ROOT->on_post_alloc ($self => sub { $self->render_child });
720 $self->SUPER::update; 849 $self->SUPER::update;
721} 850}
722 851
723sub size_allocate { 852sub invoke_size_allocate {
724 my ($self, $w, $h) = @_; 853 my ($self, $w, $h) = @_;
725 854
726 $self->SUPER::size_allocate ($w, $h);
727 $self->update; 855 $self->update;
856
857 $self->SUPER::invoke_size_allocate ($w, $h)
728} 858}
729 859
730sub _render { 860sub _render {
861 my ($self) = @_;
862
731 $_[0]{children}[0]->draw; 863 $self->{children}[0]->draw;
732} 864}
733 865
734sub render_child { 866sub render_child {
735 my ($self) = @_; 867 my ($self) = @_;
736 868
737 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 869 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
738 glClearColor 0, 0, 0, 0; 870 glClearColor 0, 0, 0, 0;
739 glClear GL_COLOR_BUFFER_BIT; 871 glClear GL_COLOR_BUFFER_BIT;
740 872
873 {
874 package CFClient::UI::Base;
875
876 ($draw_x, $draw_y, $draw_w, $draw_h) =
877 (0, 0, $self->{w}, $self->{h});
878 }
879
741 $self->_render; 880 $self->_render;
742 }; 881 };
743} 882}
744 883
745sub _draw { 884sub _draw {
746 my ($self) = @_; 885 my ($self) = @_;
747 886
748 my ($w, $h) = ($self->w, $self->h); 887 my ($w, $h) = @$self{qw(w h)};
749 888
750 my $tex = $self->{texture} 889 my $tex = $self->{texture}
751 or return; 890 or return;
752 891
753 glEnable GL_TEXTURE_2D; 892 glEnable GL_TEXTURE_2D;
754 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 893 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
755 glColor 1, 1, 1, 1; 894 glColor 0, 0, 0, 1;
756 895
757 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 896 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
758 897
759 glDisable GL_TEXTURE_2D; 898 glDisable GL_TEXTURE_2D;
760} 899}
763 902
764package CFClient::UI::ViewPort; 903package CFClient::UI::ViewPort;
765 904
766our @ISA = CFClient::UI::Window::; 905our @ISA = CFClient::UI::Window::;
767 906
907sub new {
908 my $class = shift;
909
910 $class->SUPER::new (
911 scroll_x => 0,
912 scroll_y => 1,
913 @_,
914 )
915}
916
768sub size_request { 917sub size_request {
769 my ($self) = @_; 918 my ($self) = @_;
770 919
771 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 920 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
772 921
773 @$self{qw(child_w child_h)} 922 $w = 10 if $self->{scroll_x};
774} 923 $h = 10 if $self->{scroll_y};
775 924
925 ($w, $h)
926}
927
776sub size_allocate { 928sub invoke_size_allocate {
777 my ($self, $w, $h) = @_; 929 my ($self, $w, $h) = @_;
778 930
779 my ($cw, $ch) = @$self{qw(child_w child_h)}; 931 my $child = $self->child;
780# $w = $self->{w}; 932
933 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w};
934 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h};
935
781 $self->child->configure (0, 0, $cw, $ch); 936 $self->child->configure (0, 0, $w, $h);
782 $self->update; 937 $self->update;
938
939 1
783} 940}
784 941
785sub set_offset { 942sub set_offset {
786 my ($self, $x, $y) = @_; 943 my ($self, $x, $y) = @_;
787 944
820} 977}
821 978
822sub _render { 979sub _render {
823 my ($self) = @_; 980 my ($self) = @_;
824 981
982 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x};
983 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y};
984
825 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 985 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
826 986
827 $self->SUPER::_render; 987 $self->SUPER::_render;
828} 988}
829 989
832package CFClient::UI::ScrolledWindow; 992package CFClient::UI::ScrolledWindow;
833 993
834our @ISA = CFClient::UI::HBox::; 994our @ISA = CFClient::UI::HBox::;
835 995
836sub new { 996sub new {
837 my $class = shift; 997 my ($class, %arg) = @_;
998
999 my $child = delete $arg{child};
838 1000
839 my $self; 1001 my $self;
840 1002
841 my $slider = new CFClient::UI::Slider 1003 my $slider = new CFClient::UI::Slider
842 vertical => 1, 1004 vertical => 1,
843 range => [0, 0, 1, 0.01], # HACK fix 1005 range => [0, 0, 1, 0.01], # HACK fix
844 connect_changed => sub { 1006 on_changed => sub {
845 $self->{vp}->set_offset (0, $_[1]); 1007 $self->{vp}->set_offset (0, $_[1]);
846 }, 1008 },
847 ; 1009 ;
848 1010
849 $self = $class->SUPER::new ( 1011 $self = $class->SUPER::new (
850 vp => (new CFClient::UI::ViewPort expand => 1), 1012 vp => (new CFClient::UI::ViewPort expand => 1),
1013 can_events => 1,
851 slider => $slider, 1014 slider => $slider,
852 @_, 1015 %arg,
853 ); 1016 );
854 1017
855 $self->{vp}->add ($self->{scrolled});
856 $self->add ($self->{vp});
857 $self->add ($self->{slider}); 1018 $self->SUPER::add ($self->{vp}, $self->{slider});
1019 $self->add ($child) if $child;
858 1020
859 $self 1021 $self
860} 1022}
861 1023
1024#TODO# update range on size_allocate depending on child
1025
1026sub add {
1027 my ($self, $widget) = @_;
1028
1029 $self->{vp}->add ($self->{child} = $widget);
1030}
1031
1032sub invoke_mouse_wheel {
1033 my ($self, $ev) = @_;
1034
1035 return 0 unless $ev->{dy}; # only vertical movements
1036
1037 $self->{slider}->emit (mouse_wheel => $ev);
1038
1039 1
1040}
1041
1042sub update_slider {
1043 my ($self) = @_;
1044
1045 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]);
1046}
1047
1048sub update {
1049 my ($self) = @_;
1050
1051 $self->SUPER::update;
1052
1053 $self->update_slider;
1054}
1055
862sub size_allocate { 1056sub invoke_size_allocate {
863 my ($self, $w, $h) = @_; 1057 my ($self, $w, $h) = @_;
864 1058
1059 $self->update_slider;
1060
865 $self->SUPER::size_allocate ($w, $h); 1061 $self->SUPER::invoke_size_allocate ($w, $h)
866
867 my $child = $self->{vp}->child;
868 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
869} 1062}
870
871#TODO# update range on size_allocate depending on child
872# update viewport offset on scroll
873 1063
874############################################################################# 1064#############################################################################
875 1065
876package CFClient::UI::Frame; 1066package CFClient::UI::Frame;
877 1067
893 1083
894 if ($self->{bg}) { 1084 if ($self->{bg}) {
895 my ($w, $h) = @$self{qw(w h)}; 1085 my ($w, $h) = @$self{qw(w h)};
896 1086
897 glEnable GL_BLEND; 1087 glEnable GL_BLEND;
898 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1088 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
899 glColor @{ $self->{bg} }; 1089 glColor_premultiply @{ $self->{bg} };
900 1090
901 glBegin GL_QUADS; 1091 glBegin GL_QUADS;
902 glVertex 0 , 0; 1092 glVertex 0 , 0;
903 glVertex 0 , $h; 1093 glVertex 0 , $h;
904 glVertex $w, $h; 1094 glVertex $w, $h;
917 1107
918our @ISA = CFClient::UI::Bin::; 1108our @ISA = CFClient::UI::Bin::;
919 1109
920use CFClient::OpenGL; 1110use CFClient::OpenGL;
921 1111
922my @tex = 1112my $bg =
1113 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1114 mipmap => 1, wrap => 1;
1115
1116my @border =
923 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1117 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
924 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1118 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
925 1119
926sub new { 1120sub new {
927 my $class = shift; 1121 my ($class, %arg) = @_;
928
929 # TODO: user_x, user_y, overwrite moveto?
930 1122
931 my $self = $class->SUPER::new ( 1123 my $self = $class->SUPER::new (
932 bg => [1, 1, 1, 1], 1124 bg => [1, 1, 1, 1],
933 border_bg => [1, 1, 1, 1], 1125 border_bg => [1, 1, 1, 1],
934 border => 0.6, 1126 border => 0.6,
935 is_toplevel => 1,
936 can_events => 1, 1127 can_events => 1,
937 @_ 1128 min_w => 64,
1129 min_h => 32,
1130 %arg,
938 ); 1131 );
939 1132
940 $self->{title} &&= new CFClient::UI::Label 1133 $self->{title_widget} = new CFClient::UI::Label
941 align => 0, 1134 align => 0,
942 valign => 1, 1135 valign => 1,
943 text => $self->{title}, 1136 text => $self->{title},
944 fontsize => $self->{border}; 1137 fontsize => $self->{border},
1138 if exists $self->{title};
1139
1140 if ($self->{has_close_button}) {
1141 $self->{close_button} =
1142 new CFClient::UI::ImageButton
1143 path => 'x1_close.png',
1144 on_activate => sub { $self->emit ("delete") };
1145
1146 $self->CFClient::UI::Container::add ($self->{close_button});
1147 }
945 1148
946 $self 1149 $self
1150}
1151
1152sub add {
1153 my ($self, @widgets) = @_;
1154
1155 $self->SUPER::add (@widgets);
1156 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button};
1157 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
947} 1158}
948 1159
949sub border { 1160sub border {
950 int $_[0]{border} * $::FONTSIZE 1161 int $_[0]{border} * $::FONTSIZE
951} 1162}
952 1163
953sub size_request { 1164sub size_request {
954 my ($self) = @_; 1165 my ($self) = @_;
1166
1167 $self->{title_widget}->size_request
1168 if $self->{title_widget};
1169
1170 $self->{close_button}->size_request
1171 if $self->{close_button};
955 1172
956 my ($w, $h) = $self->SUPER::size_request; 1173 my ($w, $h) = $self->SUPER::size_request;
957 1174
958 ( 1175 (
959 $w + $self->border * 2, 1176 $w + $self->border * 2,
960 $h + $self->border * 2, 1177 $h + $self->border * 2,
961 ) 1178 )
962} 1179}
963 1180
964sub size_allocate { 1181sub invoke_size_allocate {
965 my ($self, $w, $h) = @_; 1182 my ($self, $w, $h) = @_;
966 1183
1184 if ($self->{title_widget}) {
1185 $self->{title_widget}{w} = $w;
1186 $self->{title_widget}{h} = $h;
1187 $self->{title_widget}->invoke_size_allocate ($w, $h);
1188 }
1189
1190 my $border = $self->border;
1191
967 $h -= List::Util::max 0, $self->border * 2; 1192 $h -= List::Util::max 0, $border * 2;
968 $w -= List::Util::max 0, $self->border * 2; 1193 $w -= List::Util::max 0, $border * 2;
969 1194
970 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
971 if $self->{title};
972
973 $self->child->configure ($self->border, $self->border, $w, $h); 1195 $self->child->configure ($border, $border, $w, $h);
974}
975 1196
1197 $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border)
1198 if $self->{close_button};
1199
1200 1
1201}
1202
1203sub invoke_delete {
1204 my ($self) = @_;
1205
1206 $self->hide;
1207
1208 1
1209}
1210
976sub button_down { 1211sub invoke_button_down {
977 my ($self, $ev, $x, $y) = @_; 1212 my ($self, $ev, $x, $y) = @_;
978 1213
979 my ($w, $h) = @$self{qw(w h)}; 1214 my ($w, $h) = @$self{qw(w h)};
980 my $border = $self->border; 1215 my $border = $self->border;
981 1216
994 my ($ev, $x, $y) = @_; 1229 my ($ev, $x, $y) = @_;
995 1230
996 my $dx = $ev->{x} - $ox; 1231 my $dx = $ev->{x} - $ox;
997 my $dy = $ev->{y} - $oy; 1232 my $dy = $ev->{y} - $oy;
998 1233
999 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1234 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1000 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1235 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1236
1001 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1237 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1002 $self->check_size; 1238 $self->realloc;
1003 }; 1239 };
1004 1240
1005 } elsif ($lr ^ $td) { 1241 } elsif ($lr ^ $td) {
1006 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1242 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1007 my ($bx, $by) = ($self->{x}, $self->{y}); 1243 my ($bx, $by) = ($self->{x}, $self->{y});
1009 $self->{motion} = sub { 1245 $self->{motion} = sub {
1010 my ($ev, $x, $y) = @_; 1246 my ($ev, $x, $y) = @_;
1011 1247
1012 ($x, $y) = ($ev->{x}, $ev->{y}); 1248 ($x, $y) = ($ev->{x}, $ev->{y});
1013 1249
1014 $self->move ($bx + $x - $ox, $by + $y - $oy); 1250 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1015 $self->update; 1251 # HACK: the next line is required to enforce placement
1252 $self->{parent}->invoke_size_allocate ($self->{parent}{w}, $self->{parent}{h});
1016 }; 1253 };
1254 } else {
1255 return 0;
1256 }
1257
1017 } 1258 1
1018} 1259}
1019 1260
1020sub button_up { 1261sub invoke_button_up {
1021 my ($self, $ev, $x, $y) = @_; 1262 my ($self, $ev, $x, $y) = @_;
1022 1263
1023 delete $self->{motion}; 1264 ! ! delete $self->{motion}
1024} 1265}
1025 1266
1026sub mouse_motion { 1267sub invoke_mouse_motion {
1027 my ($self, $ev, $x, $y) = @_; 1268 my ($self, $ev, $x, $y) = @_;
1028 1269
1029 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1270 $self->{motion}->($ev, $x, $y) if $self->{motion};
1271
1272 ! ! $self->{motion}
1030} 1273}
1031 1274
1032sub _draw { 1275sub _draw {
1033 my ($self) = @_; 1276 my ($self) = @_;
1034 1277
1278 my $child = $self->{children}[0];
1279
1035 my ($w, $h ) = ($self->{w}, $self->{h}); 1280 my ($w, $h ) = ($self->{w}, $self->{h});
1036 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1281 my ($cw, $ch) = ($child->{w}, $child->{h});
1037 1282
1038 glEnable GL_TEXTURE_2D; 1283 glEnable GL_TEXTURE_2D;
1039 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1284 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1040 1285
1041 my $border = $self->border; 1286 my $border = $self->border;
1042 1287
1043 glColor @{ $self->{border_bg} }; 1288 glColor @{ $self->{border_bg} };
1044 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1289 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1045 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1290 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1046 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1291 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1047 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1292 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1048 1293
1049 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1294 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1050 my $bg = $tex[0]; 1295 glColor @{ $self->{bg} };
1051 1296
1052 # TODO: repeat texture not scale 1297 # TODO: repeat texture not scale
1298 # solve this better(?)
1053 my $rep_x = $cw / $bg->{w}; 1299 $bg->{s} = $cw / $bg->{w};
1054 my $rep_y = $ch / $bg->{h}; 1300 $bg->{t} = $ch / $bg->{h};
1055
1056 glColor @{ $self->{bg} };
1057
1058 $bg->{s} = $rep_x;
1059 $bg->{t} = $rep_y;
1060 $bg->{wrap_mode} = 1;
1061 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1301 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1062 } 1302 }
1063 1303
1064 glDisable GL_TEXTURE_2D; 1304 glDisable GL_TEXTURE_2D;
1065 1305
1066 $self->{title}->draw if $self->{title};
1067
1068 $self->child->draw; 1306 $child->draw;
1307
1308 if ($self->{title_widget}) {
1309 glTranslate 0, $border - $self->{h};
1310 $self->{title_widget}->_draw;
1311
1312 glTranslate 0, - ($border - $self->{h});
1313 }
1314
1315 $self->{close_button}->draw
1316 if $self->{close_button};
1069} 1317}
1070 1318
1071############################################################################# 1319#############################################################################
1072 1320
1073package CFClient::UI::Table; 1321package CFClient::UI::Table;
1081sub new { 1329sub new {
1082 my $class = shift; 1330 my $class = shift;
1083 1331
1084 $class->SUPER::new ( 1332 $class->SUPER::new (
1085 col_expand => [], 1333 col_expand => [],
1086 @_ 1334 @_,
1087 ) 1335 )
1336}
1337
1338sub children {
1339 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1088} 1340}
1089 1341
1090sub add { 1342sub add {
1091 my ($self, $x, $y, $child) = @_; 1343 my ($self, $x, $y, $child) = @_;
1092 1344
1093 $child->set_parent ($self); 1345 $child->set_parent ($self);
1094 $self->{children}[$y][$x] = $child; 1346 $self->{children}[$y][$x] = $child;
1095 1347
1096 $child->check_size; 1348 $self->realloc;
1097} 1349}
1098 1350
1099sub children { 1351sub remove {
1100 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1352 my ($self, $child) = @_;
1101}
1102 1353
1354 # TODO: not yet implemented
1355}
1356
1103# TODO: move to container class maybe? send childs a signal on removal? 1357# TODO: move to container class maybe? send children a signal on removal?
1104sub clear { 1358sub clear {
1105 my ($self) = @_; 1359 my ($self) = @_;
1106 1360
1107 my @children = $self->children; 1361 my @children = $self->children;
1108 delete $self->{children}; 1362 delete $self->{children};
1110 for (@children) { 1364 for (@children) {
1111 delete $_->{parent}; 1365 delete $_->{parent};
1112 $_->hide; 1366 $_->hide;
1113 } 1367 }
1114 1368
1115 $self->update; 1369 $self->realloc;
1116} 1370}
1117 1371
1118sub get_wh { 1372sub get_wh {
1119 my ($self) = @_; 1373 my ($self) = @_;
1120 1374
1146 (sum @$ws), 1400 (sum @$ws),
1147 (sum @$hs), 1401 (sum @$hs),
1148 ) 1402 )
1149} 1403}
1150 1404
1151sub size_allocate { 1405sub invoke_size_allocate {
1152 my ($self, $w, $h) = @_; 1406 my ($self, $w, $h) = @_;
1153 1407
1154 my ($ws, $hs) = $self->get_wh; 1408 my ($ws, $hs) = $self->get_wh;
1155 1409
1156 my $req_w = sum @$ws; 1410 my $req_w = (sum @$ws) || 1;
1157 my $req_h = sum @$hs; 1411 my $req_h = (sum @$hs) || 1;
1158 1412
1159 # TODO: nicer code && do row_expand 1413 # TODO: nicer code && do row_expand
1160 my @col_expand = @{$self->{col_expand}}; 1414 my @col_expand = @{$self->{col_expand}};
1161 @col_expand = (1) x @$ws unless @col_expand; 1415 @col_expand = (1) x @$ws unless @col_expand;
1162 my $col_expand = (sum @col_expand) || 1; 1416 my $col_expand = (sum @col_expand) || 1;
1188 } 1442 }
1189 1443
1190 $y += $row_h; 1444 $y += $row_h;
1191 } 1445 }
1192 1446
1447 1
1193} 1448}
1194 1449
1195sub find_widget { 1450sub find_widget {
1196 my ($self, $x, $y) = @_; 1451 my ($self, $x, $y) = @_;
1197 1452
1216 } 1471 }
1217} 1472}
1218 1473
1219############################################################################# 1474#############################################################################
1220 1475
1476package CFClient::UI::Box;
1477
1478our @ISA = CFClient::UI::Container::;
1479
1480sub size_request {
1481 my ($self) = @_;
1482
1483 $self->{vertical}
1484 ? (
1485 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1486 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1487 )
1488 : (
1489 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1490 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1491 )
1492}
1493
1494sub invoke_size_allocate {
1495 my ($self, $w, $h) = @_;
1496
1497 my $space = $self->{vertical} ? $h : $w;
1498 my @children = $self->visible_children;
1499
1500 my @req;
1501
1502 if ($self->{homogeneous}) {
1503 @req = ($space / (@children || 1)) x @children;
1504 } else {
1505 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @children;
1506 my $req = List::Util::sum @req;
1507
1508 if ($req > $space) {
1509 # ah well, not enough space
1510 $_ *= $space / $req for @req;
1511 } else {
1512 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1513
1514 $space = ($space - $req) / $expand; # remaining space to give away
1515
1516 $req[$_] += $space * $children[$_]{expand}
1517 for 0 .. $#children;
1518 }
1519 }
1520
1521 CFClient::UI::harmonize \@req;
1522
1523 my $pos = 0;
1524 for (0 .. $#children) {
1525 my $alloc = $req[$_];
1526 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1527
1528 $pos += $alloc;
1529 }
1530
1531 1
1532}
1533
1534#############################################################################
1535
1221package CFClient::UI::HBox; 1536package CFClient::UI::HBox;
1222 1537
1223# TODO: wrap into common Box base class
1224
1225our @ISA = CFClient::UI::Container::; 1538our @ISA = CFClient::UI::Box::;
1226 1539
1227sub size_request { 1540sub new {
1228 my ($self) = @_; 1541 my $class = shift;
1229 1542
1230 my @alloc = map [$_->size_request], @{$self->{children}}; 1543 $class->SUPER::new (
1231 1544 vertical => 0,
1232 ( 1545 @_,
1233 (List::Util::sum map $_->[0], @alloc),
1234 (List::Util::max map $_->[1], @alloc),
1235 ) 1546 )
1236} 1547}
1237 1548
1238sub size_allocate {
1239 my ($self, $w, $h) = @_;
1240
1241 ($h, $w) = ($w, $h);
1242
1243 my $children = $self->{children};
1244
1245 my @h = map $_->{req_w}, @$children;
1246
1247 my $req_h = List::Util::sum @h;
1248
1249 if ($req_h > $h) {
1250 # ah well, not enough space
1251 $_ *= $h / $req_h for @h;
1252 } else {
1253 my $exp = List::Util::sum map $_->{expand}, @$children;
1254 $exp ||= 1;
1255
1256 for (0 .. $#$children) {
1257 my $child = $children->[$_];
1258
1259 my $alloc_h = $h[$_];
1260 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1261 $h[$_] = $alloc_h;
1262 }
1263 }
1264
1265 CFClient::UI::harmonize \@h;
1266
1267 my $y = 0;
1268 for (0 .. $#$children) {
1269 my $child = $children->[$_];
1270 my $h = $h[$_];
1271 $child->configure ($y, 0, $h, $w);
1272
1273 $y += $h;
1274 }
1275
1276 1
1277}
1278
1279############################################################################# 1549#############################################################################
1280 1550
1281package CFClient::UI::VBox; 1551package CFClient::UI::VBox;
1282 1552
1283# TODO: wrap into common Box base class
1284
1285our @ISA = CFClient::UI::Container::; 1553our @ISA = CFClient::UI::Box::;
1286 1554
1287sub size_request { 1555sub new {
1288 my ($self) = @_; 1556 my $class = shift;
1289 1557
1290 my @alloc = map [$_->size_request], @{$self->{children}}; 1558 $class->SUPER::new (
1291 1559 vertical => 1,
1292 ( 1560 @_,
1293 (List::Util::max map $_->[0], @alloc),
1294 (List::Util::sum map $_->[1], @alloc),
1295 ) 1561 )
1296}
1297
1298sub size_allocate {
1299 my ($self, $w, $h) = @_;
1300
1301 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1302
1303 my $children = $self->{children};
1304
1305 my @h = map $_->{req_h}, @$children;
1306
1307 my $req_h = List::Util::sum @h;
1308
1309 if ($req_h > $h) {
1310 # ah well, not enough space
1311 $_ *= $h / $req_h for @h;
1312 } else {
1313 my $exp = List::Util::sum map $_->{expand}, @$children;
1314 $exp ||= 1;
1315
1316 for (0 .. $#$children) {
1317 my $child = $children->[$_];
1318
1319 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1320 }
1321 }
1322
1323 CFClient::UI::harmonize \@h;
1324
1325 my $y = 0;
1326 for (0 .. $#$children) {
1327 my $child = $children->[$_];
1328 my $h = $h[$_];
1329 $child->configure (0, $y, $w, $h);
1330
1331 $y += $h;
1332 }
1333
1334 1
1335} 1562}
1336 1563
1337############################################################################# 1564#############################################################################
1338 1565
1339package CFClient::UI::Label; 1566package CFClient::UI::Label;
1356 ellipsise => 3, # end 1583 ellipsise => 3, # end
1357 layout => (new CFClient::Layout), 1584 layout => (new CFClient::Layout),
1358 fontsize => 1, 1585 fontsize => 1,
1359 align => -1, 1586 align => -1,
1360 valign => -1, 1587 valign => -1,
1361 padding => 2, 1588 padding_x => 2,
1589 padding_y => 2,
1362 can_events => 0, 1590 can_events => 0,
1363 %arg 1591 %arg
1364 ); 1592 );
1365 1593
1366 if (exists $self->{template}) { 1594 if (exists $self->{template}) {
1393 1621
1394 delete $self->{texture}; 1622 delete $self->{texture};
1395 $self->SUPER::update; 1623 $self->SUPER::update;
1396} 1624}
1397 1625
1626sub realloc {
1627 my ($self) = @_;
1628
1629 delete $self->{ox};
1630 $self->SUPER::realloc;
1631}
1632
1398sub set_text { 1633sub set_text {
1399 my ($self, $text) = @_; 1634 my ($self, $text) = @_;
1400 1635
1401 return if $self->{text} eq "T$text"; 1636 return if $self->{text} eq "T$text";
1402 $self->{text} = "T$text"; 1637 $self->{text} = "T$text";
1403 1638
1404 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1405 $self->{layout}->set_text ($text); 1639 $self->{layout}->set_text ($text);
1406 1640
1641 delete $self->{size_req};
1642 $self->realloc;
1407 $self->update; 1643 $self->update;
1408 $self->check_size;
1409} 1644}
1410 1645
1411sub set_markup { 1646sub set_markup {
1412 my ($self, $markup) = @_; 1647 my ($self, $markup) = @_;
1413 1648
1414 return if $self->{text} eq "M$markup"; 1649 return if $self->{text} eq "M$markup";
1415 $self->{text} = "M$markup"; 1650 $self->{text} = "M$markup";
1416 1651
1417 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1652 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1418 1653
1419 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1420 $self->{layout}->set_markup ($markup); 1654 $self->{layout}->set_markup ($markup);
1421 1655
1656 delete $self->{size_req};
1657 $self->realloc;
1422 $self->update; 1658 $self->update;
1423 $self->check_size;
1424} 1659}
1425 1660
1426sub size_request { 1661sub size_request {
1427 my ($self) = @_; 1662 my ($self) = @_;
1428 1663
1664 $self->{size_req} ||= do {
1429 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1665 $self->{layout}->set_font ($self->{font}) if $self->{font};
1430 $self->{layout}->set_width ($self->{max_w} || -1); 1666 $self->{layout}->set_width ($self->{max_w} || -1);
1431 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1667 $self->{layout}->set_ellipsise ($self->{ellipsise});
1432 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1668 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1433 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1669 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1434 1670
1435 my ($w, $h) = $self->{layout}->size; 1671 my ($w, $h) = $self->{layout}->size;
1436 1672
1437 if (exists $self->{template}) { 1673 if (exists $self->{template}) {
1438 $self->{template}->set_font ($self->{font}) if $self->{font}; 1674 $self->{template}->set_font ($self->{font}) if $self->{font};
1675 $self->{template}->set_width ($self->{max_w} || -1);
1439 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1676 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1440 1677
1441 my ($w2, $h2) = $self->{template}->size; 1678 my ($w2, $h2) = $self->{template}->size;
1442 1679
1443 $w = List::Util::max $w, $w2; 1680 $w = List::Util::max $w, $w2;
1444 $h = List::Util::max $h, $h2; 1681 $h = List::Util::max $h, $h2;
1682 }
1683
1684 [$w, $h]
1445 } 1685 };
1446 1686
1447 ( 1687 @{ $self->{size_req} }
1448 $w + $self->{padding} * 2,
1449 $h + $self->{padding} * 2,
1450 )
1451} 1688}
1452 1689
1690sub baseline_shift {
1691 $_[0]{layout}->descent
1692}
1693
1453sub size_allocate { 1694sub invoke_size_allocate {
1454 my ($self, $w, $h) = @_; 1695 my ($self, $w, $h) = @_;
1455 1696
1697 delete $self->{ox};
1698
1456 delete $self->{texture}; 1699 delete $self->{texture}
1700 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1701
1702 1
1457} 1703}
1458 1704
1459sub set_fontsize { 1705sub set_fontsize {
1460 my ($self, $fontsize) = @_; 1706 my ($self, $fontsize) = @_;
1461 1707
1462 $self->{fontsize} = $fontsize; 1708 $self->{fontsize} = $fontsize;
1709 delete $self->{size_req};
1463 delete $self->{texture}; 1710 delete $self->{texture};
1464 1711
1465 $self->update; 1712 $self->realloc;
1466 $self->check_size; 1713}
1714
1715sub reconfigure {
1716 my ($self) = @_;
1717
1718 delete $self->{size_req};
1719 delete $self->{texture};
1720
1721 $self->SUPER::reconfigure;
1467} 1722}
1468 1723
1469sub _draw { 1724sub _draw {
1470 my ($self) = @_; 1725 my ($self) = @_;
1471 1726
1472 $self->SUPER::_draw; # draw background, if applicable 1727 $self->SUPER::_draw; # draw background, if applicable
1473 1728
1474 my $tex = $self->{texture} ||= do { 1729 my $size = $self->{texture} ||= do {
1475 $self->{layout}->set_foreground (@{$self->{fg}}); 1730 $self->{layout}->set_foreground (@{$self->{fg}});
1476 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1731 $self->{layout}->set_font ($self->{font}) if $self->{font};
1477 $self->{layout}->set_width ($self->{w}); 1732 $self->{layout}->set_width ($self->{w});
1478 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1733 $self->{layout}->set_ellipsise ($self->{ellipsise});
1479 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1734 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1480 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1735 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1481 1736
1482 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1737 [$self->{layout}->size]
1483
1484 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1485 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1486 : ($self->{w} - $tex->{w}) * 0.5);
1487
1488 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1489 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1490 : ($self->{h} - $tex->{h}) * 0.5);
1491
1492 $tex
1493 }; 1738 };
1494 1739
1495 glEnable GL_TEXTURE_2D; 1740 unless (exists $self->{ox}) {
1496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1741 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1742 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x}
1743 : ($self->{w} - $size->[0]) * 0.5);
1497 1744
1498 if ($tex->{format} == GL_ALPHA) { 1745 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1499 glColor @{$self->{fg}}; 1746 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1500 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); 1747 : ($self->{h} - $size->[1]) * 0.5);
1501 } else {
1502 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1503 } 1748 };
1504 1749
1505 glDisable GL_TEXTURE_2D; 1750 my $w = List::Util::min $self->{w} + 4, $size->[0];
1751 my $h = List::Util::min $self->{h} + 2, $size->[1];
1752
1753 $self->{layout}->render ($self->{ox}, $self->{oy});
1506} 1754}
1507 1755
1508############################################################################# 1756#############################################################################
1509 1757
1510package CFClient::UI::EntryBase; 1758package CFClient::UI::EntryBase;
1524 can_hover => 1, 1772 can_hover => 1,
1525 can_focus => 1, 1773 can_focus => 1,
1526 valign => 0, 1774 valign => 0,
1527 can_events => 1, 1775 can_events => 1,
1528 #text => ... 1776 #text => ...
1777 #hidden => "*",
1529 @_ 1778 @_
1530 ) 1779 )
1531} 1780}
1532 1781
1533sub _set_text { 1782sub _set_text {
1535 1784
1536 delete $self->{cur_h}; 1785 delete $self->{cur_h};
1537 1786
1538 return if $self->{text} eq $text; 1787 return if $self->{text} eq $text;
1539 1788
1540 delete $self->{texture};
1541
1542 $self->{last_activity} = $::NOW; 1789 $self->{last_activity} = $::NOW;
1543 $self->{text} = $text; 1790 $self->{text} = $text;
1544 1791
1545 $text =~ s/./*/g if $self->{hidden}; 1792 $text =~ s/./*/g if $self->{hidden};
1546 $self->{layout}->set_text ("$text "); 1793 $self->{layout}->set_text ("$text ");
1794 delete $self->{size_req};
1547 1795
1548 $self->_emit (changed => $self->{text}); 1796 $self->emit (changed => $self->{text});
1797
1798 $self->realloc;
1799 $self->update;
1549} 1800}
1550 1801
1551sub set_text { 1802sub set_text {
1552 my ($self, $text) = @_; 1803 my ($self, $text) = @_;
1553 1804
1554 $self->{cursor} = length $text; 1805 $self->{cursor} = length $text;
1555 $self->_set_text ($text); 1806 $self->_set_text ($text);
1556 $self->update;
1557 $self->check_size;
1558} 1807}
1559 1808
1560sub get_text { 1809sub get_text {
1561 $_[0]{text} 1810 $_[0]{text}
1562} 1811}
1567 my ($w, $h) = $self->SUPER::size_request; 1816 my ($w, $h) = $self->SUPER::size_request;
1568 1817
1569 ($w + 1, $h) # add 1 for cursor 1818 ($w + 1, $h) # add 1 for cursor
1570} 1819}
1571 1820
1572sub key_down { 1821sub invoke_key_down {
1573 my ($self, $ev) = @_; 1822 my ($self, $ev) = @_;
1574 1823
1575 my $mod = $ev->{mod}; 1824 my $mod = $ev->{mod};
1576 my $sym = $ev->{sym}; 1825 my $sym = $ev->{sym};
1577 my $uni = $ev->{unicode}; 1826 my $uni = $ev->{unicode};
1589 } elsif ($sym == CFClient::SDLK_HOME) { 1838 } elsif ($sym == CFClient::SDLK_HOME) {
1590 $self->{cursor} = 0; 1839 $self->{cursor} = 0;
1591 } elsif ($sym == CFClient::SDLK_END) { 1840 } elsif ($sym == CFClient::SDLK_END) {
1592 $self->{cursor} = length $text; 1841 $self->{cursor} = length $text;
1593 } elsif ($uni == 27) { 1842 } elsif ($uni == 27) {
1594 $self->_emit ('escape'); 1843 $self->emit ('escape');
1595 } elsif ($uni) { 1844 } elsif ($uni) {
1596 substr $text, $self->{cursor}++, 0, chr $uni; 1845 substr $text, $self->{cursor}++, 0, chr $uni;
1846 } else {
1847 return 0;
1597 } 1848 }
1598 1849
1599 $self->_set_text ($text); 1850 $self->_set_text ($text);
1600 $self->update;
1601 $self->check_size;
1602}
1603 1851
1852 $self->realloc;
1853
1854 1
1855}
1856
1604sub focus_in { 1857sub invoke_focus_in {
1605 my ($self) = @_; 1858 my ($self) = @_;
1606 1859
1607 $self->{last_activity} = $::NOW; 1860 $self->{last_activity} = $::NOW;
1608 1861
1609 $self->SUPER::focus_in; 1862 $self->SUPER::invoke_focus_in
1610} 1863}
1611 1864
1612sub button_down { 1865sub invoke_button_down {
1613 my ($self, $ev, $x, $y) = @_; 1866 my ($self, $ev, $x, $y) = @_;
1614 1867
1615 $self->SUPER::button_down ($ev, $x, $y); 1868 $self->SUPER::invoke_button_down ($ev, $x, $y);
1616 1869
1617 my $idx = $self->{layout}->xy_to_index ($x, $y); 1870 my $idx = $self->{layout}->xy_to_index ($x, $y);
1618 1871
1619 # byte-index to char-index 1872 # byte-index to char-index
1620 my $text = $self->{text}; 1873 my $text = $self->{text};
1621 utf8::encode $text; 1874 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1622 $self->{cursor} = length substr $text, 0, $idx; 1875 $self->{cursor} = length $text;
1623 1876
1624 $self->_set_text ($self->{text}); 1877 $self->_set_text ($self->{text});
1625 $self->update; 1878 $self->update;
1879
1880 1
1626} 1881}
1627 1882
1628sub mouse_motion { 1883sub invoke_mouse_motion {
1629 my ($self, $ev, $x, $y) = @_; 1884 my ($self, $ev, $x, $y) = @_;
1630# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1885# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1886
1887 1
1631} 1888}
1632 1889
1633sub _draw { 1890sub _draw {
1634 my ($self) = @_; 1891 my ($self) = @_;
1635 1892
1636 local $self->{fg} = $self->{fg}; 1893 local $self->{fg} = $self->{fg};
1637 1894
1638 if ($FOCUS == $self) { 1895 if ($FOCUS == $self) {
1639 glColor @{$self->{active_bg}}; 1896 glColor_premultiply @{$self->{active_bg}};
1640 $self->{fg} = $self->{active_fg}; 1897 $self->{fg} = $self->{active_fg};
1641 } else { 1898 } else {
1642 glColor @{$self->{bg}}; 1899 glColor_premultiply @{$self->{bg}};
1643 } 1900 }
1644 1901
1645 glEnable GL_BLEND; 1902 glEnable GL_BLEND;
1646 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1903 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1647 glBegin GL_QUADS; 1904 glBegin GL_QUADS;
1648 glVertex 0 , 0; 1905 glVertex 0 , 0;
1649 glVertex 0 , $self->{h}; 1906 glVertex 0 , $self->{h};
1650 glVertex $self->{w}, $self->{h}; 1907 glVertex $self->{w}, $self->{h};
1651 glVertex $self->{w}, 0; 1908 glVertex $self->{w}, 0;
1676 1933
1677our @ISA = CFClient::UI::EntryBase::; 1934our @ISA = CFClient::UI::EntryBase::;
1678 1935
1679use CFClient::OpenGL; 1936use CFClient::OpenGL;
1680 1937
1681sub key_down { 1938sub invoke_key_down {
1682 my ($self, $ev) = @_; 1939 my ($self, $ev) = @_;
1683 1940
1684 my $sym = $ev->{sym}; 1941 my $sym = $ev->{sym};
1685 1942
1686 if ($sym == 13) { 1943 if ($sym == 13) {
1687 unshift @{$self->{history}}, 1944 unshift @{$self->{history}},
1688 my $txt = $self->get_text; 1945 my $txt = $self->get_text;
1946
1689 $self->{history_pointer} = -1; 1947 $self->{history_pointer} = -1;
1690 $self->{history_saveback} = ''; 1948 $self->{history_saveback} = '';
1691 $self->_emit (activate => $txt); 1949 $self->emit (activate => $txt);
1692 $self->update; 1950 $self->update;
1693 1951
1694 } elsif ($sym == CFClient::SDLK_UP) { 1952 } elsif ($sym == CFClient::SDLK_UP) {
1695 if ($self->{history_pointer} < 0) { 1953 if ($self->{history_pointer} < 0) {
1696 $self->{history_saveback} = $self->get_text; 1954 $self->{history_saveback} = $self->get_text;
1712 } else { 1970 } else {
1713 $self->set_text ($self->{history_saveback}); 1971 $self->set_text ($self->{history_saveback});
1714 } 1972 }
1715 1973
1716 } else { 1974 } else {
1717 $self->SUPER::key_down ($ev); 1975 return $self->SUPER::invoke_key_down ($ev)
1976 }
1977
1718 } 1978 1
1719
1720} 1979}
1721 1980
1722############################################################################# 1981#############################################################################
1723 1982
1724package CFClient::UI::Button; 1983package CFClient::UI::Button;
1733 1992
1734sub new { 1993sub new {
1735 my $class = shift; 1994 my $class = shift;
1736 1995
1737 $class->SUPER::new ( 1996 $class->SUPER::new (
1738 padding => 4, 1997 padding_x => 4,
1998 padding_y => 4,
1739 fg => [1, 1, 1], 1999 fg => [1, 1, 1],
1740 active_fg => [0, 0, 1], 2000 active_fg => [0, 0, 1],
1741 can_hover => 1, 2001 can_hover => 1,
1742 align => 0, 2002 align => 0,
1743 valign => 0, 2003 valign => 0,
1744 can_events => 1, 2004 can_events => 1,
1745 @_ 2005 @_
1746 ) 2006 )
1747} 2007}
1748 2008
1749sub activate { }
1750
1751sub button_up { 2009sub invoke_button_up {
1752 my ($self, $ev, $x, $y) = @_; 2010 my ($self, $ev, $x, $y) = @_;
1753 2011
1754 $self->emit ("activate") 2012 $self->emit ("activate")
1755 if $x >= 0 && $x < $self->{w} 2013 if $x >= 0 && $x < $self->{w}
1756 && $y >= 0 && $y < $self->{h}; 2014 && $y >= 0 && $y < $self->{h};
2015
2016 1
1757} 2017}
1758 2018
1759sub _draw { 2019sub _draw {
1760 my ($self) = @_; 2020 my ($self) = @_;
1761 2021
1762 local $self->{fg} = $self->{fg}; 2022 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
1763
1764 if ($GRAB == $self) {
1765 $self->{fg} = $self->{active_fg};
1766 }
1767 2023
1768 glEnable GL_TEXTURE_2D; 2024 glEnable GL_TEXTURE_2D;
1769 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2025 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1770 glColor 0, 0, 0, 1; 2026 glColor 0, 0, 0, 1;
1771 2027
1776 $self->SUPER::_draw; 2032 $self->SUPER::_draw;
1777} 2033}
1778 2034
1779############################################################################# 2035#############################################################################
1780 2036
2037package CFClient::UI::ImageButton;
2038
2039our @ISA = CFClient::UI::Image::;
2040
2041use CFClient::OpenGL;
2042
2043my %textures;
2044
2045sub new {
2046 my $class = shift;
2047
2048 my $self = $class->SUPER::new (
2049 padding_x => 4,
2050 padding_y => 4,
2051 fg => [1, 1, 1],
2052 active_fg => [0, 0, 1],
2053 can_hover => 1,
2054 align => 0,
2055 valign => 0,
2056 can_events => 1,
2057 @_
2058 );
2059}
2060
2061sub invoke_button_up {
2062 my ($self, $ev, $x, $y) = @_;
2063
2064 $self->emit ("activate")
2065 if $x >= 0 && $x < $self->{w}
2066 && $y >= 0 && $y < $self->{h};
2067
2068 1
2069}
2070
2071#############################################################################
2072
1781package CFClient::UI::CheckBox; 2073package CFClient::UI::CheckBox;
1782 2074
1783our @ISA = CFClient::UI::DrawBG::; 2075our @ISA = CFClient::UI::DrawBG::;
1784 2076
1785my @tex = 2077my @tex =
1790 2082
1791sub new { 2083sub new {
1792 my $class = shift; 2084 my $class = shift;
1793 2085
1794 $class->SUPER::new ( 2086 $class->SUPER::new (
1795 padding => 2, 2087 padding_x => 2,
2088 padding_y => 2,
1796 fg => [1, 1, 1], 2089 fg => [1, 1, 1],
1797 active_fg => [1, 1, 0], 2090 active_fg => [1, 1, 0],
1798 bg => [0, 0, 0, 0.2], 2091 bg => [0, 0, 0, 0.2],
1799 active_bg => [1, 1, 1, 0.5], 2092 active_bg => [1, 1, 1, 0.5],
1800 state => 0, 2093 state => 0,
1804} 2097}
1805 2098
1806sub size_request { 2099sub size_request {
1807 my ($self) = @_; 2100 my ($self) = @_;
1808 2101
1809 ($self->{padding} * 2 + 6) x 2 2102 (6) x 2
1810} 2103}
1811 2104
2105sub toggle {
2106 my ($self) = @_;
2107
2108 $self->{state} = !$self->{state};
2109 $self->emit (changed => $self->{state});
2110 $self->update;
2111}
2112
1812sub button_down { 2113sub invoke_button_down {
1813 my ($self, $ev, $x, $y) = @_; 2114 my ($self, $ev, $x, $y) = @_;
1814 2115
1815 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 2116 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1816 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 2117 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1817 $self->{state} = !$self->{state}; 2118 $self->toggle;
1818 $self->_emit (changed => $self->{state}); 2119 } else {
2120 return 0
2121 }
2122
1819 } 2123 1
1820} 2124}
1821 2125
1822sub _draw { 2126sub _draw {
1823 my ($self) = @_; 2127 my ($self) = @_;
1824 2128
1825 $self->SUPER::_draw; 2129 $self->SUPER::_draw;
1826 2130
1827 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 2131 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
1828 2132
1829 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 2133 my ($w, $h) = @$self{qw(w h)};
2134
2135 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
1830 2136
1831 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 2137 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1832 2138
1833 my $tex = $self->{state} ? $tex[1] : $tex[0]; 2139 my $tex = $self->{state} ? $tex[1] : $tex[0];
1834 2140
1842package CFClient::UI::Image; 2148package CFClient::UI::Image;
1843 2149
1844our @ISA = CFClient::UI::Base::; 2150our @ISA = CFClient::UI::Base::;
1845 2151
1846use CFClient::OpenGL; 2152use CFClient::OpenGL;
1847use Carp qw/confess/;
1848 2153
1849our %loaded_images; 2154our %texture_cache;
1850 2155
1851sub new { 2156sub new {
1852 my $class = shift; 2157 my $class = shift;
1853 2158
1854 my $self = $class->SUPER::new (can_events => 0, @_); 2159 my $self = $class->SUPER::new (
2160 can_events => 0,
2161 @_,
2162 );
1855 2163
1856 $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; 2164 $self->{path} || $self->{tex}
2165 or Carp::croak "'path' or 'tex' attributes required";
1857 2166
1858 $loaded_images{$self->{image}} ||= 2167 $self->{tex} ||= $texture_cache{$self->{path}} ||=
1859 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; 2168 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
1860 2169
1861 my $tex = $self->{tex} = $loaded_images{$self->{image}}; 2170 Scalar::Util::weaken $texture_cache{$self->{path}};
1862 2171
1863 Scalar::Util::weaken $loaded_images{$self->{image}}; 2172 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
1864
1865 $self->{aspect} = $tex->{w} / $tex->{h};
1866 2173
1867 $self 2174 $self
1868} 2175}
1869 2176
1870sub size_request { 2177sub size_request {
1871 my ($self) = @_; 2178 my ($self) = @_;
1872 2179
1873 ($self->{tex}->{w}, $self->{tex}->{h}) 2180 ($self->{tex}{w}, $self->{tex}{h})
1874} 2181}
1875 2182
1876sub _draw { 2183sub _draw {
1877 my ($self) = @_; 2184 my ($self) = @_;
1878 2185
1987 my $ycut1 = max 0, min 1, $ycut; 2294 my $ycut1 = max 0, min 1, $ycut;
1988 my $ycut2 = max 0, min 1, $ycut - 1; 2295 my $ycut2 = max 0, min 1, $ycut - 1;
1989 2296
1990 my $h1 = $self->{h} * (1 - $ycut1); 2297 my $h1 = $self->{h} * (1 - $ycut1);
1991 my $h2 = $self->{h} * (1 - $ycut2); 2298 my $h2 = $self->{h} * (1 - $ycut2);
2299 my $h3 = $self->{h};
2300
2301 $_ = $_ * (284-4)/288 + 4/288 for ($h1, $h2, $h3);
1992 2302
1993 glEnable GL_BLEND; 2303 glEnable GL_BLEND;
1994 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2304 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2305 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1995 glEnable GL_TEXTURE_2D; 2306 glEnable GL_TEXTURE_2D;
1996 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2307 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1997 2308
1998 glBindTexture GL_TEXTURE_2D, $t1->{name}; 2309 glBindTexture GL_TEXTURE_2D, $t1->{name};
1999 glBegin GL_QUADS; 2310 glBegin GL_QUADS;
2014 2325
2015 if ($t3) { 2326 if ($t3) {
2016 glBindTexture GL_TEXTURE_2D, $t3->{name}; 2327 glBindTexture GL_TEXTURE_2D, $t3->{name};
2017 glBegin GL_QUADS; 2328 glBegin GL_QUADS;
2018 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; 2329 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
2019 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; 2330 glTexCoord 0 , $t3->{t}; glVertex 0 , $h3;
2020 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; 2331 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $h3;
2021 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; 2332 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
2022 glEnd; 2333 glEnd;
2023 } 2334 }
2024 2335
2025 glDisable GL_BLEND; 2336 glDisable GL_BLEND;
2099 fg => [1, 1, 1], 2410 fg => [1, 1, 1],
2100 active_fg => [0, 0, 0], 2411 active_fg => [0, 0, 0],
2101 bg => [0, 0, 0, 0.2], 2412 bg => [0, 0, 0, 0.2],
2102 active_bg => [1, 1, 1, 0.5], 2413 active_bg => [1, 1, 1, 0.5],
2103 range => [0, 0, 100, 10, 0], 2414 range => [0, 0, 100, 10, 0],
2104 req_w => $::WIDTH / 80, 2415 min_w => $::WIDTH / 80,
2105 req_h => $::WIDTH / 80, 2416 min_h => $::WIDTH / 80,
2106 vertical => 0, 2417 vertical => 0,
2107 can_hover => 1, 2418 can_hover => 1,
2108 inner_pad => 0.02, 2419 inner_pad => 0.02,
2109 @_ 2420 @_
2110 ); 2421 );
2116} 2427}
2117 2428
2118sub set_range { 2429sub set_range {
2119 my ($self, $range) = @_; 2430 my ($self, $range) = @_;
2120 2431
2121 $self->{range} = $range; 2432 ($range, $self->{range}) = ($self->{range}, $range);
2122 2433
2434 if ("@$range" ne "@{$self->{range}}") {
2123 $self->update; 2435 $self->update;
2436 $self->set_value ($self->{range}[0]);
2437 }
2124} 2438}
2125 2439
2126sub set_value { 2440sub set_value {
2127 my ($self, $value) = @_; 2441 my ($self, $value) = @_;
2128 2442
2139 if $unit; 2453 if $unit;
2140 2454
2141 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2455 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2142 2456
2143 if ($value != $old_value) { 2457 if ($value != $old_value) {
2144 $self->_emit (changed => $value); 2458 $self->emit (changed => $value);
2145 $self->update; 2459 $self->update;
2146 } 2460 }
2147} 2461}
2148 2462
2149sub size_request { 2463sub size_request {
2150 my ($self) = @_; 2464 my ($self) = @_;
2151 2465
2152 my $w = $self->{req_w}; 2466 ($self->{req_w}, $self->{req_h})
2153 my $h = $self->{req_h};
2154
2155 $self->{vertical} ? ($h, $w) : ($w, $h)
2156} 2467}
2157 2468
2158sub button_down { 2469sub invoke_button_down {
2159 my ($self, $ev, $x, $y) = @_; 2470 my ($self, $ev, $x, $y) = @_;
2160 2471
2161 $self->SUPER::button_down ($ev, $x, $y); 2472 $self->SUPER::invoke_button_down ($ev, $x, $y);
2162 2473
2163 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2474 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2164 2475
2165 $self->mouse_motion ($ev, $x, $y); 2476 $self->invoke_mouse_motion ($ev, $x, $y)
2166} 2477}
2167 2478
2168sub mouse_motion { 2479sub invoke_mouse_motion {
2169 my ($self, $ev, $x, $y) = @_; 2480 my ($self, $ev, $x, $y) = @_;
2170 2481
2171 if ($GRAB == $self) { 2482 if ($GRAB == $self) {
2172 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2483 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2173 2484
2174 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2485 my (undef, $lo, $hi, $page) = @{$self->{range}};
2175 2486
2176 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 2487 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2177 2488
2178 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 2489 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2490 } else {
2491 return 0;
2492 }
2493
2179 } 2494 1
2495}
2496
2497sub invoke_mouse_wheel {
2498 my ($self, $ev) = @_;
2499
2500 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2501
2502 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1);
2503
2504 ! ! $delta
2180} 2505}
2181 2506
2182sub update { 2507sub update {
2183 my ($self) = @_; 2508 my ($self) = @_;
2184 2509
2185 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2510 delete $self->{knob_w};
2511 $self->SUPER::update;
2512}
2513
2514sub _draw {
2515 my ($self) = @_;
2516
2517 unless ($self->{knob_w}) {
2186 $self->set_value ($self->{range}[0]); 2518 $self->set_value ($self->{range}[0]);
2187 2519
2188 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2520 my ($value, $lo, $hi, $page) = @{$self->{range}};
2189 my $range = ($hi - $page - $lo) || 1e-100; 2521 my $range = ($hi - $page - $lo) || 1e-100;
2190 2522
2196 $value = ($value - $lo) / $range; 2528 $value = ($value - $lo) / $range;
2197 $value = $value * $self->{scale} + $self->{offset}; 2529 $value = $value * $self->{scale} + $self->{offset};
2198 2530
2199 $self->{knob_x} = $value - $knob_w * 0.5; 2531 $self->{knob_x} = $value - $knob_w * 0.5;
2200 $self->{knob_w} = $knob_w; 2532 $self->{knob_w} = $knob_w;
2201 }); 2533 }
2202
2203 $self->SUPER::update;
2204}
2205
2206sub _draw {
2207 my ($self) = @_;
2208 2534
2209 $self->SUPER::_draw (); 2535 $self->SUPER::_draw ();
2210 2536
2211 glScale $self->{w}, $self->{h}; 2537 glScale $self->{w}, $self->{h};
2212 2538
2273sub set_range { shift->{slider}->set_range (@_) } 2599sub set_range { shift->{slider}->set_range (@_) }
2274sub set_value { shift->{slider}->set_value (@_) } 2600sub set_value { shift->{slider}->set_value (@_) }
2275 2601
2276############################################################################# 2602#############################################################################
2277 2603
2278package CFClient::UI::TextView; 2604package CFClient::UI::TextScroller;
2279 2605
2280our @ISA = CFClient::UI::HBox::; 2606our @ISA = CFClient::UI::HBox::;
2281 2607
2282use CFClient::OpenGL; 2608use CFClient::OpenGL;
2283 2609
2284sub new { 2610sub new {
2285 my $class = shift; 2611 my $class = shift;
2286 2612
2287 my $self = $class->SUPER::new ( 2613 my $self = $class->SUPER::new (
2288 fontsize => 1, 2614 fontsize => 1,
2289 can_events => 0, 2615 can_events => 1,
2616 indent => 0,
2290 #font => default_font 2617 #font => default_font
2291 @_, 2618 @_,
2292 2619
2293 layout => (new CFClient::Layout 1), 2620 layout => (new CFClient::Layout),
2294 par => [], 2621 par => [],
2295 height => 0, 2622 height => 0,
2296 children => [ 2623 children => [
2297 (new CFClient::UI::Empty expand => 1), 2624 (new CFClient::UI::Empty expand => 1),
2298 (new CFClient::UI::Slider vertical => 1), 2625 (new CFClient::UI::Slider vertical => 1),
2309 2636
2310 $self->{fontsize} = $fontsize; 2637 $self->{fontsize} = $fontsize;
2311 $self->reflow; 2638 $self->reflow;
2312} 2639}
2313 2640
2641sub size_request {
2642 my ($self) = @_;
2643
2644 my ($empty, $slider) = @{ $self->{children} };
2645
2646 local $self->{children} = [$empty, $slider];
2647 $self->SUPER::size_request
2648}
2649
2314sub size_allocate { 2650sub invoke_size_allocate {
2315 my ($self, $w, $h) = @_; 2651 my ($self, $w, $h) = @_;
2316 2652
2317 $self->SUPER::size_allocate ($w, $h); 2653 my ($empty, $slider, @other) = @{ $self->{children} };
2654 $_->configure (@$_{qw(x y req_w req_h)}) for @other;
2318 2655
2319 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2656 $self->{layout}->set_font ($self->{font}) if $self->{font};
2320 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2657 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2321 $self->{layout}->set_width ($self->{children}[0]{w}); 2658 $self->{layout}->set_width ($empty->{w});
2659 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2322 2660
2323 $self->reflow; 2661 $self->reflow;
2324}
2325 2662
2326sub text_size { 2663 local $self->{children} = [$empty, $slider];
2664 $self->SUPER::invoke_size_allocate ($w, $h)
2665}
2666
2667sub invoke_mouse_wheel {
2327 my ($self, $text, $indent) = @_; 2668 my ($self, $ev) = @_;
2669
2670 return 0 unless $ev->{dy}; # only vertical movements
2671
2672 $self->{children}[1]->emit (mouse_wheel => $ev);
2673
2674 1
2675}
2676
2677sub get_layout {
2678 my ($self, $para) = @_;
2328 2679
2329 my $layout = $self->{layout}; 2680 my $layout = $self->{layout};
2330 2681
2682 $layout->set_font ($self->{font}) if $self->{font};
2683 $layout->set_foreground (@{$para->{fg}});
2331 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2684 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2332 $layout->set_width ($self->{children}[0]{w} - $indent); 2685 $layout->set_width ($self->{children}[0]{w} - $para->{indent});
2686 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2333 $layout->set_markup ($text); 2687 $layout->set_markup ($para->{markup});
2688
2689 $layout->set_shapes (
2690 map
2691 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
2692 @{$para->{widget}}
2334 2693 );
2694
2335 $layout->size 2695 $layout
2336} 2696}
2337 2697
2338sub reflow { 2698sub reflow {
2339 my ($self) = @_; 2699 my ($self) = @_;
2340 2700
2349 $self->{children}[1]->set_value ($offset); 2709 $self->{children}[1]->set_value ($offset);
2350} 2710}
2351 2711
2352sub clear { 2712sub clear {
2353 my ($self) = @_; 2713 my ($self) = @_;
2714
2715 my (undef, undef, @other) = @{ $self->{children} };
2716 $self->remove ($_) for @other;
2354 2717
2355 $self->{par} = []; 2718 $self->{par} = [];
2356 $self->{height} = 0; 2719 $self->{height} = 0;
2357 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 2720 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2358} 2721}
2359 2722
2360sub add_paragraph { 2723sub add_paragraph {
2361 my ($self, $color, $text, $indent) = @_; 2724 my ($self, $color, $para, $indent) = @_;
2362 2725
2363 for my $line (split /\n/, $text) { 2726 my ($text, @w) = ref $para ? @$para : $para;
2364 my ($w, $h) = $self->text_size ($line); 2727
2365 $self->{height} += $h; 2728 $para = {
2366 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; 2729 w => 1e10,
2730 wrapped => 1,
2731 fg => $color,
2732 indent => $indent,
2733 markup => $text,
2734 widget => \@w,
2367 } 2735 };
2368 2736
2369 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); 2737 $self->add (@w) if @w;
2738 push @{$self->{par}}, $para;
2739
2740 $self->{need_reflow}++;
2741 $self->update;
2742}
2743
2744sub scroll_to_bottom {
2745 my ($self) = @_;
2746
2747 $self->{scroll_to_bottom} = 1;
2748 $self->update;
2370} 2749}
2371 2750
2372sub update { 2751sub update {
2373 my ($self) = @_; 2752 my ($self) = @_;
2374 2753
2376 2755
2377 return unless $self->{h} > 0; 2756 return unless $self->{h} > 0;
2378 2757
2379 delete $self->{texture}; 2758 delete $self->{texture};
2380 2759
2381 $ROOT->on_post_alloc ($self, sub { 2760 $ROOT->on_post_alloc ($self => sub {
2382 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2761 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2383 2762
2384 if (delete $self->{need_reflow}) { 2763 if (delete $self->{need_reflow}) {
2385 my $height = 0; 2764 my $height = 0;
2386 2765
2387 my $layout = $self->{layout};
2388
2389 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2390
2391 for (@{$self->{par}}) { 2766 for my $para (@{$self->{par}}) {
2392 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support 2767 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2393 $layout->set_width ($W - $_->[3]); 2768 my $layout = $self->get_layout ($para);
2394 $layout->set_markup ($_->[4]);
2395 my ($w, $h) = $layout->size; 2769 my ($w, $h) = $layout->size;
2396 $_->[0] = $w + $_->[3]; 2770
2397 $_->[1] = $h; 2771 $para->{w} = $w + $para->{indent};
2772 $para->{h} = $h;
2773 $para->{wrapped} = $layout->has_wrapped;
2398 } 2774 }
2399 2775
2400 $height += $_->[1]; 2776 $height += $para->{h};
2401 } 2777 }
2402 2778
2403 $self->{height} = $height; 2779 $self->{height} = $height;
2404 2780
2405 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); 2781 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2406 2782
2407 delete $self->{texture}; 2783 delete $self->{texture};
2408 } 2784 }
2409 2785
2786 if (delete $self->{scroll_to_bottom}) {
2787 $self->{children}[1]->set_value (1e10);
2788 }
2789
2410 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2790 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2411 glClearColor 0.5, 0.5, 0.5, 0; 2791 glClearColor 0, 0, 0, 0;
2412 glClear GL_COLOR_BUFFER_BIT; 2792 glClear GL_COLOR_BUFFER_BIT;
2413 2793
2414 my $top = int $self->{children}[1]{range}[0]; 2794 my $top = int $self->{children}[1]{range}[0];
2415 2795
2416 my $y0 = $top; 2796 my $y0 = $top;
2417 my $y1 = $top + $H; 2797 my $y1 = $top + $H;
2418 2798
2419 my $y = 0; 2799 my $y = 0;
2420 2800
2421 my $layout = $self->{layout};
2422
2423 $layout->set_font ($self->{font}) if $self->{font};
2424
2425 glEnable GL_BLEND;
2426 #TODO# not correct in windows where rgba is forced off
2427 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2428
2429 for my $par (@{$self->{par}}) { 2801 for my $para (@{$self->{par}}) {
2430 my $h = $par->[1]; 2802 my $h = $para->{h};
2431 2803
2432 if ($y0 < $y + $h && $y < $y1) { 2804 if ($y0 < $y + $h && $y < $y1) {
2433 $layout->set_foreground (@{ $par->[2] });
2434 $layout->set_width ($W - $par->[3]);
2435 $layout->set_markup ($par->[4]);
2436 2805
2437 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2806 my $layout = $self->get_layout ($para);
2438 2807
2439 glRasterPos $par->[3], $y - $y0; 2808 $layout->render ($para->{indent}, $y - $y0);
2440 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 2809
2810 if (my @w = @{ $para->{widget} }) {
2811 my @s = $layout->get_shapes;
2812
2813 for (@w) {
2814 my ($dx, $dy) = splice @s, 0, 2, ();
2815
2816 $_->{x} = $dx + $para->{indent};
2817 $_->{y} = $dy + $y - $y0;
2818
2819 $_->draw;
2820 }
2821 }
2441 } 2822 }
2442 2823
2443 $y += $h; 2824 $y += $h;
2444 } 2825 }
2445
2446 glDisable GL_BLEND;
2447 }; 2826 };
2448 }); 2827 });
2449} 2828}
2450 2829
2830sub reconfigure {
2831 my ($self) = @_;
2832
2833 $self->SUPER::reconfigure;
2834
2835 $_->{w} = 1e10 for @{ $self->{par} };
2836 $self->reflow;
2837}
2838
2451sub _draw { 2839sub _draw {
2452 my ($self) = @_; 2840 my ($self) = @_;
2453 2841
2454 glEnable GL_TEXTURE_2D; 2842 glEnable GL_TEXTURE_2D;
2455 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2843 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2456 glColor 1, 1, 1, 1; 2844 glColor 0, 0, 0, 1;
2457 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2845 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2458 glDisable GL_TEXTURE_2D; 2846 glDisable GL_TEXTURE_2D;
2459 2847
2460 $self->{children}[1]->draw; 2848 $self->{children}[1]->draw;
2461
2462} 2849}
2463 2850
2464############################################################################# 2851#############################################################################
2465 2852
2466package CFClient::UI::Animator; 2853package CFClient::UI::Animator;
2511 2898
2512sub new { 2899sub new {
2513 my $class = shift; 2900 my $class = shift;
2514 2901
2515 my $self = $class->SUPER::new ( 2902 my $self = $class->SUPER::new (
2516 state => 0, 2903 state => 0,
2517 connect_activate => \&toggle_flopper, 2904 on_activate => \&toggle_flopper,
2518 @_ 2905 @_
2519 ); 2906 );
2520 2907
2521 if ($self->{state}) {
2522 $self->{state} = 0;
2523 $self->toggle_flopper;
2524 }
2525
2526 $self 2908 $self
2527} 2909}
2528 2910
2529sub toggle_flopper { 2911sub toggle_flopper {
2530 my ($self) = @_; 2912 my ($self) = @_;
2531 2913
2532 # TODO: use animation 2914 $self->{other}->toggle_visibility;
2533 if ($self->{state} = !$self->{state}) {
2534 $CFClient::UI::ROOT->add ($self->{other});
2535 $self->{other}->move ($self->coord2global (0, $self->{h}));
2536 $self->_emit ("open");
2537 } else {
2538 $CFClient::UI::ROOT->remove ($self->{other});
2539 $self->_emit ("close");
2540 }
2541
2542 $self->_emit (changed => $self->{state});
2543} 2915}
2544 2916
2545############################################################################# 2917#############################################################################
2546 2918
2547package CFClient::UI::Tooltip; 2919package CFClient::UI::Tooltip;
2560} 2932}
2561 2933
2562sub set_tooltip_from { 2934sub set_tooltip_from {
2563 my ($self, $widget) = @_; 2935 my ($self, $widget) = @_;
2564 2936
2937 my $tooltip = $widget->{tooltip};
2938
2939 if ($ENV{CFPLUS_DEBUG} & 2) {
2940 $tooltip .= "\n\n" . (ref $widget) . "\n"
2941 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2942 . "req $widget->{req_w} $widget->{req_h}\n"
2943 . "visible $widget->{visible}";
2944 }
2945
2946 $tooltip =~ s/^\n+//;
2947 $tooltip =~ s/\n+$//;
2948
2565 $self->add (new CFClient::UI::Label 2949 $self->add (new CFClient::UI::Label
2566 markup => $widget->{tooltip}, 2950 markup => $tooltip,
2567 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2951 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2568 fontsize => 0.8, 2952 fontsize => 0.8,
2569 fg => [0, 0, 0, 1], 2953 fg => [0, 0, 0, 1],
2570 ellipsise => 0, 2954 ellipsise => 0,
2571 font => ($widget->{tooltip_font} || $::FONT_PROP), 2955 font => ($widget->{tooltip_font} || $::FONT_PROP),
2578 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2962 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2579 2963
2580 ($w + 4, $h + 4) 2964 ($w + 4, $h + 4)
2581} 2965}
2582 2966
2583sub size_allocate { 2967sub invoke_size_allocate {
2584 my ($self, $w, $h) = @_; 2968 my ($self, $w, $h) = @_;
2585 2969
2586 $self->SUPER::size_allocate ($w - 4, $h - 4); 2970 $self->SUPER::invoke_size_allocate ($w - 4, $h - 4)
2971}
2972
2973sub invoke_visibility_change {
2974 my ($self, $visible) = @_;
2975
2976 return unless $visible;
2977
2978 $self->{root}->on_post_alloc ("move_$self" => sub {
2979 my $widget = $self->{owner}
2980 or return;
2981
2982 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2983
2984 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2985 if $x + $self->{w} > $self->{root}{w};
2986
2987 $self->move_abs ($x, $y);
2988 });
2587} 2989}
2588 2990
2589sub _draw { 2991sub _draw {
2590 my ($self) = @_; 2992 my ($self) = @_;
2591 2993
2608 glVertex $w, $h; 3010 glVertex $w, $h;
2609 glVertex $w, 0; 3011 glVertex $w, 0;
2610 glEnd; 3012 glEnd;
2611 3013
2612 glTranslate 2 - 0.375, 2 - 0.375; 3014 glTranslate 2 - 0.375, 2 - 0.375;
3015
2613 $self->SUPER::_draw; 3016 $self->SUPER::_draw;
2614} 3017}
2615 3018
2616############################################################################# 3019#############################################################################
2617 3020
2623 3026
2624sub new { 3027sub new {
2625 my $class = shift; 3028 my $class = shift;
2626 3029
2627 my $self = $class->SUPER::new ( 3030 my $self = $class->SUPER::new (
2628 aspect => 1, 3031 aspect => 1,
3032 can_events => 0,
2629 @_, 3033 @_,
2630 ); 3034 );
2631 3035
2632 if ($self->{anim} && $self->{animspeed}) { 3036 if ($self->{anim} && $self->{animspeed}) {
2633 Scalar::Util::weaken (my $widget = $self); 3037 Scalar::Util::weaken (my $widget = $self);
2675 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 3079 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2676 3080
2677 if ($tex) { 3081 if ($tex) {
2678 glEnable GL_TEXTURE_2D; 3082 glEnable GL_TEXTURE_2D;
2679 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3083 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2680 glColor 1, 1, 1, 1; 3084 glColor 0, 0, 0, 1;
2681 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3085 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2682 glDisable GL_TEXTURE_2D; 3086 glDisable GL_TEXTURE_2D;
2683 } 3087 }
2684} 3088}
2685 3089
2686sub DESTROY { 3090sub destroy {
2687 my ($self) = @_; 3091 my ($self) = @_;
2688 3092
2689 $self->{timer}->cancel 3093 $self->{timer}->cancel
2690 if $self->{timer}; 3094 if $self->{timer};
2691 3095
2692 $self->SUPER::DESTROY; 3096 $self->SUPER::destroy;
2693} 3097}
2694 3098
2695############################################################################# 3099#############################################################################
2696 3100
2697package CFClient::UI::InventoryItem; 3101package CFClient::UI::Buttonbar;
2698 3102
2699our @ISA = CFClient::UI::HBox::; 3103our @ISA = CFClient::UI::HBox::;
2700 3104
2701sub _item_to_desc { 3105# TODO: should actualyl wrap buttons and other goodies.
2702 my ($item) = @_;
2703
2704 my $desc =
2705 $item->{nrof} < 2
2706 ? $item->{name}
2707 : "$item->{nrof} × $item->{name_pl}";
2708
2709 $item->{flags} & Crossfire::Protocol::F_OPEN
2710 and $desc .= " (open)";
2711 $item->{flags} & Crossfire::Protocol::F_APPLIED
2712 and $desc .= " (applied)";
2713 $item->{flags} & Crossfire::Protocol::F_UNPAID
2714 and $desc .= " (unpaid)";
2715 $item->{flags} & Crossfire::Protocol::F_MAGIC
2716 and $desc .= " (magic)";
2717 $item->{flags} & Crossfire::Protocol::F_CURSED
2718 and $desc .= " (cursed)";
2719 $item->{flags} & Crossfire::Protocol::F_DAMNED
2720 and $desc .= " (damned)";
2721 $item->{flags} & Crossfire::Protocol::F_LOCKED
2722 and $desc .= " *";
2723
2724 $desc
2725}
2726
2727sub new {
2728 my $class = shift;
2729
2730 my %args = @_;
2731
2732 my $item = delete $args{item};
2733
2734 my $desc = _item_to_desc ($item);
2735
2736 my $self = $class->SUPER::new (
2737 can_hover => 1,
2738 can_events => 1,
2739 tooltip => ((CFClient::UI::Label::escape $desc)
2740 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2741 connect_button_down => sub {
2742 my ($self, $ev, $x, $y) = @_;
2743
2744 # todo: maybe put examine on 1? but should just be a tooltip :(
2745 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2746 my $targ = $::CONN->{player}{tag};
2747
2748 if ($item->{container} == $::CONN->{player}{tag}) {
2749 $targ = $main::OPENCONT;
2750 }
2751
2752 $::CONN->send ("move $targ $item->{tag} 0");
2753 } elsif ($ev->{button} == 1) {
2754 $::CONN->send ("examine $item->{tag}");
2755 } elsif ($ev->{button} == 2) {
2756 $::CONN->send ("apply $item->{tag}");
2757 } elsif ($ev->{button} == 3) {
2758 my @menu_items = (
2759 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2760 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2761 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2762 (
2763 $item->{flags} & Crossfire::Protocol::F_LOCKED
2764 ? (
2765 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2766 )
2767 : (
2768 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2769 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2770 )
2771 ),
2772 );
2773
2774 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2775 }
2776
2777 1
2778 },
2779 %args
2780 );
2781
2782
2783 $self->add (new CFClient::UI::Face
2784 can_events => 0,
2785 face => $item->{face},
2786 anim => $item->{anim},
2787 animspeed => $item->{animspeed},
2788 );
2789
2790 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2791
2792 $self->{item} = $item;
2793
2794 $self->update_item;
2795
2796 $self
2797}
2798
2799sub update_item {
2800 my ($self) = @_;
2801
2802 my $desc = _item_to_desc ($self->{item});
2803
2804 $self->{name_lbl}->set_text ($desc);
2805}
2806
2807#############################################################################
2808
2809package CFClient::UI::Inventory;
2810
2811our @ISA = CFClient::UI::ScrolledWindow::;
2812
2813sub new {
2814 my $class = shift;
2815
2816 my $self = $class->SUPER::new (
2817 scrolled => (new CFClient::UI::Table),
2818 @_,
2819 );
2820
2821 $self
2822}
2823
2824sub set_items {
2825 my ($self, $items) = @_;
2826
2827 $self->{scrolled}->clear;
2828 return unless $items;
2829
2830 my @items = sort {
2831 ($a->{type} <=> $b->{type})
2832 or ($a->{name} cmp $b->{name})
2833 } @$items;
2834
2835 $self->{real_items} = \@items;
2836
2837 for my $item (@items) {
2838 $item->{item} = $item;
2839 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2840 $item->update_item ();
2841 }
2842
2843 my $i = 0;
2844 for (@items) {
2845 $self->{scrolled}->add (0, $i, $_);
2846 my $nrof = $_->{item}->{nrof} || 1;
2847 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2848 }
2849
2850# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2851}
2852
2853sub size_request {
2854 my ($self) = @_;
2855 ($self->{req_w}, $self->{req_h});
2856}
2857 3106
2858############################################################################# 3107#############################################################################
2859 3108
2860package CFClient::UI::Menu; 3109package CFClient::UI::Menu;
2861 3110
2873 ); 3122 );
2874 3123
2875 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3124 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2876 3125
2877 for my $item (@{ $self->{items} }) { 3126 for my $item (@{ $self->{items} }) {
2878 my ($widget, $cb) = @$item; 3127 my ($widget, $cb, $tooltip) = @$item;
2879 3128
2880 # handle various types of items, only text for now 3129 # handle various types of items, only text for now
2881 if (!ref $widget) { 3130 if (!ref $widget) {
3131 if ($widget =~ /\t/) {
3132 my ($left, $right) = split /\t/, $widget, 2;
3133
3134 $widget = new CFClient::UI::HBox
3135 can_hover => 1,
3136 can_events => 1,
3137 tooltip => $tooltip,
3138 children => [
3139 (new CFClient::UI::Label markup => $left, expand => 1),
3140 (new CFClient::UI::Label markup => $right, align => +1),
3141 ],
3142 ;
3143
3144 } else {
2882 $widget = new CFClient::UI::Label 3145 $widget = new CFClient::UI::Label
2883 can_hover => 1, 3146 can_hover => 1,
2884 can_events => 1, 3147 can_events => 1,
2885 text => $widget; 3148 markup => $widget,
3149 tooltip => $tooltip;
3150 }
2886 } 3151 }
2887 3152
2888 $self->{item}{$widget} = $item; 3153 $self->{item}{$widget} = $item;
2889 3154
2890 $self->{vbox}->add ($widget); 3155 $self->{vbox}->add ($widget);
2895 3160
2896# popup given the event (must be a mouse button down event currently) 3161# popup given the event (must be a mouse button down event currently)
2897sub popup { 3162sub popup {
2898 my ($self, $ev) = @_; 3163 my ($self, $ev) = @_;
2899 3164
2900 $self->_emit ("popdown"); 3165 $self->emit ("popdown");
2901 3166
2902 # maybe save $GRAB? must be careful about events... 3167 # maybe save $GRAB? must be careful about events...
2903 $GRAB = $self; 3168 $GRAB = $self;
2904 $self->{button} = $ev->{button}; 3169 $self->{button} = $ev->{button};
2905 3170
2906 $self->show; 3171 $self->show;
2907 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3172 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2908} 3173}
2909 3174
2910sub mouse_motion { 3175sub invoke_mouse_motion {
2911 my ($self, $ev, $x, $y) = @_; 3176 my ($self, $ev, $x, $y) = @_;
2912 3177
2913 # TODO: should use vbox->find_widget or so 3178 # TODO: should use vbox->find_widget or so
2914 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 3179 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2915 $self->{hover} = $self->{item}{$HOVER}; 3180 $self->{hover} = $self->{item}{$HOVER};
2916}
2917 3181
3182 0
3183}
3184
2918sub button_up { 3185sub invoke_button_up {
2919 my ($self, $ev, $x, $y) = @_; 3186 my ($self, $ev, $x, $y) = @_;
2920 3187
2921 if ($ev->{button} == $self->{button}) { 3188 if ($ev->{button} == $self->{button}) {
2922 undef $GRAB; 3189 undef $GRAB;
2923 $self->hide; 3190 $self->hide;
2924 3191
2925 $self->_emit ("popdown"); 3192 $self->emit ("popdown");
2926 $self->{hover}[1]->() if $self->{hover}; 3193 $self->{hover}[1]->() if $self->{hover};
3194 } else {
3195 return 0
3196 }
3197
2927 } 3198 1
2928} 3199}
2929 3200
2930############################################################################# 3201#############################################################################
2931 3202
2932package CFClient::UI::Statusbox; 3203package CFClient::UI::Multiplexer;
2933 3204
2934our @ISA = CFClient::UI::VBox::; 3205our @ISA = CFClient::UI::Container::;
2935 3206
2936sub new { 3207sub new {
2937 my $class = shift; 3208 my $class = shift;
2938 3209
2939 $class->SUPER::new ( 3210 my $self = $class->SUPER::new (
3211 @_,
3212 );
3213
3214 $self->{current} = $self->{children}[0]
3215 if @{ $self->{children} };
3216
3217 $self
3218}
3219
3220sub add {
3221 my ($self, @widgets) = @_;
3222
3223 $self->SUPER::add (@widgets);
3224
3225 $self->{current} = $self->{children}[0]
3226 if @{ $self->{children} };
3227}
3228
3229sub get_current_page {
3230 my ($self) = @_;
3231
3232 $self->{current}
3233}
3234
3235sub set_current_page {
3236 my ($self, $page_or_widget) = @_;
3237
3238 my $widget = ref $page_or_widget
3239 ? $page_or_widget
3240 : $self->{children}[$page_or_widget];
3241
3242 $self->{current} = $widget;
3243 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3244
3245 $self->emit (page_changed => $self->{current});
3246
3247 $self->realloc;
3248}
3249
3250sub visible_children {
3251 $_[0]{current}
3252}
3253
3254sub size_request {
3255 my ($self) = @_;
3256
3257 $self->{current}->size_request
3258}
3259
3260sub invoke_size_allocate {
3261 my ($self, $w, $h) = @_;
3262
3263 $self->{current}->configure (0, 0, $w, $h);
3264
3265 1
3266}
3267
3268sub _draw {
3269 my ($self) = @_;
3270
3271 $self->{current}->draw;
3272}
3273
3274#############################################################################
3275
3276package CFClient::UI::Notebook;
3277
3278our @ISA = CFClient::UI::VBox::;
3279
3280sub new {
3281 my $class = shift;
3282
3283 my $self = $class->SUPER::new (
3284 buttonbar => (new CFClient::UI::Buttonbar),
3285 multiplexer => (new CFClient::UI::Multiplexer expand => 1),
3286 # filter => # will be put between multiplexer and $self
3287 @_,
3288 );
3289
3290 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3291 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3292
3293 $self
3294}
3295
3296sub add {
3297 my ($self, $title, $widget, $tooltip) = @_;
3298
3299 Scalar::Util::weaken $self;
3300
3301 $self->{buttonbar}->add (new CFClient::UI::Button
3302 markup => $title,
3303 tooltip => $tooltip,
3304 on_activate => sub { $self->set_current_page ($widget) },
3305 );
3306
3307 $self->{multiplexer}->add ($widget);
3308}
3309
3310sub get_current_page {
3311 my ($self) = @_;
3312
3313 $self->{multiplexer}->get_current_page
3314}
3315
3316sub set_current_page {
3317 my ($self, $page) = @_;
3318
3319 $self->{multiplexer}->set_current_page ($page);
3320 $self->emit (page_changed => $self->{multiplexer}{current});
3321}
3322
3323#############################################################################
3324
3325package CFClient::UI::Combobox;
3326
3327use utf8;
3328
3329our @ISA = CFClient::UI::Button::;
3330
3331sub new {
3332 my $class = shift;
3333
3334 my $self = $class->SUPER::new (
3335 options => [], # [value, title, longdesc], ...
3336 value => undef,
3337 @_,
3338 );
3339
3340 $self->_set_value ($self->{value});
3341
3342 $self
3343}
3344
3345sub invoke_button_down {
3346 my ($self, $ev) = @_;
3347
3348 my @menu_items;
3349
3350 for (@{ $self->{options} }) {
3351 my ($value, $title, $tooltip) = @$_;
3352
3353 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3354 }
3355
3356 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
3357}
3358
3359sub _set_value {
3360 my ($self, $value) = @_;
3361
3362 my ($item) = grep $_->[0] eq $value, @{ $self->{options} }
3363 or return;
3364
3365 $self->{value} = $item->[0];
3366 $self->set_markup ("$item->[1] ⇓");
3367 $self->set_tooltip ($item->[2]);
3368}
3369
3370sub set_value {
3371 my ($self, $value) = @_;
3372
3373 return unless $self->{value} ne $value;
3374
3375 $self->_set_value ($value);
3376 $self->emit (changed => $value);
3377}
3378
3379#############################################################################
3380
3381package CFClient::UI::Statusbox;
3382
3383our @ISA = CFClient::UI::VBox::;
3384
3385sub new {
3386 my $class = shift;
3387
3388 my $self = $class->SUPER::new (
2940 fontsize => 0.8, 3389 fontsize => 0.8,
2941 @_, 3390 @_,
2942 ) 3391 );
3392
3393 Scalar::Util::weaken (my $this = $self);
3394
3395 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3396
3397 $self
2943} 3398}
2944 3399
2945sub reorder { 3400sub reorder {
2946 my ($self) = @_; 3401 my ($self) = @_;
2947 my $NOW = time; 3402 my $NOW = Time::HiRes::time;
3403
3404 # freeze display when hovering over any label
3405 return if $CFClient::UI::TOOLTIP->{owner}
3406 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label},
3407 values %{ $self->{item} };
2948 3408
2949 while (my ($k, $v) = each %{ $self->{item} }) { 3409 while (my ($k, $v) = each %{ $self->{item} }) {
2950 delete $self->{item}{$k} if $v->{timeout} < $NOW; 3410 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2951 } 3411 }
2952 3412
2955 my @items = sort { 3415 my @items = sort {
2956 $a->{pri} <=> $b->{pri} 3416 $a->{pri} <=> $b->{pri}
2957 or $b->{id} <=> $a->{id} 3417 or $b->{id} <=> $a->{id}
2958 } values %{ $self->{item} }; 3418 } values %{ $self->{item} };
2959 3419
3420 $self->{timer}->interval (1);
3421
2960 my $count = 10 + 1; 3422 my $count = 10 + 1;
2961 for my $item (@items) { 3423 for my $item (@items) {
2962 last unless --$count; 3424 last unless --$count;
2963 3425
2964 push @widgets, $item->{label} ||= do { 3426 my $label = $item->{label} ||= do {
2965 # TODO: doesn't handle markup well (read as: at all) 3427 # TODO: doesn't handle markup well (read as: at all)
2966 my $short = $item->{count} > 1 3428 my $short = $item->{count} > 1
2967 ? "<b>$item->{count} ×</b> $item->{text}" 3429 ? "<b>$item->{count} ×</b> $item->{text}"
2968 : $item->{text}; 3430 : $item->{text};
2969 3431
2977 tooltip => $item->{tooltip}, 3439 tooltip => $item->{tooltip},
2978 tooltip_font => $::FONT_PROP, 3440 tooltip_font => $::FONT_PROP,
2979 tooltip_width => 0.67, 3441 tooltip_width => 0.67,
2980 fontsize => $item->{fontsize} || $self->{fontsize}, 3442 fontsize => $item->{fontsize} || $self->{fontsize},
2981 max_w => $::WIDTH * 0.44, 3443 max_w => $::WIDTH * 0.44,
2982 fg => $item->{fg}, 3444 fg => [@{ $item->{fg} }],
2983 can_events => 1, 3445 can_events => 1,
2984 can_hover => 1 3446 can_hover => 1
2985 }; 3447 };
3448
3449 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3450 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3451 $label->update;
3452 $label->set_max_size (undef, $label->{req_h} * $diff)
3453 if $diff < 1;
3454 $self->{timer}->interval (1/30);
3455 } else {
3456 $label->{fg}[3] = $item->{fg}[3] || 1;
3457 }
3458
3459 push @widgets, $label;
2986 } 3460 }
2987 3461
2988 $self->clear; 3462 $self->clear;
2989 $self->SUPER::add (reverse @widgets); 3463 $self->SUPER::add (reverse @widgets);
2990} 3464}
2993 my ($self, $text, %arg) = @_; 3467 my ($self, $text, %arg) = @_;
2994 3468
2995 $text =~ s/^\s+//; 3469 $text =~ s/^\s+//;
2996 $text =~ s/\s+$//; 3470 $text =~ s/\s+$//;
2997 3471
3472 return unless $text;
3473
2998 my $timeout = time + ((delete $arg{timeout}) || 60); 3474 my $timeout = (int time) + ((delete $arg{timeout}) || 60);
2999 3475
3000 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 3476 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
3001 3477
3002 if (my $item = $self->{item}{$group}) { 3478 if (my $item = $self->{item}{$group}) {
3003 if ($item->{text} eq $text) { 3479 if ($item->{text} eq $text) {
3004 $item->{count}++; 3480 $item->{count}++;
3005 } else { 3481 } else {
3006 $item->{count} = 1; 3482 $item->{count} = 1;
3007 $item->{text} = $item->{tooltip} = $text; 3483 $item->{text} = $item->{tooltip} = $text;
3008 } 3484 }
3009 $item->{id} = ++$self->{id}; 3485 $item->{id} += 0.2;#d#
3010 $item->{timeout} = $timeout; 3486 $item->{timeout} = $timeout;
3011 delete $item->{label}; 3487 delete $item->{label};
3012 } else { 3488 } else {
3013 $self->{item}{$group} = { 3489 $self->{item}{$group} = {
3014 id => ++$self->{id}, 3490 id => ++$self->{id},
3020 count => 1, 3496 count => 1,
3021 %arg, 3497 %arg,
3022 }; 3498 };
3023 } 3499 }
3024 3500
3501 $ROOT->on_refresh (reorder => sub {
3025 $self->reorder; 3502 $self->reorder;
3503 });
3026} 3504}
3027 3505
3028sub reconfigure { 3506sub reconfigure {
3029 my ($self) = @_; 3507 my ($self) = @_;
3030 3508
3033 3511
3034 $self->reorder; 3512 $self->reorder;
3035 $self->SUPER::reconfigure; 3513 $self->SUPER::reconfigure;
3036} 3514}
3037 3515
3516sub destroy {
3517 my ($self) = @_;
3518
3519 $self->{timer}->cancel;
3520
3521 $self->SUPER::destroy;
3522}
3523
3038############################################################################# 3524#############################################################################
3039 3525
3040package CFClient::UI::Root; 3526package CFClient::UI::Inventory;
3041 3527
3042our @ISA = CFClient::UI::Container::; 3528our @ISA = CFClient::UI::Table::;
3043
3044use CFClient::OpenGL;
3045 3529
3046sub new { 3530sub new {
3047 my $class = shift; 3531 my $class = shift;
3048 3532
3049 $class->SUPER::new ( 3533 my $self = $class->SUPER::new (
3534 col_expand => [0, 1, 0],
3535 @_,
3536 );
3537
3538 $self
3539}
3540
3541sub set_items {
3542 my ($self, $items) = @_;
3543
3544 $self->clear;
3545 return unless $items;
3546
3547 my @items = sort {
3548 ($a->{type} <=> $b->{type})
3549 or ($a->{name} cmp $b->{name})
3550 } values %$items;
3551
3552 $self->{real_items} = \@items;
3553
3554 my $row = 0;
3555 for my $item (@items) {
3556 CFClient::Item::update_widgets $item;
3557
3558 $self->add (0, $row, $item->{face_widget});
3559 $self->add (1, $row, $item->{desc_widget});
3560 $self->add (2, $row, $item->{weight_widget});
3561
3562 $row++;
3563 }
3564}
3565
3566#############################################################################
3567
3568package CFClient::UI::SpellList;
3569
3570our @ISA = CFClient::UI::Table::;
3571
3572sub new {
3573 my $class = shift;
3574
3575 my $self = $class->SUPER::new (
3576 binding => [],
3577 commands => [],
3050 @_, 3578 @_,
3051 ) 3579 )
3052} 3580}
3053 3581
3054sub configure { 3582my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3055 my ($self, $x, $y, $w, $h) = @_;
3056 3583
3057 $self->{w} = $w; 3584my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3058 $self->{h} = $h; 3585 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3059} 3586my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3587 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3588my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3589 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3590my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3591 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3592my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3593 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3060 3594
3061sub check_size { 3595sub rebuild_spell_list {
3596 my ($self) = @_;
3597
3598 $CFClient::UI::ROOT->on_refresh ($self => sub {
3599 $self->clear;
3600
3601 return unless $::CONN;
3602
3603 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3604 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3605 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3606 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3607 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3608
3609 my $row = 0;
3610
3611 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3612 my $spell = $self->{spell}{$_};
3613
3614 $row++;
3615
3616 my $spell_cb = sub {
3617 my ($widget, $ev) = @_;
3618
3619 if ($ev->{button} == 1) {
3620 $::CONN->user_send ("cast $spell->{name}");
3621 } elsif ($ev->{button} == 2) {
3622 $::CONN->user_send ("invoke $spell->{name}");
3623 } elsif ($ev->{button} == 3) {
3624 (new CFClient::UI::Menu
3625 items => [
3626 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3627 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3628 ],
3629 )->popup ($ev);
3630 } else {
3631 return 0;
3632 }
3633
3634 1
3635 };
3636
3637 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3638
3639 #TODO: add path info to tooltip
3640 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3641
3642 $self->add (0, $row, new CFClient::UI::Face
3643 face => $spell->{face},
3644 can_hover => 1,
3645 can_events => 1,
3646 tooltip => $tooltip,
3647 on_button_down => $spell_cb,
3648 );
3649
3650 $self->add (1, $row, new CFClient::UI::Label
3651 expand => 1,
3652 text => $spell->{name},
3653 can_hover => 1,
3654 can_events => 1,
3655 tooltip => $tooltip,
3656 on_button_down => $spell_cb,
3657 );
3658
3659 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3660 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3661 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3662 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3663 }
3664 });
3665}
3666
3667sub add_spell {
3062 my ($self) = @_; 3668 my ($self, $spell) = @_;
3063 3669
3064 $self->size_allocate ($self->{w}, $self->{h}) 3670 $self->{spell}->{$spell->{name}} = $spell;
3065 if $self->{w}; 3671 $self->rebuild_spell_list;
3672}
3673
3674sub remove_spell {
3675 my ($self, $spell) = @_;
3676
3677 delete $self->{spell}->{$spell->{name}};
3678 $self->rebuild_spell_list;
3679}
3680
3681sub clear_spells {
3682 my ($self) = @_;
3683
3684 $self->{spell} = {};
3685 $self->rebuild_spell_list;
3686}
3687
3688#############################################################################
3689
3690package CFClient::UI::Root;
3691
3692our @ISA = CFClient::UI::Container::;
3693
3694use List::Util qw(min max);
3695
3696use CFClient::OpenGL;
3697
3698sub new {
3699 my $class = shift;
3700
3701 my $self = $class->SUPER::new (
3702 visible => 1,
3703 @_,
3704 );
3705
3706 Scalar::Util::weaken ($self->{root} = $self);
3707
3708 $self
3066} 3709}
3067 3710
3068sub size_request { 3711sub size_request {
3069 my ($self) = @_; 3712 my ($self) = @_;
3070 3713
3071 ($self->{w}, $self->{h}) 3714 ($self->{w}, $self->{h})
3072} 3715}
3073 3716
3717sub _to_pixel {
3718 my ($coord, $size, $max) = @_;
3719
3720 $coord =
3721 $coord eq "center" ? ($max - $size) * 0.5
3722 : $coord eq "max" ? $max
3723 : $coord;
3724
3725 $coord = 0 if $coord < 0;
3726 $coord = $max - $size if $coord > $max - $size;
3727
3728 int $coord + 0.5
3729}
3730
3074sub size_allocate { 3731sub invoke_size_allocate {
3075 my ($self, $w, $h) = @_; 3732 my ($self, $w, $h) = @_;
3076 3733
3077 for my $child ($self->children) { 3734 for my $child ($self->children) {
3078 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3735 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3079 3736
3080 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3737 $X = $child->{force_x} if exists $child->{force_x};
3081 if exists $child->{req_x}; 3738 $Y = $child->{force_y} if exists $child->{force_y};
3082 3739
3083 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3740 $X = _to_pixel $X, $W, $self->{w};
3084 if exists $child->{req_y}; 3741 $Y = _to_pixel $Y, $H, $self->{h};
3085
3086 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3087 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3088 3742
3089 $child->configure ($X, $Y, $W, $H); 3743 $child->configure ($X, $Y, $W, $H);
3090 } 3744 }
3745
3746 1
3091} 3747}
3092 3748
3093sub coord2local { 3749sub coord2local {
3094 my ($self, $x, $y) = @_; 3750 my ($self, $x, $y) = @_;
3095 3751
3103} 3759}
3104 3760
3105sub update { 3761sub update {
3106 my ($self) = @_; 3762 my ($self) = @_;
3107 3763
3108 $self->check_size;
3109 $::WANT_REFRESH++; 3764 $::WANT_REFRESH++;
3110} 3765}
3111 3766
3112sub add { 3767sub add {
3113 my ($self, @children) = @_; 3768 my ($self, @children) = @_;
3114 3769
3115 for (my @widgets = @children; my $w = pop @widgets; ) {
3116 push @widgets, $w->children;
3117 $w->{root} = $self;
3118 $w->{visible} = 1;
3119 }
3120
3121 for my $child (@children) {
3122 $child->{is_toplevel} = 1; 3770 $_->{is_toplevel} = 1
3123 3771 for @children;
3124 # integerise window positions
3125 $child->{x} = int $child->{x};
3126 $child->{y} = int $child->{y};
3127 }
3128 3772
3129 $self->SUPER::add (@children); 3773 $self->SUPER::add (@children);
3130} 3774}
3131 3775
3132sub remove { 3776sub remove {
3133 my ($self, @children) = @_; 3777 my ($self, @children) = @_;
3134 3778
3135 $self->SUPER::remove (@children); 3779 $self->SUPER::remove (@children);
3780
3781 delete $self->{is_toplevel}
3782 for @children;
3136 3783
3137 while (@children) { 3784 while (@children) {
3138 my $w = pop @children; 3785 my $w = pop @children;
3139 push @children, $w->children; 3786 push @children, $w->children;
3140 $w->set_invisible; 3787 $w->set_invisible;
3159 while ($self->{refresh_hook}) { 3806 while ($self->{refresh_hook}) {
3160 $_->() 3807 $_->()
3161 for values %{delete $self->{refresh_hook}}; 3808 for values %{delete $self->{refresh_hook}};
3162 } 3809 }
3163 3810
3164 if ($self->{check_size}) { 3811 if ($self->{realloc}) {
3165 my @queue = ([], []); 3812 my %queue;
3813 my @queue;
3814 my $widget;
3166 3815
3167 for (;;) { 3816 outer:
3168 if ($self->{check_size}) { 3817 while () {
3169 # heuristic: check containers last 3818 if (my $realloc = delete $self->{realloc}) {
3170 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3819 for $widget (values %$realloc) {
3171 for values %{delete $self->{check_size}} 3820 $widget->{visible} or next; # do not resize invisible widgets
3821
3822 $queue{$widget+0}++ and next; # duplicates are common
3823
3824 push @{ $queue[$widget->{visible}] }, $widget;
3825 }
3172 } 3826 }
3173 3827
3174 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3828 while () {
3829 @queue or last outer;
3175 3830
3176 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3831 $widget = pop @{ $queue[-1] || [] }
3177 ? @$widget{qw(user_w user_h)} 3832 and last;
3178 : $widget->size_request;
3179
3180 if (delete $widget->{force_alloc}
3181 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3182 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3183 3833
3834 pop @queue;
3835 }
3836
3837 delete $queue{$widget+0};
3838
3839 my ($w, $h) = $widget->size_request;
3840
3841 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3842 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3843
3844 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3845 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3846
3847 $w = $widget->{force_w} if exists $widget->{force_w};
3848 $h = $widget->{force_h} if exists $widget->{force_h};
3849
3850 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3851 || delete $widget->{force_realloc}) {
3184 $widget->{req_w} = $w; 3852 $widget->{req_w} = $w;
3185 $widget->{req_h} = $h; 3853 $widget->{req_h} = $h;
3186 3854
3187 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3855 $self->{size_alloc}{$widget+0} = $widget;
3188 3856
3189 $widget->{parent}->check_size
3190 if $widget->{parent}; 3857 if (my $parent = $widget->{parent}) {
3858 $self->{realloc}{$parent+0} = $parent
3859 unless $queue{$parent+0};
3860
3861 $parent->{force_size_alloc} = 1;
3862 $self->{size_alloc}{$parent+0} = $parent;
3863 }
3191 } 3864 }
3865
3866 delete $self->{realloc}{$widget+0};
3192 } 3867 }
3193 } 3868 }
3194 3869
3195 while ($self->{size_alloc}) { 3870 while (my $size_alloc = delete $self->{size_alloc}) {
3196 for (values %{delete $self->{size_alloc}}) { 3871 my @queue = sort { $b->{visible} <=> $a->{visible} }
3197 my ($widget, $w, $h) = @$_; 3872 values %$size_alloc;
3873
3874 while () {
3875 my $widget = pop @queue || last;
3876
3877 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3198 3878
3199 $w = 0 if $w < 0; 3879 $w = 0 if $w < 0;
3200 $h = 0 if $h < 0; 3880 $h = 0 if $h < 0;
3201 3881
3882 $w = max $widget->{min_w}, $w;
3883 $h = max $widget->{min_h}, $h;
3884
3885 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3886 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3887
3888 $w = int $w + 0.5;
3889 $h = int $h + 0.5;
3890
3891 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3892 $widget->{old_w} = $widget->{w};
3893 $widget->{old_h} = $widget->{h};
3894
3202 $widget->{w} = $w; 3895 $widget->{w} = $w;
3203 $widget->{h} = $h; 3896 $widget->{h} = $h;
3897
3204 $widget->emit (size_allocate => $w, $h); 3898 $widget->emit (size_allocate => $w, $h);
3899 }
3205 } 3900 }
3206 } 3901 }
3207 3902
3208 while ($self->{post_alloc_hook}) { 3903 while ($self->{post_alloc_hook}) {
3209 $_->() 3904 $_->()
3210 for values %{delete $self->{post_alloc_hook}}; 3905 for values %{delete $self->{post_alloc_hook}};
3211 } 3906 }
3907
3212 3908
3213 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3909 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3214 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3910 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3215 glClear GL_COLOR_BUFFER_BIT; 3911 glClear GL_COLOR_BUFFER_BIT;
3216 3912
3218 glLoadIdentity; 3914 glLoadIdentity;
3219 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 3915 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3220 glMatrixMode GL_MODELVIEW; 3916 glMatrixMode GL_MODELVIEW;
3221 glLoadIdentity; 3917 glLoadIdentity;
3222 3918
3919 {
3920 package CFClient::UI::Base;
3921
3922 ($draw_x, $draw_y, $draw_w, $draw_h) =
3923 (0, 0, $self->{w}, $self->{h});
3924 }
3925
3223 $self->_draw; 3926 $self->_draw;
3224} 3927}
3225 3928
3226############################################################################# 3929#############################################################################
3227 3930

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines