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.221 by root, Mon May 22 03:48:50 2006 UTC vs.
Revision 1.436 by root, Wed Aug 22 21:40:57 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines