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.319 by root, Sun Jul 2 21:07:26 2006 UTC vs.
Revision 1.479 by root, Thu Feb 4 20:01:09 2010 UTC

1package CFClient::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use Scalar::Util ();
7use List::Util (); 5use List::Util ();
8use Event;
9 6
10use CFClient; 7use Guard ();
8
9use DC;
10use DC::Pod;
11use CFClient::Texture; 11use DC::Texture;
12 12
13our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
14 14
15our $LAYOUT; 15our $LAYOUT;
16our $ROOT; 16our $ROOT;
17our $TOOLTIP; 17our $TOOLTIP;
18our $BUTTON_STATE; 18our $BUTTON_STATE;
19 19
20our %WIDGET; # all widgets, weak-referenced 20our %WIDGET; # all widgets, weak-referenced
21 21
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
23 $_[0]->stop;
24
23 if (!$GRAB) { 25 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 26 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 27 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 28 if ($TOOLTIP->{owner} != $widget) {
29 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
27 $TOOLTIP->hide; 30 $TOOLTIP->hide;
28 31
29 $TOOLTIP->{owner} = $widget; 32 $TOOLTIP->{owner} = $widget;
33 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
30 34
31 return if $ENV{CFPLUS_DEBUG} & 8; 35 return if $ENV{CFPLUS_DEBUG} & 8;
32 36
33 my $tip = $widget->{tooltip};
34
35 $tip = $tip->($widget) if CODE:: eq ref $tip;
36
37 $TOOLTIP->set_tooltip_from ($widget); 37 $TOOLTIP->set_tooltip_from ($widget);
38 $TOOLTIP->show; 38 $TOOLTIP->show;
39 } 39 }
40 40
41 return; 41 return;
42 } 42 }
43 } 43 }
44 } 44 }
45 45
46 $TOOLTIP->hide; 46 $TOOLTIP->hide;
47 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
47 delete $TOOLTIP->{owner}; 48 delete $TOOLTIP->{owner};
48}); 49};
49 50
50sub get_layout { 51sub get_layout {
51 my $layout; 52 my $layout;
52 53
53 for (grep { $_->{name} } values %WIDGET) { 54 for (grep { $_->{name} } values %WIDGET) {
79sub feed_sdl_key_up_event { 80sub feed_sdl_key_up_event {
80 $FOCUS->emit (key_up => $_[0]) 81 $FOCUS->emit (key_up => $_[0])
81 if $FOCUS; 82 if $FOCUS;
82} 83}
83 84
85sub check_hover {
86 my ($widget) = @_;
87
88 if ($widget != $HOVER) {
89 my $hover = $HOVER; $HOVER = $widget;
90
91 $hover->update if $hover && $hover->{can_hover};
92 $HOVER->update if $HOVER && $HOVER->{can_hover};
93
94 $TOOLTIP_WATCHER->again;
95 }
96}
97
84sub feed_sdl_button_down_event { 98sub feed_sdl_button_down_event {
85 my ($ev) = @_; 99 my ($ev) = @_;
86 my ($x, $y) = ($ev->{x}, $ev->{y}); 100 my ($x, $y) = ($ev->{x}, $ev->{y});
87 101
88 if (!$BUTTON_STATE) { 102 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
103
104 unless ($GRAB) {
89 my $widget = $ROOT->find_widget ($x, $y); 105 my $widget = $ROOT->find_widget ($x, $y);
90 106
91 $GRAB = $widget; 107 $GRAB = $widget;
92 $GRAB->update if $GRAB; 108 $GRAB->update if $GRAB;
93 109
94 $TOOLTIP_WATCHER->cb->(); 110 $TOOLTIP_WATCHER->invoke;
95 } 111 }
96 112
97 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 113 if ($GRAB) {
114 if ($ev->{button} == 4 || $ev->{button} == 5) {
115 # mousewheel
116 my $delta = $ev->{button} * 2 - 9;
117 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
98 118
99 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) 119 $ev->{dx} = $shift ? $delta : 0;
100 if $GRAB; 120 $ev->{dy} = $shift ? 0 : $delta;
121
122 $GRAB->emit (mouse_wheel => $ev);
123 } else {
124 $GRAB->emit (button_down => $ev)
125 }
126 }
101} 127}
102 128
103sub feed_sdl_button_up_event { 129sub feed_sdl_button_up_event {
104 my ($ev) = @_; 130 my ($ev) = @_;
105 my ($x, $y) = ($ev->{x}, $ev->{y});
106 131
107 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 132 my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y});
108 133
109 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 134 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
110 135
111 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) 136 $GRAB->emit (button_up => $ev)
112 if $GRAB; 137 if $GRAB && $ev->{button} != 4 && $ev->{button} != 5;
113 138
114 if (!$BUTTON_STATE) { 139 unless ($BUTTON_STATE) {
115 my $grab = $GRAB; undef $GRAB; 140 my $grab = $GRAB; undef $GRAB;
116 $grab->update if $grab; 141 $grab->update if $grab;
117 $GRAB->update if $GRAB; 142 $GRAB->update if $GRAB;
118 143
144 check_hover $widget;
119 $TOOLTIP_WATCHER->cb->(); 145 $TOOLTIP_WATCHER->invoke;
120 } 146 }
121} 147}
122 148
123sub feed_sdl_motion_event { 149sub feed_sdl_motion_event {
124 my ($ev) = @_; 150 my ($ev) = @_;
125 my ($x, $y) = ($ev->{x}, $ev->{y}); 151 my ($x, $y) = ($ev->{x}, $ev->{y});
126 152
127 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 153 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
128 154
129 if ($widget != $HOVER) { 155 check_hover $widget;
130 my $hover = $HOVER; $HOVER = $widget;
131 156
132 $hover->update if $hover && $hover->{can_hover}; 157 $HOVER->emit (mouse_motion => $ev)
133 $HOVER->update if $HOVER && $HOVER->{can_hover};
134
135 $TOOLTIP_WATCHER->start;
136 }
137
138 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
139 if $HOVER; 158 if $HOVER;
140} 159}
141 160
142# convert position array to integers 161# convert position array to integers
143sub harmonize { 162sub harmonize {
171# call when resolution changes etc. 190# call when resolution changes etc.
172sub rescale_widgets { 191sub rescale_widgets {
173 my ($sx, $sy) = @_; 192 my ($sx, $sy) = @_;
174 193
175 for my $widget (values %WIDGET) { 194 for my $widget (values %WIDGET) {
176 if ($widget->{is_toplevel}) { 195 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
177 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 196 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
178 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 197 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
179 198
180 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 199 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
181 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 200 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
193 reconfigure_widgets; 212 reconfigure_widgets;
194} 213}
195 214
196############################################################################# 215#############################################################################
197 216
217package DC::UI::Event;
218
219sub xy {
220 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
221}
222
223#############################################################################
224
198package CFClient::UI::Base; 225package DC::UI::Base;
199 226
200use strict; 227use strict;
201 228
202use CFClient::OpenGL; 229use DC::OpenGL;
203 230
204sub new { 231sub new {
205 my $class = shift; 232 my $class = shift;
206 233
207 my $self = bless { 234 my $self = bless {
212 h => undef, 239 h => undef,
213 can_events => 1, 240 can_events => 1,
214 @_ 241 @_
215 }, $class; 242 }, $class;
216 243
217 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 244 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
218 245
219 for (keys %$self) { 246 for (keys %$self) {
220 if (/^on_(.*)$/) { 247 if (/^on_(.*)$/) {
221 $self->connect ($1 => delete $self->{$_}); 248 $self->connect ($1 => delete $self->{$_});
222 } 249 }
223 } 250 }
224 251
225 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 252 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
226 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{x}; 253 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
227 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{y}; 254 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
228 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{w}; 255 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
229 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{h}; 256 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
230 257
231 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 258 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
232 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 259 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
233 260
234 $self->show if $layout->{show}; 261 $self->show if $layout->{show};
239 266
240sub destroy { 267sub destroy {
241 my ($self) = @_; 268 my ($self) = @_;
242 269
243 $self->hide; 270 $self->hide;
271 $self->emit ("destroy");
244 %$self = (); 272 %$self = ();
245} 273}
246 274
275sub TO_JSON {
276 { "\fw" => $_[0]{s_id} }
277}
278
247sub show { 279sub show {
248 my ($self) = @_; 280 my ($self) = @_;
249 281
250 return if $self->{parent}; 282 return if $self->{parent};
251 283
252 $CFClient::UI::ROOT->add ($self); 284 $DC::UI::ROOT->add ($self);
253} 285}
254 286
255sub set_visible { 287sub set_visible {
256 my ($self) = @_; 288 my ($self) = @_;
257 289
258 return if $self->{visible}; 290 return if $self->{visible};
291
292 $self->{parent} && $self->{parent}{root}#d#
293 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
259 294
260 $self->{root} = $self->{parent}{root}; 295 $self->{root} = $self->{parent}{root};
261 $self->{visible} = $self->{parent}{visible} + 1; 296 $self->{visible} = $self->{parent}{visible} + 1;
262 297
263 $self->emit (visibility_change => 1); 298 $self->emit (visibility_change => 1);
264 299
265 $self->realloc if !exists $self->{req_w}; 300 $self->realloc if !exists $self->{req_w};
266 301
267 $_->set_visible for $self->children; 302 $_->set_visible for $self->visible_children;
268} 303}
269 304
270sub set_invisible { 305sub set_invisible {
271 my ($self) = @_; 306 my ($self) = @_;
272 307
278 delete $self->{root}; 313 delete $self->{root};
279 314
280 undef $GRAB if $GRAB == $self; 315 undef $GRAB if $GRAB == $self;
281 undef $HOVER if $HOVER == $self; 316 undef $HOVER if $HOVER == $self;
282 317
283 $CFClient::UI::TOOLTIP_WATCHER->cb->() 318 $DC::UI::TOOLTIP_WATCHER->invoke
284 if $TOOLTIP->{owner} == $self; 319 if $TOOLTIP->{owner} == $self;
285 320
286 $self->emit ("focus_out"); 321 $self->emit ("focus_out");
287 $self->emit (visibility_change => 0); 322 $self->emit (visibility_change => 0);
288} 323}
290sub set_visibility { 325sub set_visibility {
291 my ($self, $visible) = @_; 326 my ($self, $visible) = @_;
292 327
293 return if $self->{visible} == $visible; 328 return if $self->{visible} == $visible;
294 329
295 $visible ? $self->hide 330 $visible ? $self->show
296 : $self->show; 331 : $self->hide;
297} 332}
298 333
299sub toggle_visibility { 334sub toggle_visibility {
300 my ($self) = @_; 335 my ($self) = @_;
301 336
307sub hide { 342sub hide {
308 my ($self) = @_; 343 my ($self) = @_;
309 344
310 $self->set_invisible; 345 $self->set_invisible;
311 346
347 # extra $parent copy for 5.8.8+ bug workaround
348 # (otherwise $_[0] in remove gets freed
349 if (my $parent = $self->{parent}) {
312 $self->{parent}->remove ($self) 350 $parent->remove ($self);
313 if $self->{parent}; 351 }
314} 352}
315 353
316sub move_abs { 354sub move_abs {
317 my ($self, $x, $y, $z) = @_; 355 my ($self, $x, $y, $z) = @_;
318 356
330 $self->{force_h} = $h; 368 $self->{force_h} = $h;
331 369
332 $self->realloc; 370 $self->realloc;
333} 371}
334 372
373# traverse the widget chain up to find the maximum "physical" size constraints
374sub get_max_wh {
375 my ($self) = @_;
376
377 my ($w, $h) = @$self{qw(max_w max_h)};
378
379 if ($w <= 0 || $h <= 0) {
380 my ($mw, $mh) = $self->{parent}
381 ? $self->{parent}->get_max_wh
382 : ($::WIDTH, $::HEIGHT);
383
384 $w = $mw if $w <= 0;
385 $h = $mh if $h <= 0;
386 }
387
388 ($w, $h)
389}
390
335sub size_request { 391sub size_request {
336 require Carp; 392 require Carp;
337 Carp::confess "size_request is abstract"; 393 Carp::confess "size_request is abstract";
338} 394}
339 395
345 my ($self, $x, $y, $w, $h) = @_; 401 my ($self, $x, $y, $w, $h) = @_;
346 402
347 if ($self->{aspect}) { 403 if ($self->{aspect}) {
348 my ($ow, $oh) = ($w, $h); 404 my ($ow, $oh) = ($w, $h);
349 405
350 $w = List::Util::min $w, int $h * $self->{aspect}; 406 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
351 $h = List::Util::min $h, int $w / $self->{aspect}; 407 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
352 408
353 # use alignment to adjust x, y 409 # use alignment to adjust x, y
354 410
355 $x += int 0.5 * ($ow - $w); 411 $x += int 0.5 * ($ow - $w);
356 $y += int 0.5 * ($oh - $h); 412 $y += int 0.5 * ($oh - $h);
397 453
398 return if $self->{tooltip} eq $tooltip; 454 return if $self->{tooltip} eq $tooltip;
399 455
400 $self->{tooltip} = $tooltip; 456 $self->{tooltip} = $tooltip;
401 457
402 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 458 if ($DC::UI::TOOLTIP->{owner} == $self) {
403 delete $CFClient::UI::TOOLTIP->{owner}; 459 delete $DC::UI::TOOLTIP->{owner};
404 $CFClient::UI::TOOLTIP_WATCHER->cb->(); 460 $DC::UI::TOOLTIP_WATCHER->invoke;
405 } 461 }
406} 462}
407 463
408# translate global coordinates to local coordinate system 464# translate global coordinates to local coordinate system
409sub coord2local { 465sub coord2local {
410 my ($self, $x, $y) = @_; 466 my ($self, $x, $y) = @_;
411 467
468 return (undef, undef) unless $self->{parent};
469
412 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 470 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
413} 471}
414 472
415# translate local coordinates to global coordinate system 473# translate local coordinates to global coordinate system
416sub coord2global { 474sub coord2global {
417 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
418 476
477 return (undef, undef) unless $self->{parent};
478
419 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 479 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
420} 480}
421 481
422sub invoke_focus_in { 482sub invoke_focus_in {
423 my ($self) = @_; 483 my ($self) = @_;
452 512
453 $FOCUS->emit ("focus_out") if $FOCUS; 513 $FOCUS->emit ("focus_out") if $FOCUS;
454 $self->emit ("focus_in"); 514 $self->emit ("focus_in");
455} 515}
456 516
457sub invoke_mouse_motion { 1 } 517sub invoke_mouse_motion { 0 }
458sub invoke_button_up { 1 } 518sub invoke_button_up { 0 }
459sub invoke_key_down { 1 } 519sub invoke_key_down { 0 }
460sub invoke_key_up { 1 } 520sub invoke_key_up { 0 }
521sub invoke_mouse_wheel { 0 }
461 522
462sub invoke_button_down { 523sub invoke_button_down {
463 my ($self, $ev, $x, $y) = @_; 524 my ($self, $ev, $x, $y) = @_;
464 525
465 $self->grab_focus; 526 $self->grab_focus;
466 527
467 1 528 0
468} 529}
469 530
470sub connect { 531sub connect {
471 my ($self, $signal, $cb) = @_; 532 my ($self, $signal, $cb) = @_;
472 533
473 push @{ $self->{signal_cb}{$signal} }, $cb; 534 push @{ $self->{signal_cb}{$signal} }, $cb;
535
536 defined wantarray and Guard::guard {
537 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
538 @{ $self->{signal_cb}{$signal} };
539 }
474} 540}
541
542sub disconnect_all {
543 my ($self, $signal) = @_;
544
545 delete $self->{signal_cb}{$signal};
546}
547
548my %has_coords = (
549 button_down => 1,
550 button_up => 1,
551 mouse_motion => 1,
552 mouse_wheel => 1,
553);
475 554
476sub emit { 555sub emit {
477 my ($self, $signal, @args) = @_; 556 my ($self, $signal, @args) = @_;
478 557
479 #d##TODO# stop propagating at first true, do not use sum 558 # I do not really like this solution, but I do not like duplication
480 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before 559 # and needlessly verbose code, either.
560 my @append
561 = $has_coords{$signal}
562 ? $args[0]->xy ($self)
563 : ();
564
565 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
566
567 for my $cb (
568 @{$self->{signal_cb}{$signal} || []}, # before
481 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure 569 ($self->can ("invoke_$signal") || sub { 1 }), # closure
570 ) {
571 return $cb->($self, @args, @append) || next;
572 }
573
574 # parent
482 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 575 $self->{parent} && $self->{parent}->emit ($signal, @args)
483} 576}
484 577
485sub find_widget { 578#sub find_widget {
486 my ($self, $x, $y) = @_; 579# in .xs
487
488 return () unless $self->{can_events};
489
490 return $self
491 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
492 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
493
494 ()
495}
496 580
497sub set_parent { 581sub set_parent {
498 my ($self, $parent) = @_; 582 my ($self, $parent) = @_;
499 583
500 Scalar::Util::weaken ($self->{parent} = $parent); 584 DC::weaken ($self->{parent} = $parent);
501 $self->set_visible if $parent->{visible}; 585 $self->set_visible if $parent->{visible};
502} 586}
503 587
504sub realloc { 588sub realloc {
505 my ($self) = @_; 589 my ($self) = @_;
531 615
532# using global variables seems a bit hacky, but passing through all drawing 616# using global variables seems a bit hacky, but passing through all drawing
533# functions seems pointless. 617# functions seems pointless.
534our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 618our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
535 619
536sub draw { 620#sub draw {
537 my ($self) = @_; 621#CFPlus.xs
538
539 return unless $self->{h} && $self->{w};
540
541 # update screen rectangle
542 local $draw_x = $draw_x + $self->{x};
543 local $draw_y = $draw_y + $self->{y};
544
545 # skip widgets that are entirely outside the drawing area
546 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
547 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
548
549 glPushMatrix;
550 glTranslate $self->{x}, $self->{y}, 0;
551
552 if ($self == $HOVER && $self->{can_hover}) {
553 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
554 glEnable GL_BLEND;
555 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
556 glBegin GL_QUADS;
557 glVertex 0 , 0;
558 glVertex $self->{w}, 0;
559 glVertex $self->{w}, $self->{h};
560 glVertex 0 , $self->{h};
561 glEnd;
562 glDisable GL_BLEND;
563 }
564
565 if ($ENV{CFPLUS_DEBUG} & 1) {
566 glPushMatrix;
567 glColor 1, 1, 0, 1;
568 glTranslate 0.375, 0.375;
569 glBegin GL_LINE_LOOP;
570 glVertex 0 , 0;
571 glVertex $self->{w} - 1, 0;
572 glVertex $self->{w} - 1, $self->{h} - 1;
573 glVertex 0 , $self->{h} - 1;
574 glEnd;
575 glPopMatrix;
576 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
577 }
578
579 $self->_draw;
580 glPopMatrix;
581}
582 622
583sub _draw { 623sub _draw {
584 my ($self) = @_; 624 my ($self) = @_;
585 625
586 warn "no draw defined for $self\n"; 626 warn "no draw defined for $self\n";
587} 627}
588 628
589sub DESTROY { 629sub DESTROY {
590 my ($self) = @_; 630 my ($self) = @_;
591 631
592 delete $WIDGET{$self+0}; 632 return if DC::in_destruct;
593 633
634 local $@;
594 eval { $self->destroy }; 635 eval { $self->destroy };
595 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
637
638 delete $WIDGET{$self+0};
596} 639}
597 640
598############################################################################# 641#############################################################################
599 642
600package CFClient::UI::DrawBG; 643package DC::UI::DrawBG;
601 644
602our @ISA = CFClient::UI::Base::; 645our @ISA = DC::UI::Base::;
603 646
604use strict; 647use strict;
605use CFClient::OpenGL; 648use DC::OpenGL;
606 649
607sub new { 650sub new {
608 my $class = shift; 651 my $class = shift;
609
610 # range [value, low, high, page]
611 652
612 $class->SUPER::new ( 653 $class->SUPER::new (
613 #bg => [0, 0, 0, 0.2], 654 #bg => [0, 0, 0, 0.2],
614 #active_bg => [1, 1, 1, 0.5], 655 #active_bg => [1, 1, 1, 0.5],
615 @_ 656 @_
616 ) 657 )
617} 658}
618 659
660sub set_bg {
661 my ($self, $bg) = @_;
662
663 $self->{bg} = $bg;
664 $self->update;
665}
666
619sub _draw { 667sub _draw {
620 my ($self) = @_; 668 my ($self) = @_;
621 669
622 my $color = $FOCUS == $self && $self->{active_bg} 670 my $color = $FOCUS == $self
623 ? $self->{active_bg} 671 ? $self->{active_bg} || $self->{bg}
624 : $self->{bg}; 672 : $self->{bg};
625 673
626 if ($color && (@$color < 4 || $color->[3])) { 674 if ($color && (@$color < 4 || $color->[3])) {
627 my ($w, $h) = @$self{qw(w h)}; 675 my ($w, $h) = @$self{qw(w h)};
628 676
629 glEnable GL_BLEND; 677 glEnable GL_BLEND;
630 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 678 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
631 glColor_premultiply @$color; 679 glColor_premultiply @$color;
632
633 glBegin GL_QUADS;
634 glVertex 0 , 0;
635 glVertex 0 , $h;
636 glVertex $w, $h; 680 glRect 0, 0, $w, $h;
637 glVertex $w, 0;
638 glEnd;
639
640 glDisable GL_BLEND; 681 glDisable GL_BLEND;
641 } 682 }
642} 683}
643 684
644############################################################################# 685#############################################################################
645 686
646package CFClient::UI::Empty; 687package DC::UI::Empty;
647 688
648our @ISA = CFClient::UI::Base::; 689our @ISA = DC::UI::Base::;
649 690
650sub new { 691sub new {
651 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
652 $class->SUPER::new (can_events => 0, %arg); 693 $class->SUPER::new (can_events => 0, %arg);
653} 694}
660 701
661sub draw { } 702sub draw { }
662 703
663############################################################################# 704#############################################################################
664 705
665package CFClient::UI::Container; 706package DC::UI::Container;
666 707
667our @ISA = CFClient::UI::Base::; 708our @ISA = DC::UI::Base::;
668 709
669sub new { 710sub new {
670 my ($class, %arg) = @_; 711 my ($class, %arg) = @_;
671 712
672 my $children = delete $arg{children}; 713 my $children = delete $arg{children};
676 can_events => 0, 717 can_events => 0,
677 %arg, 718 %arg,
678 ); 719 );
679 720
680 $self->add (@$children) 721 $self->add (@$children)
681 if $children; 722 if $children && @$children;
682 723
683 $self 724 $self
725}
726
727sub realloc {
728 my ($self) = @_;
729
730 $self->{force_realloc} = 1;
731 $self->{force_size_alloc} = 1;
732 $self->SUPER::realloc;
684} 733}
685 734
686sub add { 735sub add {
687 my ($self, @widgets) = @_; 736 my ($self, @widgets) = @_;
688 737
689 $_->set_parent ($self) 738 $_->set_parent ($self)
690 for @widgets; 739 for @widgets;
691 740
741 # TODO: only do this in widgets that need it, e.g. root, fixed
692 use sort 'stable'; 742 use sort 'stable';
693 743
694 $self->{children} = [ 744 $self->{children} = [
695 sort { $a->{z} <=> $b->{z} } 745 sort { $a->{z} <=> $b->{z} }
696 @{$self->{children}}, @widgets 746 @{$self->{children}}, @widgets
697 ]; 747 ];
698 748
699 $self->realloc; 749 $self->realloc;
750
751 $self->emit (c_add => \@widgets);
752
753 map $_+0, @widgets
700} 754}
701 755
702sub children { 756sub children {
703 @{ $_[0]{children} } 757 @{ $_[0]{children} }
704} 758}
705 759
706sub remove { 760sub remove {
707 my ($self, $child) = @_; 761 my ($self, @widgets) = @_;
708 762
763 $self->emit (c_remove => \@widgets);
764
765 for my $child (@widgets) {
709 delete $child->{parent}; 766 delete $child->{parent};
710 $child->hide; 767 $child->hide;
711
712 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 768 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
769 }
713 770
714 $self->realloc; 771 $self->realloc;
715} 772}
716 773
717sub clear { 774sub clear {
718 my ($self) = @_; 775 my ($self) = @_;
719 776
720 my $children = delete $self->{children}; 777 my $children = $self->{children};
721 $self->{children} = []; 778 $self->{children} = [];
722 779
723 for (@$children) { 780 for (@$children) {
724 delete $_->{parent}; 781 delete $_->{parent};
725 $_->hide; 782 $_->hide;
745} 802}
746 803
747sub _draw { 804sub _draw {
748 my ($self) = @_; 805 my ($self) = @_;
749 806
750 $_->draw for @{$self->{children}}; 807 $_->draw for $self->visible_children;
751} 808}
752 809
753############################################################################# 810#############################################################################
754 811
755package CFClient::UI::Bin; 812package DC::UI::Bin;
756 813
757our @ISA = CFClient::UI::Container::; 814our @ISA = DC::UI::Container::;
758 815
759sub new { 816sub new {
760 my ($class, %arg) = @_; 817 my ($class, %arg) = @_;
761 818
762 my $child = (delete $arg{child}) || new CFClient::UI::Empty::; 819 my $child = (delete $arg{child}) || new DC::UI::Empty::;
763 820
764 $class->SUPER::new (children => [$child], %arg) 821 $class->SUPER::new (children => [$child], %arg)
765} 822}
766 823
767sub add { 824sub add {
768 my ($self, $child) = @_; 825 my ($self, $child) = @_;
769 826
770 $self->SUPER::remove ($_) for @{ $self->{children} }; 827 $self->clear;
771 $self->SUPER::add ($child); 828 $self->SUPER::add ($child);
772} 829}
773 830
774sub remove { 831sub remove {
775 my ($self, $widget) = @_; 832 my ($self, $widget) = @_;
776 833
777 $self->SUPER::remove ($widget); 834 $self->SUPER::remove ($widget);
778 835
779 $self->{children} = [new CFClient::UI::Empty] 836 $self->{children} = [new DC::UI::Empty]
780 unless @{$self->{children}}; 837 unless @{$self->{children}};
781} 838}
782 839
783sub child { $_[0]->{children}[0] } 840sub child { $_[0]->{children}[0] }
784 841
793 850
794 1 851 1
795} 852}
796 853
797############################################################################# 854#############################################################################
798
799# back-buffered drawing area 855# back-buffered drawing area
800 856
801package CFClient::UI::Window; 857package DC::UI::Window;
802 858
803our @ISA = CFClient::UI::Bin::; 859our @ISA = DC::UI::Bin::;
804 860
805use CFClient::OpenGL; 861use DC::OpenGL;
806 862
807sub new { 863sub new {
808 my ($class, %arg) = @_; 864 my ($class, %arg) = @_;
809 865
810 my $self = $class->SUPER::new (%arg); 866 my $self = $class->SUPER::new (%arg);
832} 888}
833 889
834sub render_child { 890sub render_child {
835 my ($self) = @_; 891 my ($self) = @_;
836 892
837 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 893 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
838 glClearColor 0, 0, 0, 0; 894 glClearColor 0, 0, 0, 0;
839 glClear GL_COLOR_BUFFER_BIT; 895 glClear GL_COLOR_BUFFER_BIT;
840 896
841 { 897 {
842 package CFClient::UI::Base; 898 package DC::UI::Base;
843 899
844 ($draw_x, $draw_y, $draw_w, $draw_h) = 900 local ($draw_x, $draw_y, $draw_w, $draw_h) =
845 (0, 0, $self->{w}, $self->{h}); 901 (0, 0, $self->{w}, $self->{h});
902
903 $self->_render;
846 } 904 }
847
848 $self->_render;
849 }; 905 };
850} 906}
851 907
852sub _draw { 908sub _draw {
853 my ($self) = @_; 909 my ($self) = @_;
854
855 my ($w, $h) = @$self{qw(w h)};
856 910
857 my $tex = $self->{texture} 911 my $tex = $self->{texture}
858 or return; 912 or return;
859 913
860 glEnable GL_TEXTURE_2D; 914 glEnable GL_TEXTURE_2D;
861 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 915 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
862 glColor 0, 0, 0, 1; 916 glColor 0, 0, 0, 1;
863 917
864 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 918 $tex->draw_quad_alpha_premultiplied (0, 0);
865 919
866 glDisable GL_TEXTURE_2D; 920 glDisable GL_TEXTURE_2D;
867} 921}
868 922
869############################################################################# 923#############################################################################
870 924
871package CFClient::UI::ViewPort; 925package DC::UI::ViewPort;
872 926
927use List::Util qw(min max);
928
873our @ISA = CFClient::UI::Window::; 929our @ISA = DC::UI::Window::;
874 930
875sub new { 931sub new {
876 my $class = shift; 932 my $class = shift;
877 933
878 $class->SUPER::new ( 934 $class->SUPER::new (
885sub size_request { 941sub size_request {
886 my ($self) = @_; 942 my ($self) = @_;
887 943
888 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 944 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
889 945
890 $w = 10 if $self->{scroll_x}; 946 $w = 1 if $self->{scroll_x};
891 $h = 10 if $self->{scroll_y}; 947 $h = 1 if $self->{scroll_y};
892 948
893 ($w, $h) 949 ($w, $h)
894} 950}
895 951
896sub invoke_size_allocate { 952sub invoke_size_allocate {
908} 964}
909 965
910sub set_offset { 966sub set_offset {
911 my ($self, $x, $y) = @_; 967 my ($self, $x, $y) = @_;
912 968
969 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
970 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
971
972 if ($x != $self->{view_x} or $y != $self->{view_y}) {
913 $self->{view_x} = int $x; 973 $self->{view_x} = $x;
914 $self->{view_y} = int $y; 974 $self->{view_y} = $y;
915 975
976 $self->emit (changed => $x, $y);
916 $self->update; 977 $self->update;
978 }
979}
980
981sub set_center {
982 my ($self, $x, $y) = @_;
983
984 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
985}
986
987sub make_visible {
988 my ($self, $x, $y, $border) = @_;
989
990 if ( $x < $self->{view_x} + $self->{w} * $border
991 || $x > $self->{view_x} + $self->{w} * (1 - $border)
992 || $y < $self->{view_y} + $self->{h} * $border
993 || $y > $self->{view_y} + $self->{h} * (1 - $border)
994 ) {
995 $self->set_center ($x, $y);
996 }
917} 997}
918 998
919# hmm, this does not work for topleft of $self... but we should not ask for that 999# hmm, this does not work for topleft of $self... but we should not ask for that
920sub coord2local { 1000sub coord2local {
921 my ($self, $x, $y) = @_; 1001 my ($self, $x, $y) = @_;
936 my ($self, $x, $y) = @_; 1016 my ($self, $x, $y) = @_;
937 1017
938 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
939 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
940 ) { 1020 ) {
941 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
942 } else { 1022 } else {
943 $self->CFClient::UI::Base::find_widget ($x, $y) 1023 $self->DC::UI::Base::find_widget ($x, $y)
944 } 1024 }
945} 1025}
946 1026
947sub _render { 1027sub _render {
948 my ($self) = @_; 1028 my ($self) = @_;
949 1029
950 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x}; 1030 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
951 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y}; 1031 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
952 1032
953 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1033 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
954 1034
955 $self->SUPER::_render; 1035 $self->SUPER::_render;
956} 1036}
957 1037
958############################################################################# 1038#############################################################################
959 1039
960package CFClient::UI::ScrolledWindow; 1040package DC::UI::ScrolledWindow;
961 1041
962our @ISA = CFClient::UI::HBox::; 1042our @ISA = DC::UI::Table::;
963 1043
964sub new { 1044sub new {
965 my ($class, %arg) = @_; 1045 my ($class, %arg) = @_;
966 1046
967 my $child = delete $arg{child}; 1047 my $child = delete $arg{child};
968 1048
969 my $self; 1049 my $self;
970 1050
971 my $slider = new CFClient::UI::Slider 1051 my $hslider = new DC::UI::Slider
1052 c_col => 0,
1053 c_row => 1,
1054 vertical => 0,
1055 range => [0, 0, 1, 0.01], # HACK fix
1056 on_changed => sub {
1057 $self->{hpos} = $_[1];
1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 },
1060 ;
1061
1062 my $vslider = new DC::UI::Slider
1063 c_col => 1,
1064 c_row => 0,
972 vertical => 1, 1065 vertical => 1,
973 range => [0, 0, 1, 0.01], # HACK fix 1066 range => [0, 0, 1, 0.01], # HACK fix
974 on_changed => sub { 1067 on_changed => sub {
975 $self->{vp}->set_offset (0, $_[1]); 1068 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
976 }, 1070 },
977 ; 1071 ;
978 1072
979 $self = $class->SUPER::new ( 1073 $self = $class->SUPER::new (
980 vp => (new CFClient::UI::ViewPort expand => 1), 1074 scroll_x => 0,
1075 scroll_y => 1,
1076 can_events => 1,
981 slider => $slider, 1077 hslider => $hslider,
1078 vslider => $vslider,
1079 col_expand => [1, 0],
1080 row_expand => [1, 0],
982 %arg, 1081 %arg,
983 ); 1082 );
984 1083
1084 $self->{vp} = new DC::UI::ViewPort
1085 c_col => 0,
1086 c_row => 0,
1087 expand => 1,
1088 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y},
1090 on_changed => sub {
1091 my ($vp, $x, $y) = @_;
1092
1093 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y);
1095
1096 0
1097 },
1098 on_size_allocate => sub {
1099 my ($vp, $w, $h) = @_;
1100 $vp->{parent}->update_slider;
1101 0
1102 },
1103 ;
1104
985 $self->SUPER::add ($self->{vp}, $self->{slider}); 1105 $self->SUPER::add ($self->{vp});
1106
986 $self->add ($child) if $child; 1107 $self->add ($child) if $child;
987 1108
988 $self 1109 $self
989} 1110}
990
991#TODO# update range on size_allocate depending on child
992 1111
993sub add { 1112sub add {
994 my ($self, $widget) = @_; 1113 my ($self, $widget) = @_;
995 1114
996 $self->{vp}->add ($self->{child} = $widget); 1115 $self->{vp}->add ($self->{child} = $widget);
997} 1116}
998 1117
1118sub set_offset { shift->{vp}->set_offset (@_) }
1119sub set_center { shift->{vp}->set_center (@_) }
1120sub make_visible { shift->{vp}->make_visible (@_) }
1121
999sub update_slider { 1122sub update_slider {
1000 my ($self) = @_; 1123 my ($self) = @_;
1001 1124
1125 my $child = ($self->{vp} or return)->child;
1126
1127 if ($self->{scroll_x}) {
1128 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1002 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1129 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1003}
1004 1130
1005sub update { 1131 my $visible = $w1 > $w2;
1132 if ($visible != $self->{hslider_visible}) {
1133 $self->{hslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{hslider})
1135 : $self->SUPER::remove ($self->{hslider});
1136 }
1137 }
1138
1139 if ($self->{scroll_y}) {
1140 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1141 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1142
1143 my $visible = $h1 > $h2;
1144 if ($visible != $self->{vslider_visible}) {
1145 $self->{vslider_visible} = $visible;
1146 $visible ? $self->SUPER::add ($self->{vslider})
1147 : $self->SUPER::remove ($self->{vslider});
1148 }
1149 }
1150}
1151
1152sub start_dragging {
1006 my ($self) = @_; 1153 my ($self, $ev) = @_;
1007 1154
1008 $self->SUPER::update; 1155 $self->grab_focus;
1009 1156
1010 $self->update_slider; 1157 my $ox = $self->{vp}{view_x};
1158 my $oy = $self->{vp}{view_y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1162
1163 $ox -= $ev->{xrel};
1164 $oy -= $ev->{yrel};
1165
1166 $self->{vp}->set_offset ($ox, $oy);
1167 };
1168}
1169
1170sub invoke_mouse_wheel {
1171 my ($self, $ev) = @_;
1172
1173 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1174 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1175
1176 1
1177}
1178
1179sub invoke_button_down {
1180 my ($self, $ev, $x, $y) = @_;
1181
1182 if ($ev->{button} == 2) {
1183 $self->start_dragging ($ev);
1184 return 1;
1185 }
1186
1187 0
1188}
1189
1190sub invoke_button_up {
1191 my ($self, $ev, $x, $y) = @_;
1192
1193 if (delete $self->{motion}) {
1194 return 1;
1195 }
1196
1197 0
1198}
1199
1200sub invoke_mouse_motion {
1201 my ($self, $ev, $x, $y) = @_;
1202
1203 if ($self->{motion}) {
1204 $self->{motion}->($ev, $x, $y);
1205 return 1;
1206 }
1207
1208 0
1011} 1209}
1012 1210
1013sub invoke_size_allocate { 1211sub invoke_size_allocate {
1014 my ($self, $w, $h) = @_; 1212 my ($self, $w, $h) = @_;
1015 1213
1016 $self->update_slider; 1214 $self->update_slider;
1017
1018 $self->SUPER::invoke_size_allocate ($w, $h) 1215 $self->SUPER::invoke_size_allocate ($w, $h)
1019} 1216}
1020 1217
1021############################################################################# 1218#############################################################################
1022 1219
1023package CFClient::UI::Frame; 1220package DC::UI::Frame;
1024 1221
1025our @ISA = CFClient::UI::Bin::; 1222our @ISA = DC::UI::Bin::;
1026 1223
1027use CFClient::OpenGL; 1224use DC::OpenGL;
1028 1225
1029sub new { 1226sub new {
1030 my $class = shift; 1227 my $class = shift;
1031 1228
1032 $class->SUPER::new ( 1229 $class->SUPER::new (
1042 my ($w, $h) = @$self{qw(w h)}; 1239 my ($w, $h) = @$self{qw(w h)};
1043 1240
1044 glEnable GL_BLEND; 1241 glEnable GL_BLEND;
1045 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1242 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1046 glColor_premultiply @{ $self->{bg} }; 1243 glColor_premultiply @{ $self->{bg} };
1047
1048 glBegin GL_QUADS;
1049 glVertex 0 , 0;
1050 glVertex 0 , $h;
1051 glVertex $w, $h; 1244 glRect 0, 0, $w, $h;
1052 glVertex $w, 0;
1053 glEnd;
1054
1055 glDisable GL_BLEND; 1245 glDisable GL_BLEND;
1056 } 1246 }
1057 1247
1058 $self->SUPER::_draw; 1248 $self->SUPER::_draw;
1059} 1249}
1060 1250
1061############################################################################# 1251#############################################################################
1062 1252
1063package CFClient::UI::FancyFrame; 1253package DC::UI::FancyFrame;
1064 1254
1065our @ISA = CFClient::UI::Bin::; 1255our @ISA = DC::UI::Bin::;
1066 1256
1067use CFClient::OpenGL; 1257use DC::OpenGL;
1258
1259sub new {
1260 my ($class, %arg) = @_;
1261
1262 if ((exists $arg{label}) && !ref $arg{label}) {
1263 $arg{label} = new DC::UI::Label
1264 align => 1,
1265 valign => 0.5,
1266 text => $arg{label},
1267 fontsize => ($arg{border} || 0.8) * 0.75;
1268 }
1269
1270 my $self = $class->SUPER::new (
1271 # label => "",
1272 fg => undef,
1273 border => 0.8,
1274 style => 'single',
1275 %arg,
1276 );
1277
1278 $self
1279}
1280
1281sub add {
1282 my ($self, @widgets) = @_;
1283
1284 $self->SUPER::add (@widgets);
1285 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1286}
1287
1288sub border {
1289 int $_[0]{border} * $::FONTSIZE
1290}
1291
1292sub size_request {
1293 my ($self) = @_;
1294
1295 ($self->{label_w}, undef) = $self->{label}->size_request
1296 if $self->{label};
1297
1298 my ($w, $h) = $self->SUPER::size_request;
1299
1300 (
1301 $w + $self->border * 2,
1302 $h + $self->border * 2,
1303 )
1304}
1305
1306sub invoke_size_allocate {
1307 my ($self, $w, $h) = @_;
1308
1309 my $border = $self->border;
1310
1311 $w -= List::Util::max 0, $border * 2;
1312 $h -= List::Util::max 0, $border * 2;
1313
1314 if (my $label = $self->{label}) {
1315 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1316 $label->{h} = List::Util::min $h, $border;
1317 $label->invoke_size_allocate ($label->{w}, $label->{h});
1318 }
1319
1320 $self->child->configure ($border, $border, $w, $h);
1321
1322 1
1323}
1324
1325sub _draw {
1326 my ($self) = @_;
1327
1328 my $child = $self->{children}[0];
1329
1330 my $border = $self->border;
1331 my ($w, $h) = ($self->{w}, $self->{h});
1332
1333 $child->draw;
1334
1335 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1336 glBegin GL_LINE_STRIP;
1337 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1338 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1339 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1340 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1341 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1342 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1343 glEnd;
1344
1345 if ($self->{label}) {
1346 glTranslate $border * 2, 0;
1347 $self->{label}->_draw;
1348 }
1349}
1350
1351#############################################################################
1352
1353package DC::UI::Toplevel;
1354
1355our @ISA = DC::UI::Bin::;
1356
1357use DC::OpenGL;
1068 1358
1069my $bg = 1359my $bg =
1070 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", 1360 new_from_resource DC::Texture "d1_bg.png",
1071 mipmap => 1, wrap => 1; 1361 mipmap => 1, wrap => 1;
1072 1362
1073my @border = 1363my @border =
1074 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1364 map { new_from_resource DC::Texture $_, mipmap => 1 }
1075 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1365 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1366
1367my @icon =
1368 map { new_from_resource DC::Texture $_, mipmap => 1 }
1369 qw(x1_move.png x1_resize.png);
1076 1370
1077sub new { 1371sub new {
1078 my ($class, %arg) = @_; 1372 my ($class, %arg) = @_;
1079 1373
1080 my $self = $class->SUPER::new ( 1374 my $self = $class->SUPER::new (
1081 bg => [1, 1, 1, 1], 1375 bg => [1, 1, 1, 1],
1082 border_bg => [1, 1, 1, 1], 1376 border_bg => [1, 1, 1, 1],
1083 border => 0.6, 1377 border => 0.8,
1084 can_events => 1, 1378 can_events => 1,
1085 min_w => 16, 1379 min_w => 64,
1086 min_h => 16, 1380 min_h => 32,
1087 %arg, 1381 %arg,
1088 ); 1382 );
1089 1383
1090 $self->{title_widget} = new CFClient::UI::Label 1384 $self->{title_widget} = new DC::UI::Label
1091 align => 0, 1385 align => 0.5,
1092 valign => 1, 1386 valign => 1,
1093 text => $self->{title}, 1387 text => $self->{title},
1094 fontsize => $self->{border}, 1388 fontsize => $self->{border},
1095 if exists $self->{title}; 1389 if exists $self->{title};
1096 1390
1097 if ($self->{has_close_button}) { 1391 if ($self->{has_close_button}) {
1098 $self->{close_button} = 1392 $self->{close_button} =
1099 new CFClient::UI::ImageButton 1393 new DC::UI::ImageButton
1100 path => 'x1_close.png', 1394 path => 'x1_close.png',
1101 on_activate => sub { $self->emit ("delete") }; 1395 on_activate => sub { $self->emit ("delete") };
1102 1396
1103 $self->CFClient::UI::Container::add ($self->{close_button}); 1397 $self->DC::UI::Container::add ($self->{close_button});
1104 } 1398 }
1105 1399
1106 $self 1400 $self
1107} 1401}
1108 1402
1109sub add { 1403sub add {
1110 my ($self, @widgets) = @_; 1404 my ($self, @widgets) = @_;
1111 1405
1112 $self->SUPER::add (@widgets); 1406 $self->SUPER::add (@widgets);
1113 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1407 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1114 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1408 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1115} 1409}
1116 1410
1117sub border { 1411sub border {
1118 int $_[0]{border} * $::FONTSIZE 1412 int $_[0]{border} * $::FONTSIZE
1413}
1414
1415sub get_max_wh {
1416 my ($self) = @_;
1417
1418 return ($self->{w}, $self->{h})
1419 if $self->{visible} && $self->{w};
1420
1421 $self->SUPER::get_max_wh
1119} 1422}
1120 1423
1121sub size_request { 1424sub size_request {
1122 my ($self) = @_; 1425 my ($self) = @_;
1123 1426
1227 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1530 $self->{motion}->($ev, $x, $y) if $self->{motion};
1228 1531
1229 ! ! $self->{motion} 1532 ! ! $self->{motion}
1230} 1533}
1231 1534
1535sub invoke_visibility_change {
1536 my ($self, $visible) = @_;
1537
1538 delete $self->{motion} unless $visible;
1539
1540 0
1541}
1542
1232sub _draw { 1543sub _draw {
1233 my ($self) = @_; 1544 my ($self) = @_;
1234 1545
1235 my $child = $self->{children}[0]; 1546 my $child = $self->{children}[0];
1236 1547
1240 glEnable GL_TEXTURE_2D; 1551 glEnable GL_TEXTURE_2D;
1241 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1552 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1242 1553
1243 my $border = $self->border; 1554 my $border = $self->border;
1244 1555
1556 if ($border) {
1245 glColor @{ $self->{border_bg} }; 1557 glColor @{ $self->{border_bg} };
1246 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1558 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1247 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1559 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1248 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1560 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1249 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1561 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1562
1563 # move
1564 my $w2 = ($w - $border) * .5;
1565 my $h2 = ($h - $border) * .5;
1566 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1567 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1568 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1569
1570 # resize
1571 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1572 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1573 unless $self->{has_close_button};
1574 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1575 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1576 }
1250 1577
1251 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1578 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1252 glColor @{ $self->{bg} }; 1579 glColor @{ $self->{bg} };
1253 1580
1254 # TODO: repeat texture not scale 1581 # TODO: repeat texture not scale
1273 if $self->{close_button}; 1600 if $self->{close_button};
1274} 1601}
1275 1602
1276############################################################################# 1603#############################################################################
1277 1604
1278package CFClient::UI::Table; 1605package DC::UI::Table;
1279 1606
1280our @ISA = CFClient::UI::Base::; 1607our @ISA = DC::UI::Container::;
1281 1608
1282use List::Util qw(max sum); 1609use List::Util qw(max sum);
1283 1610
1284use CFClient::OpenGL; 1611use DC::OpenGL;
1285 1612
1286sub new { 1613sub new {
1287 my $class = shift; 1614 my $class = shift;
1288 1615
1289 $class->SUPER::new ( 1616 $class->SUPER::new (
1290 col_expand => [], 1617 col_expand => [],
1618 row_expand => [],
1291 @_, 1619 @_,
1292 ) 1620 )
1293} 1621}
1294 1622
1295sub children {
1296 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1297}
1298
1299sub add { 1623sub add {
1300 my ($self, $x, $y, $child) = @_; 1624 my ($self, @widgets) = @_;
1301 1625
1302 $child->set_parent ($self); 1626 for my $child (@widgets) {
1303 $self->{children}[$y][$x] = $child; 1627 $child->{c_rowspan} ||= 1;
1628 $child->{c_colspan} ||= 1;
1629 }
1304 1630
1305 $self->realloc; 1631 $self->SUPER::add (@widgets);
1306} 1632}
1307 1633
1308sub remove { 1634sub add_at {
1635 my $self = shift;
1636
1637 my @widgets;
1638
1639 while (@_) {
1640 my ($col, $row, $child) = splice @_, 0, 3, ();
1641
1642 $child->{c_row} = $row;
1643 $child->{c_col} = $col;
1644
1645 push @widgets, $child;
1646 }
1647
1648 $self->add (@widgets);
1649}
1650
1651sub get_wh {
1309 my ($self, $child) = @_; 1652 my ($self) = @_;
1310 1653
1311 # TODO: not yet implemented 1654 my (@w, @h);
1312}
1313
1314# TODO: move to container class maybe? send children a signal on removal?
1315sub clear {
1316 my ($self) = @_;
1317 1655
1318 my @children = $self->children; 1656 my @children = $self->children;
1319 delete $self->{children}; 1657
1658 # first pass, columns
1659 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1660 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1661
1662 my $sw = sum @w[$c .. $c + $cs - 1];
1663
1664 if ($w > $sw) {
1665 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1666 }
1320 1667 }
1321 for (@children) {
1322 delete $_->{parent};
1323 $_->hide;
1324 }
1325 1668
1326 $self->realloc; 1669 # second pass, rows
1327} 1670 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1328
1329sub get_wh {
1330 my ($self) = @_;
1331
1332 my (@w, @h);
1333
1334 for my $y (0 .. $#{$self->{children}}) {
1335 my $row = $self->{children}[$y]
1336 or next;
1337
1338 for my $x (0 .. $#$row) {
1339 my $widget = $row->[$x]
1340 or next;
1341 my ($w, $h) = @$widget{qw(req_w req_h)}; 1671 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1342 1672
1343 $w[$x] = max $w[$x], $w; 1673 my $sh = sum @h[$r .. $r + $rs - 1];
1344 $h[$y] = max $h[$y], $h; 1674
1675 if ($h > $sh) {
1676 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1345 } 1677 }
1346 } 1678 }
1347 1679
1348 (\@w, \@h) 1680 (\@w, \@h)
1349} 1681}
1365 my ($ws, $hs) = $self->get_wh; 1697 my ($ws, $hs) = $self->get_wh;
1366 1698
1367 my $req_w = (sum @$ws) || 1; 1699 my $req_w = (sum @$ws) || 1;
1368 my $req_h = (sum @$hs) || 1; 1700 my $req_h = (sum @$hs) || 1;
1369 1701
1370 # TODO: nicer code && do row_expand 1702 # now linearly scale the rows/columns to the allocated size
1371 my @col_expand = @{$self->{col_expand}}; 1703 my @col_expand = @{$self->{col_expand}};
1372 @col_expand = (1) x @$ws unless @col_expand; 1704 @col_expand = (1) x @$ws unless @col_expand;
1373 my $col_expand = (sum @col_expand) || 1; 1705 my $col_expand = (sum @col_expand) || 1;
1374 1706
1375 # linearly scale sizes
1376 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1707 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1377 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1378 1708
1379 CFClient::UI::harmonize $ws; 1709 DC::UI::harmonize $ws;
1710
1711 my @row_expand = @{$self->{row_expand}};
1712 @row_expand = (1) x @$ws unless @row_expand;
1713 my $row_expand = (sum @row_expand) || 1;
1714
1715 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1716
1380 CFClient::UI::harmonize $hs; 1717 DC::UI::harmonize $hs;
1381 1718
1382 my $y; 1719 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1720 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1383 1721
1384 for my $r (0 .. $#{$self->{children}}) { 1722 for my $widget ($self->children) {
1385 my $row = $self->{children}[$r] 1723 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1386 or next;
1387 1724
1388 my $x = 0; 1725 $widget->configure (
1389 my $row_h = $hs->[$r]; 1726 $x[$c], $y[$r],
1727 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1390 1728 );
1391 for my $c (0 .. $#$row) { 1729 }
1392 my $col_w = $ws->[$c];
1393 1730
1394 if (my $widget = $row->[$c]) { 1731 1
1395 $widget->configure ($x, $y, $col_w, $row_h); 1732}
1396 }
1397 1733
1398 $x += $col_w; 1734#############################################################################
1735
1736package DC::UI::Fixed;
1737
1738use List::Util qw(min max);
1739
1740our @ISA = DC::UI::Container::;
1741
1742sub _scale($$$) {
1743 my ($rel, $val, $max) = @_;
1744
1745 $rel ? $val * $max : $val
1746}
1747
1748sub size_request {
1749 my ($self) = @_;
1750
1751 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1752
1753 # determine overall size by querying abs widgets
1754 for my $child ($self->visible_children) {
1755 unless ($child->{c_rel}) {
1756 my $x = $child->{c_x};
1757 my $y = $child->{c_y};
1758
1759 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1760 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1399 } 1761 }
1762 }
1400 1763
1401 $y += $row_h; 1764 my $W = $x2 - $x1;
1765 my $H = $y2 - $y1;
1766
1767 # now layout remaining widgets
1768 for my $child ($self->visible_children) {
1769 if ($child->{c_rel}) {
1770 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1771 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1772
1773 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1774 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1775 }
1776 }
1777
1778 my $W = $x2 - $x1;
1779 my $H = $y2 - $y1;
1780
1781 ($W, $H)
1782}
1783
1784sub invoke_size_allocate {
1785 my ($self, $W, $H) = @_;
1786
1787 for my $child ($self->visible_children) {
1788 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1789 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1790
1791 $x += $child->{c_halign} * $child->{req_w};
1792 $y += $child->{c_valign} * $child->{req_h};
1793
1794 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1402 } 1795 }
1403 1796
1404 1 1797 1
1405} 1798}
1406 1799
1407sub find_widget {
1408 my ($self, $x, $y) = @_;
1409
1410 $x -= $self->{x};
1411 $y -= $self->{y};
1412
1413 my $res;
1414
1415 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1416 $res = $_->find_widget ($x, $y)
1417 and return $res;
1418 }
1419
1420 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1421}
1422
1423sub _draw {
1424 my ($self) = @_;
1425
1426 for (grep $_, @{$self->{children}}) {
1427 $_->draw for grep $_, @$_;
1428 }
1429}
1430
1431############################################################################# 1800#############################################################################
1432 1801
1433package CFClient::UI::Box; 1802package DC::UI::Box;
1434 1803
1435our @ISA = CFClient::UI::Container::; 1804our @ISA = DC::UI::Container::;
1436 1805
1437sub size_request { 1806sub size_request {
1438 my ($self) = @_; 1807 my ($self) = @_;
1808
1809 my @children = $self->visible_children;
1439 1810
1440 $self->{vertical} 1811 $self->{vertical}
1441 ? ( 1812 ? (
1442 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1813 (List::Util::max map $_->{req_w}, @children),
1443 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1814 (List::Util::sum map $_->{req_h}, @children),
1444 ) 1815 )
1445 : ( 1816 : (
1446 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1817 (List::Util::sum map $_->{req_w}, @children),
1447 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1818 (List::Util::max map $_->{req_h}, @children),
1448 ) 1819 )
1449} 1820}
1450 1821
1451sub invoke_size_allocate { 1822sub invoke_size_allocate {
1452 my ($self, $w, $h) = @_; 1823 my ($self, $w, $h) = @_;
1473 $req[$_] += $space * $children[$_]{expand} 1844 $req[$_] += $space * $children[$_]{expand}
1474 for 0 .. $#children; 1845 for 0 .. $#children;
1475 } 1846 }
1476 } 1847 }
1477 1848
1478 CFClient::UI::harmonize \@req; 1849 DC::UI::harmonize \@req;
1479 1850
1480 my $pos = 0; 1851 my $pos = 0;
1481 for (0 .. $#children) { 1852 for (0 .. $#children) {
1482 my $alloc = $req[$_]; 1853 my $alloc = $req[$_];
1483 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1854 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1488 1 1859 1
1489} 1860}
1490 1861
1491############################################################################# 1862#############################################################################
1492 1863
1493package CFClient::UI::HBox; 1864package DC::UI::HBox;
1494 1865
1495our @ISA = CFClient::UI::Box::; 1866our @ISA = DC::UI::Box::;
1496 1867
1497sub new { 1868sub new {
1498 my $class = shift; 1869 my $class = shift;
1499 1870
1500 $class->SUPER::new ( 1871 $class->SUPER::new (
1503 ) 1874 )
1504} 1875}
1505 1876
1506############################################################################# 1877#############################################################################
1507 1878
1508package CFClient::UI::VBox; 1879package DC::UI::VBox;
1509 1880
1510our @ISA = CFClient::UI::Box::; 1881our @ISA = DC::UI::Box::;
1511 1882
1512sub new { 1883sub new {
1513 my $class = shift; 1884 my $class = shift;
1514 1885
1515 $class->SUPER::new ( 1886 $class->SUPER::new (
1518 ) 1889 )
1519} 1890}
1520 1891
1521############################################################################# 1892#############################################################################
1522 1893
1523package CFClient::UI::Label; 1894package DC::UI::Label;
1524 1895
1525our @ISA = CFClient::UI::DrawBG::; 1896our @ISA = DC::UI::DrawBG::;
1526 1897
1527use CFClient::OpenGL; 1898use DC::OpenGL;
1528 1899
1529sub new { 1900sub new {
1530 my ($class, %arg) = @_; 1901 my ($class, %arg) = @_;
1531 1902
1532 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1535 #active_bg => none 1906 #active_bg => none
1536 #font => default_font 1907 #font => default_font
1537 #text => initial text 1908 #text => initial text
1538 #markup => initial narkup 1909 #markup => initial narkup
1539 #max_w => maximum pixel width 1910 #max_w => maximum pixel width
1911 #style => 0, # render flags
1540 ellipsise => 3, # end 1912 ellipsise => 3, # end
1541 layout => (new CFClient::Layout), 1913 layout => (new DC::Layout),
1542 fontsize => 1, 1914 fontsize => 1,
1543 align => -1, 1915 align => 0.5,
1544 valign => -1, 1916 valign => 0.5,
1545 padding_x => 2, 1917 padding_x => 4,
1546 padding_y => 2, 1918 padding_y => 2,
1547 can_events => 0, 1919 can_events => 0,
1548 %arg 1920 %arg
1549 ); 1921 );
1550 1922
1551 if (exists $self->{template}) { 1923 if (exists $self->{template}) {
1552 my $layout = new CFClient::Layout; 1924 my $layout = new DC::Layout;
1553 $layout->set_text (delete $self->{template}); 1925 $layout->set_text (delete $self->{template});
1554 $self->{template} = $layout; 1926 $self->{template} = $layout;
1555 } 1927 }
1556 1928
1557 if (exists $self->{markup}) { 1929 if (exists $self->{markup}) {
1561 } 1933 }
1562 1934
1563 $self 1935 $self
1564} 1936}
1565 1937
1566sub escape($) {
1567 local $_ = $_[0];
1568
1569 s/&/&amp;/g;
1570 s/>/&gt;/g;
1571 s/</&lt;/g;
1572
1573 $_
1574}
1575
1576sub update { 1938sub update {
1577 my ($self) = @_; 1939 my ($self) = @_;
1578 1940
1579 delete $self->{texture}; 1941 delete $self->{texture};
1580 $self->SUPER::update; 1942 $self->SUPER::update;
1585 1947
1586 delete $self->{ox}; 1948 delete $self->{ox};
1587 $self->SUPER::realloc; 1949 $self->SUPER::realloc;
1588} 1950}
1589 1951
1952sub clear {
1953 my ($self) = @_;
1954
1955 $self->set_text ("");
1956}
1957
1590sub set_text { 1958sub set_text {
1591 my ($self, $text) = @_; 1959 my ($self, $text) = @_;
1592 1960
1593 return if $self->{text} eq "T$text"; 1961 return if $self->{text} eq "T$text";
1594 $self->{text} = "T$text"; 1962 $self->{text} = "T$text";
1595 1963
1596 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1597 $self->{layout}->set_text ($text); 1964 $self->{layout}->set_text ($text);
1598 1965
1599 delete $self->{size_req}; 1966 delete $self->{size_req};
1600 $self->realloc; 1967 $self->realloc;
1601 $self->update; 1968 $self->update;
1607 return if $self->{text} eq "M$markup"; 1974 return if $self->{text} eq "M$markup";
1608 $self->{text} = "M$markup"; 1975 $self->{text} = "M$markup";
1609 1976
1610 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1977 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1611 1978
1612 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1613 $self->{layout}->set_markup ($markup); 1979 $self->{layout}->set_markup ($markup);
1614 1980
1615 delete $self->{size_req}; 1981 delete $self->{size_req};
1616 $self->realloc; 1982 $self->realloc;
1617 $self->update; 1983 $self->update;
1619 1985
1620sub size_request { 1986sub size_request {
1621 my ($self) = @_; 1987 my ($self) = @_;
1622 1988
1623 $self->{size_req} ||= do { 1989 $self->{size_req} ||= do {
1990 my ($max_w, $max_h) = $self->get_max_wh;
1991
1624 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1992 $self->{layout}->set_font ($self->{font}) if $self->{font};
1625 $self->{layout}->set_width ($self->{max_w} || -1); 1993 $self->{layout}->set_width ($max_w);
1626 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1994 $self->{layout}->set_ellipsise ($self->{ellipsise});
1627 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1995 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1628 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1996 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1629 1997
1630 my ($w, $h) = $self->{layout}->size; 1998 my ($w, $h) = $self->{layout}->size;
1631 1999
1632 if (exists $self->{template}) { 2000 if (exists $self->{template}) {
1633 $self->{template}->set_font ($self->{font}) if $self->{font}; 2001 $self->{template}->set_font ($self->{font}) if $self->{font};
2002 $self->{template}->set_width ($max_w);
1634 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2003 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1635 2004
1636 my ($w2, $h2) = $self->{template}->size; 2005 my ($w2, $h2) = $self->{template}->size;
1637 2006
1638 $w = List::Util::max $w, $w2; 2007 $w = List::Util::max $w, $w2;
1672 2041
1673sub reconfigure { 2042sub reconfigure {
1674 my ($self) = @_; 2043 my ($self) = @_;
1675 2044
1676 delete $self->{size_req}; 2045 delete $self->{size_req};
2046 delete $self->{texture};
1677 2047
1678 $self->SUPER::reconfigure; 2048 $self->SUPER::reconfigure;
1679} 2049}
1680 2050
1681sub _draw { 2051sub _draw {
1682 my ($self) = @_; 2052 my ($self) = @_;
1683 2053
1684 $self->SUPER::_draw; # draw background, if applicable 2054 $self->SUPER::_draw; # draw background, if applicable
1685 2055
1686 my $tex = $self->{texture} ||= do { 2056 my $size = $self->{texture} ||= do {
1687 $self->{layout}->set_foreground (@{$self->{fg}}); 2057 $self->{layout}->set_foreground (@{$self->{fg}});
1688 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2058 $self->{layout}->set_font ($self->{font}) if $self->{font};
1689 $self->{layout}->set_width ($self->{w}); 2059 $self->{layout}->set_width ($self->{w});
1690 $self->{layout}->set_ellipsise ($self->{ellipsise}); 2060 $self->{layout}->set_ellipsise ($self->{ellipsise});
1691 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 2061 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1692 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2062 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1693 2063
1694 new_from_layout CFClient::Texture $self->{layout} 2064 [$self->{layout}->size]
1695 }; 2065 };
1696 2066
1697 unless (exists $self->{ox}) { 2067 unless (exists $self->{ox}) {
1698 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2068 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
1699 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 2069 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
1700 : ($self->{w} - $tex->{w}) * 0.5);
1701 2070
1702 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2071 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1703 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1704 : ($self->{h} - $tex->{h}) * 0.5);
1705 }; 2072 };
1706 2073
1707 glEnable GL_TEXTURE_2D; 2074# unless ($self->{list}) {
1708 2075# $self->{list} = DC::OpenGL::glGenList;
1709 my $w = List::Util::min $self->{w} + 4, $tex->{w}; 2076# DC::OpenGL::glNewList $self->{list};
1710 my $h = List::Util::min $self->{h} + 2, $tex->{h}; 2077# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1711 2078# DC::OpenGL::glEndList;
1712 if ($tex->{format} == GL_ALPHA) {
1713 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1714 glColor @{$self->{fg}};
1715 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h);
1716 } else {
1717 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1718 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h);
1719 } 2079# }
2080#
2081# DC::OpenGL::glCallList $self->{list};
1720 2082
1721 glDisable GL_TEXTURE_2D; 2083 $self->{layout}->draw;
1722} 2084}
2085
2086#sub destroy {
2087# my ($self) = @_;
2088#
2089# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2090#
2091# $self->SUPER::destroy;
2092#}
1723 2093
1724############################################################################# 2094#############################################################################
1725 2095
1726package CFClient::UI::EntryBase; 2096package DC::UI::EntryBase;
1727 2097
1728our @ISA = CFClient::UI::Label::; 2098our @ISA = DC::UI::Label::;
1729 2099
1730use CFClient::OpenGL; 2100use DC::OpenGL;
1731 2101
1732sub new { 2102sub new {
1733 my $class = shift; 2103 my $class = shift;
1734 2104
1735 $class->SUPER::new ( 2105 $class->SUPER::new (
1736 fg => [1, 1, 1], 2106 fg => [1, 1, 1],
1737 bg => [0, 0, 0, 0.2], 2107 bg => [0, 0, 0, 0.2],
2108 outline => undef,
1738 active_bg => [1, 1, 1, 0.5], 2109 active_bg => [0, 0, 1, .2],
1739 active_fg => [0, 0, 0], 2110 active_fg => [1, 1, 1],
2111 active_outline => [1, 1, 0],
1740 can_hover => 1, 2112 can_hover => 1,
1741 can_focus => 1, 2113 can_focus => 1,
2114 align => 0,
1742 valign => 0, 2115 valign => 0.5,
1743 can_events => 1, 2116 can_events => 1,
2117 ellipsise => 0,
2118 padding_x => 4,
2119 padding_y => 2,
1744 #text => ... 2120 #text => ...
1745 #hidden => "*", 2121 #hidden => "*",
1746 @_ 2122 @_
1747 ) 2123 )
1748} 2124}
1793 my $sym = $ev->{sym}; 2169 my $sym = $ev->{sym};
1794 my $uni = $ev->{unicode}; 2170 my $uni = $ev->{unicode};
1795 2171
1796 my $text = $self->get_text; 2172 my $text = $self->get_text;
1797 2173
1798 if ($uni == 8) { 2174 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2175
2176 if ($sym == DC::SDLK_BACKSPACE) {
1799 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2177 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1800 } elsif ($uni == 127) { 2178 } elsif ($sym == DC::SDLK_DELETE) {
1801 substr $text, $self->{cursor}, 1, ""; 2179 substr $text, $self->{cursor}, 1, "";
1802 } elsif ($sym == CFClient::SDLK_LEFT) { 2180 } elsif ($sym == DC::SDLK_LEFT) {
1803 --$self->{cursor} if $self->{cursor}; 2181 --$self->{cursor} if $self->{cursor};
1804 } elsif ($sym == CFClient::SDLK_RIGHT) { 2182 } elsif ($sym == DC::SDLK_RIGHT) {
1805 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2183 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1806 } elsif ($sym == CFClient::SDLK_HOME) { 2184 } elsif ($sym == DC::SDLK_HOME) {
2185 # what a hack
2186 $self->{cursor} =
2187 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2188 ? length $1
2189 : 0;
2190 } elsif ($sym == DC::SDLK_END) {
2191 # uh, again
2192 $self->{cursor} =
2193 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2194 ? $self->{cursor} + length $1
2195 : length $self->{text};
2196 } elsif ($uni == 21) { # ctrl-u
2197 $text = "";
1807 $self->{cursor} = 0; 2198 $self->{cursor} = 0;
1808 } elsif ($sym == CFClient::SDLK_END) {
1809 $self->{cursor} = length $text;
1810 } elsif ($uni == 27) { 2199 } elsif ($uni == 27) {
1811 $self->emit ('escape'); 2200 $self->emit ('escape');
1812 } elsif ($uni) { 2201 } elsif ($uni == 0x0d) {
2202 substr $text, $self->{cursor}++, 0, "\012";
2203 } elsif ($uni >= 0x20) {
1813 substr $text, $self->{cursor}++, 0, chr $uni; 2204 substr $text, $self->{cursor}++, 0, chr $uni;
1814 } else { 2205 } else {
1815 return 0; 2206 return 0;
1816 } 2207 }
1817 2208
1818 $self->_set_text ($text); 2209 $self->_set_text ($text);
1819 2210
1820 $self->realloc; 2211 $self->realloc;
2212 $self->update;
1821 2213
1822 1 2214 1
1823} 2215}
1824 2216
1825sub invoke_focus_in { 2217sub invoke_focus_in {
1837 2229
1838 my $idx = $self->{layout}->xy_to_index ($x, $y); 2230 my $idx = $self->{layout}->xy_to_index ($x, $y);
1839 2231
1840 # byte-index to char-index 2232 # byte-index to char-index
1841 my $text = $self->{text}; 2233 my $text = $self->{text};
1842 utf8::encode $text; 2234 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1843 $self->{cursor} = length substr $text, 0, $idx; 2235 $self->{cursor} = length $text;
1844 2236
1845 $self->_set_text ($self->{text}); 2237 $self->_set_text ($self->{text});
1846 $self->update; 2238 $self->update;
1847 2239
1848 1 2240 1
1867 glColor_premultiply @{$self->{bg}}; 2259 glColor_premultiply @{$self->{bg}};
1868 } 2260 }
1869 2261
1870 glEnable GL_BLEND; 2262 glEnable GL_BLEND;
1871 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2263 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1872 glBegin GL_QUADS;
1873 glVertex 0 , 0;
1874 glVertex 0 , $self->{h};
1875 glVertex $self->{w}, $self->{h}; 2264 glRect 0, 0, $self->{w}, $self->{h};
1876 glVertex $self->{w}, 0;
1877 glEnd;
1878 glDisable GL_BLEND; 2265 glDisable GL_BLEND;
1879 2266
1880 $self->SUPER::_draw; 2267 $self->SUPER::_draw;
1881 2268
1882 #TODO: force update every cursor change :( 2269 #TODO: force update every cursor change :(
1884 2271
1885 unless (exists $self->{cur_h}) { 2272 unless (exists $self->{cur_h}) {
1886 my $text = substr $self->{text}, 0, $self->{cursor}; 2273 my $text = substr $self->{text}, 0, $self->{cursor};
1887 utf8::encode $text; 2274 utf8::encode $text;
1888 2275
1889 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2276 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1890 } 2277 }
1891 2278
1892 glColor @{$self->{fg}}; 2279 glColor_premultiply @{$self->{active_fg}};
1893 glBegin GL_LINES; 2280 glBegin GL_LINES;
1894 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2281 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
1895 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2282 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1896 glEnd; 2283 glEnd;
1897 }
1898}
1899 2284
2285 glLineWidth 3;
2286 glColor @{$self->{active_outline}};
2287 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2288 glLineWidth 1;
2289
2290 } else {
2291 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2292 glBegin GL_LINE_STRIP;
2293 glVertex .5, $self->{h} * .5;
2294 glVertex .5, $self->{h} - 2.5;
2295 glVertex $self->{w} - .5, $self->{h} - 2.5;
2296 glVertex $self->{w} - .5, $self->{h} * .5;
2297 glEnd;
2298 }
2299}
2300
2301#############################################################################
2302
1900package CFClient::UI::Entry; 2303package DC::UI::Entry;
1901 2304
1902our @ISA = CFClient::UI::EntryBase::; 2305our @ISA = DC::UI::EntryBase::;
1903 2306
1904use CFClient::OpenGL; 2307use DC::OpenGL;
2308
2309sub new {
2310 my $class = shift;
2311
2312 $class->SUPER::new (
2313 history_pointer => -1,
2314 @_
2315 )
2316}
2317
1905 2318
1906sub invoke_key_down { 2319sub invoke_key_down {
1907 my ($self, $ev) = @_; 2320 my ($self, $ev) = @_;
1908 2321
1909 my $sym = $ev->{sym}; 2322 my $sym = $ev->{sym};
1910 2323
1911 if ($sym == 13) { 2324 if ($ev->{uni} == 0x0d || $sym == 13) {
1912 unshift @{$self->{history}}, 2325 unshift @{$self->{history}},
1913 my $txt = $self->get_text; 2326 my $txt = $self->get_text;
1914 2327
1915 $self->{history_pointer} = -1; 2328 $self->{history_pointer} = -1;
1916 $self->{history_saveback} = ''; 2329 $self->{history_saveback} = '';
1917 $self->emit (activate => $txt); 2330 $self->emit (activate => $txt);
1918 $self->update; 2331 $self->update;
1919 2332
1920 } elsif ($sym == CFClient::SDLK_UP) { 2333 } elsif ($sym == DC::SDLK_UP) {
1921 if ($self->{history_pointer} < 0) { 2334 if ($self->{history_pointer} < 0) {
1922 $self->{history_saveback} = $self->get_text; 2335 $self->{history_saveback} = $self->get_text;
1923 } 2336 }
1924 if (@{$self->{history} || []} > 0) { 2337 if (@{$self->{history} || []} > 0) {
1925 $self->{history_pointer}++; 2338 $self->{history_pointer}++;
1927 $self->{history_pointer} = @{$self->{history} || []} - 1; 2340 $self->{history_pointer} = @{$self->{history} || []} - 1;
1928 } 2341 }
1929 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1930 } 2343 }
1931 2344
1932 } elsif ($sym == CFClient::SDLK_DOWN) { 2345 } elsif ($sym == DC::SDLK_DOWN) {
1933 $self->{history_pointer}--; 2346 $self->{history_pointer}--;
1934 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2347 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1935 2348
1936 if ($self->{history_pointer} >= 0) { 2349 if ($self->{history_pointer} >= 0) {
1937 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2350 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1938 } else { 2351 } else {
2352 if (defined $self->{history_saveback}) {
1939 $self->set_text ($self->{history_saveback}); 2353 $self->set_text ($self->{history_saveback});
2354 $self->{history_saveback} = undef;
2355 }
1940 } 2356 }
1941 2357
1942 } else { 2358 } else {
1943 return $self->SUPER::invoke_key_down ($ev) 2359 return $self->SUPER::invoke_key_down ($ev)
1944 } 2360 }
1946 1 2362 1
1947} 2363}
1948 2364
1949############################################################################# 2365#############################################################################
1950 2366
1951package CFClient::UI::Button; 2367package DC::UI::TextEdit;
1952 2368
1953our @ISA = CFClient::UI::Label::; 2369our @ISA = DC::UI::EntryBase::;
1954 2370
1955use CFClient::OpenGL; 2371use DC::OpenGL;
1956
1957my @tex =
1958 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1959 qw(b1_button_active.png);
1960 2372
1961sub new { 2373sub new {
1962 my $class = shift; 2374 my $class = shift;
1963 2375
1964 $class->SUPER::new ( 2376 $class->SUPER::new (
1965 padding_x => 4,
1966 padding_y => 4, 2377 padding_y => 4,
1967 fg => [1, 1, 1], 2378
1968 active_fg => [0, 0, 1], 2379 @_
2380 )
2381}
2382
2383sub move_cursor_ver {
2384 my ($self, $dy) = @_;
2385
2386 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2387
2388 $line += $dy;
2389
2390 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2391 $self->{cursor} = $index;
2392 delete $self->{cur_h};
2393 $self->update;
2394 return;
2395 }
2396}
2397
2398sub invoke_key_down {
2399 my ($self, $ev) = @_;
2400
2401 my $sym = $ev->{sym};
2402
2403 if ($sym == DC::SDLK_UP) {
2404 $self->move_cursor_ver (-1);
2405 } elsif ($sym == DC::SDLK_DOWN) {
2406 $self->move_cursor_ver (+1);
2407 } else {
2408 return $self->SUPER::invoke_key_down ($ev)
2409 }
2410
2411 1
2412}
2413
2414#############################################################################
2415
2416package DC::UI::ButtonBin;
2417
2418our @ISA = DC::UI::Bin::;
2419
2420use DC::OpenGL;
2421
2422my @tex =
2423 map { new_from_resource DC::Texture $_, mipmap => 1 }
2424 qw(b1_button_inactive.png b1_button_active.png);
2425
2426sub new {
2427 my $class = shift;
2428
2429 $class->SUPER::new (
1969 can_hover => 1, 2430 can_hover => 1,
1970 align => 0, 2431 align => 0.5,
1971 valign => 0, 2432 valign => 0.5,
1972 can_events => 1, 2433 can_events => 1,
1973 @_ 2434 @_
1974 ) 2435 )
1975} 2436}
1976 2437
1985} 2446}
1986 2447
1987sub _draw { 2448sub _draw {
1988 my ($self) = @_; 2449 my ($self) = @_;
1989 2450
1990 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
1991
1992 glEnable GL_TEXTURE_2D; 2451 glEnable GL_TEXTURE_2D;
1993 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2452 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1994 glColor 0, 0, 0, 1; 2453 glColor 0, 0, 0, 1;
1995 2454
2455 my $tex = $tex[$GRAB == $self];
1996 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2456 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1997 2457
1998 glDisable GL_TEXTURE_2D; 2458 glDisable GL_TEXTURE_2D;
1999 2459
2000 $self->SUPER::_draw; 2460 $self->SUPER::_draw;
2001} 2461}
2002 2462
2003############################################################################# 2463#############################################################################
2004 2464
2005package CFClient::UI::ImageButton; 2465package DC::UI::Button;
2006 2466
2007our @ISA = CFClient::UI::Image::; 2467our @ISA = DC::UI::Label::;
2008 2468
2009use CFClient::OpenGL; 2469use DC::OpenGL;
2010 2470
2011my %textures; 2471my @tex =
2472 map { new_from_resource DC::Texture $_, mipmap => 1 }
2473 qw(b1_button_inactive.png b1_button_active.png);
2012 2474
2013sub new { 2475sub new {
2014 my $class = shift; 2476 my $class = shift;
2015 2477
2016 my $self = $class->SUPER::new ( 2478 $class->SUPER::new (
2017 padding_x => 4, 2479 padding_x => 8,
2018 padding_y => 4, 2480 padding_y => 4,
2019 fg => [1, 1, 1], 2481 fg => [1.0, 1.0, 1.0],
2020 active_fg => [0, 0, 1], 2482 active_fg => [0.8, 0.8, 0.8],
2021 can_hover => 1, 2483 can_hover => 1,
2022 align => 0, 2484 align => 0.5,
2023 valign => 0, 2485 valign => 0.5,
2024 can_events => 1, 2486 can_events => 1,
2025 @_ 2487 @_
2026 ); 2488 )
2027} 2489}
2028 2490
2029sub invoke_button_up { 2491sub invoke_button_up {
2030 my ($self, $ev, $x, $y) = @_; 2492 my ($self, $ev, $x, $y) = @_;
2031 2493
2034 && $y >= 0 && $y < $self->{h}; 2496 && $y >= 0 && $y < $self->{h};
2035 2497
2036 1 2498 1
2037} 2499}
2038 2500
2501sub _draw {
2502 my ($self) = @_;
2503
2504 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2505
2506 glEnable GL_TEXTURE_2D;
2507 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2508 glColor 0, 0, 0, 1;
2509
2510 my $tex = $tex[$GRAB == $self];
2511 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2512
2513 glDisable GL_TEXTURE_2D;
2514
2515 $self->SUPER::_draw;
2516}
2517
2039############################################################################# 2518#############################################################################
2040 2519
2041package CFClient::UI::CheckBox; 2520package DC::UI::CheckBox;
2042 2521
2043our @ISA = CFClient::UI::DrawBG::; 2522our @ISA = DC::UI::DrawBG::;
2044 2523
2045my @tex = 2524my @tex =
2046 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2525 map { new_from_resource DC::Texture $_, mipmap => 1 }
2047 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2526 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2048 2527
2049use CFClient::OpenGL; 2528use DC::OpenGL;
2050 2529
2051sub new { 2530sub new {
2052 my $class = shift; 2531 my $class = shift;
2053 2532
2054 $class->SUPER::new ( 2533 $class->SUPER::new (
2534 fontsize => 1,
2055 padding_x => 2, 2535 padding_x => 2,
2056 padding_y => 2, 2536 padding_y => 2,
2057 fg => [1, 1, 1], 2537 fg => [1, 1, 1],
2058 active_fg => [1, 1, 0], 2538 active_fg => [1, 1, 0],
2059 bg => [0, 0, 0, 0.2], 2539 bg => [0, 0, 0, 0.2],
2065} 2545}
2066 2546
2067sub size_request { 2547sub size_request {
2068 my ($self) = @_; 2548 my ($self) = @_;
2069 2549
2070 (6) x 2 2550 ($self->{fontsize} * $::FONTSIZE) x 2
2071} 2551}
2072 2552
2073sub toggle { 2553sub toggle {
2074 my ($self) = @_; 2554 my ($self) = @_;
2075 2555
2094sub _draw { 2574sub _draw {
2095 my ($self) = @_; 2575 my ($self) = @_;
2096 2576
2097 $self->SUPER::_draw; 2577 $self->SUPER::_draw;
2098 2578
2099 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2579 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2100 2580
2101 my ($w, $h) = @$self{qw(w h)}; 2581 my ($w, $h) = @$self{qw(w h)};
2102 2582
2103 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2583 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2104 2584
2111 glDisable GL_TEXTURE_2D; 2591 glDisable GL_TEXTURE_2D;
2112} 2592}
2113 2593
2114############################################################################# 2594#############################################################################
2115 2595
2116package CFClient::UI::Image; 2596package DC::UI::Image;
2117 2597
2118our @ISA = CFClient::UI::Base::; 2598our @ISA = DC::UI::DrawBG::;
2119 2599
2120use CFClient::OpenGL; 2600use DC::OpenGL;
2121 2601
2122our %texture_cache; 2602our %texture_cache;
2123 2603
2124sub new { 2604sub new {
2125 my $class = shift; 2605 my $class = shift;
2126 2606
2127 my $self = $class->SUPER::new ( 2607 my $self = $class->SUPER::new (
2128 can_events => 0, 2608 can_events => 0,
2609 scale => 1,
2129 @_, 2610 @_,
2130 ); 2611 );
2131 2612
2613 $self->{path} || $self->{tex}
2614 or Carp::croak "'path' or 'tex' attributes required";
2615
2616 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2617 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2618
2619 DC::weaken $texture_cache{$self->{path}};
2620
2621 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2622
2623 $self
2624}
2625
2626sub STORABLE_freeze {
2627 my ($self, $cloning) = @_;
2628
2132 $self->{path} 2629 $self->{path}
2133 or Carp::croak "required attribute 'path' not set"; 2630 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2134 2631
2135 $self->{tex} = $texture_cache{$self->{path}} ||= 2632 $self->{path}
2136 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; 2633}
2137 2634
2138 Scalar::Util::weaken $texture_cache{$self->{path}}; 2635sub STORABLE_attach {
2636 my ($self, $cloning, $path) = @_;
2139 2637
2140 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2638 $self->new (path => $path)
2639}
2141 2640
2142 $self 2641sub set_texture {
2642 my ($self, $tex) = @_;
2643
2644 $self->{tex} = $tex;
2645 $self->update;
2143} 2646}
2144 2647
2145sub size_request { 2648sub size_request {
2146 my ($self) = @_; 2649 my ($self) = @_;
2147 2650
2148 ($self->{tex}{w}, $self->{tex}{h}) 2651 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2149} 2652}
2150 2653
2151sub _draw { 2654sub _draw {
2152 my ($self) = @_; 2655 my ($self) = @_;
2656
2657 $self->SUPER::_draw;
2153 2658
2154 my $tex = $self->{tex}; 2659 my $tex = $self->{tex};
2155 2660
2156 my ($w, $h) = ($self->{w}, $self->{h}); 2661 my ($w, $h) = ($self->{w}, $self->{h});
2157 2662
2170 glDisable GL_TEXTURE_2D; 2675 glDisable GL_TEXTURE_2D;
2171} 2676}
2172 2677
2173############################################################################# 2678#############################################################################
2174 2679
2680package DC::UI::ImageButton;
2681
2682our @ISA = DC::UI::Image::;
2683
2684use DC::OpenGL;
2685
2686sub new {
2687 my $class = shift;
2688
2689 my $self = $class->SUPER::new (
2690 padding_x => 4,
2691 padding_y => 4,
2692 fg => [1, 1, 1],
2693 active_fg => [0, 0, 1],
2694 can_hover => 1,
2695 align => 0.5,
2696 valign => 0.5,
2697 can_events => 1,
2698 @_
2699 );
2700}
2701
2702sub invoke_button_down {
2703 my ($self, $ev, $x, $y) = @_;
2704
2705 1
2706}
2707
2708sub invoke_button_up {
2709 my ($self, $ev, $x, $y) = @_;
2710
2711 $self->emit ("activate")
2712 if $x >= 0 && $x < $self->{w}
2713 && $y >= 0 && $y < $self->{h};
2714
2715 1
2716}
2717
2718#############################################################################
2719
2175package CFClient::UI::VGauge; 2720package DC::UI::VGauge;
2176 2721
2177our @ISA = CFClient::UI::Base::; 2722our @ISA = DC::UI::Base::;
2178 2723
2179use List::Util qw(min max); 2724use List::Util qw(min max);
2180 2725
2181use CFClient::OpenGL; 2726use DC::OpenGL;
2182 2727
2183my %tex = ( 2728my %tex = (
2184 food => [ 2729 food => [
2185 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2730 map { new_from_resource DC::Texture $_, mipmap => 1 }
2186 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2731 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2187 ], 2732 ],
2188 grace => [ 2733 grace => [
2189 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2190 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2735 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2191 ], 2736 ],
2192 hp => [ 2737 hp => [
2193 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2194 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2739 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2195 ], 2740 ],
2196 mana => [ 2741 mana => [
2197 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2198 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2743 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2199 ], 2744 ],
2200); 2745);
2201 2746
2202# eg. VGauge->new (gauge => 'food'), default gauge: food 2747# eg. VGauge->new (gauge => 'food'), default gauge: food
2305 glDisable GL_TEXTURE_2D; 2850 glDisable GL_TEXTURE_2D;
2306} 2851}
2307 2852
2308############################################################################# 2853#############################################################################
2309 2854
2855package DC::UI::Progress;
2856
2857our @ISA = DC::UI::Label::;
2858
2859use DC::OpenGL;
2860
2861sub new {
2862 my ($class, %arg) = @_;
2863
2864 my $self = $class->SUPER::new (
2865 padding_x => 2,
2866 padding_y => 2,
2867 fg => [1, 1, 1],
2868 bg => [0, 0, 1, 0.2],
2869 bar => [0.7, 0.5, 0.1, 0.8],
2870 outline => [0.4, 0.3, 0],
2871 fontsize => 0.9,
2872 valign => 0.5,
2873 align => 0.5,
2874 can_events => 1,
2875 ellipsise => 1,
2876 label => "%d%%",
2877 %arg,
2878 );
2879
2880 $self->set_value ($arg{value} || -1);
2881
2882 $self
2883}
2884
2885sub set_label {
2886 my ($self, $label) = @_;
2887
2888 return if $self->{label} eq $label;
2889 $self->{label} = $label;
2890
2891 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2892}
2893
2894sub set_value {
2895 my ($self, $value) = @_;
2896
2897 if ($self->{value} ne $value) {
2898 $self->{value} = $value;
2899
2900 if ($value < 0) {
2901 $self->set_text ("-");
2902 } else {
2903 $self->set_text (sprintf $self->{label}, $value * 100);
2904 }
2905
2906 $self->update;
2907 }
2908}
2909
2910sub _draw {
2911 my ($self) = @_;
2912
2913 glEnable GL_BLEND;
2914 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2915
2916 my $px = $self->{padding_x};
2917 my $py = $self->{padding_y};
2918
2919 if ($self->{value} >= 0) {
2920 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2921
2922 glColor_premultiply @{$self->{bar}};
2923 glRect $px, $py, $s, $self->{h} - $py;
2924 glColor_premultiply @{$self->{bg}};
2925 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2926 }
2927
2928 glColor_premultiply @{$self->{outline}};
2929
2930 $px -= .5;
2931 $py -= .5;
2932
2933 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2934
2935 glDisable GL_BLEND;
2936
2937 {
2938 local $self->{bg}; # do not draw background
2939 $self->SUPER::_draw;
2940 }
2941}
2942
2943#############################################################################
2944
2945package DC::UI::ExperienceProgress;
2946
2947our @ISA = DC::UI::Progress::;
2948
2949sub new {
2950 my ($class, %arg) = @_;
2951
2952 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2953
2954 my $self = $class->SUPER::new (
2955 %arg,
2956 tooltip => sub {
2957 my ($self) = @_;
2958
2959 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2960 $tt,
2961 $self->{lvl},
2962 ::formsep ($self->{exp}),
2963 ::formsep ($self->{nxt}),
2964 ::formsep ($self->{nxt} - $self->{exp}),
2965 $self->_percent * 100,
2966 },
2967 );
2968
2969 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2970 if $::CONN;
2971
2972 $self
2973}
2974
2975sub DESTROY {
2976 my ($self) = @_;
2977
2978 delete $::CONN->{on_exp_update}{$self+0}
2979 if $::CONN;
2980
2981 $self->SUPER::DESTROY;
2982}
2983
2984sub _percent {
2985 my ($self) = @_;
2986
2987 my $table = $::CONN && $::CONN->{exp_table}
2988 or return -1;
2989
2990 my $l0 = $table->[$self->{lvl} - 1];
2991 my $l1 = $table->[$self->{lvl}];
2992
2993 $self->{nxt} = $l1;
2994
2995 ($self->{exp} - $l0) / ($l1 - $l0)
2996}
2997
2998sub set_value {
2999 my ($self, $lvl, $exp) = @_;
3000
3001 $self->{lvl} = $lvl;
3002 $self->{exp} = $exp;
3003
3004 $self->SUPER::set_value ($self->_percent);
3005}
3006
3007#############################################################################
3008
2310package CFClient::UI::Gauge; 3009package DC::UI::Gauge;
2311 3010
2312our @ISA = CFClient::UI::VBox::; 3011our @ISA = DC::UI::VBox::;
2313 3012
2314sub new { 3013sub new {
2315 my ($class, %arg) = @_; 3014 my ($class, %arg) = @_;
2316 3015
2317 my $self = $class->SUPER::new ( 3016 my $self = $class->SUPER::new (
2319 can_hover => 1, 3018 can_hover => 1,
2320 can_events => 1, 3019 can_events => 1,
2321 %arg, 3020 %arg,
2322 ); 3021 );
2323 3022
2324 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 3023 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2325 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3024 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2326 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); 3025 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2327 3026
2328 $self 3027 $self
2329} 3028}
2330 3029
2331sub set_fontsize { 3030sub set_fontsize {
2352 $self->{value}->set_text ($val); 3051 $self->{value}->set_text ($val);
2353} 3052}
2354 3053
2355############################################################################# 3054#############################################################################
2356 3055
2357package CFClient::UI::Slider; 3056package DC::UI::Slider;
2358 3057
2359use strict; 3058use strict;
2360 3059
2361use CFClient::OpenGL; 3060use DC::OpenGL;
2362 3061
2363our @ISA = CFClient::UI::DrawBG::; 3062our @ISA = DC::UI::DrawBG::;
2364 3063
2365my @tex = 3064my @tex =
2366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 3065 map { new_from_resource DC::Texture $_ }
2367 qw(s1_slider.png s1_slider_bg.png); 3066 qw(s1_slider.png s1_slider_bg.png);
2368 3067
2369sub new { 3068sub new {
2370 my $class = shift; 3069 my $class = shift;
2371 3070
2408sub set_value { 3107sub set_value {
2409 my ($self, $value) = @_; 3108 my ($self, $value) = @_;
2410 3109
2411 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3110 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2412 3111
2413 $hi = $lo + 1 if $hi <= $lo; 3112 $hi = $lo if $hi < $lo;
2414 3113
2415 $page = $hi - $lo if $page > $hi - $lo; 3114 $value = $hi - $page if $value > $hi - $page;
2416
2417 $value = $lo if $value < $lo; 3115 $value = $lo if $value < $lo;
2418 $value = $hi - $page if $value > $hi - $page;
2419 3116
2420 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3117 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2421 if $unit; 3118 if $unit;
2422 3119
2423 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3120 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2439 3136
2440 $self->SUPER::invoke_button_down ($ev, $x, $y); 3137 $self->SUPER::invoke_button_down ($ev, $x, $y);
2441 3138
2442 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3139 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2443 3140
2444 $self->invoke_mouse_motion ($ev, $x, $y) 3141 $self->invoke_mouse_motion ($ev, $x, $y);
3142
3143 1
2445} 3144}
2446 3145
2447sub invoke_mouse_motion { 3146sub invoke_mouse_motion {
2448 my ($self, $ev, $x, $y) = @_; 3147 my ($self, $ev, $x, $y) = @_;
2449 3148
2460 } 3159 }
2461 3160
2462 1 3161 1
2463} 3162}
2464 3163
3164sub invoke_mouse_wheel {
3165 my ($self, $ev) = @_;
3166
3167 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3168
3169 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3170
3171 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3172
3173 1
3174}
3175
2465sub update { 3176sub update {
2466 my ($self) = @_; 3177 my ($self) = @_;
2467 3178
2468 delete $self->{knob_w}; 3179 delete $self->{knob_w};
2469 $self->SUPER::update; 3180 $self->SUPER::update;
2473 my ($self) = @_; 3184 my ($self) = @_;
2474 3185
2475 unless ($self->{knob_w}) { 3186 unless ($self->{knob_w}) {
2476 $self->set_value ($self->{range}[0]); 3187 $self->set_value ($self->{range}[0]);
2477 3188
2478 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3189 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2479 my $range = ($hi - $page - $lo) || 1e-100; 3190 my $range = ($hi - $page - $lo) || 1e-10;
2480 3191
2481 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3192 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
2482 3193
2483 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3194 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2484 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3195 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2485 3196
2486 $value = ($value - $lo) / $range; 3197 $value = ($value - $lo) / $range;
2516 glDisable GL_TEXTURE_2D; 3227 glDisable GL_TEXTURE_2D;
2517} 3228}
2518 3229
2519############################################################################# 3230#############################################################################
2520 3231
2521package CFClient::UI::ValSlider; 3232package DC::UI::ValSlider;
2522 3233
2523our @ISA = CFClient::UI::HBox::; 3234our @ISA = DC::UI::HBox::;
2524 3235
2525sub new { 3236sub new {
2526 my ($class, %arg) = @_; 3237 my ($class, %arg) = @_;
2527 3238
2528 my $range = delete $arg{range}; 3239 my $range = delete $arg{range};
2529 3240
2530 my $self = $class->SUPER::new ( 3241 my $self = $class->SUPER::new (
2531 slider => (new CFClient::UI::Slider expand => 1, range => $range), 3242 slider => (new DC::UI::Slider expand => 1, range => $range),
2532 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), 3243 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2533 to_value => sub { shift }, 3244 to_value => sub { shift },
2534 from_value => sub { shift }, 3245 from_value => sub { shift },
2535 %arg, 3246 %arg,
2536 ); 3247 );
2537 3248
2557sub set_range { shift->{slider}->set_range (@_) } 3268sub set_range { shift->{slider}->set_range (@_) }
2558sub set_value { shift->{slider}->set_value (@_) } 3269sub set_value { shift->{slider}->set_value (@_) }
2559 3270
2560############################################################################# 3271#############################################################################
2561 3272
2562package CFClient::UI::TextScroller; 3273package DC::UI::TextScroller;
2563 3274
2564our @ISA = CFClient::UI::HBox::; 3275our @ISA = DC::UI::HBox::;
2565 3276
2566use CFClient::OpenGL; 3277use DC::OpenGL;
2567 3278
2568sub new { 3279sub new {
2569 my $class = shift; 3280 my $class = shift;
2570 3281
2571 my $self = $class->SUPER::new ( 3282 my $self = $class->SUPER::new (
2572 fontsize => 1, 3283 fontsize => 1,
2573 can_events => 0, 3284 can_events => 1,
2574 indent => 0, 3285 indent => 0,
2575 #font => default_font 3286 #font => default_font
2576 @_, 3287 @_,
2577 3288
2578 layout => (new CFClient::Layout 1), 3289 layout => (new DC::Layout),
2579 par => [], 3290 par => [],
3291 max_par => 0,
2580 height => 0, 3292 height => 0,
2581 children => [ 3293 children => [
2582 (new CFClient::UI::Empty expand => 1), 3294 (new DC::UI::Empty expand => 1),
2583 (new CFClient::UI::Slider vertical => 1), 3295 (new DC::UI::Slider vertical => 1),
2584 ], 3296 ],
2585 ); 3297 );
2586 3298
2587 $self->{children}[1]->connect (changed => sub { $self->update }); 3299 $self->{children}[1]->connect (changed => sub { $self->update });
2588 3300
2597} 3309}
2598 3310
2599sub size_request { 3311sub size_request {
2600 my ($self) = @_; 3312 my ($self) = @_;
2601 3313
2602 my ($empty, $slider) = @{ $self->{children} }; 3314 my ($empty, $slider) = $self->visible_children;
2603 3315
2604 local $self->{children} = [$empty, $slider]; 3316 local $self->{children} = [$empty, $slider];
2605 $self->SUPER::size_request 3317 $self->SUPER::size_request
2606} 3318}
2607 3319
2618 3330
2619 $self->reflow; 3331 $self->reflow;
2620 3332
2621 local $self->{children} = [$empty, $slider]; 3333 local $self->{children} = [$empty, $slider];
2622 $self->SUPER::invoke_size_allocate ($w, $h) 3334 $self->SUPER::invoke_size_allocate ($w, $h)
3335}
3336
3337sub invoke_mouse_wheel {
3338 my ($self, $ev) = @_;
3339
3340 return 0 unless $ev->{dy}; # only vertical movements
3341
3342 $self->{children}[1]->emit (mouse_wheel => $ev);
3343
3344 1
2623} 3345}
2624 3346
2625sub get_layout { 3347sub get_layout {
2626 my ($self, $para) = @_; 3348 my ($self, $para) = @_;
2627 3349
2634 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3356 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2635 $layout->set_markup ($para->{markup}); 3357 $layout->set_markup ($para->{markup});
2636 3358
2637 $layout->set_shapes ( 3359 $layout->set_shapes (
2638 map 3360 map
2639 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3361 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
2640 @{$para->{widget}} 3362 @{$para->{widget}}
2641 ); 3363 );
2642 3364
2643 $layout 3365 $layout
2644} 3366}
2653sub set_offset { 3375sub set_offset {
2654 my ($self, $offset) = @_; 3376 my ($self, $offset) = @_;
2655 3377
2656 # todo: base offset on lines or so, not on pixels 3378 # todo: base offset on lines or so, not on pixels
2657 $self->{children}[1]->set_value ($offset); 3379 $self->{children}[1]->set_value ($offset);
3380}
3381
3382sub current_paragraph {
3383 my ($self) = @_;
3384
3385 $self->{top_paragraph} - 1
3386}
3387
3388sub scroll_to {
3389 my ($self, $para) = @_;
3390
3391 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
3392
3393 $self->{scroll_to} = $para;
3394 $self->update;
2658} 3395}
2659 3396
2660sub clear { 3397sub clear {
2661 my ($self) = @_; 3398 my ($self) = @_;
2662 3399
2667 $self->{height} = 0; 3404 $self->{height} = 0;
2668 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 3405 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2669} 3406}
2670 3407
2671sub add_paragraph { 3408sub add_paragraph {
2672 my ($self, $color, $para, $indent) = @_; 3409 my $self = shift;
2673 3410
2674 my ($text, @w) = ref $para ? @$para : $para; 3411 for my $para (@_) {
2675
2676 $para = { 3412 $para = {
3413 fg => [1, 1, 1, 1],
3414 indent => 0,
3415 markup => "",
3416 widget => [],
3417 ref $para ? %$para : (markup => $para),
2677 w => 1e10, 3418 w => 1e10,
2678 wrapped => 1, 3419 wrapped => 1,
2679 fg => $color,
2680 indent => $indent,
2681 markup => $text,
2682 widget => \@w,
2683 }; 3420 };
2684 3421
2685 $self->add (@w) if @w; 3422 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2686 push @{$self->{par}}, $para; 3423 push @{$self->{par}}, $para;
3424 }
3425
3426 if (my $max = $self->{max_par}) {
3427 shift @{$self->{par}} while @{$self->{par}} > $max;
3428 }
2687 3429
2688 $self->{need_reflow}++; 3430 $self->{need_reflow}++;
2689 $self->update; 3431 $self->update;
2690} 3432}
2691 3433
2692sub scroll_to_bottom { 3434sub scroll_to_bottom {
2693 my ($self) = @_; 3435 my ($self) = @_;
2694 3436
2695 $self->{scroll_to_bottom} = 1; 3437 $self->{scroll_to} = $#{$self->{par}};
2696 $self->update; 3438 $self->update;
2697} 3439}
2698 3440
3441sub force_uptodate {
3442 my ($self) = @_;
3443
3444 if (delete $self->{need_reflow}) {
3445 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3446
3447 my $height = 0;
3448
3449 for my $para (@{$self->{par}}) {
3450 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3451 my $layout = $self->get_layout ($para);
3452 my ($w, $h) = $layout->size;
3453
3454 $para->{w} = $w + $para->{indent};
3455 $para->{h} = $h;
3456 $para->{wrapped} = $layout->has_wrapped;
3457 }
3458
3459 $para->{y} = $height;
3460 $height += $para->{h};
3461 }
3462
3463 $self->{height} = $height;
3464 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3465
3466 delete $self->{texture};
3467 }
3468
3469 if (my $paridx = delete $self->{scroll_to}) {
3470 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3471 }
3472}
3473
2699sub update { 3474sub update {
2700 my ($self) = @_; 3475 my ($self) = @_;
2701 3476
2702 $self->SUPER::update; 3477 $self->SUPER::update;
2703 3478
2704 return unless $self->{h} > 0; 3479 return unless $self->{h} > 0;
2705 3480
2706 delete $self->{texture}; 3481 delete $self->{texture};
2707 3482
2708 $ROOT->on_post_alloc ($self => sub { 3483 $ROOT->on_post_alloc ($self => sub {
3484 $self->force_uptodate;
3485
2709 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3486 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2710 3487
2711 if (delete $self->{need_reflow}) {
2712 my $height = 0;
2713
2714 for my $para (@{$self->{par}}) {
2715 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2716 my $layout = $self->get_layout ($para);
2717 my ($w, $h) = $layout->size;
2718
2719 $para->{w} = $w + $para->{indent};
2720 $para->{h} = $h;
2721 $para->{wrapped} = $layout->has_wrapped;
2722 }
2723
2724 $height += $para->{h};
2725 }
2726
2727 $self->{height} = $height;
2728
2729 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2730
2731 delete $self->{texture};
2732 }
2733
2734 if (delete $self->{scroll_to_bottom}) {
2735 $self->{children}[1]->set_value (1e10);
2736 }
2737
2738 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 3488 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
2739 glClearColor 0, 0, 0, 0; 3489 glClearColor 0, 0, 0, 0;
2740 glClear GL_COLOR_BUFFER_BIT; 3490 glClear GL_COLOR_BUFFER_BIT;
2741 3491
3492 package DC::UI::Base;
3493 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3494 (0, 0, $self->{w}, $self->{h});
3495
3496 my $top = int $self->{children}[1]{range}[0];
3497
3498 my $paridx = 0;
3499 my $top_paragraph;
2742 my $top = int $self->{children}[1]{range}[0]; 3500 my $top = int $self->{children}[1]{range}[0];
2743 3501
2744 my $y0 = $top; 3502 my $y0 = $top;
2745 my $y1 = $top + $H; 3503 my $y1 = $top + $H;
2746 3504
2747 my $y = 0;
2748
2749 glEnable GL_BLEND;
2750 #TODO# not correct in windows where rgba is forced off
2751 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2752
2753 for my $para (@{$self->{par}}) { 3505 for my $para (@{$self->{par}}) {
2754 my $h = $para->{h}; 3506 my $h = $para->{h};
3507 my $y = $para->{y};
2755 3508
2756 if ($y0 < $y + $h && $y < $y1) { 3509 if ($y0 < $y + $h && $y < $y1) {
2757
2758 my $layout = $self->get_layout ($para); 3510 my $layout = $self->get_layout ($para);
2759 3511
2760 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2761
2762 glRasterPos $para->{indent}, $y - $y0; 3512 $layout->render ($para->{indent}, $y - $y0);
2763 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 3513 $layout->draw;
2764 3514
2765 if (my @w = @{ $para->{widget} }) { 3515 if (my @w = @{ $para->{widget} }) {
2766 my @s = $layout->get_shapes; 3516 my @s = $layout->get_shapes;
2767 3517
2768 glDisable GL_BLEND;
2769 for (@w) { 3518 for (@w) {
2770 my ($dx, $dy) = splice @s, 0, 2, (); 3519 my ($dx, $dy) = splice @s, 0, 2, ();
2771 3520
2772 $_->{x} = $dx + $para->{indent}; 3521 $_->{x} = $dx + $para->{indent};
2773 $_->{y} = $dy + $y - $y0; 3522 $_->{y} = $dy + $y - $y0;
2774 3523
2775 $_->draw; 3524 $_->draw;
2776 } 3525 }
2777 glEnable GL_BLEND;
2778 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2779 } 3526 }
2780 } 3527 }
2781 3528
2782 $y += $h; 3529 $paridx++;
3530 $top_paragraph ||= $paridx if $y >= $top;
2783 } 3531 }
2784 3532
2785 glDisable GL_BLEND; 3533 $self->{top_paragraph} = $top_paragraph;
2786 }; 3534 };
2787 }); 3535 });
2788} 3536}
2789 3537
2790sub reconfigure { 3538sub reconfigure {
2808 $self->{children}[1]->draw; 3556 $self->{children}[1]->draw;
2809} 3557}
2810 3558
2811############################################################################# 3559#############################################################################
2812 3560
2813package CFClient::UI::Animator; 3561package DC::UI::Animator;
2814 3562
2815use CFClient::OpenGL; 3563use DC::OpenGL;
2816 3564
2817our @ISA = CFClient::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
2818 3566
2819sub moveto { 3567sub moveto {
2820 my ($self, $x, $y) = @_; 3568 my ($self, $x, $y) = @_;
2821 3569
2822 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3570 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2850 glPopMatrix; 3598 glPopMatrix;
2851} 3599}
2852 3600
2853############################################################################# 3601#############################################################################
2854 3602
2855package CFClient::UI::Flopper; 3603package DC::UI::Flopper;
2856 3604
2857our @ISA = CFClient::UI::Button::; 3605our @ISA = DC::UI::Button::;
2858 3606
2859sub new { 3607sub new {
2860 my $class = shift; 3608 my $class = shift;
2861 3609
2862 my $self = $class->SUPER::new ( 3610 my $self = $class->SUPER::new (
2874 $self->{other}->toggle_visibility; 3622 $self->{other}->toggle_visibility;
2875} 3623}
2876 3624
2877############################################################################# 3625#############################################################################
2878 3626
2879package CFClient::UI::Tooltip; 3627package DC::UI::Tooltip;
2880 3628
2881our @ISA = CFClient::UI::Bin::; 3629our @ISA = DC::UI::Bin::;
2882 3630
2883use CFClient::OpenGL; 3631use DC::OpenGL;
2884 3632
2885sub new { 3633sub new {
2886 my $class = shift; 3634 my $class = shift;
2887 3635
2888 $class->SUPER::new ( 3636 $class->SUPER::new (
2892} 3640}
2893 3641
2894sub set_tooltip_from { 3642sub set_tooltip_from {
2895 my ($self, $widget) = @_; 3643 my ($self, $widget) = @_;
2896 3644
2897 my $tooltip = $widget->{tooltip}; 3645 my $tip = $widget->{tooltip};
3646 $tip = $tip->($widget) if "CODE" eq ref $tip;
3647
3648 $tip = DC::Pod::section_label tooltip => $1
3649 if $tip =~ /^#(.*)$/;
2898 3650
2899 if ($ENV{CFPLUS_DEBUG} & 2) { 3651 if ($ENV{CFPLUS_DEBUG} & 2) {
2900 $tooltip .= "\n\n" . (ref $widget) . "\n" 3652 $tip .= "\n\n" . (ref $widget) . "\n"
2901 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3653 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2902 . "req $widget->{req_w} $widget->{req_h}\n" 3654 . "req $widget->{req_w} $widget->{req_h}\n"
2903 . "visible $widget->{visible}"; 3655 . "visible $widget->{visible}";
2904 } 3656 }
2905 3657
2906 $tooltip =~ s/^\n+//; 3658 $tip =~ s/^\n+//;
2907 $tooltip =~ s/\n+$//; 3659 $tip =~ s/\n+$//;
2908 3660
2909 $self->add (new CFClient::UI::Label 3661 $self->add (new DC::UI::Label
3662 fg => $DC::THEME{tooltip_fg},
2910 markup => $tooltip, 3663 markup => $tip,
2911 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3664 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3665 align => 0,
2912 fontsize => 0.8, 3666 fontsize => 0.8,
2913 fg => [0, 0, 0, 1], 3667 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
2914 ellipsise => 0, 3668 ellipsise => 0,
2915 font => ($widget->{tooltip_font} || $::FONT_PROP), 3669 font => ($widget->{tooltip_font} || $::FONT_PROP),
2916 ); 3670 );
2917} 3671}
2918 3672
2937 3691
2938 $self->{root}->on_post_alloc ("move_$self" => sub { 3692 $self->{root}->on_post_alloc ("move_$self" => sub {
2939 my $widget = $self->{owner} 3693 my $widget = $self->{owner}
2940 or return; 3694 or return;
2941 3695
3696 if ($widget->{visible}) {
2942 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3697 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2943 3698
2944 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3699 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2945 if $x + $self->{w} > $self->{root}{w}; 3700 if $x + $self->{w} > $self->{root}{w};
2946 3701
2947 $self->move_abs ($x, $y); 3702 $self->move_abs ($x, $y);
3703 } else {
3704 $self->hide;
3705 }
2948 }); 3706 });
2949} 3707}
2950 3708
2951sub _draw { 3709sub _draw {
2952 my ($self) = @_; 3710 my ($self) = @_;
2953 3711
2954 glTranslate 0.375, 0.375;
2955
2956 my ($w, $h) = @$self{qw(w h)}; 3712 my ($w, $h) = @$self{qw(w h)};
2957 3713
2958 glColor 1, 0.8, 0.4; 3714 glColor @{ $DC::THEME{tooltip_bg} };
2959 glBegin GL_QUADS; 3715 glRect 0, 0, $w, $h;
2960 glVertex 0 , 0;
2961 glVertex 0 , $h;
2962 glVertex $w, $h;
2963 glVertex $w, 0;
2964 glEnd;
2965 3716
2966 glColor 0, 0, 0; 3717 glColor @{ $DC::THEME{tooltip_border} };
2967 glBegin GL_LINE_LOOP; 3718 glRect_lineloop .5, .5, $w + .5, $h + .5;
2968 glVertex 0 , 0;
2969 glVertex 0 , $h;
2970 glVertex $w, $h;
2971 glVertex $w, 0;
2972 glEnd;
2973 3719
2974 glTranslate 2 - 0.375, 2 - 0.375; 3720 glTranslate 2, 2;
2975 3721
2976 $self->SUPER::_draw; 3722 $self->SUPER::_draw;
2977} 3723}
2978 3724
2979############################################################################# 3725#############################################################################
2980 3726
2981package CFClient::UI::Face; 3727package DC::UI::Face;
2982 3728
2983our @ISA = CFClient::UI::Base::; 3729our @ISA = DC::UI::DrawBG::;
2984 3730
2985use CFClient::OpenGL; 3731use DC::OpenGL;
2986 3732
2987sub new { 3733sub new {
2988 my $class = shift; 3734 my $class = shift;
2989 3735
2990 my $self = $class->SUPER::new ( 3736 my $self = $class->SUPER::new (
3737 size_w => 32,
3738 size_h => 8,
2991 aspect => 1, 3739 aspect => 1,
2992 can_events => 0, 3740 can_events => 0,
2993 @_, 3741 @_,
2994 ); 3742 );
2995 3743
2996 if ($self->{anim} && $self->{animspeed}) { 3744 $self->update_anim;
2997 Scalar::Util::weaken (my $widget = $self);
2998
2999 $self->{timer} = Event->timer (
3000 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3001 hard => 1,
3002 interval => $self->{animspeed},
3003 cb => sub {
3004 ++$widget->{frame};
3005 $widget->update;
3006 },
3007 );
3008 }
3009 3745
3010 $self 3746 $self
3011} 3747}
3012 3748
3749sub update_timer {
3750 my ($self) = @_;
3751
3752 return unless $self->{timer};
3753
3754 if ($self->{visible}) {
3755 $self->{timer}->start;
3756 } else {
3757 $self->{timer}->stop;
3758 }
3759}
3760
3761sub update_face {
3762 my ($self) = @_;
3763
3764 if ($::CONN) {
3765 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3766 if ($anim && @$anim) {
3767 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3768 delete $self->{face_change_cb};
3769
3770 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3771 unless ($tex->{name} || $tex->{loading}) {
3772 $tex->upload (sub { $self->reconfigure });
3773 }
3774 }
3775 }
3776 }
3777 }
3778}
3779
3780sub update_anim {
3781 my ($self) = @_;
3782
3783 if ($self->{anim} && $self->{animspeed}) {
3784 DC::weaken (my $widget = $self);
3785
3786 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3787 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3788 return unless $::CONN;
3789
3790 my $w = $widget
3791 or return;
3792
3793 ++$w->{frame};
3794 $w->update_face;
3795
3796 # somehow, $widget can go away
3797 $w->update;
3798 $w->update_timer;
3799 };
3800
3801 $self->update_face;
3802 $self->update_timer;
3803 } else {
3804 delete $self->{timer};
3805 }
3806}
3807
3013sub size_request { 3808sub size_request {
3014 (32, 8) 3809 my ($self) = @_;
3810
3811 if ($::CONN) {
3812 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3813 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3814 if ($tex->{name}) {
3815 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3816 } elsif (!$tex->{loading}) {
3817 $tex->upload (sub { $self->reconfigure });
3818 }
3819 }
3820
3821 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3822 }
3823 }
3824
3825 ($self->{size_w} || 8, $self->{size_h} || 8)
3015} 3826}
3016 3827
3017sub update { 3828sub update {
3018 my ($self) = @_; 3829 my ($self) = @_;
3019 3830
3020 return unless $self->{visible}; 3831 return unless $self->{visible};
3021 3832
3022 $self->SUPER::update; 3833 $self->SUPER::update;
3023} 3834}
3024 3835
3836sub set_face {
3837 my ($self, $face) = @_;
3838
3839 $self->{face} = $face;
3840 $self->reconfigure;
3841}
3842
3843sub set_anim {
3844 my ($self, $anim) = @_;
3845
3846 $self->{anim} = $anim;
3847 $self->update_anim;
3848}
3849
3850sub set_animspeed {
3851 my ($self, $animspeed) = @_;
3852
3853 $self->{animspeed} = $animspeed;
3854 $self->update_anim;
3855}
3856
3857sub invoke_visibility_change {
3858 my ($self) = @_;
3859
3860 $self->update_timer;
3861
3862 0
3863}
3864
3025sub _draw { 3865sub _draw {
3026 my ($self) = @_; 3866 my ($self) = @_;
3027 3867
3028 return unless $::CONN; 3868 $self->SUPER::_draw;
3029 3869
3030 my $face; 3870 if (my $tex = $self->{tex}) {
3031
3032 if ($self->{frame}) {
3033 my $anim = $::CONN->{anim}[$self->{anim}];
3034
3035 $face = $anim->[ $self->{frame} % @$anim ]
3036 if $anim && @$anim;
3037 }
3038
3039 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3040
3041 if ($tex) {
3042 glEnable GL_TEXTURE_2D; 3871 glEnable GL_TEXTURE_2D;
3043 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3044 glColor 0, 0, 0, 1; 3873 glColor 0, 0, 0, 1;
3045 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3874 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3046 glDisable GL_TEXTURE_2D; 3875 glDisable GL_TEXTURE_2D;
3048} 3877}
3049 3878
3050sub destroy { 3879sub destroy {
3051 my ($self) = @_; 3880 my ($self) = @_;
3052 3881
3053 $self->{timer}->cancel 3882 (delete $self->{timer})->cancel
3054 if $self->{timer}; 3883 if $self->{timer};
3055 3884
3056 $self->SUPER::destroy; 3885 $self->SUPER::destroy;
3057} 3886}
3058 3887
3059############################################################################# 3888#############################################################################
3060 3889
3061package CFClient::UI::Buttonbar; 3890package DC::UI::Buttonbar;
3062 3891
3063our @ISA = CFClient::UI::HBox::; 3892our @ISA = DC::UI::HBox::;
3064 3893
3065# TODO: should actualyl wrap buttons and other goodies. 3894# TODO: should actually wrap buttons and other goodies.
3066 3895
3067############################################################################# 3896#############################################################################
3068 3897
3069package CFClient::UI::Menu; 3898package DC::UI::Menu;
3070 3899
3071our @ISA = CFClient::UI::FancyFrame::; 3900our @ISA = DC::UI::Toplevel::;
3072 3901
3073use CFClient::OpenGL; 3902use DC::OpenGL;
3074 3903
3075sub new { 3904sub new {
3076 my $class = shift; 3905 my $class = shift;
3077 3906
3078 my $self = $class->SUPER::new ( 3907 my $self = $class->SUPER::new (
3079 items => [], 3908 items => [],
3080 z => 100, 3909 z => 100,
3081 @_, 3910 @_,
3082 ); 3911 );
3083 3912
3084 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3913 $self->add ($self->{vbox} = new DC::UI::VBox);
3085 3914
3086 for my $item (@{ $self->{items} }) { 3915 for my $item (@{ $self->{items} }) {
3087 my ($widget, $cb, $tooltip) = @$item; 3916 my ($widget, $cb, $tooltip) = @$item;
3088 3917
3089 # handle various types of items, only text for now 3918 # handle various types of items, only text for now
3090 if (!ref $widget) { 3919 if (!ref $widget) {
3920 if ($widget =~ /\t/) {
3921 my ($left, $right) = split /\t/, $widget, 2;
3922
3091 $widget = new CFClient::UI::Label 3923 $widget = new DC::UI::HBox
3092 can_hover => 1, 3924 can_hover => 1,
3093 can_events => 1, 3925 can_events => 1,
3926 tooltip => $tooltip,
3927 children => [
3928 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3929 (new DC::UI::Label markup => $right, align => 1),
3930 ],
3931 ;
3932
3933 } else {
3934 $widget = new DC::UI::Label
3935 can_hover => 1,
3936 can_events => 1,
3937 align => 0,
3094 markup => $widget, 3938 markup => $widget,
3095 tooltip => $tooltip 3939 tooltip => $tooltip;
3940 }
3096 } 3941 }
3097 3942
3098 $self->{item}{$widget} = $item; 3943 $self->{item}{$widget} = $item;
3099 3944
3100 $self->{vbox}->add ($widget); 3945 $self->{vbox}->add ($widget);
3112 # maybe save $GRAB? must be careful about events... 3957 # maybe save $GRAB? must be careful about events...
3113 $GRAB = $self; 3958 $GRAB = $self;
3114 $self->{button} = $ev->{button}; 3959 $self->{button} = $ev->{button};
3115 3960
3116 $self->show; 3961 $self->show;
3117 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3962
3963 my $x = $ev->{x};
3964 my $y = $ev->{y};
3965
3966 $self->{root}->on_post_alloc ($self => sub {
3967 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3968 });
3969
3970 1 # so it can be used inside event handlers
3118} 3971}
3119 3972
3120sub invoke_mouse_motion { 3973sub invoke_mouse_motion {
3121 my ($self, $ev, $x, $y) = @_; 3974 my ($self, $ev, $x, $y) = @_;
3122 3975
3143 1 3996 1
3144} 3997}
3145 3998
3146############################################################################# 3999#############################################################################
3147 4000
3148package CFClient::UI::Multiplexer; 4001package DC::UI::Multiplexer;
3149 4002
3150our @ISA = CFClient::UI::Container::; 4003our @ISA = DC::UI::Container::;
3151 4004
3152sub new { 4005sub new {
3153 my $class = shift; 4006 my $class = shift;
3154 4007
3155 my $self = $class->SUPER::new ( 4008 my $self = $class->SUPER::new (
3156 @_, 4009 @_,
3157 ); 4010 );
3158 4011
3159 $self->{current} = $self->{children}[0] 4012 $self->set_current_page (0);
3160 if @{ $self->{children} };
3161 4013
3162 $self 4014 $self
3163} 4015}
3164 4016
3165sub add { 4017sub add {
3166 my ($self, @widgets) = @_; 4018 my ($self, @widgets) = @_;
3167 4019
3168 $self->SUPER::add (@widgets); 4020 $self->SUPER::add (@widgets);
3169 4021
3170 $self->{current} = $self->{children}[0] 4022 $self->set_current_page (0)
3171 if @{ $self->{children} }; 4023 if @widgets == @{ $self->{children} };
3172} 4024}
3173 4025
3174sub get_current_page { 4026sub get_current_page {
3175 my ($self) = @_; 4027 my ($self) = @_;
3176 4028
3182 4034
3183 my $widget = ref $page_or_widget 4035 my $widget = ref $page_or_widget
3184 ? $page_or_widget 4036 ? $page_or_widget
3185 : $self->{children}[$page_or_widget]; 4037 : $self->{children}[$page_or_widget];
3186 4038
4039 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4040
3187 $self->{current} = $widget; 4041 if (($self->{current} = $widget)) {
4042 $self->{current}->set_visible if $self->{current} && $self->{visible};
3188 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4043 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3189 4044
3190 $self->emit (page_changed => $self->{current}); 4045 $self->emit (page_changed => $self->{current});
4046 }
3191 4047
3192 $self->realloc; 4048 $self->realloc;
3193} 4049}
3194 4050
3195sub visible_children { 4051sub visible_children {
3196 $_[0]{current} 4052 $_[0]{current} || ()
3197} 4053}
3198 4054
3199sub size_request { 4055sub size_request {
3200 my ($self) = @_; 4056 my ($self) = @_;
3201 4057
4058 $self->{current}
3202 $self->{current}->size_request 4059 ? $self->{current}->size_request
4060 : (0, 0)
3203} 4061}
3204 4062
3205sub invoke_size_allocate { 4063sub invoke_size_allocate {
3206 my ($self, $w, $h) = @_; 4064 my ($self, $w, $h) = @_;
3207 4065
3208 $self->{current}->configure (0, 0, $w, $h); 4066 $self->{current}->configure (0, 0, $w, $h)
4067 if $self->{current};
3209 4068
3210 1 4069 1
3211} 4070}
3212 4071
3213sub _draw { 4072sub _draw {
3214 my ($self) = @_; 4073 my ($self) = @_;
3215 4074
3216 $self->{current}->draw; 4075 $self->{current}->draw
4076 if $self->{current};
3217} 4077}
3218 4078
3219############################################################################# 4079#############################################################################
3220 4080
3221package CFClient::UI::Notebook; 4081package DC::UI::Notebook;
3222 4082
4083use DC::OpenGL;
4084
3223our @ISA = CFClient::UI::VBox::; 4085our @ISA = DC::UI::VBox::;
3224 4086
3225sub new { 4087sub new {
3226 my $class = shift; 4088 my $class = shift;
3227 4089
3228 my $self = $class->SUPER::new ( 4090 my $self = $class->SUPER::new (
3229 buttonbar => (new CFClient::UI::Buttonbar), 4091 buttonbar => (new DC::UI::Buttonbar),
3230 multiplexer => (new CFClient::UI::Multiplexer expand => 1), 4092 multiplexer => (new DC::UI::Multiplexer expand => 1),
4093 active_outline => [.7, .7, 0.2],
3231 # filter => # will be put between multiplexer and $self 4094 # filter => # will be put between multiplexer and $self
3232 @_, 4095 @_,
3233 ); 4096 );
3234 4097
3235 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4098 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3236 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4099 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3237 4100
4101 {
4102 Scalar::Util::weaken (my $wself = $self);
4103
4104 $self->{multiplexer}->connect (c_add => sub {
4105 my ($mplex, $widgets) = @_;
4106
4107 for my $child (@$widgets) {
4108 Scalar::Util::weaken $child;
4109 $child->{c_tab_} ||= do {
4110 my $tab =
4111 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4112 ? $child->{c_tab}
4113 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4114
4115 $tab->connect (activate => sub {
4116 $wself->set_current_page ($child);
4117 });
4118
4119 $tab
4120 };
4121
4122 $self->{buttonbar}->add ($child->{c_tab_});
4123 }
4124 });
4125
4126 $self->{multiplexer}->connect (c_remove => sub {
4127 my ($mplex, $widgets) = @_;
4128
4129 for my $child (@$widgets) {
4130 $wself->{buttonbar}->remove ($child->{c_tab_});
4131 }
4132 });
4133 }
4134
3238 $self 4135 $self
3239} 4136}
3240 4137
3241sub add { 4138sub add {
4139 my ($self, @widgets) = @_;
4140
4141 $self->{multiplexer}->add (@widgets)
4142}
4143
4144sub remove {
4145 my ($self, @widgets) = @_;
4146
4147 $self->{multiplexer}->remove (@widgets)
4148}
4149
4150sub pages {
4151 my ($self) = @_;
4152 $self->{multiplexer}->children
4153}
4154
4155sub page_index {
4156 my ($self, $widget) = @_;
4157
4158 my $i = 0;
4159 for ($self->pages) {
4160 if ($_ eq $widget) { return $i };
4161 $i++;
4162 }
4163
4164 undef
4165}
4166
4167sub add_tab {
3242 my ($self, $title, $widget, $tooltip) = @_; 4168 my ($self, $title, $widget, $tooltip) = @_;
3243 4169
3244 Scalar::Util::weaken $self; 4170 $title = [$title, $tooltip] unless ref $title;
4171 $widget->{c_tab} = $title;
3245 4172
3246 $self->{buttonbar}->add (new CFClient::UI::Button
3247 markup => $title,
3248 tooltip => $tooltip,
3249 on_activate => sub { $self->set_current_page ($widget) },
3250 );
3251
3252 $self->{multiplexer}->add ($widget); 4173 $self->add ($widget);
3253} 4174}
3254 4175
3255sub get_current_page { 4176sub get_current_page {
3256 my ($self) = @_; 4177 my ($self) = @_;
3257 4178
3263 4184
3264 $self->{multiplexer}->set_current_page ($page); 4185 $self->{multiplexer}->set_current_page ($page);
3265 $self->emit (page_changed => $self->{multiplexer}{current}); 4186 $self->emit (page_changed => $self->{multiplexer}{current});
3266} 4187}
3267 4188
4189sub _draw {
4190 my ($self) = @_;
4191
4192 $self->SUPER::_draw ();
4193
4194 if (my $cur = $self->{multiplexer}{current}) {
4195 if ($cur = $cur->{c_tab_}) {
4196 glTranslate $self->{buttonbar}{x} + $cur->{x},
4197 $self->{buttonbar}{y} + $cur->{y};
4198 glLineWidth 3;
4199 #glEnable GL_BLEND;
4200 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4201 glColor @{$self->{active_outline}};
4202 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4203 glLineWidth 1;
4204 #glDisable GL_BLEND;
4205 }
4206 }
4207}
4208
3268############################################################################# 4209#############################################################################
3269 4210
3270package CFClient::UI::Combobox; 4211package DC::UI::Selector;
3271 4212
3272use utf8; 4213use utf8;
3273 4214
3274our @ISA = CFClient::UI::Button::; 4215our @ISA = DC::UI::Button::;
3275 4216
3276sub new { 4217sub new {
3277 my $class = shift; 4218 my $class = shift;
3278 4219
3279 my $self = $class->SUPER::new ( 4220 my $self = $class->SUPER::new (
3296 my ($value, $title, $tooltip) = @$_; 4237 my ($value, $title, $tooltip) = @$_;
3297 4238
3298 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4239 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3299 } 4240 }
3300 4241
3301 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 4242 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3302} 4243}
3303 4244
3304sub _set_value { 4245sub _set_value {
3305 my ($self, $value) = @_; 4246 my ($self, $value) = @_;
3306 4247
3307 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4248 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4249 $item ||= $self->{options}[0]
3308 or return; 4250 or return;
3309 4251
3310 $self->{value} = $item->[0]; 4252 $self->{value} = $item->[0];
3311 $self->set_markup ("$item->[1] ⇓"); 4253 $self->set_markup ("$item->[1] ⇓");
3312 $self->set_tooltip ($item->[2]); 4254# $self->set_tooltip ($item->[2]);
3313} 4255}
3314 4256
3315sub set_value { 4257sub set_value {
3316 my ($self, $value) = @_; 4258 my ($self, $value) = @_;
3317 4259
3319 4261
3320 $self->_set_value ($value); 4262 $self->_set_value ($value);
3321 $self->emit (changed => $value); 4263 $self->emit (changed => $value);
3322} 4264}
3323 4265
4266sub set_options {
4267 my ($self, $options) = @_;
4268
4269 $self->{options} = $options;
4270 $self->_set_value ($self->{value});
4271}
4272
3324############################################################################# 4273#############################################################################
3325 4274
3326package CFClient::UI::Statusbox; 4275package DC::UI::Statusbox;
3327 4276
3328our @ISA = CFClient::UI::VBox::; 4277our @ISA = DC::UI::VBox::;
3329 4278
3330sub new { 4279sub new {
3331 my $class = shift; 4280 my $class = shift;
3332 4281
3333 my $self = $class->SUPER::new ( 4282 my $self = $class->SUPER::new (
3334 fontsize => 0.8, 4283 fontsize => 0.8,
3335 @_, 4284 @_,
3336 ); 4285 );
3337 4286
3338 Scalar::Util::weaken (my $this = $self); 4287 DC::weaken (my $this = $self);
3339 4288
3340 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4289 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3341 4290
3342 $self 4291 $self
3343} 4292}
3344 4293
3345sub reorder { 4294sub reorder {
3346 my ($self) = @_; 4295 my ($self) = @_;
3347 my $NOW = Time::HiRes::time; 4296 my $NOW = EV::time;
3348 4297
3349 # freeze display when hovering over any label 4298 # freeze display when hovering over any label
3350 return if $CFClient::UI::TOOLTIP->{owner} 4299 return if $DC::UI::TOOLTIP->{owner}
3351 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label}, 4300 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3352 values %{ $self->{item} }; 4301 values %{ $self->{item} };
3353 4302
3354 while (my ($k, $v) = each %{ $self->{item} }) { 4303 while (my ($k, $v) = each %{ $self->{item} }) {
3355 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4304 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3356 } 4305 }
4306
4307 $self->{timer}->set (1, 1);
3357 4308
3358 my @widgets; 4309 my @widgets;
3359 4310
3360 my @items = sort { 4311 my @items = sort {
3361 $a->{pri} <=> $b->{pri} 4312 $a->{pri} <=> $b->{pri}
3362 or $b->{id} <=> $a->{id} 4313 or $b->{id} <=> $a->{id}
3363 } values %{ $self->{item} }; 4314 } values %{ $self->{item} };
3364
3365 $self->{timer}->interval (1);
3366 4315
3367 my $count = 10 + 1; 4316 my $count = 10 + 1;
3368 for my $item (@items) { 4317 for my $item (@items) {
3369 last unless --$count; 4318 last unless --$count;
3370 4319
3377 for ($short) { 4326 for ($short) {
3378 s/^\s+//; 4327 s/^\s+//;
3379 s/\s+/ /g; 4328 s/\s+/ /g;
3380 } 4329 }
3381 4330
3382 new CFClient::UI::Label 4331 new DC::UI::Label
3383 markup => $short, 4332 markup => $short,
3384 tooltip => $item->{tooltip}, 4333 tooltip => $item->{tooltip},
3385 tooltip_font => $::FONT_PROP, 4334 tooltip_font => $::FONT_PROP,
3386 tooltip_width => 0.67, 4335 tooltip_width => 0.67,
3387 fontsize => $item->{fontsize} || $self->{fontsize}, 4336 fontsize => $item->{fontsize} || $self->{fontsize},
3388 max_w => $::WIDTH * 0.44, 4337 max_w => $::WIDTH * 0.44,
4338 align => 0,
3389 fg => [@{ $item->{fg} }], 4339 fg => [@{ $item->{fg} }],
3390 can_events => 1, 4340 can_events => 1,
3391 can_hover => 1 4341 can_hover => 1
3392 }; 4342 };
3393 4343
3394 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4344 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3395 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4345 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3396 $label->update; 4346 $label->update;
3397 $label->set_max_size (undef, $label->{req_h} * $diff) 4347 $label->set_max_size (undef, $label->{req_h} * $diff)
3398 if $diff < 1; 4348 if $diff < 1;
3399 $self->{timer}->interval (1/30); 4349 $self->{timer}->set (1/30, 1/30);
3400 } else { 4350 } else {
3401 $label->{fg}[3] = $item->{fg}[3] || 1; 4351 $label->{fg}[3] = $item->{fg}[3] || 1;
3402 } 4352 }
3403 4353
3404 push @widgets, $label; 4354 push @widgets, $label;
3405 } 4355 }
4356
4357 my $hash = join ",", @widgets;
4358 return if $hash eq $self->{last_widget_hash};
4359 $self->{last_widget_hash} = $hash;
3406 4360
3407 $self->clear; 4361 $self->clear;
3408 $self->SUPER::add (reverse @widgets); 4362 $self->SUPER::add (reverse @widgets);
3409} 4363}
3410 4364
3466 $self->SUPER::destroy; 4420 $self->SUPER::destroy;
3467} 4421}
3468 4422
3469############################################################################# 4423#############################################################################
3470 4424
3471package CFClient::UI::Inventory;
3472
3473our @ISA = CFClient::UI::Table::;
3474
3475sub new {
3476 my $class = shift;
3477
3478 my $self = $class->SUPER::new (
3479 col_expand => [0, 1, 0],
3480 @_,
3481 );
3482
3483 $self
3484}
3485
3486sub set_items {
3487 my ($self, $items) = @_;
3488
3489 $self->clear;
3490 return unless $items;
3491
3492 my @items = sort {
3493 ($a->{type} <=> $b->{type})
3494 or ($a->{name} cmp $b->{name})
3495 } values %$items;
3496
3497 $self->{real_items} = \@items;
3498
3499 my $row = 0;
3500 for my $item (@items) {
3501 CFClient::Item::update_widgets $item;
3502
3503 $self->add (0, $row, $item->{face_widget});
3504 $self->add (1, $row, $item->{desc_widget});
3505 $self->add (2, $row, $item->{weight_widget});
3506
3507 $row++;
3508 }
3509}
3510
3511#############################################################################
3512
3513package CFClient::UI::SpellList;
3514
3515our @ISA = CFClient::UI::Table::;
3516
3517sub new {
3518 my $class = shift;
3519
3520 my $self = $class->SUPER::new (
3521 binding => [],
3522 commands => [],
3523 @_,
3524 )
3525}
3526
3527my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3528
3529my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3530 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3531my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3532 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3533my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3534 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3535my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3536 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3537my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3538 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3539
3540sub rebuild_spell_list {
3541 my ($self) = @_;
3542
3543 $CFClient::UI::ROOT->on_refresh ($self => sub {
3544 $self->clear;
3545
3546 return unless $::CONN;
3547
3548 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3549 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3550 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3551 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3552 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3553
3554 my $row = 0;
3555
3556 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3557 my $spell = $self->{spell}{$_};
3558
3559 $row++;
3560
3561 my $spell_cb = sub {
3562 my ($widget, $ev) = @_;
3563
3564 if ($ev->{button} == 1) {
3565 $::CONN->user_send ("cast $spell->{name}");
3566 } elsif ($ev->{button} == 2) {
3567 $::CONN->user_send ("invoke $spell->{name}");
3568 } elsif ($ev->{button} == 3) {
3569 (new CFClient::UI::Menu
3570 items => [
3571 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3572 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3573 ],
3574 )->popup ($ev);
3575 } else {
3576 return 0;
3577 }
3578
3579 1
3580 };
3581
3582 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3583
3584 #TODO: add path info to tooltip
3585 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3586
3587 $self->add (0, $row, new CFClient::UI::Face
3588 face => $spell->{face},
3589 can_hover => 1,
3590 can_events => 1,
3591 tooltip => $tooltip,
3592 on_button_down => $spell_cb,
3593 );
3594
3595 $self->add (1, $row, new CFClient::UI::Label
3596 expand => 1,
3597 text => $spell->{name},
3598 can_hover => 1,
3599 can_events => 1,
3600 tooltip => $tooltip,
3601 on_button_down => $spell_cb,
3602 );
3603
3604 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3605 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3606 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3607 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3608 }
3609 });
3610}
3611
3612sub add_spell {
3613 my ($self, $spell) = @_;
3614
3615 $self->{spell}->{$spell->{name}} = $spell;
3616 $self->rebuild_spell_list;
3617}
3618
3619sub remove_spell {
3620 my ($self, $spell) = @_;
3621
3622 delete $self->{spell}->{$spell->{name}};
3623 $self->rebuild_spell_list;
3624}
3625
3626sub clear_spells {
3627 my ($self) = @_;
3628
3629 $self->{spell} = {};
3630 $self->rebuild_spell_list;
3631}
3632
3633#############################################################################
3634
3635package CFClient::UI::Root; 4425package DC::UI::Root;
3636 4426
3637our @ISA = CFClient::UI::Container::; 4427our @ISA = DC::UI::Container::;
3638 4428
3639use List::Util qw(min max); 4429use List::Util qw(min max);
3640 4430
3641use CFClient::OpenGL; 4431use DC::OpenGL;
3642 4432
3643sub new { 4433sub new {
3644 my $class = shift; 4434 my $class = shift;
3645 4435
3646 my $self = $class->SUPER::new ( 4436 my $self = $class->SUPER::new (
3647 visible => 1, 4437 visible => 1,
3648 @_, 4438 @_,
3649 ); 4439 );
3650 4440
3651 Scalar::Util::weaken ($self->{root} = $self); 4441 DC::weaken ($self->{root} = $self);
3652 4442
3653 $self 4443 $self
3654} 4444}
3655 4445
3656sub size_request { 4446sub size_request {
3704} 4494}
3705 4495
3706sub update { 4496sub update {
3707 my ($self) = @_; 4497 my ($self) = @_;
3708 4498
3709 $::WANT_REFRESH++; 4499 $::WANT_REFRESH = 1;
3710} 4500}
3711 4501
3712sub add { 4502sub add {
3713 my ($self, @children) = @_; 4503 my ($self, @children) = @_;
3714 4504
3751 while ($self->{refresh_hook}) { 4541 while ($self->{refresh_hook}) {
3752 $_->() 4542 $_->()
3753 for values %{delete $self->{refresh_hook}}; 4543 for values %{delete $self->{refresh_hook}};
3754 } 4544 }
3755 4545
3756 if ($self->{realloc}) { 4546 while ($self->{realloc}) {
3757 my %queue; 4547 my %queue;
3758 my @queue; 4548 my @queue;
3759 my $widget; 4549 my $widget;
3760 4550
3761 outer: 4551 outer:
3781 4571
3782 delete $queue{$widget+0}; 4572 delete $queue{$widget+0};
3783 4573
3784 my ($w, $h) = $widget->size_request; 4574 my ($w, $h) = $widget->size_request;
3785 4575
3786 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4576 $w += $widget->{padding_x} * 2;
3787 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4577 $h += $widget->{padding_y} * 2;
4578
4579 $w = max $widget->{min_w}, $w;
4580 $h = max $widget->{min_h}, $h;
3788 4581
3789 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4582 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3790 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4583 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3791 4584
3792 $w = $widget->{force_w} if exists $widget->{force_w}; 4585 $w = $widget->{force_w} if exists $widget->{force_w};
3808 } 4601 }
3809 } 4602 }
3810 4603
3811 delete $self->{realloc}{$widget+0}; 4604 delete $self->{realloc}{$widget+0};
3812 } 4605 }
3813 }
3814 4606
3815 while (my $size_alloc = delete $self->{size_alloc}) { 4607 while (my $size_alloc = delete $self->{size_alloc}) {
3816 my @queue = sort { $b->{visible} <=> $a->{visible} } 4608 my @queue = sort { $a->{visible} <=> $b->{visible} }
3817 values %$size_alloc; 4609 values %$size_alloc;
3818 4610
3819 while () { 4611 while () {
3820 my $widget = pop @queue || last; 4612 my $widget = pop @queue || last;
3821 4613
3822 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4614 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3823 4615
3824 $w = 0 if $w < 0; 4616 $w = max $widget->{min_w}, $w;
3825 $h = 0 if $h < 0; 4617 $h = max $widget->{min_h}, $h;
3826 4618
4619# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4620# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4621
4622 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4623 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4624
3827 $w = int $w + 0.5; 4625 $w = int $w + 0.5;
3828 $h = int $h + 0.5; 4626 $h = int $h + 0.5;
3829 4627
3830 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4628 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3831 $widget->{old_w} = $widget->{w}; 4629 $widget->{old_w} = $widget->{w};
3832 $widget->{old_h} = $widget->{h}; 4630 $widget->{old_h} = $widget->{h};
3833 4631
3834 $widget->{w} = $w; 4632 $widget->{w} = $w;
3835 $widget->{h} = $h; 4633 $widget->{h} = $h;
3836 4634
3837 $widget->emit (size_allocate => $w, $h); 4635 $widget->emit (size_allocate => $w, $h);
4636 }
3838 } 4637 }
3839 } 4638 }
3840 } 4639 }
3841 4640
3842 while ($self->{post_alloc_hook}) { 4641 while ($self->{post_alloc_hook}) {
3843 $_->() 4642 $_->()
3844 for values %{delete $self->{post_alloc_hook}}; 4643 for values %{delete $self->{post_alloc_hook}};
3845 } 4644 }
3846
3847 4645
3848 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4646 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3849 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4647 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3850 glClear GL_COLOR_BUFFER_BIT; 4648 glClear GL_COLOR_BUFFER_BIT;
3851 4649
3854 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4652 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3855 glMatrixMode GL_MODELVIEW; 4653 glMatrixMode GL_MODELVIEW;
3856 glLoadIdentity; 4654 glLoadIdentity;
3857 4655
3858 { 4656 {
3859 package CFClient::UI::Base; 4657 package DC::UI::Base;
3860 4658
3861 ($draw_x, $draw_y, $draw_w, $draw_h) = 4659 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3862 (0, 0, $self->{w}, $self->{h}); 4660 (0, 0, $self->{w}, $self->{h});
3863 }
3864 4661
3865 $self->_draw; 4662 $self->_draw;
4663 }
3866} 4664}
3867 4665
3868############################################################################# 4666#############################################################################
3869 4667
3870package CFClient::UI; 4668package DC::UI;
3871 4669
3872$ROOT = new CFClient::UI::Root; 4670$ROOT = new DC::UI::Root;
3873$TOOLTIP = new CFClient::UI::Tooltip z => 900; 4671$TOOLTIP = new DC::UI::Tooltip z => 900;
3874 4672
38751 46731
3876

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines