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.174 by root, Tue Apr 25 11:18:49 2006 UTC vs.
Revision 1.181 by root, Sat Apr 29 16:17:09 2006 UTC

669 669
670 $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 {
671 glClearColor 0, 0, 0, 0; 671 glClearColor 0, 0, 0, 0;
672 glClear GL_COLOR_BUFFER_BIT; 672 glClear GL_COLOR_BUFFER_BIT;
673 $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;
674 }; 681 };
675} 682}
676 683
677sub _draw { 684sub _draw {
678 my ($self) = @_; 685 my ($self) = @_;
781 # TODO: user_x, user_y, overwrite moveto? 788 # TODO: user_x, user_y, overwrite moveto?
782 789
783 my $self = $class->SUPER::new ( 790 my $self = $class->SUPER::new (
784 bg => [1, 1, 1, 1], 791 bg => [1, 1, 1, 1],
785 border_bg => [1, 1, 1, 1], 792 border_bg => [1, 1, 1, 1],
786 border => 0.8, 793 border => 0.6,
787 can_events => 1, 794 can_events => 1,
788 @_ 795 @_
789 ); 796 );
790 797
791 $self->{title} &&= new CFClient::UI::Label 798 $self->{title} &&= new CFClient::UI::Label
792 align => 0, 799 align => 0,
793 valign => 1, 800 valign => 1,
794 text => $self->{title}, 801 text => $self->{title},
795 fontsize => 1; 802 fontsize => $self->{border};
796 803
797 $self 804 $self
798} 805}
799 806
800sub border { 807sub border {
825} 832}
826 833
827sub button_down { 834sub button_down {
828 my ($self, $ev, $x, $y) = @_; 835 my ($self, $ev, $x, $y) = @_;
829 836
837 my ($w, $h) = @$self{qw(w h)};
830 my $border = $self->border; 838 my $border = $self->border;
831 839
832 if ($x < $self->{w} && $x >= $self->{w} - $border 840 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
833 && $y < $self->{h} && $y >= $self->{h} - $border) { 841 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
834 842
843 if ($lr & $td) {
844 my ($wx, $wy) = ($self->{x}, $self->{y});
835 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 845 my ($ox, $oy) = ($ev->{x}, $ev->{y});
836 my ($bw, $bh) = ($self->{w}, $self->{h}); 846 my ($bw, $bh) = ($self->{w}, $self->{h});
837 847
848 my $mx = $x < $border;
849 my $my = $y < $border;
850
838 $self->{motion} = sub { 851 $self->{motion} = sub {
839 my ($ev, $x, $y) = @_; 852 my ($ev, $x, $y) = @_;
840 853
841 ($x, $y) = ($ev->{x}, $ev->{y}); 854 my $dx = $ev->{x} - $ox;
855 my $dy = $ev->{y} - $oy;
842 856
843 $self->{user_w} = $bw + $x - $ox; 857 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
844 $self->{user_h} = $bh + $y - $oy; 858 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
859 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
845 $self->check_size; 860 $self->check_size;
846 }; 861 };
847 862
848 } elsif ($x >= 0 && $x < $self->{w} 863 } elsif ($lr ^ $td) {
849 && $y >= 0 && $y < $border) {
850
851 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 864 my ($ox, $oy) = ($ev->{x}, $ev->{y});
852 my ($bx, $by) = ($self->{x}, $self->{y}); 865 my ($bx, $by) = ($self->{x}, $self->{y});
853 866
854 $self->{motion} = sub { 867 $self->{motion} = sub {
855 my ($ev, $x, $y) = @_; 868 my ($ev, $x, $y) = @_;
891 $tex[1]->draw_quad (0, 0, $w, $border); 904 $tex[1]->draw_quad (0, 0, $w, $border);
892 $tex[3]->draw_quad (0, $border, $border, $ch); 905 $tex[3]->draw_quad (0, $border, $border, $ch);
893 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 906 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
894 $tex[4]->draw_quad (0, $h - $border, $w, $border); 907 $tex[4]->draw_quad (0, $h - $border, $w, $border);
895 908
909 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
896 my $bg = $tex[0]; 910 my $bg = $tex[0];
897 911
898 # TODO: repeat texture not scale 912 # TODO: repeat texture not scale
899 my $rep_x = $cw / $bg->{w}; 913 my $rep_x = $cw / $bg->{w};
900 my $rep_y = $ch / $bg->{h}; 914 my $rep_y = $ch / $bg->{h};
901 915
902 glColor @{ $self->{bg} }; 916 glColor @{ $self->{bg} };
903 917
904 $bg->{s} = $rep_x; 918 $bg->{s} = $rep_x;
905 $bg->{t} = $rep_y; 919 $bg->{t} = $rep_y;
906 $bg->{wrap_mode} = 1; 920 $bg->{wrap_mode} = 1;
907 $bg->draw_quad ($border, $border, $cw, $ch); 921 $bg->draw_quad ($border, $border, $cw, $ch);
908 922
909 glDisable GL_TEXTURE_2D; 923 glDisable GL_TEXTURE_2D;
910 glDisable GL_BLEND; 924 glDisable GL_BLEND;
925 }
911 926
912 $self->{title}->draw if $self->{title}; 927 $self->{title}->draw if $self->{title};
928
913 $self->child->draw; 929 $self->child->draw;
914} 930}
915 931
916############################################################################# 932#############################################################################
917 933
2076 (new CFClient::UI::Empty expand => 1), 2092 (new CFClient::UI::Empty expand => 1),
2077 (new CFClient::UI::Slider vertical => 1), 2093 (new CFClient::UI::Slider vertical => 1),
2078 ], 2094 ],
2079 ); 2095 );
2080 2096
2081 $self->{children}[1]->connect (changed => sub { 2097 $self->{children}[1]->connect (changed => sub { $self->update });
2082 $self->update;
2083 });
2084 2098
2085 $self 2099 $self
2086} 2100}
2087 2101
2088sub set_fontsize { 2102sub set_fontsize {
2096 my ($self, $text) = @_; 2110 my ($self, $text) = @_;
2097 2111
2098 my $layout = $self->{layout}; 2112 my $layout = $self->{layout};
2099 2113
2100 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2114 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2101 $layout->set_width ($self->{w}); 2115 $layout->set_width ($self->{children}[0]{w});
2102 $layout->set_text ($text); 2116 $layout->set_text ($text);
2103 2117
2104 ($layout->size)[1] 2118 ($layout->size)[1]
2105} 2119}
2106 2120
2159 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2173 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2160 2174
2161 delete $self->{texture}; 2175 delete $self->{texture};
2162 } 2176 }
2163 2177
2164 $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 {
2165 glClearColor 0, 0, 0, 1; 2179 glClearColor 0, 0, 0, 0;
2166 glClear GL_COLOR_BUFFER_BIT; 2180 glClear GL_COLOR_BUFFER_BIT;
2167 2181
2168 glEnable GL_BLEND; 2182 glEnable GL_BLEND;
2169 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2183 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2170 glEnable GL_TEXTURE_2D; 2184 glEnable GL_TEXTURE_2D;
2202} 2216}
2203 2217
2204sub _draw { 2218sub _draw {
2205 my ($self) = @_; 2219 my ($self) = @_;
2206 2220
2207 if ($self->{texture}) { 2221 glEnable GL_BLEND;
2222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2208 glEnable GL_TEXTURE_2D; 2223 glEnable GL_TEXTURE_2D;
2209 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2224 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2210 glColor 1, 1, 1, 1; 2225 glColor 1, 1, 1, 1;
2211 $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});
2212 glDisable GL_TEXTURE_2D; 2227 glDisable GL_TEXTURE_2D;
2213 } 2228 glDisable GL_BLEND;
2214 2229
2215 $self->{children}[1]->draw; 2230 $self->{children}[1]->draw;
2216 2231
2217} 2232}
2218 2233
2387 2402
2388sub size_request { 2403sub size_request {
2389 (32, 8) 2404 (32, 8)
2390} 2405}
2391 2406
2392sub draw { 2407sub _draw {
2393 my ($self) = @_; 2408 my ($self) = @_;
2394 2409
2395 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2396 2411
2412 # TODO animation
2397 if ($tex) { 2413 if ($tex) {
2398 glEnable GL_BLEND; 2414 glEnable GL_BLEND;
2399 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2415 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2400 glEnable GL_TEXTURE_2D; 2416 glEnable GL_TEXTURE_2D;
2401 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2406 } 2422 }
2407} 2423}
2408 2424
2409############################################################################# 2425#############################################################################
2410 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
2411package CFClient::UI::Root; 2499package CFClient::UI::Root;
2412 2500
2413our @ISA = CFClient::UI::Container::; 2501our @ISA = CFClient::UI::Container::;
2414 2502
2415use CFClient::OpenGL; 2503use CFClient::OpenGL;
2487 $self->_draw; 2575 $self->_draw;
2488} 2576}
2489 2577
2490############################################################################# 2578#############################################################################
2491 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 = sort { $a->{type} <=> $b->{type} } @$items;
2606
2607 $self->{real_items} = \@items;
2608
2609 for my $item (@items) {
2610 my $desc = $item->{nrof} < 2
2611 ? $item->{name}
2612 : "$item->{nrof} $item->{name_pl}";
2613
2614 $self->add (my $hb = new CFClient::UI::HBox);
2615
2616 $hb->add (my $f = new CFClient::UI::Face
2617 can_events => 0,
2618 face => $item->{face},
2619 anim => $item->{anim},
2620 animspeed => $item->{animspeed},
2621 expand => 1,
2622 );
2623 $hb->add (new CFClient::UI::Label text => $desc, expand => 1);
2624 }
2625
2626 $self->{max_pos} = (scalar @items) - 1;
2627
2628 my $range = $self->{range};
2629 my $page = $self->{h} / 32; # what information source to use for face size?
2630 # it should be configurable I guess...
2631 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2632 $range->update;
2633}
2634
2635sub size_request {
2636 my ($self) = @_;
2637 (100, 200)
2638}
2639
2640sub set_range {
2641 my ($self, $range) = @_;
2642
2643 $self->{range} = $range;
2644 $range->connect (changed => sub { $self->update });
2645
2646 my $page = $self->{h} / 32; # waht information souce to use for face size?
2647 # it should be configurable i guess...
2648
2649 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2650 $range->update;
2651
2652 $self->update;
2653}
2654
2655sub _draw {
2656 my ($self) = @_;
2657
2658 my ($w, $h) = ($self->{w}, $self->{h});
2659
2660 $self->{pos} = int $self->{range}{range}[0];
2661
2662 my $y = 0;
2663 my $cnt = 0;
2664 my $hrem = $self->{h}; # horiz. remaining space
2665
2666 for (my $i = $self->{pos}; $i < @{$self->{children} || []}; $i++) {
2667 my $chld = $self->{children}->[$i];
2668
2669 if ($hrem >= $chld->{h}) {
2670 $chld->configure (0, $y, $chld->{w}, $chld->{h});
2671
2672 $chld->draw;
2673
2674 $hrem -= $chld->{h};
2675 $y += $chld->{h};
2676 } else {
2677 last
2678 }
2679 }
2680}
2681
2682
2683#############################################################################
2684
2492package CFClient::UI; 2685package CFClient::UI;
2493 2686
2494$ROOT = new CFClient::UI::Root; 2687$ROOT = new CFClient::UI::Root;
2495$TOOLTIP = new CFClient::UI::Tooltip; 2688$TOOLTIP = new CFClient::UI::Tooltip;
2496 2689

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines