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.69 by root, Tue Apr 11 17:32:14 2006 UTC vs.
Revision 1.482 by root, Sat Aug 13 23:18:19 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines