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.202 by root, Sun May 14 20:51:19 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
17our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
18 20
19sub check_tooltip { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
20 if (!$GRAB) { 24 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
23
24 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
29 $TOOLTIP->hide;
30
25 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
26 33
27 my $tip = $widget->{tooltip}; 34 return if $ENV{CFPLUS_DEBUG} & 8;
28 35
29 $tip = $tip->($widget) if CODE:: eq ref $tip;
30
31 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show; 37 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 if ($x + $TOOLTIP->{w} > $::WIDTH) {
37 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
38 }
39
40 $TOOLTIP->move ($x, $y);
41 $TOOLTIP->check_size;
42 $TOOLTIP->update;
43 } 38 }
44 39
45 return; 40 return;
46 } 41 }
47 } 42 }
48 } 43 }
49 44
50 $TOOLTIP->hide; 45 $TOOLTIP->hide;
46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
51 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;
52} 71}
53 72
54# class methods for events 73# class methods for events
55sub feed_sdl_key_down_event { 74sub feed_sdl_key_down_event {
56 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 75 $FOCUS->emit (key_down => $_[0])
57 if $FOCUS; 76 if $FOCUS;
58} 77}
59 78
60sub feed_sdl_key_up_event { 79sub feed_sdl_key_up_event {
61 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 80 $FOCUS->emit (key_up => $_[0])
62 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 }
63} 95}
64 96
65sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
66 my ($ev) = @_; 98 my ($ev) = @_;
67 my ($x, $y) = ($ev->{x}, $ev->{y}); 99 my ($x, $y) = ($ev->{x}, $ev->{y});
68 100
69 if (!$BUTTON_STATE) { 101 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
102
103 unless ($GRAB) {
70 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
71 105
72 $GRAB = $widget; 106 $GRAB = $widget;
73 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
74 108
75 check_tooltip; 109 $TOOLTIP_WATCHER->invoke;
76 } 110 }
77
78 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
79 111
80 if ($GRAB) { 112 if ($GRAB) {
81 ($x, $y) = $GRAB->coord2local ($x, $y); 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
82 $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 }
83 } 125 }
84} 126}
85 127
86sub feed_sdl_button_up_event { 128sub feed_sdl_button_up_event {
87 my ($ev) = @_; 129 my ($ev) = @_;
88 my ($x, $y) = ($ev->{x}, $ev->{y});
89 130
90 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 131 my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y});
91 132
92 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 133 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
93 134
94 if ($GRAB) { 135 $GRAB->emit (button_up => $ev)
95 ($x, $y) = $GRAB->coord2local ($x, $y); 136 if $GRAB && $ev->{button} != 4 && $ev->{button} != 5;
96 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
97 }
98 137
99 if (!$BUTTON_STATE) { 138 unless ($BUTTON_STATE) {
100 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
101 $grab->update if $grab; 140 $grab->update if $grab;
102 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
103 142
104 check_tooltip; 143 check_hover $widget;
144 $TOOLTIP_WATCHER->invoke;
105 } 145 }
106} 146}
107 147
108sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
109 my ($ev) = @_; 149 my ($ev) = @_;
110 my ($x, $y) = ($ev->{x}, $ev->{y}); 150 my ($x, $y) = ($ev->{x}, $ev->{y});
111 151
112 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 152 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
113 153
114 if ($widget != $HOVER) { 154 check_hover $widget;
115 my $hover = $HOVER; $HOVER = $widget;
116 155
117 $hover->update if $hover && $hover->{can_hover}; 156 $HOVER->emit (mouse_motion => $ev)
118 $HOVER->update if $HOVER && $HOVER->{can_hover};
119
120 check_tooltip;
121 }
122
123 if ($HOVER) { 157 if $HOVER;
124 ($x, $y) = $HOVER->coord2local ($x, $y);
125 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
126 }
127} 158}
128 159
129# convert position array to integers 160# convert position array to integers
130sub harmonize { 161sub harmonize {
131 my ($vals) = @_; 162 my ($vals) = @_;
137 $rem += $_ - $i; 168 $rem += $_ - $i;
138 $_ = $i; 169 $_ = $i;
139 } 170 }
140} 171}
141 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
142# call when resolution changes etc. 189# call when resolution changes etc.
143sub rescale_widgets { 190sub rescale_widgets {
144 my ($sx, $sy) = @_; 191 my ($sx, $sy) = @_;
145 192
146 for my $widget (values %WIDGET) { 193 for my $widget (values %WIDGET) {
147 if ($widget->{toplevel}) { 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
148 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
149 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
150 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 200 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
151 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
152 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 201 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
153 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 202 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
154 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 203 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
155 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_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
156 } 208 }
157
158 $widget->reconfigure;
159 } 209 }
210
211 reconfigure_widgets;
160} 212}
161 213
162############################################################################# 214#############################################################################
163 215
216package dc::UI::Event;
217
218sub xy {
219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
220}
221
222#############################################################################
223
164package CFClient::UI::Base; 224package dc::UI::Base;
165 225
166use strict; 226use strict;
167 227
168use CFClient::OpenGL; 228use dc::OpenGL;
169 229
170sub new { 230sub new {
171 my $class = shift; 231 my $class = shift;
172 232
173 my $self = bless { 233 my $self = bless {
174 x => 0, 234 x => "center",
175 y => 0, 235 y => "center",
176 z => 0, 236 z => 0,
237 w => undef,
238 h => undef,
177 can_events => 1, 239 can_events => 1,
178 @_ 240 @_
179 }, $class; 241 }, $class;
180 242
243 dc::weaken ($dc::UI::WIDGET{$self+0} = $self);
244
181 for (keys %$self) { 245 for (keys %$self) {
182 if (/^connect_(.*)$/) { 246 if (/^on_(.*)$/) {
183 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
184 } 248 }
185 } 249 }
186 250
187 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 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 }
188 262
189 $self 263 $self
190} 264}
191 265
192sub destroy { 266sub destroy {
193 my ($self) = @_; 267 my ($self) = @_;
194 268
195 $self->hide; 269 $self->hide;
270 $self->emit ("destroy");
196 %$self = (); 271 %$self = ();
197} 272}
198 273
274sub TO_JSON {
275 { "\fw" => $_[0]{s_id} }
276}
277
199sub show { 278sub show {
200 my ($self) = @_; 279 my ($self) = @_;
201 280
202 return if $self->{parent}; 281 return if $self->{parent};
203 282
204 $self->{toplevel} = 1;
205 $CFClient::UI::ROOT->add ($self); 283 $dc::UI::ROOT->add ($self);
206} 284}
207 285
208sub hide { 286sub set_visible {
209 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};
210 310
211 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
212 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
213 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}) {
214 $self->{parent}->remove ($self) 346 $parent->remove ($self);
215 if $self->{parent}; 347 }
216} 348}
217 349
218sub move { 350sub move_abs {
219 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
220 352
221 $self->{x} = int $x; 353 $self->{x} = List::Util::max 0, List::Util::min $self->{root}{w} - $self->{w}, int $x;
222 $self->{y} = int $y; 354 $self->{y} = List::Util::max 0, List::Util::min $self->{root}{h} - $self->{h}, int $y;
223 $self->{z} = $z if defined $z; 355 $self->{z} = $z if defined $z;
224 356
225 $self->update; 357 $self->update;
226} 358}
227 359
228sub set_size { 360sub set_size {
229 my ($self, $w, $h) = @_; 361 my ($self, $w, $h) = @_;
230 362
231 $self->{user_w} = $w; 363 $self->{force_w} = $w;
232 $self->{user_h} = $h; 364 $self->{force_h} = $h;
233 365
234 $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)
235} 377}
236 378
237sub size_request { 379sub size_request {
238 require Carp; 380 require Carp;
239 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
240} 382}
241 383
384sub baseline_shift {
385 0
386}
387
242sub configure { 388sub configure {
243 my ($self, $x, $y, $w, $h) = @_; 389 my ($self, $x, $y, $w, $h) = @_;
244 390
245 if ($self->{aspect}) { 391 if ($self->{aspect}) {
392 my ($ow, $oh) = ($w, $h);
393
246 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 394 $w = List::Util::min $w, dc::ceil $h * $self->{aspect};
247 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 395 $h = List::Util::min $h, dc::ceil $w / $self->{aspect};
248 396
249 # use alignment to adjust x, y 397 # use alignment to adjust x, y
250 398
251 $x += int +($w - $w2) * 0.5; 399 $x += int 0.5 * ($ow - $w);
252 $y += int +($h - $h2) * 0.5; 400 $y += int 0.5 * ($oh - $h);
253
254 ($w, $h) = ($w2, $h2);
255 } 401 }
256 402
257 if ($self->{x} != $x || $self->{y} != $y) { 403 if ($self->{x} ne $x || $self->{y} ne $y) {
258 $self->{x} = $x; 404 $self->{x} = $x;
259 $self->{y} = $y; 405 $self->{y} = $y;
260 $self->update; 406 $self->update;
261 } 407 }
262 408
263 if ($self->{w} != $w || $self->{h} != $h) { 409 if ($self->{alloc_w} != $w || $self->{alloc_h} != $h) {
264 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 410 return unless $self->{visible};
265 }
266}
267 411
268sub size_allocate { 412 $self->{alloc_w} = $w;
269 # nothing to be done 413 $self->{alloc_h} = $h;
270}
271 414
272sub reconfigure { 415 $self->{root}{size_alloc}{$self+0} = $self;
273 my ($self) = @_; 416 }
274
275 $self->check_size (1);
276 $self->update;
277} 417}
278 418
279sub children { 419sub children {
420 # nop
421}
422
423sub visible_children {
424 $_[0]->children
280} 425}
281 426
282sub set_max_size { 427sub set_max_size {
283 my ($self, $w, $h) = @_; 428 my ($self, $w, $h) = @_;
284 429
285 delete $self->{max_w}; $self->{max_w} = $w if $w; 430 $self->{max_w} = int $w if defined $w;
286 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 }
287} 450}
288 451
289# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
290sub coord2local { 453sub coord2local {
291 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
292 455
456 return (undef, undef) unless $self->{parent};
457
293 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
294} 459}
295 460
296# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
297sub coord2global { 462sub coord2global {
298 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
299 464
465 return (undef, undef) unless $self->{parent};
466
300 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
301} 468}
302 469
303sub focus_in { 470sub invoke_focus_in {
304 my ($self) = @_; 471 my ($self) = @_;
305 472
306 return if $FOCUS == $self; 473 return if $FOCUS == $self;
307 return unless $self->{can_focus}; 474 return unless $self->{can_focus};
308 475
309 my $focus = $FOCUS; $FOCUS = $self; 476 $FOCUS = $self;
310 477
311 $self->emit (focus_in => $focus); 478 $self->update;
312 479
313 $focus->update if $focus; 480 0
314 $FOCUS->update;
315} 481}
316 482
317sub focus_out { 483sub invoke_focus_out {
318 my ($self) = @_; 484 my ($self) = @_;
319 485
320 return unless $FOCUS == $self; 486 return unless $FOCUS == $self;
321 487
322 my $focus = $FOCUS; undef $FOCUS; 488 undef $FOCUS;
323 489
324 $self->emit (focus_out => $focus); 490 $self->update;
325 491
326 $focus->update if $focus; #? 492 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus
327} 493 unless $FOCUS;
328 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
329sub mouse_motion { } 505sub invoke_mouse_motion { 0 }
330sub button_up { } 506sub invoke_button_up { 0 }
331sub key_down { } 507sub invoke_key_down { 0 }
332sub key_up { } 508sub invoke_key_up { 0 }
509sub invoke_mouse_wheel { 0 }
333 510
334sub button_down { 511sub invoke_button_down {
335 my ($self, $ev, $x, $y) = @_; 512 my ($self, $ev, $x, $y) = @_;
336 513
337 $self->focus_in; 514 $self->grab_focus;
338}
339 515
340sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 516 0
341sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 517}
342sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
343sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
344sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
345 518
346sub 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 {
347 my ($self) = @_; 531 my ($self, $signal) = @_;
348 532
349 return unless $self->{h} && $self->{w}; 533 delete $self->{signal_cb}{$signal};
350
351 glPushMatrix;
352 glTranslate $self->{x}, $self->{y}, 0;
353 $self->_draw;
354 glPopMatrix;
355
356 if ($self == $HOVER && $self->{can_hover}) {
357 my ($x, $y) = @$self{qw(x y)};
358
359 glColor 1, 0.8, 0.5, 0.2;
360 glEnable GL_BLEND;
361 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
362 glBegin GL_QUADS;
363 glVertex $x , $y;
364 glVertex $x + $self->{w}, $y;
365 glVertex $x + $self->{w}, $y + $self->{h};
366 glVertex $x , $y + $self->{h};
367 glEnd;
368 glDisable GL_BLEND;
369 }
370
371 if ($ENV{PCLIENT_DEBUG}) {
372 glPushMatrix;
373 glColor 1, 1, 0, 1;
374 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
375 glBegin GL_LINE_LOOP;
376 glVertex 0 , 0;
377 glVertex $self->{w}, 0;
378 glVertex $self->{w}, $self->{h};
379 glVertex 0 , $self->{h};
380 glEnd;
381 glPopMatrix;
382 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
383 }
384} 534}
385 535
386sub _draw { 536my %has_coords = (
387 my ($self) = @_; 537 button_down => 1,
538 button_up => 1,
539 mouse_motion => 1,
540 mouse_wheel => 1,
541);
388 542
389 warn "no draw defined for $self\n"; 543sub emit {
390} 544 my ($self, $signal, @args) = @_;
391 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
392sub find_widget { 566#sub find_widget {
393 my ($self, $x, $y) = @_; 567# in .xs
394
395 return () unless $self->{can_events};
396
397 return $self
398 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
399 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
400
401 ()
402}
403 568
404sub set_parent { 569sub set_parent {
405 my ($self, $parent) = @_; 570 my ($self, $parent) = @_;
406 571
407 Scalar::Util::weaken ($self->{parent} = $parent); 572 dc::weaken ($self->{parent} = $parent);
408 573 $self->set_visible if $parent->{visible};
409 # TODO: req_w _does_change after ->reconfigure
410 $self->check_size
411 unless exists $self->{req_w};
412} 574}
413 575
414sub check_size { 576sub realloc {
415 my ($self, $forced) = @_; 577 my ($self) = @_;
416 578
417 $self->{force_alloc} = 1 if $forced; 579 if ($self->{visible}) {
418 $CFClient::UI::ROOT->{check_size}{$self} = $self; 580 return if $self->{root}{realloc}{$self+0};
581
582 $self->{root}{realloc}{$self+0} = $self;
583 $self->{root}->update;
584 } else {
585 delete $self->{req_w};
586 delete $self->{req_h};
587 }
419} 588}
420 589
421sub update { 590sub update {
422 my ($self) = @_; 591 my ($self) = @_;
423 592
424 $self->{parent}->update 593 $self->{parent}->update
425 if $self->{parent}; 594 if $self->{parent};
426} 595}
427 596
428sub connect { 597sub reconfigure {
429 my ($self, $signal, $cb) = @_; 598 my ($self) = @_;
430 599
431 push @{ $self->{signal_cb}{$signal} }, $cb; 600 $self->realloc;
601 $self->update;
432} 602}
433 603
434sub emit { 604# using global variables seems a bit hacky, but passing through all drawing
435 my ($self, $signal, @args) = @_; 605# functions seems pointless.
606our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
436 607
437 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";
438} 615}
439 616
440sub DESTROY { 617sub DESTROY {
441 my ($self) = @_; 618 my ($self) = @_;
442 619
620 return if dc::in_destruct;
621
622 local $@;
623 eval { $self->destroy };
624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
625
443 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
444 #$self->deactivate;
445} 627}
446 628
447############################################################################# 629#############################################################################
448 630
449package CFClient::UI::DrawBG; 631package dc::UI::DrawBG;
450 632
451our @ISA = CFClient::UI::Base::; 633our @ISA = dc::UI::Base::;
452 634
453use strict; 635use strict;
454use CFClient::OpenGL; 636use dc::OpenGL;
455 637
456sub new { 638sub new {
457 my $class = shift; 639 my $class = shift;
458 640
459 # range [value, low, high, page]
460
461 $class->SUPER::new ( 641 $class->SUPER::new (
462 bg => [0, 0, 0, 0.2], 642 #bg => [0, 0, 0, 0.2],
463 active_bg => [1, 1, 1, 0.5], 643 #active_bg => [1, 1, 1, 0.5],
464 @_ 644 @_
465 ) 645 )
466} 646}
467 647
468sub _draw { 648sub _draw {
469 my ($self) = @_; 649 my ($self) = @_;
470 650
651 my $color = $FOCUS == $self && $self->{active_bg}
652 ? $self->{active_bg}
653 : $self->{bg};
654
655 if ($color && (@$color < 4 || $color->[3])) {
471 my ($w, $h) = @$self{qw(w h)}; 656 my ($w, $h) = @$self{qw(w h)};
472 657
473 glEnable GL_BLEND; 658 glEnable GL_BLEND;
474 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 659 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
475 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 660 glColor_premultiply @$color;
476 661 glRect 0, 0, $w, $h;
477 glBegin GL_QUADS;
478 glVertex 0 , 0;
479 glVertex 0 , $h;
480 glVertex $w, $h;
481 glVertex $w, 0;
482 glEnd;
483
484 glDisable GL_BLEND; 662 glDisable GL_BLEND;
663 }
485} 664}
486 665
487############################################################################# 666#############################################################################
488 667
489package CFClient::UI::Empty; 668package dc::UI::Empty;
490 669
491our @ISA = CFClient::UI::Base::; 670our @ISA = dc::UI::Base::;
492 671
493sub new { 672sub new {
494 my ($class, %arg) = @_; 673 my ($class, %arg) = @_;
495 $class->SUPER::new (can_events => 0, %arg); 674 $class->SUPER::new (can_events => 0, %arg);
496} 675}
497 676
498sub size_request { 677sub size_request {
499 (0, 0) 678 my ($self) = @_;
679
680 ($self->{w} + 0, $self->{h} + 0)
500} 681}
501 682
502sub draw { } 683sub draw { }
503 684
504############################################################################# 685#############################################################################
505 686
506package CFClient::UI::Container; 687package dc::UI::Container;
507 688
508our @ISA = CFClient::UI::Base::; 689our @ISA = dc::UI::Base::;
509 690
510sub new { 691sub new {
511 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
512 693
513 my $children = delete $arg{children} || []; 694 my $children = delete $arg{children};
514 695
515 my $self = $class->SUPER::new ( 696 my $self = $class->SUPER::new (
516 children => [], 697 children => [],
517 can_events => 0, 698 can_events => 0,
518 %arg, 699 %arg,
519 ); 700 );
701
520 $self->add ($_) for @$children; 702 $self->add (@$children)
703 if $children && @$children;
521 704
522 $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;
523} 714}
524 715
525sub add { 716sub add {
526 my ($self, @widgets) = @_; 717 my ($self, @widgets) = @_;
527 718
528 $_->set_parent ($self) 719 $_->set_parent ($self)
529 for @widgets; 720 for @widgets;
530 721
722 # TODO: only do this in widgets that need it, e.g. root, fixed
531 use sort 'stable'; 723 use sort 'stable';
532 724
533 $self->{children} = [ 725 $self->{children} = [
534 sort { $a->{z} <=> $b->{z} } 726 sort { $a->{z} <=> $b->{z} }
535 @{$self->{children}}, @widgets 727 @{$self->{children}}, @widgets
536 ]; 728 ];
537 729
538 $self->check_size (1); 730 $self->realloc;
539 $self->update; 731
732 $self->emit (c_add => \@widgets);
733
734 map $_+0, @widgets
540} 735}
541 736
542sub children { 737sub children {
543 @{ $_[0]{children} } 738 @{ $_[0]{children} }
544} 739}
545 740
546sub remove { 741sub remove {
547 my ($self, $child) = @_; 742 my ($self, @widgets) = @_;
548 743
744 $self->emit (c_remove => \@widgets);
745
746 for my $child (@widgets) {
549 delete $child->{parent}; 747 delete $child->{parent};
550 $child->hide; 748 $child->hide;
551
552 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 749 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
750 }
553 751
554 $self->check_size; 752 $self->realloc;
555 $self->update;
556} 753}
557 754
558sub clear { 755sub clear {
559 my ($self) = @_; 756 my ($self) = @_;
560 757
561 my $children = delete $self->{children}; 758 my $children = $self->{children};
562 $self->{children} = []; 759 $self->{children} = [];
563 760
564 for (@$children) { 761 for (@$children) {
565 delete $_->{parent}; 762 delete $_->{parent};
566 $_->hide; 763 $_->hide;
567 } 764 }
568 765
569 $self->check_size; 766 $self->realloc;
570 $self->update;
571} 767}
572 768
573sub find_widget { 769sub find_widget {
574 my ($self, $x, $y) = @_; 770 my ($self, $x, $y) = @_;
575 771
576 $x -= $self->{x}; 772 $x -= $self->{x};
577 $y -= $self->{y}; 773 $y -= $self->{y};
578 774
579 my $res; 775 my $res;
580 776
581 for (reverse @{ $self->{children} }) { 777 for (reverse $self->visible_children) {
582 $res = $_->find_widget ($x, $y) 778 $res = $_->find_widget ($x, $y)
583 and return $res; 779 and return $res;
584 } 780 }
585 781
586 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) 782 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
587} 783}
588 784
589sub _draw { 785sub _draw {
590 my ($self) = @_; 786 my ($self) = @_;
591 787
592 $_->draw for @{$self->{children}}; 788 $_->draw for $self->visible_children;
593} 789}
594 790
595############################################################################# 791#############################################################################
596 792
597package CFClient::UI::Bin; 793package dc::UI::Bin;
598 794
599our @ISA = CFClient::UI::Container::; 795our @ISA = dc::UI::Container::;
600 796
601sub new { 797sub new {
602 my ($class, %arg) = @_; 798 my ($class, %arg) = @_;
603 799
604 my $child = (delete $arg{child}) || new CFClient::UI::Empty::; 800 my $child = (delete $arg{child}) || new dc::UI::Empty::;
605 801
606 $class->SUPER::new (children => [$child], %arg) 802 $class->SUPER::new (children => [$child], %arg)
607} 803}
608 804
609sub add { 805sub add {
610 my ($self, $child) = @_; 806 my ($self, $child) = @_;
611 807
612 $self->{children} = []; 808 $self->clear;
613
614 $self->SUPER::add ($child); 809 $self->SUPER::add ($child);
615} 810}
616 811
617sub remove { 812sub remove {
618 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
619 814
620 $self->SUPER::remove ($widget); 815 $self->SUPER::remove ($widget);
621 816
622 $self->{children} = [new CFClient::UI::Empty] 817 $self->{children} = [new dc::UI::Empty]
623 unless @{$self->{children}}; 818 unless @{$self->{children}};
624} 819}
625 820
626sub child { $_[0]->{children}[0] } 821sub child { $_[0]->{children}[0] }
627 822
628sub size_request { 823sub size_request {
629 $_[0]{children}[0]->size_request 824 $_[0]{children}[0]->size_request
630} 825}
631 826
632sub size_allocate { 827sub invoke_size_allocate {
633 my ($self, $w, $h) = @_; 828 my ($self, $w, $h) = @_;
634 829
635 $self->{children}[0]->configure (0, 0, $w, $h); 830 $self->{children}[0]->configure (0, 0, $w, $h);
831
832 1
636} 833}
637 834
638############################################################################# 835#############################################################################
836# back-buffered drawing area
639 837
640package CFClient::UI::Window; 838package dc::UI::Window;
641 839
642our @ISA = CFClient::UI::Bin::; 840our @ISA = dc::UI::Bin::;
643 841
644use CFClient::OpenGL; 842use dc::OpenGL;
645 843
646sub new { 844sub new {
647 my ($class, %arg) = @_; 845 my ($class, %arg) = @_;
648 846
649 my $self = $class->SUPER::new (%arg); 847 my $self = $class->SUPER::new (%arg);
654 852
655 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 853 $ROOT->on_post_alloc ($self => sub { $self->render_child });
656 $self->SUPER::update; 854 $self->SUPER::update;
657} 855}
658 856
659sub size_allocate { 857sub invoke_size_allocate {
660 my ($self, $w, $h) = @_; 858 my ($self, $w, $h) = @_;
661 859
662 $self->SUPER::size_allocate ($w, $h);
663 $self->update; 860 $self->update;
861
862 $self->SUPER::invoke_size_allocate ($w, $h)
664} 863}
665 864
666sub _render { 865sub _render {
866 my ($self) = @_;
867
667 $_[0]{children}[0]->draw; 868 $self->{children}[0]->draw;
668} 869}
669 870
670sub render_child { 871sub render_child {
671 my ($self) = @_; 872 my ($self) = @_;
672 873
673 $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 {
674 glClearColor 0, 0, 0, 0; 875 glClearColor 0, 0, 0, 0;
675 glClear GL_COLOR_BUFFER_BIT; 876 glClear GL_COLOR_BUFFER_BIT;
676 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
677 $self->_render; 884 $self->_render;
678# glColorMask 1, 1, 1, 0; 885 }
679# glEnable GL_BLEND;
680# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
681# glRasterPos 0, 0;
682# glCopyPixels 0, 0, $self->{w}, $self->{h};
683# glDisable GL_BLEND;
684# glColorMask 1, 1, 1, 1;
685 }; 886 };
686} 887}
687 888
688sub _draw { 889sub _draw {
689 my ($self) = @_; 890 my ($self) = @_;
690
691 my ($w, $h) = ($self->w, $self->h);
692 891
693 my $tex = $self->{texture} 892 my $tex = $self->{texture}
694 or return; 893 or return;
695 894
696 glEnable GL_TEXTURE_2D; 895 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 896 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
698 glColor 0, 0, 0, 1; 897 glColor 0, 0, 0, 1;
699 898
700 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 899 $tex->draw_quad_alpha_premultiplied (0, 0);
701 900
702 glDisable GL_TEXTURE_2D; 901 glDisable GL_TEXTURE_2D;
703} 902}
704 903
705############################################################################# 904#############################################################################
706 905
707package CFClient::UI::ViewPort; 906package dc::UI::ViewPort;
708 907
908use List::Util qw(min max);
909
709our @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}
710 921
711sub size_request { 922sub size_request {
712 my ($self) = @_; 923 my ($self) = @_;
713 924
714 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 925 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
715 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
716 926
717 @$self{qw(child_w child_h)} 927 $w = 1 if $self->{scroll_x};
718} 928 $h = 1 if $self->{scroll_y};
719 929
930 ($w, $h)
931}
932
720sub size_allocate { 933sub invoke_size_allocate {
721 my ($self, $w, $h) = @_; 934 my ($self, $w, $h) = @_;
722 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);
723 $self->update; 942 $self->update;
943
944 1
724} 945}
725 946
726sub set_offset { 947sub set_offset {
727 my ($self, $x, $y) = @_; 948 my ($self, $x, $y) = @_;
728 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}) {
729 $self->{view_x} = int $x; 954 $self->{view_x} = $x;
730 $self->{view_y} = int $y; 955 $self->{view_y} = $y;
731 956
957 $self->emit (changed => $x, $y);
732 $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 }
733} 978}
734 979
735# 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
736sub coord2local { 981sub coord2local {
737 my ($self, $x, $y) = @_; 982 my ($self, $x, $y) = @_;
752 my ($self, $x, $y) = @_; 997 my ($self, $x, $y) = @_;
753 998
754 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
755 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
756 ) { 1001 ) {
757 $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})
758 } else { 1003 } else {
759 $self->CFClient::UI::Base::find_widget ($x, $y) 1004 $self->dc::UI::Base::find_widget ($x, $y)
760 } 1005 }
761} 1006}
762 1007
763sub _render { 1008sub _render {
764 my ($self) = @_; 1009 my ($self) = @_;
765 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
766 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1014 dc::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
767 1015
768 $self->SUPER::_render; 1016 $self->SUPER::_render;
769} 1017}
770 1018
771############################################################################# 1019#############################################################################
772 1020
773package CFClient::UI::ScrolledWindow; 1021package dc::UI::ScrolledWindow;
774 1022
775our @ISA = CFClient::UI::HBox::; 1023our @ISA = dc::UI::Table::;
776 1024
777sub new { 1025sub new {
778 my $class = shift; 1026 my ($class, %arg) = @_;
1027
1028 my $child = delete $arg{child};
779 1029
780 my $self; 1030 my $self;
781 1031
782 my $slider = new CFClient::UI::Slider 1032 my $hslider = new dc::UI::Slider
1033 c_col => 0,
1034 c_row => 1,
783 vertical => 1, 1035 vertical => 0,
784 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
785 connect_changed => sub { 1037 on_changed => sub {
786 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 1038 $self->{hpos} = $_[1];
1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
787 }, 1040 },
788 ; 1041 ;
789 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
790 $self = $class->SUPER::new ( 1054 $self = $class->SUPER::new (
791 vp => (new CFClient::UI::ViewPort), 1055 scroll_x => 0,
1056 scroll_y => 1,
1057 can_events => 1,
792 slider => $slider, 1058 hslider => $hslider,
793 @_, 1059 vslider => $vslider,
1060 col_expand => [1, 0],
1061 row_expand => [1, 0],
1062 %arg,
794 ); 1063 );
795 1064
796 $self->{vp}->add ($self->{scrolled}); 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
797 $self->add ($self->{vp}); 1086 $self->SUPER::add ($self->{vp});
798 $self->add ($self->{slider}); 1087
1088 $self->add ($child) if $child;
799 1089
800 $self 1090 $self
801} 1091}
802 1092
803#TODO# update range on size_allocate depending on child 1093sub add {
804# update viewport offset on scroll 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}
805 1198
806############################################################################# 1199#############################################################################
807 1200
808package CFClient::UI::Frame; 1201package dc::UI::Frame;
809 1202
810our @ISA = CFClient::UI::Bin::; 1203our @ISA = dc::UI::Bin::;
811 1204
812use CFClient::OpenGL; 1205use dc::OpenGL;
813 1206
814sub new { 1207sub new {
815 my $class = shift; 1208 my $class = shift;
816 1209
817 $class->SUPER::new ( 1210 $class->SUPER::new (
825 1218
826 if ($self->{bg}) { 1219 if ($self->{bg}) {
827 my ($w, $h) = @$self{qw(w h)}; 1220 my ($w, $h) = @$self{qw(w h)};
828 1221
829 glEnable GL_BLEND; 1222 glEnable GL_BLEND;
830 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1223 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
831 glColor @{ $self->{bg} }; 1224 glColor_premultiply @{ $self->{bg} };
832
833 glBegin GL_QUADS;
834 glVertex 0 , 0;
835 glVertex 0 , $h;
836 glVertex $w, $h; 1225 glRect 0, 0, $w, $h;
837 glVertex $w, 0;
838 glEnd;
839
840 glDisable GL_BLEND; 1226 glDisable GL_BLEND;
841 } 1227 }
842 1228
843 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
844} 1230}
845 1231
846############################################################################# 1232#############################################################################
847 1233
848package CFClient::UI::FancyFrame; 1234package dc::UI::FancyFrame;
849 1235
850our @ISA = CFClient::UI::Bin::; 1236our @ISA = dc::UI::Bin::;
851 1237
852use CFClient::OpenGL; 1238use dc::OpenGL;
853
854my @tex =
855 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
856 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
857 1239
858sub new { 1240sub new {
859 my $class = shift; 1241 my ($class, %arg) = @_;
860 1242
861 # TODO: user_x, user_y, overwrite moveto? 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 }
862 1250
863 my $self = $class->SUPER::new ( 1251 my $self = $class->SUPER::new (
1252 # label => "",
864 bg => [1, 1, 1, 1], 1253 fg => [0.6, 0.3, 0.1],
865 border_bg => [1, 1, 1, 1],
866 border => 0.6, 1254 border => 0.8,
867 toplevel => 1, 1255 style => 'single',
868 can_events => 1, 1256 %arg,
869 @_
870 ); 1257 );
871 1258
872 $self->{title} &&= new CFClient::UI::Label
873 align => 0,
874 valign => 1,
875 text => $self->{title},
876 fontsize => $self->{border};
877
878 $self 1259 $self
1260}
1261
1262sub add {
1263 my ($self, @widgets) = @_;
1264
1265 $self->SUPER::add (@widgets);
1266 $self->dc::UI::Container::add ($self->{label}) if $self->{label};
879} 1267}
880 1268
881sub border { 1269sub border {
882 int $_[0]{border} * $::FONTSIZE 1270 int $_[0]{border} * $::FONTSIZE
883} 1271}
884 1272
885sub size_request { 1273sub size_request {
886 my ($self) = @_; 1274 my ($self) = @_;
1275
1276 ($self->{label_w}, undef) = $self->{label}->size_request
1277 if $self->{label};
887 1278
888 my ($w, $h) = $self->SUPER::size_request; 1279 my ($w, $h) = $self->SUPER::size_request;
889 1280
890 ( 1281 (
891 $w + $self->border * 2, 1282 $w + $self->border * 2,
892 $h + $self->border * 2, 1283 $h + $self->border * 2,
893 ) 1284 )
894} 1285}
895 1286
896sub size_allocate { 1287sub invoke_size_allocate {
897 my ($self, $w, $h) = @_; 1288 my ($self, $w, $h) = @_;
898 1289
899 $h -= List::Util::max 0, $self->border * 2; 1290 my $border = $self->border;
1291
900 $w -= List::Util::max 0, $self->border * 2; 1292 $w -= List::Util::max 0, $border * 2;
1293 $h -= List::Util::max 0, $border * 2;
901 1294
902 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 1295 if (my $label = $self->{label}) {
903 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 }
904 1300
905 $self->child->configure ($self->border, $self->border, $w, $h); 1301 $self->child->configure ($border, $border, $w, $h);
906}
907 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
908sub button_down { 1452sub invoke_button_down {
909 my ($self, $ev, $x, $y) = @_; 1453 my ($self, $ev, $x, $y) = @_;
910 1454
911 my ($w, $h) = @$self{qw(w h)}; 1455 my ($w, $h) = @$self{qw(w h)};
912 my $border = $self->border; 1456 my $border = $self->border;
913 1457
926 my ($ev, $x, $y) = @_; 1470 my ($ev, $x, $y) = @_;
927 1471
928 my $dx = $ev->{x} - $ox; 1472 my $dx = $ev->{x} - $ox;
929 my $dy = $ev->{y} - $oy; 1473 my $dy = $ev->{y} - $oy;
930 1474
931 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1475 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
932 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1476 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1477
933 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1478 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
934 $self->check_size; 1479 $self->realloc;
935 }; 1480 };
936 1481
937 } elsif ($lr ^ $td) { 1482 } elsif ($lr ^ $td) {
938 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1483 my ($ox, $oy) = ($ev->{x}, $ev->{y});
939 my ($bx, $by) = ($self->{x}, $self->{y}); 1484 my ($bx, $by) = ($self->{x}, $self->{y});
941 $self->{motion} = sub { 1486 $self->{motion} = sub {
942 my ($ev, $x, $y) = @_; 1487 my ($ev, $x, $y) = @_;
943 1488
944 ($x, $y) = ($ev->{x}, $ev->{y}); 1489 ($x, $y) = ($ev->{x}, $ev->{y});
945 1490
946 $self->move ($bx + $x - $ox, $by + $y - $oy); 1491 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
947 $self->update; 1492 # HACK: the next line is required to enforce placement
1493 $self->{parent}->invoke_size_allocate ($self->{parent}{w}, $self->{parent}{h});
948 }; 1494 };
1495 } else {
1496 return 0;
1497 }
1498
949 } 1499 1
950} 1500}
951 1501
952sub button_up { 1502sub invoke_button_up {
953 my ($self, $ev, $x, $y) = @_; 1503 my ($self, $ev, $x, $y) = @_;
954 1504
955 delete $self->{motion}; 1505 ! ! delete $self->{motion}
956} 1506}
957 1507
958sub mouse_motion { 1508sub invoke_mouse_motion {
959 my ($self, $ev, $x, $y) = @_; 1509 my ($self, $ev, $x, $y) = @_;
960 1510
961 $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
962} 1522}
963 1523
964sub _draw { 1524sub _draw {
965 my ($self) = @_; 1525 my ($self) = @_;
966 1526
1527 my $child = $self->{children}[0];
1528
967 my ($w, $h ) = ($self->{w}, $self->{h}); 1529 my ($w, $h ) = ($self->{w}, $self->{h});
968 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1530 my ($cw, $ch) = ($child->{w}, $child->{h});
969 1531
970 glEnable GL_TEXTURE_2D; 1532 glEnable GL_TEXTURE_2D;
971 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1533 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
972 1534
973 my $border = $self->border; 1535 my $border = $self->border;
974 1536
975 glColor @{ $self->{border_bg} }; 1537 glColor @{ $self->{border_bg} };
976 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1538 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
977 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1539 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
978 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1540 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
979 $tex[4]->draw_quad_alpha (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);
980 1556
981 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1557 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
982 my $bg = $tex[0]; 1558 glColor @{ $self->{bg} };
983 1559
984 # TODO: repeat texture not scale 1560 # TODO: repeat texture not scale
1561 # solve this better(?)
985 my $rep_x = $cw / $bg->{w}; 1562 $bg->{s} = $cw / $bg->{w};
986 my $rep_y = $ch / $bg->{h}; 1563 $bg->{t} = $ch / $bg->{h};
987
988 glColor @{ $self->{bg} };
989
990 $bg->{s} = $rep_x;
991 $bg->{t} = $rep_y;
992 $bg->{wrap_mode} = 1;
993 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1564 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
994 } 1565 }
995 1566
996 glDisable GL_TEXTURE_2D; 1567 glDisable GL_TEXTURE_2D;
997 1568
998 $self->{title}->draw if $self->{title};
999
1000 $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};
1001} 1580}
1002 1581
1003############################################################################# 1582#############################################################################
1004 1583
1005package CFClient::UI::Table; 1584package dc::UI::Table;
1006 1585
1007our @ISA = CFClient::UI::Base::; 1586our @ISA = dc::UI::Container::;
1008 1587
1009use List::Util qw(max sum); 1588use List::Util qw(max sum);
1010 1589
1011use CFClient::OpenGL; 1590use dc::OpenGL;
1012 1591
1013sub new { 1592sub new {
1014 my $class = shift; 1593 my $class = shift;
1015 1594
1016 $class->SUPER::new ( 1595 $class->SUPER::new (
1017 col_expand => [], 1596 col_expand => [],
1597 row_expand => [],
1018 @_ 1598 @_,
1019 ) 1599 )
1020} 1600}
1021 1601
1022sub add { 1602sub add {
1023 my ($self, $x, $y, $child) = @_;
1024
1025 $child->set_parent ($self);
1026 $self->{children}[$y][$x] = $child;
1027
1028 $child->check_size;
1029}
1030
1031sub children {
1032 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1033}
1034
1035# TODO: move to container class maybe? send childs a signal on removal?
1036sub clear {
1037 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);
1038 1634
1039 my @children = $self->children; 1635 my @children = $self->children;
1040 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 }
1041 1646 }
1042 for (@children) {
1043 delete $_->{parent};
1044 $_->hide;
1045 }
1046 1647
1047 $self->update; 1648 # second pass, rows
1048} 1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1049
1050sub get_wh {
1051 my ($self) = @_;
1052
1053 my (@w, @h);
1054
1055 for my $y (0 .. $#{$self->{children}}) {
1056 my $row = $self->{children}[$y]
1057 or next;
1058
1059 for my $x (0 .. $#$row) {
1060 my $widget = $row->[$x]
1061 or next;
1062 my ($w, $h) = @$widget{qw(req_w req_h)}; 1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1063 1651
1064 $w[$x] = max $w[$x], $w; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1065 $h[$y] = max $h[$y], $h; 1653
1654 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1066 } 1656 }
1067 } 1657 }
1068 1658
1069 (\@w, \@h) 1659 (\@w, \@h)
1070} 1660}
1078 (sum @$ws), 1668 (sum @$ws),
1079 (sum @$hs), 1669 (sum @$hs),
1080 ) 1670 )
1081} 1671}
1082 1672
1083sub size_allocate { 1673sub invoke_size_allocate {
1084 my ($self, $w, $h) = @_; 1674 my ($self, $w, $h) = @_;
1085 1675
1086 my ($ws, $hs) = $self->get_wh; 1676 my ($ws, $hs) = $self->get_wh;
1087 1677
1088 my $req_w = sum @$ws; 1678 my $req_w = (sum @$ws) || 1;
1089 my $req_h = sum @$hs; 1679 my $req_h = (sum @$hs) || 1;
1090 1680
1091 # TODO: nicer code && do row_expand 1681 # now linearly scale the rows/columns to the allocated size
1092 my @col_expand = @{$self->{col_expand}}; 1682 my @col_expand = @{$self->{col_expand}};
1093 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1094 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1095 1685
1096 # linearly scale sizes
1097 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1098 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1099 1687
1100 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
1101 CFClient::UI::harmonize $hs; 1696 dc::UI::harmonize $hs;
1102 1697
1103 my $y; 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1104 1700
1105 for my $r (0 .. $#{$self->{children}}) { 1701 for my $widget ($self->children) {
1106 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)};
1107 or next;
1108 1703
1109 my $x = 0; 1704 $widget->configure (
1110 my $row_h = $hs->[$r]; 1705 $x[$c], $y[$r],
1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1111 1707 );
1112 for my $c (0 .. $#$row) { 1708 }
1113 my $col_w = $ws->[$c];
1114 1709
1115 if (my $widget = $row->[$c]) { 1710 1
1116 $widget->configure ($x, $y, $col_w, $row_h); 1711}
1117 }
1118 1712
1119 $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};
1120 } 1740 }
1121
1122 $y += $row_h;
1123 } 1741 }
1124 1742
1125} 1743 my $W = $x2 - $x1;
1744 my $H = $y2 - $y1;
1126 1745
1127sub 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 {
1128 my ($self, $x, $y) = @_; 1764 my ($self, $W, $H) = @_;
1129 1765
1130 $x -= $self->{x}; 1766 for my $child ($self->visible_children) {
1131 $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;
1132 1769
1133 my $res; 1770 $x += $child->{c_halign} * $child->{req_w};
1771 $y += $child->{c_valign} * $child->{req_h};
1134 1772
1135 for (grep $_, map @$_, grep $_, @{ $self->{children} }) { 1773 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1136 $res = $_->find_widget ($x, $y) 1774 }
1137 and return $res; 1775
1138 } 1776 1
1139
1140 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1141}
1142
1143sub _draw {
1144 my ($self) = @_;
1145
1146 for (grep $_, @{$self->{children}}) {
1147 $_->draw for grep $_, @$_;
1148 }
1149} 1777}
1150 1778
1151############################################################################# 1779#############################################################################
1152 1780
1153package CFClient::UI::HBox; 1781package dc::UI::Box;
1154 1782
1155# TODO: wrap into common Box base class
1156
1157our @ISA = CFClient::UI::Container::; 1783our @ISA = dc::UI::Container::;
1158 1784
1159sub size_request { 1785sub size_request {
1160 my ($self) = @_; 1786 my ($self) = @_;
1161 1787
1162 my @alloc = map [$_->size_request], @{$self->{children}}; 1788 my @children = $self->visible_children;
1163 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 }
1164 ( 1826 }
1165 (List::Util::sum map $_->[0], @alloc), 1827
1166 (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 @_,
1167 ) 1853 )
1168} 1854}
1169 1855
1170sub size_allocate {
1171 my ($self, $w, $h) = @_;
1172
1173 ($h, $w) = ($w, $h);
1174
1175 my $children = $self->{children};
1176
1177 my @h = map $_->{req_w}, @$children;
1178
1179 my $req_h = List::Util::sum @h;
1180
1181 if ($req_h > $h) {
1182 # ah well, not enough space
1183 $_ *= $h / $req_h for @h;
1184 } else {
1185 my $exp = List::Util::sum map $_->{expand}, @$children;
1186 $exp ||= 1;
1187
1188 for (0 .. $#$children) {
1189 my $child = $children->[$_];
1190
1191 my $alloc_h = $h[$_];
1192 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1193 $h[$_] = $alloc_h;
1194 }
1195 }
1196
1197 CFClient::UI::harmonize \@h;
1198
1199 my $y = 0;
1200 for (0 .. $#$children) {
1201 my $child = $children->[$_];
1202 my $h = $h[$_];
1203 $child->configure ($y, 0, $h, $w);
1204
1205 $y += $h;
1206 }
1207
1208 1
1209}
1210
1211############################################################################# 1856#############################################################################
1212 1857
1213package CFClient::UI::VBox; 1858package dc::UI::VBox;
1214 1859
1215# TODO: wrap into common Box base class 1860our @ISA = dc::UI::Box::;
1216 1861
1217our @ISA = CFClient::UI::Container::; 1862sub new {
1863 my $class = shift;
1218 1864
1219sub size_request { 1865 $class->SUPER::new (
1220 my ($self) = @_; 1866 vertical => 1,
1221 1867 @_,
1222 my @alloc = map [$_->size_request], @{$self->{children}};
1223
1224 (
1225 (List::Util::max map $_->[0], @alloc),
1226 (List::Util::sum map $_->[1], @alloc),
1227 ) 1868 )
1228} 1869}
1229 1870
1230sub size_allocate {
1231 my ($self, $w, $h) = @_;
1232
1233 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1234
1235 my $children = $self->{children};
1236
1237 my @h = map $_->{req_h}, @$children;
1238
1239 my $req_h = List::Util::sum @h;
1240
1241 if ($req_h > $h) {
1242 # ah well, not enough space
1243 $_ *= $h / $req_h for @h;
1244 } else {
1245 my $exp = List::Util::sum map $_->{expand}, @$children;
1246 $exp ||= 1;
1247
1248 for (0 .. $#$children) {
1249 my $child = $children->[$_];
1250
1251 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1252 }
1253 }
1254
1255 CFClient::UI::harmonize \@h;
1256
1257 my $y = 0;
1258 for (0 .. $#$children) {
1259 my $child = $children->[$_];
1260 my $h = $h[$_];
1261 $child->configure (0, $y, $w, $h);
1262
1263 $y += $h;
1264 }
1265
1266 1
1267}
1268
1269############################################################################# 1871#############################################################################
1270 1872
1271package CFClient::UI::Label; 1873package dc::UI::Label;
1272 1874
1273our @ISA = CFClient::UI::Base::; 1875our @ISA = dc::UI::DrawBG::;
1274 1876
1275use CFClient::OpenGL; 1877use dc::OpenGL;
1276 1878
1277sub new { 1879sub new {
1278 my ($class, %arg) = @_; 1880 my ($class, %arg) = @_;
1279 1881
1280 my $self = $class->SUPER::new ( 1882 my $self = $class->SUPER::new (
1281 fg => [1, 1, 1], 1883 fg => [1, 1, 1],
1884 #bg => none
1885 #active_bg => none
1282 #font => default_font 1886 #font => default_font
1283 #text => initial text 1887 #text => initial text
1284 #markup => initial narkup 1888 #markup => initial narkup
1889 #max_w => maximum pixel width
1890 #style => 0, # render flags
1891 ellipsise => 3, # end
1285 layout => (new CFClient::Layout), 1892 layout => (new dc::Layout),
1286 fontsize => 1, 1893 fontsize => 1,
1287 align => -1, 1894 align => -1,
1288 valign => -1, 1895 valign => -1,
1289 padding => 2, 1896 padding_x => 2,
1897 padding_y => 2,
1290 can_events => 0, 1898 can_events => 0,
1291 %arg 1899 %arg
1292 ); 1900 );
1293 1901
1294 if (exists $self->{template}) { 1902 if (exists $self->{template}) {
1295 my $layout = new CFClient::Layout; 1903 my $layout = new dc::Layout;
1296 $layout->set_text (delete $self->{template}); 1904 $layout->set_text (delete $self->{template});
1297 $self->{template} = $layout; 1905 $self->{template} = $layout;
1298 } 1906 }
1299 1907
1300 if (exists $self->{markup}) { 1908 if (exists $self->{markup}) {
1304 } 1912 }
1305 1913
1306 $self 1914 $self
1307} 1915}
1308 1916
1309sub escape {
1310 local $_ = $_[1];
1311
1312 s/&/&amp;/g;
1313 s/>/&gt;/g;
1314 s/</&lt;/g;
1315
1316 $_[1]
1317}
1318
1319sub update { 1917sub update {
1320 my ($self) = @_; 1918 my ($self) = @_;
1321 1919
1322 delete $self->{texture}; 1920 delete $self->{texture};
1323 $self->SUPER::update; 1921 $self->SUPER::update;
1324} 1922}
1325 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
1326sub set_text { 1937sub set_text {
1327 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1328 1939
1329 return if $self->{text} eq "T$text"; 1940 return if $self->{text} eq "T$text";
1330 $self->{text} = "T$text"; 1941 $self->{text} = "T$text";
1331 1942
1332 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1333 $self->{layout}->set_text ($text); 1943 $self->{layout}->set_text ($text);
1334 1944
1945 delete $self->{size_req};
1946 $self->realloc;
1335 $self->update; 1947 $self->update;
1336 $self->check_size;
1337} 1948}
1338 1949
1339sub set_markup { 1950sub set_markup {
1340 my ($self, $markup) = @_; 1951 my ($self, $markup) = @_;
1341 1952
1342 return if $self->{text} eq "M$markup"; 1953 return if $self->{text} eq "M$markup";
1343 $self->{text} = "M$markup"; 1954 $self->{text} = "M$markup";
1344 1955
1345 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1956 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1346 1957
1347 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1348 $self->{layout}->set_markup ($markup); 1958 $self->{layout}->set_markup ($markup);
1349 1959
1960 delete $self->{size_req};
1961 $self->realloc;
1350 $self->update; 1962 $self->update;
1351 $self->check_size;
1352} 1963}
1353 1964
1354sub size_request { 1965sub size_request {
1355 my ($self) = @_; 1966 my ($self) = @_;
1356 1967
1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1357 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1358 $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});
1359 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1360 1976
1361 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
1362 1978
1363 if (exists $self->{template}) { 1979 if (exists $self->{template}) {
1364 $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);
1365 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1982 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1366 1983
1367 my ($w2, $h2) = $self->{template}->size; 1984 my ($w2, $h2) = $self->{template}->size;
1368 1985
1369 $w = List::Util::max $w, $w2; 1986 $w = List::Util::max $w, $w2;
1370 $h = List::Util::max $h, $h2; 1987 $h = List::Util::max $h, $h2;
1988 }
1989
1990 [$w, $h]
1371 } 1991 };
1372 1992
1373 ( 1993 @{ $self->{size_req} }
1374 $w + $self->{padding} * 2,
1375 $h + $self->{padding} * 2,
1376 )
1377} 1994}
1378 1995
1996sub baseline_shift {
1997 $_[0]{layout}->descent
1998}
1999
1379sub size_allocate { 2000sub invoke_size_allocate {
1380 my ($self, $w, $h) = @_; 2001 my ($self, $w, $h) = @_;
1381 2002
2003 delete $self->{ox};
2004
1382 delete $self->{texture}; 2005 delete $self->{texture}
2006 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
2007
2008 1
1383} 2009}
1384 2010
1385sub set_fontsize { 2011sub set_fontsize {
1386 my ($self, $fontsize) = @_; 2012 my ($self, $fontsize) = @_;
1387 2013
1388 $self->{fontsize} = $fontsize; 2014 $self->{fontsize} = $fontsize;
2015 delete $self->{size_req};
1389 delete $self->{texture}; 2016 delete $self->{texture};
1390 2017
1391 $self->update; 2018 $self->realloc;
1392 $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;
1393} 2028}
1394 2029
1395sub _draw { 2030sub _draw {
1396 my ($self) = @_; 2031 my ($self) = @_;
1397 2032
2033 $self->SUPER::_draw; # draw background, if applicable
2034
1398 my $tex = $self->{texture} ||= do { 2035 my $size = $self->{texture} ||= do {
1399 $self->{layout}->set_foreground (@{$self->{fg}}); 2036 $self->{layout}->set_foreground (@{$self->{fg}});
1400 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2037 $self->{layout}->set_font ($self->{font}) if $self->{font};
1401 $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});
1402 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 2041 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1403 2042
1404 my $tex = new_from_layout CFClient::Texture $self->{layout}; 2043 [$self->{layout}->size]
1405
1406 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1407 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1408 : ($self->{w} - $tex->{w}) * 0.5;
1409
1410 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1411 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1412 : ($self->{h} - $tex->{h}) * 0.5;
1413
1414 $tex
1415 }; 2044 };
1416 2045
1417 glEnable GL_TEXTURE_2D; 2046 unless (exists $self->{ox}) {
1418 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);
1419 2050
1420 if ($tex->{format} == GL_ALPHA) { 2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1421 glColor @{$self->{fg}}; 2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1422 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); 2053 : ($self->{h} - $size->[1]) * 0.5);
1423 } else { 2054
1424 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); 2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1425 } 2056 };
1426 2057
1427 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;
1428} 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#}
1429 2077
1430############################################################################# 2078#############################################################################
1431 2079
1432package CFClient::UI::EntryBase; 2080package dc::UI::EntryBase;
1433 2081
1434our @ISA = CFClient::UI::Label::; 2082our @ISA = dc::UI::Label::;
1435 2083
1436use CFClient::OpenGL; 2084use dc::OpenGL;
1437 2085
1438sub new { 2086sub new {
1439 my $class = shift; 2087 my $class = shift;
1440 2088
1441 $class->SUPER::new ( 2089 $class->SUPER::new (
1442 fg => [1, 1, 1], 2090 fg => [1, 1, 1],
1443 bg => [0, 0, 0, 0.2], 2091 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1],
1444 active_bg => [1, 1, 1, 0.5], 2093 active_bg => [0, 0, 1, .2],
1445 active_fg => [0, 0, 0], 2094 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0],
1446 can_hover => 1, 2096 can_hover => 1,
1447 can_focus => 1, 2097 can_focus => 1,
1448 valign => 0, 2098 valign => 0,
1449 can_events => 1, 2099 can_events => 1,
2100 ellipsise => 0,
2101 #text => ...
2102 #hidden => "*",
1450 @_ 2103 @_
1451 ) 2104 )
1452} 2105}
1453 2106
1454sub _set_text { 2107sub _set_text {
1456 2109
1457 delete $self->{cur_h}; 2110 delete $self->{cur_h};
1458 2111
1459 return if $self->{text} eq $text; 2112 return if $self->{text} eq $text;
1460 2113
1461 delete $self->{texture};
1462
1463 $self->{last_activity} = $::NOW; 2114 $self->{last_activity} = $::NOW;
1464 $self->{text} = $text; 2115 $self->{text} = $text;
1465 2116
1466 $text =~ s/./*/g if $self->{hidden}; 2117 $text =~ s/./*/g if $self->{hidden};
1467 $self->{layout}->set_text ("$text "); 2118 $self->{layout}->set_text ("$text ");
2119 delete $self->{size_req};
1468 2120
1469 $self->emit (changed => $self->{text}); 2121 $self->emit (changed => $self->{text});
2122
2123 $self->realloc;
2124 $self->update;
1470} 2125}
1471 2126
1472sub set_text { 2127sub set_text {
1473 my ($self, $text) = @_; 2128 my ($self, $text) = @_;
1474 2129
1475 $self->{cursor} = length $text; 2130 $self->{cursor} = length $text;
1476 $self->_set_text ($text); 2131 $self->_set_text ($text);
1477 $self->check_size;
1478 $self->update;
1479} 2132}
1480 2133
1481sub get_text { 2134sub get_text {
1482 $_[0]{text} 2135 $_[0]{text}
1483} 2136}
1488 my ($w, $h) = $self->SUPER::size_request; 2141 my ($w, $h) = $self->SUPER::size_request;
1489 2142
1490 ($w + 1, $h) # add 1 for cursor 2143 ($w + 1, $h) # add 1 for cursor
1491} 2144}
1492 2145
1493sub size_allocate {
1494 my ($self, $w, $h) = @_;
1495
1496 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1497}
1498
1499sub key_down { 2146sub invoke_key_down {
1500 my ($self, $ev) = @_; 2147 my ($self, $ev) = @_;
1501 2148
1502 my $mod = $ev->{mod}; 2149 my $mod = $ev->{mod};
1503 my $sym = $ev->{sym}; 2150 my $sym = $ev->{sym};
1504 my $uni = $ev->{unicode}; 2151 my $uni = $ev->{unicode};
1505 2152
1506 my $text = $self->get_text; 2153 my $text = $self->get_text;
2154
2155 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
1507 2156
1508 if ($uni == 8) { 2157 if ($uni == 8) {
1509 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1510 } elsif ($uni == 127) { 2159 } elsif ($uni == 127) {
1511 substr $text, $self->{cursor}, 1, ""; 2160 substr $text, $self->{cursor}, 1, "";
1512 } elsif ($sym == CFClient::SDLK_LEFT) { 2161 } elsif ($sym == dc::SDLK_LEFT) {
1513 --$self->{cursor} if $self->{cursor}; 2162 --$self->{cursor} if $self->{cursor};
1514 } elsif ($sym == CFClient::SDLK_RIGHT) { 2163 } elsif ($sym == dc::SDLK_RIGHT) {
1515 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2164 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1516 } 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 = "";
1517 $self->{cursor} = 0; 2179 $self->{cursor} = 0;
1518 } elsif ($sym == CFClient::SDLK_END) {
1519 $self->{cursor} = length $text;
1520 } elsif ($uni == 27) { 2180 } elsif ($uni == 27) {
1521 $self->emit ('escape'); 2181 $self->emit ('escape');
1522 } elsif ($uni) { 2182 } elsif ($uni == 0x0d) {
2183 substr $text, $self->{cursor}++, 0, "\012";
2184 } elsif ($uni >= 0x20) {
1523 substr $text, $self->{cursor}++, 0, chr $uni; 2185 substr $text, $self->{cursor}++, 0, chr $uni;
2186 } else {
2187 return 0;
1524 } 2188 }
1525 2189
1526 $self->_set_text ($text); 2190 $self->_set_text ($text);
2191
2192 $self->realloc;
1527 $self->update; 2193 $self->update;
1528}
1529 2194
2195 1
2196}
2197
1530sub focus_in { 2198sub invoke_focus_in {
1531 my ($self) = @_; 2199 my ($self) = @_;
1532 2200
1533 $self->{last_activity} = $::NOW; 2201 $self->{last_activity} = $::NOW;
1534 2202
1535 $self->SUPER::focus_in; 2203 $self->SUPER::invoke_focus_in
1536} 2204}
1537 2205
1538sub button_down { 2206sub invoke_button_down {
1539 my ($self, $ev, $x, $y) = @_; 2207 my ($self, $ev, $x, $y) = @_;
1540 2208
1541 $self->SUPER::button_down ($ev, $x, $y); 2209 $self->SUPER::invoke_button_down ($ev, $x, $y);
1542 2210
1543 my $idx = $self->{layout}->xy_to_index ($x, $y); 2211 my $idx = $self->{layout}->xy_to_index ($x, $y);
1544 2212
1545 # byte-index to char-index 2213 # byte-index to char-index
1546 my $text = $self->{text}; 2214 my $text = $self->{text};
1547 utf8::encode $text; 2215 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1548 $self->{cursor} = length substr $text, 0, $idx; 2216 $self->{cursor} = length $text;
1549 2217
1550 $self->_set_text ($self->{text}); 2218 $self->_set_text ($self->{text});
1551 $self->update; 2219 $self->update;
2220
2221 1
1552} 2222}
1553 2223
1554sub mouse_motion { 2224sub invoke_mouse_motion {
1555 my ($self, $ev, $x, $y) = @_; 2225 my ($self, $ev, $x, $y) = @_;
1556# 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
1557} 2229}
1558 2230
1559sub _draw { 2231sub _draw {
1560 my ($self) = @_; 2232 my ($self) = @_;
1561 2233
1562 local $self->{fg} = $self->{fg}; 2234 local $self->{fg} = $self->{fg};
1563 2235
1564 if ($FOCUS == $self) { 2236 if ($FOCUS == $self) {
1565 glColor @{$self->{active_bg}}; 2237 glColor_premultiply @{$self->{active_bg}};
1566 $self->{fg} = $self->{active_fg}; 2238 $self->{fg} = $self->{active_fg};
1567 } else { 2239 } else {
1568 glColor @{$self->{bg}}; 2240 glColor_premultiply @{$self->{bg}};
1569 } 2241 }
1570 2242
1571 glEnable GL_BLEND; 2243 glEnable GL_BLEND;
1572 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2244 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1573 glBegin GL_QUADS;
1574 glVertex 0 , 0;
1575 glVertex 0 , $self->{h};
1576 glVertex $self->{w}, $self->{h}; 2245 glRect 0, 0, $self->{w}, $self->{h};
1577 glVertex $self->{w}, 0;
1578 glEnd;
1579 glDisable GL_BLEND; 2246 glDisable GL_BLEND;
1580 2247
1581 $self->SUPER::_draw; 2248 $self->SUPER::_draw;
1582 2249
1583 #TODO: force update every cursor change :( 2250 #TODO: force update every cursor change :(
1585 2252
1586 unless (exists $self->{cur_h}) { 2253 unless (exists $self->{cur_h}) {
1587 my $text = substr $self->{text}, 0, $self->{cursor}; 2254 my $text = substr $self->{text}, 0, $self->{cursor};
1588 utf8::encode $text; 2255 utf8::encode $text;
1589 2256
1590 @$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);
1591 } 2258 }
1592 2259
1593 glColor @{$self->{fg}}; 2260 glColor_premultiply @{$self->{active_fg}};
1594 glBegin GL_LINES; 2261 glBegin GL_LINES;
1595 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2262 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
1596 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};
1597 glEnd; 2264 glEnd;
1598 }
1599}
1600 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
1601package CFClient::UI::Entry; 2284package dc::UI::Entry;
1602 2285
1603our @ISA = CFClient::UI::EntryBase::; 2286our @ISA = dc::UI::EntryBase::;
1604 2287
1605use CFClient::OpenGL; 2288use dc::OpenGL;
1606 2289
1607sub key_down { 2290sub invoke_key_down {
1608 my ($self, $ev) = @_; 2291 my ($self, $ev) = @_;
1609 2292
1610 my $sym = $ev->{sym}; 2293 my $sym = $ev->{sym};
1611 2294
1612 if ($sym == 13) { 2295 if ($ev->{uni} == 0x0d || $sym == 13) {
1613 unshift @{$self->{history}}, 2296 unshift @{$self->{history}},
1614 my $txt = $self->get_text; 2297 my $txt = $self->get_text;
2298
1615 $self->{history_pointer} = -1; 2299 $self->{history_pointer} = -1;
1616 $self->{history_saveback} = ''; 2300 $self->{history_saveback} = '';
1617 $self->emit (activate => $txt); 2301 $self->emit (activate => $txt);
1618 $self->update; 2302 $self->update;
1619 2303
1620 } elsif ($sym == CFClient::SDLK_UP) { 2304 } elsif ($sym == dc::SDLK_UP) {
1621 if ($self->{history_pointer} < 0) { 2305 if ($self->{history_pointer} < 0) {
1622 $self->{history_saveback} = $self->get_text; 2306 $self->{history_saveback} = $self->get_text;
1623 } 2307 }
1624 if (@{$self->{history} || []} > 0) { 2308 if (@{$self->{history} || []} > 0) {
1625 $self->{history_pointer}++; 2309 $self->{history_pointer}++;
1627 $self->{history_pointer} = @{$self->{history} || []} - 1; 2311 $self->{history_pointer} = @{$self->{history} || []} - 1;
1628 } 2312 }
1629 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2313 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1630 } 2314 }
1631 2315
1632 } elsif ($sym == CFClient::SDLK_DOWN) { 2316 } elsif ($sym == dc::SDLK_DOWN) {
1633 $self->{history_pointer}--; 2317 $self->{history_pointer}--;
1634 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1635 2319
1636 if ($self->{history_pointer} >= 0) { 2320 if ($self->{history_pointer} >= 0) {
1637 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2321 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1638 } else { 2322 } else {
1639 $self->set_text ($self->{history_saveback}); 2323 $self->set_text ($self->{history_saveback});
1640 } 2324 }
1641 2325
1642 } else { 2326 } else {
1643 $self->SUPER::key_down ($ev); 2327 return $self->SUPER::invoke_key_down ($ev)
2328 }
2329
1644 } 2330 1
1645
1646} 2331}
1647 2332
1648############################################################################# 2333#############################################################################
1649 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
1650package CFClient::UI::Button; 2374package dc::UI::ButtonBin;
1651 2375
1652our @ISA = CFClient::UI::Label::; 2376our @ISA = dc::UI::Bin::;
1653 2377
1654use CFClient::OpenGL; 2378use dc::OpenGL;
1655 2379
1656my @tex = 2380my @tex =
1657 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2381 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
1658 qw(b1_button_active.png); 2382 qw(b1_button_inactive.png b1_button_active.png);
1659 2383
1660sub new { 2384sub new {
1661 my $class = shift; 2385 my $class = shift;
1662 2386
1663 $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 (
1664 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,
1665 fg => [1, 1, 1], 2642 fg => [1, 1, 1],
1666 bg => [1, 1, 1, 0.2],
1667 active_fg => [0, 0, 1], 2643 active_fg => [0, 0, 1],
1668 can_hover => 1, 2644 can_hover => 1,
1669 align => 0, 2645 align => 0,
1670 valign => 0, 2646 valign => 0,
1671 can_events => 1, 2647 can_events => 1,
1672 @_ 2648 @_
1673 ) 2649 );
1674} 2650}
1675 2651
1676sub button_up { 2652sub invoke_button_down {
1677 my ($self, $ev, $x, $y) = @_; 2653 my ($self, $ev, $x, $y) = @_;
1678 2654
2655 1
2656}
2657
2658sub invoke_button_up {
2659 my ($self, $ev, $x, $y) = @_;
2660
2661 $self->emit ("activate")
1679 if ($x >= 0 && $x < $self->{w} 2662 if $x >= 0 && $x < $self->{w}
1680 && $y >= 0 && $y < $self->{h}) { 2663 && $y >= 0 && $y < $self->{h};
1681 $self->emit ("activate"); 2664
1682 } 2665 1
1683}
1684
1685sub _draw {
1686 my ($self) = @_;
1687
1688 local $self->{fg} = $self->{fg};
1689
1690 if ($GRAB == $self) {
1691 $self->{fg} = $self->{active_fg};
1692 }
1693
1694 glEnable GL_TEXTURE_2D;
1695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1696 glColor 0, 0, 0, 1;
1697
1698 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1699
1700 glDisable GL_TEXTURE_2D;
1701
1702 $self->SUPER::_draw;
1703} 2666}
1704 2667
1705############################################################################# 2668#############################################################################
1706 2669
1707package CFClient::UI::CheckBox; 2670package dc::UI::VGauge;
1708 2671
1709our @ISA = CFClient::UI::DrawBG::;
1710
1711my @tex =
1712 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1713 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1714
1715use CFClient::OpenGL;
1716
1717sub new {
1718 my $class = shift;
1719
1720 $class->SUPER::new (
1721 padding => 2,
1722 fg => [1, 1, 1],
1723 active_fg => [1, 1, 0],
1724 state => 0,
1725 can_hover => 1,
1726 @_
1727 )
1728}
1729
1730sub size_request {
1731 my ($self) = @_;
1732
1733 ($self->{padding} * 2 + 6) x 2
1734}
1735
1736sub button_down {
1737 my ($self, $ev, $x, $y) = @_;
1738
1739 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1740 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1741 $self->{state} = !$self->{state};
1742 $self->emit (changed => $self->{state});
1743 }
1744}
1745
1746sub _draw {
1747 my ($self) = @_;
1748
1749 $self->SUPER::_draw;
1750
1751 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1752
1753 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1754
1755 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1756
1757 my $tex = $self->{state} ? $tex[1] : $tex[0];
1758
1759 glEnable GL_TEXTURE_2D;
1760 $tex->draw_quad_alpha (0, 0, $s, $s);
1761 glDisable GL_TEXTURE_2D;
1762}
1763
1764#############################################################################
1765
1766package CFClient::UI::Image;
1767
1768our @ISA = CFClient::UI::Base::; 2672our @ISA = dc::UI::Base::;
1769
1770use CFClient::OpenGL;
1771use Carp qw/confess/;
1772
1773our %loaded_images;
1774
1775sub new {
1776 my $class = shift;
1777
1778 my $self = $class->SUPER::new (can_events => 0, @_);
1779
1780 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1781
1782 $loaded_images{$self->{image}} ||=
1783 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1784
1785 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1786
1787 Scalar::Util::weaken $loaded_images{$self->{image}};
1788
1789 $self->{aspect} = $tex->{w} / $tex->{h};
1790
1791 $self
1792}
1793
1794sub size_request {
1795 my ($self) = @_;
1796
1797 ($self->{tex}->{w}, $self->{tex}->{h})
1798}
1799
1800sub _draw {
1801 my ($self) = @_;
1802
1803 my $tex = $self->{tex};
1804
1805 my ($w, $h) = ($self->{w}, $self->{h});
1806
1807 if ($self->{rot90}) {
1808 glRotate 90, 0, 0, 1;
1809 glTranslate 0, -$self->{w}, 0;
1810
1811 ($w, $h) = ($h, $w);
1812 }
1813
1814 glEnable GL_TEXTURE_2D;
1815 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1816
1817 $tex->draw_quad_alpha (0, 0, $w, $h);
1818
1819 glDisable GL_TEXTURE_2D;
1820}
1821
1822#############################################################################
1823
1824package CFClient::UI::VGauge;
1825
1826our @ISA = CFClient::UI::Base::;
1827 2673
1828use List::Util qw(min max); 2674use List::Util qw(min max);
1829 2675
1830use CFClient::OpenGL; 2676use dc::OpenGL;
1831 2677
1832my %tex = ( 2678my %tex = (
1833 food => [ 2679 food => [
1834 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2680 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
1835 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1836 ], 2682 ],
1837 grace => [ 2683 grace => [
1838 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2684 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
1839 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/
1840 ], 2686 ],
1841 hp => [ 2687 hp => [
1842 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2688 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
1843 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1844 ], 2690 ],
1845 mana => [ 2691 mana => [
1846 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2692 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
1847 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/
1848 ], 2694 ],
1849); 2695);
1850 2696
1851# eg. VGauge->new (gauge => 'food'), default gauge: food 2697# eg. VGauge->new (gauge => 'food'), default gauge: food
1911 my $ycut1 = max 0, min 1, $ycut; 2757 my $ycut1 = max 0, min 1, $ycut;
1912 my $ycut2 = max 0, min 1, $ycut - 1; 2758 my $ycut2 = max 0, min 1, $ycut - 1;
1913 2759
1914 my $h1 = $self->{h} * (1 - $ycut1); 2760 my $h1 = $self->{h} * (1 - $ycut1);
1915 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);
1916 2765
1917 glEnable GL_BLEND; 2766 glEnable GL_BLEND;
1918 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;
1919 glEnable GL_TEXTURE_2D; 2769 glEnable GL_TEXTURE_2D;
1920 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1921 2771
1922 glBindTexture GL_TEXTURE_2D, $t1->{name}; 2772 glBindTexture GL_TEXTURE_2D, $t1->{name};
1923 glBegin GL_QUADS; 2773 glBegin GL_QUADS;
1938 2788
1939 if ($t3) { 2789 if ($t3) {
1940 glBindTexture GL_TEXTURE_2D, $t3->{name}; 2790 glBindTexture GL_TEXTURE_2D, $t3->{name};
1941 glBegin GL_QUADS; 2791 glBegin GL_QUADS;
1942 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; 2792 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1943 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; 2793 glTexCoord 0 , $t3->{t}; glVertex 0 , $h3;
1944 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; 2794 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $h3;
1945 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; 2795 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1946 glEnd; 2796 glEnd;
1947 } 2797 }
1948 2798
1949 glDisable GL_BLEND; 2799 glDisable GL_BLEND;
1950 glDisable GL_TEXTURE_2D; 2800 glDisable GL_TEXTURE_2D;
1951} 2801}
1952 2802
1953############################################################################# 2803#############################################################################
1954 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
1955package CFClient::UI::Gauge; 2943package dc::UI::Gauge;
1956 2944
1957our @ISA = CFClient::UI::VBox::; 2945our @ISA = dc::UI::VBox::;
1958 2946
1959sub new { 2947sub new {
1960 my ($class, %arg) = @_; 2948 my ($class, %arg) = @_;
1961 2949
1962 my $self = $class->SUPER::new ( 2950 my $self = $class->SUPER::new (
1964 can_hover => 1, 2952 can_hover => 1,
1965 can_events => 1, 2953 can_events => 1,
1966 %arg, 2954 %arg,
1967 ); 2955 );
1968 2956
1969 $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");
1970 $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);
1971 $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");
1972 2960
1973 $self 2961 $self
1974} 2962}
1975 2963
1976sub set_fontsize { 2964sub set_fontsize {
1997 $self->{value}->set_text ($val); 2985 $self->{value}->set_text ($val);
1998} 2986}
1999 2987
2000############################################################################# 2988#############################################################################
2001 2989
2002package CFClient::UI::Slider; 2990package dc::UI::Slider;
2003 2991
2004use strict; 2992use strict;
2005 2993
2006use CFClient::OpenGL; 2994use dc::OpenGL;
2007 2995
2008our @ISA = CFClient::UI::DrawBG::; 2996our @ISA = dc::UI::DrawBG::;
2009 2997
2010my @tex = 2998my @tex =
2011 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 2999 map { new_from_file dc::Texture dc::find_rcfile $_ }
2012 qw(s1_slider.png s1_slider_bg.png); 3000 qw(s1_slider.png s1_slider_bg.png);
2013 3001
2014sub new { 3002sub new {
2015 my $class = shift; 3003 my $class = shift;
2016 3004
2017 # range [value, low, high, page] 3005 # range [value, low, high, page, unit]
2018 3006
2019 # TODO: 0-width page 3007 # TODO: 0-width page
2020 # TODO: req_w/h are wrong with vertical 3008 # TODO: req_w/h are wrong with vertical
2021 # TODO: calculations are off 3009 # TODO: calculations are off
2022 my $self = $class->SUPER::new ( 3010 my $self = $class->SUPER::new (
2023 fg => [1, 1, 1], 3011 fg => [1, 1, 1],
2024 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],
2025 range => [0, 0, 100, 10], 3015 range => [0, 0, 100, 10, 0],
2026 req_w => $::WIDTH / 80, 3016 min_w => $::WIDTH / 80,
2027 req_h => $::WIDTH / 80, 3017 min_h => $::WIDTH / 80,
2028 vertical => 0, 3018 vertical => 0,
2029 can_hover => 1, 3019 can_hover => 1,
2030 inner_pad => 5, 3020 inner_pad => 0.02,
2031 @_ 3021 @_
2032 ); 3022 );
2033 3023
3024 $self->set_value ($self->{range}[0]);
3025 $self->update;
3026
2034 $self 3027 $self
2035} 3028}
2036 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
2037sub size_request { 3064sub size_request {
2038 my ($self) = @_; 3065 my ($self) = @_;
2039 3066
2040 my $w = $self->{req_w}; 3067 ($self->{req_w}, $self->{req_h})
2041 my $h = $self->{req_h};
2042
2043 $self->{vertical} ? ($h, $w) : ($w, $h)
2044} 3068}
2045 3069
2046sub button_down { 3070sub invoke_button_down {
2047 my ($self, $ev, $x, $y) = @_; 3071 my ($self, $ev, $x, $y) = @_;
2048 3072
2049 $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
2050 $self->mouse_motion ($ev, $x, $y); 3077 $self->invoke_mouse_motion ($ev, $x, $y);
2051}
2052 3078
3079 1
3080}
3081
2053sub mouse_motion { 3082sub invoke_mouse_motion {
2054 my ($self, $ev, $x, $y) = @_; 3083 my ($self, $ev, $x, $y) = @_;
2055 3084
2056 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
2057 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3125 my ($value, $lo, $hi, $page) = @{$self->{range}};
3126 my $range = ($hi - $page - $lo) || 1e-100;
2058 3127
2059 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3128 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2060 3129
2061 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 3130 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2062 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 3131 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2063 3132
2064 $x -= $inner_pad_px; # substract the padding 3133 $value = ($value - $lo) / $range;
2065 $x = $x * ($hi - $lo) / $inner_w + $lo; 3134 $value = $value * $self->{scale} + $self->{offset};
2066 $x = $lo if $x < $lo;
2067 $x = $hi - $page if $x > $hi - $page;
2068 $self->{range}[0] = $x;
2069 3135
2070 $self->emit (changed => $x); 3136 $self->{knob_x} = $value - $knob_w * 0.5;
2071 $self->update; 3137 $self->{knob_w} = $knob_w;
2072 } 3138 }
2073}
2074
2075# the inner_* stuff is for generating a padding for the slider handle,
2076# so that the handle doesn't leave the texture. This calculation isn't 100%
2077# correct propably, but it does the job for now
2078sub _calc_inner_pad_px {
2079 my ($self, $w) = @_;
2080 ($w / 100) * $self->{inner_pad} # % to pixels
2081}
2082
2083sub _draw {
2084 my ($self) = @_;
2085 3139
2086 $self->SUPER::_draw (); 3140 $self->SUPER::_draw ();
2087 3141
2088 my ($w, $h) = @$self{qw(w h)}; 3142 glScale $self->{w}, $self->{h};
2089 3143
2090 if ($self->{vertical}) { 3144 if ($self->{vertical}) {
2091 # draw a vertical slider like a rotated horizontal slider 3145 # draw a vertical slider like a rotated horizontal slider
2092 3146
3147 glTranslate 1, 0, 0;
2093 glRotate 90, 0, 0, 1; 3148 glRotate 90, 0, 0, 1;
2094 glTranslate 0, -$self->{w}, 0;
2095
2096 ($w, $h) = ($h, $w);
2097 } 3149 }
2098 3150
2099 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 3151 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2100 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 3152 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2101 3153
2102 my ($value, $lo, $hi, $page) = @{$self->{range}};
2103
2104 $hi = $value + 1 if $lo == $hi;
2105
2106 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2107 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2108
2109 $page = int $page * $inner_w / ($hi - $lo);
2110 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2111
2112 $w -= $page;
2113 $page &= ~1;
2114 glTranslate $page * 0.5, 0, 0;
2115 $page ||= 2;
2116
2117 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2118 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2119
2120 glEnable GL_TEXTURE_2D; 3154 glEnable GL_TEXTURE_2D;
2121 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3155 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2122 3156
2123 # draw background 3157 # draw background
2124 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 3158 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2125 3159
2126 # draw handle 3160 # draw handle
2127 $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);
2128 3162
2129 glDisable GL_TEXTURE_2D; 3163 glDisable GL_TEXTURE_2D;
2130} 3164}
2131 3165
2132############################################################################# 3166#############################################################################
2133 3167
2134package CFClient::UI::TextView; 3168package dc::UI::ValSlider;
2135 3169
2136our @ISA = CFClient::UI::HBox::; 3170our @ISA = dc::UI::HBox::;
2137 3171
2138use 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;
2139 3214
2140sub new { 3215sub new {
2141 my $class = shift; 3216 my $class = shift;
2142 3217
2143 my $self = $class->SUPER::new ( 3218 my $self = $class->SUPER::new (
2144 fontsize => 1, 3219 fontsize => 1,
2145 can_events => 0, 3220 can_events => 1,
3221 indent => 0,
2146 #font => default_font 3222 #font => default_font
2147 @_, 3223 @_,
2148 3224
2149 layout => (new CFClient::Layout 1), 3225 layout => (new dc::Layout),
2150 par => [], 3226 par => [],
3227 max_par => 0,
2151 height => 0, 3228 height => 0,
2152 children => [ 3229 children => [
2153 (new CFClient::UI::Empty expand => 1), 3230 (new dc::UI::Empty expand => 1),
2154 (new CFClient::UI::Slider vertical => 1), 3231 (new dc::UI::Slider vertical => 1),
2155 ], 3232 ],
2156 ); 3233 );
2157 3234
2158 $self->{children}[1]->connect (changed => sub { $self->update }); 3235 $self->{children}[1]->connect (changed => sub { $self->update });
2159 3236
2165 3242
2166 $self->{fontsize} = $fontsize; 3243 $self->{fontsize} = $fontsize;
2167 $self->reflow; 3244 $self->reflow;
2168} 3245}
2169 3246
2170sub 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 {
2171 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) = @_;
2172 3285
2173 my $layout = $self->{layout}; 3286 my $layout = $self->{layout};
2174 3287
3288 $layout->set_font ($self->{font}) if $self->{font};
3289 $layout->set_foreground (@{$para->{fg}});
2175 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 3290 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2176 $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});
2177 $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}}
2178 3299 );
2179 ($layout->size)[1] 3300
3301 $layout
2180} 3302}
2181 3303
2182sub reflow { 3304sub reflow {
2183 my ($self) = @_; 3305 my ($self) = @_;
2184 3306
2185 $self->{need_reflow}++; 3307 $self->{need_reflow}++;
2186 $self->update; 3308 $self->update;
2187} 3309}
2188 3310
2189sub 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 {
2190 my ($self, $w, $h) = @_; 3325 my ($self, $para) = @_;
2191 3326
2192 $self->SUPER::size_allocate ($w, $h); 3327 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2193 3328
2194 $self->{layout}->set_font ($self->{font}) if $self->{font}; 3329 $self->{scroll_to} = $para;
2195 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 3330 $self->update;
2196 $self->{layout}->set_width ($self->{children}[0]{w}); 3331}
2197 3332
2198 $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]);
2199} 3342}
2200 3343
2201sub add_paragraph { 3344sub add_paragraph {
2202 my ($self, $color, $text) = @_; 3345 my $self = shift;
2203 3346
2204 #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 };
2205 3357
2206 my $height = $self->text_height ($text); 3358 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
3359 push @{$self->{par}}, $para;
3360 }
2207 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
2208 $self->{height} += $height; 3399 $self->{height} = $height;
3400 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2209 3401
2210 push @{$self->{par}}, [$height, $color, $text]; 3402 delete $self->{texture};
3403 }
2211 3404
2212 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 3405 if (my $paridx = delete $self->{scroll_to}) {
2213 $self->{children}[1]->update; 3406 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3407 }
2214} 3408}
2215 3409
2216sub update { 3410sub update {
2217 my ($self) = @_; 3411 my ($self) = @_;
2218 3412
2220 3414
2221 return unless $self->{h} > 0; 3415 return unless $self->{h} > 0;
2222 3416
2223 delete $self->{texture}; 3417 delete $self->{texture};
2224 3418
2225 $ROOT->on_post_alloc ($self, sub { 3419 $ROOT->on_post_alloc ($self => sub {
2226 if (delete $self->{need_reflow}) { 3420 $self->force_uptodate;
2227 my $height = 0;
2228 3421
2229 $height += $_->[0] = $self->text_height ($_->[2]) 3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2230 for @{$self->{par}};
2231 3423
2232 $self->{height} = $height; 3424 $self->{texture} ||= new_from_opengl dc::Texture $W, $H, sub {
2233
2234 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2235
2236 delete $self->{texture};
2237 }
2238
2239 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2240 glClearColor 0, 0, 0, 0; 3425 glClearColor 0, 0, 0, 0;
2241 glClear GL_COLOR_BUFFER_BIT; 3426 glClear GL_COLOR_BUFFER_BIT;
2242 3427
2243 glEnable GL_TEXTURE_2D; 3428 package dc::UI::Base;
2244 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});
2245 3431
2246 my $top = int $self->{children}[1]{range}[0]; 3432 my $top = int $self->{children}[1]{range}[0];
2247 3433
3434 my $paridx = 0;
3435 my $top_paragraph;
3436 my $top = int $self->{children}[1]{range}[0];
3437
2248 my $y0 = $top; 3438 my $y0 = $top;
2249 my $y1 = $top + $self->{h}; 3439 my $y1 = $top + $H;
2250 3440
2251 my $y = 0;
2252
2253 my $layout = $self->{layout};
2254
2255 $layout->set_font ($self->{font}) if $self->{font};
2256
2257 for my $par (@{$self->{par}}) { 3441 for my $para (@{$self->{par}}) {
2258 my $h = $par->[0]; 3442 my $h = $para->{h};
3443 my $y = $para->{y};
2259 3444
2260 if ($y0 < $y + $h && $y < $y1) { 3445 if ($y0 < $y + $h && $y < $y1) {
2261 $layout->set_foreground (@{ $par->[1] }); 3446 my $layout = $self->get_layout ($para);
2262 $layout->set_markup ($par->[2]);
2263 3447
2264 my ($W, $H) = $layout->size; 3448 $layout->render ($para->{indent}, $y - $y0);
2265 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 }
2266 } 3463 }
2267 3464
2268 $y += $h; 3465 $paridx++;
3466 $top_paragraph ||= $paridx if $y >= $top;
2269 } 3467 }
2270 3468
2271 glDisable GL_TEXTURE_2D; 3469 $self->{top_paragraph} = $top_paragraph;
2272 }; 3470 };
2273 }); 3471 });
2274} 3472}
2275 3473
3474sub reconfigure {
3475 my ($self) = @_;
3476
3477 $self->SUPER::reconfigure;
3478
3479 $_->{w} = 1e10 for @{ $self->{par} };
3480 $self->reflow;
3481}
3482
2276sub _draw { 3483sub _draw {
2277 my ($self) = @_; 3484 my ($self) = @_;
2278 3485
2279 glEnable GL_TEXTURE_2D; 3486 glEnable GL_TEXTURE_2D;
2280 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3487 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2281 glColor 1, 1, 1, 1; 3488 glColor 0, 0, 0, 1;
2282 $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});
2283 glDisable GL_TEXTURE_2D; 3490 glDisable GL_TEXTURE_2D;
2284 3491
2285 $self->{children}[1]->draw; 3492 $self->{children}[1]->draw;
2286
2287} 3493}
2288 3494
2289############################################################################# 3495#############################################################################
2290 3496
2291package CFClient::UI::Animator; 3497package dc::UI::Animator;
2292 3498
2293use CFClient::OpenGL; 3499use dc::OpenGL;
2294 3500
2295our @ISA = CFClient::UI::Bin::; 3501our @ISA = dc::UI::Bin::;
2296 3502
2297sub moveto { 3503sub moveto {
2298 my ($self, $x, $y) = @_; 3504 my ($self, $x, $y) = @_;
2299 3505
2300 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2328 glPopMatrix; 3534 glPopMatrix;
2329} 3535}
2330 3536
2331############################################################################# 3537#############################################################################
2332 3538
2333package CFClient::UI::Flopper; 3539package dc::UI::Flopper;
2334 3540
2335our @ISA = CFClient::UI::Button::; 3541our @ISA = dc::UI::Button::;
2336 3542
2337sub new { 3543sub new {
2338 my $class = shift; 3544 my $class = shift;
2339 3545
2340 my $self = $class->SUPER::new ( 3546 my $self = $class->SUPER::new (
2341 state => 0, 3547 state => 0,
2342 connect_activate => \&toggle_flopper, 3548 on_activate => \&toggle_flopper,
2343 @_ 3549 @_
2344 ); 3550 );
2345 3551
2346 if ($self->{state}) {
2347 $self->{state} = 0;
2348 $self->toggle_flopper;
2349 }
2350
2351 $self 3552 $self
2352} 3553}
2353 3554
2354sub toggle_flopper { 3555sub toggle_flopper {
2355 my ($self) = @_; 3556 my ($self) = @_;
2356 3557
2357 # TODO: use animation 3558 $self->{other}->toggle_visibility;
2358 if ($self->{state} = !$self->{state}) {
2359 $CFClient::UI::ROOT->add ($self->{other});
2360 $self->{other}->move ($self->coord2global (0, $self->{h}));
2361 $self->emit ("open");
2362 } else {
2363 $CFClient::UI::ROOT->remove ($self->{other});
2364 $self->emit ("close");
2365 }
2366
2367 $self->emit (changed => $self->{state});
2368} 3559}
2369 3560
2370############################################################################# 3561#############################################################################
2371 3562
2372package CFClient::UI::Tooltip; 3563package dc::UI::Tooltip;
2373 3564
2374our @ISA = CFClient::UI::Bin::; 3565our @ISA = dc::UI::Bin::;
2375 3566
2376use CFClient::OpenGL; 3567use dc::OpenGL;
2377 3568
2378sub new { 3569sub new {
2379 my $class = shift; 3570 my $class = shift;
2380 3571
2381 $class->SUPER::new ( 3572 $class->SUPER::new (
2385} 3576}
2386 3577
2387sub set_tooltip_from { 3578sub set_tooltip_from {
2388 my ($self, $widget) = @_; 3579 my ($self, $widget) = @_;
2389 3580
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
2390 $self->add (new CFClient::UI::Label 3597 $self->add (new dc::UI::Label
2391 markup => $widget->{tooltip}, 3598 markup => $tip,
2392 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2393 fontsize => 0.8, 3600 fontsize => 0.8,
2394 fg => [0, 0, 0, 1], 3601 style => 1, # FLAG_INVERSE
3602 ellipsise => 0,
2395 font => ($widget->{tooltip_font} || $::FONT_PROP), 3603 font => ($widget->{tooltip_font} || $::FONT_PROP),
2396 ); 3604 );
2397} 3605}
2398 3606
2399sub size_request { 3607sub size_request {
2400 my ($self) = @_; 3608 my ($self) = @_;
2402 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 3610 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2403 3611
2404 ($w + 4, $h + 4) 3612 ($w + 4, $h + 4)
2405} 3613}
2406 3614
2407sub size_allocate { 3615sub invoke_size_allocate {
2408 my ($self, $w, $h) = @_; 3616 my ($self, $w, $h) = @_;
2409 3617
2410 $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 });
2411} 3641}
2412 3642
2413sub _draw { 3643sub _draw {
2414 my ($self) = @_; 3644 my ($self) = @_;
2415 3645
2416 glTranslate 0.375, 0.375;
2417
2418 my ($w, $h) = @$self{qw(w h)}; 3646 my ($w, $h) = @$self{qw(w h)};
2419 3647
2420 glColor 1, 0.8, 0.4; 3648 glColor 1, 0.8, 0.4;
2421 glBegin GL_QUADS; 3649 glRect 0, 0, $w, $h;
2422 glVertex 0 , 0;
2423 glVertex 0 , $h;
2424 glVertex $w, $h;
2425 glVertex $w, 0;
2426 glEnd;
2427 3650
2428 glColor 0, 0, 0; 3651 glColor 0, 0, 0;
2429 glBegin GL_LINE_LOOP; 3652 glRect_lineloop .5, .5, $w + .5, $h + .5;
2430 glVertex 0 , 0;
2431 glVertex 0 , $h;
2432 glVertex $w, $h;
2433 glVertex $w, 0;
2434 glEnd;
2435 3653
2436 glTranslate 2 - 0.375, 2 - 0.375; 3654 glTranslate 2, 2;
3655
2437 $self->SUPER::_draw; 3656 $self->SUPER::_draw;
2438} 3657}
2439 3658
2440############################################################################# 3659#############################################################################
2441 3660
2442package CFClient::UI::Face; 3661package dc::UI::Face;
2443 3662
2444our @ISA = CFClient::UI::Base::; 3663our @ISA = dc::UI::DrawBG::;
2445 3664
2446use CFClient::OpenGL; 3665use dc::OpenGL;
2447 3666
2448sub new { 3667sub new {
2449 my $class = shift; 3668 my $class = shift;
2450 3669
2451 $class->SUPER::new ( 3670 my $self = $class->SUPER::new (
3671 size_w => 32,
3672 size_h => 8,
2452 aspect => 1, 3673 aspect => 1,
3674 can_events => 0,
2453 @_, 3675 @_,
2454 ) 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 }
2455} 3732}
2456 3733
2457sub size_request { 3734sub size_request {
2458 (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
2459} 3768}
2460 3769
2461sub _draw { 3770sub _draw {
2462 my ($self) = @_; 3771 my ($self) = @_;
2463 3772
2464 return unless $::CONN;#d# manage and cache textures differently 3773 $self->SUPER::_draw;
2465 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2466 3774
2467 # TODO animation 3775 if (my $tex = $self->{tex}) {
2468 if ($tex) {
2469 glEnable GL_TEXTURE_2D; 3776 glEnable GL_TEXTURE_2D;
2470 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3777 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2471 glColor 1, 1, 1, 1; 3778 glColor 0, 0, 0, 1;
2472 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3779 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2473 glDisable GL_TEXTURE_2D; 3780 glDisable GL_TEXTURE_2D;
2474 } 3781 }
2475} 3782}
2476 3783
3784sub destroy {
3785 my ($self) = @_;
3786
3787 (delete $self->{timer})->cancel
3788 if $self->{timer};
3789
3790 $self->SUPER::destroy;
3791}
3792
2477############################################################################# 3793#############################################################################
2478 3794
2479package CFClient::UI::InventoryItem; 3795package dc::UI::Buttonbar;
2480 3796
2481our @ISA = CFClient::UI::HBox::; 3797our @ISA = dc::UI::HBox::;
2482 3798
2483sub new { 3799# TODO: should actually wrap buttons and other goodies.
2484 my $class = shift;
2485
2486 my %args = @_;
2487
2488 my $item = delete $args{item};
2489
2490 my $desc = $item->{nrof} < 2
2491 ? $item->{name}
2492 : "$item->{nrof} $item->{name_pl}";
2493
2494
2495 my $self = $class->SUPER::new (
2496 can_hover => 1,
2497 can_events => 1,
2498 tooltip => (CFClient::UI::Label->escape ($desc)
2499 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2500 connect_button_down => sub {
2501 my ($self, $ev, $x, $y) = @_;
2502
2503 # todo: maybe put examine on 1? but should just be a tooltip :(
2504 if ($ev->{button} == 1) {
2505 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2506 } elsif ($ev->{button} == 2) {
2507 $::CONN->send ("apply $item->{tag}");
2508 } elsif ($ev->{button} == 3) {
2509 CFClient::UI::Menu->new (
2510 items => [
2511 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2512 [
2513 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2514 sub { $::CONN->send ("lock $item->{tag}") },
2515 ],
2516 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2517 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2518 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2519 ],
2520 )->popup ($ev);
2521 }
2522
2523 1
2524 },
2525 %args
2526 );
2527
2528 $self->add (new CFClient::UI::Face
2529 can_events => 0,
2530 face => $item->{face},
2531 anim => $item->{anim},
2532 animspeed => $item->{animspeed},
2533 );
2534
2535 $self->add (new CFClient::UI::Label
2536 can_events => 0,
2537 text => $desc,
2538 );
2539
2540 $self
2541}
2542 3800
2543############################################################################# 3801#############################################################################
2544 3802
2545package CFClient::UI::Inventory;
2546
2547our @ISA = CFClient::UI::ScrolledWindow::;
2548
2549sub new {
2550 my $class = shift;
2551
2552 my $self = $class->SUPER::new (
2553 scrolled => (new CFClient::UI::VBox),
2554 @_,
2555 );
2556
2557 $self
2558}
2559
2560sub set_items {
2561 my ($self, $items) = @_;
2562
2563 $self->{scrolled}->clear;
2564 return unless $items;
2565
2566 my @items = sort {
2567 ($a->{type} <=> $b->{type})
2568 or ($a->{name} cmp $b->{name})
2569 } @$items;
2570
2571 $self->{real_items} = \@items;
2572
2573 for my $item (@items) {
2574 my $desc = $item->{nrof} < 2
2575 ? $item->{name}
2576 : "$item->{nrof} $item->{name_pl}";
2577
2578 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2579 }
2580
2581 $self->{scrolled}->add (@items);
2582
2583# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2584}
2585
2586sub size_request {
2587 my ($self) = @_;
2588 ($self->{req_w}, $self->{req_h});
2589}
2590
2591#############################################################################
2592
2593package CFClient::UI::Menu; 3803package dc::UI::Menu;
2594 3804
2595our @ISA = CFClient::UI::FancyFrame::; 3805our @ISA = dc::UI::Toplevel::;
2596 3806
2597use CFClient::OpenGL; 3807use dc::OpenGL;
2598 3808
2599sub new { 3809sub new {
2600 my $class = shift; 3810 my $class = shift;
2601 3811
2602 my $self = $class->SUPER::new ( 3812 my $self = $class->SUPER::new (
2603 items => [], 3813 items => [],
2604 z => 100, 3814 z => 100,
2605 @_, 3815 @_,
2606 ); 3816 );
2607 3817
2608 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3818 $self->add ($self->{vbox} = new dc::UI::VBox);
2609 3819
2610 for my $item (@{ $self->{items} }) { 3820 for my $item (@{ $self->{items} }) {
2611 my ($widget, $cb) = @$item; 3821 my ($widget, $cb, $tooltip) = @$item;
2612 3822
2613 # handle various types of items, only text for now 3823 # handle various types of items, only text for now
2614 if (!ref $widget) { 3824 if (!ref $widget) {
2615 $widget = new CFClient::UI::Label 3825 if ($widget =~ /\t/) {
3826 my ($left, $right) = split /\t/, $widget, 2;
3827
3828 $widget = new dc::UI::HBox
2616 can_hover => 1, 3829 can_hover => 1,
2617 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,
2618 text => $widget; 3842 markup => $widget,
3843 tooltip => $tooltip;
3844 }
2619 } 3845 }
2620 3846
2621 $self->{item}{$widget} = $item; 3847 $self->{item}{$widget} = $item;
2622 3848
2623 $self->{vbox}->add ($widget); 3849 $self->{vbox}->add ($widget);
2635 # maybe save $GRAB? must be careful about events... 3861 # maybe save $GRAB? must be careful about events...
2636 $GRAB = $self; 3862 $GRAB = $self;
2637 $self->{button} = $ev->{button}; 3863 $self->{button} = $ev->{button};
2638 3864
2639 $self->show; 3865 $self->show;
2640 $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);
2641} 3867}
2642 3868
2643sub mouse_motion { 3869sub invoke_mouse_motion {
2644 my ($self, $ev, $x, $y) = @_; 3870 my ($self, $ev, $x, $y) = @_;
2645 3871
2646 # TODO: should use vbox->find_widget or so 3872 # TODO: should use vbox->find_widget or so
2647 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 3873 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2648 $self->{hover} = $self->{item}{$HOVER}; 3874 $self->{hover} = $self->{item}{$HOVER};
2649}
2650 3875
3876 0
3877}
3878
2651sub button_up { 3879sub invoke_button_up {
2652 my ($self, $ev, $x, $y) = @_; 3880 my ($self, $ev, $x, $y) = @_;
2653 3881
2654 if ($ev->{button} == $self->{button}) { 3882 if ($ev->{button} == $self->{button}) {
2655 undef $GRAB; 3883 undef $GRAB;
2656 $self->hide; 3884 $self->hide;
2657 3885
2658 $self->emit ("popdown"); 3886 $self->emit ("popdown");
2659 $self->{hover}[1]->() if $self->{hover}; 3887 $self->{hover}[1]->() if $self->{hover};
3888 } else {
3889 return 0
3890 }
3891
2660 } 3892 1
2661} 3893}
2662 3894
2663############################################################################# 3895#############################################################################
2664 3896
2665package CFClient::UI::Statusbox; 3897package dc::UI::Multiplexer;
2666 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
2667our @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}
2668 4170
2669sub reorder { 4171sub reorder {
2670 my ($self) = @_; 4172 my ($self) = @_;
2671 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} };
2672 4179
2673 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
2674 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2675 } 4182 }
4183
4184 $self->{timer}->set (1, 1);
2676 4185
2677 my @widgets; 4186 my @widgets;
2678 4187
2679 my @items = sort { 4188 my @items = sort {
2680 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
2683 4192
2684 my $count = 10 + 1; 4193 my $count = 10 + 1;
2685 for my $item (@items) { 4194 for my $item (@items) {
2686 last unless --$count; 4195 last unless --$count;
2687 4196
2688 push @widgets, $item->{label} ||= do { 4197 my $label = $item->{label} ||= do {
2689 # TODO: doesn't handle markup well (read as: at all) 4198 # TODO: doesn't handle markup well (read as: at all)
2690 my $short = $item->{count} > 1 4199 my $short = $item->{count} > 1
2691 ? "<b>$item->{count} ×</b> $item->{text}" 4200 ? "<b>$item->{count} ×</b> $item->{text}"
2692 : $item->{text}; 4201 : $item->{text};
2693 4202
2694 for ($short) { 4203 for ($short) {
2695 s/^\s+//; 4204 s/^\s+//;
2696 s/\012.*/…/s; 4205 s/\s+/ /g;
2697 my $len = int 40 / $item->{fontsize};
2698 substr $_, $len, length, "…" if $len < length;
2699 } 4206 }
2700 4207
2701 new CFClient::UI::Label 4208 new dc::UI::Label
2702 markup => $short, 4209 markup => $short,
2703 tooltip => $item->{tooltip}, 4210 tooltip => $item->{tooltip},
2704 tooltip_font => $::FONT_PROP, 4211 tooltip_font => $::FONT_PROP,
2705 tooltip_width => 0.67, 4212 tooltip_width => 0.67,
2706 fontsize => $item->{fontsize}, 4213 fontsize => $item->{fontsize} || $self->{fontsize},
2707 color => $item->{color}, 4214 max_w => $::WIDTH * 0.44,
4215 fg => [@{ $item->{fg} }],
2708 can_events => 1, 4216 can_events => 1,
2709 can_hover => 1 4217 can_hover => 1
2710 }; 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;
2711 } 4231 }
2712 4232
2713 $self->clear; 4233 $self->clear;
2714 $self->SUPER::add (reverse @widgets); 4234 $self->SUPER::add (reverse @widgets);
2715} 4235}
2718 my ($self, $text, %arg) = @_; 4238 my ($self, $text, %arg) = @_;
2719 4239
2720 $text =~ s/^\s+//; 4240 $text =~ s/^\s+//;
2721 $text =~ s/\s+$//; 4241 $text =~ s/\s+$//;
2722 4242
4243 return unless $text;
4244
2723 my $timeout = time + ((delete $arg{timeout}) || 60); 4245 my $timeout = (int time) + ((delete $arg{timeout}) || 60);
2724 4246
2725 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 4247 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2726 4248
2727 if (my $item = $self->{item}{$group}) { 4249 if (my $item = $self->{item}{$group}) {
2728 if ($item->{text} eq $text) { 4250 if ($item->{text} eq $text) {
2729 $item->{count}++; 4251 $item->{count}++;
2730 } else { 4252 } else {
2731 $item->{count} = 1; 4253 $item->{count} = 1;
2732 $item->{text} = $item->{tooltip} = $text; 4254 $item->{text} = $item->{tooltip} = $text;
2733 } 4255 }
2734 $item->{id} = ++$self->{id}; 4256 $item->{id} += 0.2;#d#
2735 $item->{timeout} = $timeout; 4257 $item->{timeout} = $timeout;
2736 delete $item->{label}; 4258 delete $item->{label};
2737 } else { 4259 } else {
2738 $self->{item}{$group} = { 4260 $self->{item}{$group} = {
2739 id => ++$self->{id}, 4261 id => ++$self->{id},
2740 text => $text, 4262 text => $text,
2741 timeout => $timeout, 4263 timeout => $timeout,
2742 tooltip => $text, 4264 tooltip => $text,
2743 fontsize => 0.8,
2744 color => [0.8, 0.8, 0.8, 0.8], 4265 fg => [0.8, 0.8, 0.8, 0.8],
2745 pri => 0, 4266 pri => 0,
2746 count => 1, 4267 count => 1,
2747 %arg, 4268 %arg,
2748 }; 4269 };
2749 } 4270 }
2750 4271
4272 $ROOT->on_refresh (reorder => sub {
4273 $self->reorder;
4274 });
4275}
4276
4277sub reconfigure {
4278 my ($self) = @_;
4279
4280 delete $_->{label}
4281 for values %{ $self->{item} || {} };
4282
2751 $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;
2752} 4293}
2753 4294
2754############################################################################# 4295#############################################################################
2755 4296
2756package CFClient::UI::Root; 4297package dc::UI::Root;
2757 4298
2758our @ISA = CFClient::UI::Container::; 4299our @ISA = dc::UI::Container::;
2759 4300
2760use CFClient::OpenGL; 4301use List::Util qw(min max);
4302
4303use dc::OpenGL;
2761 4304
2762sub new { 4305sub new {
2763 my $class = shift; 4306 my $class = shift;
2764 4307
2765 $class->SUPER::new ( 4308 my $self = $class->SUPER::new (
4309 visible => 1,
2766 @_, 4310 @_,
2767 ) 4311 );
2768}
2769 4312
2770sub configure { 4313 dc::weaken ($self->{root} = $self);
2771 my ($self, $x, $y, $w, $h) = @_;
2772 4314
2773 $self->{w} = $w; 4315 $self
2774 $self->{h} = $h;
2775}
2776
2777sub check_size {
2778 my ($self) = @_;
2779
2780 $self->size_allocate ($self->{w}, $self->{h})
2781 if $self->{w};
2782} 4316}
2783 4317
2784sub size_request { 4318sub size_request {
2785 my ($self) = @_; 4319 my ($self) = @_;
2786 4320
2787 ($self->{w}, $self->{h}) 4321 ($self->{w}, $self->{h})
2788} 4322}
2789 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
2790sub size_allocate { 4338sub invoke_size_allocate {
2791 my ($self, $w, $h) = @_; 4339 my ($self, $w, $h) = @_;
2792
2793 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2794 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2795
2796 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2797 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2798 4340
2799 for my $child ($self->children) { 4341 for my $child ($self->children) {
2800 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)};
2801 4343
2802 $X = List::Util::max 0, List::Util::min $w - $W, $X; 4344 $X = $child->{force_x} if exists $child->{force_x};
2803 $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
2804 $child->configure ($X, $Y, $W, $H); 4350 $child->configure ($X, $Y, $W, $H);
2805 } 4351 }
4352
4353 1
2806} 4354}
2807 4355
2808sub coord2local { 4356sub coord2local {
2809 my ($self, $x, $y) = @_; 4357 my ($self, $x, $y) = @_;
2810 4358
2818} 4366}
2819 4367
2820sub update { 4368sub update {
2821 my ($self) = @_; 4369 my ($self) = @_;
2822 4370
2823 $self->check_size;
2824 $::WANT_REFRESH++; 4371 $::WANT_REFRESH = 1;
2825} 4372}
2826 4373
2827sub add { 4374sub add {
2828 my ($self, $child) = @_; 4375 my ($self, @children) = @_;
2829 4376
2830 # integerise window positions 4377 $_->{is_toplevel} = 1
2831 $child->{x} = int $child->{x}; 4378 for @children;
2832 $child->{y} = int $child->{y};
2833 4379
2834 $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 }
2835} 4396}
2836 4397
2837sub on_refresh { 4398sub on_refresh {
2838 my ($self, $id, $cb) = @_; 4399 my ($self, $id, $cb) = @_;
2839 4400
2852 while ($self->{refresh_hook}) { 4413 while ($self->{refresh_hook}) {
2853 $_->() 4414 $_->()
2854 for values %{delete $self->{refresh_hook}}; 4415 for values %{delete $self->{refresh_hook}};
2855 } 4416 }
2856 4417
2857 if ($self->{check_size}) { 4418 while ($self->{realloc}) {
2858 my @queue = ([], []); 4419 my %queue;
4420 my @queue;
4421 my $widget;
2859 4422
2860 for (;;) { 4423 outer:
2861 if ($self->{check_size}) { 4424 while () {
2862 # heuristic: check containers last 4425 if (my $realloc = delete $self->{realloc}) {
2863 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 4426 for $widget (values %$realloc) {
2864 for values %{delete $self->{check_size}} 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 }
2865 } 4433 }
2866 4434
2867 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 4435 while () {
4436 @queue or last outer;
2868 4437
2869 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 4438 $widget = pop @{ $queue[-1] || [] }
2870 ? @$widget{qw(user_w user_h)} 4439 and last;
2871 : $widget->size_request;
2872
2873 if (delete $widget->{force_alloc}
2874 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2875 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2876 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}) {
2877 $widget->{req_w} = $w; 4459 $widget->{req_w} = $w;
2878 $widget->{req_h} = $h; 4460 $widget->{req_h} = $h;
2879 4461
2880 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 4462 $self->{size_alloc}{$widget+0} = $widget;
2881 4463
2882 $widget->{parent}->check_size
2883 if $widget->{parent}; 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 }
2884 } 4471 }
4472
4473 delete $self->{realloc}{$widget+0};
2885 } 4474 }
2886 }
2887 4475
2888 while ($self->{size_alloc}) { 4476 while (my $size_alloc = delete $self->{size_alloc}) {
2889 for (values %{delete $self->{size_alloc}}) { 4477 my @queue = sort { $a->{visible} <=> $b->{visible} }
2890 my ($widget, $w, $h) = @$_; 4478 values %$size_alloc;
2891 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
2892 $w = 0 if $w < 0; 4494 $w = int $w + 0.5;
2893 $h = 0 if $h < 0; 4495 $h = int $h + 0.5;
2894 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
2895 $widget->{w} = $w; 4501 $widget->{w} = $w;
2896 $widget->{h} = $h; 4502 $widget->{h} = $h;
2897 $widget->size_allocate ($w, $h); 4503
2898 $widget->emit (size_allocate => $w, $h); 4504 $widget->emit (size_allocate => $w, $h);
4505 }
4506 }
2899 } 4507 }
2900 } 4508 }
2901 4509
2902 while ($self->{post_alloc_hook}) { 4510 while ($self->{post_alloc_hook}) {
2903 $_->() 4511 $_->()
2908 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4516 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2909 glClear GL_COLOR_BUFFER_BIT; 4517 glClear GL_COLOR_BUFFER_BIT;
2910 4518
2911 glMatrixMode GL_PROJECTION; 4519 glMatrixMode GL_PROJECTION;
2912 glLoadIdentity; 4520 glLoadIdentity;
2913 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2914 glMatrixMode GL_MODELVIEW; 4522 glMatrixMode GL_MODELVIEW;
2915 glLoadIdentity; 4523 glLoadIdentity;
2916 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
2917 $self->_draw; 4531 $self->_draw;
4532 }
2918} 4533}
2919 4534
2920############################################################################# 4535#############################################################################
2921 4536
2922package CFClient::UI; 4537package dc::UI;
2923 4538
2924$ROOT = new CFClient::UI::Root; 4539$ROOT = new dc::UI::Root;
2925$TOOLTIP = new CFClient::UI::Tooltip; 4540$TOOLTIP = new dc::UI::Tooltip z => 900;
2926 4541
29271 45421
2928 4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines