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.91 by root, Wed Apr 12 23:15:39 2006 UTC vs.
Revision 1.172 by root, Tue Apr 25 09:52:04 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (length $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_markup ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
45 if $FOCUS;
18} 46}
19 47
20sub feed_sdl_key_up_event { 48sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS; 49 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
50 if $FOCUS;
22} 51}
23 52
24sub feed_sdl_button_down_event { 53sub feed_sdl_button_down_event {
25 my ($ev) = @_; 54 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 55 my ($x, $y) = ($ev->{x}, $ev->{y});
27 56
28 if (!$BUTTON_STATE) { 57 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 58 my $widget = $ROOT->find_widget ($x, $y);
30 59
31 $GRAB = $widget; 60 $GRAB = $widget;
32 $GRAB->update if $GRAB; 61 $GRAB->update if $GRAB;
33 }
34 62
63 check_tooltip;
64 }
65
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 66 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 67
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 68 if ($GRAB) {
69 ($x, $y) = $GRAB->coord2local ($x, $y);
70 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
71 }
38} 72}
39 73
40sub feed_sdl_button_up_event { 74sub feed_sdl_button_up_event {
41 my ($ev) = @_; 75 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 76 my ($x, $y) = ($ev->{x}, $ev->{y});
43 77
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 78 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 79
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 80 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 81
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 82 if ($GRAB) {
83 ($x, $y) = $GRAB->coord2local ($x, $y);
84 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
85 }
49 86
50 if (!$BUTTON_STATE) { 87 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 88 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 89 $grab->update if $grab;
53 $GRAB->update if $GRAB; 90 $GRAB->update if $GRAB;
91
92 check_tooltip;
54 } 93 }
55} 94}
56 95
57sub feed_sdl_motion_event { 96sub feed_sdl_motion_event {
58 my ($ev) = @_; 97 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 98 my ($x, $y) = ($ev->{x}, $ev->{y});
60 99
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 100 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 101
63 if ($widget != $HOVER) { 102 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 103 my $hover = $HOVER; $HOVER = $widget;
65 104
66 $hover->update if $hover; 105 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 106 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 107
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 108 check_tooltip;
109 }
110
111 if ($HOVER) {
112 ($x, $y) = $HOVER->coord2local ($x, $y);
113 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
114 }
115}
116
117# convert position array to integers
118sub harmonize {
119 my ($vals) = @_;
120
121 my $rem = 0;
122
123 for (@$vals) {
124 my $i = int $_ + $rem;
125 $rem += $_ - $i;
126 $_ = $i;
127 }
71} 128}
72 129
73############################################################################# 130#############################################################################
74 131
75package CFClient::UI::Base; 132package CFClient::UI::Base;
76 133
77use strict; 134use strict;
78 135
79use SDL::OpenGL; 136use CFClient::OpenGL;
80 137
81sub new { 138sub new {
82 my $class = shift; 139 my $class = shift;
83 140
84 my $self = bless { 141 my $self = bless {
85 x => 0, 142 x => 0,
86 y => 0, 143 y => 0,
87 z => 0, 144 z => 0,
88 w => -1, 145 can_events => 1,
89 h => -1,
90 @_ 146 @_
91 }, $class; 147 }, $class;
92 148
93 for (keys %$self) { 149 for (keys %$self) {
94 if (/^connect_(.*)$/) { 150 if (/^connect_(.*)$/) {
97 } 153 }
98 154
99 $self 155 $self
100} 156}
101 157
158sub destroy {
159 my ($self) = @_;
160
161 $self->hide;
162 %$self = ();
163}
164
165sub show {
166 my ($self) = @_;
167
168 return if $self->{parent};
169
170 $CFClient::UI::ROOT->add ($self);
171}
172
173sub hide {
174 my ($self) = @_;
175
176 undef $GRAB if $GRAB == $self;
177 undef $HOVER if $HOVER == $self;
178
179 $self->{parent}->remove ($self)
180 if $self->{parent};
181}
182
102sub move { 183sub move {
103 my ($self, $x, $y, $z) = @_; 184 my ($self, $x, $y, $z) = @_;
185
104 $self->{x} = $x; 186 $self->{x} = int $x;
105 $self->{y} = $y; 187 $self->{y} = int $y;
106 $self->{z} = $z if defined $z; 188 $self->{z} = $z if defined $z;
107}
108 189
109sub needs_redraw { 190 $self->update;
110 0 191}
192
193sub set_size {
194 my ($self, $w, $h) = @_;
195
196 $self->{user_w} = $w;
197 $self->{user_h} = $h;
198
199 $self->check_size;
111} 200}
112 201
113sub size_request { 202sub size_request {
114 require Carp; 203 require Carp;
115 Carp::confess "size_request is abtract"; 204 Carp::confess "size_request is abstract";
116} 205}
117 206
118sub _size_allocate { 207sub configure {
119 my ($self, $x, $y, $w, $h) = @_; 208 my ($self, $x, $y, $w, $h) = @_;
120 209
210 if ($self->{aspect}) {
211 my $w2 = List::Util::min $w, int $h * $self->{aspect};
212 my $h2 = List::Util::min $h, int $w / $self->{aspect};
213
214 # use alignment to adjust x, y
215
216 $x += int +($w - $w2) * 0.5;
217 $y += int +($h - $h2) * 0.5;
218
219 ($w, $h) = ($w2, $h2);
220 }
221
222 if ($self->{x} != $x || $self->{y} != $y) {
121 $self->{x} = $x; 223 $self->{x} = $x;
122 $self->{y} = $y; 224 $self->{y} = $y;
225 $self->update;
226 }
123 227
124 return unless $self->{w} != $w || $self->{h} != $h; 228 if ($self->{w} != $w || $self->{h} != $h) {
125
126 $self->{w} = $w; 229 $self->{w} = $w;
127 $self->{h} = $h; 230 $self->{h} = $h;
128 231
232 $self->size_allocate ($w, $h);
233 $self->update;
129 1 234 }
130} 235}
131 236
132sub size_allocate { 237sub size_allocate {
238 # nothing to be done
239}
240
241sub children {
242}
243
244sub set_max_size {
133 my ($self, $x, $y, $w, $h) = @_; 245 my ($self, $w, $h) = @_;
134 246
135 $self->_size_allocate ($x, $y, $w, $h); 247 delete $self->{max_w}; $self->{max_w} = $w if $w;
248 delete $self->{max_h}; $self->{max_h} = $h if $h;
249}
250
251# return top left coordinates
252sub _topleft {
253 my ($self, $x, $y) = @_;
254
255 $self->{parent}
256 or Carp::confess "no parent widget in _topleft\n";#d#
257
258 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 259}
137 260
138# translate global coordinates to local coordinate system 261# translate global coordinates to local coordinate system
139sub translate { 262sub coord2local {
140 my ($self, $x, $y) = @_; 263 my ($self, $x, $y) = @_;
141 264
142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 265 my ($X, $Y) = $self->_topleft;
266 ($x - $X, $y - $Y)
267}
268
269# translate local coordinates to global coordinate system
270sub coord2global {
271 my ($self, $x, $y) = @_;
272
273 my ($X, $Y) = $self->_topleft;
274 ($x + $X, $y + $Y)
143} 275}
144 276
145sub focus_in { 277sub focus_in {
146 my ($self) = @_; 278 my ($self) = @_;
147 279
148 return if $FOCUS == $self; 280 return if $FOCUS == $self;
281 return unless $self->{can_focus};
149 282
150 my $focus = $FOCUS; $FOCUS = $self; 283 my $focus = $FOCUS; $FOCUS = $self;
284
285 $self->emit (focus_in => $focus);
286
151 $focus->update if $focus; 287 $focus->update if $focus;
152 $FOCUS->update; 288 $FOCUS->update;
153} 289}
154 290
155sub focus_out { 291sub focus_out {
156 my ($self) = @_; 292 my ($self) = @_;
157 293
158 return unless $FOCUS == $self; 294 return unless $FOCUS == $self;
159 295
160 my $focus = $FOCUS; undef $FOCUS; 296 my $focus = $FOCUS; undef $FOCUS;
297
298 $self->emit (focus_out => $focus);
299
161 $focus->update if $focus; #? 300 $focus->update if $focus; #?
162} 301}
163 302
164sub mouse_motion { } 303sub mouse_motion { }
165sub button_up { } 304sub button_up { }
186 glPushMatrix; 325 glPushMatrix;
187 glTranslate $self->{x}, $self->{y}, 0; 326 glTranslate $self->{x}, $self->{y}, 0;
188 $self->_draw; 327 $self->_draw;
189 glPopMatrix; 328 glPopMatrix;
190 329
191 if ($self == $HOVER) { 330 if ($self == $HOVER && $self->{can_hover}) {
192 my ($x, $y) = @$self{qw(x y)}; 331 my ($x, $y) = @$self{qw(x y)};
193 332
194 glColor 0, 0, 1, 0.2; 333 glColor 1, 0.8, 0.5, 0.2;
195 glEnable GL_BLEND; 334 glEnable GL_BLEND;
196 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 335 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
197 glBegin GL_QUADS; 336 glBegin GL_QUADS;
198 glVertex $x , $y; 337 glVertex $x , $y;
199 glVertex $x + $self->{w}, $y; 338 glVertex $x + $self->{w}, $y;
200 glVertex $x + $self->{w}, $y + $self->{h}; 339 glVertex $x + $self->{w}, $y + $self->{h};
201 glVertex $x , $y + $self->{h}; 340 glVertex $x , $y + $self->{h};
202 glEnd; 341 glEnd;
203 glDisable GL_BLEND; 342 glDisable GL_BLEND;
204 } 343 }
344
345 if ($ENV{PCLIENT_DEBUG}) {
346 glPushMatrix;
347 glColor 1, 1, 0, 1;
348 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
349 glBegin GL_LINE_LOOP;
350 glVertex 0 , 0;
351 glVertex $self->{w}, 0;
352 glVertex $self->{w}, $self->{h};
353 glVertex 0 , $self->{h};
354 glEnd;
355 glPopMatrix;
356 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
357 }
205} 358}
206 359
207sub _draw { 360sub _draw {
208 my ($self) = @_; 361 my ($self) = @_;
209 362
210 warn "no draw defined for $self\n"; 363 warn "no draw defined for $self\n";
211} 364}
212 365
213sub find_widget { 366sub find_widget {
214 my ($self, $x, $y) = @_; 367 my ($self, $x, $y) = @_;
368
369 return () unless $self->{can_events};
215 370
216 return $self 371 return $self
217 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 372 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
218 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 373 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
219 374
220 () 375 ()
221} 376}
222 377
223sub set_parent { 378sub set_parent {
224 my ($self, $par) = @_; 379 my ($self, $parent) = @_;
225 380
226 $self->{parent} = $par;
227 Scalar::Util::weaken $self->{parent}; 381 Scalar::Util::weaken ($self->{parent} = $parent);
228} 382}
229 383
230sub get_parent { 384sub check_size {
385 my ($self) = @_;
386
231 $_[0]->{parent} 387 $self->{parent}
388 or return 1;
389
390 my ($w, $h) = $self->{user_w} && $self->{user_h}
391 ? @$self{qw(user_w user_h)}
392 : $self->size_request;
393
394 if ($w != $self->{req_w} || $h != $self->{req_h}) {
395 $self->{req_w} = $w;
396 $self->{req_h} = $h;
397
398 $self->{parent}->check_size
399 or $self->size_allocate (
400 (List::Util::max $self->{w}, $w),
401 (List::Util::max $self->{h}, $h),
402 );
403
404 1
405 } else {
406 0
407 }
232} 408}
233 409
234sub update { 410sub update {
235 my ($self) = @_; 411 my ($self) = @_;
236 412
239} 415}
240 416
241sub connect { 417sub connect {
242 my ($self, $signal, $cb) = @_; 418 my ($self, $signal, $cb) = @_;
243 419
244 push @{ $self->{cb}{$signal} }, $cb; 420 push @{ $self->{signal_cb}{$signal} }, $cb;
245} 421}
246 422
247sub emit { 423sub emit {
248 my ($self, $signal, @args) = @_; 424 my ($self, $signal, @args) = @_;
249 425
250 $_->($self, @args) 426 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
251 for @{$self->{cb}{$signal} || []};
252} 427}
253 428
254sub DESTROY { 429sub DESTROY {
255 my ($self) = @_; 430 my ($self) = @_;
256 431
262package CFClient::UI::DrawBG; 437package CFClient::UI::DrawBG;
263 438
264our @ISA = CFClient::UI::Base::; 439our @ISA = CFClient::UI::Base::;
265 440
266use strict; 441use strict;
267use SDL::OpenGL; 442use CFClient::OpenGL;
268 443
269sub new { 444sub new {
270 my $class = shift; 445 my $class = shift;
271 446
272 # range [value, low, high, page] 447 # range [value, low, high, page]
301 476
302package CFClient::UI::Empty; 477package CFClient::UI::Empty;
303 478
304our @ISA = CFClient::UI::Base::; 479our @ISA = CFClient::UI::Base::;
305 480
481sub new {
482 my ($class, %arg) = @_;
483 $class->SUPER::new (can_events => 0, %arg);
484}
485
306sub size_request { 486sub size_request {
307 (0, 0) 487 (0, 0)
308} 488}
309 489
310sub draw { } 490sub draw { }
318sub new { 498sub new {
319 my ($class, %arg) = @_; 499 my ($class, %arg) = @_;
320 500
321 my $children = delete $arg{children} || []; 501 my $children = delete $arg{children} || [];
322 502
323 my $self = $class->SUPER::new (children => [], %arg); 503 my $self = $class->SUPER::new (
504 children => [],
505 can_events => 0,
506 %arg,
507 );
324 $self->add ($_) for @$children; 508 $self->add ($_) for @$children;
325 509
326 $self 510 $self
327} 511}
328 512
329sub add { 513sub add {
330 my ($self, $chld) = @_; 514 my ($self, $child) = @_;
331 515
332 $chld->set_parent ($self); 516 $child->set_parent ($self);
517
518 use sort 'stable';
333 519
334 $self->{children} = [ 520 $self->{children} = [
335 sort { $a->{z} <=> $b->{z} } 521 sort { $a->{z} <=> $b->{z} }
336 @{$self->{children}}, $chld 522 @{$self->{children}}, $child
337 ]; 523 ];
338 524
339 $self->{w} = $self->{h} = -1; 525 $child->check_size;
526}
527
528sub children {
529 @{ $_[0]{children} }
530}
531
532sub remove {
533 my ($self, $child) = @_;
534
535 delete $child->{parent};
536 $child->hide;
537
538 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
539
540 $self->check_size;
340 $self->update; 541 $self->update;
341} 542}
342 543
343sub remove { 544sub clear {
344 my ($self, $widget) = @_; 545 my ($self) = @_;
345 546
346 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 547 my $children = delete $self->{children};
548 $self->{children} = [];
347 549
348 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 550 for (@$children) {
551 delete $_->{parent};
552 $_->hide;
553 }
349} 554}
350 555
351sub find_widget { 556sub find_widget {
352 my ($self, $x, $y) = @_; 557 my ($self, $x, $y) = @_;
353 558
383 588
384 $class->SUPER::new (children => [$child], %arg) 589 $class->SUPER::new (children => [$child], %arg)
385} 590}
386 591
387sub add { 592sub add {
388 my ($self, $widget) = @_; 593 my ($self, $child) = @_;
389 594
390 $self->{children} = []; 595 $self->{children} = [];
391 596
392 $self->SUPER::add ($widget); 597 $self->SUPER::add ($child);
393} 598}
394 599
395sub remove { 600sub remove {
396 my ($self, $widget) = @_; 601 my ($self, $widget) = @_;
397 602
406sub size_request { 611sub size_request {
407 $_[0]{children}[0]->size_request 612 $_[0]{children}[0]->size_request
408} 613}
409 614
410sub size_allocate { 615sub size_allocate {
411 my ($self, $x, $y, $w, $h) = @_; 616 my ($self, $w, $h) = @_;
412 617
413 $self->_size_allocate ($x, $y, $w, $h) or return;
414
415 $self->{children}[0]->size_allocate (0, 0, $w, $h); 618 $self->{children}[0]->configure (0, 0, $w, $h);
416} 619}
417 620
418############################################################################# 621#############################################################################
419 622
420package CFClient::UI::Window; 623package CFClient::UI::Window;
421 624
422our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
423 626
424use SDL::OpenGL; 627use CFClient::OpenGL;
425 628
426sub new { 629sub new {
427 my ($class, %arg) = @_; 630 my ($class, %arg) = @_;
428 631
429 my $self = $class->SUPER::new (%arg); 632 my $self = $class->SUPER::new (%arg);
438} 641}
439 642
440sub render_chld { 643sub render_chld {
441 my ($self) = @_; 644 my ($self) = @_;
442 645
443 $self->{texture} = 646 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
444 CFClient::Texture->new_from_opengl ( 647 glClearColor 0, 0, 0, 1;
445 $self->{w}, $self->{h}, sub { $self->child->draw } 648 glClear GL_COLOR_BUFFER_BIT;
446 ); 649 $self->child->draw;
650 };
447} 651}
448 652
449sub size_allocate { 653sub size_allocate {
450 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
451 655
452 $self->_size_allocate ($x, $y, $w, $h) or return;
453
454 $self->child->size_allocate (0, 0, $w, $h); 656 $self->child->configure (0, 0, $w, $h);
455 657
456 $self->render_chld; 658 $self->render_chld;
457} 659}
458 660
459sub _draw { 661sub _draw {
475 glDisable GL_TEXTURE_2D; 677 glDisable GL_TEXTURE_2D;
476} 678}
477 679
478############################################################################# 680#############################################################################
479 681
682package CFClient::UI::ViewPort;
683
684our @ISA = CFClient::UI::Window::;
685
686sub new { die }
687
688sub size_request {
689 my ($self) = @_;
690
691 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
692 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
693
694 @$self{qw(child_w child_h)}
695}
696
697sub _draw {
698 my ($self) = @_;
699
700 $self->{children}[1]->draw;
701}
702
703
704#############################################################################
705
480package CFClient::UI::Frame; 706package CFClient::UI::Frame;
481 707
482our @ISA = CFClient::UI::Bin::; 708our @ISA = CFClient::UI::Bin::;
483 709
484use SDL::OpenGL; 710use CFClient::OpenGL;
485 711
486sub size_request { 712sub new {
487 my ($self) = @_; 713 my $class = shift;
488 my $chld = $self->child
489 or return (0, 0);
490 714
491 $chld->move (2, 2); 715 my $self = $class->SUPER::new (
492 716 bg => [1, 1, 1, 1],
493 map { $_ + 4 } $chld->size_request; 717 border_bg => [1, 1, 1, 1],
494} 718 border => 0.8,
495 719 @_
496sub size_allocate {
497 my ($self, $x, $y, $w, $h) = @_;
498 720 );
499 $self->_size_allocate ($x, $y, $w, $h) or return;
500 721
501 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 722 $self
502} 723}
503 724
504sub _draw { 725sub _draw {
505 my ($self) = @_; 726 my ($self) = @_;
506 727
507 my $chld = $self->child;
508
509 my ($w, $h) = $chld->size_request;
510
511 glBegin GL_QUADS;
512 glColor 0, 0, 0;
513 glVertex 0 , 0;
514 glVertex 0 , $h + 4;
515 glVertex $w + 4 , $h + 4;
516 glVertex $w + 4 , 0;
517 glEnd;
518
519 $chld->draw;
520}
521
522#############################################################################
523
524package CFClient::UI::FancyFrame;
525
526our @ISA = CFClient::UI::Bin::;
527
528use SDL::OpenGL;
529
530my @tex =
531 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
532 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
533
534sub size_request {
535 my ($self) = @_;
536
537 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
538
539 my ($w, $h) = $self->SUPER::size_request;
540
541 $h += $tex[1]->{h};
542 $h += $tex[4]->{h};
543 $w += $tex[2]->{w};
544 $w += $tex[3]->{w};
545
546 ($w, $h)
547}
548
549sub size_allocate {
550 my ($self, $x, $y, $w, $h) = @_;
551
552 $self->_size_allocate ($x, $y, $w, $h) or return;
553
554 $h -= $tex[1]{h};
555 $h -= $tex[4]{h};
556 $w -= $tex[2]{w};
557 $w -= $tex[3]{w};
558
559 $h = $h < 0 ? 0 : $h;
560 $w = $w < 0 ? 0 : $w;
561
562 my $child = $self->child;
563
564 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
565}
566
567sub button_down {
568 my ($self, $ev, $x, $y) = @_;
569
570 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
571 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
572
573 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
574 my ($bw, $bh) = ($self->{w}, $self->{h});
575
576 $self->{motion} = sub {
577 my ($ev, $x, $y) = @_;
578
579 ($x, $y) = ($ev->motion_x, $ev->motion_y);
580
581 $self->{user_w} = $bw + $x - $ox;
582 $self->{user_h} = $bh + $y - $oy;
583 $self->update;
584 };
585
586 } elsif ($x >= 0 && $x < $self->{w}
587 && $y >= 0 && $y < $tex[1]{h}) {
588
589 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
590 my ($bx, $by) = ($self->{x}, $self->{y});
591
592 $self->{motion} = sub {
593 my ($ev, $x, $y) = @_;
594
595 ($x, $y) = ($ev->motion_x, $ev->motion_y);
596
597 $self->move ($bx + $x - $ox, $by + $y - $oy);
598 $self->update;
599 };
600 }
601}
602
603sub button_up {
604 my ($self, $ev, $x, $y) = @_;
605
606 delete $self->{motion};
607}
608
609sub mouse_motion {
610 my ($self, $ev, $x, $y) = @_;
611
612 $self->{motion}->($ev, $x, $y) if $self->{motion};
613}
614
615sub _draw {
616 my ($self) = @_;
617
618 my ($w, $h) = ($self->{w}, $self->{h}); 728 my ($w, $h) = ($self->{w}, $self->{h});
619 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
620 729
621 glEnable GL_BLEND; 730 glEnable GL_BLEND;
622 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 731 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
623 glEnable GL_TEXTURE_2D; 732 glEnable GL_TEXTURE_2D;
624 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 733 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
625 734
626 my $top = $tex[1]; 735# glBegin GL_QUADS;
736# glColor 0, 0, 0, 0;
737# glVertex 0 , 0;
738# glVertex 0 , $h;
739# glVertex $w, $h;
740# glVertex $w, 0;
741# glEnd;
742
743
744 $self->child->draw;
745 glDisable GL_BLEND;
746 glDisable GL_TEXTURE_2D;
747}
748
749#############################################################################
750
751package CFClient::UI::FancyFrame;
752
753our @ISA = CFClient::UI::Bin::;
754
755use CFClient::OpenGL;
756
757my @tex =
758 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
759 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
760
761sub new {
762 my $class = shift;
763
764 # TODO: user_x, user_y, overwrite moveto?
765
766 my $self = $class->SUPER::new (
767 bg => [1, 1, 1, 1],
768 border_bg => [1, 1, 1, 1],
769 border => 0.8,
770 can_events => 1,
771 @_
772 );
773
774 $self->{title} &&= new CFClient::UI::Label
775 align => 0,
776 valign => 1,
777 text => $self->{title},
778 fontsize => 1;
779
780 $self
781}
782
783sub border {
784 int $_[0]{border} * $::FONTSIZE
785}
786
787sub size_request {
788 my ($self) = @_;
789
790 my ($w, $h) = $self->SUPER::size_request;
791
792 (
793 $w + $self->border * 2,
794 $h + $self->border * 2,
795 )
796}
797
798sub size_allocate {
799 my ($self, $w, $h) = @_;
800
801 $h -= List::Util::max 0, $self->border * 2;
802 $w -= List::Util::max 0, $self->border * 2;
803
804 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
805 if $self->{title};
806
807 $self->child->configure ($self->border, $self->border, $w, $h);
808}
809
810sub button_down {
811 my ($self, $ev, $x, $y) = @_;
812
813 my $border = $self->border;
814
815 if ($x < $self->{w} && $x >= $self->{w} - $border
816 && $y < $self->{h} && $y >= $self->{h} - $border) {
817
818 my ($ox, $oy) = ($ev->{x}, $ev->{y});
819 my ($bw, $bh) = ($self->{w}, $self->{h});
820
821 $self->{motion} = sub {
822 my ($ev, $x, $y) = @_;
823
824 ($x, $y) = ($ev->{x}, $ev->{y});
825
826 $self->{user_w} = $bw + $x - $ox;
827 $self->{user_h} = $bh + $y - $oy;
828 $self->check_size;
829 };
830
831 } elsif ($x >= 0 && $x < $self->{w}
832 && $y >= 0 && $y < $border) {
833
834 my ($ox, $oy) = ($ev->{x}, $ev->{y});
835 my ($bx, $by) = ($self->{x}, $self->{y});
836
837 $self->{motion} = sub {
838 my ($ev, $x, $y) = @_;
839
840 ($x, $y) = ($ev->{x}, $ev->{y});
841
842 $self->move ($bx + $x - $ox, $by + $y - $oy);
843 $self->update;
844 };
845 }
846}
847
848sub button_up {
849 my ($self, $ev, $x, $y) = @_;
850
851 delete $self->{motion};
852}
853
854sub mouse_motion {
855 my ($self, $ev, $x, $y) = @_;
856
857 $self->{motion}->($ev, $x, $y) if $self->{motion};
858}
859
860sub _draw {
861 my ($self) = @_;
862
863 my ($w, $h ) = ($self->{w}, $self->{h});
864 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
865
866 glEnable GL_BLEND;
867 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
868 glEnable GL_TEXTURE_2D;
869 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
870
871 my $border = $self->border;
872
873 glColor @{ $self->{border_bg} };
627 $top->draw_quad (0, 0, $w, $top->{h}); 874 $tex[1]->draw_quad (0, 0, $w, $border);
628 875 $tex[3]->draw_quad (0, $border, $border, $ch);
629 my $left = $tex[3]; 876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
630 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 877 $tex[4]->draw_quad (0, $h - $border, $w, $border);
631
632 my $right = $tex[2];
633 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
634
635 my $bottom = $tex[4];
636 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
637 878
638 my $bg = $tex[0]; 879 my $bg = $tex[0];
639 880
640 glBindTexture GL_TEXTURE_2D, $bg->{name}; 881 # TODO: repeat texture not scale
641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
642
643 my $rep_x = $cw / $bg->{w}; 882 my $rep_x = $cw / $bg->{w};
644 my $rep_y = $ch / $bg->{h}; 883 my $rep_y = $ch / $bg->{h};
645 884
646 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 885 glColor @{ $self->{bg} };
886
887 $bg->{s} = $rep_x;
888 $bg->{t} = $rep_y;
889 $bg->{wrap_mode} = 1;
890 $bg->draw_quad ($border, $border, $cw, $ch);
647 891
648 glDisable GL_TEXTURE_2D; 892 glDisable GL_TEXTURE_2D;
649 glDisable GL_BLEND; 893 glDisable GL_BLEND;
650 894
895 $self->{title}->draw if $self->{title};
651 $self->child->draw; 896 $self->child->draw;
652
653} 897}
654 898
655############################################################################# 899#############################################################################
656 900
657package CFClient::UI::Table; 901package CFClient::UI::Table;
658 902
659our @ISA = CFClient::UI::Base::; 903our @ISA = CFClient::UI::Base::;
660 904
661use List::Util qw(max sum); 905use List::Util qw(max sum);
662 906
663use SDL::OpenGL; 907use CFClient::OpenGL;
664 908
665sub new { 909sub new {
666 my $class = shift; 910 my $class = shift;
667 911
668 $class->SUPER::new ( 912 $class->SUPER::new (
670 @_ 914 @_
671 ) 915 )
672} 916}
673 917
674sub add { 918sub add {
675 my ($self, $x, $y, $chld) = @_; 919 my ($self, $x, $y, $child) = @_;
676 920
921 $child->set_parent ($self);
677 $self->{children}[$y][$x] = $chld; 922 $self->{children}[$y][$x] = $child;
678 $chld->set_parent ($self);
679 923
680 $self->{w} = $self->{h} = -1; 924 $child->check_size;
925}
926
927sub children {
928 grep $_, map @$_, grep $_, @{ $_[0]{children} }
929}
930
931# TODO: move to container class maybe? send childs a signal on removal?
932sub clear {
933 my ($self) = @_;
934
935 my @children = $self->children;
936 delete $self->{children};
937
938 for (@children) {
939 delete $_->{parent};
940 $_->hide;
941 }
942
681 $self->update; 943 $self->update;
682} 944}
683 945
684sub get_wh { 946sub get_wh {
685 my ($self) = @_; 947 my ($self) = @_;
691 or next; 953 or next;
692 954
693 for my $x (0 .. $#$row) { 955 for my $x (0 .. $#$row) {
694 my $widget = $row->[$x] 956 my $widget = $row->[$x]
695 or next; 957 or next;
696 my ($w, $h) = $widget->size_request; 958 my ($w, $h) = @$widget{qw(req_w req_h)};
697 959
698 $w[$x] = max $w[$x], $w; 960 $w[$x] = max $w[$x], $w;
699 $h[$y] = max $h[$y], $h; 961 $h[$y] = max $h[$y], $h;
700 } 962 }
701 } 963 }
713 (sum @$hs), 975 (sum @$hs),
714 ) 976 )
715} 977}
716 978
717sub size_allocate { 979sub size_allocate {
718 my ($self, $x, $y, $w, $h) = @_; 980 my ($self, $w, $h) = @_;
719
720 $self->_size_allocate ($x, $y, $w, $h) or return;
721 981
722 my ($ws, $hs) = $self->get_wh; 982 my ($ws, $hs) = $self->get_wh;
723 983
724 my $req_w = sum @$ws; 984 my $req_w = sum @$ws;
725 my $req_h = sum @$hs; 985 my $req_h = sum @$hs;
730 my $col_expand = (sum @col_expand) || 1; 990 my $col_expand = (sum @col_expand) || 1;
731 991
732 # linearly scale sizes 992 # linearly scale sizes
733 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 993 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
734 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 994 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
995
996 CFClient::UI::harmonize $ws;
997 CFClient::UI::harmonize $hs;
735 998
736 my $y; 999 my $y;
737 1000
738 for my $r (0 .. $#{$self->{children}}) { 1001 for my $r (0 .. $#{$self->{children}}) {
739 my $row = $self->{children}[$r] 1002 my $row = $self->{children}[$r]
744 1007
745 for my $c (0 .. $#$row) { 1008 for my $c (0 .. $#$row) {
746 my $col_w = $ws->[$c]; 1009 my $col_w = $ws->[$c];
747 1010
748 if (my $widget = $row->[$c]) { 1011 if (my $widget = $row->[$c]) {
749 $widget->size_allocate ($x, $y, $col_w, $row_h); 1012 $widget->configure ($x, $y, $col_w, $row_h);
750 } 1013 }
751 1014
752 $x += $col_w; 1015 $x += $col_w;
753 } 1016 }
754 1017
799 (List::Util::max map $_->[1], @alloc), 1062 (List::Util::max map $_->[1], @alloc),
800 ) 1063 )
801} 1064}
802 1065
803sub size_allocate { 1066sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 1067 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h);
807 1068
808 ($h, $w) = ($w, $h); 1069 ($h, $w) = ($w, $h);
809 1070
810 my $children = $self->{children}; 1071 my $children = $self->{children};
811 1072
812 my @h = map +($_->size_request)[0], @$children; 1073 my @h = map $_->{req_w}, @$children;
813 1074
814 my $req_h = List::Util::sum @h; 1075 my $req_h = List::Util::sum @h;
815 1076
816 if ($req_h > $h) { 1077 if ($req_h > $h) {
817 # ah well, not enough space 1078 # ah well, not enough space
827 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1088 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
828 $h[$_] = $alloc_h; 1089 $h[$_] = $alloc_h;
829 } 1090 }
830 } 1091 }
831 1092
1093 CFClient::UI::harmonize \@h;
1094
832 my $y = 0; 1095 my $y = 0;
833 for (0 .. $#$children) { 1096 for (0 .. $#$children) {
834 my $child = $children->[$_]; 1097 my $child = $children->[$_];
835 my $h = $h[$_]; 1098 my $h = $h[$_];
836 $child->size_allocate ($y, 0, $h, $w); 1099 $child->configure ($y, 0, $h, $w);
837 1100
838 $y += $h; 1101 $y += $h;
839 } 1102 }
1103
1104 1
840} 1105}
841 1106
842############################################################################# 1107#############################################################################
843 1108
844package CFClient::UI::VBox; 1109package CFClient::UI::VBox;
857 (List::Util::sum map $_->[1], @alloc), 1122 (List::Util::sum map $_->[1], @alloc),
858 ) 1123 )
859} 1124}
860 1125
861sub size_allocate { 1126sub size_allocate {
862 my ($self, $x, $y, $w, $h) = @_; 1127 my ($self, $w, $h) = @_;
863
864 $self->_size_allocate ($x, $y, $w, $h);
865 1128
866 my $children = $self->{children}; 1129 my $children = $self->{children};
867 1130
868 my @h = map +($_->size_request)[1], @$children; 1131 my @h = map $_->{req_h}, @$children;
869 1132
870 my $req_h = List::Util::sum @h; 1133 my $req_h = List::Util::sum @h;
871 1134
872 if ($req_h > $h) { 1135 if ($req_h > $h) {
873 # ah well, not enough space 1136 # ah well, not enough space
881 1144
882 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1145 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
883 } 1146 }
884 } 1147 }
885 1148
1149 CFClient::UI::harmonize \@h;
1150
886 my $y = 0; 1151 my $y = 0;
887 for (0 .. $#$children) { 1152 for (0 .. $#$children) {
888 my $child = $children->[$_]; 1153 my $child = $children->[$_];
889 my $h = $h[$_]; 1154 my $h = $h[$_];
890 $child->size_allocate (0, $y, $w, $h); 1155 $child->configure (0, $y, $w, $h);
891 1156
892 $y += $h; 1157 $y += $h;
893 } 1158 }
1159
1160 1
894} 1161}
895 1162
896############################################################################# 1163#############################################################################
897 1164
898package CFClient::UI::Label; 1165package CFClient::UI::Label;
899 1166
900our @ISA = CFClient::UI::Base::; 1167our @ISA = CFClient::UI::Base::;
901 1168
902use SDL::OpenGL; 1169use CFClient::OpenGL;
903 1170
904sub new { 1171sub new {
905 my ($class, %arg) = @_; 1172 my ($class, %arg) = @_;
906 1173
907 my $self = $class->SUPER::new ( 1174 my $self = $class->SUPER::new (
908 fg => [1, 1, 1], 1175 fg => [1, 1, 1],
909 height => $::FONTSIZE, 1176 #font => default_font
1177 fontsize => 1,
910 text => "", 1178 text => "",
911 align => -1, 1179 align => -1,
1180 valign => -1,
912 padding => 2, 1181 padding => 2,
913 layout => new CFClient::Layout, 1182 layout => new CFClient::Layout,
1183 can_events => 0,
914 %arg 1184 %arg
915 ); 1185 );
916 1186
917 $self->set_text ($self->{text}); 1187 if (exists $self->{template}) {
1188 my $layout = new CFClient::Layout;
1189 $layout->set_text (delete $self->{template});
1190 $self->{template} = $layout;
1191 }
1192
1193 $self->set_text (delete $self->{text}) if exists $self->{text};
1194 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
918 1195
919 $self 1196 $self
920} 1197}
921 1198
922sub escape_text { 1199sub escape {
923 local $_ = $_[1]; 1200 local $_ = $_[1];
924 1201
925 s/&/&amp;/g; 1202 s/&/&amp;/g;
926 s/>/&gt;/g; 1203 s/>/&gt;/g;
927 s/</&lt;/g; 1204 s/</&lt;/g;
930} 1207}
931 1208
932sub set_text { 1209sub set_text {
933 my ($self, $text) = @_; 1210 my ($self, $text) = @_;
934 1211
935 $self->{text} = $text;
936 $self->{layout}->set_markup ($text); 1212 $self->{layout}->set_text ($text);
937 1213
938 delete $self->{texture}; 1214 delete $self->{texture};
939# $self->{w} = $self->{h} = -1; 1215 $self->check_size;
940 $self->update; 1216 $self->update;
941} 1217}
942 1218
943sub get_text { 1219sub set_markup {
944 my ($self, $text) = @_; 1220 my ($self, $markup) = @_;
945 1221
946 $self->{text} 1222 $self->{layout}->set_markup ($markup);
1223
1224 delete $self->{texture};
1225 $self->check_size;
1226 $self->update;
947} 1227}
948 1228
949sub size_request { 1229sub size_request {
950 my ($self) = @_; 1230 my ($self) = @_;
951 1231
952 $self->{layout}->set_width; 1232 $self->{layout}->set_font ($self->{font}) if $self->{font};
1233 $self->{layout}->set_width ($self->{max_w} || -1);
953 $self->{layout}->set_height ($self->{height}); 1234 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1235
954 my ($w, $h) = $self->{layout}->size; 1236 my ($w, $h) = $self->{layout}->size;
1237
1238 if (exists $self->{template}) {
1239 $self->{template}->set_font ($self->{font}) if $self->{font};
1240 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1241
1242 my ($w2, $h2) = $self->{template}->size;
1243
1244 $w = List::Util::max $w, $w2;
1245 $h = List::Util::max $h, $h2;
1246 }
955 1247
956 ( 1248 (
957 $w + $self->{padding} * 2, 1249 $w + $self->{padding} * 2,
958 $h + $self->{padding} * 2, 1250 $h + $self->{padding} * 2,
959 ) 1251 )
960} 1252}
961 1253
962sub size_allocate { 1254sub size_allocate {
963 my ($self, $x, $y, $w, $h) = @_; 1255 my ($self, $w, $h) = @_;
964
965 $self->_size_allocate ($x, $y, $w, $h) or return;
966 1256
967 delete $self->{texture}; 1257 delete $self->{texture};
968} 1258}
969 1259
970sub update { 1260sub set_fontsize {
971 my ($self) = @_; 1261 my ($self, $fontsize) = @_;
972 1262
1263 $self->{fontsize} = $fontsize;
973 delete $self->{texture}; 1264 delete $self->{texture};
1265 $self->check_size;
974 $self->SUPER::update; 1266 $self->update;
975} 1267}
976 1268
977sub _draw { 1269sub _draw {
978 my ($self) = @_; 1270 my ($self) = @_;
979 1271
980 my $tex = $self->{texture} ||= do { 1272 my $tex = $self->{texture} ||= do {
1273 $self->{layout}->set_font ($self->{font}) if $self->{font};
981 $self->{layout}->set_width ($self->{w}); 1274 $self->{layout}->set_width ($self->{w});
982 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1275 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
983 new_from_layout CFClient::Texture $self->{layout} 1276 new_from_layout CFClient::Texture $self->{layout}
984 }; 1277 };
985 1278
986 glEnable GL_BLEND; 1279 glEnable GL_BLEND;
987 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1280 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
988 glEnable GL_TEXTURE_2D; 1281 glEnable GL_TEXTURE_2D;
989 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1282 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
990 1283
991 glColor @{$self->{fg}}; 1284 glColor @{$self->{fg}};
992 1285
993 my $x = 1286 $self->{ox} = int (
994 $self->{align} < 0 ? $self->{padding} 1287 $self->{align} < 0 ? $self->{padding}
995 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1288 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
996 : ($self->{w} - $tex->{w}) * 0.5; 1289 : ($self->{w} - $tex->{w}) * 0.5
1290 );
997 1291
998 glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0; 1292 $self->{oy} = int (
999 $tex->draw_quad (0, 0); 1293 $self->{valign} < 0 ? $self->{padding}
1294 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1295 : ($self->{h} - $tex->{h}) * 0.5
1296 );
1297
1298 $tex->draw_quad ($self->{ox}, $self->{oy});
1000 1299
1001 glDisable GL_TEXTURE_2D; 1300 glDisable GL_TEXTURE_2D;
1002 glDisable GL_BLEND; 1301 glDisable GL_BLEND;
1003} 1302}
1004 1303
1005############################################################################# 1304#############################################################################
1006 1305
1007package CFClient::UI::Entry; 1306package CFClient::UI::EntryBase;
1008 1307
1009our @ISA = CFClient::UI::Label::; 1308our @ISA = CFClient::UI::Label::;
1010 1309
1011use SDL; 1310use CFClient::OpenGL;
1012use SDL::OpenGL;
1013 1311
1014sub new { 1312sub new {
1015 my $class = shift; 1313 my $class = shift;
1016 1314
1017 $class->SUPER::new ( 1315 $class->SUPER::new (
1018 fg => [1, 1, 1], 1316 fg => [1, 1, 1],
1019 bg => [0, 0, 0, 0.2], 1317 bg => [0, 0, 0, 0.2],
1020 active_bg => [1, 1, 1, 0.5], 1318 active_bg => [1, 1, 1, 0.5],
1021 active_fg => [0, 0, 0], 1319 active_fg => [0, 0, 0],
1320 can_hover => 1,
1321 can_focus => 1,
1322 valign => 0,
1323 can_events => 1,
1022 @_ 1324 @_
1023 ) 1325 )
1024} 1326}
1025 1327
1026sub _set_text { 1328sub _set_text {
1027 my ($self, $text) = @_; 1329 my ($self, $text) = @_;
1028 1330
1331 delete $self->{cur_h};
1332
1333 return if $self->{text} eq $text;
1334
1335 delete $self->{texture};
1336
1029 $self->{last_activity} = $::NOW; 1337 $self->{last_activity} = $::NOW;
1030
1031 $self->{text} = $text; 1338 $self->{text} = $text;
1032 $self->{layout}->set_width ($self->{w});
1033 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
1034 1339
1035 $text =~ s/./*/g if $self->{hidden}; 1340 $text =~ s/./*/g if $self->{hidden};
1341 $self->{layout}->set_text ("$text ");
1036 1342
1037 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1343 $self->emit (changed => $self->{text});
1344}
1038 1345
1039 $text = substr $text, 0, $self->{cursor}; 1346sub get_text {
1040 utf8::encode $text; 1347 $_[0]{text}
1041
1042 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1043} 1348}
1044 1349
1045sub size_request { 1350sub size_request {
1046 my ($self) = @_; 1351 my ($self) = @_;
1047 1352
1049 1354
1050 ($w + 1, $h) # add 1 for cursor 1355 ($w + 1, $h) # add 1 for cursor
1051} 1356}
1052 1357
1053sub size_allocate { 1358sub size_allocate {
1054 my ($self, $x, $y, $w, $h) = @_; 1359 my ($self, $w, $h) = @_;
1055 1360
1056 $self->SUPER::size_allocate ($x, $y, $w, $h); 1361 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1057
1058 $self->_set_text ($self->{text});
1059} 1362}
1060 1363
1061sub set_text { 1364sub set_text {
1062 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1063 1366
1067} 1370}
1068 1371
1069sub key_down { 1372sub key_down {
1070 my ($self, $ev) = @_; 1373 my ($self, $ev) = @_;
1071 1374
1072 my $mod = $ev->key_mod; 1375 my $mod = $ev->{mod};
1073 my $sym = $ev->key_sym; 1376 my $sym = $ev->{sym};
1074
1075 my $uni = $ev->key_unicode; 1377 my $uni = $ev->{unicode};
1076 1378
1077 my $text = $self->get_text; 1379 my $text = $self->get_text;
1078 1380
1079 if ($sym == SDLK_BACKSPACE) { 1381 if ($sym == 8) {
1080 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1382 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1081 } elsif ($sym == SDLK_DELETE) { 1383 } elsif ($sym == 127) {
1082 substr $text, $self->{cursor}, 1, ""; 1384 substr $text, $self->{cursor}, 1, "";
1083 } elsif ($sym == SDLK_LEFT) { 1385 } elsif ($sym == CFClient::SDLK_LEFT) {
1084 --$self->{cursor} if $self->{cursor}; 1386 --$self->{cursor} if $self->{cursor};
1085 } elsif ($sym == SDLK_RIGHT) { 1387 } elsif ($sym == CFClient::SDLK_RIGHT) {
1086 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1388 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1087 } elsif ($sym == SDLK_HOME) { 1389 } elsif ($sym == CFClient::SDLK_HOME) {
1088 $self->{cursor} = 0; 1390 $self->{cursor} = 0;
1089 } elsif ($sym == SDLK_END) { 1391 } elsif ($sym == CFClient::SDLK_END) {
1090 $self->{cursor} = length $text; 1392 $self->{cursor} = length $text;
1393 } elsif ($sym == 27) {
1394 $self->emit ('escape');
1091 } elsif ($uni) { 1395 } elsif ($uni) {
1092 substr $text, $self->{cursor}++, 0, chr $uni; 1396 substr $text, $self->{cursor}++, 0, chr $uni;
1093 } 1397 }
1094 1398
1095 $self->_set_text ($text); 1399 $self->_set_text ($text);
1149 1453
1150 $self->SUPER::_draw; 1454 $self->SUPER::_draw;
1151 1455
1152 #TODO: force update every cursor change :( 1456 #TODO: force update every cursor change :(
1153 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1457 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1458
1459 unless (exists $self->{cur_h}) {
1460 my $text = substr $self->{text}, 0, $self->{cursor};
1461 utf8::encode $text;
1462
1463 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1464 }
1465
1154 glColor @{$self->{fg}}; 1466 glColor @{$self->{fg}};
1155 glBegin GL_LINES; 1467 glBegin GL_LINES;
1156 glVertex $self->{cur_x}, $self->{cur_y};
1157 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1468 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1469 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1158 glEnd; 1470 glEnd;
1159 } 1471 }
1160} 1472}
1161 1473
1474package CFClient::UI::Entry;
1475
1476our @ISA = CFClient::UI::EntryBase::;
1477
1478use CFClient::OpenGL;
1479
1480sub key_down {
1481 my ($self, $ev) = @_;
1482
1483 my $sym = $ev->{sym};
1484
1485 if ($sym == 13) {
1486 unshift @{$self->{history}},
1487 my $txt = $self->get_text;
1488 $self->{history_pointer} = -1;
1489 $self->{history_saveback} = '';
1490 $self->emit (activate => $txt);
1491 $self->update;
1492
1493 } elsif ($sym == CFClient::SDLK_UP) {
1494 if ($self->{history_pointer} < 0) {
1495 $self->{history_saveback} = $self->get_text;
1496 }
1497 if (@{$self->{history} || []} > 0) {
1498 $self->{history_pointer}++;
1499 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1500 $self->{history_pointer} = @{$self->{history} || []} - 1;
1501 }
1502 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1503 }
1504
1505 } elsif ($sym == CFClient::SDLK_DOWN) {
1506 $self->{history_pointer}--;
1507 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1508
1509 if ($self->{history_pointer} >= 0) {
1510 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1511 } else {
1512 $self->set_text ($self->{history_saveback});
1513 }
1514
1515 } else {
1516 $self->SUPER::key_down ($ev);
1517 }
1518
1519}
1520
1162############################################################################# 1521#############################################################################
1163 1522
1164package CFClient::UI::Button; 1523package CFClient::UI::Button;
1165 1524
1166our @ISA = CFClient::UI::Label::; 1525our @ISA = CFClient::UI::Label::;
1167 1526
1168use SDL; 1527use CFClient::OpenGL;
1169use SDL::OpenGL;
1170 1528
1171my @tex = 1529my @tex =
1172 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1530 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1173 qw(b1_button_active.png); 1531 qw(b1_button_active.png);
1174 1532
1175sub new { 1533sub new {
1176 my $class = shift; 1534 my $class = shift;
1177 1535
1178 $class->SUPER::new ( 1536 $class->SUPER::new (
1179 padding => 4, 1537 padding => 4,
1180 fg => [1, 1, 1], 1538 fg => [1, 1, 1],
1181 bg => [1, 1, 1, 0.2], 1539 bg => [1, 1, 1, 0.2],
1182 active_fg => [1, 1, 0],
1183# active_bg => [0, 0, 0, 0.5], 1540 active_fg => [0, 0, 1],
1184# border_fg => [1, 1, 0], 1541 can_hover => 1,
1542 align => 0,
1543 valign => 0,
1544 can_events => 1,
1185 @_ 1545 @_
1186 ) 1546 )
1187} 1547}
1188 1548
1189sub button_up { 1549sub button_up {
1197 1557
1198sub _draw { 1558sub _draw {
1199 my ($self) = @_; 1559 my ($self) = @_;
1200 1560
1201 local $self->{fg} = $self->{fg}; 1561 local $self->{fg} = $self->{fg};
1202 my $tex = $tex[0];
1203
1204 glEnable GL_BLEND;
1205 glEnable GL_TEXTURE_2D;
1206 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1207 1562
1208 if ($GRAB == $self) { 1563 if ($GRAB == $self) {
1209 $self->{fg} = $self->{active_fg}; 1564 $self->{fg} = $self->{active_fg};
1210 } 1565 }
1211 1566
1212 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1567 glEnable GL_BLEND;
1568 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1569 glEnable GL_TEXTURE_2D;
1213 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1570 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1571 glColor 0, 0, 0, 1;
1214 1572
1215 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1573 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1216 1574
1217 glDisable GL_TEXTURE_2D; 1575 glDisable GL_TEXTURE_2D;
1218 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1219 1577
1220 $self->SUPER::_draw; 1578 $self->SUPER::_draw;
1224 1582
1225package CFClient::UI::CheckBox; 1583package CFClient::UI::CheckBox;
1226 1584
1227our @ISA = CFClient::UI::DrawBG::; 1585our @ISA = CFClient::UI::DrawBG::;
1228 1586
1229use SDL; 1587my @tex =
1230use SDL::OpenGL; 1588 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1589 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1590
1591use CFClient::OpenGL;
1231 1592
1232sub new { 1593sub new {
1233 my $class = shift; 1594 my $class = shift;
1234 1595
1235 $class->SUPER::new ( 1596 $class->SUPER::new (
1236 padding => 2, 1597 padding => 2,
1237 fg => [1, 1, 1], 1598 fg => [1, 1, 1],
1238 active_fg => [1, 1, 0], 1599 active_fg => [1, 1, 0],
1239 state => 0, 1600 state => 0,
1601 can_hover => 1,
1240 @_ 1602 @_
1241 ) 1603 )
1242} 1604}
1243 1605
1244sub size_request { 1606sub size_request {
1245 my ($self) = @_; 1607 my ($self) = @_;
1246 1608
1247 ($self->{padding} * 2 + 6) x 2 1609 ($self->{padding} * 2 + 6) x 2
1248}
1249
1250sub size_allocate {
1251 my ($self, $x, $y, $w, $h) = @_;
1252
1253 $self->_size_allocate ($x, $y, $w, $h);
1254} 1610}
1255 1611
1256sub button_down { 1612sub button_down {
1257 my ($self, $ev, $x, $y) = @_; 1613 my ($self, $ev, $x, $y) = @_;
1258 1614
1269 $self->SUPER::_draw; 1625 $self->SUPER::_draw;
1270 1626
1271 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1627 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1272 1628
1273 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1629 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1274 1630
1275 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1631 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1276 1632
1277 glBegin GL_LINE_LOOP; 1633 glEnable GL_BLEND;
1278 glVertex 0 , 0; 1634 glEnable GL_TEXTURE_2D;
1279 glVertex 0 , $s; 1635 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1280 glVertex $s, $s;
1281 glVertex $s, 0;
1282 glEnd;
1283 1636
1284 if ($self->{state}) { 1637 my $tex = $self->{state} ? $tex[1] : $tex[0];
1285 glBegin GL_LINES;
1286 glVertex 0 , 0;
1287 glVertex $s, $s;
1288 glVertex $s, 0;
1289 glVertex 0 , $s;
1290 glEnd;
1291 }
1292}
1293 1638
1294############################################################################# 1639 $tex->draw_quad (0, 0, $s, $s);
1295 1640
1641 glDisable GL_TEXTURE_2D;
1642 glDisable GL_BLEND;
1643}
1644
1645#############################################################################
1646
1296package CFClient::UI::Slider; 1647package CFClient::UI::Image;
1297 1648
1298use strict;
1299
1300use SDL::OpenGL;
1301
1302our @ISA = CFClient::UI::DrawBG::; 1649our @ISA = CFClient::UI::Base::;
1650
1651use CFClient::OpenGL;
1652use Carp qw/confess/;
1653
1654our %loaded_images;
1303 1655
1304sub new { 1656sub new {
1305 my $class = shift; 1657 my $class = shift;
1306 1658
1659 my $self = $class->SUPER::new (can_events => 0, @_);
1660
1661 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1662
1663 $loaded_images{$self->{image}} ||=
1664 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1665
1666 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1667
1668 Scalar::Util::weaken $loaded_images{$self->{image}};
1669
1670 $self->{aspect} = $tex->{w} / $tex->{h};
1671
1672 $self
1673}
1674
1675sub size_request {
1676 my ($self) = @_;
1677
1678 ($self->{tex}->{w}, $self->{tex}->{h})
1679}
1680
1681sub _draw {
1682 my ($self) = @_;
1683
1684 my $tex = $self->{tex};
1685
1686 my ($w, $h) = ($self->{w}, $self->{h});
1687
1688 if ($self->{rot90}) {
1689 glRotate 90, 0, 0, 1;
1690 glTranslate 0, -$self->{w}, 0;
1691
1692 ($w, $h) = ($h, $w);
1693 }
1694
1695 glEnable GL_BLEND;
1696 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1697 glEnable GL_TEXTURE_2D;
1698 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1699
1700 $tex->draw_quad (0, 0, $w, $h);
1701
1702 glDisable GL_BLEND;
1703 glDisable GL_TEXTURE_2D;
1704}
1705
1706#############################################################################
1707
1708package CFClient::UI::VGauge;
1709
1710our @ISA = CFClient::UI::Base::;
1711
1712use List::Util qw(min max);
1713
1714use CFClient::OpenGL;
1715
1716my %tex = (
1717 food => [
1718 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1719 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1720 ],
1721 grace => [
1722 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1723 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1724 ],
1725 hp => [
1726 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1727 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1728 ],
1729 mana => [
1730 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1731 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1732 ],
1733);
1734
1735# eg. VGauge->new (gauge => 'food'), default gauge: food
1736sub new {
1737 my $class = shift;
1738
1739 my $self = $class->SUPER::new (
1740 type => 'food',
1741 @_
1742 );
1743
1744 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1745
1746 $self
1747}
1748
1749sub size_request {
1750 my ($self) = @_;
1751
1752 #my $tex = $tex{$self->{type}}[0];
1753 #@$tex{qw(w h)}
1754 (0, 0)
1755}
1756
1757sub set_max {
1758 my ($self, $max) = @_;
1759
1760 $self->{max_val} = $max;
1761}
1762
1763sub set_value {
1764 my ($self, $val, $max) = @_;
1765
1766 $self->set_max ($max)
1767 if defined $max;
1768
1769 $max = $self->{max_val};
1770 $self->{val} = $val;
1771
1772 $self->update;
1773}
1774
1775sub _draw {
1776 my ($self) = @_;
1777
1778 my $tex = $tex{$self->{type}};
1779 my ($t1, $t2, $t3) = @$tex;
1780
1781 my ($w, $h) = ($self->{w}, $self->{h});
1782
1783 if ($self->{vertical}) {
1784 glRotate 90, 0, 0, 1;
1785 glTranslate 0, -$self->{w}, 0;
1786
1787 ($w, $h) = ($h, $w);
1788 }
1789
1790 my $ycut = $self->{val} / ($self->{max_val} || 1);
1791
1792 my $ycut1 = max 0, min 1, $ycut;
1793 my $ycut2 = max 0, min 1, $ycut - 1;
1794
1795 my $h1 = $self->{h} * (1 - $ycut1);
1796 my $h2 = $self->{h} * (1 - $ycut2);
1797
1798 glEnable GL_BLEND;
1799 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1800 glEnable GL_TEXTURE_2D;
1801 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1802
1803 glBindTexture GL_TEXTURE_2D, $t1->{name};
1804 glBegin GL_QUADS;
1805 glTexCoord 0 , 0; glVertex 0 , 0;
1806 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1807 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1808 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1809 glEnd;
1810
1811 my $ycut1 = List::Util::min 1, $ycut;
1812 glBindTexture GL_TEXTURE_2D, $t2->{name};
1813 glBegin GL_QUADS;
1814 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1815 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1816 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1817 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1818 glEnd;
1819
1820 if ($t3) {
1821 glBindTexture GL_TEXTURE_2D, $t3->{name};
1822 glBegin GL_QUADS;
1823 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1824 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1825 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1826 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1827 glEnd;
1828 }
1829
1830 glDisable GL_BLEND;
1831 glDisable GL_TEXTURE_2D;
1832}
1833
1834#############################################################################
1835
1836package CFClient::UI::Gauge;
1837
1838our @ISA = CFClient::UI::VBox::;
1839
1840sub new {
1841 my ($class, %arg) = @_;
1842
1843 my $self = $class->SUPER::new (
1844 tooltip => $arg{type},
1845 can_hover => 1,
1846 can_events => 1,
1847 %arg,
1848 );
1849
1850 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1851 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1852 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1853
1854 $self
1855}
1856
1857sub set_fontsize {
1858 my ($self, $fsize) = @_;
1859
1860 $self->{value}->set_fontsize ($fsize);
1861 $self->{max} ->set_fontsize ($fsize);
1862}
1863
1864sub set_value {
1865 my ($self, $val, $max) = @_;
1866
1867 $self->set_max ($max)
1868 if defined $max;
1869
1870 $self->{gauge}->set_value ($val, $max);
1871 $self->{value}->set_text ($val);
1872}
1873
1874sub set_max {
1875 my ($self, $max) = @_;
1876
1877 $self->{gauge}->set_max ($max);
1878 $self->{max}->set_text ($max);
1879}
1880
1881#############################################################################
1882
1883package CFClient::UI::Slider;
1884
1885use strict;
1886
1887use CFClient::OpenGL;
1888
1889our @ISA = CFClient::UI::DrawBG::;
1890
1891my @tex =
1892 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1893 qw(s1_slider.png s1_slider_bg.png);
1894
1895sub new {
1896 my $class = shift;
1897
1307 # range [value, low, high, page] 1898 # range [value, low, high, page]
1308 1899
1900 # TODO: 0-width page
1901 # TODO: req_w/h are wrong with vertical
1902 # TODO: calculations are off
1309 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1310 fg => [1, 1, 1], 1904 fg => [1, 1, 1],
1311 active_fg => [0, 0, 0], 1905 active_fg => [0, 0, 0],
1312 range => [0, 0, 100, 10], 1906 range => [0, 0, 100, 10],
1313 req_w => 40, 1907 req_w => 20,
1314 req_h => 10, 1908 req_h => 20,
1315 vertical => 0, 1909 vertical => 0,
1910 can_hover => 1,
1911 inner_pad => 5,
1316 @_ 1912 @_
1317 ); 1913 );
1318 1914
1319 $self 1915 $self
1320} 1916}
1341 if ($GRAB == $self) { 1937 if ($GRAB == $self) {
1342 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1938 my ($value, $lo, $hi, $page) = @{$self->{range}};
1343 1939
1344 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1940 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1345 1941
1942 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1943 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1944
1945 $x -= $inner_pad_px; # substract the padding
1346 $x = $x * ($hi - $lo) / $w + $lo; 1946 $x = $x * ($hi - $lo) / $inner_w + $lo;
1347 $x = $lo if $x < $lo; 1947 $x = $lo if $x < $lo;
1348 $x = $hi - $page if $x > $hi - $page; 1948 $x = $hi - $page if $x > $hi - $page;
1349 $self->{range}[0] = $x; 1949 $self->{range}[0] = $x;
1350 1950
1351 $self->emit (changed => $x); 1951 $self->emit (changed => $x);
1352 $self->update; 1952 $self->update;
1353 } 1953 }
1954}
1955
1956# the inner_* stuff is for generating a padding for the slider handle,
1957# so that the handle doesn't leave the texture. This calculation isn't 100%
1958# correct propably, but it does the job for now
1959sub _calc_inner_pad_px {
1960 my ($self, $w) = @_;
1961 ($w / 100) * $self->{inner_pad} # % to pixels
1354} 1962}
1355 1963
1356sub _draw { 1964sub _draw {
1357 my ($self) = @_; 1965 my ($self) = @_;
1358 1966
1372 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1980 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1373 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1981 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1374 1982
1375 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1983 my ($value, $lo, $hi, $page) = @{$self->{range}};
1376 1984
1985 $hi = $value + 1 if $lo == $hi;
1986
1987 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1988 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1989
1377 $page = int $page * $w / ($hi - $lo); 1990 $page = int $page * $inner_w / ($hi - $lo);
1378 $value = int +($value - $lo) * $w / ($hi - $lo); 1991 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1379 1992
1380 $w -= $page; 1993 $w -= $page;
1381 $page &= ~1; 1994 $page &= ~1;
1382 glTranslate $page * 0.5, 0, 0; 1995 glTranslate $page * 0.5, 0, 0;
1383 $page ||= 2; 1996 $page ||= 2;
1384 1997
1385 glColor @$fg;
1386 glBegin GL_LINES;
1387 glVertex 0, 0; glVertex 0, $h;
1388 glVertex $w - 1, 0; glVertex $w - 1, $h;
1389 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1390 glEnd;
1391
1392 my $knob_a = $value - $page * 0.5; 1998 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1393 my $knob_b = $value + $page * 0.5; 1999 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1394 2000
1395 glBegin GL_QUADS; 2001 glEnable GL_BLEND;
1396 glColor @$fg; 2002 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1397 glVertex $knob_a, 0; 2003 glEnable GL_TEXTURE_2D;
1398 glVertex $knob_a, $h; 2004 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1399 glVertex $knob_b, $h;
1400 glVertex $knob_b, 0;
1401 2005
1402 if ($knob_a < $knob_b - 2) { 2006 # draw background
1403 glColor @$bg; 2007 $tex[1]->draw_quad (0, 0, $w, $h);
1404 glVertex $knob_a + 1, 1;
1405 glVertex $knob_a + 1, $h - 1;
1406 glVertex $knob_b - 1, $h - 1;
1407 glVertex $knob_b - 1, 1;
1408 }
1409 glEnd;
1410}
1411 2008
1412############################################################################# 2009 # draw handle
2010 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1413 2011
2012 glDisable GL_BLEND;
2013 glDisable GL_TEXTURE_2D;
2014}
2015
2016#############################################################################
2017
1414package CFClient::UI::MapWidget; 2018package CFClient::UI::TextView;
1415 2019
1416use strict;
1417
1418use List::Util qw(min max);
1419
1420use SDL;
1421use SDL::OpenGL;
1422
1423our @ISA = CFClient::UI::Base::; 2020our @ISA = CFClient::UI::HBox::;
2021
2022use CFClient::OpenGL;
1424 2023
1425sub new { 2024sub new {
1426 my $class = shift; 2025 my $class = shift;
1427 2026
1428 $class->SUPER::new ( 2027 my $self = $class->SUPER::new (
1429 z => -1, 2028 fontsize => 1,
1430 list => (glGenLists 1), 2029 can_events => 0,
2030 #font => default_font
1431 @_ 2031 @_,
2032
2033 layout => (new CFClient::Layout),
2034 par => [],
2035 height => 0,
2036 children => [
2037 (new CFClient::UI::Empty expand => 1),
2038 (new CFClient::UI::Slider vertical => 1),
2039 ],
1432 ) 2040 );
1433}
1434 2041
1435sub key_down { 2042 $self->{children}[1]->connect (changed => sub {
1436 print "MAPKEYDOWN\n"; 2043 $self->update;
1437} 2044 });
1438 2045
1439sub key_up { 2046 $self
1440} 2047}
1441 2048
1442sub size_request { 2049sub set_fontsize {
2050 my ($self, $fontsize) = @_;
2051
2052 $self->{fontsize} = $fontsize;
2053 $self->reflow;
2054}
2055
2056sub text_height {
2057 my ($self, $text) = @_;
2058
2059 my $layout = $self->{layout};
2060
2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2062 $layout->set_width ($self->{w});
2063 $layout->set_text ($text);
1443 ( 2064
1444 1 + 32 * int $::WIDTH / 32, 2065 ($layout->size)[1]
1445 1 + 32 * int $::HEIGHT / 32, 2066}
1446 ) 2067
2068sub reflow {
2069 my ($self) = @_;
2070
2071 $self->{need_reflow}++;
2072 $self->update;
2073}
2074
2075sub size_allocate {
2076 my ($self, $w, $h) = @_;
2077
2078 $self->SUPER::size_allocate ($w, $h);
2079
2080 $self->{layout}->set_font ($self->{font}) if $self->{font};
2081 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2082 $self->{layout}->set_width ($self->{children}[0]{w});
2083
2084 $self->reflow;
2085}
2086
2087sub add_paragraph {
2088 my ($self, $color, $text) = @_;
2089
2090 #TODO: intelligently "reformat" paragraph
2091
2092 my $height = $self->text_height ($text);
2093
2094 $self->{height} += $height;
2095
2096 push @{$self->{par}}, [$height, $color, $text];
2097
2098 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2099 $self->{children}[1]->update;
1447} 2100}
1448 2101
1449sub update { 2102sub update {
1450 my ($self) = @_; 2103 my ($self) = @_;
1451 2104
1452 $self->{need_update} = 1;
1453 $self->SUPER::update; 2105 $self->SUPER::update;
1454}
1455 2106
1456sub draw { 2107 return unless $self->{h} > 0;
1457 my ($self) = @_;
1458 2108
2109 delete $self->{texture};
2110
2111 $ROOT->on_refresh ($self, sub {
1459 if (delete $self->{need_update}) { 2112 if (delete $self->{need_reflow}) {
1460 glNewList $self->{list}, GL_COMPILE; 2113 my $height = 0;
1461 2114
1462 my $mx = $::CONN->{mapx}; 2115 $height += $_->[0] = $self->text_height ($_->[2])
1463 my $my = $::CONN->{mapy}; 2116 for @{$self->{par}};
1464 2117
1465 my $map = $::CONN->{map}; 2118 $self->{height} = $height;
1466 2119
1467 my ($xofs, $yofs); 2120 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1468 2121
1469 my $sw = 1 + int $::WIDTH / 32; 2122 delete $self->{texture};
1470 my $sh = 1 + int $::HEIGHT / 32;
1471
1472 if ($::CONN->{mapw} > $sw) {
1473 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1474 } else {
1475 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1476 } 2123 }
1477 2124
1478 if ($::CONN->{maph} > $sh) { 2125 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1479 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 2126 glClearColor 0, 0, 0, 1;
1480 } else { 2127 glClear GL_COLOR_BUFFER_BIT;
1481 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 2128
2129 glEnable GL_BLEND;
2130 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2131 glEnable GL_TEXTURE_2D;
2132 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2133
2134 my $top = int $self->{children}[1]{range}[0];
2135
2136 my $y0 = $top;
2137 my $y1 = $top + $self->{h};
2138
2139 my $y = 0;
2140
2141 my $layout = $self->{layout};
2142
2143 $layout->set_font ($self->{font}) if $self->{font};
2144
2145 for my $par (@{$self->{par}}) {
2146 my $h = $par->[0];
2147
2148 if ($y0 < $y + $h && $y < $y1) {
2149 $layout->set_text ($par->[2]);
2150
2151 glColor @{ $par->[1] };
2152 my ($W, $H) = $layout->size;
2153 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
2154 }
2155
2156 $y += $h;
2157 }
2158
2159 glDisable GL_TEXTURE_2D;
2160 glDisable GL_BLEND;
1482 } 2161 };
2162 });
2163}
1483 2164
2165sub _draw {
2166 my ($self) = @_;
2167
2168 if ($self->{texture}) {
1484 glEnable GL_TEXTURE_2D; 2169 glEnable GL_TEXTURE_2D;
1485 glEnable GL_BLEND;
1486 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1487 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1488 glColor 1, 1, 1, 1; 2171 glColor 1, 1, 1, 1;
1489 2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1490 my $sw4 = ($sw + 3) & ~3;
1491 my $darkness = "\x00" x ($sw4 * $sh);
1492
1493 for my $x (0 .. $sw - 1) {
1494 my $row = $map->[$x + $xofs];
1495 for my $y (0 .. $sh - 1) {
1496
1497 my $cell = $row->[$y + $yofs]
1498 or next;
1499
1500 my $dark = $cell->[0];
1501 if ($dark < 0) {
1502 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1503 } else {
1504 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1505 }
1506
1507 for my $num (grep $_, @$cell[1,2,3]) {
1508 my $tex = $::CONN->{face}[$num]{texture} || next;
1509
1510 my ($w, $h) = @$tex{qw(w h)};
1511
1512 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1513 }
1514 }
1515 }
1516
1517# if (1) { # higher quality darkness
1518# $lighting =~ s/(.)/$1$1$1/gs;
1519# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1520#
1521# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1522#
1523# $lighting = $pb->get_pixels;
1524# $lighting =~ s/(.)../$1/gs;
1525# }
1526
1527 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1528
1529 if ($::CFG->{fow_smooth}) { # smooth fog of war
1530 my @conv = (
1531 0.05, 0.05, 0.05,
1532 0.05, 0.60, 0.05,
1533 0.05, 0.05, 0.05,
1534 );
1535 CFClient::glConvolutionParameteri GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_REPLICATE_BORDER;
1536 CFClient::glConvolutionFilter2D GL_CONVOLUTION_2D,
1537 GL_ALPHA, 3, 3, GL_ALPHA, GL_FLOAT,
1538 pack "f*", @conv;
1539 glEnable GL_CONVOLUTION_2D;
1540 }
1541
1542 $darkness = new CFClient::Texture
1543 w => $sw4,
1544 h => $sh,
1545 data => $darkness,
1546 internalformat => GL_ALPHA,
1547 format => GL_ALPHA;
1548
1549 glColor +($::CFG->{fow_intensity}) x 3, 1;
1550 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1551
1552 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1553
1554 glDisable GL_TEXTURE_2D; 2173 glDisable GL_TEXTURE_2D;
1555 glDisable GL_BLEND;
1556
1557 glEndList;
1558 } 2174 }
1559 2175
1560 glCallList $self->{list}; 2176 $self->{children}[1]->draw;
1561 2177
1562 if ($FOCUS != $self) {
1563 glEnable GL_BLEND;
1564 glColor 0, 0, 1, 0.4;
1565 glBegin GL_QUADS;
1566 glVertex 0, 0;
1567 glVertex 0, $::HEIGHT;
1568 glVertex $::WIDTH, $::HEIGHT;
1569 glVertex $::WIDTH, 0;
1570 glEnd;
1571 glDisable GL_BLEND;
1572 }
1573}
1574
1575my %DIR = (
1576 SDLK_KP8, [1, "north"],
1577 SDLK_KP9, [2, "northeast"],
1578 SDLK_KP6, [3, "east"],
1579 SDLK_KP3, [4, "southeast"],
1580 SDLK_KP2, [5, "south"],
1581 SDLK_KP1, [6, "southwest"],
1582 SDLK_KP4, [7, "west"],
1583 SDLK_KP7, [8, "northwest"],
1584
1585 SDLK_UP, [1, "north"],
1586 SDLK_RIGHT, [3, "east"],
1587 SDLK_DOWN, [5, "south"],
1588 SDLK_LEFT, [7, "west"],
1589);
1590
1591sub key_down {
1592 my ($self, $ev) = @_;
1593
1594 my $mod = $ev->key_mod;
1595 my $sym = $ev->key_sym;
1596
1597 if ($sym == SDLK_KP5) {
1598 $::CONN->user_send ("command stay fire");
1599 } elsif ($sym == SDLK_a) {
1600 $::CONN->user_send ("command apply");
1601 } elsif (exists $DIR{$sym}) {
1602 if ($mod & KMOD_SHIFT) {
1603 $self->{shft}++;
1604 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1605 } elsif ($mod & KMOD_CTRL) {
1606 $self->{ctrl}++;
1607 $::CONN->user_send ("command run $DIR{$sym}[0]");
1608 } else {
1609 $::CONN->user_send ("command $DIR{$sym}[1]");
1610 }
1611 }
1612}
1613
1614sub key_up {
1615 my ($self, $ev) = @_;
1616
1617 my $mod = $ev->key_mod;
1618 my $sym = $ev->key_sym;
1619
1620 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1621 $::CONN->user_send ("command fire_stop");
1622 }
1623 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1624 $::CONN->user_send ("command run_stop");
1625 }
1626} 2178}
1627 2179
1628############################################################################# 2180#############################################################################
1629 2181
1630package CFClient::UI::Animator; 2182package CFClient::UI::Animator;
1631 2183
1632use SDL::OpenGL; 2184use CFClient::OpenGL;
1633 2185
1634our @ISA = CFClient::UI::Bin::; 2186our @ISA = CFClient::UI::Bin::;
1635 2187
1636sub moveto { 2188sub moveto {
1637 my ($self, $x, $y) = @_; 2189 my ($self, $x, $y) = @_;
1667 glPopMatrix; 2219 glPopMatrix;
1668} 2220}
1669 2221
1670############################################################################# 2222#############################################################################
1671 2223
1672package CFClient::UI::Toplevel; 2224package CFClient::UI::Flopper;
2225
2226our @ISA = CFClient::UI::Button::;
2227
2228sub new {
2229 my $class = shift;
2230
2231 my $self = $class->SUPER::new (
2232 state => 0,
2233 connect_activate => \&toggle_flopper,
2234 @_
2235 );
2236
2237 if ($self->{state}) {
2238 $self->{state} = 0;
2239 $self->toggle_flopper;
2240 }
2241
2242 $self
2243}
2244
2245sub toggle_flopper {
2246 my ($self) = @_;
2247
2248 # TODO: use animation
2249 if ($self->{state} = !$self->{state}) {
2250 $CFClient::UI::ROOT->add ($self->{other});
2251 $self->{other}->move ($self->coord2global (0, $self->{h}));
2252 $self->emit ("open");
2253 } else {
2254 $CFClient::UI::ROOT->remove ($self->{other});
2255 $self->emit ("close");
2256 }
2257
2258 $self->emit (changed => $self->{state});
2259}
2260
2261#############################################################################
2262
2263package CFClient::UI::Tooltip;
2264
2265our @ISA = CFClient::UI::Bin::;
2266
2267use CFClient::OpenGL;
2268
2269sub new {
2270 my $class = shift;
2271
2272 $class->SUPER::new (
2273 @_,
2274 can_events => 0,
2275 )
2276}
2277
2278sub set_markup {
2279 my ($self, $text) = @_;
2280
2281 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2282 $self->{label}->set_markup ($text);
2283 $self->add ($self->{label});
2284}
2285
2286sub size_request {
2287 my ($self) = @_;
2288
2289 $self->child->set_max_size ($::WIDTH * 0.3);
2290
2291 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2292
2293 ($w + 4, $h + 4)
2294}
2295
2296sub size_allocate {
2297 my ($self, $w, $h) = @_;
2298
2299 $self->SUPER::size_allocate ($w - 4, $h - 4);
2300}
2301
2302sub _draw {
2303 my ($self) = @_;
2304
2305 glPushMatrix;
2306 glTranslate 0.375, 0.375;
2307
2308 my ($w, $h) = @$self{qw(w h)};
2309
2310 glColor 1, 0.8, 0.4;
2311 glBegin GL_QUADS;
2312 glVertex 0 , 0;
2313 glVertex 0 , $h;
2314 glVertex $w, $h;
2315 glVertex $w, 0;
2316 glEnd;
2317
2318 glColor 0, 0, 0;
2319 glBegin GL_LINE_LOOP;
2320 glVertex 0 , 0;
2321 glVertex 0 , $h;
2322 glVertex $w, $h;
2323 glVertex $w, 0;
2324 glEnd;
2325
2326 glPopMatrix;
2327
2328 glTranslate 2, 2;
2329 $self->SUPER::_draw;
2330}
2331
2332#############################################################################
2333
2334package CFClient::UI::Face;
2335
2336our @ISA = CFClient::UI::Base::;
2337
2338use CFClient::OpenGL;
2339
2340sub new {
2341 my $class = shift;
2342
2343 $class->SUPER::new (
2344 aspect => 1,
2345 @_,
2346 )
2347}
2348
2349sub size_request {
2350 (32, 8)
2351}
2352
2353sub draw {
2354 my ($self) = @_;
2355
2356 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2357
2358 if ($tex) {
2359 glEnable GL_BLEND;
2360 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2361 glEnable GL_TEXTURE_2D;
2362 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2363 glColor 1, 1, 1, 1;
2364 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2365 glDisable GL_TEXTURE_2D;
2366 glDisable GL_BLEND;
2367 }
2368}
2369
2370#############################################################################
2371
2372package CFClient::UI::Root;
1673 2373
1674our @ISA = CFClient::UI::Container::; 2374our @ISA = CFClient::UI::Container::;
2375
2376use CFClient::OpenGL;
2377
2378sub check_size {
2379 my ($self) = @_;
2380
2381 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2382}
1675 2383
1676sub size_request { 2384sub size_request {
1677 ($::WIDTH, $::HEIGHT) 2385 ($::WIDTH, $::HEIGHT)
1678} 2386}
1679 2387
1680sub size_allocate { 2388sub configure {
1681 my ($self, $x, $y, $w, $h) = @_; 2389 my ($self, $x, $y, $w, $h) = @_;
1682 2390
1683 $self->_size_allocate ($x, $y, $w, $h); 2391 $self->SUPER::configure ($x, $y, $w, $h);
1684 2392
1685 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1686 for @{$self->{children}}; 2393 for my $child (@{$self->{children}}) {
1687} 2394 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1688 2395
1689sub translate { 2396 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2397 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2398 $child->configure ($X, $Y, $W,$H);
2399 }
2400}
2401
2402sub _topleft {
1690 my ($self, $x, $y) = @_; 2403 my ($self, $x, $y) = @_;
1691 2404
1692 ($x, $y) 2405 ($x, $y)
1693} 2406}
1694 2407
1695sub update { 2408sub update {
1696 my ($self) = @_; 2409 my ($self) = @_;
1697 2410
1698 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2411 $self->check_size;
1699 ::refresh (); 2412 ::refresh ();
1700} 2413}
1701 2414
1702sub add { 2415sub add {
1703 my ($self, $widget) = @_; 2416 my ($self, $child) = @_;
1704 2417
2418 # integerize window positions
2419 $child->{x} = int $child->{x};
2420 $child->{y} = int $child->{y};
2421
1705 $self->SUPER::add ($widget); 2422 $self->SUPER::add ($child);
2423}
1706 2424
1707 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2425sub on_refresh {
2426 my ($self, $id, $cb) = @_;
2427
2428 $self->{refresh_hook}{$id} = $cb;
1708} 2429}
1709 2430
1710sub draw { 2431sub draw {
1711 my ($self) = @_; 2432 my ($self) = @_;
1712 2433
2434 while (my $rcb = delete $self->{refresh_hook}) {
2435 $_->() for values %$rcb;
2436 }
2437
2438 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2439 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2440 glClear GL_COLOR_BUFFER_BIT;
2441
2442 glMatrixMode GL_PROJECTION;
2443 glLoadIdentity;
2444 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2445 glMatrixMode GL_MODELVIEW;
2446 glLoadIdentity;
2447
1713 $self->_draw; 2448 $self->_draw;
1714} 2449}
1715 2450
1716############################################################################# 2451#############################################################################
1717 2452
1718package CFClient::UI; 2453package CFClient::UI;
1719 2454
2455$ROOT = new CFClient::UI::Root;
1720$TOPLEVEL = new CFClient::UI::Toplevel; 2456$TOOLTIP = new CFClient::UI::Tooltip;
1721 2457
17221 24581
1723 2459

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines