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.112 by root, Sat Apr 15 12:29:46 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 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->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 99
100 check_tooltip;
101 }
102
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 104}
72 105
73# convert position array to integers 106# convert position array to integers
74sub harmonize { 107sub harmonize {
75 my ($vals) = @_; 108 my ($vals) = @_;
76 109
77 my $rem = 0; 110 my $rem = 0;
78 111
79 for ($vals) { 112 for (@$vals) {
80 my $i = int $_ + $rem; 113 my $i = int $_ + $rem;
81 $rem += $_ - $i; 114 $rem += $_ - $i;
82 $_ = $i; 115 $_ = $i;
83 } 116 }
84} 117}
87 120
88package CFClient::UI::Base; 121package CFClient::UI::Base;
89 122
90use strict; 123use strict;
91 124
92use SDL::OpenGL; 125use CFClient::OpenGL;
93 126
94sub new { 127sub new {
95 my $class = shift; 128 my $class = shift;
96 129
97 my $self = bless { 130 my $self = bless {
98 x => 0, 131 x => 0,
99 y => 0, 132 y => 0,
100 z => 0, 133 z => 0,
101 w => -1, 134 can_events => 1,
102 h => -1,
103 @_ 135 @_
104 }, $class; 136 }, $class;
105 137
106 for (keys %$self) { 138 for (keys %$self) {
107 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
110 } 142 }
111 143
112 $self 144 $self
113} 145}
114 146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
154sub show {
155 my ($self) = @_;
156
157 return if $self->{parent};
158
159 $CFClient::UI::ROOT->add ($self);
160}
161
162sub hide {
163 my ($self) = @_;
164
165 return unless $self->{parent};
166
167 $self->{parent}->remove ($self);
168}
169
115sub move { 170sub move {
116 my ($self, $x, $y, $z) = @_; 171 my ($self, $x, $y, $z) = @_;
172
117 $self->{x} = $x; 173 $self->{x} = int $x;
118 $self->{y} = $y; 174 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
120}
121 176
122sub needs_redraw { 177 $self->update;
123 0 178}
179
180sub set_size {
181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
124} 187}
125 188
126sub size_request { 189sub size_request {
127 require Carp; 190 require Carp;
128 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
129} 192}
130 193
131sub _size_allocate { 194sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
133 196
197 if ($self->{aspect}) {
198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
200
201 # use alignment to adjust x, y
202
203 $x += int +($w - $w2) * 0.5;
204 $y += int +($h - $h2) * 0.5;
205
206 ($w, $h) = ($w2, $h2);
207 }
208
209 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 210 $self->{x} = $x;
135 $self->{y} = $y; 211 $self->{y} = $y;
212 $self->update;
213 }
136 214
137 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 216 $self->{w} = $w;
140 $self->{h} = $h; 217 $self->{h} = $h;
141 218
219 $self->size_allocate ($w, $h);
220 $self->update;
142 1 221 }
143} 222}
144 223
145sub size_allocate { 224sub size_allocate {
225 # nothing to be done
226}
227
228sub set_max_size {
146 my ($self, $x, $y, $w, $h) = @_; 229 my ($self, $w, $h) = @_;
147 230
148 $self->_size_allocate ($x, $y, $w, $h); 231 delete $self->{max_w}; $self->{max_w} = $w if $w;
232 delete $self->{max_h}; $self->{max_h} = $h if $h;
233}
234
235# return top left coordinates
236sub _topleft {
237 my ($self, $x, $y) = @_;
238
239 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
149} 240}
150 241
151# translate global coordinates to local coordinate system 242# translate global coordinates to local coordinate system
152sub translate { 243sub coord2local {
153 my ($self, $x, $y) = @_; 244 my ($self, $x, $y) = @_;
154 245
155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 246 my ($X, $Y) = $self->_topleft;
247 ($x - $X, $y - $Y)
248}
249
250# translate local coordinates to global coordinate system
251sub coord2global {
252 my ($self, $x, $y) = @_;
253
254 my ($X, $Y) = $self->_topleft;
255 ($x + $X, $y + $Y)
156} 256}
157 257
158sub focus_in { 258sub focus_in {
159 my ($self) = @_; 259 my ($self) = @_;
160 260
161 return if $FOCUS == $self; 261 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 262 return unless $self->{can_focus};
163 263
164 my $focus = $FOCUS; $FOCUS = $self; 264 my $focus = $FOCUS; $FOCUS = $self;
265
266 $self->emit (focus_in => $focus);
267
165 $focus->update if $focus; 268 $focus->update if $focus;
166 $FOCUS->update; 269 $FOCUS->update;
167} 270}
168 271
169sub focus_out { 272sub focus_out {
170 my ($self) = @_; 273 my ($self) = @_;
171 274
172 return unless $FOCUS == $self; 275 return unless $FOCUS == $self;
173 276
174 my $focus = $FOCUS; undef $FOCUS; 277 my $focus = $FOCUS; undef $FOCUS;
278
279 $self->emit (focus_out => $focus);
280
175 $focus->update if $focus; #? 281 $focus->update if $focus; #?
176} 282}
177 283
178sub mouse_motion { } 284sub mouse_motion { }
179sub button_up { } 285sub button_up { }
203 glPopMatrix; 309 glPopMatrix;
204 310
205 if ($self == $HOVER && $self->{can_hover}) { 311 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 312 my ($x, $y) = @$self{qw(x y)};
207 313
208 glColor 0, 0, 1, 0.2; 314 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 315 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 316 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 317 glBegin GL_QUADS;
212 glVertex $x , $y; 318 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 319 glVertex $x + $self->{w}, $y;
214 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
215 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
216 glEnd; 322 glEnd;
217 glDisable GL_BLEND; 323 glDisable GL_BLEND;
218 } 324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
219} 339}
220 340
221sub _draw { 341sub _draw {
222 my ($self) = @_; 342 my ($self) = @_;
223 343
224 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
225} 345}
226 346
227sub find_widget { 347sub find_widget {
228 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
229 351
230 return $self 352 return $self
231 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
232 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
233 355
234 () 356 ()
235} 357}
236 358
237sub set_parent { 359sub set_parent {
238 my ($self, $par) = @_; 360 my ($self, $parent) = @_;
239 361
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 362 Scalar::Util::weaken ($self->{parent} = $parent);
242} 363}
243 364
244sub get_parent { 365sub check_size {
366 my ($self) = @_;
367
245 $_[0]->{parent} 368 $self->{parent}
369 or return 1;
370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
376 $self->{req_w} = $w;
377 $self->{req_h} = $h;
378
379 $self->{parent}->check_size
380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
388 }
246} 389}
247 390
248sub update { 391sub update {
249 my ($self) = @_; 392 my ($self) = @_;
250 393
253} 396}
254 397
255sub connect { 398sub connect {
256 my ($self, $signal, $cb) = @_; 399 my ($self, $signal, $cb) = @_;
257 400
258 push @{ $self->{cb}{$signal} }, $cb; 401 push @{ $self->{signal_cb}{$signal} }, $cb;
259} 402}
260 403
261sub emit { 404sub emit {
262 my ($self, $signal, @args) = @_; 405 my ($self, $signal, @args) = @_;
263 406
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
264 $_->($self, @args) 408 $cb->($self, @args);
265 for @{$self->{cb}{$signal} || []}; 409 }
266} 410}
267 411
268sub DESTROY { 412sub DESTROY {
269 my ($self) = @_; 413 my ($self) = @_;
270 414
276package CFClient::UI::DrawBG; 420package CFClient::UI::DrawBG;
277 421
278our @ISA = CFClient::UI::Base::; 422our @ISA = CFClient::UI::Base::;
279 423
280use strict; 424use strict;
281use SDL::OpenGL; 425use CFClient::OpenGL;
282 426
283sub new { 427sub new {
284 my $class = shift; 428 my $class = shift;
285 429
286 # range [value, low, high, page] 430 # range [value, low, high, page]
315 459
316package CFClient::UI::Empty; 460package CFClient::UI::Empty;
317 461
318our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
319 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
320sub size_request { 469sub size_request {
321 (0, 0) 470 (0, 0)
322} 471}
323 472
324sub draw { } 473sub draw { }
332sub new { 481sub new {
333 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
334 483
335 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
336 485
337 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
338 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
339 488
340 $self 489 $self
341} 490}
342 491
343sub add { 492sub add {
344 my ($self, $chld) = @_; 493 my ($self, $child) = @_;
345 494
346 $chld->set_parent ($self); 495 $child->set_parent ($self);
496
497 use sort 'stable';
347 498
348 $self->{children} = [ 499 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 500 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 501 @{$self->{children}}, $child
351 ]; 502 ];
352 503
353 $self->{w} = $self->{h} = -1; 504 $child->check_size;
505}
506
507sub remove {
508 my ($self, $child) = @_;
509
510 delete $child->{parent};
511
512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
513
514 $self->check_size;
354 $self->update; 515 $self->update;
355} 516}
356 517
357sub remove { 518sub clear {
358 my ($self, $widget) = @_; 519 my ($self) = @_;
359 520
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 521 delete $_->{parent}
522 for @{ delete $self->{children} };
361 523
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 524 $self->{children} = [];
363} 525}
364 526
365sub find_widget { 527sub find_widget {
366 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
367 529
397 559
398 $class->SUPER::new (children => [$child], %arg) 560 $class->SUPER::new (children => [$child], %arg)
399} 561}
400 562
401sub add { 563sub add {
402 my ($self, $widget) = @_; 564 my ($self, $child) = @_;
403 565
404 $self->{children} = []; 566 $self->{children} = [];
405 567
406 $self->SUPER::add ($widget); 568 $self->SUPER::add ($child);
407} 569}
408 570
409sub remove { 571sub remove {
410 my ($self, $widget) = @_; 572 my ($self, $widget) = @_;
411 573
420sub size_request { 582sub size_request {
421 $_[0]{children}[0]->size_request 583 $_[0]{children}[0]->size_request
422} 584}
423 585
424sub size_allocate { 586sub size_allocate {
425 my ($self, $x, $y, $w, $h) = @_; 587 my ($self, $w, $h) = @_;
426 588
427 $self->_size_allocate ($x, $y, $w, $h) or return;
428
429 $self->{children}[0]->size_allocate (0, 0, $w, $h); 589 $self->{children}[0]->configure (0, 0, $w, $h);
430} 590}
431 591
432############################################################################# 592#############################################################################
433 593
434package CFClient::UI::Window; 594package CFClient::UI::Window;
435 595
436our @ISA = CFClient::UI::Bin::; 596our @ISA = CFClient::UI::Bin::;
437 597
438use SDL::OpenGL; 598use CFClient::OpenGL;
439 599
440sub new { 600sub new {
441 my ($class, %arg) = @_; 601 my ($class, %arg) = @_;
442 602
443 my $self = $class->SUPER::new (%arg); 603 my $self = $class->SUPER::new (%arg);
460 $self->child->draw; 620 $self->child->draw;
461 }; 621 };
462} 622}
463 623
464sub size_allocate { 624sub size_allocate {
465 my ($self, $x, $y, $w, $h) = @_; 625 my ($self, $w, $h) = @_;
466 626
467 $self->_size_allocate ($x, $y, $w, $h) or return;
468
469 $self->child->size_allocate (0, 0, $w, $h); 627 $self->child->configure (0, 0, $w, $h);
470 628
471 $self->render_chld; 629 $self->render_chld;
472} 630}
473 631
474sub _draw { 632sub _draw {
490 glDisable GL_TEXTURE_2D; 648 glDisable GL_TEXTURE_2D;
491} 649}
492 650
493############################################################################# 651#############################################################################
494 652
653package CFClient::UI::ViewPort;
654
655our @ISA = CFClient::UI::Window::;
656
657sub new { die }
658
659sub size_request {
660 my ($self) = @_;
661
662 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
664
665 @$self{qw(child_w child_h)}
666}
667
668sub _draw {
669 my ($self) = @_;
670
671 $self->{children}[1]->draw;
672}
673
674
675#############################################################################
676
495package CFClient::UI::Frame; 677package CFClient::UI::Frame;
496 678
497our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
498 680
499use SDL::OpenGL; 681use CFClient::OpenGL;
500
501sub size_request {
502 my ($self) = @_;
503 my $chld = $self->child
504 or return (0, 0);
505
506 $chld->move (2, 2);
507
508 map { $_ + 4 } $chld->size_request;
509}
510
511sub size_allocate {
512 my ($self, $x, $y, $w, $h) = @_;
513
514 $self->_size_allocate ($x, $y, $w, $h) or return;
515
516 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
517}
518
519sub _draw {
520 my ($self) = @_;
521
522 my $chld = $self->child;
523
524 my ($w, $h) = $chld->size_request;
525
526 glBegin GL_QUADS;
527 glColor 0, 0, 0;
528 glVertex 0 , 0;
529 glVertex 0 , $h + 4;
530 glVertex $w + 4 , $h + 4;
531 glVertex $w + 4 , 0;
532 glEnd;
533
534 $chld->draw;
535}
536
537#############################################################################
538
539package CFClient::UI::FancyFrame;
540
541our @ISA = CFClient::UI::Bin::;
542
543use SDL::OpenGL;
544
545my @tex =
546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
547 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
548 682
549sub new { 683sub new {
550 my $class = shift; 684 my $class = shift;
551 685
552 # TODO: user_x, user_y, overwrite moveto?
553
554 $class->SUPER::new ( 686 my $self = $class->SUPER::new (
555 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 689 border => 0.8,
558 @_ 690 @_
691 );
692
693 $self
694}
695
696sub _draw {
697 my ($self) = @_;
698
699 my ($w, $h) = ($self->{w}, $self->{h});
700
701 glEnable GL_BLEND;
702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
703 glEnable GL_TEXTURE_2D;
704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
705
706# glBegin GL_QUADS;
707# glColor 0, 0, 0, 0;
708# glVertex 0 , 0;
709# glVertex 0 , $h;
710# glVertex $w, $h;
711# glVertex $w, 0;
712# glEnd;
713
714
715 $self->child->draw;
716 glDisable GL_BLEND;
717 glDisable GL_TEXTURE_2D;
718}
719
720#############################################################################
721
722package CFClient::UI::FancyFrame;
723
724our @ISA = CFClient::UI::Bin::;
725
726use CFClient::OpenGL;
727
728my @tex =
729 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
730 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
731
732sub new {
733 my $class = shift;
734
735 # TODO: user_x, user_y, overwrite moveto?
736
737 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1],
740 border => 0.8,
741 can_events => 1,
742 @_
743 );
744
745 $self->{title} &&= new CFClient::UI::Label
746 align => 0,
747 valign => 1,
748 text => $self->{title},
749 fontsize => 1;
750
751 $self
752}
753
754sub border {
755 int $_[0]{border} * $::FONTSIZE
756}
757
758sub size_request {
759 my ($self) = @_;
760
761 my ($w, $h) = $self->SUPER::size_request;
762
763 (
764 $w + $self->border * 2,
765 $h + $self->border * 2,
559 ) 766 )
560} 767}
561 768
562sub size_request {
563 my ($self) = @_;
564
565 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
566
567 my ($w, $h) = $self->SUPER::size_request;
568
569 (
570 $w + $self->{border} * 2,
571 $h + $self->{border} * 2,
572 )
573}
574
575sub size_allocate { 769sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 770 my ($self, $w, $h) = @_;
577 771
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $h -= List::Util::max 0, $self->{border} * 2; 772 $h -= List::Util::max 0, $self->border * 2;
581 $w -= List::Util::max 0, $self->{border} * 2; 773 $w -= List::Util::max 0, $self->border * 2;
582 774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 778 $self->child->configure ($self->border, $self->border, $w, $h);
584} 779}
585 780
586sub button_down { 781sub button_down {
587 my ($self, $ev, $x, $y) = @_; 782 my ($self, $ev, $x, $y) = @_;
588 783
784 my $border = $self->border;
785
589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 786 if ($x < $self->{w} && $x >= $self->{w} - $border
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 787 && $y < $self->{h} && $y >= $self->{h} - $border) {
591 788
592 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
593 my ($bw, $bh) = ($self->{w}, $self->{h}); 790 my ($bw, $bh) = ($self->{w}, $self->{h});
594 791
595 $self->{motion} = sub { 792 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_; 793 my ($ev, $x, $y) = @_;
597 794
598 ($x, $y) = ($ev->motion_x, $ev->motion_y); 795 ($x, $y) = ($ev->{x}, $ev->{y});
599 796
600 $self->{user_w} = $bw + $x - $ox; 797 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy; 798 $self->{user_h} = $bh + $y - $oy;
602 $self->update; 799 $self->check_size;
603 }; 800 };
604 801
605 } elsif ($x >= 0 && $x < $self->{w} 802 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) { 803 && $y >= 0 && $y < $border) {
607 804
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
609 my ($bx, $by) = ($self->{x}, $self->{y}); 806 my ($bx, $by) = ($self->{x}, $self->{y});
610 807
611 $self->{motion} = sub { 808 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_; 809 my ($ev, $x, $y) = @_;
613 810
614 ($x, $y) = ($ev->motion_x, $ev->motion_y); 811 ($x, $y) = ($ev->{x}, $ev->{y});
615 812
616 $self->move ($bx + $x - $ox, $by + $y - $oy); 813 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update; 814 $self->update;
618 }; 815 };
619 } 816 }
640 glEnable GL_BLEND; 837 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 838 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 839 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644 841
842 my $border = $self->border;
843
645 glColor @{ $self->{border_bg} }; 844 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 845 $tex[1]->draw_quad (0, 0, $w, $border);
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 846 $tex[3]->draw_quad (0, $border, $border, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 847 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 848 $tex[4]->draw_quad (0, $h - $border, $w, $border);
650 849
651 my $bg = $tex[0]; 850 my $bg = $tex[0];
652 851
653 # TODO: repeat texture not scale 852 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w}; 853 my $rep_x = $cw / $bg->{w};
657 glColor @{ $self->{bg} }; 856 glColor @{ $self->{bg} };
658 857
659 $bg->{s} = $rep_x; 858 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y; 859 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1; 860 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 861 $bg->draw_quad ($border, $border, $cw, $ch);
663 862
664 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND; 864 glDisable GL_BLEND;
666 865
866 $self->{title}->draw if $self->{title};
667 $self->child->draw; 867 $self->child->draw;
668} 868}
669 869
670############################################################################# 870#############################################################################
671 871
673 873
674our @ISA = CFClient::UI::Base::; 874our @ISA = CFClient::UI::Base::;
675 875
676use List::Util qw(max sum); 876use List::Util qw(max sum);
677 877
678use SDL::OpenGL; 878use CFClient::OpenGL;
679 879
680sub new { 880sub new {
681 my $class = shift; 881 my $class = shift;
682 882
683 $class->SUPER::new ( 883 $class->SUPER::new (
685 @_ 885 @_
686 ) 886 )
687} 887}
688 888
689sub add { 889sub add {
690 my ($self, $x, $y, $chld) = @_; 890 my ($self, $x, $y, $child) = @_;
691 891
892 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 893 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 894
695 $self->{w} = $self->{h} = -1; 895 $child->check_size;
896}
897
898# TODO: move to container class maybe? send childs a signal on removal?
899sub clear {
900 my ($self) = @_;
901
902 delete $self->{children};
696 $self->update; 903 $self->update;
697} 904}
698 905
699sub get_wh { 906sub get_wh {
700 my ($self) = @_; 907 my ($self) = @_;
706 or next; 913 or next;
707 914
708 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
709 my $widget = $row->[$x] 916 my $widget = $row->[$x]
710 or next; 917 or next;
711 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
712 919
713 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
714 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
715 } 922 }
716 } 923 }
728 (sum @$hs), 935 (sum @$hs),
729 ) 936 )
730} 937}
731 938
732sub size_allocate { 939sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_; 940 my ($self, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736 941
737 my ($ws, $hs) = $self->get_wh; 942 my ($ws, $hs) = $self->get_wh;
738 943
739 my $req_w = sum @$ws; 944 my $req_w = sum @$ws;
740 my $req_h = sum @$hs; 945 my $req_h = sum @$hs;
762 967
763 for my $c (0 .. $#$row) { 968 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c]; 969 my $col_w = $ws->[$c];
765 970
766 if (my $widget = $row->[$c]) { 971 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h); 972 $widget->configure ($x, $y, $col_w, $row_h);
768 } 973 }
769 974
770 $x += $col_w; 975 $x += $col_w;
771 } 976 }
772 977
817 (List::Util::max map $_->[1], @alloc), 1022 (List::Util::max map $_->[1], @alloc),
818 ) 1023 )
819} 1024}
820 1025
821sub size_allocate { 1026sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 1027 my ($self, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825 1028
826 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
827 1030
828 my $children = $self->{children}; 1031 my $children = $self->{children};
829 1032
830 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
831 1034
832 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
833 1036
834 if ($req_h > $h) { 1037 if ($req_h > $h) {
835 # ah well, not enough space 1038 # ah well, not enough space
851 1054
852 my $y = 0; 1055 my $y = 0;
853 for (0 .. $#$children) { 1056 for (0 .. $#$children) {
854 my $child = $children->[$_]; 1057 my $child = $children->[$_];
855 my $h = $h[$_]; 1058 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 1059 $child->configure ($y, 0, $h, $w);
857 1060
858 $y += $h; 1061 $y += $h;
859 } 1062 }
1063
1064 1
860} 1065}
861 1066
862############################################################################# 1067#############################################################################
863 1068
864package CFClient::UI::VBox; 1069package CFClient::UI::VBox;
877 (List::Util::sum map $_->[1], @alloc), 1082 (List::Util::sum map $_->[1], @alloc),
878 ) 1083 )
879} 1084}
880 1085
881sub size_allocate { 1086sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885 1088
886 my $children = $self->{children}; 1089 my $children = $self->{children};
887 1090
888 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
889 1092
890 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
891 1094
892 if ($req_h > $h) { 1095 if ($req_h > $h) {
893 # ah well, not enough space 1096 # ah well, not enough space
907 1110
908 my $y = 0; 1111 my $y = 0;
909 for (0 .. $#$children) { 1112 for (0 .. $#$children) {
910 my $child = $children->[$_]; 1113 my $child = $children->[$_];
911 my $h = $h[$_]; 1114 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 1115 $child->configure (0, $y, $w, $h);
913 1116
914 $y += $h; 1117 $y += $h;
915 } 1118 }
1119
1120 1
916} 1121}
917 1122
918############################################################################# 1123#############################################################################
919 1124
920package CFClient::UI::Label; 1125package CFClient::UI::Label;
921 1126
922our @ISA = CFClient::UI::Base::; 1127our @ISA = CFClient::UI::Base::;
923 1128
924use SDL::OpenGL; 1129use CFClient::OpenGL;
925 1130
926sub new { 1131sub new {
927 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
928 1133
929 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
931 fontsize => $::FONTSIZE, 1137 fontsize => 1,
932 text => "", 1138 text => "",
933 align => -1, 1139 align => -1,
1140 valign => -1,
934 padding => 2, 1141 padding => 2,
935 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
936 %arg 1144 %arg
937 ); 1145 );
938 1146
939 $self->set_text ($self->{text}); 1147 if (exists $self->{template}) {
1148 my $layout = new CFClient::Layout;
1149 $layout->set_text (delete $self->{template});
1150 $self->{template} = $layout;
1151 }
1152
1153 $self->set_text (delete $self->{text}) if exists $self->{text};
1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
940 1155
941 $self 1156 $self
942} 1157}
943 1158
944sub escape_text { 1159sub escape {
945 local $_ = $_[1]; 1160 local $_ = $_[1];
946 1161
947 s/&/&amp;/g; 1162 s/&/&amp;/g;
948 s/>/&gt;/g; 1163 s/>/&gt;/g;
949 s/</&lt;/g; 1164 s/</&lt;/g;
952} 1167}
953 1168
954sub set_text { 1169sub set_text {
955 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
956 1171
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1172 $self->{layout}->set_text ($text);
959 1173
960 delete $self->{texture}; 1174 delete $self->{texture};
961# $self->{w} = $self->{h} = -1; 1175 $self->check_size;
962 $self->update; 1176 $self->update;
963} 1177}
964 1178
965sub get_text { 1179sub set_markup {
966 my ($self, $text) = @_; 1180 my ($self, $markup) = @_;
967 1181
968 $self->{text} 1182 $self->{layout}->set_markup ($markup);
1183
1184 delete $self->{texture};
1185 $self->check_size;
1186 $self->update;
969} 1187}
970 1188
971sub size_request { 1189sub size_request {
972 my ($self) = @_; 1190 my ($self) = @_;
973 1191
974 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
975 $self->{layout}->set_height ($self->{fontsize}); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1195
976 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1197
1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1201
1202 my ($w2, $h2) = $self->{template}->size;
1203
1204 $w = List::Util::max $w, $w2;
1205 $h = List::Util::max $h, $h2;
1206 }
977 1207
978 ( 1208 (
979 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
981 ) 1211 )
982} 1212}
983 1213
984sub size_allocate { 1214sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988 1216
989 delete $self->{texture}; 1217 delete $self->{texture};
990} 1218}
991 1219
992sub update { 1220sub set_fontsize {
993 my ($self) = @_; 1221 my ($self, $fontsize) = @_;
994 1222
1223 $self->{fontsize} = $fontsize;
995 delete $self->{texture}; 1224 delete $self->{texture};
1225 $self->check_size;
996 $self->SUPER::update; 1226 $self->update;
997} 1227}
998 1228
999sub _draw { 1229sub _draw {
1000 my ($self) = @_; 1230 my ($self) = @_;
1001 1231
1002 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1003 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1005 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1237 };
1007 1238
1008 glEnable GL_BLEND; 1239 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1241 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1243
1013 glColor @{$self->{fg}}; 1244 glColor @{$self->{fg}};
1014 1245
1015 my $x = 1246 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1247 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1248 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1249 : ($self->{w} - $tex->{w}) * 0.5
1250 );
1019 1251
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1252 $self->{oy} = int (
1253 $self->{valign} < 0 ? $self->{padding}
1254 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1255 : ($self->{h} - $tex->{h}) * 0.5
1256 );
1257
1258 $tex->draw_quad ($self->{ox}, $self->{oy});
1021 1259
1022 glDisable GL_TEXTURE_2D; 1260 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1261 glDisable GL_BLEND;
1024} 1262}
1025 1263
1026############################################################################# 1264#############################################################################
1027 1265
1028package CFClient::UI::Entry; 1266package CFClient::UI::EntryBase;
1029 1267
1030our @ISA = CFClient::UI::Label::; 1268our @ISA = CFClient::UI::Label::;
1031 1269
1032use SDL; 1270use CFClient::OpenGL;
1033use SDL::OpenGL;
1034 1271
1035sub new { 1272sub new {
1036 my $class = shift; 1273 my $class = shift;
1037 1274
1038 $class->SUPER::new ( 1275 $class->SUPER::new (
1040 bg => [0, 0, 0, 0.2], 1277 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1043 can_hover => 1, 1280 can_hover => 1,
1044 can_focus => 1, 1281 can_focus => 1,
1282 valign => 0,
1283 can_events => 1,
1045 @_ 1284 @_
1046 ) 1285 )
1047} 1286}
1048 1287
1049sub _set_text { 1288sub _set_text {
1050 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1051 1290
1052 my $old_text = $self->{text}; 1291 delete $self->{cur_h};
1292
1293 return if $self->{text} eq $text;
1294
1295 delete $self->{texture};
1053 1296
1054 $self->{last_activity} = $::NOW; 1297 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1298 $self->{text} = $text;
1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1059 1299
1060 $text =~ s/./*/g if $self->{hidden}; 1300 $text =~ s/./*/g if $self->{hidden};
1301 $self->{layout}->set_text ("$text ");
1061 1302
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1303 $self->emit (changed => $self->{text});
1304}
1063 1305
1064 $text = substr $text, 0, $self->{cursor}; 1306sub get_text {
1065 utf8::encode $text; 1307 $_[0]{text}
1066
1067 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1068
1069 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1070 if $old_text ne $self->{text};
1071} 1308}
1072 1309
1073sub size_request { 1310sub size_request {
1074 my ($self) = @_; 1311 my ($self) = @_;
1075 1312
1077 1314
1078 ($w + 1, $h) # add 1 for cursor 1315 ($w + 1, $h) # add 1 for cursor
1079} 1316}
1080 1317
1081sub size_allocate { 1318sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1083 1320
1084 $self->SUPER::size_allocate ($x, $y, $w, $h); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1085
1086 $self->_set_text ($self->{text});
1087} 1322}
1088 1323
1089sub set_text { 1324sub set_text {
1090 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1091 1326
1095} 1330}
1096 1331
1097sub key_down { 1332sub key_down {
1098 my ($self, $ev) = @_; 1333 my ($self, $ev) = @_;
1099 1334
1100 my $mod = $ev->key_mod; 1335 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1336 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1337 my $uni = $ev->{unicode};
1104 1338
1105 my $text = $self->get_text; 1339 my $text = $self->get_text;
1106 1340
1107 if ($sym == SDLK_BACKSPACE) { 1341 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1342 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1343 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1344 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1345 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1346 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1347 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1348 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1349 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1350 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1351 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1352 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1353 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1354 $self->emit ('escape');
1121 } elsif ($uni) { 1355 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1356 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1357 }
1124 1358
1179 1413
1180 $self->SUPER::_draw; 1414 $self->SUPER::_draw;
1181 1415
1182 #TODO: force update every cursor change :( 1416 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1417 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1418
1419 unless (exists $self->{cur_h}) {
1420 my $text = substr $self->{text}, 0, $self->{cursor};
1421 utf8::encode $text;
1422
1423 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1424 }
1425
1184 glColor @{$self->{fg}}; 1426 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1427 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1428 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1429 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1430 glEnd;
1189 } 1431 }
1190} 1432}
1191 1433
1192package CFClient::UI::LineEntry; 1434package CFClient::UI::Entry;
1193 1435
1194our @ISA = CFClient::UI::Entry::; 1436our @ISA = CFClient::UI::EntryBase::;
1195 1437
1196use SDL; 1438use CFClient::OpenGL;
1197use SDL::OpenGL;
1198 1439
1199sub key_down { 1440sub key_down {
1200 my ($self, $ev) = @_; 1441 my ($self, $ev) = @_;
1201 1442
1202 my $sym = $ev->key_sym; 1443 my $sym = $ev->{sym};
1203 1444
1204 if ($sym == SDLK_RETURN) { 1445 if ($sym == 13) {
1205 $self->emit (activate => $self->get_text); 1446 $self->emit (activate => $self->get_text);
1206 $self->update; 1447 $self->update;
1207 1448
1208 } else { 1449 } else {
1209 $self->SUPER::key_down ($ev); 1450 $self->SUPER::key_down ($ev);
1215 1456
1216package CFClient::UI::Button; 1457package CFClient::UI::Button;
1217 1458
1218our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1219 1460
1220use SDL; 1461use CFClient::OpenGL;
1221use SDL::OpenGL;
1222 1462
1223my @tex = 1463my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1225 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1226 1466
1227sub new { 1467sub new {
1228 my $class = shift; 1468 my $class = shift;
1229 1469
1230 $class->SUPER::new ( 1470 $class->SUPER::new (
1231 padding => 4, 1471 padding => 4,
1232 fg => [1, 1, 1], 1472 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1474 active_fg => [0, 0, 1],
1235 can_hover => 1, 1475 can_hover => 1,
1476 align => 0,
1477 valign => 0,
1478 can_events => 1,
1236 @_ 1479 @_
1237 ) 1480 )
1238} 1481}
1239 1482
1240sub button_up { 1483sub button_up {
1248 1491
1249sub _draw { 1492sub _draw {
1250 my ($self) = @_; 1493 my ($self) = @_;
1251 1494
1252 local $self->{fg} = $self->{fg}; 1495 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258 1496
1259 if ($GRAB == $self) { 1497 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1498 $self->{fg} = $self->{active_fg};
1261 } 1499 }
1262 1500
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1501 glEnable GL_BLEND;
1502 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1503 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1504 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1505 glColor 0, 0, 0, 1;
1265 1506
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1507 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1508
1268 glDisable GL_TEXTURE_2D; 1509 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1510 glDisable GL_BLEND;
1270 1511
1271 $self->SUPER::_draw; 1512 $self->SUPER::_draw;
1276package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1277 1518
1278our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1279 1520
1280my @tex = 1521my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1524
1284use SDL; 1525use CFClient::OpenGL;
1285use SDL::OpenGL;
1286 1526
1287sub new { 1527sub new {
1288 my $class = shift; 1528 my $class = shift;
1289 1529
1290 $class->SUPER::new ( 1530 $class->SUPER::new (
1301 my ($self) = @_; 1541 my ($self) = @_;
1302 1542
1303 ($self->{padding} * 2 + 6) x 2 1543 ($self->{padding} * 2 + 6) x 2
1304} 1544}
1305 1545
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1546sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1547 my ($self, $ev, $x, $y) = @_;
1314 1548
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1549 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1550 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1342 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1343} 1577}
1344 1578
1345############################################################################# 1579#############################################################################
1346 1580
1581package CFClient::UI::Image;
1582
1583our @ISA = CFClient::UI::Base::;
1584
1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1587
1588our %loaded_images;
1589
1590sub new {
1591 my $class = shift;
1592
1593 my $self = $class->SUPER::new (can_events => 0, @_);
1594
1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1596
1597 $loaded_images{$self->{image}} ||=
1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1599
1600 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1601
1602 Scalar::Util::weaken $loaded_images{$self->{image}};
1603
1604 $self->{aspect} = $tex->{w} / $tex->{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 ($self->{tex}->{w}, $self->{tex}->{h})
1613}
1614
1615sub _draw {
1616 my ($self) = @_;
1617
1618 my $tex = $self->{tex};
1619
1620 my ($w, $h) = ($self->{w}, $self->{h});
1621
1622 if ($self->{rot90}) {
1623 glRotate 90, 0, 0, 1;
1624 glTranslate 0, -$self->{w}, 0;
1625
1626 ($w, $h) = ($h, $w);
1627 }
1628
1629 glEnable GL_BLEND;
1630 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1631 glEnable GL_TEXTURE_2D;
1632 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1633
1634 $tex->draw_quad (0, 0, $w, $h);
1635
1636 glDisable GL_BLEND;
1637 glDisable GL_TEXTURE_2D;
1638}
1639
1640#############################################################################
1641
1642package CFClient::UI::VGauge;
1643
1644our @ISA = CFClient::UI::Base::;
1645
1646use List::Util qw(min max);
1647
1648use CFClient::OpenGL;
1649
1650my %tex = (
1651 food => [
1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1654 ],
1655 grace => [
1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1658 ],
1659 hp => [
1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1662 ],
1663 mana => [
1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1666 ],
1667);
1668
1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1670sub new {
1671 my $class = shift;
1672
1673 my $self = $class->SUPER::new (
1674 type => 'food',
1675 @_
1676 );
1677
1678 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1679
1680 $self
1681}
1682
1683sub size_request {
1684 my ($self) = @_;
1685
1686 #my $tex = $tex{$self->{type}}[0];
1687 #@$tex{qw(w h)}
1688 (0, 0)
1689}
1690
1691sub set_max {
1692 my ($self, $max) = @_;
1693
1694 $self->{max_val} = $max;
1695}
1696
1697sub set_value {
1698 my ($self, $val, $max) = @_;
1699
1700 $self->set_max ($max)
1701 if defined $max;
1702
1703 $max = $self->{max_val};
1704 $self->{val} = $val;
1705
1706 $self->update;
1707}
1708
1709sub _draw {
1710 my ($self) = @_;
1711
1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1714
1715 my ($w, $h) = ($self->{w}, $self->{h});
1716
1717 if ($self->{vertical}) {
1718 glRotate 90, 0, 0, 1;
1719 glTranslate 0, -$self->{w}, 0;
1720
1721 ($w, $h) = ($h, $w);
1722 }
1723
1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1725
1726 my $ycut1 = max 0, min 1, $ycut;
1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1729 my $h1 = $self->{h} * (1 - $ycut1);
1730 my $h2 = $self->{h} * (1 - $ycut2);
1731
1732 glEnable GL_BLEND;
1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1734 glEnable GL_TEXTURE_2D;
1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1736
1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1738 glBegin GL_QUADS;
1739 glTexCoord 0 , 0; glVertex 0 , 0;
1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1743 glEnd;
1744
1745 my $ycut1 = List::Util::min 1, $ycut;
1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1747 glBegin GL_QUADS;
1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1763
1764 glDisable GL_BLEND;
1765 glDisable GL_TEXTURE_2D;
1766}
1767
1768#############################################################################
1769
1770package CFClient::UI::Gauge;
1771
1772our @ISA = CFClient::UI::VBox::;
1773
1774sub new {
1775 my ($class, %arg) = @_;
1776
1777 my $self = $class->SUPER::new (
1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1782 );
1783
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1787
1788 $self
1789}
1790
1791sub set_fontsize {
1792 my ($self, $fsize) = @_;
1793
1794 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize);
1796}
1797
1798sub set_value {
1799 my ($self, $val, $max) = @_;
1800
1801 $self->set_max ($max)
1802 if defined $max;
1803
1804 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1813}
1814
1815#############################################################################
1816
1347package CFClient::UI::Slider; 1817package CFClient::UI::Slider;
1348 1818
1349use strict; 1819use strict;
1350 1820
1351use SDL::OpenGL; 1821use CFClient::OpenGL;
1352 1822
1353our @ISA = CFClient::UI::DrawBG::; 1823our @ISA = CFClient::UI::DrawBG::;
1354 1824
1355my @tex = 1825my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1366 # TODO: calculations are off 1836 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1837 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1838 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1839 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1840 range => [0, 0, 100, 10],
1371 req_w => 40, 1841 req_w => 20,
1372 req_h => 13, 1842 req_h => 20,
1373 vertical => 0, 1843 vertical => 0,
1374 can_hover => 1, 1844 can_hover => 1,
1375 inner_pad => 5, 1845 inner_pad => 5,
1376 @_ 1846 @_
1377 ); 1847 );
1481 1951
1482package CFClient::UI::TextView; 1952package CFClient::UI::TextView;
1483 1953
1484our @ISA = CFClient::UI::HBox::; 1954our @ISA = CFClient::UI::HBox::;
1485 1955
1486use SDL::OpenGL; 1956use CFClient::OpenGL;
1487 1957
1488sub new { 1958sub new {
1489 my $class = shift; 1959 my $class = shift;
1490 1960
1491 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1962 fontsize => 1,
1963 #font => default_font
1495 @_, 1964 @_,
1496 1965
1497 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1498 par => [], 1967 par => [],
1499 height => 0, 1968 height => 0,
1520sub text_height { 1989sub text_height {
1521 my ($self, $text) = @_; 1990 my ($self, $text) = @_;
1522 1991
1523 my $layout = $self->{layout}; 1992 my $layout = $self->{layout};
1524 1993
1525 $layout->set_height ($self->{fontsize}); 1994 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1995 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1996 $layout->set_text ($text);
1528 1997
1529 ($layout->size)[1] 1998 ($layout->size)[1]
1530} 1999}
1534 2003
1535 $self->{need_reflow}++; 2004 $self->{need_reflow}++;
1536 $self->update; 2005 $self->update;
1537} 2006}
1538 2007
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 2008sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1547 2010
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1549 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1550 $self->{layout}->set_height ($self->{fontsize}); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1552 2016
1553 $self->reflow; 2017 $self->reflow;
1554} 2018}
1555 2019
1556sub add_paragraph { 2020sub add_paragraph {
1575 2039
1576 return unless $self->{h} > 0; 2040 return unless $self->{h} > 0;
1577 2041
1578 delete $self->{texture}; 2042 delete $self->{texture};
1579 2043
1580 $TOPLEVEL->on_refresh ($self, sub { 2044 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 2045 if (delete $self->{need_reflow}) {
1582 my $height = 0; 2046 my $height = 0;
1583 2047
1584 $height += $_->[0] = $self->text_height ($_->[2]) 2048 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 2049 for @{$self->{par}};
1606 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1607 2071
1608 my $y = 0; 2072 my $y = 0;
1609 2073
1610 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1611 2077
1612 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1613 my $h = $par->[0]; 2079 my $h = $par->[0];
1614 2080
1615 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
1643 2109
1644} 2110}
1645 2111
1646############################################################################# 2112#############################################################################
1647 2113
1648package CFClient::UI::MapWidget;
1649
1650use strict;
1651
1652use List::Util qw(min max);
1653
1654use SDL;
1655use SDL::OpenGL;
1656
1657our @ISA = CFClient::UI::Base::;
1658
1659sub new {
1660 my $class = shift;
1661
1662 $class->SUPER::new (
1663 z => -1,
1664 can_focus => 1,
1665 list => (glGenLists 1),
1666 @_
1667 )
1668}
1669
1670sub key_down {
1671 print "MAPKEYDOWN\n";
1672}
1673
1674sub key_up {
1675}
1676
1677sub button_down {
1678 my ($self, $ev, $x, $y) = @_;
1679
1680 $self->focus_in;
1681
1682 if ($ev->button == 2) {
1683 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1684 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1685
1686 $self->{motion} = sub {
1687 my ($ev, $x, $y) = @_;
1688
1689 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1690
1691 $::CFG->{map_shift_x} = $bw + $x - $ox;
1692 $::CFG->{map_shift_y} = $bh + $y - $oy;
1693
1694 $self->update;
1695 };
1696 }
1697}
1698
1699sub button_up {
1700 my ($self, $ev, $x, $y) = @_;
1701
1702 delete $self->{motion};
1703}
1704
1705sub mouse_motion {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->{motion}->($ev, $x, $y) if $self->{motion};
1709}
1710
1711sub size_request {
1712 (
1713 1 + 32 * int $::WIDTH / 32,
1714 1 + 32 * int $::HEIGHT / 32,
1715 )
1716}
1717
1718sub update {
1719 my ($self) = @_;
1720
1721 $self->{need_update} = 1;
1722 $self->SUPER::update;
1723}
1724
1725sub draw {
1726 my ($self) = @_;
1727
1728 if (delete $self->{need_update}) {
1729 glNewList $self->{list}, GL_COMPILE;
1730
1731 if ($::MAP) {
1732 my $sw = int $::WIDTH / 32;
1733 my $sh = int $::HEIGHT / 32;
1734
1735 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1736 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1737
1738 glTranslate $sx0 - 32, $sy0 - 32, 0;
1739
1740 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1741
1742 if ($::CFG->{fow_enable}) {
1743 if ($::CFG->{fow_smooth}) { # smooth fog of war
1744 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1745 glConvolutionFilter2D
1746 GL_CONVOLUTION_2D,
1747 GL_ALPHA,
1748 3, 3,
1749 GL_ALPHA, GL_FLOAT,
1750 pack "f*",
1751 0.1, 0.1, 0.1,
1752 0.1, 0.2, 0.1,
1753 0.1, 0.1, 0.1,
1754 ;
1755 glEnable GL_CONVOLUTION_2D;
1756 }
1757
1758 my $tex = new CFClient::Texture
1759 w => $w,
1760 h => $h,
1761 data => $data,
1762 internalformat => GL_ALPHA,
1763 format => GL_ALPHA;
1764
1765 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1766
1767 glEnable GL_BLEND;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771
1772 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1774
1775 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND;
1777 }
1778
1779 # HACK BEGIN
1780 {
1781 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1782 my ($w, $h) = (250, 250);
1783
1784 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788
1789 CFClient::Texture->new (
1790 w => $w,
1791 h => $h,
1792 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV
1794 )->draw_quad (100, 100);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799 # HACK END
1800 }
1801
1802 glEndList;
1803 }
1804
1805 glPushMatrix;
1806 glCallList $self->{list};
1807 glPopMatrix;
1808
1809 if ($FOCUS != $self) {
1810 glColor 64/255, 64/255, 64/255;
1811 glLogicOp GL_AND;
1812 glEnable GL_COLOR_LOGIC_OP;
1813 glBegin GL_QUADS;
1814 glVertex 0, 0;
1815 glVertex 0, $::HEIGHT;
1816 glVertex $::WIDTH, $::HEIGHT;
1817 glVertex $::WIDTH, 0;
1818 glEnd;
1819 glDisable GL_COLOR_LOGIC_OP;
1820 }
1821}
1822
1823my %DIR = (
1824 SDLK_KP8, [1, "north"],
1825 SDLK_KP9, [2, "northeast"],
1826 SDLK_KP6, [3, "east"],
1827 SDLK_KP3, [4, "southeast"],
1828 SDLK_KP2, [5, "south"],
1829 SDLK_KP1, [6, "southwest"],
1830 SDLK_KP4, [7, "west"],
1831 SDLK_KP7, [8, "northwest"],
1832
1833 SDLK_UP, [1, "north"],
1834 SDLK_RIGHT, [3, "east"],
1835 SDLK_DOWN, [5, "south"],
1836 SDLK_LEFT, [7, "west"],
1837);
1838
1839sub key_down {
1840 my ($self, $ev) = @_;
1841
1842 my $mod = $ev->key_mod;
1843 my $sym = $ev->key_sym;
1844
1845 if ($sym == SDLK_KP5) {
1846 $::CONN->user_send ("command stay fire");
1847 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply");
1849 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/');
1853 } elsif (exists $DIR{$sym}) {
1854 if ($mod & KMOD_SHIFT) {
1855 $self->{shft}++;
1856 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1857 } elsif ($mod & KMOD_CTRL) {
1858 $self->{ctrl}++;
1859 $::CONN->user_send ("command run $DIR{$sym}[0]");
1860 } else {
1861 $::CONN->user_send ("command $DIR{$sym}[1]");
1862 }
1863 }
1864}
1865
1866sub key_up {
1867 my ($self, $ev) = @_;
1868
1869 my $mod = $ev->key_mod;
1870 my $sym = $ev->key_sym;
1871
1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1873 $::CONN->user_send ("command fire_stop");
1874 }
1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1876 $::CONN->user_send ("command run_stop");
1877 }
1878}
1879
1880#############################################################################
1881
1882package CFClient::UI::Animator; 2114package CFClient::UI::Animator;
1883 2115
1884use SDL::OpenGL; 2116use CFClient::OpenGL;
1885 2117
1886our @ISA = CFClient::UI::Bin::; 2118our @ISA = CFClient::UI::Bin::;
1887 2119
1888sub moveto { 2120sub moveto {
1889 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
1929 my $class = shift; 2161 my $class = shift;
1930 2162
1931 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
1932 state => 0, 2164 state => 0,
1933 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
1934 @_ 2167 @_
1935 ); 2168 );
1936 2169
1937 if ($self->{state}) { 2170 if ($self->{state}) {
1938 $self->{state} = 0; 2171 $self->{state} = 0;
1945sub toggle_flopper { 2178sub toggle_flopper {
1946 my ($self) = @_; 2179 my ($self) = @_;
1947 2180
1948 # TODO: use animation 2181 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 2182 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2183 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 2184 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 2185 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 2186 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2187 $CFClient::UI::ROOT->remove ($self->{other});
2188 $self->emit ("close");
2189 }
2190
2191 $self->emit (changed => $self->{state});
2192}
2193
2194#############################################################################
2195
2196package CFClient::UI::Tooltip;
2197
2198our @ISA = CFClient::UI::Bin::;
2199
2200use CFClient::OpenGL;
2201
2202sub new {
2203 my $class = shift;
2204
2205 $class->SUPER::new (
2206 @_,
2207 can_events => 0,
2208 )
2209}
2210
2211sub set_markup {
2212 my ($self, $text) = @_;
2213
2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2215 $self->{label}->set_markup ($text);
2216 $self->add ($self->{label});
2217}
2218
2219sub size_request {
2220 my ($self) = @_;
2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2225
2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2233}
2234
2235sub _draw {
2236 my ($self) = @_;
2237
2238 glPushMatrix;
2239 glTranslate 0.375, 0.375;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 glColor 1, 0.8, 0.4;
2244 glBegin GL_QUADS;
2245 glVertex 0 , 0;
2246 glVertex 0 , $h;
2247 glVertex $w, $h;
2248 glVertex $w, 0;
2249 glEnd;
1957 } 2250
1958} 2251 glColor 0, 0, 0;
2252 glBegin GL_LINE_LOOP;
2253 glVertex 0 , 0;
2254 glVertex 0 , $h;
2255 glVertex $w, $h;
2256 glVertex $w, 0;
2257 glEnd;
2258
2259 glPopMatrix;
1959 2260
1960############################################################################# 2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
1961 2264
2265#############################################################################
2266
1962package CFClient::UI::Toplevel; 2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
2305package CFClient::UI::Root;
1963 2306
1964our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
1965 2308
1966use SDL::OpenGL; 2309use CFClient::OpenGL;
2310
2311sub check_size {
2312 my ($self) = @_;
2313
2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2315}
1967 2316
1968sub size_request { 2317sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
1970} 2319}
1971 2320
1972sub size_allocate { 2321sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
1974 2323
1975 $self->_size_allocate ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
1976 2325
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
1979} 2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1980 2328
1981sub translate { 2329 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H);
2332 }
2333}
2334
2335sub _topleft {
1982 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
1983 2337
1984 ($x, $y) 2338 ($x, $y)
1985} 2339}
1986 2340
1987sub update { 2341sub update {
1988 my ($self) = @_; 2342 my ($self) = @_;
1989 2343
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2344 $self->check_size;
1991 ::refresh (); 2345 ::refresh ();
1992} 2346}
1993 2347
1994sub add { 2348sub add {
1995 my ($self, $widget) = @_; 2349 my ($self, $child) = @_;
1996 2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
2354
1997 $self->SUPER::add ($widget); 2355 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 2356}
2001 2357
2002sub on_refresh { 2358sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2359 my ($self, $id, $cb) = @_;
2004 2360
2027 2383
2028############################################################################# 2384#############################################################################
2029 2385
2030package CFClient::UI; 2386package CFClient::UI;
2031 2387
2388$ROOT = new CFClient::UI::Root;
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2389$TOOLTIP = new CFClient::UI::Tooltip;
2033 2390
20341 23911
2035 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines