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.64 by root, Tue Apr 11 13:38:22 2006 UTC vs.
Revision 1.78 by root, Tue Apr 11 22:49:13 2006 UTC

1package CFClient::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6 6use List::Util ();
7use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9 7
10use CFClient; 8use CFClient;
11 9
12our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
13 11
45 43
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47 45
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49 47
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51 49
52 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab; 52 $grab->update if $grab;
55 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
70 } 68 }
71 69
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73} 71}
74 72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
78
79use SDL::OpenGL;
80
75sub new { 81sub new {
76 my $class = shift; 82 my $class = shift;
77 83
78 bless { @_ }, $class 84 bless {
85 x => 0,
86 y => 0,
87 z => 0,
88 w => -1,
89 h => -1,
90 @_
91 }, $class
79} 92}
80 93
81sub move { 94sub move {
82 my ($self, $x, $y, $z) = @_; 95 my ($self, $x, $y, $z) = @_;
83 $self->{x} = $x; 96 $self->{x} = $x;
92sub size_request { 105sub size_request {
93 require Carp; 106 require Carp;
94 Carp::confess "size_request is abtract"; 107 Carp::confess "size_request is abtract";
95} 108}
96 109
97sub size_allocate { 110sub _size_allocate {
98 my ($self, $w, $h) = @_; 111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
99 117
100 $self->{w} = $w; 118 $self->{w} = $w;
101 $self->{h} = $h; 119 $self->{h} = $h;
120
121 1
122}
123
124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
102} 128}
103 129
104# translate global koordinates to local coordinate system 130# translate global koordinates to local coordinate system
105sub translate { 131sub translate {
106 my ($self, $x, $y) = @_; 132 my ($self, $x, $y) = @_;
109} 135}
110 136
111sub focus_in { 137sub focus_in {
112 my ($self) = @_; 138 my ($self) = @_;
113 139
140 return if $FOCUS == $self;
141
114 my $focus = $FOCUS; $FOCUS = $self; 142 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus; 143 $focus->update if $focus;
116 $FOCUS->update; 144 $FOCUS->update;
117} 145}
118 146
125 $focus->update if $focus; #? 153 $focus->update if $focus; #?
126} 154}
127 155
128sub mouse_motion { } 156sub mouse_motion { }
129sub button_up { } 157sub button_up { }
130sub button_down { }
131sub key_down { } 158sub key_down { }
132sub key_up { } 159sub key_up { }
160
161sub button_down {
162 my ($self, $ev, $x, $y) = @_;
163
164 $self->focus_in;
165}
133 166
134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 167sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 168sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 169sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 170sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 171sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
139 172
140sub draw { 173sub draw {
141 my ($self) = @_; 174 my ($self) = @_;
142 175
176 return unless $self->{h} && $self->{w};
177
143 glPushMatrix; 178 glPushMatrix;
144 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
145 $self->_draw; 180 $self->_draw;
181 glPopMatrix;
182
146 if ($self == $HOVER) { 183 if ($self == $HOVER) {
147 glColor 1, 1, 1, 0.4; 184 my ($x, $y) = @$self{qw(x y)};
185
186 glColor 0, 0, 1, 0.2;
148 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
149 glBegin GL_QUADS; 189 glBegin GL_QUADS;
150 glVertex 0 , 0; 190 glVertex $x , $y;
151 glVertex $self->{w}, 0; 191 glVertex $x + $self->{w}, $y;
152 glVertex $self->{w}, $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
153 glVertex 0 , $self->{h}; 193 glVertex $x , $y + $self->{h};
154 glEnd; 194 glEnd;
155 glDisable GL_BLEND; 195 glDisable GL_BLEND;
156 } 196 }
157 glPopMatrix;
158} 197}
159 198
160sub _draw { 199sub _draw {
161 my ($self) = @_; 200 my ($self) = @_;
162 201
163 warn "no draw defined for $self\n"; 202 warn "no draw defined for $self\n";
164}
165
166sub bbox {
167 my ($self) = @_;
168 my ($w, $h) = $self->size_request;
169 (
170 $self->{x},
171 $self->{y},
172 $self->{x} = $w,
173 $self->{y} = $h
174 )
175} 203}
176 204
177sub find_widget { 205sub find_widget {
178 my ($self, $x, $y) = @_; 206 my ($self, $x, $y) = @_;
179 207
182 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 210 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
183 211
184 () 212 ()
185} 213}
186 214
187sub del_parent { $_[0]->{parent} = undef }
188
189sub set_parent { 215sub set_parent {
190 my ($self, $par) = @_; 216 my ($self, $par) = @_;
191 217
192 $self->{parent} = $par; 218 $self->{parent} = $par;
193 Scalar::Util::weaken $self->{parent}; 219 Scalar::Util::weaken $self->{parent};
202 228
203 $self->{parent}->update 229 $self->{parent}->update
204 if $self->{parent}; 230 if $self->{parent};
205} 231}
206 232
233sub connect {
234 my ($self, $signal, $cb) = @_;
235
236 push @{ $self->{cb}{$signal} }, $cb;
237}
238
239sub emit {
240 my ($self, $signal, @args) = @_;
241
242 $_->($self, @args)
243 for @{$self->{cb}{$signal} || []};
244}
245
207sub DESTROY { 246sub DESTROY {
208 my ($self) = @_; 247 my ($self) = @_;
209 248
210 #$self->deactivate; 249 #$self->deactivate;
211} 250}
212 251
213############################################################################# 252#############################################################################
214 253
254package CFClient::UI::DrawBG;
255
256our @ISA = CFClient::UI::Base::;
257
258use strict;
259use SDL::OpenGL;
260
261sub new {
262 my $class = shift;
263
264 # range [value, low, high, page]
265
266 $class->SUPER::new (
267 bg => [0, 0, 0, 0.2],
268 active_bg => [1, 1, 1],
269 @_
270 )
271}
272
273sub _draw {
274 my ($self) = @_;
275
276 my ($w, $h) = @$self{qw(w h)};
277
278 glEnable GL_BLEND;
279 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
280 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
281
282 glBegin GL_QUADS;
283 glVertex 0 , 0;
284 glVertex 0 , $h;
285 glVertex $w, $h;
286 glVertex $w, 0;
287 glEnd;
288
289 glDisable GL_BLEND;
290}
291
292#############################################################################
293
294package CFClient::UI::Empty;
295
296our @ISA = CFClient::UI::Base::;
297
298sub size_request {
299 (0, 0)
300}
301
302sub draw { }
303
304#############################################################################
305
215package CFClient::Widget::Container; 306package CFClient::UI::Container;
216 307
217our @ISA = CFClient::Widget::; 308our @ISA = CFClient::UI::Base::;
218 309
219sub new { 310sub new {
220 my ($class, %arg) = @_; 311 my ($class, %arg) = @_;
221 312
222 my $children = delete $arg{children} || []; 313 my $children = delete $arg{children} || [];
223 314
224 my $self = $class->SUPER::new (children => []); 315 my $self = $class->SUPER::new (children => [], %arg);
225 $self->add ($_) for @$children; 316 $self->add ($_) for @$children;
226 317
227 $self 318 $self
228} 319}
229 320
230sub add { 321sub add {
231 my ($self, $chld, $expand) = @_; 322 my ($self, $chld) = @_;
232 323
233 $chld->{expand} = $expand;
234 $chld->set_parent ($self); 324 $chld->set_parent ($self);
235 325
236 @{$self->{children}} = 326 $self->{children} = [
237 sort { $a->{z} <=> $b->{z} } 327 sort { $a->{z} <=> $b->{z} }
238 @{$self->{children}}, $chld; 328 @{$self->{children}}, $chld
329 ];
239 330
240 $self->size_allocate ($self->{w}, $self->{h}) 331 $self->{w} = $self->{h} = -1;
241 if $self->{w}; #TODO: check for "realised state" 332 $self->update;
242} 333}
243 334
244sub remove { 335sub remove {
245 my ($self, $widget) = @_; 336 my ($self, $widget) = @_;
246 337
247 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 338 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
248 339
249 $self->size_allocate ($self->{w}, $self->{h}); 340 $self->size_allocate (0, 0, $self->{w}, $self->{h});
250} 341}
251 342
252sub find_widget { 343sub find_widget {
253 my ($self, $x, $y) = @_; 344 my ($self, $x, $y) = @_;
254 345
271 $_->draw for @{$self->{children}}; 362 $_->draw for @{$self->{children}};
272} 363}
273 364
274############################################################################# 365#############################################################################
275 366
276package CFClient::Widget::Bin; 367package CFClient::UI::Bin;
277 368
278our @ISA = CFClient::Widget::Container::; 369our @ISA = CFClient::UI::Container::;
370
371sub new {
372 my ($class, %arg) = @_;
373
374 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
375
376 $class->SUPER::new (children => [$child], %arg)
377}
378
379sub add {
380 my ($self, $widget) = @_;
381
382 $self->{children} = [];
383
384 $self->SUPER::add ($widget);
385}
386
387sub remove {
388 my ($self, $widget) = @_;
389
390 $self->SUPER::remove ($widget);
391
392 $self->{children} = [new CFClient::UI::Empty]
393 unless @{$self->{children}};
394}
279 395
280sub child { $_[0]->{children}[0] } 396sub child { $_[0]->{children}[0] }
281 397
282sub size_request { 398sub size_request {
283 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 399 $_[0]{children}[0]->size_request
284} 400}
285 401
286sub size_allocate { 402sub size_allocate {
287 my ($self, $w, $h) = @_; 403 my ($self, $x, $y, $w, $h) = @_;
288 404
289 $self->SUPER::size_allocate ($w, $h); 405 $self->_size_allocate ($x, $y, $w, $h) or return;
406
290 $self->{children}[0]->size_allocate ($w, $h) 407 $self->{children}[0]->size_allocate (0, 0, $w, $h);
291 if $self->{children}[0]
292} 408}
293 409
294############################################################################# 410#############################################################################
295 411
296package CFClient::Widget::Window; 412package CFClient::UI::Window;
297 413
298our @ISA = CFClient::Widget::Bin::; 414our @ISA = CFClient::UI::Bin::;
299 415
300use SDL::OpenGL; 416use SDL::OpenGL;
301 417
302sub new { 418sub new {
303 my ($class, %arg) = @_; 419 my ($class, %arg) = @_;
321 $self->{w}, $self->{h}, sub { $self->child->draw } 437 $self->{w}, $self->{h}, sub { $self->child->draw }
322 ); 438 );
323} 439}
324 440
325sub size_allocate { 441sub size_allocate {
326 my ($self, $w, $h) = @_; 442 my ($self, $x, $y, $w, $h) = @_;
327 443
328 $self->{w} = $w; 444 $self->_size_allocate ($x, $y, $w, $h) or return;
329 $self->{h} = $h;
330 445
331 $self->child->size_allocate ($w, $h); 446 $self->child->size_allocate (0, 0, $w, $h);
332 447
333 $self->render_chld; 448 $self->render_chld;
334} 449}
335 450
336sub _draw { 451sub _draw {
340 455
341 my $tex = $self->{texture} 456 my $tex = $self->{texture}
342 or return; 457 or return;
343 458
344 glEnable GL_BLEND; 459 glEnable GL_BLEND;
460 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
345 glEnable GL_TEXTURE_2D; 461 glEnable GL_TEXTURE_2D;
346 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 462 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
347 463
348 $tex->draw_quad (0, 0, $w, $h); 464 $tex->draw_quad (0, 0, $w, $h);
349 465
351 glDisable GL_TEXTURE_2D; 467 glDisable GL_TEXTURE_2D;
352} 468}
353 469
354############################################################################# 470#############################################################################
355 471
356package CFClient::Widget::Frame; 472package CFClient::UI::Frame;
357 473
358our @ISA = CFClient::Widget::Bin::; 474our @ISA = CFClient::UI::Bin::;
359 475
360use SDL::OpenGL; 476use SDL::OpenGL;
361 477
362sub size_request { 478sub size_request {
363 my ($self) = @_; 479 my ($self) = @_;
368 484
369 map { $_ + 4 } $chld->size_request; 485 map { $_ + 4 } $chld->size_request;
370} 486}
371 487
372sub size_allocate { 488sub size_allocate {
373 my ($self, $w, $h) = @_; 489 my ($self, $x, $y, $w, $h) = @_;
374 490
375 $self->{w} = $w; 491 $self->_size_allocate ($x, $y, $w, $h) or return;
376 $self->{h} = $h;
377 492
378 $self->child->size_allocate ($w - 4, $h - 4); 493 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
379 $self->child->move (2, 2);
380} 494}
381 495
382sub _draw { 496sub _draw {
383 my ($self) = @_; 497 my ($self) = @_;
384 498
397 $chld->draw; 511 $chld->draw;
398} 512}
399 513
400############################################################################# 514#############################################################################
401 515
402package CFClient::Widget::FancyFrame; 516package CFClient::UI::FancyFrame;
403 517
404our @ISA = CFClient::Widget::Bin::; 518our @ISA = CFClient::UI::Bin::;
405 519
406use SDL::OpenGL; 520use SDL::OpenGL;
407 521
408my @tex = 522my @tex =
409 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 523 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
410 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 524 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
411 525
412sub size_request { 526sub size_request {
413 my ($self) = @_; 527 my ($self) = @_;
414 528
529 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
530
415 my ($w, $h) = $self->SUPER::size_request; 531 my ($w, $h) = $self->SUPER::size_request;
416 532
417 $h += $tex[1]->{height}; 533 $h += $tex[1]->{h};
418 $h += $tex[4]->{height}; 534 $h += $tex[4]->{h};
419 $w += $tex[2]->{width}; 535 $w += $tex[2]->{w};
420 $w += $tex[3]->{width}; 536 $w += $tex[3]->{w};
421 537
422 ($w, $h) 538 ($w, $h)
423} 539}
424 540
425sub size_allocate { 541sub size_allocate {
426 my ($self, $w, $h) = @_; 542 my ($self, $x, $y, $w, $h) = @_;
427 543
428 $self->SUPER::size_allocate ($w, $h); 544 $self->_size_allocate ($x, $y, $w, $h) or return;
429 545
430 $h -= $tex[1]->{height}; 546 $h -= $tex[1]{h};
431 $h -= $tex[4]->{height}; 547 $h -= $tex[4]{h};
432 $w -= $tex[2]->{width}; 548 $w -= $tex[2]{w};
433 $w -= $tex[3]->{width}; 549 $w -= $tex[3]{w};
434 550
435 $h = $h < 0 ? 0 : $h; 551 $h = $h < 0 ? 0 : $h;
436 $w = $w < 0 ? 0 : $w; 552 $w = $w < 0 ? 0 : $w;
437 553
438 $self->child->size_allocate ($w, $h); 554 my $child = $self->child;
439 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 555
556 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
557}
558
559sub button_down {
560 my ($self, $ev, $x, $y) = @_;
561
562 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
563 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
564
565 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
566 my ($bw, $bh) = ($self->{w}, $self->{h});
567
568 $self->{motion} = sub {
569 my ($ev, $x, $y) = @_;
570
571 ($x, $y) = ($ev->motion_x, $ev->motion_y);
572
573 $self->{user_w} = $bw + $x - $ox;
574 $self->{user_h} = $bh + $y - $oy;
575 $self->update;
576 };
577
578 } elsif ($x >= 0 && $x < $self->{w}
579 && $y >= 0 && $y < $tex[1]{h}) {
580
581 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
582 my ($bx, $by) = ($self->{x}, $self->{y});
583
584 $self->{motion} = sub {
585 my ($ev, $x, $y) = @_;
586
587 ($x, $y) = ($ev->motion_x, $ev->motion_y);
588
589 $self->move ($bx + $x - $ox, $by + $y - $oy);
590 $self->update;
591 };
592 }
593}
594
595sub button_up {
596 my ($self, $ev, $x, $y) = @_;
597
598 delete $self->{motion};
599}
600
601sub mouse_motion {
602 my ($self, $ev, $x, $y) = @_;
603
604 $self->{motion}->($ev, $x, $y) if $self->{motion};
440} 605}
441 606
442sub _draw { 607sub _draw {
443 my ($self) = @_; 608 my ($self) = @_;
444 609
445 my ($w, $h) = ($self->{w}, $self->{h}); 610 my ($w, $h) = ($self->{w}, $self->{h});
446 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 611 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
447 612
448 glEnable GL_BLEND; 613 glEnable GL_BLEND;
614 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
449 glEnable GL_TEXTURE_2D; 615 glEnable GL_TEXTURE_2D;
450 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
451 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
452 617
453 my $top = $tex[1]; 618 my $top = $tex[1];
454 $top->draw_quad (0, 0, $w, $top->{height}); 619 $top->draw_quad (0, 0, $w, $top->{h});
455 620
456 my $left = $tex[3]; 621 my $left = $tex[3];
457 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 622 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
458 623
459 my $right = $tex[2]; 624 my $right = $tex[2];
460 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 625 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
461 626
462 my $bottom = $tex[4]; 627 my $bottom = $tex[4];
463 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 628 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
464 629
465 my $bg = $tex[0]; 630 my $bg = $tex[0];
631
466 glBindTexture GL_TEXTURE_2D, $bg->{name}; 632 glBindTexture GL_TEXTURE_2D, $bg->{name};
467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 633 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 634 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
469 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 635 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
470 636
471 my $rep_x = $cw / $bg->{width}; 637 my $rep_x = $cw / $bg->{w};
472 my $rep_y = $ch / $bg->{height}; 638 my $rep_y = $ch / $bg->{h};
473 639
474 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 640 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
475 641
642 glDisable GL_TEXTURE_2D;
476 glDisable GL_BLEND; 643 glDisable GL_BLEND;
477 glDisable GL_TEXTURE_2D;
478 644
479 $self->child->draw; 645 $self->child->draw;
480 646
481} 647}
482 648
483############################################################################# 649#############################################################################
484 650
485package CFClient::Widget::Table; 651package CFClient::UI::Table;
486 652
487our @ISA = CFClient::Widget::Bin::; 653our @ISA = CFClient::UI::Base::;
654
655use List::Util qw(max sum);
488 656
489use SDL::OpenGL; 657use SDL::OpenGL;
658
659sub new {
660 my $class = shift;
661
662 $class->SUPER::new (
663 col_expand => [],
664 @_
665 )
666}
490 667
491sub add { 668sub add {
492 my ($self, $x, $y, $chld) = @_; 669 my ($self, $x, $y, $chld) = @_;
493 my $old_chld = $self->{children}[$y][$x];
494 670
495 $self->{children}[$y][$x] = $chld; 671 $self->{children}[$y][$x] = $chld;
496 $chld->set_parent ($self); 672 $chld->set_parent ($self);
673
674 $self->{w} = $self->{h} = -1;
497 $self->update; 675 $self->update;
498} 676}
499 677
500sub max_row_height { 678sub get_wh {
501 my ($self, $row) = @_; 679 my ($self) = @_;
502 680
503 my $hs = 0; 681 my (@w, @h);
504 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 682
683 for my $y (0 .. $#{$self->{children}}) {
505 my $c = $self->{children}->[$row]->[$xi]; 684 my $row = $self->{children}[$y]
506 if ($c) { 685 or next;
686
687 for my $x (0 .. $#$row) {
688 my $widget = $row->[$x]
689 or next;
507 my ($w, $h) = $c->size_request; 690 my ($w, $h) = $widget->size_request;
508 if ($hs < $h) { $hs = $h } 691
692 $w[$x] = max $w[$x], $w;
693 $h[$y] = max $h[$y], $h;
509 } 694 }
510 } 695 }
511 return $hs;
512}
513 696
514sub max_col_width { 697 (\@w, \@h)
698}
699
700sub size_request {
515 my ($self, $col) = @_; 701 my ($self) = @_;
516 702
703 my ($ws, $hs) = $self->get_wh;
704
705 (
706 (sum @$ws),
707 (sum @$hs),
708 )
709}
710
711sub size_allocate {
712 my ($self, $x, $y, $w, $h) = @_;
713
714 $self->_size_allocate ($x, $y, $w, $h) or return;
715
716 my ($ws, $hs) = $self->get_wh;
717
718 my $req_w = sum @$ws;
719 my $req_h = sum @$hs;
720
721 # TODO: nicer code && do row_expand
722 my @col_expand = @{$self->{col_expand}};
723 @col_expand = (1) x @$ws unless @col_expand;
724 my $col_expand = (sum @col_expand) || 1;
725
726 # linearly scale sizes
727 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
728 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
729
730 my $y;
731
732 for my $r (0 .. $#{$self->{children}}) {
733 my $row = $self->{children}[$r]
734 or next;
735
517 my $ws = 0; 736 my $x = 0;
518 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 737 my $row_h = $hs->[$r];
519 my $c = ($self->{children}->[$yi] || [])->[$col]; 738
520 if ($c) { 739 for my $c (0 .. $#$row) {
521 my ($w, $h) = $c->size_request; 740 my $widget = $row->[$c]
522 if ($ws < $w) { $ws = $w } 741 or next;
742
743 my $col_w = $ws->[$c];
744
745 $widget->size_allocate ($x, $y, $col_w, $row_h);
746
747 $x += $col_w;
523 } 748 }
749
750 $y += $row_h;
524 } 751 }
525 return $ws; 752
526} 753}
754
755sub find_widget {
756 my ($self, $x, $y) = @_;
757
758 $x -= $self->{x};
759 $y -= $self->{y};
760
761 my $res;
762
763 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
764 $res = $_->find_widget ($x, $y)
765 and return $res;
766 }
767
768 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
769}
770
771sub _draw {
772 my ($self) = @_;
773
774 for (grep $_, @{$self->{children}}) {
775 $_->draw for grep $_, @$_;
776 }
777}
778
779#############################################################################
780
781package CFClient::UI::HBox;
782
783# TODO: wrap into common Box base class
784
785our @ISA = CFClient::UI::Container::;
527 786
528sub size_request { 787sub size_request {
529 my ($self) = @_; 788 my ($self) = @_;
530 789
790 my @alloc = map [$_->size_request], @{$self->{children}};
791
792 (
793 (List::Util::sum map $_->[0], @alloc),
794 (List::Util::max map $_->[1], @alloc),
795 )
796}
797
798sub size_allocate {
799 my ($self, $x, $y, $w, $h) = @_;
800
801 $self->_size_allocate ($x, $y, $w, $h) or return;
802
803 return unless $self->{w};
804
531 my ($hs, $ws) = (0, 0); 805 ($h, $w) = ($w, $h);
532 806
533 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 807 my $children = $self->{children};
534 $hs += $self->max_row_height ($yi);
535 }
536 808
537 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 809 my @h = map +($_->size_request)[0], @$children;
538 my $wm = 0; 810
539 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 811 my $req_h = List::Util::sum @h;
540 $wm += $self->max_col_width ($xi) 812
813 if ($req_h > $h) {
814 # ah well, not enough space
815 $_ *= $h / $req_h for @h;
816 } else {
817 my $exp = List::Util::sum map $_->{expand}, @$children;
818 $exp ||= 1;
819
820 for (0 .. $#$children) {
821 my $child = $children->[$_];
822
823 my $alloc_h = $h[$_];
824 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
825 $h[$_] = $alloc_h;
541 } 826 }
542 if ($ws < $wm) { $ws = $wm }
543 } 827 }
544
545 return ($ws, $hs);
546}
547
548sub _draw {
549 my ($self) = @_;
550 828
551 my $y = 0; 829 my $y = 0;
552 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 830 for (0 .. $#$children) {
831 my $child = $children->[$_];
553 my $x = 0; 832 my $h = $h[$_];
833 $child->size_allocate ($y, 0, $h, $w);
554 834
555 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 835 $y += $h;
556
557 my $c = $self->{children}->[$yi]->[$xi];
558 if ($c) {
559 $c->move ($x, $y, 0); #TODO: Move to size_request
560 $c->draw if $c;
561 }
562
563 $x += $self->max_col_width ($xi);
564 }
565
566 $y += $self->max_row_height ($yi);
567 } 836 }
568} 837}
569 838
570############################################################################# 839#############################################################################
571 840
572package CFClient::Widget::VBox; 841package CFClient::UI::VBox;
573 842
843# TODO: wrap into common Box base class
844
574our @ISA = CFClient::Widget::Container::; 845our @ISA = CFClient::UI::Container::;
575
576use SDL::OpenGL;
577 846
578sub size_request { 847sub size_request {
579 my ($self) = @_; 848 my ($self) = @_;
580 849
581 my @alloc = map [$_->size_request], @{$self->{children}}; 850 my @alloc = map [$_->size_request], @{$self->{children}};
585 (List::Util::sum map $_->[1], @alloc), 854 (List::Util::sum map $_->[1], @alloc),
586 ) 855 )
587} 856}
588 857
589sub size_allocate { 858sub size_allocate {
590 my ($self, $w, $h) = @_; 859 my ($self, $x, $y, $w, $h) = @_;
591 860
592 $self->w ($w); 861 $self->_size_allocate ($x, $y, $w, $h) or return;
593 $self->h ($h);
594 862
595 my $exp; 863 return unless $self->{h};
596 my @oth; 864
597 # find expand widget 865 my $children = $self->{children};
598 for (@{$self->{children}}) { 866
599 if ($_->{expand}) { 867 my @h = map +($_->size_request)[1], @$children;
868
869 my $req_h = List::Util::sum @h;
870
871 if ($req_h > $h) {
872 # ah well, not enough space
873 $_ *= $h / $req_h for @h;
874 } else {
875 my $exp = List::Util::sum map $_->{expand}, @$children;
600 $exp = $_; 876 $exp ||= 1;
601 last; 877
878 for (0 .. $#$children) {
879 my $child = $children->[$_];
880
881 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
602 } 882 }
603 push @oth, $_;
604 }
605
606 my ($ow, $oh);
607
608 # get sizes of other widgets
609 for (@oth) {
610 my ($w, $h) = $_->size_request;
611 $oh += $h;
612 if ($ow < $w) { $ow = $w }
613 } 883 }
614 884
615 my $y = 0; 885 my $y = 0;
616 for (@{$self->{children}}) { 886 for (0 .. $#$children) {
617 $_->move (0, $y); 887 my $child = $children->[$_];
618 888 my $h = $h[$_];
619 if ($_ == $exp) {
620 $_->size_allocate ($w, $h - $oh);
621 $y += $h - $oh;
622 } else {
623 my ($cw, $h) = $_->size_request;
624 $_->size_allocate ($w, $h); 889 $child->size_allocate (0, $y, $w, $h);
890
625 $y += $h; 891 $y += $h;
626 }
627 } 892 }
628} 893}
629 894
630############################################################################# 895#############################################################################
631 896
632package CFClient::Widget::Label; 897package CFClient::UI::Label;
633 898
634our @ISA = CFClient::Widget::; 899our @ISA = CFClient::UI::Base::;
635 900
636use SDL::OpenGL; 901use SDL::OpenGL;
637 902
638sub new { 903sub new {
639 my ($class, %arg) = @_; 904 my ($class, %arg) = @_;
640 905
641 # TODO: color, and make height, xyz etc. optional
642 my $self = $class->SUPER::new ( 906 my $self = $class->SUPER::new (
643 color => [1, 1, 1], 907 fg => [1, 1, 1],
644 height => $::FONTSIZE, 908 height => $::FONTSIZE,
645 text => "", 909 text => "",
910 align => -1,
911 padding => 2,
646 layout => new CFClient::Layout, 912 layout => new CFClient::Layout,
647 %arg 913 %arg
648 ); 914 );
649 915
650 $self->set_text ($self->{text}); 916 $self->set_text ($self->{text});
651 917
652 $self 918 $self
653} 919}
654 920
921sub escape_text {
922 local $_ = $_[1];
923
924 s/&/&amp;/g;
925 s/>/&gt;/g;
926 s/</&lt;/g;
927
928 $_[1]
929}
930
655sub set_text { 931sub set_text {
656 my ($self, $text) = @_; 932 my ($self, $text) = @_;
657 933
658 $self->{text} = $text; 934 $self->{text} = $text;
659 $self->{layout}->set_markup ($text); 935 $self->{layout}->set_markup ($text);
660 936
661 delete $self->{texture}; 937 delete $self->{texture};
938 $self->update;
662} 939}
663 940
664sub get_text { 941sub get_text {
665 my ($self, $text) = @_; 942 my ($self, $text) = @_;
666 943
670sub size_request { 947sub size_request {
671 my ($self) = @_; 948 my ($self) = @_;
672 949
673 $self->{layout}->set_width; 950 $self->{layout}->set_width;
674 $self->{layout}->set_height ($self->{height}); 951 $self->{layout}->set_height ($self->{height});
675 $self->{layout}->size 952 my ($w, $h) = $self->{layout}->size;
676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 953
677# ( 954 (
678# $self->{texture}{width}, 955 $w + $self->{padding} * 2,
679# $self->{texture}{height}, 956 $h + $self->{padding} * 2,
680# ) 957 )
681# } else {
682# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
683#
684# ($w, $h)
685# }
686} 958}
687 959
688sub size_allocate { 960sub size_allocate {
689 my ($self, $w, $h) = @_; 961 my ($self, $x, $y, $w, $h) = @_;
690 962
691 $self->SUPER::size_allocate ($w, $h); 963 $self->_size_allocate ($x, $y, $w, $h) or return;
964
692 delete $self->{texture}; 965 delete $self->{texture};
966}
967
968sub update {
969 my ($self) = @_;
970
971 delete $self->{texture};
972 $self->SUPER::update;
693} 973}
694 974
695sub _draw { 975sub _draw {
696 my ($self) = @_; 976 my ($self) = @_;
697 977
698 my $tex = $self->{texture} ||= do { 978 my $tex = $self->{texture} ||= do {
699 $self->{layout}->set_width ($self->{w}); 979 $self->{layout}->set_width ($self->{w});
980 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{height});
700 new_from_layout CFClient::Texture $self->{layout}; 981 new_from_layout CFClient::Texture $self->{layout}
701 }; 982 };
702 983
703 glEnable GL_BLEND; 984 glEnable GL_BLEND;
985 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
704 glEnable GL_TEXTURE_2D; 986 glEnable GL_TEXTURE_2D;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 987 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 988
708 glColor @{$self->{color}}; 989 glColor @{$self->{fg}};
709 990
710 $tex->draw_quad (0, 0); 991 my $x =
992 $self->{align} < 0 ? $self->{padding}
993 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
994 : ($self->{w} - $tex->{w}) * 0.5;
711 995
996 $tex->draw_quad ($x, $self->{padding});
997
998 glDisable GL_TEXTURE_2D;
712 glDisable GL_BLEND; 999 glDisable GL_BLEND;
713 glDisable GL_TEXTURE_2D;
714} 1000}
715 1001
716############################################################################# 1002#############################################################################
717 1003
718package CFClient::Widget::Entry; 1004package CFClient::UI::Entry;
719 1005
720our @ISA = CFClient::Widget::Label::; 1006our @ISA = CFClient::UI::Label::;
721 1007
722use SDL; 1008use SDL;
723use SDL::OpenGL; 1009use SDL::OpenGL;
724 1010
1011sub new {
1012 my $class = shift;
1013
1014 $class->SUPER::new (
1015 fg => [1, 1, 1],
1016 bg => [0, 0, 0, 0.2],
1017 active_bg => [1, 1, 1],
1018 active_fg => [0, 0, 0],
1019 @_
1020 )
1021}
1022
1023sub _set_text {
1024 my ($self, $text) = @_;
1025
1026 $self->{last_activity} = $::NOW;
1027
1028 $self->{text} = $text;
1029 $self->{layout}->set_width ($self->{w});
1030
1031 $text =~ s/./*/g if $self->{hidden};
1032
1033 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
1034
1035 $text = substr $text, 0, $self->{cursor};
1036 utf8::encode $text;
1037
1038 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1039}
1040
1041sub size_request {
1042 my ($self) = @_;
1043
1044 my ($w, $h) = $self->SUPER::size_request;
1045
1046 ($w + 1, $h) # add 1 for cursor
1047}
1048
1049sub size_allocate {
1050 my ($self, $x, $y, $w, $h) = @_;
1051
1052 $self->SUPER::size_allocate ($x, $y, $w, $h);
1053
1054 $self->_set_text ($self->{text});
1055}
1056
1057sub set_text {
1058 my ($self, $text) = @_;
1059
1060 $self->{cursor} = length $text;
1061 $self->_set_text ($text);
1062 $self->update;
1063}
1064
725sub key_down { 1065sub key_down {
726 my ($self, $ev) = @_; 1066 my ($self, $ev) = @_;
727 1067
728 my $mod = $ev->key_mod; 1068 my $mod = $ev->key_mod;
729 my $sym = $ev->key_sym; 1069 my $sym = $ev->key_sym;
731 my $uni = $ev->key_unicode; 1071 my $uni = $ev->key_unicode;
732 1072
733 my $text = $self->get_text; 1073 my $text = $self->get_text;
734 1074
735 if ($sym == SDLK_BACKSPACE) { 1075 if ($sym == SDLK_BACKSPACE) {
736 substr $text, -1, 1, ''; 1076 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1077 } elsif ($sym == SDLK_DELETE) {
1078 substr $text, $self->{cursor}, 1, "";
1079 } elsif ($sym == SDLK_LEFT) {
1080 --$self->{cursor} if $self->{cursor};
1081 } elsif ($sym == SDLK_RIGHT) {
1082 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1083 } elsif ($sym == SDLK_HOME) {
1084 $self->{cursor} = 0;
1085 } elsif ($sym == SDLK_END) {
1086 $self->{cursor} = length $text;
737 } elsif ($uni) { 1087 } elsif ($uni) {
738 $text .= chr $uni; 1088 substr $text, $self->{cursor}++, 0, chr $uni;
739 } 1089 }
740 1090
741 $self->set_text ($text); 1091 $self->_set_text ($text);
1092 $self->update;
1093}
1094
1095sub focus_in {
1096 my ($self) = @_;
1097
1098 $self->{last_activity} = $::NOW;
1099
1100 $self->SUPER::focus_in;
742} 1101}
743 1102
744sub button_down { 1103sub button_down {
745 my ($self, $ev) = @_; 1104 my ($self, $ev, $x, $y) = @_;
746 1105
747 $self->focus_in; 1106 $self->SUPER::button_down ($ev, $x, $y);
1107
1108 my $idx = $self->{layout}->xy_to_index ($x, $y);
1109
1110 # byte-index to char-index
1111 my $text = $self->{text};
1112 utf8::encode $text;
1113 $self->{cursor} = length substr $text, 0, $idx;
1114
1115 $self->_set_text ($self->{text});
1116 $self->update;
748} 1117}
749 1118
750sub mouse_motion { 1119sub mouse_motion {
751 my ($self, $ev, $x, $y) = @_; 1120 my ($self, $ev, $x, $y) = @_;
752 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1121# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
753} 1122}
754 1123
755sub _draw { 1124sub _draw {
756 my ($self) = @_; 1125 my ($self) = @_;
757 1126
1127 local $self->{fg} = $self->{fg};
1128
758 if ($FOCUS == $self) { 1129 if ($FOCUS == $self) {
759 glColor 1, 1, 1; 1130 glColor @{$self->{active_bg}};
1131 $self->{fg} = $self->{active_fg};
760 } else { 1132 } else {
761 glColor 0.7, 0.7, 0.7; 1133 glColor @{$self->{bg}};
762 } 1134 }
763 1135
1136 glEnable GL_BLEND;
1137 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
764 glBegin GL_QUADS; 1138 glBegin GL_QUADS;
765 glVertex 0 , 0; 1139 glVertex 0 , 0;
766 glVertex 0 , $self->{h} - 1; 1140 glVertex 0 , $self->{h};
767 glVertex $self->{w} - 1, $self->{h} - 1; 1141 glVertex $self->{w}, $self->{h};
768 glVertex $self->{w} - 1, 0; 1142 glVertex $self->{w}, 0;
769 glEnd; 1143 glEnd;
1144 glDisable GL_BLEND;
770 1145
771 $self->SUPER::_draw; 1146 $self->SUPER::_draw;
772}
773 1147
774############################################################################# 1148 #TODO: force update every cursor change :(
1149 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1150 glColor @{$self->{fg}};
1151 glBegin GL_LINES;
1152 glVertex $self->{cur_x}, $self->{cur_y};
1153 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1154 glEnd;
1155 }
1156}
775 1157
1158#############################################################################
1159
1160package CFClient::UI::Slider;
1161
1162use strict;
1163
1164use SDL::OpenGL;
1165
1166our @ISA = CFClient::UI::DrawBG::;
1167
1168sub new {
1169 my $class = shift;
1170
1171 # range [value, low, high, page]
1172
1173 my $self = $class->SUPER::new (
1174 fg => [1, 1, 1],
1175 active_fg => [0, 0, 0],
1176 range => [0, 0, 100, 10],
1177 req_w => 40,
1178 req_h => 10,
1179 vertical => 0,
1180 @_
1181 );
1182
1183 $self
1184}
1185
1186sub size_request {
1187 my ($self) = @_;
1188
1189 my $w = $self->{req_w};
1190 my $h = $self->{req_h};
1191
1192 $self->{vertical} ? ($h, $w) : ($w, $h)
1193}
1194
1195sub button_down {
1196 my ($self, $ev, $x, $y) = @_;
1197
1198 $self->SUPER::button_down ($ev, $x, $y);
1199 $self->mouse_motion ($ev, $x, $y);
1200}
1201
1202sub mouse_motion {
1203 my ($self, $ev, $x, $y) = @_;
1204
1205 if ($GRAB == $self) {
1206 my ($value, $lo, $hi, $page) = @{$self->{range}};
1207
1208 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1209
1210 $x = $x * ($hi - $lo) / $w + $lo;
1211 $x = $lo if $x < $lo;
1212 $x = $hi - $page if $x > $hi - $page;
1213 $self->{range}[0] = $x;
1214
1215 $self->emit (changed => $x);
1216 $self->update;
1217 }
1218}
1219
1220sub _draw {
1221 my ($self) = @_;
1222
1223 $self->SUPER::_draw ();
1224
1225 my ($w, $h) = @$self{qw(w h)};
1226
1227 if ($self->{vertical}) {
1228 # draw a vertical slider like a rotated horizontal slider
1229
1230 glRotate 90, 0, 0, 1;
1231 glTranslate 0, -$self->{w}, 0;
1232
1233 ($w, $h) = ($h, $w);
1234 }
1235
1236 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1237 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1238
1239 my ($value, $lo, $hi, $page) = @{$self->{range}};
1240
1241 $page = int $page * $w / ($hi - $lo);
1242 $value = int +($value - $lo) * $w / ($hi - $lo);
1243
1244 $w -= $page;
1245 $page &= ~1;
1246 glTranslate $page * 0.5, 0, 0;
1247
1248 glColor @$fg;
1249 glBegin GL_LINES;
1250 glVertex 0, 0; glVertex 0, $h;
1251 glVertex $w - 1, 0; glVertex $w - 1, $h;
1252 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1253 glEnd;
1254
1255 my $knob_a = $value - $page * 0.5;
1256 my $knob_b = $value + $page * 0.5;
1257
1258 glBegin GL_QUADS;
1259 glColor @$fg;
1260 glVertex $knob_a, 0;
1261 glVertex $knob_a, $h;
1262 glVertex $knob_b, $h;
1263 glVertex $knob_b, 0;
1264
1265 if ($knob_a < $knob_b - 2) {
1266 glColor @$bg;
1267 glVertex $knob_a + 1, 1;
1268 glVertex $knob_a + 1, $h - 1;
1269 glVertex $knob_b - 1, $h - 1;
1270 glVertex $knob_b - 1, 1;
1271 }
1272 glEnd;
1273}
1274
1275#############################################################################
1276
776package CFClient::Widget::MapWidget; 1277package CFClient::UI::MapWidget;
777 1278
778use strict; 1279use strict;
779 1280
780use List::Util qw(min max); 1281use List::Util qw(min max);
781 1282
782use SDL; 1283use SDL;
783use SDL::OpenGL; 1284use SDL::OpenGL;
784use SDL::OpenGL::Constants;
785 1285
786our @ISA = CFClient::Widget::; 1286our @ISA = CFClient::UI::Base::;
787 1287
788sub new { 1288sub new {
789 my $class = shift; 1289 my $class = shift;
790 1290
791 $class->SUPER::new (z => -1, @_) 1291 $class->SUPER::new (
1292 z => -1,
1293 list => (glGenLists 1),
1294 @_
1295 )
792} 1296}
793 1297
794sub key_down { 1298sub key_down {
795 print "MAPKEYDOWN\n"; 1299 print "MAPKEYDOWN\n";
796} 1300}
798sub key_up { 1302sub key_up {
799} 1303}
800 1304
801sub size_request { 1305sub size_request {
802 ( 1306 (
803 1 + int $::WIDTH / 32, 1307 1 + 32 * int $::WIDTH / 32,
804 1 + int $::HEIGHT / 32, 1308 1 + 32 * int $::HEIGHT / 32,
805 ) 1309 )
806} 1310}
807 1311
1312sub update {
1313 my ($self) = @_;
1314
1315 $self->{need_update} = 1;
1316 $self->SUPER::update;
1317}
1318
808sub _draw { 1319sub draw {
809 my ($self) = @_; 1320 my ($self) = @_;
810 1321
1322 if (delete $self->{need_update}) {
1323 glNewList $self->{list}, GL_COMPILE;
1324
811 my $mx = $::CONN->{mapx}; 1325 my $mx = $::CONN->{mapx};
812 my $my = $::CONN->{mapy}; 1326 my $my = $::CONN->{mapy};
813 1327
814 my $map = $::CONN->{map}; 1328 my $map = $::CONN->{map};
815 1329
816 my ($xofs, $yofs); 1330 my ($xofs, $yofs);
817 1331
818 my $sw = 1 + int $::WIDTH / 32; 1332 my $sw = 1 + int $::WIDTH / 32;
819 my $sh = 1 + int $::HEIGHT / 32; 1333 my $sh = 1 + int $::HEIGHT / 32;
820 1334
821 if ($::CONN->{mapw} > $sw) { 1335 if ($::CONN->{mapw} > $sw) {
822 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1336 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
823 } else { 1337 } else {
824 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1338 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
825 } 1339 }
826 1340
827 if ($::CONN->{maph} > $sh) { 1341 if ($::CONN->{maph} > $sh) {
828 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1342 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
829 } else { 1343 } else {
830 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1344 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
831 } 1345 }
832 1346
833 glEnable GL_TEXTURE_2D; 1347 glEnable GL_TEXTURE_2D;
834 glEnable GL_BLEND; 1348 glEnable GL_BLEND;
1349 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
835 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
836 1351
837 my $sw4 = ($sw + 3) & ~3; 1352 my $sw4 = ($sw + 3) & ~3;
838 my $darkness = "\x00" x ($sw4 * $sh); 1353 my $darkness = "\x00" x ($sw4 * $sh);
839 1354
840 for my $x (0 .. $sw - 1) { 1355 for my $x (0 .. $sw - 1) {
841 my $row = $map->[$x + $xofs]; 1356 my $row = $map->[$x + $xofs];
842 for my $y (0 .. $sh - 1) { 1357 for my $y (0 .. $sh - 1) {
843 1358
844 my $cell = $row->[$y + $yofs] 1359 my $cell = $row->[$y + $yofs]
845 or next; 1360 or next;
846 1361
847 my $dark = $cell->[0]; 1362 my $dark = $cell->[0];
848 if ($dark < 0) { 1363 if ($dark < 0) {
849 substr $darkness, $y * $sw4 + $x, 1, chr 224; 1364 substr $darkness, $y * $sw4 + $x, 1, chr 224;
850 } else { 1365 } else {
851 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; 1366 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
852 } 1367 }
853 1368
854 for my $num (grep $_, @$cell[1,2,3]) { 1369 for my $num (grep $_, @$cell[1,2,3]) {
855 my $tex = $::CONN->{face}[$num]{texture} || next; 1370 my $tex = $::CONN->{face}[$num]{texture} || next;
856 1371
857 my $w = $tex->{width}; 1372 my ($w, $h) = @$tex{qw(w h)};
858 my $h = $tex->{height};
859 1373
860 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); 1374 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1375 }
861 } 1376 }
862 } 1377 }
863 }
864 1378
865# if (1) { # higher quality darkness 1379# if (1) { # higher quality darkness
866# $lighting =~ s/(.)/$1$1$1/gs; 1380# $lighting =~ s/(.)/$1$1$1/gs;
867# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 1381# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
868# 1382#
870# 1384#
871# $lighting = $pb->get_pixels; 1385# $lighting = $pb->get_pixels;
872# $lighting =~ s/(.)../$1/gs; 1386# $lighting =~ s/(.)../$1/gs;
873# } 1387# }
874 1388
875 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
876 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1389 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
877 1390
878 $darkness = new CFClient::Texture 1391 $darkness = new CFClient::Texture
879 width => $sw4, 1392 w => $sw4,
880 height => $sh, 1393 h => $sh,
881 data => $darkness, 1394 data => $darkness,
882 internalformat => GL_ALPHA, 1395 internalformat => GL_ALPHA,
883 format => GL_ALPHA; 1396 format => GL_ALPHA;
884 1397
885 glColor 0.45, 0.45, 0.45, 1; 1398 glColor 0.45, 0.45, 0.45, 1;
886 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 1399 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
887 1400
888 glDisable GL_TEXTURE_2D; 1401 glDisable GL_TEXTURE_2D;
889 glDisable GL_BLEND; 1402 glDisable GL_BLEND;
1403
1404 glEndList;
1405 }
1406
1407 glCallList $self->{list};
890} 1408}
891 1409
892my %DIR = ( 1410my %DIR = (
893 SDLK_KP8, [1, "north"], 1411 SDLK_KP8, [1, "north"],
894 SDLK_KP9, [2, "northeast"], 1412 SDLK_KP9, [2, "northeast"],
940 } 1458 }
941} 1459}
942 1460
943############################################################################# 1461#############################################################################
944 1462
945package CFClient::Widget::Animator; 1463package CFClient::UI::Animator;
946 1464
947use SDL::OpenGL; 1465use SDL::OpenGL;
948 1466
949our @ISA = CFClient::Widget::Bin::; 1467our @ISA = CFClient::UI::Bin::;
950 1468
951sub moveto { 1469sub moveto {
952 my ($self, $x, $y) = @_; 1470 my ($self, $x, $y) = @_;
953 1471
954 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1472 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
982 glPopMatrix; 1500 glPopMatrix;
983} 1501}
984 1502
985############################################################################# 1503#############################################################################
986 1504
987package CFClient::Widget::Toplevel; 1505package CFClient::UI::Toplevel;
988 1506
989our @ISA = CFClient::Widget::Container::; 1507our @ISA = CFClient::UI::Container::;
990 1508
991sub size_request { 1509sub size_request {
992 ($::WIDTH, $::HEIGHT) 1510 ($::WIDTH, $::HEIGHT)
993} 1511}
994 1512
995sub size_allocate { 1513sub size_allocate {
996 my ($self, $w, $h) = @_; 1514 my ($self, $x, $y, $w, $h) = @_;
997 1515
998 $self->SUPER::size_allocate ($w, $h); 1516 $self->_size_allocate ($x, $y, $w, $h);
999 1517
1000 $_->size_allocate ($_->size_request) 1518 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1001 for @{$self->{children}}; 1519 for @{$self->{children}};
1002} 1520}
1003 1521
1004sub translate { 1522sub translate {
1005 my ($self, $x, $y) = @_; 1523 my ($self, $x, $y) = @_;
1008} 1526}
1009 1527
1010sub update { 1528sub update {
1011 my ($self) = @_; 1529 my ($self) = @_;
1012 1530
1013 $self->size_allocate ($self->size_request); 1531 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1014 ::refresh (); 1532 ::refresh ();
1015} 1533}
1016 1534
1017sub add { 1535sub add {
1018 my ($self, $widget) = @_; 1536 my ($self, $widget) = @_;
1019 1537
1020 $self->SUPER::add ($widget); 1538 $self->SUPER::add ($widget);
1021 1539
1022 $widget->size_allocate ($widget->size_request); 1540 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1023} 1541}
1024 1542
1025sub draw { 1543sub draw {
1026 my ($self) = @_; 1544 my ($self) = @_;
1027 1545
1028 $self->_draw; 1546 $self->_draw;
1029} 1547}
1030 1548
1031############################################################################# 1549#############################################################################
1032 1550
1033package CFClient::Widget; 1551package CFClient::UI;
1034 1552
1035$TOPLEVEL = new CFClient::Widget::Toplevel; 1553$TOPLEVEL = new CFClient::UI::Toplevel;
1036 1554
10371 15551
1038 1556

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines