ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines