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.195 by root, Thu May 11 23:54:31 2006 UTC vs.
Revision 1.453 by root, Wed Dec 26 20:46:39 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines