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.172 by root, Tue Apr 25 09:52:04 2006 UTC vs.
Revision 1.405 by root, Sat Jul 21 23:07:57 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines