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.172 by root, Tue Apr 25 09:52:04 2006 UTC vs.
Revision 1.179 by elmex, Fri Apr 28 05:17:21 2006 UTC

24 my $tip = $widget->{tooltip}; 24 my $tip = $widget->{tooltip};
25 25
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 27
28 $TOOLTIP->set_markup ($widget->{tooltip}); 28 $TOOLTIP->set_markup ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); 29
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 }
37
38 $TOOLTIP->move ($x, $y);
31 } 39 }
32 40
33 return; 41 return;
34 } 42 }
35 } 43 }
237sub size_allocate { 245sub size_allocate {
238 # nothing to be done 246 # nothing to be done
239} 247}
240 248
241sub children { 249sub children {
250}
251
252# call when resoltuion changes etc.
253sub reconfigure {
254 my ($self) = @_;
255
256 $_->reconfigure
257 for $self->children;
258
259 $_->check_size;
242} 260}
243 261
244sub set_max_size { 262sub set_max_size {
245 my ($self, $w, $h) = @_; 263 my ($self, $w, $h) = @_;
246 264
633} 651}
634 652
635sub update { 653sub update {
636 my ($self) = @_; 654 my ($self) = @_;
637 655
638 # we want to do this delayed... 656 $ROOT->on_refresh ($self => sub { $self->render_child });
639 $self->render_chld;
640 $self->SUPER::update; 657 $self->SUPER::update;
641} 658}
642 659
660sub size_allocate {
661 my ($self, $w, $h) = @_;
662
663 $self->SUPER::size_allocate ($w, $h);
664 $self->update;
665}
666
643sub render_chld { 667sub render_child {
644 my ($self) = @_; 668 my ($self) = @_;
645 669
646 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 670 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
647 glClearColor 0, 0, 0, 1; 671 glClearColor 0, 0, 0, 0;
648 glClear GL_COLOR_BUFFER_BIT; 672 glClear GL_COLOR_BUFFER_BIT;
649 $self->child->draw; 673 $self->child->draw;
674# glColorMask 1, 1, 1, 0;
675# glEnable GL_BLEND;
676# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
677# glRasterPos 0, 0;
678# glCopyPixels 0, 0, $self->{w}, $self->{h};
679# glDisable GL_BLEND;
680# glColorMask 1, 1, 1, 1;
650 }; 681 };
651}
652
653sub size_allocate {
654 my ($self, $w, $h) = @_;
655
656 $self->child->configure (0, 0, $w, $h);
657
658 $self->render_chld;
659} 682}
660 683
661sub _draw { 684sub _draw {
662 my ($self) = @_; 685 my ($self) = @_;
663 686
665 688
666 my $tex = $self->{texture} 689 my $tex = $self->{texture}
667 or return; 690 or return;
668 691
669 glEnable GL_BLEND; 692 glEnable GL_BLEND;
670 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 693 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
671 glEnable GL_TEXTURE_2D; 694 glEnable GL_TEXTURE_2D;
672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
696 glColor 0, 0, 0, 1;
673 697
674 $tex->draw_quad (0, 0, $w, $h); 698 $tex->draw_quad (0, 0, $w, $h);
675 699
676 glDisable GL_BLEND; 700 glDisable GL_BLEND;
677 glDisable GL_TEXTURE_2D; 701 glDisable GL_TEXTURE_2D;
764 # TODO: user_x, user_y, overwrite moveto? 788 # TODO: user_x, user_y, overwrite moveto?
765 789
766 my $self = $class->SUPER::new ( 790 my $self = $class->SUPER::new (
767 bg => [1, 1, 1, 1], 791 bg => [1, 1, 1, 1],
768 border_bg => [1, 1, 1, 1], 792 border_bg => [1, 1, 1, 1],
769 border => 0.8, 793 border => 0.6,
770 can_events => 1, 794 can_events => 1,
771 @_ 795 @_
772 ); 796 );
773 797
774 $self->{title} &&= new CFClient::UI::Label 798 $self->{title} &&= new CFClient::UI::Label
775 align => 0, 799 align => 0,
776 valign => 1, 800 valign => 1,
777 text => $self->{title}, 801 text => $self->{title},
778 fontsize => 1; 802 fontsize => $self->{border};
779 803
780 $self 804 $self
781} 805}
782 806
783sub border { 807sub border {
808} 832}
809 833
810sub button_down { 834sub button_down {
811 my ($self, $ev, $x, $y) = @_; 835 my ($self, $ev, $x, $y) = @_;
812 836
837 my ($w, $h) = @$self{qw(w h)};
813 my $border = $self->border; 838 my $border = $self->border;
814 839
815 if ($x < $self->{w} && $x >= $self->{w} - $border 840 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
816 && $y < $self->{h} && $y >= $self->{h} - $border) { 841 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
817 842
843 if ($lr & $td) {
844 my ($wx, $wy) = ($self->{x}, $self->{y});
818 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 845 my ($ox, $oy) = ($ev->{x}, $ev->{y});
819 my ($bw, $bh) = ($self->{w}, $self->{h}); 846 my ($bw, $bh) = ($self->{w}, $self->{h});
820 847
848 my $mx = $x < $border;
849 my $my = $y < $border;
850
821 $self->{motion} = sub { 851 $self->{motion} = sub {
822 my ($ev, $x, $y) = @_; 852 my ($ev, $x, $y) = @_;
823 853
824 ($x, $y) = ($ev->{x}, $ev->{y}); 854 my $dx = $ev->{x} - $ox;
855 my $dy = $ev->{y} - $oy;
825 856
826 $self->{user_w} = $bw + $x - $ox; 857 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
827 $self->{user_h} = $bh + $y - $oy; 858 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
859 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
828 $self->check_size; 860 $self->check_size;
829 }; 861 };
830 862
831 } elsif ($x >= 0 && $x < $self->{w} 863 } elsif ($lr ^ $td) {
832 && $y >= 0 && $y < $border) {
833
834 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 864 my ($ox, $oy) = ($ev->{x}, $ev->{y});
835 my ($bx, $by) = ($self->{x}, $self->{y}); 865 my ($bx, $by) = ($self->{x}, $self->{y});
836 866
837 $self->{motion} = sub { 867 $self->{motion} = sub {
838 my ($ev, $x, $y) = @_; 868 my ($ev, $x, $y) = @_;
874 $tex[1]->draw_quad (0, 0, $w, $border); 904 $tex[1]->draw_quad (0, 0, $w, $border);
875 $tex[3]->draw_quad (0, $border, $border, $ch); 905 $tex[3]->draw_quad (0, $border, $border, $ch);
876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 906 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
877 $tex[4]->draw_quad (0, $h - $border, $w, $border); 907 $tex[4]->draw_quad (0, $h - $border, $w, $border);
878 908
909 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
879 my $bg = $tex[0]; 910 my $bg = $tex[0];
880 911
881 # TODO: repeat texture not scale 912 # TODO: repeat texture not scale
882 my $rep_x = $cw / $bg->{w}; 913 my $rep_x = $cw / $bg->{w};
883 my $rep_y = $ch / $bg->{h}; 914 my $rep_y = $ch / $bg->{h};
884 915
885 glColor @{ $self->{bg} }; 916 glColor @{ $self->{bg} };
886 917
887 $bg->{s} = $rep_x; 918 $bg->{s} = $rep_x;
888 $bg->{t} = $rep_y; 919 $bg->{t} = $rep_y;
889 $bg->{wrap_mode} = 1; 920 $bg->{wrap_mode} = 1;
890 $bg->draw_quad ($border, $border, $cw, $ch); 921 $bg->draw_quad ($border, $border, $cw, $ch);
891 922
892 glDisable GL_TEXTURE_2D; 923 glDisable GL_TEXTURE_2D;
893 glDisable GL_BLEND; 924 glDisable GL_BLEND;
925 }
894 926
895 $self->{title}->draw if $self->{title}; 927 $self->{title}->draw if $self->{title};
928
896 $self->child->draw; 929 $self->child->draw;
897} 930}
898 931
899############################################################################# 932#############################################################################
900 933
1204 s/</&lt;/g; 1237 s/</&lt;/g;
1205 1238
1206 $_[1] 1239 $_[1]
1207} 1240}
1208 1241
1242sub update {
1243 my ($self) = @_;
1244
1245 delete $self->{texture};
1246 $self->SUPER::update;
1247}
1248
1249sub reconfigure {
1250 my ($self) = @_;
1251
1252 delete $self->{texture};
1253}
1254
1209sub set_text { 1255sub set_text {
1210 my ($self, $text) = @_; 1256 my ($self, $text) = @_;
1211 1257
1258 return if $self->{text} eq "T$text";
1259 $self->{text} = "T$text";
1260
1212 $self->{layout}->set_text ($text); 1261 $self->{layout}->set_text ($text);
1213 1262
1214 delete $self->{texture}; 1263 delete $self->{texture};
1264 $self->update;
1215 $self->check_size; 1265 $self->check_size;
1216 $self->update;
1217} 1266}
1218 1267
1219sub set_markup { 1268sub set_markup {
1220 my ($self, $markup) = @_; 1269 my ($self, $markup) = @_;
1221 1270
1271 return if $self->{text} eq "M$markup";
1272 $self->{text} = "M$markup";
1273
1222 $self->{layout}->set_markup ($markup); 1274 $self->{layout}->set_markup ($markup);
1223 1275
1224 delete $self->{texture}; 1276 delete $self->{texture};
1277 $self->update;
1225 $self->check_size; 1278 $self->check_size;
1226 $self->update;
1227} 1279}
1228 1280
1229sub size_request { 1281sub size_request {
1230 my ($self) = @_; 1282 my ($self) = @_;
1231 1283
1755} 1807}
1756 1808
1757sub set_max { 1809sub set_max {
1758 my ($self, $max) = @_; 1810 my ($self, $max) = @_;
1759 1811
1812 return if $self->{max_val} == $max;
1813
1760 $self->{max_val} = $max; 1814 $self->{max_val} = $max;
1815 $self->update;
1761} 1816}
1762 1817
1763sub set_value { 1818sub set_value {
1764 my ($self, $val, $max) = @_; 1819 my ($self, $val, $max) = @_;
1765 1820
1766 $self->set_max ($max) 1821 $self->set_max ($max)
1767 if defined $max; 1822 if defined $max;
1768 1823
1769 $max = $self->{max_val}; 1824 return if $self->{val} == $val;
1825
1770 $self->{val} = $val; 1826 $self->{val} = $val;
1771
1772 $self->update; 1827 $self->update;
1773} 1828}
1774 1829
1775sub _draw { 1830sub _draw {
1776 my ($self) = @_; 1831 my ($self) = @_;
1859 1914
1860 $self->{value}->set_fontsize ($fsize); 1915 $self->{value}->set_fontsize ($fsize);
1861 $self->{max} ->set_fontsize ($fsize); 1916 $self->{max} ->set_fontsize ($fsize);
1862} 1917}
1863 1918
1919sub set_max {
1920 my ($self, $max) = @_;
1921
1922 $self->{gauge}->set_max ($max);
1923 $self->{max}->set_text ($max);
1924}
1925
1864sub set_value { 1926sub set_value {
1865 my ($self, $val, $max) = @_; 1927 my ($self, $val, $max) = @_;
1866 1928
1867 $self->set_max ($max) 1929 $self->set_max ($max)
1868 if defined $max; 1930 if defined $max;
1869 1931
1870 $self->{gauge}->set_value ($val, $max); 1932 $self->{gauge}->set_value ($val, $max);
1871 $self->{value}->set_text ($val); 1933 $self->{value}->set_text ($val);
1872}
1873
1874sub set_max {
1875 my ($self, $max) = @_;
1876
1877 $self->{gauge}->set_max ($max);
1878 $self->{max}->set_text ($max);
1879} 1934}
1880 1935
1881############################################################################# 1936#############################################################################
1882 1937
1883package CFClient::UI::Slider; 1938package CFClient::UI::Slider;
2037 (new CFClient::UI::Empty expand => 1), 2092 (new CFClient::UI::Empty expand => 1),
2038 (new CFClient::UI::Slider vertical => 1), 2093 (new CFClient::UI::Slider vertical => 1),
2039 ], 2094 ],
2040 ); 2095 );
2041 2096
2042 $self->{children}[1]->connect (changed => sub { 2097 $self->{children}[1]->connect (changed => sub { $self->update });
2043 $self->update;
2044 });
2045 2098
2046 $self 2099 $self
2047} 2100}
2048 2101
2049sub set_fontsize { 2102sub set_fontsize {
2057 my ($self, $text) = @_; 2110 my ($self, $text) = @_;
2058 2111
2059 my $layout = $self->{layout}; 2112 my $layout = $self->{layout};
2060 2113
2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2114 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2062 $layout->set_width ($self->{w}); 2115 $layout->set_width ($self->{children}[0]{w});
2063 $layout->set_text ($text); 2116 $layout->set_text ($text);
2064 2117
2065 ($layout->size)[1] 2118 ($layout->size)[1]
2066} 2119}
2067 2120
2120 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2173 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2121 2174
2122 delete $self->{texture}; 2175 delete $self->{texture};
2123 } 2176 }
2124 2177
2125 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2178 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2126 glClearColor 0, 0, 0, 1; 2179 glClearColor 0, 0, 0, 0;
2127 glClear GL_COLOR_BUFFER_BIT; 2180 glClear GL_COLOR_BUFFER_BIT;
2128 2181
2129 glEnable GL_BLEND; 2182 glEnable GL_BLEND;
2130 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2183 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2131 glEnable GL_TEXTURE_2D; 2184 glEnable GL_TEXTURE_2D;
2163} 2216}
2164 2217
2165sub _draw { 2218sub _draw {
2166 my ($self) = @_; 2219 my ($self) = @_;
2167 2220
2168 if ($self->{texture}) { 2221 glEnable GL_BLEND;
2222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2169 glEnable GL_TEXTURE_2D; 2223 glEnable GL_TEXTURE_2D;
2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2224 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2171 glColor 1, 1, 1, 1; 2225 glColor 1, 1, 1, 1;
2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2226 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2173 glDisable GL_TEXTURE_2D; 2227 glDisable GL_TEXTURE_2D;
2174 } 2228 glDisable GL_BLEND;
2175 2229
2176 $self->{children}[1]->draw; 2230 $self->{children}[1]->draw;
2177 2231
2178} 2232}
2179 2233
2348 2402
2349sub size_request { 2403sub size_request {
2350 (32, 8) 2404 (32, 8)
2351} 2405}
2352 2406
2353sub draw { 2407sub _draw {
2354 my ($self) = @_; 2408 my ($self) = @_;
2355 2409
2356 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2357 2411
2412 # TODO animation
2358 if ($tex) { 2413 if ($tex) {
2359 glEnable GL_BLEND; 2414 glEnable GL_BLEND;
2360 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2415 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2361 glEnable GL_TEXTURE_2D; 2416 glEnable GL_TEXTURE_2D;
2362 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2367 } 2422 }
2368} 2423}
2369 2424
2370############################################################################# 2425#############################################################################
2371 2426
2427package CFClient::UI::Menu;
2428
2429our @ISA = CFClient::UI::FancyFrame::;
2430
2431use CFClient::OpenGL;
2432
2433sub new {
2434 my $class = shift;
2435
2436 my $self = $class->SUPER::new (
2437 items => [],
2438 z => 100,
2439 @_,
2440 );
2441
2442 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2443
2444 for my $item (@{ $self->{items} }) {
2445 my ($widget, $cb) = @$item;
2446
2447 # handle various types of items, only text for now
2448 if (!ref $widget) {
2449 $widget = new CFClient::UI::Label
2450 can_hover => 1,
2451 can_events => 1,
2452 text => $widget;
2453 }
2454
2455 $self->{item}{$widget} = $item;
2456
2457 $self->{vbox}->add ($widget);
2458 }
2459
2460 $self
2461}
2462
2463# popup given the event (must be a mouse button down event currently)
2464sub popup {
2465 my ($self, $ev) = @_;
2466
2467 $self->emit ("popdown");
2468
2469 # maybe save $GRAB? must be careful about events...
2470 $GRAB = $self;
2471 $self->{button} = $ev->{button};
2472
2473 $self->show;
2474 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2475}
2476
2477sub mouse_motion {
2478 my ($self, $ev, $x, $y) = @_;
2479
2480 # TODO: should use vbox->fdind_widget or so
2481 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2482 $self->{hover} = $self->{item}{$HOVER};
2483}
2484
2485sub button_up {
2486 my ($self, $ev, $x, $y) = @_;
2487
2488 if ($ev->{button} == $self->{button}) {
2489 undef $GRAB;
2490 $self->hide;
2491
2492 $self->emit ("popdown");
2493 $self->{hover}[1]->() if $self->{hover};
2494 }
2495}
2496
2497#############################################################################
2498
2372package CFClient::UI::Root; 2499package CFClient::UI::Root;
2373 2500
2374our @ISA = CFClient::UI::Container::; 2501our @ISA = CFClient::UI::Container::;
2375 2502
2376use CFClient::OpenGL; 2503use CFClient::OpenGL;
2448 $self->_draw; 2575 $self->_draw;
2449} 2576}
2450 2577
2451############################################################################# 2578#############################################################################
2452 2579
2580package CFClient::UI::Inventory;
2581
2582our @ISA = CFClient::UI::Container::;
2583
2584use CFClient::OpenGL;
2585
2586sub new {
2587 my $class = shift;
2588
2589 my $self = $class->SUPER::new (@_);
2590
2591 $self
2592}
2593
2594sub size_allocate {
2595 my ($self, $w, $h) = @_;
2596
2597 $self->{w} = $w;
2598 $self->{h} = $h;
2599
2600 $self->check_size;
2601}
2602
2603sub set_items {
2604 my ($self, $items) = @_;
2605 my @items = values %{$items};
2606 @items = sort { $a->{type} <=> $b->{type} } @items;
2607
2608 $self->{real_items} = \@items;
2609
2610 for my $item (@items) {
2611 my $desc = $item->{nrof} < 2
2612 ? $item->{name}
2613 : "$item->{nrof} $item->{name_pl}";
2614
2615 $self->add (my $hb = new CFClient::UI::HBox);
2616
2617 $hb->add (my $f = new CFClient::UI::Face
2618 can_events => 0,
2619 face => $item->{face},
2620 anim => $item->{anim},
2621 animspeed => $item->{animspeed},
2622 expand => 1
2623 );
2624 $hb->add (new CFClient::UI::Label text => $desc, expand => 1);
2625 }
2626
2627 $self->{max_pos} = (scalar @items) - 1;
2628
2629 my $range = $self->{range};
2630 my $page = $self->{h} / 32; # waht information souce to use for face size?
2631 # it should be configurable i guess...
2632 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2633 $range->update;
2634}
2635
2636sub size_request {
2637 my ($self) = @_;
2638 (100, 200)
2639}
2640
2641sub set_range {
2642 my ($self, $range) = @_;
2643
2644 $self->{range} = $range;
2645 $range->connect (changed => sub { $self->update });
2646
2647 my $page = $self->{h} / 32; # waht information souce to use for face size?
2648 # it should be configurable i guess...
2649
2650 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2651 $range->update;
2652
2653 $self->update;
2654}
2655
2656sub _draw {
2657 my ($self) = @_;
2658
2659 my ($w, $h) = ($self->{w}, $self->{h});
2660
2661 $self->{pos} = int $self->{range}{range}[0];
2662
2663 my $y = 0;
2664 my $cnt = 0;
2665 my $hrem = $self->{h}; # horiz. remaining space
2666
2667 for (my $i = $self->{pos}; $i < @{$self->{children} || []}; $i++) {
2668 my $chld = $self->{children}->[$i];
2669
2670 if ($hrem >= $chld->{h}) {
2671 $chld->configure (0, $y, $chld->{w}, $chld->{h});
2672
2673 $chld->show;
2674 $chld->draw;
2675
2676 $hrem -= $chld->{h};
2677 $y += $chld->{h};
2678 } else {
2679 last
2680 }
2681 }
2682}
2683
2684
2685#############################################################################
2686
2453package CFClient::UI; 2687package CFClient::UI;
2454 2688
2455$ROOT = new CFClient::UI::Root; 2689$ROOT = new CFClient::UI::Root;
2456$TOOLTIP = new CFClient::UI::Tooltip; 2690$TOOLTIP = new CFClient::UI::Tooltip;
2457 2691

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines