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.114 by root, Sat Apr 15 13:56:25 2006 UTC vs.
Revision 1.172 by root, Tue Apr 25 09:52:04 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 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 = $ROOT->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->coord2local ($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 || $ROOT->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->coord2local ($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 || $ROOT->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 && $hover->{can_hover}; 105 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 106 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 107
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($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 }
71} 115}
72 116
73# convert position array to integers 117# convert position array to integers
74sub harmonize { 118sub harmonize {
75 my ($vals) = @_; 119 my ($vals) = @_;
76 120
77 my $rem = 0; 121 my $rem = 0;
78 122
79 for ($vals) { 123 for (@$vals) {
80 my $i = int $_ + $rem; 124 my $i = int $_ + $rem;
81 $rem += $_ - $i; 125 $rem += $_ - $i;
82 $_ = $i; 126 $_ = $i;
83 } 127 }
84} 128}
87 131
88package CFClient::UI::Base; 132package CFClient::UI::Base;
89 133
90use strict; 134use strict;
91 135
92use SDL::OpenGL; 136use CFClient::OpenGL;
93 137
94sub new { 138sub new {
95 my $class = shift; 139 my $class = shift;
96 140
97 my $self = bless { 141 my $self = bless {
98 x => 0, 142 x => 0,
99 y => 0, 143 y => 0,
100 z => 0, 144 z => 0,
101 w => -1, 145 can_events => 1,
102 h => -1,
103 @_ 146 @_
104 }, $class; 147 }, $class;
105 148
106 for (keys %$self) { 149 for (keys %$self) {
107 if (/^connect_(.*)$/) { 150 if (/^connect_(.*)$/) {
110 } 153 }
111 154
112 $self 155 $self
113} 156}
114 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
115sub move { 183sub move {
116 my ($self, $x, $y, $z) = @_; 184 my ($self, $x, $y, $z) = @_;
185
117 $self->{x} = $x; 186 $self->{x} = int $x;
118 $self->{y} = $y; 187 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 188 $self->{z} = $z if defined $z;
120}
121 189
122sub needs_redraw { 190 $self->update;
123 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;
124} 200}
125 201
126sub size_request { 202sub size_request {
127 require Carp; 203 require Carp;
128 Carp::confess "size_request is abtract"; 204 Carp::confess "size_request is abstract";
129} 205}
130 206
131sub _size_allocate { 207sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 208 my ($self, $x, $y, $w, $h) = @_;
133 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) {
134 $self->{x} = $x; 223 $self->{x} = $x;
135 $self->{y} = $y; 224 $self->{y} = $y;
225 $self->update;
226 }
136 227
137 return unless $self->{w} != $w || $self->{h} != $h; 228 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 229 $self->{w} = $w;
140 $self->{h} = $h; 230 $self->{h} = $h;
141 231
232 $self->size_allocate ($w, $h);
233 $self->update;
142 1 234 }
143} 235}
144 236
145sub size_allocate { 237sub size_allocate {
238 # nothing to be done
239}
240
241sub children {
242}
243
244sub set_max_size {
146 my ($self, $x, $y, $w, $h) = @_; 245 my ($self, $w, $h) = @_;
147 246
148 $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;
149} 249}
150 250
151# return top left coordinates 251# return top left coordinates
152sub _topleft { 252sub _topleft {
153 my ($self, $x, $y) = @_; 253 my ($self, $x, $y) = @_;
154 254
255 $self->{parent}
256 or Carp::confess "no parent widget in _topleft\n";#d#
257
155 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 258 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
156} 259}
157 260
158# translate global coordinates to local coordinate system 261# translate global coordinates to local coordinate system
159sub coord2local { 262sub coord2local {
176 279
177 return if $FOCUS == $self; 280 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 281 return unless $self->{can_focus};
179 282
180 my $focus = $FOCUS; $FOCUS = $self; 283 my $focus = $FOCUS; $FOCUS = $self;
284
285 $self->emit (focus_in => $focus);
286
181 $focus->update if $focus; 287 $focus->update if $focus;
182 $FOCUS->update; 288 $FOCUS->update;
183} 289}
184 290
185sub focus_out { 291sub focus_out {
186 my ($self) = @_; 292 my ($self) = @_;
187 293
188 return unless $FOCUS == $self; 294 return unless $FOCUS == $self;
189 295
190 my $focus = $FOCUS; undef $FOCUS; 296 my $focus = $FOCUS; undef $FOCUS;
297
298 $self->emit (focus_out => $focus);
299
191 $focus->update if $focus; #? 300 $focus->update if $focus; #?
192} 301}
193 302
194sub mouse_motion { } 303sub mouse_motion { }
195sub button_up { } 304sub button_up { }
219 glPopMatrix; 328 glPopMatrix;
220 329
221 if ($self == $HOVER && $self->{can_hover}) { 330 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 331 my ($x, $y) = @$self{qw(x y)};
223 332
224 glColor 0, 0, 1, 0.2; 333 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 334 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 335 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 336 glBegin GL_QUADS;
228 glVertex $x , $y; 337 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 338 glVertex $x + $self->{w}, $y;
230 glVertex $x + $self->{w}, $y + $self->{h}; 339 glVertex $x + $self->{w}, $y + $self->{h};
231 glVertex $x , $y + $self->{h}; 340 glVertex $x , $y + $self->{h};
232 glEnd; 341 glEnd;
233 glDisable GL_BLEND; 342 glDisable GL_BLEND;
234 } 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 }
235} 358}
236 359
237sub _draw { 360sub _draw {
238 my ($self) = @_; 361 my ($self) = @_;
239 362
240 warn "no draw defined for $self\n"; 363 warn "no draw defined for $self\n";
241} 364}
242 365
243sub find_widget { 366sub find_widget {
244 my ($self, $x, $y) = @_; 367 my ($self, $x, $y) = @_;
368
369 return () unless $self->{can_events};
245 370
246 return $self 371 return $self
247 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 372 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
248 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 373 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
249 374
254 my ($self, $parent) = @_; 379 my ($self, $parent) = @_;
255 380
256 Scalar::Util::weaken ($self->{parent} = $parent); 381 Scalar::Util::weaken ($self->{parent} = $parent);
257} 382}
258 383
384sub check_size {
385 my ($self) = @_;
386
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 }
408}
409
259sub update { 410sub update {
260 my ($self) = @_; 411 my ($self) = @_;
261 412
262 $self->{parent}->update 413 $self->{parent}->update
263 if $self->{parent}; 414 if $self->{parent};
264} 415}
265 416
266sub connect { 417sub connect {
267 my ($self, $signal, $cb) = @_; 418 my ($self, $signal, $cb) = @_;
268 419
269 push @{ $self->{cb}{$signal} }, $cb; 420 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 421}
271 422
272sub emit { 423sub emit {
273 my ($self, $signal, @args) = @_; 424 my ($self, $signal, @args) = @_;
274 425
275 $_->($self, @args) 426 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
276 for @{$self->{cb}{$signal} || []};
277} 427}
278 428
279sub DESTROY { 429sub DESTROY {
280 my ($self) = @_; 430 my ($self) = @_;
281 431
287package CFClient::UI::DrawBG; 437package CFClient::UI::DrawBG;
288 438
289our @ISA = CFClient::UI::Base::; 439our @ISA = CFClient::UI::Base::;
290 440
291use strict; 441use strict;
292use SDL::OpenGL; 442use CFClient::OpenGL;
293 443
294sub new { 444sub new {
295 my $class = shift; 445 my $class = shift;
296 446
297 # range [value, low, high, page] 447 # range [value, low, high, page]
326 476
327package CFClient::UI::Empty; 477package CFClient::UI::Empty;
328 478
329our @ISA = CFClient::UI::Base::; 479our @ISA = CFClient::UI::Base::;
330 480
481sub new {
482 my ($class, %arg) = @_;
483 $class->SUPER::new (can_events => 0, %arg);
484}
485
331sub size_request { 486sub size_request {
332 (0, 0) 487 (0, 0)
333} 488}
334 489
335sub draw { } 490sub draw { }
343sub new { 498sub new {
344 my ($class, %arg) = @_; 499 my ($class, %arg) = @_;
345 500
346 my $children = delete $arg{children} || []; 501 my $children = delete $arg{children} || [];
347 502
348 my $self = $class->SUPER::new (children => [], %arg); 503 my $self = $class->SUPER::new (
504 children => [],
505 can_events => 0,
506 %arg,
507 );
349 $self->add ($_) for @$children; 508 $self->add ($_) for @$children;
350 509
351 $self 510 $self
352} 511}
353 512
361 $self->{children} = [ 520 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 521 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 522 @{$self->{children}}, $child
364 ]; 523 ];
365 524
366 $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;
367 $self->update; 541 $self->update;
368} 542}
369 543
370sub remove { 544sub clear {
371 my ($self, $widget) = @_; 545 my ($self) = @_;
372 546
373 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 547 my $children = delete $self->{children};
548 $self->{children} = [];
374 549
375 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 550 for (@$children) {
551 delete $_->{parent};
552 $_->hide;
553 }
376} 554}
377 555
378sub find_widget { 556sub find_widget {
379 my ($self, $x, $y) = @_; 557 my ($self, $x, $y) = @_;
380 558
410 588
411 $class->SUPER::new (children => [$child], %arg) 589 $class->SUPER::new (children => [$child], %arg)
412} 590}
413 591
414sub add { 592sub add {
415 my ($self, $widget) = @_; 593 my ($self, $child) = @_;
416 594
417 $self->{children} = []; 595 $self->{children} = [];
418 596
419 $self->SUPER::add ($widget); 597 $self->SUPER::add ($child);
420} 598}
421 599
422sub remove { 600sub remove {
423 my ($self, $widget) = @_; 601 my ($self, $widget) = @_;
424 602
433sub size_request { 611sub size_request {
434 $_[0]{children}[0]->size_request 612 $_[0]{children}[0]->size_request
435} 613}
436 614
437sub size_allocate { 615sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 616 my ($self, $w, $h) = @_;
439 617
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 618 $self->{children}[0]->configure (0, 0, $w, $h);
443} 619}
444 620
445############################################################################# 621#############################################################################
446 622
447package CFClient::UI::Window; 623package CFClient::UI::Window;
448 624
449our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
450 626
451use SDL::OpenGL; 627use CFClient::OpenGL;
452 628
453sub new { 629sub new {
454 my ($class, %arg) = @_; 630 my ($class, %arg) = @_;
455 631
456 my $self = $class->SUPER::new (%arg); 632 my $self = $class->SUPER::new (%arg);
473 $self->child->draw; 649 $self->child->draw;
474 }; 650 };
475} 651}
476 652
477sub size_allocate { 653sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
479 655
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 656 $self->child->configure (0, 0, $w, $h);
483 657
484 $self->render_chld; 658 $self->render_chld;
485} 659}
486 660
487sub _draw { 661sub _draw {
503 glDisable GL_TEXTURE_2D; 677 glDisable GL_TEXTURE_2D;
504} 678}
505 679
506############################################################################# 680#############################################################################
507 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
508package CFClient::UI::Frame; 706package CFClient::UI::Frame;
509 707
510our @ISA = CFClient::UI::Bin::; 708our @ISA = CFClient::UI::Bin::;
511 709
512use SDL::OpenGL; 710use CFClient::OpenGL;
513
514sub size_request {
515 my ($self) = @_;
516 my $chld = $self->child
517 or return (0, 0);
518
519 $chld->move (2, 2);
520
521 map { $_ + 4 } $chld->size_request;
522}
523
524sub size_allocate {
525 my ($self, $x, $y, $w, $h) = @_;
526
527 $self->_size_allocate ($x, $y, $w, $h) or return;
528
529 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
530}
531
532sub _draw {
533 my ($self) = @_;
534
535 my $chld = $self->child;
536
537 my ($w, $h) = $chld->size_request;
538
539 glBegin GL_QUADS;
540 glColor 0, 0, 0;
541 glVertex 0 , 0;
542 glVertex 0 , $h + 4;
543 glVertex $w + 4 , $h + 4;
544 glVertex $w + 4 , 0;
545 glEnd;
546
547 $chld->draw;
548}
549
550#############################################################################
551
552package CFClient::UI::FancyFrame;
553
554our @ISA = CFClient::UI::Bin::;
555
556use SDL::OpenGL;
557
558my @tex =
559 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
560 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
561 711
562sub new { 712sub new {
563 my $class = shift; 713 my $class = shift;
564 714
565 # TODO: user_x, user_y, overwrite moveto?
566
567 $class->SUPER::new ( 715 my $self = $class->SUPER::new (
568 bg => [1, 1, 1, 1], 716 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 717 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 718 border => 0.8,
571 @_ 719 @_
720 );
721
722 $self
723}
724
725sub _draw {
726 my ($self) = @_;
727
728 my ($w, $h) = ($self->{w}, $self->{h});
729
730 glEnable GL_BLEND;
731 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
732 glEnable GL_TEXTURE_2D;
733 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
734
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,
572 ) 795 )
573} 796}
574 797
575sub size_request {
576 my ($self) = @_;
577
578 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
579
580 my ($w, $h) = $self->SUPER::size_request;
581
582 (
583 $w + $self->{border} * 2,
584 $h + $self->{border} * 2,
585 )
586}
587
588sub size_allocate { 798sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 799 my ($self, $w, $h) = @_;
590 800
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 801 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 802 $w -= List::Util::max 0, $self->border * 2;
595 803
804 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
805 if $self->{title};
806
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 807 $self->child->configure ($self->border, $self->border, $w, $h);
597} 808}
598 809
599sub button_down { 810sub button_down {
600 my ($self, $ev, $x, $y) = @_; 811 my ($self, $ev, $x, $y) = @_;
601 812
813 my $border = $self->border;
814
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 815 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 816 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 817
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 818 my ($ox, $oy) = ($ev->{x}, $ev->{y});
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 819 my ($bw, $bh) = ($self->{w}, $self->{h});
607 820
608 $self->{motion} = sub { 821 $self->{motion} = sub {
609 my ($ev, $x, $y) = @_; 822 my ($ev, $x, $y) = @_;
610 823
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 824 ($x, $y) = ($ev->{x}, $ev->{y});
612 825
613 $self->{user_w} = $bw + $x - $ox; 826 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 827 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 828 $self->check_size;
616 }; 829 };
617 830
618 } elsif ($x >= 0 && $x < $self->{w} 831 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 832 && $y >= 0 && $y < $border) {
620 833
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 834 my ($ox, $oy) = ($ev->{x}, $ev->{y});
622 my ($bx, $by) = ($self->{x}, $self->{y}); 835 my ($bx, $by) = ($self->{x}, $self->{y});
623 836
624 $self->{motion} = sub { 837 $self->{motion} = sub {
625 my ($ev, $x, $y) = @_; 838 my ($ev, $x, $y) = @_;
626 839
627 ($x, $y) = ($ev->motion_x, $ev->motion_y); 840 ($x, $y) = ($ev->{x}, $ev->{y});
628 841
629 $self->move ($bx + $x - $ox, $by + $y - $oy); 842 $self->move ($bx + $x - $ox, $by + $y - $oy);
630 $self->update; 843 $self->update;
631 }; 844 };
632 } 845 }
653 glEnable GL_BLEND; 866 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 867 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 868 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 869 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 870
871 my $border = $self->border;
872
658 glColor @{ $self->{border_bg} }; 873 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 874 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 875 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 877 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 878
664 my $bg = $tex[0]; 879 my $bg = $tex[0];
665 880
666 # TODO: repeat texture not scale 881 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 882 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 885 glColor @{ $self->{bg} };
671 886
672 $bg->{s} = $rep_x; 887 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 888 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 889 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 890 $bg->draw_quad ($border, $border, $cw, $ch);
676 891
677 glDisable GL_TEXTURE_2D; 892 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 893 glDisable GL_BLEND;
679 894
895 $self->{title}->draw if $self->{title};
680 $self->child->draw; 896 $self->child->draw;
681} 897}
682 898
683############################################################################# 899#############################################################################
684 900
686 902
687our @ISA = CFClient::UI::Base::; 903our @ISA = CFClient::UI::Base::;
688 904
689use List::Util qw(max sum); 905use List::Util qw(max sum);
690 906
691use SDL::OpenGL; 907use CFClient::OpenGL;
692 908
693sub new { 909sub new {
694 my $class = shift; 910 my $class = shift;
695 911
696 $class->SUPER::new ( 912 $class->SUPER::new (
703 my ($self, $x, $y, $child) = @_; 919 my ($self, $x, $y, $child) = @_;
704 920
705 $child->set_parent ($self); 921 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 922 $self->{children}[$y][$x] = $child;
707 923
708 $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
709 $self->update; 943 $self->update;
710} 944}
711 945
712sub get_wh { 946sub get_wh {
713 my ($self) = @_; 947 my ($self) = @_;
719 or next; 953 or next;
720 954
721 for my $x (0 .. $#$row) { 955 for my $x (0 .. $#$row) {
722 my $widget = $row->[$x] 956 my $widget = $row->[$x]
723 or next; 957 or next;
724 my ($w, $h) = $widget->size_request; 958 my ($w, $h) = @$widget{qw(req_w req_h)};
725 959
726 $w[$x] = max $w[$x], $w; 960 $w[$x] = max $w[$x], $w;
727 $h[$y] = max $h[$y], $h; 961 $h[$y] = max $h[$y], $h;
728 } 962 }
729 } 963 }
741 (sum @$hs), 975 (sum @$hs),
742 ) 976 )
743} 977}
744 978
745sub size_allocate { 979sub size_allocate {
746 my ($self, $x, $y, $w, $h) = @_; 980 my ($self, $w, $h) = @_;
747
748 $self->_size_allocate ($x, $y, $w, $h) or return;
749 981
750 my ($ws, $hs) = $self->get_wh; 982 my ($ws, $hs) = $self->get_wh;
751 983
752 my $req_w = sum @$ws; 984 my $req_w = sum @$ws;
753 my $req_h = sum @$hs; 985 my $req_h = sum @$hs;
775 1007
776 for my $c (0 .. $#$row) { 1008 for my $c (0 .. $#$row) {
777 my $col_w = $ws->[$c]; 1009 my $col_w = $ws->[$c];
778 1010
779 if (my $widget = $row->[$c]) { 1011 if (my $widget = $row->[$c]) {
780 $widget->size_allocate ($x, $y, $col_w, $row_h); 1012 $widget->configure ($x, $y, $col_w, $row_h);
781 } 1013 }
782 1014
783 $x += $col_w; 1015 $x += $col_w;
784 } 1016 }
785 1017
830 (List::Util::max map $_->[1], @alloc), 1062 (List::Util::max map $_->[1], @alloc),
831 ) 1063 )
832} 1064}
833 1065
834sub size_allocate { 1066sub size_allocate {
835 my ($self, $x, $y, $w, $h) = @_; 1067 my ($self, $w, $h) = @_;
836
837 $self->_size_allocate ($x, $y, $w, $h);
838 1068
839 ($h, $w) = ($w, $h); 1069 ($h, $w) = ($w, $h);
840 1070
841 my $children = $self->{children}; 1071 my $children = $self->{children};
842 1072
843 my @h = map +($_->size_request)[0], @$children; 1073 my @h = map $_->{req_w}, @$children;
844 1074
845 my $req_h = List::Util::sum @h; 1075 my $req_h = List::Util::sum @h;
846 1076
847 if ($req_h > $h) { 1077 if ($req_h > $h) {
848 # ah well, not enough space 1078 # ah well, not enough space
864 1094
865 my $y = 0; 1095 my $y = 0;
866 for (0 .. $#$children) { 1096 for (0 .. $#$children) {
867 my $child = $children->[$_]; 1097 my $child = $children->[$_];
868 my $h = $h[$_]; 1098 my $h = $h[$_];
869 $child->size_allocate ($y, 0, $h, $w); 1099 $child->configure ($y, 0, $h, $w);
870 1100
871 $y += $h; 1101 $y += $h;
872 } 1102 }
1103
1104 1
873} 1105}
874 1106
875############################################################################# 1107#############################################################################
876 1108
877package CFClient::UI::VBox; 1109package CFClient::UI::VBox;
890 (List::Util::sum map $_->[1], @alloc), 1122 (List::Util::sum map $_->[1], @alloc),
891 ) 1123 )
892} 1124}
893 1125
894sub size_allocate { 1126sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 1127 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h);
898 1128
899 my $children = $self->{children}; 1129 my $children = $self->{children};
900 1130
901 my @h = map +($_->size_request)[1], @$children; 1131 my @h = map $_->{req_h}, @$children;
902 1132
903 my $req_h = List::Util::sum @h; 1133 my $req_h = List::Util::sum @h;
904 1134
905 if ($req_h > $h) { 1135 if ($req_h > $h) {
906 # ah well, not enough space 1136 # ah well, not enough space
920 1150
921 my $y = 0; 1151 my $y = 0;
922 for (0 .. $#$children) { 1152 for (0 .. $#$children) {
923 my $child = $children->[$_]; 1153 my $child = $children->[$_];
924 my $h = $h[$_]; 1154 my $h = $h[$_];
925 $child->size_allocate (0, $y, $w, $h); 1155 $child->configure (0, $y, $w, $h);
926 1156
927 $y += $h; 1157 $y += $h;
928 } 1158 }
1159
1160 1
929} 1161}
930 1162
931############################################################################# 1163#############################################################################
932 1164
933package CFClient::UI::Label; 1165package CFClient::UI::Label;
934 1166
935our @ISA = CFClient::UI::Base::; 1167our @ISA = CFClient::UI::Base::;
936 1168
937use SDL::OpenGL; 1169use CFClient::OpenGL;
938 1170
939sub new { 1171sub new {
940 my ($class, %arg) = @_; 1172 my ($class, %arg) = @_;
941 1173
942 my $self = $class->SUPER::new ( 1174 my $self = $class->SUPER::new (
943 fg => [1, 1, 1], 1175 fg => [1, 1, 1],
944 fontsize => $::FONTSIZE, 1176 #font => default_font
1177 fontsize => 1,
945 text => "", 1178 text => "",
946 align => -1, 1179 align => -1,
1180 valign => -1,
947 padding => 2, 1181 padding => 2,
948 layout => new CFClient::Layout, 1182 layout => new CFClient::Layout,
1183 can_events => 0,
949 %arg 1184 %arg
950 ); 1185 );
951 1186
952 $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};
953 1195
954 $self 1196 $self
955} 1197}
956 1198
957sub escape_text { 1199sub escape {
958 local $_ = $_[1]; 1200 local $_ = $_[1];
959 1201
960 s/&/&amp;/g; 1202 s/&/&amp;/g;
961 s/>/&gt;/g; 1203 s/>/&gt;/g;
962 s/</&lt;/g; 1204 s/</&lt;/g;
965} 1207}
966 1208
967sub set_text { 1209sub set_text {
968 my ($self, $text) = @_; 1210 my ($self, $text) = @_;
969 1211
970 $self->{text} = $text;
971 $self->{layout}->set_markup ($text); 1212 $self->{layout}->set_text ($text);
972 1213
973 delete $self->{texture}; 1214 delete $self->{texture};
974# $self->{w} = $self->{h} = -1; 1215 $self->check_size;
975 $self->update; 1216 $self->update;
976} 1217}
977 1218
978sub get_text { 1219sub set_markup {
979 my ($self, $text) = @_; 1220 my ($self, $markup) = @_;
980 1221
981 $self->{text} 1222 $self->{layout}->set_markup ($markup);
1223
1224 delete $self->{texture};
1225 $self->check_size;
1226 $self->update;
982} 1227}
983 1228
984sub size_request { 1229sub size_request {
985 my ($self) = @_; 1230 my ($self) = @_;
986 1231
987 $self->{layout}->set_width; 1232 $self->{layout}->set_font ($self->{font}) if $self->{font};
1233 $self->{layout}->set_width ($self->{max_w} || -1);
988 $self->{layout}->set_height ($self->{fontsize}); 1234 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1235
989 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 }
990 1247
991 ( 1248 (
992 $w + $self->{padding} * 2, 1249 $w + $self->{padding} * 2,
993 $h + $self->{padding} * 2, 1250 $h + $self->{padding} * 2,
994 ) 1251 )
995} 1252}
996 1253
997sub size_allocate { 1254sub size_allocate {
998 my ($self, $x, $y, $w, $h) = @_; 1255 my ($self, $w, $h) = @_;
999
1000 $self->_size_allocate ($x, $y, $w, $h) or return;
1001 1256
1002 delete $self->{texture}; 1257 delete $self->{texture};
1003} 1258}
1004 1259
1005sub update { 1260sub set_fontsize {
1006 my ($self) = @_; 1261 my ($self, $fontsize) = @_;
1007 1262
1263 $self->{fontsize} = $fontsize;
1008 delete $self->{texture}; 1264 delete $self->{texture};
1265 $self->check_size;
1009 $self->SUPER::update; 1266 $self->update;
1010} 1267}
1011 1268
1012sub _draw { 1269sub _draw {
1013 my ($self) = @_; 1270 my ($self) = @_;
1014 1271
1015 my $tex = $self->{texture} ||= do { 1272 my $tex = $self->{texture} ||= do {
1273 $self->{layout}->set_font ($self->{font}) if $self->{font};
1016 $self->{layout}->set_width ($self->{w}); 1274 $self->{layout}->set_width ($self->{w});
1017 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1275 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1018 new_from_layout CFClient::Texture $self->{layout} 1276 new_from_layout CFClient::Texture $self->{layout}
1019 }; 1277 };
1020 1278
1021 glEnable GL_BLEND; 1279 glEnable GL_BLEND;
1022 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1280 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1023 glEnable GL_TEXTURE_2D; 1281 glEnable GL_TEXTURE_2D;
1024 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1282 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1025 1283
1026 glColor @{$self->{fg}}; 1284 glColor @{$self->{fg}};
1027 1285
1028 my $x = 1286 $self->{ox} = int (
1029 $self->{align} < 0 ? $self->{padding} 1287 $self->{align} < 0 ? $self->{padding}
1030 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1288 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1031 : ($self->{w} - $tex->{w}) * 0.5; 1289 : ($self->{w} - $tex->{w}) * 0.5
1290 );
1032 1291
1033 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1292 $self->{oy} = int (
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});
1034 1299
1035 glDisable GL_TEXTURE_2D; 1300 glDisable GL_TEXTURE_2D;
1036 glDisable GL_BLEND; 1301 glDisable GL_BLEND;
1037} 1302}
1038 1303
1039############################################################################# 1304#############################################################################
1040 1305
1041package CFClient::UI::Entry; 1306package CFClient::UI::EntryBase;
1042 1307
1043our @ISA = CFClient::UI::Label::; 1308our @ISA = CFClient::UI::Label::;
1044 1309
1045use SDL; 1310use CFClient::OpenGL;
1046use SDL::OpenGL;
1047 1311
1048sub new { 1312sub new {
1049 my $class = shift; 1313 my $class = shift;
1050 1314
1051 $class->SUPER::new ( 1315 $class->SUPER::new (
1052 fg => [1, 1, 1], 1316 fg => [1, 1, 1],
1053 bg => [0, 0, 0, 0.2], 1317 bg => [0, 0, 0, 0.2],
1054 active_bg => [1, 1, 1, 0.5], 1318 active_bg => [1, 1, 1, 0.5],
1055 active_fg => [0, 0, 0], 1319 active_fg => [0, 0, 0],
1056 can_hover => 1, 1320 can_hover => 1,
1057 can_focus => 1, 1321 can_focus => 1,
1322 valign => 0,
1323 can_events => 1,
1058 @_ 1324 @_
1059 ) 1325 )
1060} 1326}
1061 1327
1062sub _set_text { 1328sub _set_text {
1063 my ($self, $text) = @_; 1329 my ($self, $text) = @_;
1064 1330
1065 my $old_text = $self->{text}; 1331 delete $self->{cur_h};
1332
1333 return if $self->{text} eq $text;
1334
1335 delete $self->{texture};
1066 1336
1067 $self->{last_activity} = $::NOW; 1337 $self->{last_activity} = $::NOW;
1068
1069 $self->{text} = $text; 1338 $self->{text} = $text;
1070 $self->{layout}->set_width ($self->{w});
1071 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1072 1339
1073 $text =~ s/./*/g if $self->{hidden}; 1340 $text =~ s/./*/g if $self->{hidden};
1341 $self->{layout}->set_text ("$text ");
1074 1342
1075 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1343 $self->emit (changed => $self->{text});
1344}
1076 1345
1077 $text = substr $text, 0, $self->{cursor}; 1346sub get_text {
1078 utf8::encode $text; 1347 $_[0]{text}
1079
1080 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1081
1082 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1083 if $old_text ne $self->{text};
1084} 1348}
1085 1349
1086sub size_request { 1350sub size_request {
1087 my ($self) = @_; 1351 my ($self) = @_;
1088 1352
1090 1354
1091 ($w + 1, $h) # add 1 for cursor 1355 ($w + 1, $h) # add 1 for cursor
1092} 1356}
1093 1357
1094sub size_allocate { 1358sub size_allocate {
1095 my ($self, $x, $y, $w, $h) = @_; 1359 my ($self, $w, $h) = @_;
1096 1360
1097 $self->SUPER::size_allocate ($x, $y, $w, $h); 1361 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1098
1099 $self->_set_text ($self->{text});
1100} 1362}
1101 1363
1102sub set_text { 1364sub set_text {
1103 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1104 1366
1108} 1370}
1109 1371
1110sub key_down { 1372sub key_down {
1111 my ($self, $ev) = @_; 1373 my ($self, $ev) = @_;
1112 1374
1113 my $mod = $ev->key_mod; 1375 my $mod = $ev->{mod};
1114 my $sym = $ev->key_sym; 1376 my $sym = $ev->{sym};
1115
1116 my $uni = $ev->key_unicode; 1377 my $uni = $ev->{unicode};
1117 1378
1118 my $text = $self->get_text; 1379 my $text = $self->get_text;
1119 1380
1120 if ($sym == SDLK_BACKSPACE) { 1381 if ($sym == 8) {
1121 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1382 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1122 } elsif ($sym == SDLK_DELETE) { 1383 } elsif ($sym == 127) {
1123 substr $text, $self->{cursor}, 1, ""; 1384 substr $text, $self->{cursor}, 1, "";
1124 } elsif ($sym == SDLK_LEFT) { 1385 } elsif ($sym == CFClient::SDLK_LEFT) {
1125 --$self->{cursor} if $self->{cursor}; 1386 --$self->{cursor} if $self->{cursor};
1126 } elsif ($sym == SDLK_RIGHT) { 1387 } elsif ($sym == CFClient::SDLK_RIGHT) {
1127 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1388 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1128 } elsif ($sym == SDLK_HOME) { 1389 } elsif ($sym == CFClient::SDLK_HOME) {
1129 $self->{cursor} = 0; 1390 $self->{cursor} = 0;
1130 } elsif ($sym == SDLK_END) { 1391 } elsif ($sym == CFClient::SDLK_END) {
1131 $self->{cursor} = length $text; 1392 $self->{cursor} = length $text;
1132 } elsif ($sym == SDLK_ESCAPE) { 1393 } elsif ($sym == 27) {
1133 $self->emit ('escape'); 1394 $self->emit ('escape');
1134 } elsif ($uni) { 1395 } elsif ($uni) {
1135 substr $text, $self->{cursor}++, 0, chr $uni; 1396 substr $text, $self->{cursor}++, 0, chr $uni;
1136 } 1397 }
1137 1398
1192 1453
1193 $self->SUPER::_draw; 1454 $self->SUPER::_draw;
1194 1455
1195 #TODO: force update every cursor change :( 1456 #TODO: force update every cursor change :(
1196 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
1197 glColor @{$self->{fg}}; 1466 glColor @{$self->{fg}};
1198 glBegin GL_LINES; 1467 glBegin GL_LINES;
1199 glVertex $self->{cur_x}, $self->{cur_y};
1200 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};
1201 glEnd; 1470 glEnd;
1202 } 1471 }
1203} 1472}
1204 1473
1205package CFClient::UI::LineEntry; 1474package CFClient::UI::Entry;
1206 1475
1207our @ISA = CFClient::UI::Entry::; 1476our @ISA = CFClient::UI::EntryBase::;
1208 1477
1209use SDL; 1478use CFClient::OpenGL;
1210use SDL::OpenGL;
1211 1479
1212sub key_down { 1480sub key_down {
1213 my ($self, $ev) = @_; 1481 my ($self, $ev) = @_;
1214 1482
1215 my $sym = $ev->key_sym; 1483 my $sym = $ev->{sym};
1216 1484
1217 if ($sym == SDLK_RETURN) { 1485 if ($sym == 13) {
1486 unshift @{$self->{history}},
1487 my $txt = $self->get_text;
1488 $self->{history_pointer} = -1;
1489 $self->{history_saveback} = '';
1218 $self->emit (activate => $self->get_text); 1490 $self->emit (activate => $txt);
1219 $self->update; 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 }
1220 1514
1221 } else { 1515 } else {
1222 $self->SUPER::key_down ($ev); 1516 $self->SUPER::key_down ($ev);
1223 } 1517 }
1224 1518
1228 1522
1229package CFClient::UI::Button; 1523package CFClient::UI::Button;
1230 1524
1231our @ISA = CFClient::UI::Label::; 1525our @ISA = CFClient::UI::Label::;
1232 1526
1233use SDL; 1527use CFClient::OpenGL;
1234use SDL::OpenGL;
1235 1528
1236my @tex = 1529my @tex =
1237 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1530 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1238 qw(b1_button_active.png); 1531 qw(b1_button_active.png);
1239 1532
1240sub new { 1533sub new {
1241 my $class = shift; 1534 my $class = shift;
1242 1535
1243 $class->SUPER::new ( 1536 $class->SUPER::new (
1244 padding => 4, 1537 padding => 4,
1245 fg => [1, 1, 1], 1538 fg => [1, 1, 1],
1246 bg => [1, 1, 1, 0.2], 1539 bg => [1, 1, 1, 0.2],
1247 active_fg => [1, 1, 0], 1540 active_fg => [0, 0, 1],
1248 can_hover => 1, 1541 can_hover => 1,
1542 align => 0,
1543 valign => 0,
1544 can_events => 1,
1249 @_ 1545 @_
1250 ) 1546 )
1251} 1547}
1252 1548
1253sub button_up { 1549sub button_up {
1261 1557
1262sub _draw { 1558sub _draw {
1263 my ($self) = @_; 1559 my ($self) = @_;
1264 1560
1265 local $self->{fg} = $self->{fg}; 1561 local $self->{fg} = $self->{fg};
1266 my $tex = $tex[0];
1267
1268 glEnable GL_BLEND;
1269 glEnable GL_TEXTURE_2D;
1270 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1271 1562
1272 if ($GRAB == $self) { 1563 if ($GRAB == $self) {
1273 $self->{fg} = $self->{active_fg}; 1564 $self->{fg} = $self->{active_fg};
1274 } 1565 }
1275 1566
1276 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;
1277 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;
1278 1572
1279 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1573 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1280 1574
1281 glDisable GL_TEXTURE_2D; 1575 glDisable GL_TEXTURE_2D;
1282 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1283 1577
1284 $self->SUPER::_draw; 1578 $self->SUPER::_draw;
1289package CFClient::UI::CheckBox; 1583package CFClient::UI::CheckBox;
1290 1584
1291our @ISA = CFClient::UI::DrawBG::; 1585our @ISA = CFClient::UI::DrawBG::;
1292 1586
1293my @tex = 1587my @tex =
1294 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1588 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1295 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1589 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1296 1590
1297use SDL; 1591use CFClient::OpenGL;
1298use SDL::OpenGL;
1299 1592
1300sub new { 1593sub new {
1301 my $class = shift; 1594 my $class = shift;
1302 1595
1303 $class->SUPER::new ( 1596 $class->SUPER::new (
1314 my ($self) = @_; 1607 my ($self) = @_;
1315 1608
1316 ($self->{padding} * 2 + 6) x 2 1609 ($self->{padding} * 2 + 6) x 2
1317} 1610}
1318 1611
1319sub size_allocate {
1320 my ($self, $x, $y, $w, $h) = @_;
1321
1322 $self->_size_allocate ($x, $y, $w, $h);
1323}
1324
1325sub button_down { 1612sub button_down {
1326 my ($self, $ev, $x, $y) = @_; 1613 my ($self, $ev, $x, $y) = @_;
1327 1614
1328 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1615 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1329 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1616 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1355 glDisable GL_BLEND; 1642 glDisable GL_BLEND;
1356} 1643}
1357 1644
1358############################################################################# 1645#############################################################################
1359 1646
1647package CFClient::UI::Image;
1648
1649our @ISA = CFClient::UI::Base::;
1650
1651use CFClient::OpenGL;
1652use Carp qw/confess/;
1653
1654our %loaded_images;
1655
1656sub new {
1657 my $class = shift;
1658
1659 my $self = $class->SUPER::new (can_events => 0, @_);
1660
1661 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1662
1663 $loaded_images{$self->{image}} ||=
1664 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1665
1666 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1667
1668 Scalar::Util::weaken $loaded_images{$self->{image}};
1669
1670 $self->{aspect} = $tex->{w} / $tex->{h};
1671
1672 $self
1673}
1674
1675sub size_request {
1676 my ($self) = @_;
1677
1678 ($self->{tex}->{w}, $self->{tex}->{h})
1679}
1680
1681sub _draw {
1682 my ($self) = @_;
1683
1684 my $tex = $self->{tex};
1685
1686 my ($w, $h) = ($self->{w}, $self->{h});
1687
1688 if ($self->{rot90}) {
1689 glRotate 90, 0, 0, 1;
1690 glTranslate 0, -$self->{w}, 0;
1691
1692 ($w, $h) = ($h, $w);
1693 }
1694
1695 glEnable GL_BLEND;
1696 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1697 glEnable GL_TEXTURE_2D;
1698 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1699
1700 $tex->draw_quad (0, 0, $w, $h);
1701
1702 glDisable GL_BLEND;
1703 glDisable GL_TEXTURE_2D;
1704}
1705
1706#############################################################################
1707
1708package CFClient::UI::VGauge;
1709
1710our @ISA = CFClient::UI::Base::;
1711
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
1360package CFClient::UI::Slider; 1883package CFClient::UI::Slider;
1361 1884
1362use strict; 1885use strict;
1363 1886
1364use SDL::OpenGL; 1887use CFClient::OpenGL;
1365 1888
1366our @ISA = CFClient::UI::DrawBG::; 1889our @ISA = CFClient::UI::DrawBG::;
1367 1890
1368my @tex = 1891my @tex =
1369 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1892 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1379 # TODO: calculations are off 1902 # TODO: calculations are off
1380 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1381 fg => [1, 1, 1], 1904 fg => [1, 1, 1],
1382 active_fg => [0, 0, 0], 1905 active_fg => [0, 0, 0],
1383 range => [0, 0, 100, 10], 1906 range => [0, 0, 100, 10],
1384 req_w => 40, 1907 req_w => 20,
1385 req_h => 13, 1908 req_h => 20,
1386 vertical => 0, 1909 vertical => 0,
1387 can_hover => 1, 1910 can_hover => 1,
1388 inner_pad => 5, 1911 inner_pad => 5,
1389 @_ 1912 @_
1390 ); 1913 );
1494 2017
1495package CFClient::UI::TextView; 2018package CFClient::UI::TextView;
1496 2019
1497our @ISA = CFClient::UI::HBox::; 2020our @ISA = CFClient::UI::HBox::;
1498 2021
1499use SDL::OpenGL; 2022use CFClient::OpenGL;
1500 2023
1501sub new { 2024sub new {
1502 my $class = shift; 2025 my $class = shift;
1503 2026
1504 my $self = $class->SUPER::new ( 2027 my $self = $class->SUPER::new (
1505 req_w => $::WIDTH / 6, 2028 fontsize => 1,
1506 req_h => $::HEIGHT / 6, 2029 can_events => 0,
1507 fontsize => $::FONTSIZE, 2030 #font => default_font
1508 @_, 2031 @_,
1509 2032
1510 layout => (new CFClient::Layout), 2033 layout => (new CFClient::Layout),
1511 par => [], 2034 par => [],
1512 height => 0, 2035 height => 0,
1513 children => [ 2036 children => [
1514 (new CFClient::UI::Empty expand => 1), 2037 (new CFClient::UI::Empty expand => 1),
1515 (new CFClient::UI::Slider vertical => 1), 2038 (new CFClient::UI::Slider vertical => 1),
1516 ], 2039 ],
1517 ); 2040 );
1518 2041
1533sub text_height { 2056sub text_height {
1534 my ($self, $text) = @_; 2057 my ($self, $text) = @_;
1535 2058
1536 my $layout = $self->{layout}; 2059 my $layout = $self->{layout};
1537 2060
1538 $layout->set_height ($self->{fontsize}); 2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1539 $layout->set_width ($self->{w}); 2062 $layout->set_width ($self->{w});
1540 $layout->set_text ($text); 2063 $layout->set_text ($text);
1541 2064
1542 ($layout->size)[1] 2065 ($layout->size)[1]
1543} 2066}
1547 2070
1548 $self->{need_reflow}++; 2071 $self->{need_reflow}++;
1549 $self->update; 2072 $self->update;
1550} 2073}
1551 2074
1552sub size_request {
1553 my ($self) = @_;
1554
1555 ($self->{req_w}, $self->{req_h})
1556}
1557
1558sub size_allocate { 2075sub size_allocate {
1559 my ($self, $x, $y, $w, $h) = @_; 2076 my ($self, $w, $h) = @_;
1560 2077
1561 $self->SUPER::size_allocate ($x, $y, $w, $h); 2078 $self->SUPER::size_allocate ($w, $h);
1562 2079
2080 $self->{layout}->set_font ($self->{font}) if $self->{font};
1563 $self->{layout}->set_height ($self->{fontsize}); 2081 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1564 $self->{layout}->set_width ($self->{w}); 2082 $self->{layout}->set_width ($self->{children}[0]{w});
1565 2083
1566 $self->reflow; 2084 $self->reflow;
1567} 2085}
1568 2086
1569sub add_paragraph { 2087sub add_paragraph {
1620 2138
1621 my $y = 0; 2139 my $y = 0;
1622 2140
1623 my $layout = $self->{layout}; 2141 my $layout = $self->{layout};
1624 2142
2143 $layout->set_font ($self->{font}) if $self->{font};
2144
1625 for my $par (@{$self->{par}}) { 2145 for my $par (@{$self->{par}}) {
1626 my $h = $par->[0]; 2146 my $h = $par->[0];
1627 2147
1628 if ($y0 < $y + $h && $y < $y1) { 2148 if ($y0 < $y + $h && $y < $y1) {
1629 $layout->set_text ($par->[2]); 2149 $layout->set_text ($par->[2]);
1646 my ($self) = @_; 2166 my ($self) = @_;
1647 2167
1648 if ($self->{texture}) { 2168 if ($self->{texture}) {
1649 glEnable GL_TEXTURE_2D; 2169 glEnable GL_TEXTURE_2D;
1650 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2171 glColor 1, 1, 1, 1;
1651 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1652 glDisable GL_TEXTURE_2D; 2173 glDisable GL_TEXTURE_2D;
1653 } 2174 }
1654 2175
1655 $self->{children}[1]->draw; 2176 $self->{children}[1]->draw;
1656 2177
1657} 2178}
1658 2179
1659############################################################################# 2180#############################################################################
1660 2181
1661package CFClient::UI::MapWidget;
1662
1663use strict;
1664
1665use List::Util qw(min max);
1666
1667use SDL;
1668use SDL::OpenGL;
1669
1670our @ISA = CFClient::UI::Base::;
1671
1672sub new {
1673 my $class = shift;
1674
1675 $class->SUPER::new (
1676 z => -1,
1677 can_focus => 1,
1678 list => (glGenLists 1),
1679 @_
1680 )
1681}
1682
1683sub key_down {
1684 print "MAPKEYDOWN\n";
1685}
1686
1687sub key_up {
1688}
1689
1690sub button_down {
1691 my ($self, $ev, $x, $y) = @_;
1692
1693 $self->focus_in;
1694
1695 if ($ev->button == 2) {
1696 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1697 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1698
1699 $self->{motion} = sub {
1700 my ($ev, $x, $y) = @_;
1701
1702 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1703
1704 $::CFG->{map_shift_x} = $bw + $x - $ox;
1705 $::CFG->{map_shift_y} = $bh + $y - $oy;
1706
1707 $self->update;
1708 };
1709 }
1710}
1711
1712sub button_up {
1713 my ($self, $ev, $x, $y) = @_;
1714
1715 delete $self->{motion};
1716}
1717
1718sub mouse_motion {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->{motion}->($ev, $x, $y) if $self->{motion};
1722}
1723
1724sub size_request {
1725 (
1726 1 + 32 * int $::WIDTH / 32,
1727 1 + 32 * int $::HEIGHT / 32,
1728 )
1729}
1730
1731sub update {
1732 my ($self) = @_;
1733
1734 $self->{need_update} = 1;
1735 $self->SUPER::update;
1736}
1737
1738sub draw {
1739 my ($self) = @_;
1740
1741 if (delete $self->{need_update}) {
1742 glNewList $self->{list}, GL_COMPILE;
1743
1744 if ($::MAP) {
1745 my $sw = int $::WIDTH / 32;
1746 my $sh = int $::HEIGHT / 32;
1747
1748 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1749 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1750
1751 glTranslate $sx0 - 32, $sy0 - 32, 0;
1752
1753 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1754
1755 if ($::CFG->{fow_enable}) {
1756 if ($::CFG->{fow_smooth}) { # smooth fog of war
1757 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1758 glConvolutionFilter2D
1759 GL_CONVOLUTION_2D,
1760 GL_ALPHA,
1761 3, 3,
1762 GL_ALPHA, GL_FLOAT,
1763 pack "f*",
1764 0.1, 0.1, 0.1,
1765 0.1, 0.2, 0.1,
1766 0.1, 0.1, 0.1,
1767 ;
1768 glEnable GL_CONVOLUTION_2D;
1769 }
1770
1771 my $tex = new CFClient::Texture
1772 w => $w,
1773 h => $h,
1774 data => $data,
1775 internalformat => GL_ALPHA,
1776 format => GL_ALPHA;
1777
1778 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1779
1780 glEnable GL_BLEND;
1781 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1782 glEnable GL_TEXTURE_2D;
1783 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1784
1785 glColor +($::CFG->{fow_intensity}) x 3, 1;
1786 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1787
1788 glDisable GL_TEXTURE_2D;
1789 glDisable GL_BLEND;
1790 }
1791
1792 # HACK BEGIN
1793 {
1794 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1795 my ($w, $h) = (250, 250);
1796
1797 glEnable GL_BLEND;
1798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1799 glEnable GL_TEXTURE_2D;
1800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1801
1802 CFClient::Texture->new (
1803 w => $w,
1804 h => $h,
1805 data => $::MAP->mapmap ($w, $h),
1806 type => GL_UNSIGNED_INT_8_8_8_8_REV
1807 )->draw_quad (100, 100);
1808
1809 glDisable GL_TEXTURE_2D;
1810 glDisable GL_BLEND;
1811 }
1812 # HACK END
1813 }
1814
1815 glEndList;
1816 }
1817
1818 glPushMatrix;
1819 glCallList $self->{list};
1820 glPopMatrix;
1821
1822 if ($FOCUS != $self) {
1823 glColor 64/255, 64/255, 64/255;
1824 glLogicOp GL_AND;
1825 glEnable GL_COLOR_LOGIC_OP;
1826 glBegin GL_QUADS;
1827 glVertex 0, 0;
1828 glVertex 0, $::HEIGHT;
1829 glVertex $::WIDTH, $::HEIGHT;
1830 glVertex $::WIDTH, 0;
1831 glEnd;
1832 glDisable GL_COLOR_LOGIC_OP;
1833 }
1834}
1835
1836my %DIR = (
1837 SDLK_KP8, [1, "north"],
1838 SDLK_KP9, [2, "northeast"],
1839 SDLK_KP6, [3, "east"],
1840 SDLK_KP3, [4, "southeast"],
1841 SDLK_KP2, [5, "south"],
1842 SDLK_KP1, [6, "southwest"],
1843 SDLK_KP4, [7, "west"],
1844 SDLK_KP7, [8, "northwest"],
1845
1846 SDLK_UP, [1, "north"],
1847 SDLK_RIGHT, [3, "east"],
1848 SDLK_DOWN, [5, "south"],
1849 SDLK_LEFT, [7, "west"],
1850);
1851
1852sub key_down {
1853 my ($self, $ev) = @_;
1854
1855 my $mod = $ev->key_mod;
1856 my $sym = $ev->key_sym;
1857
1858 if ($sym == SDLK_KP5) {
1859 $::CONN->user_send ("command stay fire");
1860 } elsif ($sym == SDLK_a) {
1861 $::CONN->user_send ("command apply");
1862 } elsif ($sym == SDLK_QUOTE) {
1863 $self->emit ('activate_console');
1864 } elsif ($sym == SDLK_SLASH) {
1865 $self->emit ('activate_console' => '/');
1866 } elsif (exists $DIR{$sym}) {
1867 if ($mod & KMOD_SHIFT) {
1868 $self->{shft}++;
1869 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1870 } elsif ($mod & KMOD_CTRL) {
1871 $self->{ctrl}++;
1872 $::CONN->user_send ("command run $DIR{$sym}[0]");
1873 } else {
1874 $::CONN->user_send ("command $DIR{$sym}[1]");
1875 }
1876 }
1877}
1878
1879sub key_up {
1880 my ($self, $ev) = @_;
1881
1882 my $mod = $ev->key_mod;
1883 my $sym = $ev->key_sym;
1884
1885 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1886 $::CONN->user_send ("command fire_stop");
1887 }
1888 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1889 $::CONN->user_send ("command run_stop");
1890 }
1891}
1892
1893#############################################################################
1894
1895package CFClient::UI::Animator; 2182package CFClient::UI::Animator;
1896 2183
1897use SDL::OpenGL; 2184use CFClient::OpenGL;
1898 2185
1899our @ISA = CFClient::UI::Bin::; 2186our @ISA = CFClient::UI::Bin::;
1900 2187
1901sub moveto { 2188sub moveto {
1902 my ($self, $x, $y) = @_; 2189 my ($self, $x, $y) = @_;
1940 2227
1941sub new { 2228sub new {
1942 my $class = shift; 2229 my $class = shift;
1943 2230
1944 my $self = $class->SUPER::new ( 2231 my $self = $class->SUPER::new (
1945 state => 0, 2232 state => 0,
1946 connect_activate => \&toggle_flopper, 2233 connect_activate => \&toggle_flopper,
1947 @_ 2234 @_
1948 ); 2235 );
1949 2236
1950 if ($self->{state}) { 2237 if ($self->{state}) {
1971 $self->emit (changed => $self->{state}); 2258 $self->emit (changed => $self->{state});
1972} 2259}
1973 2260
1974############################################################################# 2261#############################################################################
1975 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
1976package CFClient::UI::Root; 2372package CFClient::UI::Root;
1977 2373
1978our @ISA = CFClient::UI::Container::; 2374our @ISA = CFClient::UI::Container::;
1979 2375
1980use SDL::OpenGL; 2376use CFClient::OpenGL;
2377
2378sub check_size {
2379 my ($self) = @_;
2380
2381 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2382}
1981 2383
1982sub size_request { 2384sub size_request {
1983 ($::WIDTH, $::HEIGHT) 2385 ($::WIDTH, $::HEIGHT)
1984} 2386}
1985 2387
1986sub size_allocate { 2388sub configure {
1987 my ($self, $x, $y, $w, $h) = @_; 2389 my ($self, $x, $y, $w, $h) = @_;
1988 2390
1989 $self->_size_allocate ($x, $y, $w, $h); 2391 $self->SUPER::configure ($x, $y, $w, $h);
1990 2392
1991 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1992 for @{$self->{children}}; 2393 for my $child (@{$self->{children}}) {
2394 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2395
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 }
1993} 2400}
1994 2401
1995sub _topleft { 2402sub _topleft {
1996 my ($self, $x, $y) = @_; 2403 my ($self, $x, $y) = @_;
1997 2404
1999} 2406}
2000 2407
2001sub update { 2408sub update {
2002 my ($self) = @_; 2409 my ($self) = @_;
2003 2410
2004 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2411 $self->check_size;
2005 ::refresh (); 2412 ::refresh ();
2006} 2413}
2007 2414
2008sub add { 2415sub add {
2009 my ($self, $widget) = @_; 2416 my ($self, $child) = @_;
2010 2417
2418 # integerize window positions
2419 $child->{x} = int $child->{x};
2420 $child->{y} = int $child->{y};
2421
2011 $self->SUPER::add ($widget); 2422 $self->SUPER::add ($child);
2012
2013 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2014} 2423}
2015 2424
2016sub on_refresh { 2425sub on_refresh {
2017 my ($self, $id, $cb) = @_; 2426 my ($self, $id, $cb) = @_;
2018 2427
2042############################################################################# 2451#############################################################################
2043 2452
2044package CFClient::UI; 2453package CFClient::UI;
2045 2454
2046$ROOT = new CFClient::UI::Root; 2455$ROOT = new CFClient::UI::Root;
2456$TOOLTIP = new CFClient::UI::Tooltip;
2047 2457
20481 24581
2049 2459

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines