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.140 by root, Thu Apr 20 04:20:52 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
151 $self->{x} = $x;
152 $self->{y} = $y;
153
154 if ($self->{aspect}) { 152 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect}; 153 my $w2 = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect}; 154 my $h2 = List::Util::min $h, int $w / $self->{aspect};
157 }
158 155
156 # use alignment to adjust x, y
157
158 $x += int +($w - $w2) * 0.5;
159 $y += int +($h - $h2) * 0.5;
160
161 ($w, $h) = ($w2, $h2);
162 }
163
164 if ($self->{x} != $x || $self->{y} != $y) {
165 $self->{x} = $x;
166 $self->{y} = $y;
167 $self->update;
168 }
169
159 return unless $self->{w} != $w || $self->{h} != $h; 170 if ($self->{w} != $w || $self->{h} != $h) {
160
161 $self->{w} = $w; 171 $self->{w} = $w;
162 $self->{h} = $h; 172 $self->{h} = $h;
163 173
164 $self->size_allocate ($w, $h); 174 $self->size_allocate ($w, $h);
165 $self->update; 175 $self->update;
176 }
166} 177}
167 178
168sub size_allocate { 179sub size_allocate {
169 # nothing to be done 180 # nothing to be done
170} 181}
268} 279}
269 280
270sub find_widget { 281sub find_widget {
271 my ($self, $x, $y) = @_; 282 my ($self, $x, $y) = @_;
272 283
284 return () unless $self->{can_events};
285
273 return $self 286 return $self
274 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 287 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
275 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 288 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
276 289
277 () 290 ()
284} 297}
285 298
286sub check_size { 299sub check_size {
287 my ($self) = @_; 300 my ($self) = @_;
288 301
302 return unless $self->{parent};
303
289 my ($w, $h) = $self->size_request; 304 my ($w, $h) = $self->size_request;
290 305
291 if ($w != $self->{req_w} || $h != $self->{req_h}) { 306 if ($w != $self->{req_w} || $h != $self->{req_h}) {
292 $self->{req_w} = $w; 307 $self->{req_w} = $w;
293 $self->{req_h} = $h; 308 $self->{req_h} = $h;
294 309
295 $self->{parent}->check_size 310 $self->{parent}->check_size;
296 if $self->{parent};
297 } 311 }
298} 312}
299 313
300sub update { 314sub update {
301 my ($self) = @_; 315 my ($self) = @_;
368 382
369package CFClient::UI::Empty; 383package CFClient::UI::Empty;
370 384
371our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
372 386
387sub new {
388 my ($class, %arg) = @_;
389 $class->SUPER::new (can_events => 0, %arg);
390}
391
373sub size_request { 392sub size_request {
374 (0, 0) 393 (0, 0)
375} 394}
376 395
377sub draw { } 396sub draw { }
385sub new { 404sub new {
386 my ($class, %arg) = @_; 405 my ($class, %arg) = @_;
387 406
388 my $children = delete $arg{children} || []; 407 my $children = delete $arg{children} || [];
389 408
390 my $self = $class->SUPER::new (children => [], %arg); 409 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
391 $self->add ($_) for @$children; 410 $self->add ($_) for @$children;
392 411
393 $self 412 $self
394} 413}
395 414
551sub new { die } 570sub new { die }
552 571
553sub size_request { 572sub size_request {
554 my ($self) = @_; 573 my ($self) = @_;
555 574
556 @$self{qw(child_w child_h)} = $self->child->size_request; 575 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
557 $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)});
558 577
559 @$self{qw(child_w child_h)} 578 @$self{qw(child_w child_h)}
560} 579}
561 580
571package CFClient::UI::Frame; 590package CFClient::UI::Frame;
572 591
573our @ISA = CFClient::UI::Bin::; 592our @ISA = CFClient::UI::Bin::;
574 593
575use CFClient::OpenGL; 594use CFClient::OpenGL;
576
577sub size_request {
578 my ($self) = @_;
579 my $chld = $self->child
580 or return (0, 0);
581
582 $chld->move (2, 2);
583
584 map { $_ + 4 } $chld->size_request;
585}
586
587sub size_allocate {
588 my ($self, $x, $y, $w, $h) = @_;
589
590 $self->child->configure (2, 2, $w - 4, $h - 4);
591}
592
593sub _draw {
594 my ($self) = @_;
595
596 my $chld = $self->child;
597
598 my ($w, $h) = $chld->size_request;
599
600 glBegin GL_QUADS;
601 glColor 0, 0, 0;
602 glVertex 0 , 0;
603 glVertex 0 , $h + 4;
604 glVertex $w + 4 , $h + 4;
605 glVertex $w + 4 , 0;
606 glEnd;
607
608 $chld->draw;
609}
610
611#############################################################################
612
613package CFClient::UI::FancyFrame;
614
615our @ISA = CFClient::UI::Bin::;
616
617use CFClient::OpenGL;
618
619my @tex =
620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
622 595
623sub new { 596sub new {
624 my $class = shift; 597 my $class = shift;
625 598
626 # TODO: user_x, user_y, overwrite moveto?
627
628 $class->SUPER::new ( 599 my $self = $class->SUPER::new (
629 bg => [1, 1, 1, 1], 600 bg => [1, 1, 1, 1],
630 border_bg => [1, 1, 1, 1], 601 border_bg => [1, 1, 1, 1],
631 border => 0.8, 602 border => 0.8,
632 @_ 603 @_
633 ) 604 );
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
677 $self->{title} &&= new CFClient::UI::Label
678 align => 0,
679 valign => 1,
680 text => $self->{title},
681 fontsize => 1;
682
683 $self
634} 684}
635 685
636sub border { 686sub border {
637 int $_[0]{border} * $::FONTSIZE 687 int $_[0]{border} * $::FONTSIZE
638} 688}
653sub size_allocate { 703sub size_allocate {
654 my ($self, $w, $h) = @_; 704 my ($self, $w, $h) = @_;
655 705
656 $h -= List::Util::max 0, $self->border * 2; 706 $h -= List::Util::max 0, $self->border * 2;
657 $w -= List::Util::max 0, $self->border * 2; 707 $w -= List::Util::max 0, $self->border * 2;
708
709 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
710 if $self->{title};
658 711
659 $self->child->configure ($self->border, $self->border, $w, $h); 712 $self->child->configure ($self->border, $self->border, $w, $h);
660} 713}
661 714
662sub button_down { 715sub button_down {
742 $bg->draw_quad ($border, $border, $cw, $ch); 795 $bg->draw_quad ($border, $border, $cw, $ch);
743 796
744 glDisable GL_TEXTURE_2D; 797 glDisable GL_TEXTURE_2D;
745 glDisable GL_BLEND; 798 glDisable GL_BLEND;
746 799
800 $self->{title}->draw if $self->{title};
747 $self->child->draw; 801 $self->child->draw;
748} 802}
749 803
750############################################################################# 804#############################################################################
751 805
793 or next; 847 or next;
794 848
795 for my $x (0 .. $#$row) { 849 for my $x (0 .. $#$row) {
796 my $widget = $row->[$x] 850 my $widget = $row->[$x]
797 or next; 851 or next;
798 my ($w, $h) = $widget->size_request; 852 my ($w, $h) = @$widget{qw(req_w req_h)};
799 853
800 $w[$x] = max $w[$x], $w; 854 $w[$x] = max $w[$x], $w;
801 $h[$y] = max $h[$y], $h; 855 $h[$y] = max $h[$y], $h;
802 } 856 }
803 } 857 }
908 962
909 ($h, $w) = ($w, $h); 963 ($h, $w) = ($w, $h);
910 964
911 my $children = $self->{children}; 965 my $children = $self->{children};
912 966
913 my @h = map +($_->size_request)[0], @$children; 967 my @h = map $_->{req_w}, @$children;
914 968
915 my $req_h = List::Util::sum @h; 969 my $req_h = List::Util::sum @h;
916 970
917 if ($req_h > $h) { 971 if ($req_h > $h) {
918 # ah well, not enough space 972 # ah well, not enough space
966sub size_allocate { 1020sub size_allocate {
967 my ($self, $w, $h) = @_; 1021 my ($self, $w, $h) = @_;
968 1022
969 my $children = $self->{children}; 1023 my $children = $self->{children};
970 1024
971 my @h = map +($_->size_request)[1], @$children; 1025 my @h = map $_->{req_h}, @$children;
972 1026
973 my $req_h = List::Util::sum @h; 1027 my $req_h = List::Util::sum @h;
974 1028
975 if ($req_h > $h) { 1029 if ($req_h > $h) {
976 # ah well, not enough space 1030 # ah well, not enough space
1017 text => "", 1071 text => "",
1018 align => -1, 1072 align => -1,
1019 valign => -1, 1073 valign => -1,
1020 padding => 2, 1074 padding => 2,
1021 layout => new CFClient::Layout, 1075 layout => new CFClient::Layout,
1076 can_events => 0,
1022 %arg 1077 %arg
1023 ); 1078 );
1024 1079
1025 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1080 if (exists $self->{template}) {
1081 my $layout = new CFClient::Layout;
1082 $layout->set_text (delete $self->{template});
1083 $self->{template} = $layout;
1084 }
1026 1085
1027 $self->set_text (delete $self->{text}) if exists $self->{text}; 1086 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1087 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1029 1088
1030 $self 1089 $self
1044 my ($self, $text) = @_; 1103 my ($self, $text) = @_;
1045 1104
1046 $self->{layout}->set_text ($text); 1105 $self->{layout}->set_text ($text);
1047 1106
1048 delete $self->{texture}; 1107 delete $self->{texture};
1108 $self->check_size;
1049 $self->update; 1109 $self->update;
1050} 1110}
1051 1111
1052sub set_markup { 1112sub set_markup {
1053 my ($self, $markup) = @_; 1113 my ($self, $markup) = @_;
1054 1114
1055 $self->{layout}->set_markup ($markup); 1115 $self->{layout}->set_markup ($markup);
1056 1116
1057 delete $self->{texture}; 1117 delete $self->{texture};
1118 $self->check_size;
1058 $self->update; 1119 $self->update;
1059} 1120}
1060 1121
1061sub size_request { 1122sub size_request {
1062 my ($self) = @_; 1123 my ($self) = @_;
1063 1124
1064 $self->{layout}->set_width; 1125 $self->{layout}->set_width;
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066 1127
1067 my ($w, $h) = $self->{layout}->size; 1128 my ($w, $h) = $self->{layout}->size;
1129
1130 if (exists $self->{template}) {
1131 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1132
1133 my ($w2, $h2) = $self->{template}->size;
1134
1135 $w = List::Util::max $w, $w2;
1136 $h = List::Util::max $h, $h2;
1137 }
1068 1138
1069 ( 1139 (
1070 $w + $self->{padding} * 2, 1140 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2, 1141 $h + $self->{padding} * 2,
1072 ) 1142 )
1074 1144
1075sub size_allocate { 1145sub size_allocate {
1076 my ($self, $w, $h) = @_; 1146 my ($self, $w, $h) = @_;
1077 1147
1078 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;
1079} 1156}
1080 1157
1081sub _draw { 1158sub _draw {
1082 my ($self) = @_; 1159 my ($self) = @_;
1083 1160
1129 active_bg => [1, 1, 1, 0.5], 1206 active_bg => [1, 1, 1, 0.5],
1130 active_fg => [0, 0, 0], 1207 active_fg => [0, 0, 0],
1131 can_hover => 1, 1208 can_hover => 1,
1132 can_focus => 1, 1209 can_focus => 1,
1133 valign => 0, 1210 valign => 0,
1211 can_events => 1,
1134 @_ 1212 @_
1135 ) 1213 )
1136} 1214}
1137 1215
1138sub _set_text { 1216sub _set_text {
1166} 1244}
1167 1245
1168sub size_allocate { 1246sub size_allocate {
1169 my ($self, $w, $h) = @_; 1247 my ($self, $w, $h) = @_;
1170 1248
1171 $self->_set_text ($self->{text}); 1249 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1172} 1250}
1173 1251
1174sub set_text { 1252sub set_text {
1175 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1176 1254
1309our @ISA = CFClient::UI::Label::; 1387our @ISA = CFClient::UI::Label::;
1310 1388
1311use CFClient::OpenGL; 1389use CFClient::OpenGL;
1312 1390
1313my @tex = 1391my @tex =
1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1392 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1315 qw(b1_button_active.png); 1393 qw(b1_button_active.png);
1316 1394
1317sub new { 1395sub new {
1318 my $class = shift; 1396 my $class = shift;
1319 1397
1323 bg => [1, 1, 1, 0.2], 1401 bg => [1, 1, 1, 0.2],
1324 active_fg => [0, 0, 1], 1402 active_fg => [0, 0, 1],
1325 can_hover => 1, 1403 can_hover => 1,
1326 align => 0, 1404 align => 0,
1327 valign => 0, 1405 valign => 0,
1406 can_events => 1,
1328 @_ 1407 @_
1329 ) 1408 )
1330} 1409}
1331 1410
1332sub button_up { 1411sub button_up {
1366package CFClient::UI::CheckBox; 1445package CFClient::UI::CheckBox;
1367 1446
1368our @ISA = CFClient::UI::DrawBG::; 1447our @ISA = CFClient::UI::DrawBG::;
1369 1448
1370my @tex = 1449my @tex =
1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1372 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1451 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1373 1452
1374use CFClient::OpenGL; 1453use CFClient::OpenGL;
1375 1454
1376sub new { 1455sub new {
1425 glDisable GL_BLEND; 1504 glDisable GL_BLEND;
1426} 1505}
1427 1506
1428############################################################################# 1507#############################################################################
1429 1508
1430package CFClient::UI::VGauge; 1509package CFClient::UI::Image;
1431 1510
1432our @ISA = CFClient::UI::Base::; 1511our @ISA = CFClient::UI::Base::;
1433 1512
1434use CFClient::OpenGL; 1513use CFClient::OpenGL;
1514use Carp qw/confess/;
1435 1515
1436my %tex = ( 1516our %loaded_images;
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 1517
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1518sub new {
1457 my $class = shift; 1519 my $class = shift;
1458 1520
1459 my $self = $class->SUPER::new ( 1521 my $self = $class->SUPER::new (can_events => 0, @_);
1460 gauge => 'food',
1461 @_
1462 );
1463 1522
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h}; 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};
1465 1533
1466 $self 1534 $self
1467} 1535}
1468 1536
1469sub size_request { 1537sub size_request {
1470 my ($self) = @_; 1538 my ($self) = @_;
1471 1539
1472 my $tex = $tex{$self->{gauge}}[0]; 1540 ($self->{tex}->{w}, $self->{tex}->{h})
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} 1541}
1494 1542
1495sub _draw { 1543sub _draw {
1496 my ($self) = @_; 1544 my ($self) = @_;
1497 1545
1498 my $tex = $tex{$self->{gauge}}; 1546 my $tex = $self->{tex};
1499 1547
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1548 my ($w, $h) = ($self->{w}, $self->{h});
1501 1549
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1550 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1551 glRotate 90, 0, 0, 1;
1552 glTranslate 0, -$self->{w}, 0;
1504 1553
1505 my $t1 = $tex->[0]; 1554 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1555 }
1507 1556
1508 glEnable GL_BLEND; 1557 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1558 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1559 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 1561
1562 $tex->draw_quad (0, 0, $w, $h);
1563
1564 glDisable GL_BLEND;
1565 glDisable GL_TEXTURE_2D;
1566}
1567
1568#############################################################################
1569
1570package CFClient::UI::VGauge;
1571
1572our @ISA = CFClient::UI::Base::;
1573
1574use CFClient::OpenGL;
1575
1576my %tex = (
1577 food => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1580 ],
1581 grace => [
1582 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1583 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1584 ],
1585 hp => [
1586 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1587 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1588 ],
1589 mana => [
1590 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1591 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1592 ],
1593);
1594
1595# eg. VGauge->new (gauge => 'food'), default gauge: food
1596sub new {
1597 my $class = shift;
1598
1599 my $self = $class->SUPER::new (
1600 type => 'food',
1601 @_
1602 );
1603
1604 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 #my $tex = $tex{$self->{type}}[0];
1613 #@$tex{qw(w h)}
1614 (0, 0)
1615}
1616
1617sub set_max {
1618 my ($self, $max) = @_;
1619
1620 $self->{max_val} = $max;
1621}
1622
1623sub set_value {
1624 my ($self, $val, $max) = @_;
1625
1626 $self->set_max ($max)
1627 if defined $max;
1628
1629 $max = $self->{max_val};
1630 $self->{val} = $val;
1631
1632 $self->update;
1633}
1634
1635sub _draw {
1636 my ($self) = @_;
1637
1638 my $tex = $tex{$self->{type}};
1639
1640 my ($w, $h) = ($self->{w}, $self->{h});
1641
1642 if ($self->{vertical}) {
1643 glRotate 90, 0, 0, 1;
1644 glTranslate 0, -$self->{w}, 0;
1645
1646 ($w, $h) = ($h, $w);
1647 }
1648
1649 my $ycut = $self->{val} / ($self->{max_val} || 1);
1650 $ycut = 1 if $self->{val} > $self->{max_val};
1651
1652 my $t1 = $tex->[0];
1653 my $t2 = $tex->[1];
1654
1655 glEnable GL_BLEND;
1656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1657 glEnable GL_TEXTURE_2D;
1658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1659
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1660 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h}; 1661 my $h2 = $ycut * $self->{h};
1515 1662
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1663 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS; 1664 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp; 1665 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1666 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1667 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1668 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd; 1669 glEnd;
1525
1526 $yp += $h1;
1527 1670
1528 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1671 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS; 1672 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1673 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1674 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1675 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1676 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1534 glEnd; 1677 glEnd;
1535 1678
1536 glDisable GL_BLEND; 1679 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1680 glDisable GL_TEXTURE_2D;
1681}
1682
1683#############################################################################
1684
1685package CFClient::UI::Gauge;
1686
1687our @ISA = CFClient::UI::VBox::;
1688
1689sub new {
1690 my ($class, %arg) = shift;
1691
1692 my $self = $class->SUPER::new (
1693 @_,
1694 );
1695
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});
1699 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1700 $self->add ($self->{max});
1701
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);
1715}
1716
1717sub set_value {
1718 my ($self, $val, $max) = @_;
1719
1720 $self->set_max ($max)
1721 if defined $max;
1722
1723 $self->{gauge}->set_value ($val, $max);
1724 $self->{value}->set_text ($val);
1725}
1726
1727sub set_max {
1728 my ($self, $max) = @_;
1729
1730 $self->{gauge}->set_max ($max);
1731 $self->{max}->set_text ($max);
1538} 1732}
1539 1733
1540############################################################################# 1734#############################################################################
1541 1735
1542package CFClient::UI::Slider; 1736package CFClient::UI::Slider;
1882 my $class = shift; 2076 my $class = shift;
1883 2077
1884 my $self = $class->SUPER::new ( 2078 my $self = $class->SUPER::new (
1885 state => 0, 2079 state => 0,
1886 connect_activate => \&toggle_flopper, 2080 connect_activate => \&toggle_flopper,
2081 can_events => 1,
1887 @_ 2082 @_
1888 ); 2083 );
1889 2084
1890 if ($self->{state}) { 2085 if ($self->{state}) {
1891 $self->{state} = 0; 2086 $self->{state} = 0;
1920use CFClient::OpenGL; 2115use CFClient::OpenGL;
1921 2116
1922sub check_size { 2117sub check_size {
1923 my ($self) = @_; 2118 my ($self) = @_;
1924 2119
1925 $self->configure (0, 0, $::WITH, $::HEIGHT); 2120 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
1926} 2121}
1927 2122
1928sub size_request { 2123sub size_request {
1929 ($::WIDTH, $::HEIGHT) 2124 ($::WIDTH, $::HEIGHT)
1930} 2125}
1932sub configure { 2127sub configure {
1933 my ($self, $x, $y, $w, $h) = @_; 2128 my ($self, $x, $y, $w, $h) = @_;
1934 2129
1935 $self->SUPER::configure ($x, $y, $w, $h); 2130 $self->SUPER::configure ($x, $y, $w, $h);
1936 2131
1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1938 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 }
1939} 2139}
1940 2140
1941sub _topleft { 2141sub _topleft {
1942 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
1943 2143
1951 ::refresh (); 2151 ::refresh ();
1952} 2152}
1953 2153
1954sub add { 2154sub add {
1955 my ($self, $child) = @_; 2155 my ($self, $child) = @_;
2156
2157 # integerize window positions
2158 $child->{x} = int $child->{x};
2159 $child->{y} = int $child->{y};
1956 2160
1957 $self->SUPER::add ($child); 2161 $self->SUPER::add ($child);
1958} 2162}
1959 2163
1960sub on_refresh { 2164sub on_refresh {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines