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.264 by root, Thu Jun 1 02:58:30 2006 UTC vs.
Revision 1.271 by root, Fri Jun 2 22:13:47 2006 UTC

40 40
41 $LAYOUT = $layout; 41 $LAYOUT = $layout;
42} 42}
43 43
44sub check_tooltip { 44sub check_tooltip {
45 return if $ENV{CFPLUS_DEBUG} & 8;
46
45 if (!$GRAB) { 47 if (!$GRAB) {
46 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 48 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
47 if (length $widget->{tooltip}) { 49 if (length $widget->{tooltip}) {
48
49 if ($TOOLTIP->{owner} != $widget) { 50 if ($TOOLTIP->{owner} != $widget) {
50 $TOOLTIP->hide; 51 $TOOLTIP->hide;
51 52
52 $TOOLTIP->{owner} = $widget; 53 $TOOLTIP->{owner} = $widget;
53 54
170sub rescale_widgets { 171sub rescale_widgets {
171 my ($sx, $sy) = @_; 172 my ($sx, $sy) = @_;
172 173
173 for my $widget (values %WIDGET) { 174 for my $widget (values %WIDGET) {
174 if ($widget->{is_toplevel}) { 175 if ($widget->{is_toplevel}) {
175 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 176 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
176 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 177 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
177 178
178 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 179 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
179 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 180 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
180 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w}; 181 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
181 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/; 182 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 183 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
183 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h}; 184 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
184 185
185 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 186 $widget->{x} -= int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
186 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 187 $widget->{y} -= int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
187 188
188 } 189 }
189 } 190 }
190 191
191 reconfigure_widgets; 192 reconfigure_widgets;
438 439
439 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus 440 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
440 unless $FOCUS; 441 unless $FOCUS;
441} 442}
442 443
443sub mouse_motion { } 444sub mouse_motion { 0 }
444sub button_up { } 445sub button_up { 0 }
445sub key_down { } 446sub key_down { 0 }
446sub key_up { } 447sub key_up { 0 }
447 448
448sub button_down { 449sub button_down {
449 my ($self, $ev, $x, $y) = @_; 450 my ($self, $ev, $x, $y) = @_;
450 451
451 $self->focus_in; 452 $self->focus_in;
452}
453 453
454sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 454 0
455sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 455}
456sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
457sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
458sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
459 456
460sub find_widget { 457sub find_widget {
461 my ($self, $x, $y) = @_; 458 my ($self, $x, $y) = @_;
462 459
463 return () unless $self->{can_events}; 460 return () unless $self->{can_events};
525 522
526 $self->realloc; 523 $self->realloc;
527 $self->update; 524 $self->update;
528} 525}
529 526
527# using global variables seems a bit hacky, but passing through all drawing
528# functions seems pointless.
529our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
530
530sub draw { 531sub draw {
531 my ($self) = @_; 532 my ($self) = @_;
532 533
533 return unless $self->{h} && $self->{w}; 534 return unless $self->{h} && $self->{w};
535
536 # update screen rectangle
537 local $draw_x = $draw_x + $self->{x};
538 local $draw_y = $draw_y + $self->{y};
539 local $draw_w = $draw_x + $self->{w};
540 local $draw_h = $draw_y + $self->{h};
541
542 # skip widgets that are entirely outside the drawing area
543 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
544 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
534 545
535 glPushMatrix; 546 glPushMatrix;
536 glTranslate $self->{x}, $self->{y}, 0; 547 glTranslate $self->{x}, $self->{y}, 0;
537 $self->_draw; 548 $self->_draw;
538 glPopMatrix; 549 glPopMatrix;
803 $self->SUPER::size_allocate ($w, $h); 814 $self->SUPER::size_allocate ($w, $h);
804 $self->update; 815 $self->update;
805} 816}
806 817
807sub _render { 818sub _render {
819 my ($self) = @_;
820
808 $_[0]{children}[0]->draw; 821 $self->{children}[0]->draw;
809} 822}
810 823
811sub render_child { 824sub render_child {
812 my ($self) = @_; 825 my ($self) = @_;
813 826
814 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 827 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
815 glClearColor 0, 0, 0, 0; 828 glClearColor 0, 0, 0, 0;
816 glClear GL_COLOR_BUFFER_BIT; 829 glClear GL_COLOR_BUFFER_BIT;
817 830
831 {
832 package CFClient::UI::Base;
833
834 ($draw_x, $draw_y, $draw_w, $draw_h) =
835 (0, 0, $self->{w}, $self->{h});
836 }
837
818 $self->_render; 838 $self->_render;
819 }; 839 };
820} 840}
821 841
822sub _draw { 842sub _draw {
823 my ($self) = @_; 843 my ($self) = @_;
824 844
825 my ($w, $h) = ($self->w, $self->h); 845 my ($w, $h) = @$self{qw(w h)};
826 846
827 my $tex = $self->{texture} 847 my $tex = $self->{texture}
828 or return; 848 or return;
829 849
830 glEnable GL_TEXTURE_2D; 850 glEnable GL_TEXTURE_2D;
912 } 932 }
913} 933}
914 934
915sub _render { 935sub _render {
916 my ($self) = @_; 936 my ($self) = @_;
937
938 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x};
939 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y};
917 940
918 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 941 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
919 942
920 $self->SUPER::_render; 943 $self->SUPER::_render;
921} 944}
1029my @border = 1052my @border =
1030 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1053 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1031 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1054 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1032 1055
1033sub new { 1056sub new {
1034 my $class = shift; 1057 my ($class, %arg) = @_;
1058
1059 my $title = delete $arg{title};
1035 1060
1036 my $self = $class->SUPER::new ( 1061 my $self = $class->SUPER::new (
1037 bg => [1, 1, 1, 1], 1062 bg => [1, 1, 1, 1],
1038 border_bg => [1, 1, 1, 1], 1063 border_bg => [1, 1, 1, 1],
1039 border => 0.6, 1064 border => 0.6,
1040 can_events => 1, 1065 can_events => 1,
1041 min_w => 16, 1066 min_w => 16,
1042 min_h => 16, 1067 min_h => 16,
1043 @_ 1068 %arg,
1044 ); 1069 );
1045 1070
1046 $self->{title} &&= new CFClient::UI::Label 1071 $self->{title} = new CFClient::UI::Label
1047 align => 0, 1072 align => 0,
1048 valign => 1, 1073 valign => 1,
1049 text => $self->{title}, 1074 text => $title,
1050 fontsize => $self->{border}; 1075 fontsize => $self->{border}
1076 if defined $title;
1051 1077
1052 $self 1078 $self
1079}
1080
1081sub add {
1082 my ($self, @widgets) = @_;
1083
1084 $self->SUPER::add (@widgets);
1085 $self->CFClient::UI::Container::add ($self->{title}) if $self->{title};
1053} 1086}
1054 1087
1055sub border { 1088sub border {
1056 int $_[0]{border} * $::FONTSIZE 1089 int $_[0]{border} * $::FONTSIZE
1057} 1090}
1058 1091
1059sub size_request { 1092sub size_request {
1060 my ($self) = @_; 1093 my ($self) = @_;
1094
1095 $self->{title}->size_request
1096 if $self->{title};
1061 1097
1062 my ($w, $h) = $self->SUPER::size_request; 1098 my ($w, $h) = $self->SUPER::size_request;
1063 1099
1064 ( 1100 (
1065 $w + $self->border * 2, 1101 $w + $self->border * 2,
1068} 1104}
1069 1105
1070sub size_allocate { 1106sub size_allocate {
1071 my ($self, $w, $h) = @_; 1107 my ($self, $w, $h) = @_;
1072 1108
1109 if ($self->{title}) {
1110 $self->{title}{w} = $w;
1111 $self->{title}{h} = $h;
1112 $self->{title}->size_allocate ($w, $h);
1113 }
1114
1115 my $border = $self->border;
1116
1073 $h -= List::Util::max 0, $self->border * 2; 1117 $h -= List::Util::max 0, $border * 2;
1074 $w -= List::Util::max 0, $self->border * 2; 1118 $w -= List::Util::max 0, $border * 2;
1075 1119
1076 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1077 if $self->{title};
1078
1079 $self->child->configure ($self->border, $self->border, $w, $h); 1120 $self->child->configure ($border, $border, $w, $h);
1080} 1121}
1081 1122
1082sub button_down { 1123sub button_down {
1083 my ($self, $ev, $x, $y) = @_; 1124 my ($self, $ev, $x, $y) = @_;
1084 1125
1118 1159
1119 ($x, $y) = ($ev->{x}, $ev->{y}); 1160 ($x, $y) = ($ev->{x}, $ev->{y});
1120 1161
1121 $self->move_abs ($bx + $x - $ox, $by + $y - $oy); 1162 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1122 }; 1163 };
1164 } else {
1165 return 0;
1166 }
1167
1123 } 1168 1
1124} 1169}
1125 1170
1126sub button_up { 1171sub button_up {
1127 my ($self, $ev, $x, $y) = @_; 1172 my ($self, $ev, $x, $y) = @_;
1128 1173
1129 delete $self->{motion}; 1174 !!delete $self->{motion}
1130} 1175}
1131 1176
1132sub mouse_motion { 1177sub mouse_motion {
1133 my ($self, $ev, $x, $y) = @_; 1178 my ($self, $ev, $x, $y) = @_;
1134 1179
1135 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1180 $self->{motion}->($ev, $x, $y) if $self->{motion};
1181
1182 !!$self->{motion}
1136} 1183}
1137 1184
1138sub _draw { 1185sub _draw {
1139 my ($self) = @_; 1186 my ($self) = @_;
1140 1187
1188 my $child = $self->{children}[0];
1189
1141 my ($w, $h ) = ($self->{w}, $self->{h}); 1190 my ($w, $h ) = ($self->{w}, $self->{h});
1142 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1191 my ($cw, $ch) = ($child->{w}, $child->{h});
1143 1192
1144 glEnable GL_TEXTURE_2D; 1193 glEnable GL_TEXTURE_2D;
1145 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1194 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1146 1195
1147 my $border = $self->border; 1196 my $border = $self->border;
1162 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1211 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1163 } 1212 }
1164 1213
1165 glDisable GL_TEXTURE_2D; 1214 glDisable GL_TEXTURE_2D;
1166 1215
1167 $self->{title}->draw if $self->{title};
1168
1169 $self->child->draw; 1216 $child->draw;
1217
1218 if ($self->{title}) {
1219 glTranslate 0, $border - $self->{h};
1220 $self->{title}->_draw;
1221 }
1170} 1222}
1171 1223
1172############################################################################# 1224#############################################################################
1173 1225
1174package CFClient::UI::Table; 1226package CFClient::UI::Table;
1522} 1574}
1523 1575
1524sub size_allocate { 1576sub size_allocate {
1525 my ($self, $w, $h) = @_; 1577 my ($self, $w, $h) = @_;
1526 1578
1579 delete $self->{ox};
1580
1527 delete $self->{texture} 1581 delete $self->{texture}
1528 ;#d# 1582 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1529} 1583}
1530 1584
1531sub set_fontsize { 1585sub set_fontsize {
1532 my ($self, $fontsize) = @_; 1586 my ($self, $fontsize) = @_;
1533 1587
1548 $self->{layout}->set_width ($self->{w}); 1602 $self->{layout}->set_width ($self->{w});
1549 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1603 $self->{layout}->set_ellipsise ($self->{ellipsise});
1550 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1604 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1551 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1605 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1552 1606
1553 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1607 new_from_layout CFClient::Texture $self->{layout}
1608 };
1554 1609
1610 unless (exists $self->{ox}) {
1555 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1611 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1556 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1612 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1557 : ($self->{w} - $tex->{w}) * 0.5); 1613 : ($self->{w} - $tex->{w}) * 0.5);
1558 1614
1559 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1615 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1560 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1616 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1561 : ($self->{h} - $tex->{h}) * 0.5); 1617 : ($self->{h} - $tex->{h}) * 0.5);
1562
1563 $tex
1564 }; 1618 };
1565 1619
1566 glEnable GL_TEXTURE_2D; 1620 glEnable GL_TEXTURE_2D;
1567 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1621 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1568 1622
1663 $self->{cursor} = length $text; 1717 $self->{cursor} = length $text;
1664 } elsif ($uni == 27) { 1718 } elsif ($uni == 27) {
1665 $self->_emit ('escape'); 1719 $self->_emit ('escape');
1666 } elsif ($uni) { 1720 } elsif ($uni) {
1667 substr $text, $self->{cursor}++, 0, chr $uni; 1721 substr $text, $self->{cursor}++, 0, chr $uni;
1722 } else {
1723 return 0;
1668 } 1724 }
1669 1725
1670 $self->_set_text ($text); 1726 $self->_set_text ($text);
1671 1727
1672 $self->realloc; 1728 $self->realloc;
1729
1730 1
1673} 1731}
1674 1732
1675sub focus_in { 1733sub focus_in {
1676 my ($self) = @_; 1734 my ($self) = @_;
1677 1735
1692 utf8::encode $text; 1750 utf8::encode $text;
1693 $self->{cursor} = length substr $text, 0, $idx; 1751 $self->{cursor} = length substr $text, 0, $idx;
1694 1752
1695 $self->_set_text ($self->{text}); 1753 $self->_set_text ($self->{text});
1696 $self->update; 1754 $self->update;
1755
1756 1
1697} 1757}
1698 1758
1699sub mouse_motion { 1759sub mouse_motion {
1700 my ($self, $ev, $x, $y) = @_; 1760 my ($self, $ev, $x, $y) = @_;
1701# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1761# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1762
1763 0
1702} 1764}
1703 1765
1704sub _draw { 1766sub _draw {
1705 my ($self) = @_; 1767 my ($self) = @_;
1706 1768
1783 } else { 1845 } else {
1784 $self->set_text ($self->{history_saveback}); 1846 $self->set_text ($self->{history_saveback});
1785 } 1847 }
1786 1848
1787 } else { 1849 } else {
1788 $self->SUPER::key_down ($ev); 1850 return $self->SUPER::key_down ($ev)
1851 }
1852
1789 } 1853 1
1790
1791} 1854}
1792 1855
1793############################################################################# 1856#############################################################################
1794 1857
1795package CFClient::UI::Button; 1858package CFClient::UI::Button;
1824 my ($self, $ev, $x, $y) = @_; 1887 my ($self, $ev, $x, $y) = @_;
1825 1888
1826 $self->emit ("activate") 1889 $self->emit ("activate")
1827 if $x >= 0 && $x < $self->{w} 1890 if $x >= 0 && $x < $self->{w}
1828 && $y >= 0 && $y < $self->{h}; 1891 && $y >= 0 && $y < $self->{h};
1892
1893 1
1829} 1894}
1830 1895
1831sub _draw { 1896sub _draw {
1832 my ($self) = @_; 1897 my ($self) = @_;
1833 1898
1887 1952
1888 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 1953 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1889 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 1954 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1890 $self->{state} = !$self->{state}; 1955 $self->{state} = !$self->{state};
1891 $self->_emit (changed => $self->{state}); 1956 $self->_emit (changed => $self->{state});
1957 } else {
1958 return 0
1959 }
1960
1892 } 1961 1
1893} 1962}
1894 1963
1895sub _draw { 1964sub _draw {
1896 my ($self) = @_; 1965 my ($self) = @_;
1897 1966
2235 2304
2236 $self->SUPER::button_down ($ev, $x, $y); 2305 $self->SUPER::button_down ($ev, $x, $y);
2237 2306
2238 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2307 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2239 2308
2240 $self->mouse_motion ($ev, $x, $y); 2309 $self->mouse_motion ($ev, $x, $y)
2241} 2310}
2242 2311
2243sub mouse_motion { 2312sub mouse_motion {
2244 my ($self, $ev, $x, $y) = @_; 2313 my ($self, $ev, $x, $y) = @_;
2245 2314
2249 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2318 my (undef, $lo, $hi, $page) = @{$self->{range}};
2250 2319
2251 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 2320 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2252 2321
2253 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 2322 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2323 } else {
2324 return 0;
2325 }
2326
2254 } 2327 1
2255} 2328}
2256 2329
2257sub update { 2330sub update {
2258 my ($self) = @_; 2331 my ($self) = @_;
2259 2332
2781 $self->SUPER::DESTROY; 2854 $self->SUPER::DESTROY;
2782} 2855}
2783 2856
2784############################################################################# 2857#############################################################################
2785 2858
2786package CFClient::UI::Inventory;
2787
2788our @ISA = CFClient::UI::ScrolledWindow::;
2789
2790sub new {
2791 my $class = shift;
2792
2793 my $self = $class->SUPER::new (
2794 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2795 @_,
2796 );
2797
2798 $self
2799}
2800
2801sub set_items {
2802 my ($self, $items) = @_;
2803
2804 $self->{scrolled}->clear;
2805 return unless $items;
2806
2807 my @items = sort {
2808 ($a->{type} <=> $b->{type})
2809 or ($a->{name} cmp $b->{name})
2810 } @$items;
2811
2812 $self->{real_items} = \@items;
2813
2814 my $row = 0;
2815 for my $item (@items) {
2816 CFClient::Item::update_widgets $item;
2817
2818 $self->{scrolled}->add (0, $row, $item->{face_widget});
2819 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2820 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2821
2822 $row++;
2823 }
2824}
2825
2826#############################################################################
2827
2828package CFClient::UI::Menu; 2859package CFClient::UI::Menu;
2829 2860
2830our @ISA = CFClient::UI::FancyFrame::; 2861our @ISA = CFClient::UI::FancyFrame::;
2831 2862
2832use CFClient::OpenGL; 2863use CFClient::OpenGL;
2879 my ($self, $ev, $x, $y) = @_; 2910 my ($self, $ev, $x, $y) = @_;
2880 2911
2881 # TODO: should use vbox->find_widget or so 2912 # TODO: should use vbox->find_widget or so
2882 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2913 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2883 $self->{hover} = $self->{item}{$HOVER}; 2914 $self->{hover} = $self->{item}{$HOVER};
2915
2916 0
2884} 2917}
2885 2918
2886sub button_up { 2919sub button_up {
2887 my ($self, $ev, $x, $y) = @_; 2920 my ($self, $ev, $x, $y) = @_;
2888 2921
2890 undef $GRAB; 2923 undef $GRAB;
2891 $self->hide; 2924 $self->hide;
2892 2925
2893 $self->_emit ("popdown"); 2926 $self->_emit ("popdown");
2894 $self->{hover}[1]->() if $self->{hover}; 2927 $self->{hover}[1]->() if $self->{hover};
2928 } else {
2929 return 0
2930 }
2931
2895 } 2932 1
2896} 2933}
2897 2934
2898############################################################################# 2935#############################################################################
2899 2936
2900package CFClient::UI::Statusbox; 2937package CFClient::UI::Statusbox;
3005 $self->SUPER::reconfigure; 3042 $self->SUPER::reconfigure;
3006} 3043}
3007 3044
3008############################################################################# 3045#############################################################################
3009 3046
3010package CFClient::UI::Root; 3047package CFClient::UI::Inventory;
3011 3048
3012our @ISA = CFClient::UI::Container::; 3049our @ISA = CFClient::UI::ScrolledWindow::;
3013
3014use CFClient::OpenGL;
3015 3050
3016sub new { 3051sub new {
3017 my $class = shift; 3052 my $class = shift;
3018 3053
3019 my $self = $class->SUPER::new ( 3054 my $self = $class->SUPER::new (
3020 visible => 1, 3055 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
3021 @_, 3056 @_,
3022 ); 3057 );
3023 3058
3024 Scalar::Util::weaken ($self->{root} = $self);
3025
3026 $self 3059 $self
3027} 3060}
3028 3061
3029sub size_request { 3062sub set_items {
3030 my ($self) = @_;
3031
3032 ($self->{w}, $self->{h})
3033}
3034
3035sub _to_pixel {
3036 my ($coord, $size, $max) = @_;
3037
3038 $coord =
3039 $coord eq "center" ? ($max - $size) * 0.5
3040 : $coord eq "max" ? $max
3041 : $coord;
3042
3043 $coord = 0 if $coord < 0;
3044 $coord = $max - $size if $coord > $max - $size;
3045
3046 int $coord + 0.5
3047}
3048
3049sub size_allocate {
3050 my ($self, $w, $h) = @_;
3051
3052 for my $child ($self->children) {
3053 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3054
3055 $X = $child->{force_x} if exists $child->{force_x};
3056 $Y = $child->{force_y} if exists $child->{force_y};
3057
3058 $X = _to_pixel $X, $W, $self->{w};
3059 $Y = _to_pixel $Y, $H, $self->{h};
3060
3061 $child->configure ($X, $Y, $W, $H);
3062 }
3063}
3064
3065sub coord2local {
3066 my ($self, $x, $y) = @_;
3067
3068 ($x, $y)
3069}
3070
3071sub coord2global {
3072 my ($self, $x, $y) = @_;
3073
3074 ($x, $y)
3075}
3076
3077sub update {
3078 my ($self) = @_;
3079
3080 $::WANT_REFRESH++;
3081}
3082
3083sub add {
3084 my ($self, @children) = @_;
3085
3086 $_->{is_toplevel} = 1
3087 for @children;
3088
3089 $self->SUPER::add (@children);
3090}
3091
3092sub remove {
3093 my ($self, @children) = @_;
3094
3095 $self->SUPER::remove (@children);
3096
3097 delete $self->{is_toplevel}
3098 for @children;
3099
3100 while (@children) {
3101 my $w = pop @children;
3102 push @children, $w->children;
3103 $w->set_invisible;
3104 }
3105}
3106
3107sub on_refresh {
3108 my ($self, $id, $cb) = @_;
3109
3110 $self->{refresh_hook}{$id} = $cb;
3111}
3112
3113sub on_post_alloc {
3114 my ($self, $id, $cb) = @_;
3115
3116 $self->{post_alloc_hook}{$id} = $cb;
3117}
3118
3119sub draw {
3120 my ($self) = @_;
3121
3122 while ($self->{refresh_hook}) {
3123 $_->()
3124 for values %{delete $self->{refresh_hook}};
3125 }
3126
3127 if ($self->{realloc}) {
3128 my @queue;
3129
3130 while () {
3131 if ($self->{realloc}) {
3132 #TODO use array-of-depth approach
3133
3134 use sort 'stable';
3135
3136 @queue = sort { $a->{visible} <=> $b->{visible} }
3137 @queue, values %{delete $self->{realloc}};
3138 }
3139
3140 my $widget = pop @queue || last;
3141
3142 $widget->{visible} or last; # do not resize invisible widgets
3143
3144 my ($w, $h) = $widget->size_request;
3145
3146 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3147 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3148
3149 $w = $widget->{force_w} if exists $widget->{force_w};
3150 $h = $widget->{force_h} if exists $widget->{force_h};
3151
3152 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3153 || delete $widget->{force_realloc}) {
3154 $widget->{req_w} = $w;
3155 $widget->{req_h} = $h;
3156
3157 $self->{size_alloc}{$widget+0} = $widget;
3158
3159 if (my $parent = $widget->{parent}) {
3160 $self->{realloc}{$parent+0} = $parent;
3161 #unshift @queue, $parent;
3162 $parent->{force_size_alloc} = 1;
3163 $self->{size_alloc}{$parent+0} = $parent;
3164 }
3165 }
3166
3167 delete $self->{realloc}{$widget+0};
3168 }
3169 }
3170
3171 while (my $size_alloc = delete $self->{size_alloc}) {
3172 my @queue = sort { $b->{visible} <=> $a->{visible} }
3173 values %$size_alloc;
3174
3175 while () {
3176 my $widget = pop @queue || last;
3177
3178 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3179
3180 $w = 0 if $w < 0;
3181 $h = 0 if $h < 0;
3182
3183 $w = int $w + 0.5;
3184 $h = int $h + 0.5;
3185
3186 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3187 $widget->{w} = $w;
3188 $widget->{h} = $h;
3189
3190 $widget->emit (size_allocate => $w, $h);
3191 }
3192 }
3193 }
3194
3195 while ($self->{post_alloc_hook}) {
3196 $_->()
3197 for values %{delete $self->{post_alloc_hook}};
3198 }
3199
3200
3201 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3202 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3203 glClear GL_COLOR_BUFFER_BIT;
3204
3205 glMatrixMode GL_PROJECTION;
3206 glLoadIdentity;
3207 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3208 glMatrixMode GL_MODELVIEW;
3209 glLoadIdentity;
3210
3211 $self->_draw;
3212}
3213
3214#############################################################################
3215
3216package CFClient::UI::SpellList;
3217
3218our @ISA = CFClient::UI::FancyFrame::;
3219
3220sub new {
3221 my $class = shift;
3222
3223 my $self = $class->SUPER::new (binding => [], commands => [], @_);
3224
3225 $self->add (new CFClient::UI::ScrolledWindow
3226 scrolled => $self->{spellbox} = new CFClient::UI::Table);
3227
3228 $self;
3229}
3230
3231# XXX: Do sorting? Argl...
3232sub add_spell {
3233 my ($self, $spell) = @_; 3063 my ($self, $items) = @_;
3234 $self->{spells}->{$spell->{name}} = $spell;
3235 3064
3236 $self->{spellbox}->add (0, $self->{tbl_idx}, new CFClient::UI::Face 3065 $self->{scrolled}->clear;
3237 face => $spell->{face}, 3066 return unless $items;
3238 can_hover => 1,
3239 can_events => 1,
3240 tooltip => $spell->{message});
3241 3067
3242 $self->{spellbox}->add (1, $self->{tbl_idx}, new CFClient::UI::Label 3068 my @items = sort {
3243 text => $spell->{name}, 3069 ($a->{type} <=> $b->{type})
3244 can_hover => 1, 3070 or ($a->{name} cmp $b->{name})
3245 can_events => 1, 3071 } @$items;
3246 tooltip => $spell->{message},
3247 expand => 1);
3248 3072
3249 $self->{spellbox}->add (2, $self->{tbl_idx}, new CFClient::UI::Label 3073 $self->{real_items} = \@items;
3250 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3251 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3252 expand => 1);
3253 3074
3254 $self->{spellbox}->add (3, $self->{tbl_idx}++, new CFClient::UI::Button 3075 my $row = 0;
3255 text => "bind to key", 3076 for my $item (@items) {
3256 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }); 3077 CFClient::Item::update_widgets $item;
3257}
3258 3078
3259sub rebuild_spell_list { 3079 $self->{scrolled}->add (0, $row, $item->{face_widget});
3260 my ($self) = @_; 3080 $self->{scrolled}->add (1, $row, $item->{desc_widget});
3261 $self->{tbl_idx} = 0; 3081 $self->{scrolled}->add (2, $row, $item->{weight_widget});
3262 $self->add_spell ($_) for values %{$self->{spells}};
3263}
3264 3082
3265sub remove_spell { 3083 $row++;
3266 my ($self, $spell) = @_; 3084 }
3267 delete $self->{spells}->{$spell->{name}};
3268 $self->rebuild_spell_list;
3269} 3085}
3270 3086
3271############################################################################# 3087#############################################################################
3272 3088
3273package CFClient::UI::BindEditor; 3089package CFClient::UI::BindEditor;
3368 $rec = $::CONN->stop_record if $::CONN; 3184 $rec = $::CONN->stop_record if $::CONN;
3369 return unless ref $rec eq 'ARRAY'; 3185 return unless ref $rec eq 'ARRAY';
3370 $self->set_command_list ($rec); 3186 $self->set_command_list ($rec);
3371} 3187}
3372 3188
3373# if $commit is true, the binding will be set after the user entered a key combo 3189
3190sub ask_for_bind_and_commit {
3191 my ($self) = @_;
3192 $self->ask_for_bind (1);
3193}
3194
3374sub ask_for_bind { 3195sub ask_for_bind {
3375 my ($self, $commit) = @_; 3196 my ($self, $commit) = @_;
3376 3197
3377 CFClient::Binder::open_binding_dialog (sub { 3198 CFClient::Binder::open_binding_dialog (sub {
3378 my ($mod, $sym) = @_; 3199 my ($mod, $sym) = @_;
3460 } 3281 }
3461} 3282}
3462 3283
3463############################################################################# 3284#############################################################################
3464 3285
3286package CFClient::UI::SpellList;
3287
3288our @ISA = CFClient::UI::FancyFrame::;
3289
3290sub new {
3291 my $class = shift;
3292
3293 my $self = $class->SUPER::new (binding => [], commands => [], @_);
3294
3295 $self->add (new CFClient::UI::ScrolledWindow
3296 scrolled => $self->{spellbox} = new CFClient::UI::Table);
3297
3298 $self;
3299}
3300
3301# XXX: Do sorting? Argl...
3302sub add_spell {
3303 my ($self, $spell) = @_;
3304 $self->{spells}->{$spell->{name}} = $spell;
3305
3306 $self->{spellbox}->add (0, $self->{tbl_idx}, new CFClient::UI::Face
3307 face => $spell->{face},
3308 can_hover => 1,
3309 can_events => 1,
3310 tooltip => $spell->{message});
3311
3312 $self->{spellbox}->add (1, $self->{tbl_idx}, new CFClient::UI::Label
3313 text => $spell->{name},
3314 can_hover => 1,
3315 can_events => 1,
3316 tooltip => $spell->{message},
3317 expand => 1);
3318
3319 $self->{spellbox}->add (2, $self->{tbl_idx}, new CFClient::UI::Label
3320 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3321 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3322 expand => 1);
3323
3324 $self->{spellbox}->add (3, $self->{tbl_idx}++, new CFClient::UI::Button
3325 text => "bind to key",
3326 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) });
3327}
3328
3329sub rebuild_spell_list {
3330 my ($self) = @_;
3331 $self->{tbl_idx} = 0;
3332 $self->add_spell ($_) for values %{$self->{spells}};
3333}
3334
3335sub remove_spell {
3336 my ($self, $spell) = @_;
3337 delete $self->{spells}->{$spell->{name}};
3338 $self->rebuild_spell_list;
3339}
3340
3341#############################################################################
3342
3343package CFClient::UI::Root;
3344
3345our @ISA = CFClient::UI::Container::;
3346
3347use CFClient::OpenGL;
3348
3349sub new {
3350 my $class = shift;
3351
3352 my $self = $class->SUPER::new (
3353 visible => 1,
3354 @_,
3355 );
3356
3357 Scalar::Util::weaken ($self->{root} = $self);
3358
3359 $self
3360}
3361
3362sub size_request {
3363 my ($self) = @_;
3364
3365 ($self->{w}, $self->{h})
3366}
3367
3368sub _to_pixel {
3369 my ($coord, $size, $max) = @_;
3370
3371 $coord =
3372 $coord eq "center" ? ($max - $size) * 0.5
3373 : $coord eq "max" ? $max
3374 : $coord;
3375
3376 $coord = 0 if $coord < 0;
3377 $coord = $max - $size if $coord > $max - $size;
3378
3379 int $coord + 0.5
3380}
3381
3382sub size_allocate {
3383 my ($self, $w, $h) = @_;
3384
3385 for my $child ($self->children) {
3386 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3387
3388 $X = $child->{force_x} if exists $child->{force_x};
3389 $Y = $child->{force_y} if exists $child->{force_y};
3390
3391 $X = _to_pixel $X, $W, $self->{w};
3392 $Y = _to_pixel $Y, $H, $self->{h};
3393
3394 $child->configure ($X, $Y, $W, $H);
3395 }
3396}
3397
3398sub coord2local {
3399 my ($self, $x, $y) = @_;
3400
3401 ($x, $y)
3402}
3403
3404sub coord2global {
3405 my ($self, $x, $y) = @_;
3406
3407 ($x, $y)
3408}
3409
3410sub update {
3411 my ($self) = @_;
3412
3413 $::WANT_REFRESH++;
3414}
3415
3416sub add {
3417 my ($self, @children) = @_;
3418
3419 $_->{is_toplevel} = 1
3420 for @children;
3421
3422 $self->SUPER::add (@children);
3423}
3424
3425sub remove {
3426 my ($self, @children) = @_;
3427
3428 $self->SUPER::remove (@children);
3429
3430 delete $self->{is_toplevel}
3431 for @children;
3432
3433 while (@children) {
3434 my $w = pop @children;
3435 push @children, $w->children;
3436 $w->set_invisible;
3437 }
3438}
3439
3440sub on_refresh {
3441 my ($self, $id, $cb) = @_;
3442
3443 $self->{refresh_hook}{$id} = $cb;
3444}
3445
3446sub on_post_alloc {
3447 my ($self, $id, $cb) = @_;
3448
3449 $self->{post_alloc_hook}{$id} = $cb;
3450}
3451
3452sub draw {
3453 my ($self) = @_;
3454
3455 while ($self->{refresh_hook}) {
3456 $_->()
3457 for values %{delete $self->{refresh_hook}};
3458 }
3459
3460 if ($self->{realloc}) {
3461 my %queue;
3462 my @queue;
3463 my $widget;
3464
3465 outer:
3466 while () {
3467 if (my $realloc = delete $self->{realloc}) {
3468 for $widget (values %$realloc) {
3469 $widget->{visible} or next; # do not resize invisible widgets
3470
3471 $queue{$widget+0}++ and next; # duplicates are common
3472
3473 push @{ $queue[$widget->{visible}] }, $widget;
3474 }
3475 }
3476
3477 while () {
3478 @queue or last outer;
3479
3480 $widget = pop @{ $queue[-1] || [] }
3481 and last;
3482
3483 pop @queue;
3484 }
3485
3486 delete $queue{$widget+0};
3487
3488 my ($w, $h) = $widget->size_request;
3489
3490 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3491 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3492
3493 $w = $widget->{force_w} if exists $widget->{force_w};
3494 $h = $widget->{force_h} if exists $widget->{force_h};
3495
3496 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3497 || delete $widget->{force_realloc}) {
3498 $widget->{req_w} = $w;
3499 $widget->{req_h} = $h;
3500
3501 $self->{size_alloc}{$widget+0} = $widget;
3502
3503 if (my $parent = $widget->{parent}) {
3504 $self->{realloc}{$parent+0} = $parent
3505 unless $queue{$parent+0};
3506
3507 $parent->{force_size_alloc} = 1;
3508 $self->{size_alloc}{$parent+0} = $parent;
3509 }
3510 }
3511
3512 delete $self->{realloc}{$widget+0};
3513 }
3514 }
3515
3516 while (my $size_alloc = delete $self->{size_alloc}) {
3517 my @queue = sort { $b->{visible} <=> $a->{visible} }
3518 values %$size_alloc;
3519
3520 while () {
3521 my $widget = pop @queue || last;
3522
3523 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3524
3525 $w = 0 if $w < 0;
3526 $h = 0 if $h < 0;
3527
3528 $w = int $w + 0.5;
3529 $h = int $h + 0.5;
3530
3531 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3532 $widget->{old_w} = $widget->{w};
3533 $widget->{old_h} = $widget->{h};
3534
3535 $widget->{w} = $w;
3536 $widget->{h} = $h;
3537
3538 $widget->emit (size_allocate => $w, $h);
3539 }
3540 }
3541 }
3542
3543 while ($self->{post_alloc_hook}) {
3544 $_->()
3545 for values %{delete $self->{post_alloc_hook}};
3546 }
3547
3548
3549 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3550 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3551 glClear GL_COLOR_BUFFER_BIT;
3552
3553 glMatrixMode GL_PROJECTION;
3554 glLoadIdentity;
3555 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3556 glMatrixMode GL_MODELVIEW;
3557 glLoadIdentity;
3558
3559 {
3560 package CFClient::UI::Base;
3561
3562 ($draw_x, $draw_y, $draw_w, $draw_h) =
3563 (0, 0, $self->{w}, $self->{h});
3564 }
3565
3566 $self->_draw;
3567}
3568
3569#############################################################################
3570
3465package CFClient::UI; 3571package CFClient::UI;
3466 3572
3467$ROOT = new CFClient::UI::Root; 3573$ROOT = new CFClient::UI::Root;
3468$TOOLTIP = new CFClient::UI::Tooltip z => 900; 3574$TOOLTIP = new CFClient::UI::Tooltip z => 900;
3469 3575

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines