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.143 by root, Thu Apr 20 21:28:51 2006 UTC vs.
Revision 1.150 by elmex, Sun Apr 23 00:08:29 2006 UTC

96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 can_events => 1,
101 @_ 102 @_
102 }, $class; 103 }, $class;
103 104
104 for (keys %$self) { 105 for (keys %$self) {
105 if (/^connect_(.*)$/) { 106 if (/^connect_(.*)$/) {
140 0 141 0
141} 142}
142 143
143sub size_request { 144sub size_request {
144 require Carp; 145 require Carp;
145 Carp::confess "size_request is abtract"; 146 Carp::confess "size_request is abstract";
146} 147}
147 148
148sub configure { 149sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 150 my ($self, $x, $y, $w, $h) = @_;
150 151
152 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 153 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 154 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154 155
155 # use alignment to adjust x, y 156 # use alignment to adjust x, y
156 157
157 $x += ($w - $w2) * 0.5; 158 $x += int +($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5; 159 $y += int +($h - $h2) * 0.5;
159 160
160 ($w, $h) = ($w2, $h2); 161 ($w, $h) = ($w2, $h2);
161 } 162 }
162 163
163 if ($self->{x} != $x || $self->{y} != $y) { 164 if ($self->{x} != $x || $self->{y} != $y) {
278} 279}
279 280
280sub find_widget { 281sub find_widget {
281 my ($self, $x, $y) = @_; 282 my ($self, $x, $y) = @_;
282 283
284 return () unless $self->{can_events};
285
283 return $self 286 return $self
284 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 287 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
285 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 288 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
286 289
287 () 290 ()
294} 297}
295 298
296sub check_size { 299sub check_size {
297 my ($self) = @_; 300 my ($self) = @_;
298 301
302 return unless $self->{parent};
303
299 my ($w, $h) = $self->size_request; 304 my ($w, $h) = $self->size_request;
300 305
301 if ($w != $self->{req_w} || $h != $self->{req_h}) { 306 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w; 307 $self->{req_w} = $w;
303 $self->{req_h} = $h; 308 $self->{req_h} = $h;
304 309
305 $self->{parent}->check_size 310 $self->{parent}->check_size;
306 if $self->{parent};
307 } 311 }
308} 312}
309 313
310sub update { 314sub update {
311 my ($self) = @_; 315 my ($self) = @_;
378 382
379package CFClient::UI::Empty; 383package CFClient::UI::Empty;
380 384
381our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
382 386
387sub new {
388 my ($class, %arg) = @_;
389 $class->SUPER::new (can_events => 0, %arg);
390}
391
383sub size_request { 392sub size_request {
384 (0, 0) 393 (0, 0)
385} 394}
386 395
387sub draw { } 396sub draw { }
395sub new { 404sub new {
396 my ($class, %arg) = @_; 405 my ($class, %arg) = @_;
397 406
398 my $children = delete $arg{children} || []; 407 my $children = delete $arg{children} || [];
399 408
400 my $self = $class->SUPER::new (children => [], %arg); 409 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
401 $self->add ($_) for @$children; 410 $self->add ($_) for @$children;
402 411
403 $self 412 $self
404} 413}
405 414
561sub new { die } 570sub new { die }
562 571
563sub size_request { 572sub size_request {
564 my ($self) = @_; 573 my ($self) = @_;
565 574
566 @$self{qw(child_w child_h)} = $self->child->size_request; 575 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 576 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568 577
569 @$self{qw(child_w child_h)} 578 @$self{qw(child_w child_h)}
570} 579}
571 580
581package CFClient::UI::Frame; 590package CFClient::UI::Frame;
582 591
583our @ISA = CFClient::UI::Bin::; 592our @ISA = CFClient::UI::Bin::;
584 593
585use CFClient::OpenGL; 594use CFClient::OpenGL;
586
587sub size_request {
588 my ($self) = @_;
589 my $chld = $self->child
590 or return (0, 0);
591
592 $chld->move (2, 2);
593
594 map { $_ + 4 } $chld->size_request;
595}
596
597sub size_allocate {
598 my ($self, $x, $y, $w, $h) = @_;
599
600 $self->child->configure (2, 2, $w - 4, $h - 4);
601}
602
603sub _draw {
604 my ($self) = @_;
605
606 my $chld = $self->child;
607
608 my ($w, $h) = $chld->size_request;
609
610 glBegin GL_QUADS;
611 glColor 0, 0, 0;
612 glVertex 0 , 0;
613 glVertex 0 , $h + 4;
614 glVertex $w + 4 , $h + 4;
615 glVertex $w + 4 , 0;
616 glEnd;
617
618 $chld->draw;
619}
620
621#############################################################################
622
623package CFClient::UI::FancyFrame;
624
625our @ISA = CFClient::UI::Bin::;
626
627use CFClient::OpenGL;
628
629my @tex =
630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
632 595
633sub new { 596sub new {
634 my $class = shift; 597 my $class = shift;
635
636 # TODO: user_x, user_y, overwrite moveto?
637 598
638 my $self = $class->SUPER::new ( 599 my $self = $class->SUPER::new (
639 bg => [1, 1, 1, 1], 600 bg => [1, 1, 1, 1],
640 border_bg => [1, 1, 1, 1], 601 border_bg => [1, 1, 1, 1],
641 border => 0.8, 602 border => 0.8,
642 @_ 603 @_
643 ); 604 );
644 605
606 $self
607}
608
609sub set_size {
610 my ($self, $w, $h) = @_;
611 $self->{req_w} = $w;
612 $self->{req_h} = $h;
613 $self->check_size;
614}
615
616sub size_request {
617 my ($self) = @_;
618 ($self->{req_w}, $self->{req_h})
619}
620
621sub size_allocate {
622 my ($self, $w, $h) = @_;
623 $self->{w} = $w;
624 $self->{h} = $h;
625 $self->child->configure (0, 0, $w, $h);
626}
627
628sub _draw {
629 my ($self) = @_;
630
631 my ($w, $h) = ($self->{w}, $self->{h});
632
633 glEnable GL_BLEND;
634 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
635 glEnable GL_TEXTURE_2D;
636 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
637
638# glBegin GL_QUADS;
639# glColor 0, 0, 0, 0;
640# glVertex 0 , 0;
641# glVertex 0 , $h;
642# glVertex $w, $h;
643# glVertex $w, 0;
644# glEnd;
645
646
647 $self->child->draw;
648 glDisable GL_BLEND;
649 glDisable GL_TEXTURE_2D;
650}
651
652#############################################################################
653
654package CFClient::UI::FancyFrame;
655
656our @ISA = CFClient::UI::Bin::;
657
658use CFClient::OpenGL;
659
660my @tex =
661 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
662 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
663
664sub new {
665 my $class = shift;
666
667 # TODO: user_x, user_y, overwrite moveto?
668
669 my $self = $class->SUPER::new (
670 bg => [1, 1, 1, 1],
671 border_bg => [1, 1, 1, 1],
672 border => 0.8,
673 can_events => 1,
674 @_
675 );
676
645 $self->{title} &&= new CFClient::UI::Label 677 $self->{title} &&= new CFClient::UI::Label
646 align => 0, 678 align => 0,
647 valign => 1, 679 valign => 1,
648 text => $self->{title}, 680 text => $self->{title},
649 fontsize => 1; 681 fontsize => 1;
815 or next; 847 or next;
816 848
817 for my $x (0 .. $#$row) { 849 for my $x (0 .. $#$row) {
818 my $widget = $row->[$x] 850 my $widget = $row->[$x]
819 or next; 851 or next;
820 my ($w, $h) = $widget->size_request; 852 my ($w, $h) = @$widget{qw(req_w req_h)};
821 853
822 $w[$x] = max $w[$x], $w; 854 $w[$x] = max $w[$x], $w;
823 $h[$y] = max $h[$y], $h; 855 $h[$y] = max $h[$y], $h;
824 } 856 }
825 } 857 }
930 962
931 ($h, $w) = ($w, $h); 963 ($h, $w) = ($w, $h);
932 964
933 my $children = $self->{children}; 965 my $children = $self->{children};
934 966
935 my @h = map +($_->size_request)[0], @$children; 967 my @h = map $_->{req_w}, @$children;
936 968
937 my $req_h = List::Util::sum @h; 969 my $req_h = List::Util::sum @h;
938 970
939 if ($req_h > $h) { 971 if ($req_h > $h) {
940 # ah well, not enough space 972 # ah well, not enough space
988sub size_allocate { 1020sub size_allocate {
989 my ($self, $w, $h) = @_; 1021 my ($self, $w, $h) = @_;
990 1022
991 my $children = $self->{children}; 1023 my $children = $self->{children};
992 1024
993 my @h = map +($_->size_request)[1], @$children; 1025 my @h = map $_->{req_h}, @$children;
994 1026
995 my $req_h = List::Util::sum @h; 1027 my $req_h = List::Util::sum @h;
996 1028
997 if ($req_h > $h) { 1029 if ($req_h > $h) {
998 # ah well, not enough space 1030 # ah well, not enough space
1039 text => "", 1071 text => "",
1040 align => -1, 1072 align => -1,
1041 valign => -1, 1073 valign => -1,
1042 padding => 2, 1074 padding => 2,
1043 layout => new CFClient::Layout, 1075 layout => new CFClient::Layout,
1076 can_events => 0,
1044 %arg 1077 %arg
1045 ); 1078 );
1046 1079
1047 if (exists $self->{template}) { 1080 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout; 1081 my $layout = new CFClient::Layout;
1070 my ($self, $text) = @_; 1103 my ($self, $text) = @_;
1071 1104
1072 $self->{layout}->set_text ($text); 1105 $self->{layout}->set_text ($text);
1073 1106
1074 delete $self->{texture}; 1107 delete $self->{texture};
1108 $self->check_size;
1075 $self->update; 1109 $self->update;
1076} 1110}
1077 1111
1078sub set_markup { 1112sub set_markup {
1079 my ($self, $markup) = @_; 1113 my ($self, $markup) = @_;
1080 1114
1081 $self->{layout}->set_markup ($markup); 1115 $self->{layout}->set_markup ($markup);
1082 1116
1083 delete $self->{texture}; 1117 delete $self->{texture};
1118 $self->check_size;
1084 $self->update; 1119 $self->update;
1085} 1120}
1086 1121
1087sub size_request { 1122sub size_request {
1088 my ($self) = @_; 1123 my ($self) = @_;
1109 1144
1110sub size_allocate { 1145sub size_allocate {
1111 my ($self, $w, $h) = @_; 1146 my ($self, $w, $h) = @_;
1112 1147
1113 delete $self->{texture}; 1148 delete $self->{texture};
1149}
1150
1151sub set_fontsize {
1152 my ($self, $fontsize) = @_;
1153
1154 $self->{fontsize} = $fontsize;
1155 $self->check_size;
1114} 1156}
1115 1157
1116sub _draw { 1158sub _draw {
1117 my ($self) = @_; 1159 my ($self) = @_;
1118 1160
1164 active_bg => [1, 1, 1, 0.5], 1206 active_bg => [1, 1, 1, 0.5],
1165 active_fg => [0, 0, 0], 1207 active_fg => [0, 0, 0],
1166 can_hover => 1, 1208 can_hover => 1,
1167 can_focus => 1, 1209 can_focus => 1,
1168 valign => 0, 1210 valign => 0,
1211 can_events => 1,
1169 @_ 1212 @_
1170 ) 1213 )
1171} 1214}
1172 1215
1173sub _set_text { 1216sub _set_text {
1201} 1244}
1202 1245
1203sub size_allocate { 1246sub size_allocate {
1204 my ($self, $w, $h) = @_; 1247 my ($self, $w, $h) = @_;
1205 1248
1206 $self->_set_text ($self->{text}); 1249 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1207} 1250}
1208 1251
1209sub set_text { 1252sub set_text {
1210 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1211 1254
1344our @ISA = CFClient::UI::Label::; 1387our @ISA = CFClient::UI::Label::;
1345 1388
1346use CFClient::OpenGL; 1389use CFClient::OpenGL;
1347 1390
1348my @tex = 1391my @tex =
1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1392 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1350 qw(b1_button_active.png); 1393 qw(b1_button_active.png);
1351 1394
1352sub new { 1395sub new {
1353 my $class = shift; 1396 my $class = shift;
1354 1397
1358 bg => [1, 1, 1, 0.2], 1401 bg => [1, 1, 1, 0.2],
1359 active_fg => [0, 0, 1], 1402 active_fg => [0, 0, 1],
1360 can_hover => 1, 1403 can_hover => 1,
1361 align => 0, 1404 align => 0,
1362 valign => 0, 1405 valign => 0,
1406 can_events => 1,
1363 @_ 1407 @_
1364 ) 1408 )
1365} 1409}
1366 1410
1367sub button_up { 1411sub button_up {
1401package CFClient::UI::CheckBox; 1445package CFClient::UI::CheckBox;
1402 1446
1403our @ISA = CFClient::UI::DrawBG::; 1447our @ISA = CFClient::UI::DrawBG::;
1404 1448
1405my @tex = 1449my @tex =
1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1407 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1451 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1408 1452
1409use CFClient::OpenGL; 1453use CFClient::OpenGL;
1410 1454
1411sub new { 1455sub new {
1460 glDisable GL_BLEND; 1504 glDisable GL_BLEND;
1461} 1505}
1462 1506
1463############################################################################# 1507#############################################################################
1464 1508
1509package CFClient::UI::Image;
1510
1511our @ISA = CFClient::UI::Base::;
1512
1513use CFClient::OpenGL;
1514use Carp qw/confess/;
1515
1516our %loaded_images;
1517
1518sub new {
1519 my $class = shift;
1520
1521 my $self = $class->SUPER::new (can_events => 0, @_);
1522
1523 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1524
1525 $loaded_images{$self->{image}} ||=
1526 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1527
1528 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1529
1530 Scalar::Util::weaken $loaded_images{$self->{image}};
1531
1532 $self->{aspect} = $tex->{w} / $tex->{h};
1533
1534 $self
1535}
1536
1537sub size_request {
1538 my ($self) = @_;
1539
1540 ($self->{tex}->{w}, $self->{tex}->{h})
1541}
1542
1543sub _draw {
1544 my ($self) = @_;
1545
1546 my $tex = $self->{tex};
1547
1548 my ($w, $h) = ($self->{w}, $self->{h});
1549
1550 if ($self->{rot90}) {
1551 glRotate 90, 0, 0, 1;
1552 glTranslate 0, -$self->{w}, 0;
1553
1554 ($w, $h) = ($h, $w);
1555 }
1556
1557 glEnable GL_BLEND;
1558 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1559 glEnable GL_TEXTURE_2D;
1560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1561
1562 $tex->draw_quad (0, 0, $w, $h);
1563
1564 glDisable GL_BLEND;
1565 glDisable GL_TEXTURE_2D;
1566}
1567
1568#############################################################################
1569
1465package CFClient::UI::VGauge; 1570package CFClient::UI::VGauge;
1466 1571
1467our @ISA = CFClient::UI::Base::; 1572our @ISA = CFClient::UI::Base::;
1468 1573
1469use CFClient::OpenGL; 1574use CFClient::OpenGL;
1470 1575
1471my %tex = ( 1576my %tex = (
1472 food => [ 1577 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1579 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ], 1580 ],
1476 grace => [ 1581 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1582 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1583 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1479 ], 1584 ],
1480 hp => [ 1585 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1586 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1587 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ], 1588 ],
1484 mana => [ 1589 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1590 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1591 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1487 ], 1592 ],
1488); 1593);
1489 1594
1490# eg. VGauge->new (gauge => 'food'), default gauge: food 1595# eg. VGauge->new (gauge => 'food'), default gauge: food
1586 1691
1587 my $self = $class->SUPER::new ( 1692 my $self = $class->SUPER::new (
1588 @_, 1693 @_,
1589 ); 1694 );
1590 1695
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1696 $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1697 $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1698 $self->add ($self->{value});
1592 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1699 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1593 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1700 $self->add ($self->{max});
1594 1701
1595 $self 1702 $self
1703}
1704
1705sub size_request {
1706 my ($self) = @_;
1707 (($self->{max}->size_request)[0], 0)
1708}
1709
1710sub set_fontsize {
1711 my ($self, $fsize) = @_;
1712
1713 $self->{value}->set_fontsize ($fsize);
1714 $self->{max} ->set_fontsize ($fsize);
1596} 1715}
1597 1716
1598sub set_value { 1717sub set_value {
1599 my ($self, $val, $max) = @_; 1718 my ($self, $val, $max) = @_;
1600 1719
1957 my $class = shift; 2076 my $class = shift;
1958 2077
1959 my $self = $class->SUPER::new ( 2078 my $self = $class->SUPER::new (
1960 state => 0, 2079 state => 0,
1961 connect_activate => \&toggle_flopper, 2080 connect_activate => \&toggle_flopper,
2081 can_events => 1,
1962 @_ 2082 @_
1963 ); 2083 );
1964 2084
1965 if ($self->{state}) { 2085 if ($self->{state}) {
1966 $self->{state} = 0; 2086 $self->{state} = 0;
1995use CFClient::OpenGL; 2115use CFClient::OpenGL;
1996 2116
1997sub check_size { 2117sub check_size {
1998 my ($self) = @_; 2118 my ($self) = @_;
1999 2119
2000 $self->configure (0, 0, $::WITH, $::HEIGHT); 2120 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2001} 2121}
2002 2122
2003sub size_request { 2123sub size_request {
2004 ($::WIDTH, $::HEIGHT) 2124 ($::WIDTH, $::HEIGHT)
2005} 2125}
2007sub configure { 2127sub configure {
2008 my ($self, $x, $y, $w, $h) = @_; 2128 my ($self, $x, $y, $w, $h) = @_;
2009 2129
2010 $self->SUPER::configure ($x, $y, $w, $h); 2130 $self->SUPER::configure ($x, $y, $w, $h);
2011 2131
2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2013 for @{$self->{children}}; 2132 for my $child (@{$self->{children}}) {
2133 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2134
2135 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2136 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2137 $child->configure ($X, $Y, $W,$H);
2138 }
2014} 2139}
2015 2140
2016sub _topleft { 2141sub _topleft {
2017 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
2018 2143
2026 ::refresh (); 2151 ::refresh ();
2027} 2152}
2028 2153
2029sub add { 2154sub add {
2030 my ($self, $child) = @_; 2155 my ($self, $child) = @_;
2156
2157 # integerize window positions
2158 $child->{x} = int $child->{x};
2159 $child->{y} = int $child->{y};
2031 2160
2032 $self->SUPER::add ($child); 2161 $self->SUPER::add ($child);
2033} 2162}
2034 2163
2035sub on_refresh { 2164sub on_refresh {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines