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.74 by root, Tue Apr 11 19:31:18 2006 UTC vs.
Revision 1.140 by root, Thu Apr 20 04:20:52 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 $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71}
72
73# convert position array to integers
74sub harmonize {
75 my ($vals) = @_;
76
77 my $rem = 0;
78
79 for (@$vals) {
80 my $i = int $_ + $rem;
81 $rem += $_ - $i;
82 $_ = $i;
83 }
71} 84}
72 85
73############################################################################# 86#############################################################################
74 87
75package CFClient::UI::Base; 88package CFClient::UI::Base;
76 89
77use strict; 90use strict;
78 91
79use SDL::OpenGL; 92use CFClient::OpenGL;
80 93
81sub new { 94sub new {
82 my $class = shift; 95 my $class = shift;
83 96
84 bless { 97 my $self = bless {
85 x => 0, 98 x => 0,
86 y => 0, 99 y => 0,
87 z => 0, 100 z => 0,
88 @_ 101 @_
89 }, $class 102 }, $class;
103
104 for (keys %$self) {
105 if (/^connect_(.*)$/) {
106 $self->connect ($1 => delete $self->{$_});
107 }
108 }
109
110 $self
111}
112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
90} 127}
91 128
92sub move { 129sub move {
93 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
94 $self->{x} = $x; 132 $self->{x} = int $x;
95 $self->{y} = $y; 133 $self->{y} = int $y;
96 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
97} 137}
98 138
99sub needs_redraw { 139sub needs_redraw {
100 0 140 0
101} 141}
103sub size_request { 143sub size_request {
104 require Carp; 144 require Carp;
105 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abtract";
106} 146}
107 147
108sub size_allocate { 148sub configure {
109 my ($self, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
150
151 $self->{x} = $x;
152 $self->{y} = $y;
153
154 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect};
157 }
158
159 return unless $self->{w} != $w || $self->{h} != $h;
110 160
111 $self->{w} = $w; 161 $self->{w} = $w;
112 $self->{h} = $h; 162 $self->{h} = $h;
113}
114 163
115# translate global koordinates to local coordinate system 164 $self->size_allocate ($w, $h);
116sub translate { 165 $self->update;
166}
167
168sub size_allocate {
169 # nothing to be done
170}
171
172# return top left coordinates
173sub _topleft {
117 my ($self, $x, $y) = @_; 174 my ($self, $x, $y) = @_;
118 175
119 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 176 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
177}
178
179# translate global coordinates to local coordinate system
180sub coord2local {
181 my ($self, $x, $y) = @_;
182
183 my ($X, $Y) = $self->_topleft;
184 ($x - $X, $y - $Y)
185}
186
187# translate local coordinates to global coordinate system
188sub coord2global {
189 my ($self, $x, $y) = @_;
190
191 my ($X, $Y) = $self->_topleft;
192 ($x + $X, $y + $Y)
120} 193}
121 194
122sub focus_in { 195sub focus_in {
123 my ($self) = @_; 196 my ($self) = @_;
124 197
125 return if $FOCUS == $self; 198 return if $FOCUS == $self;
199 return unless $self->{can_focus};
126 200
127 my $focus = $FOCUS; $FOCUS = $self; 201 my $focus = $FOCUS; $FOCUS = $self;
202
203 $self->emit (focus_in => $focus);
204
128 $focus->update if $focus; 205 $focus->update if $focus;
129 $FOCUS->update; 206 $FOCUS->update;
130} 207}
131 208
132sub focus_out { 209sub focus_out {
133 my ($self) = @_; 210 my ($self) = @_;
134 211
135 return unless $FOCUS == $self; 212 return unless $FOCUS == $self;
136 213
137 my $focus = $FOCUS; undef $FOCUS; 214 my $focus = $FOCUS; undef $FOCUS;
215
216 $self->emit (focus_out => $focus);
217
138 $focus->update if $focus; #? 218 $focus->update if $focus; #?
139} 219}
140 220
141sub mouse_motion { } 221sub mouse_motion { }
142sub button_up { } 222sub button_up { }
163 glPushMatrix; 243 glPushMatrix;
164 glTranslate $self->{x}, $self->{y}, 0; 244 glTranslate $self->{x}, $self->{y}, 0;
165 $self->_draw; 245 $self->_draw;
166 glPopMatrix; 246 glPopMatrix;
167 247
168 if ($self == $HOVER) { 248 if ($self == $HOVER && $self->{can_hover}) {
169 my ($x, $y) = @$self{qw(x y)}; 249 my ($x, $y) = @$self{qw(x y)};
170 250
171 glColor 1, 1, 1, 0.1; 251 glColor 1, 0.8, 0.5, 0.2;
172 glEnable GL_BLEND; 252 glEnable GL_BLEND;
173 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 253 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
174 glBegin GL_QUADS; 254 glBegin GL_QUADS;
175 glVertex $x , $y; 255 glVertex $x , $y;
176 glVertex $x + $self->{w}, $y; 256 glVertex $x + $self->{w}, $y;
185 my ($self) = @_; 265 my ($self) = @_;
186 266
187 warn "no draw defined for $self\n"; 267 warn "no draw defined for $self\n";
188} 268}
189 269
190sub bbox {
191 my ($self) = @_;
192 my ($w, $h) = $self->size_request;
193 (
194 $self->{x},
195 $self->{y},
196 $self->{x} = $w,
197 $self->{y} = $h
198 )
199}
200
201sub find_widget { 270sub find_widget {
202 my ($self, $x, $y) = @_; 271 my ($self, $x, $y) = @_;
203 272
204 return $self 273 return $self
205 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 274 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
206 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 275 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
207 276
208 () 277 ()
209} 278}
210 279
211sub del_parent { $_[0]->{parent} = undef }
212
213sub set_parent { 280sub set_parent {
214 my ($self, $par) = @_; 281 my ($self, $parent) = @_;
215 282
216 $self->{parent} = $par;
217 Scalar::Util::weaken $self->{parent}; 283 Scalar::Util::weaken ($self->{parent} = $parent);
218} 284}
219 285
220sub get_parent { 286sub check_size {
221 $_[0]->{parent} 287 my ($self) = @_;
288
289 my ($w, $h) = $self->size_request;
290
291 if ($w != $self->{req_w} || $h != $self->{req_h}) {
292 $self->{req_w} = $w;
293 $self->{req_h} = $h;
294
295 $self->{parent}->check_size
296 if $self->{parent};
297 }
222} 298}
223 299
224sub update { 300sub update {
225 my ($self) = @_; 301 my ($self) = @_;
226 302
229} 305}
230 306
231sub connect { 307sub connect {
232 my ($self, $signal, $cb) = @_; 308 my ($self, $signal, $cb) = @_;
233 309
234 push @{ $self->{cb}{$signal} }, $cb; 310 push @{ $self->{signal_cb}{$signal} }, $cb;
235} 311}
236 312
237sub emit { 313sub emit {
238 my ($self, $signal, @args) = @_; 314 my ($self, $signal, @args) = @_;
239 315
316 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
240 $_->($self, @args) 317 $cb->($self, @args);
241 for @{$self->{cb}{$signal} || []}; 318 }
242} 319}
243 320
244sub DESTROY { 321sub DESTROY {
245 my ($self) = @_; 322 my ($self) = @_;
246 323
252package CFClient::UI::DrawBG; 329package CFClient::UI::DrawBG;
253 330
254our @ISA = CFClient::UI::Base::; 331our @ISA = CFClient::UI::Base::;
255 332
256use strict; 333use strict;
257use SDL::OpenGL; 334use CFClient::OpenGL;
258 335
259sub new { 336sub new {
260 my $class = shift; 337 my $class = shift;
261 338
262 # range [value, low, high, page] 339 # range [value, low, high, page]
263 340
264 $class->SUPER::new ( 341 $class->SUPER::new (
265 bg => [0, 0, 0, 0.4], 342 bg => [0, 0, 0, 0.2],
266 active_bg => [1, 1, 1], 343 active_bg => [1, 1, 1, 0.5],
267 @_ 344 @_
268 ) 345 )
269} 346}
270 347
271sub _draw { 348sub _draw {
272 my ($self) = @_; 349 my ($self) = @_;
273 350
274 my ($w, $h) = @$self{qw(w h)}; 351 my ($w, $h) = @$self{qw(w h)};
275 352
353 glEnable GL_BLEND;
354 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
276 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 355 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
356
277 glBegin GL_QUADS; 357 glBegin GL_QUADS;
278 glVertex 0 , 0; 358 glVertex 0 , 0;
279 glVertex 0 , $h; 359 glVertex 0 , $h;
280 glVertex $w, $h; 360 glVertex $w, $h;
281 glVertex $w, 0; 361 glVertex $w, 0;
282 glEnd; 362 glEnd;
363
364 glDisable GL_BLEND;
283} 365}
284 366
285############################################################################# 367#############################################################################
286 368
287package CFClient::UI::Empty; 369package CFClient::UI::Empty;
310 392
311 $self 393 $self
312} 394}
313 395
314sub add { 396sub add {
315 my ($self, $chld, $expand) = @_; 397 my ($self, $child) = @_;
316 398
317 $chld->{expand} = $expand;
318 $chld->set_parent ($self); 399 $child->set_parent ($self);
400
401 use sort 'stable';
319 402
320 $self->{children} = [ 403 $self->{children} = [
321 sort { $a->{z} <=> $b->{z} } 404 sort { $a->{z} <=> $b->{z} }
322 @{$self->{children}}, $chld 405 @{$self->{children}}, $child
323 ]; 406 ];
324 407
325 $self->size_allocate ($self->{w}, $self->{h}) 408 $child->check_size;
326 if $self->{w}; #TODO: check for "realised state"
327} 409}
328 410
329sub remove { 411sub remove {
330 my ($self, $widget) = @_; 412 my ($self, $child) = @_;
331 413
414 delete $child->{parent};
415
332 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 416 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
333 417
334 $self->size_allocate ($self->{w}, $self->{h}); 418 $self->check_size;
335} 419}
336 420
337sub find_widget { 421sub find_widget {
338 my ($self, $x, $y) = @_; 422 my ($self, $x, $y) = @_;
339 423
369 453
370 $class->SUPER::new (children => [$child], %arg) 454 $class->SUPER::new (children => [$child], %arg)
371} 455}
372 456
373sub add { 457sub add {
374 my ($self, $widget) = @_; 458 my ($self, $child) = @_;
375 459
376 $self->{children} = []; 460 $self->{children} = [];
377 461
378 $self->SUPER::add ($widget); 462 $self->SUPER::add ($child);
379} 463}
380 464
381sub remove { 465sub remove {
382 my ($self, $widget) = @_; 466 my ($self, $widget) = @_;
383 467
394} 478}
395 479
396sub size_allocate { 480sub size_allocate {
397 my ($self, $w, $h) = @_; 481 my ($self, $w, $h) = @_;
398 482
399 return unless $self->{w} != $w || $self->{h} != $h;
400
401 $self->SUPER::size_allocate ($w, $h);
402 $self->{children}[0]->size_allocate ($w, $h); 483 $self->{children}[0]->configure (0, 0, $w, $h);
403} 484}
404 485
405############################################################################# 486#############################################################################
406 487
407package CFClient::UI::Window; 488package CFClient::UI::Window;
408 489
409our @ISA = CFClient::UI::Bin::; 490our @ISA = CFClient::UI::Bin::;
410 491
411use SDL::OpenGL; 492use CFClient::OpenGL;
412 493
413sub new { 494sub new {
414 my ($class, %arg) = @_; 495 my ($class, %arg) = @_;
415 496
416 my $self = $class->SUPER::new (%arg); 497 my $self = $class->SUPER::new (%arg);
425} 506}
426 507
427sub render_chld { 508sub render_chld {
428 my ($self) = @_; 509 my ($self) = @_;
429 510
430 $self->{texture} = 511 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
431 CFClient::Texture->new_from_opengl ( 512 glClearColor 0, 0, 0, 1;
432 $self->{w}, $self->{h}, sub { $self->child->draw } 513 glClear GL_COLOR_BUFFER_BIT;
433 ); 514 $self->child->draw;
515 };
434} 516}
435 517
436sub size_allocate { 518sub size_allocate {
437 my ($self, $w, $h) = @_; 519 my ($self, $w, $h) = @_;
438 520
439 return unless $self->{w} != $w || $self->{h} != $h; 521 $self->child->configure (0, 0, $w, $h);
440
441 $self->{w} = $w;
442 $self->{h} = $h;
443
444 $self->child->size_allocate ($w, $h);
445 522
446 $self->render_chld; 523 $self->render_chld;
447} 524}
448 525
449sub _draw { 526sub _draw {
465 glDisable GL_TEXTURE_2D; 542 glDisable GL_TEXTURE_2D;
466} 543}
467 544
468############################################################################# 545#############################################################################
469 546
547package CFClient::UI::ViewPort;
548
549our @ISA = CFClient::UI::Window::;
550
551sub new { die }
552
553sub size_request {
554 my ($self) = @_;
555
556 @$self{qw(child_w child_h)} = $self->child->size_request;
557 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
558
559 @$self{qw(child_w child_h)}
560}
561
562sub _draw {
563 my ($self) = @_;
564
565 $self->{children}[1]->draw;
566}
567
568
569#############################################################################
570
470package CFClient::UI::Frame; 571package CFClient::UI::Frame;
471 572
472our @ISA = CFClient::UI::Bin::; 573our @ISA = CFClient::UI::Bin::;
473 574
474use SDL::OpenGL; 575use CFClient::OpenGL;
475 576
476sub size_request { 577sub size_request {
477 my ($self) = @_; 578 my ($self) = @_;
478 my $chld = $self->child 579 my $chld = $self->child
479 or return (0, 0); 580 or return (0, 0);
482 583
483 map { $_ + 4 } $chld->size_request; 584 map { $_ + 4 } $chld->size_request;
484} 585}
485 586
486sub size_allocate { 587sub size_allocate {
487 my ($self, $w, $h) = @_; 588 my ($self, $x, $y, $w, $h) = @_;
488 589
489 return unless $self->{w} != $w || $self->{h} != $h;
490
491 $self->{w} = $w;
492 $self->{h} = $h;
493
494 $self->child->size_allocate ($w - 4, $h - 4); 590 $self->child->configure (2, 2, $w - 4, $h - 4);
495 $self->child->move (2, 2);
496} 591}
497 592
498sub _draw { 593sub _draw {
499 my ($self) = @_; 594 my ($self) = @_;
500 595
517 612
518package CFClient::UI::FancyFrame; 613package CFClient::UI::FancyFrame;
519 614
520our @ISA = CFClient::UI::Bin::; 615our @ISA = CFClient::UI::Bin::;
521 616
522use SDL::OpenGL; 617use CFClient::OpenGL;
523 618
524my @tex = 619my @tex =
525 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
526 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
527 622
623sub new {
624 my $class = shift;
625
626 # TODO: user_x, user_y, overwrite moveto?
627
628 $class->SUPER::new (
629 bg => [1, 1, 1, 1],
630 border_bg => [1, 1, 1, 1],
631 border => 0.8,
632 @_
633 )
634}
635
636sub border {
637 int $_[0]{border} * $::FONTSIZE
638}
639
528sub size_request { 640sub size_request {
529 my ($self) = @_; 641 my ($self) = @_;
530 642
643 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
644
531 my ($w, $h) = $self->SUPER::size_request; 645 my ($w, $h) = $self->SUPER::size_request;
532 646
533 $h += $tex[1]->{h}; 647 (
534 $h += $tex[4]->{h}; 648 $w + $self->border * 2,
535 $w += $tex[2]->{w}; 649 $h + $self->border * 2,
536 $w += $tex[3]->{w}; 650 )
537
538 ($w, $h)
539} 651}
540 652
541sub size_allocate { 653sub size_allocate {
542 my ($self, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
543 655
544 return unless $self->{w} != $w || $self->{h} != $h; 656 $h -= List::Util::max 0, $self->border * 2;
657 $w -= List::Util::max 0, $self->border * 2;
545 658
546 $self->SUPER::size_allocate ($w, $h); 659 $self->child->configure ($self->border, $self->border, $w, $h);
660}
547 661
548 $h -= $tex[1]->{h}; 662sub button_down {
549 $h -= $tex[4]->{h}; 663 my ($self, $ev, $x, $y) = @_;
550 $w -= $tex[2]->{w};
551 $w -= $tex[3]->{w};
552 664
553 $h = $h < 0 ? 0 : $h; 665 my $border = $self->border;
554 $w = $w < 0 ? 0 : $w;
555 666
556 my $child = $self->child; 667 if ($x < $self->{w} && $x >= $self->{w} - $border
668 && $y < $self->{h} && $y >= $self->{h} - $border) {
557 669
558 $child->size_allocate ($w, $h); 670 my ($ox, $oy) = ($ev->{x}, $ev->{y});
559 $child->move ($tex[3]->{w}, $tex[1]->{h}); 671 my ($bw, $bh) = ($self->{w}, $self->{h});
672
673 $self->{motion} = sub {
674 my ($ev, $x, $y) = @_;
675
676 ($x, $y) = ($ev->{x}, $ev->{y});
677
678 $self->{user_w} = $bw + $x - $ox;
679 $self->{user_h} = $bh + $y - $oy;
680 $self->check_size;
681 };
682
683 } elsif ($x >= 0 && $x < $self->{w}
684 && $y >= 0 && $y < $border) {
685
686 my ($ox, $oy) = ($ev->{x}, $ev->{y});
687 my ($bx, $by) = ($self->{x}, $self->{y});
688
689 $self->{motion} = sub {
690 my ($ev, $x, $y) = @_;
691
692 ($x, $y) = ($ev->{x}, $ev->{y});
693
694 $self->move ($bx + $x - $ox, $by + $y - $oy);
695 $self->update;
696 };
697 }
698}
699
700sub button_up {
701 my ($self, $ev, $x, $y) = @_;
702
703 delete $self->{motion};
704}
705
706sub mouse_motion {
707 my ($self, $ev, $x, $y) = @_;
708
709 $self->{motion}->($ev, $x, $y) if $self->{motion};
560} 710}
561 711
562sub _draw { 712sub _draw {
563 my ($self) = @_; 713 my ($self) = @_;
564 714
565 my ($w, $h) = ($self->{w}, $self->{h}); 715 my ($w, $h ) = ($self->{w}, $self->{h});
566 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 716 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
717
718 glEnable GL_BLEND;
719 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
720 glEnable GL_TEXTURE_2D;
721 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
722
723 my $border = $self->border;
724
725 glColor @{ $self->{border_bg} };
726 $tex[1]->draw_quad (0, 0, $w, $border);
727 $tex[3]->draw_quad (0, $border, $border, $ch);
728 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
729 $tex[4]->draw_quad (0, $h - $border, $w, $border);
730
731 my $bg = $tex[0];
732
733 # TODO: repeat texture not scale
734 my $rep_x = $cw / $bg->{w};
735 my $rep_y = $ch / $bg->{h};
736
737 glColor @{ $self->{bg} };
738
739 $bg->{s} = $rep_x;
740 $bg->{t} = $rep_y;
741 $bg->{wrap_mode} = 1;
742 $bg->draw_quad ($border, $border, $cw, $ch);
743
744 glDisable GL_TEXTURE_2D;
745 glDisable GL_BLEND;
746
747 $self->child->draw;
748}
749
750#############################################################################
751
752package CFClient::UI::Table;
753
754our @ISA = CFClient::UI::Base::;
755
756use List::Util qw(max sum);
757
758use CFClient::OpenGL;
759
760sub new {
761 my $class = shift;
762
763 $class->SUPER::new (
764 col_expand => [],
765 @_
766 )
767}
768
769sub add {
770 my ($self, $x, $y, $child) = @_;
771
772 $child->set_parent ($self);
773 $self->{children}[$y][$x] = $child;
774
775 $child->check_size;
776}
777
778# TODO: move to container class maybe? send childs a signal on removal?
779sub clear {
780 my ($self) = @_;
781
782 delete $self->{children};
783 $self->update;
784}
785
786sub get_wh {
787 my ($self) = @_;
788
789 my (@w, @h);
790
791 for my $y (0 .. $#{$self->{children}}) {
792 my $row = $self->{children}[$y]
793 or next;
794
795 for my $x (0 .. $#$row) {
796 my $widget = $row->[$x]
797 or next;
798 my ($w, $h) = $widget->size_request;
799
800 $w[$x] = max $w[$x], $w;
801 $h[$y] = max $h[$y], $h;
802 }
803 }
804
805 (\@w, \@h)
806}
807
808sub size_request {
809 my ($self) = @_;
810
811 my ($ws, $hs) = $self->get_wh;
812
813 (
814 (sum @$ws),
815 (sum @$hs),
816 )
817}
818
819sub size_allocate {
820 my ($self, $w, $h) = @_;
821
822 my ($ws, $hs) = $self->get_wh;
823
824 my $req_w = sum @$ws;
825 my $req_h = sum @$hs;
826
827 # TODO: nicer code && do row_expand
828 my @col_expand = @{$self->{col_expand}};
829 @col_expand = (1) x @$ws unless @col_expand;
830 my $col_expand = (sum @col_expand) || 1;
831
832 # linearly scale sizes
833 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
834 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
835
836 CFClient::UI::harmonize $ws;
837 CFClient::UI::harmonize $hs;
838
839 my $y;
840
841 for my $r (0 .. $#{$self->{children}}) {
842 my $row = $self->{children}[$r]
843 or next;
844
845 my $x = 0;
846 my $row_h = $hs->[$r];
847
848 for my $c (0 .. $#$row) {
849 my $col_w = $ws->[$c];
850
851 if (my $widget = $row->[$c]) {
852 $widget->configure ($x, $y, $col_w, $row_h);
853 }
854
855 $x += $col_w;
856 }
857
858 $y += $row_h;
859 }
860
861}
862
863sub find_widget {
864 my ($self, $x, $y) = @_;
865
866 $x -= $self->{x};
867 $y -= $self->{y};
868
869 my $res;
870
871 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
872 $res = $_->find_widget ($x, $y)
873 and return $res;
874 }
875
876 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
877}
878
879sub _draw {
880 my ($self) = @_;
881
882 for (grep $_, @{$self->{children}}) {
883 $_->draw for grep $_, @$_;
884 }
885}
886
887#############################################################################
888
889package CFClient::UI::HBox;
890
891# TODO: wrap into common Box base class
892
893our @ISA = CFClient::UI::Container::;
894
895sub size_request {
896 my ($self) = @_;
897
898 my @alloc = map [$_->size_request], @{$self->{children}};
899
900 (
901 (List::Util::sum map $_->[0], @alloc),
902 (List::Util::max map $_->[1], @alloc),
903 )
904}
905
906sub size_allocate {
907 my ($self, $w, $h) = @_;
908
909 ($h, $w) = ($w, $h);
910
911 my $children = $self->{children};
912
913 my @h = map +($_->size_request)[0], @$children;
914
915 my $req_h = List::Util::sum @h;
916
917 if ($req_h > $h) {
918 # ah well, not enough space
919 $_ *= $h / $req_h for @h;
920 } else {
921 my $exp = List::Util::sum map $_->{expand}, @$children;
922 $exp ||= 1;
923
924 for (0 .. $#$children) {
925 my $child = $children->[$_];
926
927 my $alloc_h = $h[$_];
928 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
929 $h[$_] = $alloc_h;
930 }
931 }
932
933 CFClient::UI::harmonize \@h;
934
935 my $y = 0;
936 for (0 .. $#$children) {
937 my $child = $children->[$_];
938 my $h = $h[$_];
939 $child->configure ($y, 0, $h, $w);
940
941 $y += $h;
942 }
943
944 1
945}
946
947#############################################################################
948
949package CFClient::UI::VBox;
950
951# TODO: wrap into common Box base class
952
953our @ISA = CFClient::UI::Container::;
954
955sub size_request {
956 my ($self) = @_;
957
958 my @alloc = map [$_->size_request], @{$self->{children}};
959
960 (
961 (List::Util::max map $_->[0], @alloc),
962 (List::Util::sum map $_->[1], @alloc),
963 )
964}
965
966sub size_allocate {
967 my ($self, $w, $h) = @_;
968
969 my $children = $self->{children};
970
971 my @h = map +($_->size_request)[1], @$children;
972
973 my $req_h = List::Util::sum @h;
974
975 if ($req_h > $h) {
976 # ah well, not enough space
977 $_ *= $h / $req_h for @h;
978 } else {
979 my $exp = List::Util::sum map $_->{expand}, @$children;
980 $exp ||= 1;
981
982 for (0 .. $#$children) {
983 my $child = $children->[$_];
984
985 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
986 }
987 }
988
989 CFClient::UI::harmonize \@h;
990
991 my $y = 0;
992 for (0 .. $#$children) {
993 my $child = $children->[$_];
994 my $h = $h[$_];
995 $child->configure (0, $y, $w, $h);
996
997 $y += $h;
998 }
999
1000 1
1001}
1002
1003#############################################################################
1004
1005package CFClient::UI::Label;
1006
1007our @ISA = CFClient::UI::Base::;
1008
1009use CFClient::OpenGL;
1010
1011sub new {
1012 my ($class, %arg) = @_;
1013
1014 my $self = $class->SUPER::new (
1015 fg => [1, 1, 1],
1016 fontsize => 1,
1017 text => "",
1018 align => -1,
1019 valign => -1,
1020 padding => 2,
1021 layout => new CFClient::Layout,
1022 %arg
1023 );
1024
1025 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
1026
1027 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1029
1030 $self
1031}
1032
1033sub escape_text {
1034 local $_ = $_[1];
1035
1036 s/&/&amp;/g;
1037 s/>/&gt;/g;
1038 s/</&lt;/g;
1039
1040 $_[1]
1041}
1042
1043sub set_text {
1044 my ($self, $text) = @_;
1045
1046 $self->{layout}->set_text ($text);
1047
1048 delete $self->{texture};
1049 $self->update;
1050}
1051
1052sub set_markup {
1053 my ($self, $markup) = @_;
1054
1055 $self->{layout}->set_markup ($markup);
1056
1057 delete $self->{texture};
1058 $self->update;
1059}
1060
1061sub size_request {
1062 my ($self) = @_;
1063
1064 $self->{layout}->set_width;
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066
1067 my ($w, $h) = $self->{layout}->size;
1068
1069 (
1070 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2,
1072 )
1073}
1074
1075sub size_allocate {
1076 my ($self, $w, $h) = @_;
1077
1078 delete $self->{texture};
1079}
1080
1081sub _draw {
1082 my ($self) = @_;
1083
1084 my $tex = $self->{texture} ||= do {
1085 $self->{layout}->set_width ($self->{w});
1086 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1087 new_from_layout CFClient::Texture $self->{layout}
1088 };
567 1089
568 glEnable GL_BLEND; 1090 glEnable GL_BLEND;
569 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1091 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
570 glEnable GL_TEXTURE_2D; 1092 glEnable GL_TEXTURE_2D;
571 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1093 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
572 1094
573 my $top = $tex[1]; 1095 glColor @{$self->{fg}};
574 $top->draw_quad (0, 0, $w, $top->{h});
575 1096
576 my $left = $tex[3]; 1097 $self->{ox} = int (
577 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 1098 $self->{align} < 0 ? $self->{padding}
1099 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1100 : ($self->{w} - $tex->{w}) * 0.5
1101 );
578 1102
579 my $right = $tex[2]; 1103 $self->{oy} = int (
580 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); 1104 $self->{valign} < 0 ? $self->{padding}
1105 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1106 : ($self->{h} - $tex->{h}) * 0.5
1107 );
581 1108
582 my $bottom = $tex[4]; 1109 $tex->draw_quad ($self->{ox}, $self->{oy});
583 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
584 1110
585 my $bg = $tex[0]; 1111 glDisable GL_TEXTURE_2D;
586 glBindTexture GL_TEXTURE_2D, $bg->{name};
587 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
588 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
589 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
590
591 my $rep_x = $cw / $bg->{w};
592 my $rep_y = $ch / $bg->{h};
593
594 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
595
596 glDisable GL_BLEND; 1112 glDisable GL_BLEND;
597 glDisable GL_TEXTURE_2D;
598
599 $self->child->draw;
600
601} 1113}
602 1114
603############################################################################# 1115#############################################################################
604 1116
605package CFClient::UI::Table; 1117package CFClient::UI::EntryBase;
606 1118
607our @ISA = CFClient::UI::Base::; 1119our @ISA = CFClient::UI::Label::;
608 1120
609use SDL::OpenGL; 1121use CFClient::OpenGL;
610 1122
611sub add { 1123sub new {
612 my ($self, $x, $y, $chld) = @_; 1124 my $class = shift;
613 my $old_chld = $self->{children}[$y][$x];
614 1125
615 $self->{children}[$y][$x] = $chld; 1126 $class->SUPER::new (
616 $chld->set_parent ($self); 1127 fg => [1, 1, 1],
617 $self->update; 1128 bg => [0, 0, 0, 0.2],
1129 active_bg => [1, 1, 1, 0.5],
1130 active_fg => [0, 0, 0],
1131 can_hover => 1,
1132 can_focus => 1,
1133 valign => 0,
1134 @_
1135 )
618} 1136}
619 1137
620sub max_row_height { 1138sub _set_text {
621 my ($self, $row) = @_; 1139 my ($self, $text) = @_;
622 1140
623 my $hs = 0; 1141 delete $self->{cur_h};
624 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
625 my $c = $self->{children}->[$row]->[$xi];
626 if ($c) {
627 my ($w, $h) = $c->size_request;
628 if ($hs < $h) { $hs = $h }
629 }
630 }
631 return $hs;
632}
633 1142
634sub max_col_width { 1143 return if $self->{text} eq $text;
635 my ($self, $col) = @_;
636 1144
637 my $ws = 0; 1145 delete $self->{texture};
638 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 1146
639 my $c = ($self->{children}->[$yi] || [])->[$col]; 1147 $self->{last_activity} = $::NOW;
640 if ($c) { 1148 $self->{text} = $text;
641 my ($w, $h) = $c->size_request; 1149
642 if ($ws < $w) { $ws = $w } 1150 $text =~ s/./*/g if $self->{hidden};
643 } 1151 $self->{layout}->set_text ("$text ");
644 } 1152
645 return $ws; 1153 $self->emit (changed => $self->{text});
1154}
1155
1156sub get_text {
1157 $_[0]{text}
646} 1158}
647 1159
648sub size_request { 1160sub size_request {
649 my ($self) = @_; 1161 my ($self) = @_;
650 1162
651 my ($hs, $ws) = (0, 0); 1163 my ($w, $h) = $self->SUPER::size_request;
652 1164
653 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 1165 ($w + 1, $h) # add 1 for cursor
654 $hs += $self->max_row_height ($yi);
655 }
656
657 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
658 my $wm = 0;
659 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
660 $wm += $self->max_col_width ($xi)
661 }
662 if ($ws < $wm) { $ws = $wm }
663 }
664
665 return ($ws, $hs);
666}
667
668sub _draw {
669 my ($self) = @_;
670
671 my $y = 0;
672 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
673 my $x = 0;
674
675 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
676
677 my $c = $self->{children}->[$yi]->[$xi];
678 if ($c) {
679 $c->move ($x, $y, 0); #TODO: Move to size_request
680 $c->draw if $c;
681 }
682
683 $x += $self->max_col_width ($xi);
684 }
685
686 $y += $self->max_row_height ($yi);
687 }
688}
689
690#############################################################################
691
692package CFClient::UI::VBox;
693
694our @ISA = CFClient::UI::Container::;
695
696use SDL::OpenGL;
697
698sub size_request {
699 my ($self) = @_;
700
701 my @alloc = map [$_->size_request], @{$self->{children}};
702
703 (
704 (List::Util::max map $_->[0], @alloc),
705 (List::Util::sum map $_->[1], @alloc),
706 )
707} 1166}
708 1167
709sub size_allocate { 1168sub size_allocate {
710 my ($self, $w, $h) = @_; 1169 my ($self, $w, $h) = @_;
711
712 return unless $self->{w} != $w || $self->{h} != $h;
713
714 $self->{w} = $w;
715 $self->{h} = $h;
716
717 return unless $self->{h};
718
719 my $children = $self->{children};
720
721 my @h = map +($_->size_request)[1], @$children;
722
723 my $req_h = List::Util::sum @h;
724
725 if ($req_h > $h) {
726 # ah well, not enough space
727 $_ = $h[$_] * $h / $req_h for @h;
728 } else {
729 my @exp = grep $_->{expand}, @$children;
730 @exp = @$children unless @exp;
731
732 my %exp = map +($_ => 1), @exp;
733
734 for (0 .. $#$children) {
735 my $child = $children->[$_];
736
737 my $alloc_h = $h[$_];
738 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
739 $h[$_] = $alloc_h;
740 }
741 }
742
743 my $y = 0;
744 for (0 .. $#$children) {
745 my $child = $children->[$_];
746 my $h = $h[$_];
747 $child->move (0, $y);
748 $child->size_allocate ($w, $h);
749
750 $y += $h;
751 }
752}
753
754#############################################################################
755
756package CFClient::UI::Label;
757
758our @ISA = CFClient::UI::Base::;
759
760use SDL::OpenGL;
761
762sub new {
763 my ($class, %arg) = @_;
764
765 my $self = $class->SUPER::new (
766 fg => [1, 1, 1],
767 height => $::FONTSIZE,
768 text => "",
769 align => -1,
770 layout => new CFClient::Layout,
771 %arg
772 );
773
774 $self->set_text ($self->{text});
775
776 $self
777}
778
779sub escape_text {
780 local $_ = $_[1];
781
782 s/&/&amp;/g;
783 s/>/&gt;/g;
784 s/</&lt;/g;
785
786 $_[1]
787}
788
789sub set_text {
790 my ($self, $text) = @_;
791
792 $self->{text} = $text;
793 $self->{layout}->set_markup ($text);
794
795 delete $self->{texture};
796 $self->update;
797}
798
799sub get_text {
800 my ($self, $text) = @_;
801
802 $self->{text}
803}
804
805sub size_request {
806 my ($self) = @_;
807
808 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height});
810 $self->{layout}->size
811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
812# (
813# $self->{texture}{w},
814# $self->{texture}{h},
815# )
816# } else {
817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
818#
819# ($w, $h)
820# }
821}
822
823sub size_allocate {
824 my ($self, $w, $h) = @_;
825
826 return unless $self->{w} != $w || $self->{h} != $h;
827
828 $self->SUPER::size_allocate ($w, $h);
829 delete $self->{texture};
830}
831
832sub update {
833 my ($self) = @_;
834
835 delete $self->{texture};
836 $self->SUPER::update;
837}
838
839sub _draw {
840 my ($self) = @_;
841
842 my $tex = $self->{texture} ||= do {
843 $self->{layout}->set_width ($self->{w});
844 new_from_layout CFClient::Texture $self->{layout}
845 };
846
847 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
849 glEnable GL_TEXTURE_2D;
850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
851
852 glColor @{$self->{fg}};
853
854 my $x =
855 $self->{align} < 0 ? 0
856 : $self->{align} > 0 ? $self->{w} - $tex->{w}
857 : ($self->{w} - $tex->{w}) * 0.5;
858
859 $tex->draw_quad ($x, 0);
860
861 glDisable GL_TEXTURE_2D;
862 glDisable GL_BLEND;
863 glGetError and die;
864}
865
866#############################################################################
867
868package CFClient::UI::Entry;
869
870our @ISA = CFClient::UI::Label::;
871
872use SDL;
873use SDL::OpenGL;
874
875sub new {
876 my $class = shift;
877
878 $class->SUPER::new (
879 fg => [1, 1, 1],
880 bg => [0, 0, 0, 0.4],
881 active_bg => [1, 1, 1],
882 active_fg => [0, 0, 0],
883 @_
884 )
885}
886
887sub _set_text {
888 my ($self, $text) = @_;
889
890 $self->{last_activity} = $::NOW;
891
892 $self->{text} = $text;
893 $self->{layout}->set_width ($self->{w});
894
895 $text =~ s/./*/g if $self->{hidden};
896
897
898 $self->{layout}->set_markup ($self->escape_text ($text));
899
900 $text = substr $text, 0, $self->{cursor};
901 utf8::encode $text;
902
903 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
904}
905
906sub size_request {
907 my ($self) = @_;
908
909 my ($w, $h) = $self->SUPER::size_request;
910
911 ($w + 1, $h) # add 1 for cursor
912}
913
914sub size_allocate {
915 my ($self, $w, $h) = @_;
916
917 return unless $self->{w} != $w || $self->{h} != $h;
918
919 $self->SUPER::size_allocate ($w, $h);
920 1170
921 $self->_set_text ($self->{text}); 1171 $self->_set_text ($self->{text});
922} 1172}
923 1173
924sub set_text { 1174sub set_text {
930} 1180}
931 1181
932sub key_down { 1182sub key_down {
933 my ($self, $ev) = @_; 1183 my ($self, $ev) = @_;
934 1184
935 my $mod = $ev->key_mod; 1185 my $mod = $ev->{mod};
936 my $sym = $ev->key_sym; 1186 my $sym = $ev->{sym};
937
938 my $uni = $ev->key_unicode; 1187 my $uni = $ev->{unicode};
939 1188
940 my $text = $self->get_text; 1189 my $text = $self->get_text;
941 1190
942 if ($sym == SDLK_BACKSPACE) { 1191 if ($sym == 8) {
943 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1192 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
944 } elsif ($sym == SDLK_DELETE) { 1193 } elsif ($sym == 127) {
945 substr $text, $self->{cursor}, 1, ""; 1194 substr $text, $self->{cursor}, 1, "";
946 } elsif ($sym == SDLK_LEFT) { 1195 } elsif ($sym == CFClient::SDLK_LEFT) {
947 --$self->{cursor} if $self->{cursor}; 1196 --$self->{cursor} if $self->{cursor};
948 } elsif ($sym == SDLK_RIGHT) { 1197 } elsif ($sym == CFClient::SDLK_RIGHT) {
949 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1198 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1199 } elsif ($sym == CFClient::SDLK_HOME) {
1200 $self->{cursor} = 0;
1201 } elsif ($sym == CFClient::SDLK_END) {
1202 $self->{cursor} = length $text;
1203 } elsif ($sym == 27) {
1204 $self->emit ('escape');
950 } elsif ($uni) { 1205 } elsif ($uni) {
951 substr $text, $self->{cursor}++, 0, chr $uni; 1206 substr $text, $self->{cursor}++, 0, chr $uni;
952 } 1207 }
953 1208
954 $self->_set_text ($text); 1209 $self->_set_text ($text);
969 $self->SUPER::button_down ($ev, $x, $y); 1224 $self->SUPER::button_down ($ev, $x, $y);
970 1225
971 my $idx = $self->{layout}->xy_to_index ($x, $y); 1226 my $idx = $self->{layout}->xy_to_index ($x, $y);
972 1227
973 # byte-index to char-index 1228 # byte-index to char-index
974 my $text = $self->{layout}; 1229 my $text = $self->{text};
975 utf8::encode $text; 1230 utf8::encode $text;
976 $self->{cursor} = length substr $text, 0, $idx; 1231 $self->{cursor} = length substr $text, 0, $idx;
977 1232
978 $self->_set_text ($self->{text}); 1233 $self->_set_text ($self->{text});
979 $self->update; 1234 $self->update;
994 $self->{fg} = $self->{active_fg}; 1249 $self->{fg} = $self->{active_fg};
995 } else { 1250 } else {
996 glColor @{$self->{bg}}; 1251 glColor @{$self->{bg}};
997 } 1252 }
998 1253
1254 glEnable GL_BLEND;
1255 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
999 glBegin GL_QUADS; 1256 glBegin GL_QUADS;
1000 glVertex 0 , 0; 1257 glVertex 0 , 0;
1001 glVertex 0 , $self->{h}; 1258 glVertex 0 , $self->{h};
1002 glVertex $self->{w}, $self->{h}; 1259 glVertex $self->{w}, $self->{h};
1003 glVertex $self->{w}, 0; 1260 glVertex $self->{w}, 0;
1004 glEnd; 1261 glEnd;
1262 glDisable GL_BLEND;
1005 1263
1006 $self->SUPER::_draw; 1264 $self->SUPER::_draw;
1007 1265
1008 #TODO: force update every cursor change :( 1266 #TODO: force update every cursor change :(
1009 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1267 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1268
1269 unless (exists $self->{cur_h}) {
1270 my $text = substr $self->{text}, 0, $self->{cursor};
1271 utf8::encode $text;
1272
1273 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1274 }
1275
1010 glColor @{$self->{fg}}; 1276 glColor @{$self->{fg}};
1011 glBegin GL_LINES; 1277 glBegin GL_LINES;
1012 glVertex $self->{cur_x}, $self->{cur_y};
1013 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1278 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1279 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1014 glEnd; 1280 glEnd;
1015 } 1281 }
1016} 1282}
1017 1283
1018#############################################################################
1019
1020package CFClient::UI::Slider; 1284package CFClient::UI::Entry;
1021 1285
1022use strict;
1023
1024use SDL::OpenGL;
1025
1026our @ISA = CFClient::UI::DrawBG::; 1286our @ISA = CFClient::UI::EntryBase::;
1027 1287
1028sub size_request { 1288use CFClient::OpenGL;
1289
1290sub key_down {
1029 my ($self) = @_; 1291 my ($self, $ev) = @_;
1030 1292
1031 my $w = 50; 1293 my $sym = $ev->{sym};
1032 my $h = 10;
1033 1294
1034 $self->{vertical} ? ($h, $w) : ($w, $h) 1295 if ($sym == 13) {
1296 $self->emit (activate => $self->get_text);
1297 $self->update;
1298
1299 } else {
1300 $self->SUPER::key_down ($ev);
1301 }
1302
1035} 1303}
1304
1305#############################################################################
1306
1307package CFClient::UI::Button;
1308
1309our @ISA = CFClient::UI::Label::;
1310
1311use CFClient::OpenGL;
1312
1313my @tex =
1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1315 qw(b1_button_active.png);
1036 1316
1037sub new { 1317sub new {
1038 my $class = shift; 1318 my $class = shift;
1039 1319
1320 $class->SUPER::new (
1321 padding => 4,
1322 fg => [1, 1, 1],
1323 bg => [1, 1, 1, 0.2],
1324 active_fg => [0, 0, 1],
1325 can_hover => 1,
1326 align => 0,
1327 valign => 0,
1328 @_
1329 )
1330}
1331
1332sub button_up {
1333 my ($self, $ev, $x, $y) = @_;
1334
1335 if ($x >= 0 && $x < $self->{w}
1336 && $y >= 0 && $y < $self->{h}) {
1337 $self->emit ("activate");
1338 }
1339}
1340
1341sub _draw {
1342 my ($self) = @_;
1343
1344 local $self->{fg} = $self->{fg};
1345
1346 if ($GRAB == $self) {
1347 $self->{fg} = $self->{active_fg};
1348 }
1349
1350 glEnable GL_BLEND;
1351 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1352 glEnable GL_TEXTURE_2D;
1353 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1354 glColor 0, 0, 0, 1;
1355
1356 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1357
1358 glDisable GL_TEXTURE_2D;
1359 glDisable GL_BLEND;
1360
1361 $self->SUPER::_draw;
1362}
1363
1364#############################################################################
1365
1366package CFClient::UI::CheckBox;
1367
1368our @ISA = CFClient::UI::DrawBG::;
1369
1370my @tex =
1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1372 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1373
1374use CFClient::OpenGL;
1375
1376sub new {
1377 my $class = shift;
1378
1379 $class->SUPER::new (
1380 padding => 2,
1381 fg => [1, 1, 1],
1382 active_fg => [1, 1, 0],
1383 state => 0,
1384 can_hover => 1,
1385 @_
1386 )
1387}
1388
1389sub size_request {
1390 my ($self) = @_;
1391
1392 ($self->{padding} * 2 + 6) x 2
1393}
1394
1395sub button_down {
1396 my ($self, $ev, $x, $y) = @_;
1397
1398 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1399 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1400 $self->{state} = !$self->{state};
1401 $self->emit (changed => $self->{state});
1402 }
1403}
1404
1405sub _draw {
1406 my ($self) = @_;
1407
1408 $self->SUPER::_draw;
1409
1410 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1411
1412 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1413
1414 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1415
1416 glEnable GL_BLEND;
1417 glEnable GL_TEXTURE_2D;
1418 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1419
1420 my $tex = $self->{state} ? $tex[1] : $tex[0];
1421
1422 $tex->draw_quad (0, 0, $s, $s);
1423
1424 glDisable GL_TEXTURE_2D;
1425 glDisable GL_BLEND;
1426}
1427
1428#############################################################################
1429
1430package CFClient::UI::VGauge;
1431
1432our @ISA = CFClient::UI::Base::;
1433
1434use CFClient::OpenGL;
1435
1436my %tex = (
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new {
1457 my $class = shift;
1458
1459 my $self = $class->SUPER::new (
1460 gauge => 'food',
1461 @_
1462 );
1463
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h};
1465
1466 $self
1467}
1468
1469sub size_request {
1470 my ($self) = @_;
1471
1472 my $tex = $tex{$self->{gauge}}[0];
1473
1474 @$tex{qw(w h)}
1475}
1476
1477sub set_max {
1478 my ($self, $max) = @_;
1479
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493}
1494
1495sub _draw {
1496 my ($self) = @_;
1497
1498 my $tex = $tex{$self->{gauge}};
1499
1500 my ($w, $h) = ($self->{w}, $self->{h});
1501
1502 my $ycut = $self->{val} / ($self->{max_val} || 1);
1503 $ycut = 1 if $self->{val} > $self->{max_val};
1504
1505 my $t1 = $tex->[0];
1506 my $t2 = $tex->[1];
1507
1508 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512
1513 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h};
1515
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1524 glEnd;
1525
1526 $yp += $h1;
1527
1528 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1534 glEnd;
1535
1536 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D;
1538}
1539
1540#############################################################################
1541
1542package CFClient::UI::Slider;
1543
1544use strict;
1545
1546use CFClient::OpenGL;
1547
1548our @ISA = CFClient::UI::DrawBG::;
1549
1550my @tex =
1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1552 qw(s1_slider.png s1_slider_bg.png);
1553
1554sub new {
1555 my $class = shift;
1556
1040 # range [value, low, high, page] 1557 # range [value, low, high, page]
1041 1558
1559 # TODO: 0-width page
1560 # TODO: req_w/h are wrong with vertical
1561 # TODO: calculations are off
1042 $class->SUPER::new ( 1562 my $self = $class->SUPER::new (
1043 fg => [1, 1, 1], 1563 fg => [1, 1, 1],
1044 active_fg => [0, 0, 0], 1564 active_fg => [0, 0, 0],
1045 range => [0, 0, 100, 10], 1565 range => [0, 0, 100, 10],
1566 req_w => 20,
1567 req_h => 20,
1046 vertical => 1, 1568 vertical => 0,
1569 can_hover => 1,
1570 inner_pad => 5,
1047 @_ 1571 @_
1048 ) 1572 );
1573
1574 $self
1575}
1576
1577sub size_request {
1578 my ($self) = @_;
1579
1580 my $w = $self->{req_w};
1581 my $h = $self->{req_h};
1582
1583 $self->{vertical} ? ($h, $w) : ($w, $h)
1049} 1584}
1050 1585
1051sub button_down { 1586sub button_down {
1052 my ($self, $ev, $x, $y) = @_; 1587 my ($self, $ev, $x, $y) = @_;
1053 1588
1061 if ($GRAB == $self) { 1596 if ($GRAB == $self) {
1062 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1597 my ($value, $lo, $hi, $page) = @{$self->{range}};
1063 1598
1064 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1599 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1065 1600
1601 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1602 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1603
1604 $x -= $inner_pad_px; # substract the padding
1066 $x = $x * ($hi - $lo) / $w + $lo; 1605 $x = $x * ($hi - $lo) / $inner_w + $lo;
1067 $x = $lo if $x < $lo; 1606 $x = $lo if $x < $lo;
1068 $x = $hi - $page if $x > $hi - $page; 1607 $x = $hi - $page if $x > $hi - $page;
1069 $self->{range}[0] = $x; 1608 $self->{range}[0] = $x;
1070 1609
1071 $self->emit (changed => $x); 1610 $self->emit (changed => $x);
1072 $self->update; 1611 $self->update;
1073 } 1612 }
1613}
1614
1615# the inner_* stuff is for generating a padding for the slider handle,
1616# so that the handle doesn't leave the texture. This calculation isn't 100%
1617# correct propably, but it does the job for now
1618sub _calc_inner_pad_px {
1619 my ($self, $w) = @_;
1620 ($w / 100) * $self->{inner_pad} # % to pixels
1074} 1621}
1075 1622
1076sub _draw { 1623sub _draw {
1077 my ($self) = @_; 1624 my ($self) = @_;
1078 1625
1092 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1639 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1093 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1640 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1094 1641
1095 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1642 my ($value, $lo, $hi, $page) = @{$self->{range}};
1096 1643
1644 $hi = $value + 1 if $lo == $hi;
1645
1646 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1647 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1648
1097 $page = int $page * $w / ($hi - $lo); 1649 $page = int $page * $inner_w / ($hi - $lo);
1098 $value = int +($value - $lo) * $w / ($hi - $lo); 1650 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1099 1651
1100 $w -= $page; 1652 $w -= $page;
1101 $page &= ~1; 1653 $page &= ~1;
1102 glTranslate $page * 0.5, 0, 0; 1654 glTranslate $page * 0.5, 0, 0;
1655 $page ||= 2;
1103 1656
1104 glColor @$fg;
1105 glBegin GL_LINES;
1106 glVertex 0, 0; glVertex 0, $h;
1107 glVertex $w - 1, 0; glVertex $w - 1, $h;
1108 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1109 glEnd;
1110
1111 my $knob_a = $value - $page * 0.5; 1657 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1112 my $knob_b = $value + $page * 0.5; 1658 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1113 1659
1114 glBegin GL_QUADS; 1660 glEnable GL_BLEND;
1115 glColor @$fg; 1661 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1116 glVertex $knob_a, 0; 1662 glEnable GL_TEXTURE_2D;
1117 glVertex $knob_a, $h; 1663 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1118 glVertex $knob_b, $h;
1119 glVertex $knob_b, 0;
1120 1664
1121 if ($knob_a < $knob_b - 2) { 1665 # draw background
1122 glColor @$bg; 1666 $tex[1]->draw_quad (0, 0, $w, $h);
1123 glVertex $knob_a + 1, 1;
1124 glVertex $knob_a + 1, $h - 1;
1125 glVertex $knob_b - 1, $h - 1;
1126 glVertex $knob_b - 1, 1;
1127 }
1128 glEnd;
1129}
1130 1667
1131############################################################################# 1668 # draw handle
1669 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1132 1670
1671 glDisable GL_BLEND;
1672 glDisable GL_TEXTURE_2D;
1673}
1674
1675#############################################################################
1676
1133package CFClient::UI::MapWidget; 1677package CFClient::UI::TextView;
1134 1678
1135use strict;
1136
1137use List::Util qw(min max);
1138
1139use SDL;
1140use SDL::OpenGL;
1141
1142our @ISA = CFClient::UI::Base::; 1679our @ISA = CFClient::UI::HBox::;
1680
1681use CFClient::OpenGL;
1143 1682
1144sub new { 1683sub new {
1145 my $class = shift; 1684 my $class = shift;
1146 1685
1147 $class->SUPER::new ( 1686 my $self = $class->SUPER::new (
1148 z => -1, 1687 fontsize => 1,
1149 list => (glGenLists 1),
1150 @_ 1688 @_,
1689
1690 layout => (new CFClient::Layout),
1691 par => [],
1692 height => 0,
1693 children => [
1694 (new CFClient::UI::Empty expand => 1),
1695 (new CFClient::UI::Slider vertical => 1),
1696 ],
1151 ) 1697 );
1152}
1153 1698
1154sub key_down { 1699 $self->{children}[1]->connect (changed => sub {
1155 print "MAPKEYDOWN\n"; 1700 $self->update;
1156} 1701 });
1157 1702
1158sub key_up { 1703 $self
1159} 1704}
1160 1705
1161sub size_request { 1706sub set_fontsize {
1707 my ($self, $fontsize) = @_;
1708
1709 $self->{fontsize} = $fontsize;
1710 $self->reflow;
1711}
1712
1713sub text_height {
1714 my ($self, $text) = @_;
1715
1716 my $layout = $self->{layout};
1717
1718 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1719 $layout->set_width ($self->{w});
1720 $layout->set_text ($text);
1162 ( 1721
1163 1 + int $::WIDTH / 32, 1722 ($layout->size)[1]
1164 1 + int $::HEIGHT / 32, 1723}
1165 ) 1724
1725sub reflow {
1726 my ($self) = @_;
1727
1728 $self->{need_reflow}++;
1729 $self->update;
1730}
1731
1732sub size_allocate {
1733 my ($self, $w, $h) = @_;
1734
1735 $self->SUPER::size_allocate ($w, $h);
1736
1737 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1738 $self->{layout}->set_width ($self->{children}[0]{w});
1739
1740 $self->reflow;
1741}
1742
1743sub add_paragraph {
1744 my ($self, $color, $text) = @_;
1745
1746 #TODO: intelligently "reformat" paragraph
1747
1748 my $height = $self->text_height ($text);
1749
1750 $self->{height} += $height;
1751
1752 push @{$self->{par}}, [$height, $color, $text];
1753
1754 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1755 $self->{children}[1]->update;
1166} 1756}
1167 1757
1168sub update { 1758sub update {
1169 my ($self) = @_; 1759 my ($self) = @_;
1170 1760
1171 $self->{need_update} = 1;
1172 $self->SUPER::update; 1761 $self->SUPER::update;
1762
1763 return unless $self->{h} > 0;
1764
1765 delete $self->{texture};
1766
1767 $ROOT->on_refresh ($self, sub {
1768 if (delete $self->{need_reflow}) {
1769 my $height = 0;
1770
1771 $height += $_->[0] = $self->text_height ($_->[2])
1772 for @{$self->{par}};
1773
1774 $self->{height} = $height;
1775
1776 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1777
1778 delete $self->{texture};
1779 }
1780
1781 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1782 glClearColor 0, 0, 0, 1;
1783 glClear GL_COLOR_BUFFER_BIT;
1784
1785 glEnable GL_BLEND;
1786 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1787 glEnable GL_TEXTURE_2D;
1788 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1789
1790 my $top = int $self->{children}[1]{range}[0];
1791
1792 my $y0 = $top;
1793 my $y1 = $top + $self->{h};
1794
1795 my $y = 0;
1796
1797 my $layout = $self->{layout};
1798
1799 for my $par (@{$self->{par}}) {
1800 my $h = $par->[0];
1801
1802 if ($y0 < $y + $h && $y < $y1) {
1803 $layout->set_text ($par->[2]);
1804
1805 glColor @{ $par->[1] };
1806 my ($W, $H) = $layout->size;
1807 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1808 }
1809
1810 $y += $h;
1811 }
1812
1813 glDisable GL_TEXTURE_2D;
1814 glDisable GL_BLEND;
1815 };
1816 });
1173} 1817}
1174 1818
1175sub _draw { 1819sub _draw {
1176 my ($self) = @_; 1820 my ($self) = @_;
1177 1821
1178 if (delete $self->{need_update}) { 1822 if ($self->{texture}) {
1179 glNewList $self->{list}, GL_COMPILE;
1180
1181 my $mx = $::CONN->{mapx};
1182 my $my = $::CONN->{mapy};
1183
1184 my $map = $::CONN->{map};
1185
1186 my ($xofs, $yofs);
1187
1188 my $sw = 1 + int $::WIDTH / 32;
1189 my $sh = 1 + int $::HEIGHT / 32;
1190
1191 if ($::CONN->{mapw} > $sw) {
1192 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1193 } else {
1194 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1195 }
1196
1197 if ($::CONN->{maph} > $sh) {
1198 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1199 } else {
1200 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1201 }
1202
1203 glEnable GL_TEXTURE_2D; 1823 glEnable GL_TEXTURE_2D;
1204 glEnable GL_BLEND;
1205 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1824 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1207 1825 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1208 my $sw4 = ($sw + 3) & ~3;
1209 my $darkness = "\x00" x ($sw4 * $sh);
1210
1211 for my $x (0 .. $sw - 1) {
1212 my $row = $map->[$x + $xofs];
1213 for my $y (0 .. $sh - 1) {
1214
1215 my $cell = $row->[$y + $yofs]
1216 or next;
1217
1218 my $dark = $cell->[0];
1219 if ($dark < 0) {
1220 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1221 } else {
1222 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1223 }
1224
1225 for my $num (grep $_, @$cell[1,2,3]) {
1226 my $tex = $::CONN->{face}[$num]{texture} || next;
1227
1228 my ($w, $h) = @$tex{qw(w h)};
1229
1230 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1231 }
1232 }
1233 }
1234
1235# if (1) { # higher quality darkness
1236# $lighting =~ s/(.)/$1$1$1/gs;
1237# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1238#
1239# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1240#
1241# $lighting = $pb->get_pixels;
1242# $lighting =~ s/(.)../$1/gs;
1243# }
1244
1245 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1246
1247 $darkness = new CFClient::Texture
1248 w => $sw4,
1249 h => $sh,
1250 data => $darkness,
1251 internalformat => GL_ALPHA,
1252 format => GL_ALPHA;
1253
1254 glColor 0.45, 0.45, 0.45, 1;
1255 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1256
1257 glDisable GL_TEXTURE_2D; 1826 glDisable GL_TEXTURE_2D;
1258 glDisable GL_BLEND;
1259
1260 glEndList;
1261 } 1827 }
1262 1828
1263 glCallList $self->{list}; 1829 $self->{children}[1]->draw;
1264}
1265 1830
1266my %DIR = (
1267 SDLK_KP8, [1, "north"],
1268 SDLK_KP9, [2, "northeast"],
1269 SDLK_KP6, [3, "east"],
1270 SDLK_KP3, [4, "southeast"],
1271 SDLK_KP2, [5, "south"],
1272 SDLK_KP1, [6, "southwest"],
1273 SDLK_KP4, [7, "west"],
1274 SDLK_KP7, [8, "northwest"],
1275
1276 SDLK_UP, [1, "north"],
1277 SDLK_RIGHT, [3, "east"],
1278 SDLK_DOWN, [5, "south"],
1279 SDLK_LEFT, [7, "west"],
1280);
1281
1282sub key_down {
1283 my ($self, $ev) = @_;
1284
1285 my $mod = $ev->key_mod;
1286 my $sym = $ev->key_sym;
1287
1288 if ($sym == SDLK_KP5) {
1289 $::CONN->send ("command stay fire");
1290 } elsif (exists $DIR{$sym}) {
1291 if ($mod & KMOD_SHIFT) {
1292 $self->{shft}++;
1293 $::CONN->send ("command fire $DIR{$sym}[0]");
1294 } elsif ($mod & KMOD_CTRL) {
1295 $self->{ctrl}++;
1296 $::CONN->send ("command run $DIR{$sym}[0]");
1297 } else {
1298 $::CONN->send ("command $DIR{$sym}[1]");
1299 }
1300 }
1301}
1302
1303sub key_up {
1304 my ($self, $ev) = @_;
1305
1306 my $mod = $ev->key_mod;
1307 my $sym = $ev->key_sym;
1308
1309 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1310 $::CONN->send ("command fire_stop");
1311 }
1312 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1313 $::CONN->send ("command run_stop");
1314 }
1315} 1831}
1316 1832
1317############################################################################# 1833#############################################################################
1318 1834
1319package CFClient::UI::Animator; 1835package CFClient::UI::Animator;
1320 1836
1321use SDL::OpenGL; 1837use CFClient::OpenGL;
1322 1838
1323our @ISA = CFClient::UI::Bin::; 1839our @ISA = CFClient::UI::Bin::;
1324 1840
1325sub moveto { 1841sub moveto {
1326 my ($self, $x, $y) = @_; 1842 my ($self, $x, $y) = @_;
1356 glPopMatrix; 1872 glPopMatrix;
1357} 1873}
1358 1874
1359############################################################################# 1875#############################################################################
1360 1876
1361package CFClient::UI::Toplevel; 1877package CFClient::UI::Flopper;
1878
1879our @ISA = CFClient::UI::Button::;
1880
1881sub new {
1882 my $class = shift;
1883
1884 my $self = $class->SUPER::new (
1885 state => 0,
1886 connect_activate => \&toggle_flopper,
1887 @_
1888 );
1889
1890 if ($self->{state}) {
1891 $self->{state} = 0;
1892 $self->toggle_flopper;
1893 }
1894
1895 $self
1896}
1897
1898sub toggle_flopper {
1899 my ($self) = @_;
1900
1901 # TODO: use animation
1902 if ($self->{state} = !$self->{state}) {
1903 $CFClient::UI::ROOT->add ($self->{other});
1904 $self->{other}->move ($self->coord2global (0, $self->{h}));
1905 $self->emit ("open");
1906 } else {
1907 $CFClient::UI::ROOT->remove ($self->{other});
1908 $self->emit ("close");
1909 }
1910
1911 $self->emit (changed => $self->{state});
1912}
1913
1914#############################################################################
1915
1916package CFClient::UI::Root;
1362 1917
1363our @ISA = CFClient::UI::Container::; 1918our @ISA = CFClient::UI::Container::;
1919
1920use CFClient::OpenGL;
1921
1922sub check_size {
1923 my ($self) = @_;
1924
1925 $self->configure (0, 0, $::WITH, $::HEIGHT);
1926}
1364 1927
1365sub size_request { 1928sub size_request {
1366 ($::WIDTH, $::HEIGHT) 1929 ($::WIDTH, $::HEIGHT)
1367} 1930}
1368 1931
1369sub size_allocate { 1932sub configure {
1370 my ($self, $w, $h) = @_; 1933 my ($self, $x, $y, $w, $h) = @_;
1371 1934
1372 $self->SUPER::size_allocate ($w, $h); 1935 $self->SUPER::configure ($x, $y, $w, $h);
1373 1936
1374 $_->size_allocate ($_->size_request) 1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1375 for @{$self->{children}}; 1938 for @{$self->{children}};
1376} 1939}
1377 1940
1378sub translate { 1941sub _topleft {
1379 my ($self, $x, $y) = @_; 1942 my ($self, $x, $y) = @_;
1380 1943
1381 ($x, $y) 1944 ($x, $y)
1382} 1945}
1383 1946
1384sub update { 1947sub update {
1385 my ($self) = @_; 1948 my ($self) = @_;
1386 1949
1387 $self->size_allocate ($self->size_request); 1950 $self->check_size;
1388 ::refresh (); 1951 ::refresh ();
1389} 1952}
1390 1953
1391sub add { 1954sub add {
1392 my ($self, $widget) = @_; 1955 my ($self, $child) = @_;
1393 1956
1394 $self->SUPER::add ($widget); 1957 $self->SUPER::add ($child);
1958}
1395 1959
1396 $widget->size_allocate ($widget->size_request); 1960sub on_refresh {
1961 my ($self, $id, $cb) = @_;
1962
1963 $self->{refresh_hook}{$id} = $cb;
1397} 1964}
1398 1965
1399sub draw { 1966sub draw {
1400 my ($self) = @_; 1967 my ($self) = @_;
1401 1968
1969 while (my $rcb = delete $self->{refresh_hook}) {
1970 $_->() for values %$rcb;
1971 }
1972
1973 glViewport 0, 0, $::WIDTH, $::HEIGHT;
1974 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
1975 glClear GL_COLOR_BUFFER_BIT;
1976
1977 glMatrixMode GL_PROJECTION;
1978 glLoadIdentity;
1979 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
1980 glMatrixMode GL_MODELVIEW;
1981 glLoadIdentity;
1982
1402 $self->_draw; 1983 $self->_draw;
1403} 1984}
1404 1985
1405############################################################################# 1986#############################################################################
1406 1987
1407package CFClient::UI; 1988package CFClient::UI;
1408 1989
1409$TOPLEVEL = new CFClient::UI::Toplevel; 1990$ROOT = new CFClient::UI::Root;
1410 1991
14111 19921
1412 1993

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines