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.173 by root, Tue Apr 25 10:30:48 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 }
643} 651}
644 652
645sub update { 653sub update {
646 my ($self) = @_; 654 my ($self) = @_;
647 655
648 # we want to do this delayed... 656 $ROOT->on_refresh ($self => sub { $self->render_child });
649 $self->render_chld;
650 $self->SUPER::update; 657 $self->SUPER::update;
651} 658}
652 659
660sub size_allocate {
661 my ($self, $w, $h) = @_;
662
663 $self->SUPER::size_allocate ($w, $h);
664 $self->update;
665}
666
653sub render_chld { 667sub render_child {
654 my ($self) = @_; 668 my ($self) = @_;
655 669
656 $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 {
657 glClearColor 0, 0, 0, 1; 671 glClearColor 0, 0, 0, 0;
658 glClear GL_COLOR_BUFFER_BIT; 672 glClear GL_COLOR_BUFFER_BIT;
659 $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;
660 }; 681 };
661}
662
663sub size_allocate {
664 my ($self, $w, $h) = @_;
665
666 $self->child->configure (0, 0, $w, $h);
667
668 $self->render_chld;
669} 682}
670 683
671sub _draw { 684sub _draw {
672 my ($self) = @_; 685 my ($self) = @_;
673 686
675 688
676 my $tex = $self->{texture} 689 my $tex = $self->{texture}
677 or return; 690 or return;
678 691
679 glEnable GL_BLEND; 692 glEnable GL_BLEND;
680 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 693 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
681 glEnable GL_TEXTURE_2D; 694 glEnable GL_TEXTURE_2D;
682 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;
683 697
684 $tex->draw_quad (0, 0, $w, $h); 698 $tex->draw_quad (0, 0, $w, $h);
685 699
686 glDisable GL_BLEND; 700 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 701 glDisable GL_TEXTURE_2D;
774 # TODO: user_x, user_y, overwrite moveto? 788 # TODO: user_x, user_y, overwrite moveto?
775 789
776 my $self = $class->SUPER::new ( 790 my $self = $class->SUPER::new (
777 bg => [1, 1, 1, 1], 791 bg => [1, 1, 1, 1],
778 border_bg => [1, 1, 1, 1], 792 border_bg => [1, 1, 1, 1],
779 border => 0.8, 793 border => 0.6,
780 can_events => 1, 794 can_events => 1,
781 @_ 795 @_
782 ); 796 );
783 797
784 $self->{title} &&= new CFClient::UI::Label 798 $self->{title} &&= new CFClient::UI::Label
785 align => 0, 799 align => 0,
786 valign => 1, 800 valign => 1,
787 text => $self->{title}, 801 text => $self->{title},
788 fontsize => 1; 802 fontsize => $self->{border};
789 803
790 $self 804 $self
791} 805}
792 806
793sub border { 807sub border {
818} 832}
819 833
820sub button_down { 834sub button_down {
821 my ($self, $ev, $x, $y) = @_; 835 my ($self, $ev, $x, $y) = @_;
822 836
837 my ($w, $h) = @$self{qw(w h)};
823 my $border = $self->border; 838 my $border = $self->border;
824 839
825 if ($x < $self->{w} && $x >= $self->{w} - $border 840 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
826 && $y < $self->{h} && $y >= $self->{h} - $border) { 841 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
827 842
843 if ($lr & $td) {
844 my ($wx, $wy) = ($self->{x}, $self->{y});
828 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 845 my ($ox, $oy) = ($ev->{x}, $ev->{y});
829 my ($bw, $bh) = ($self->{w}, $self->{h}); 846 my ($bw, $bh) = ($self->{w}, $self->{h});
830 847
848 my $mx = $x < $border;
849 my $my = $y < $border;
850
831 $self->{motion} = sub { 851 $self->{motion} = sub {
832 my ($ev, $x, $y) = @_; 852 my ($ev, $x, $y) = @_;
833 853
834 ($x, $y) = ($ev->{x}, $ev->{y}); 854 my $dx = $ev->{x} - $ox;
855 my $dy = $ev->{y} - $oy;
835 856
836 $self->{user_w} = $bw + $x - $ox; 857 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
837 $self->{user_h} = $bh + $y - $oy; 858 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
859 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
838 $self->check_size; 860 $self->check_size;
839 }; 861 };
840 862
841 } elsif ($x >= 0 && $x < $self->{w} 863 } elsif ($lr ^ $td) {
842 && $y >= 0 && $y < $border) {
843
844 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 864 my ($ox, $oy) = ($ev->{x}, $ev->{y});
845 my ($bx, $by) = ($self->{x}, $self->{y}); 865 my ($bx, $by) = ($self->{x}, $self->{y});
846 866
847 $self->{motion} = sub { 867 $self->{motion} = sub {
848 my ($ev, $x, $y) = @_; 868 my ($ev, $x, $y) = @_;
884 $tex[1]->draw_quad (0, 0, $w, $border); 904 $tex[1]->draw_quad (0, 0, $w, $border);
885 $tex[3]->draw_quad (0, $border, $border, $ch); 905 $tex[3]->draw_quad (0, $border, $border, $ch);
886 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 906 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
887 $tex[4]->draw_quad (0, $h - $border, $w, $border); 907 $tex[4]->draw_quad (0, $h - $border, $w, $border);
888 908
909 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
889 my $bg = $tex[0]; 910 my $bg = $tex[0];
890 911
891 # TODO: repeat texture not scale 912 # TODO: repeat texture not scale
892 my $rep_x = $cw / $bg->{w}; 913 my $rep_x = $cw / $bg->{w};
893 my $rep_y = $ch / $bg->{h}; 914 my $rep_y = $ch / $bg->{h};
894 915
895 glColor @{ $self->{bg} }; 916 glColor @{ $self->{bg} };
896 917
897 $bg->{s} = $rep_x; 918 $bg->{s} = $rep_x;
898 $bg->{t} = $rep_y; 919 $bg->{t} = $rep_y;
899 $bg->{wrap_mode} = 1; 920 $bg->{wrap_mode} = 1;
900 $bg->draw_quad ($border, $border, $cw, $ch); 921 $bg->draw_quad ($border, $border, $cw, $ch);
901 922
902 glDisable GL_TEXTURE_2D; 923 glDisable GL_TEXTURE_2D;
903 glDisable GL_BLEND; 924 glDisable GL_BLEND;
925 }
904 926
905 $self->{title}->draw if $self->{title}; 927 $self->{title}->draw if $self->{title};
928
906 $self->child->draw; 929 $self->child->draw;
907} 930}
908 931
909############################################################################# 932#############################################################################
910 933
2069 (new CFClient::UI::Empty expand => 1), 2092 (new CFClient::UI::Empty expand => 1),
2070 (new CFClient::UI::Slider vertical => 1), 2093 (new CFClient::UI::Slider vertical => 1),
2071 ], 2094 ],
2072 ); 2095 );
2073 2096
2074 $self->{children}[1]->connect (changed => sub { 2097 $self->{children}[1]->connect (changed => sub { $self->update });
2075 $self->update;
2076 });
2077 2098
2078 $self 2099 $self
2079} 2100}
2080 2101
2081sub set_fontsize { 2102sub set_fontsize {
2089 my ($self, $text) = @_; 2110 my ($self, $text) = @_;
2090 2111
2091 my $layout = $self->{layout}; 2112 my $layout = $self->{layout};
2092 2113
2093 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2114 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2094 $layout->set_width ($self->{w}); 2115 $layout->set_width ($self->{children}[0]{w});
2095 $layout->set_text ($text); 2116 $layout->set_text ($text);
2096 2117
2097 ($layout->size)[1] 2118 ($layout->size)[1]
2098} 2119}
2099 2120
2152 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2173 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2153 2174
2154 delete $self->{texture}; 2175 delete $self->{texture};
2155 } 2176 }
2156 2177
2157 $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 {
2158 glClearColor 0, 0, 0, 1; 2179 glClearColor 0, 0, 0, 0;
2159 glClear GL_COLOR_BUFFER_BIT; 2180 glClear GL_COLOR_BUFFER_BIT;
2160 2181
2161 glEnable GL_BLEND; 2182 glEnable GL_BLEND;
2162 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2183 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2163 glEnable GL_TEXTURE_2D; 2184 glEnable GL_TEXTURE_2D;
2195} 2216}
2196 2217
2197sub _draw { 2218sub _draw {
2198 my ($self) = @_; 2219 my ($self) = @_;
2199 2220
2200 if ($self->{texture}) { 2221 glEnable GL_BLEND;
2222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2201 glEnable GL_TEXTURE_2D; 2223 glEnable GL_TEXTURE_2D;
2202 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2224 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2203 glColor 1, 1, 1, 1; 2225 glColor 1, 1, 1, 1;
2204 $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});
2205 glDisable GL_TEXTURE_2D; 2227 glDisable GL_TEXTURE_2D;
2206 } 2228 glDisable GL_BLEND;
2207 2229
2208 $self->{children}[1]->draw; 2230 $self->{children}[1]->draw;
2209 2231
2210} 2232}
2211 2233
2380 2402
2381sub size_request { 2403sub size_request {
2382 (32, 8) 2404 (32, 8)
2383} 2405}
2384 2406
2385sub draw { 2407sub _draw {
2386 my ($self) = @_; 2408 my ($self) = @_;
2387 2409
2388 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2389 2411
2412 # TODO animation
2390 if ($tex) { 2413 if ($tex) {
2391 glEnable GL_BLEND; 2414 glEnable GL_BLEND;
2392 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2415 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2393 glEnable GL_TEXTURE_2D; 2416 glEnable GL_TEXTURE_2D;
2394 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2399 } 2422 }
2400} 2423}
2401 2424
2402############################################################################# 2425#############################################################################
2403 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
2404package CFClient::UI::Root; 2499package CFClient::UI::Root;
2405 2500
2406our @ISA = CFClient::UI::Container::; 2501our @ISA = CFClient::UI::Container::;
2407 2502
2408use CFClient::OpenGL; 2503use CFClient::OpenGL;
2480 $self->_draw; 2575 $self->_draw;
2481} 2576}
2482 2577
2483############################################################################# 2578#############################################################################
2484 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
2485package CFClient::UI; 2687package CFClient::UI;
2486 2688
2487$ROOT = new CFClient::UI::Root; 2689$ROOT = new CFClient::UI::Root;
2488$TOOLTIP = new CFClient::UI::Tooltip; 2690$TOOLTIP = new CFClient::UI::Tooltip;
2489 2691

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines