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.161 by root, Mon Apr 24 03:43:51 2006 UTC vs.
Revision 1.476 by root, Sun Jan 11 23:45:52 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines