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.362 by root, Sat Dec 9 21:26:46 2006 UTC vs.
Revision 1.371 by root, Sun Jul 15 22:07:48 2007 UTC

22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
23 if (!$GRAB) { 23 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 25 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
27 $TOOLTIP->hide; 28 $TOOLTIP->hide;
28 29
29 $TOOLTIP->{owner} = $widget; 30 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
30 32
31 return if $ENV{CFPLUS_DEBUG} & 8; 33 return if $ENV{CFPLUS_DEBUG} & 8;
32 34
33 my $tip = $widget->{tooltip}; 35 my $tip = $widget->{tooltip};
34 36
42 } 44 }
43 } 45 }
44 } 46 }
45 47
46 $TOOLTIP->hide; 48 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
47 delete $TOOLTIP->{owner}; 50 delete $TOOLTIP->{owner};
48}); 51});
49 52
50sub get_layout { 53sub get_layout {
51 my $layout; 54 my $layout;
265 $self->hide; 268 $self->hide;
266 $self->emit ("destroy"); 269 $self->emit ("destroy");
267 %$self = (); 270 %$self = ();
268} 271}
269 272
273sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} }
275}
276
270sub show { 277sub show {
271 my ($self) = @_; 278 my ($self) = @_;
272 279
273 return if $self->{parent}; 280 return if $self->{parent};
274 281
313sub set_visibility { 320sub set_visibility {
314 my ($self, $visible) = @_; 321 my ($self, $visible) = @_;
315 322
316 return if $self->{visible} == $visible; 323 return if $self->{visible} == $visible;
317 324
318 $visible ? $self->hide 325 $visible ? $self->show
319 : $self->show; 326 : $self->hide;
320} 327}
321 328
322sub toggle_visibility { 329sub toggle_visibility {
323 my ($self) = @_; 330 my ($self) = @_;
324 331
1026 1033
1027############################################################################# 1034#############################################################################
1028 1035
1029package CFPlus::UI::ScrolledWindow; 1036package CFPlus::UI::ScrolledWindow;
1030 1037
1031our @ISA = CFPlus::UI::HBox::; 1038our @ISA = CFPlus::UI::Table::;
1032 1039
1033sub new { 1040sub new {
1034 my ($class, %arg) = @_; 1041 my ($class, %arg) = @_;
1035 1042
1036 my $child = delete $arg{child}; 1043 my $child = delete $arg{child};
1037 1044
1038 my $self; 1045 my $self;
1039 1046
1040 my $slider = new CFPlus::UI::Slider 1047 my $hslider = new CFPlus::UI::Slider
1048 vertical => 0,
1049 range => [0, 0, 1, 0.01], # HACK fix
1050 on_changed => sub {
1051 $self->{hpos} = $_[1];
1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1053 },
1054 ;
1055
1056 my $vslider = new CFPlus::UI::Slider
1041 vertical => 1, 1057 vertical => 1,
1042 range => [0, 0, 1, 0.01], # HACK fix 1058 range => [0, 0, 1, 0.01], # HACK fix
1043 on_changed => sub { 1059 on_changed => sub {
1044 $self->{vp}->set_offset (0, $_[1]); 1060 $self->{vpos} = $_[1];
1061 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1045 }, 1062 },
1046 ; 1063 ;
1047 1064
1048 $self = $class->SUPER::new ( 1065 $self = $class->SUPER::new (
1049 vp => (new CFPlus::UI::ViewPort expand => 1), 1066 vp => (new CFPlus::UI::ViewPort expand => 1),
1050 can_events => 1, 1067 can_events => 1,
1051 slider => $slider, 1068 hslider => $hslider,
1069 vslider => $vslider,
1070 col_expand => [1, 0],
1071 row_expand => [1, 0],
1052 %arg, 1072 %arg,
1053 ); 1073 );
1054 1074
1075 $self->SUPER::add (0, 0, $self->{vp});
1055 $self->SUPER::add ($self->{vp}, $self->{slider}); 1076 $self->SUPER::add (1, 0, $self->{vslider});
1077 $self->SUPER::add (0, 1, $self->{hslider});
1078
1056 $self->add ($child) if $child; 1079 $self->add ($child) if $child;
1057 1080
1058 $self 1081 $self
1059} 1082}
1060 1083
1069sub invoke_mouse_wheel { 1092sub invoke_mouse_wheel {
1070 my ($self, $ev) = @_; 1093 my ($self, $ev) = @_;
1071 1094
1072 return 0 unless $ev->{dy}; # only vertical movements 1095 return 0 unless $ev->{dy}; # only vertical movements
1073 1096
1074 $self->{slider}->emit (mouse_wheel => $ev); 1097 $self->{vslider}->emit (mouse_wheel => $ev);
1075 1098
1076 1 1099 1
1077} 1100}
1078 1101
1079sub update_slider { 1102sub update_slider {
1080 my ($self) = @_; 1103 my ($self) = @_;
1081 1104
1105 my $child = ($self->{vp} or return)->child;
1106
1107 my ($w1, $w2) = ($child->{w}, $self->{vp}{w});
1082 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1108 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1109 #$self->{hslider}->set_visibility ($w1 != $w2);
1110
1111 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1112 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1113 #$self->{vslider}->set_visibility ($h1 != $h2);
1083} 1114}
1084 1115
1085sub update { 1116sub update {
1086 my ($self) = @_; 1117 my ($self) = @_;
1087 1118
1088 $self->SUPER::update; 1119 $self->SUPER::update;
1089
1090 $self->update_slider; 1120 $self->update_slider;
1091} 1121}
1092 1122
1093sub invoke_size_allocate { 1123sub invoke_size_allocate {
1094 my ($self, $w, $h) = @_; 1124 my ($self, $w, $h) = @_;
1095 1125
1096 $self->update_slider; 1126 $self->update_slider;
1097
1098 $self->SUPER::invoke_size_allocate ($w, $h) 1127 $self->SUPER::invoke_size_allocate ($w, $h)
1099} 1128}
1100 1129
1101############################################################################# 1130#############################################################################
1102 1131
1474sub new { 1503sub new {
1475 my $class = shift; 1504 my $class = shift;
1476 1505
1477 $class->SUPER::new ( 1506 $class->SUPER::new (
1478 col_expand => [], 1507 col_expand => [],
1508 row_expand => [],
1479 @_, 1509 @_,
1480 ) 1510 )
1481} 1511}
1482 1512
1483sub children { 1513sub children {
1558 my ($ws, $hs) = $self->get_wh; 1588 my ($ws, $hs) = $self->get_wh;
1559 1589
1560 my $req_w = (sum @$ws) || 1; 1590 my $req_w = (sum @$ws) || 1;
1561 my $req_h = (sum @$hs) || 1; 1591 my $req_h = (sum @$hs) || 1;
1562 1592
1563 # TODO: nicer code && do row_expand 1593 # TODO: nicer code
1564 my @col_expand = @{$self->{col_expand}}; 1594 my @col_expand = @{$self->{col_expand}};
1565 @col_expand = (1) x @$ws unless @col_expand; 1595 @col_expand = (1) x @$ws unless @col_expand;
1566 my $col_expand = (sum @col_expand) || 1; 1596 my $col_expand = (sum @col_expand) || 1;
1567 1597
1568 # linearly scale sizes
1569 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1598 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1570 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1571 1599
1572 CFPlus::UI::harmonize $ws; 1600 CFPlus::UI::harmonize $ws;
1601
1602 my @row_expand = @{$self->{row_expand}};
1603 @row_expand = (1) x @$ws unless @row_expand;
1604 my $row_expand = (sum @row_expand) || 1;
1605
1606 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1607
1573 CFPlus::UI::harmonize $hs; 1608 CFPlus::UI::harmonize $hs;
1574 1609
1575 my $y; 1610 my $y;
1576 1611
1577 for my $r (0 .. $#{$self->{children}}) { 1612 for my $r (0 .. $#{$self->{children}}) {
1967 my $sym = $ev->{sym}; 2002 my $sym = $ev->{sym};
1968 my $uni = $ev->{unicode}; 2003 my $uni = $ev->{unicode};
1969 2004
1970 my $text = $self->get_text; 2005 my $text = $self->get_text;
1971 2006
2007 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2008
1972 if ($uni == 8) { 2009 if ($uni == 8) {
1973 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2010 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1974 } elsif ($uni == 127) { 2011 } elsif ($uni == 127) {
1975 substr $text, $self->{cursor}, 1, ""; 2012 substr $text, $self->{cursor}, 1, "";
1976 } elsif ($sym == CFPlus::SDLK_LEFT) { 2013 } elsif ($sym == CFPlus::SDLK_LEFT) {
1977 --$self->{cursor} if $self->{cursor}; 2014 --$self->{cursor} if $self->{cursor};
1978 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2015 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1979 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2016 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1980 } elsif ($sym == CFPlus::SDLK_HOME) { 2017 } elsif ($sym == CFPlus::SDLK_HOME) {
2018 # what a hack
1981 $self->{cursor} = 0; 2019 $self->{cursor} =
2020 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2021 ? length $1
2022 : 0;
1982 } elsif ($sym == CFPlus::SDLK_END) { 2023 } elsif ($sym == CFPlus::SDLK_END) {
2024 # uh, again
2025 $self->{cursor} =
2026 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
1983 $self->{cursor} = length $text; 2027 ? $self->{cursor} + length $1
2028 : length $self->{text};
1984 } elsif ($uni == 21) { # ctrl-u 2029 } elsif ($uni == 21) { # ctrl-u
1985 $text = ""; 2030 $text = "";
1986 $self->{cursor} = 0; 2031 $self->{cursor} = 0;
1987 } elsif ($uni == 27) { 2032 } elsif ($uni == 27) {
1988 $self->emit ('escape'); 2033 $self->emit ('escape');
1989 } elsif ($uni >= 0x20 || $uni == 0x0d) { 2034 } elsif ($uni == 0x0d) {
2035 substr $text, $self->{cursor}++, 0, "\012";
2036 } elsif ($uni >= 0x20) {
1990 substr $text, $self->{cursor}++, 0, chr $uni; 2037 substr $text, $self->{cursor}++, 0, chr $uni;
1991 } else { 2038 } else {
1992 return 0; 2039 return 0;
1993 } 2040 }
1994 2041
2698sub invoke_mouse_wheel { 2745sub invoke_mouse_wheel {
2699 my ($self, $ev) = @_; 2746 my ($self, $ev) = @_;
2700 2747
2701 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2748 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2702 2749
2750 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2751
2703 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 2752 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2704 2753
2705 ! ! $delta 2754 ! ! $delta
2706} 2755}
2707 2756
2708sub update { 2757sub update {
2818 #font => default_font 2867 #font => default_font
2819 @_, 2868 @_,
2820 2869
2821 layout => (new CFPlus::Layout), 2870 layout => (new CFPlus::Layout),
2822 par => [], 2871 par => [],
2872 max_par => 0,
2823 height => 0, 2873 height => 0,
2824 children => [ 2874 children => [
2825 (new CFPlus::UI::Empty expand => 1), 2875 (new CFPlus::UI::Empty expand => 1),
2826 (new CFPlus::UI::Slider vertical => 1), 2876 (new CFPlus::UI::Slider vertical => 1),
2827 ], 2877 ],
2950 wrapped => 1, 3000 wrapped => 1,
2951 }; 3001 };
2952 3002
2953 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 3003 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2954 push @{$self->{par}}, $para; 3004 push @{$self->{par}}, $para;
3005 }
3006
3007 if (my $max = $self->{max_par}) {
3008 shift @{$self->{par}} while @{$self->{par}} > $max;
2955 } 3009 }
2956 3010
2957 $self->{need_reflow}++; 3011 $self->{need_reflow}++;
2958 $self->update; 3012 $self->update;
2959} 3013}
3280 $self->{timer} = Event->timer ( 3334 $self->{timer} = Event->timer (
3281 at => $self->{animspeed} * int $::NOW / $self->{animspeed}, 3335 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3282 hard => 1, 3336 hard => 1,
3283 interval => $self->{animspeed}, 3337 interval => $self->{animspeed},
3284 cb => sub { 3338 cb => sub {
3339 delete $self->{wait_face};
3285 ++$widget->{frame}; 3340 ++$widget->{frame};
3286 $widget->update; 3341 $widget->update;
3287 }, 3342 },
3288 ); 3343 );
3289 } 3344 }
3316 my $anim = $::CONN->{anim}[$self->{anim}]; 3371 my $anim = $::CONN->{anim}[$self->{anim}];
3317 3372
3318 $face = $anim->[ $self->{frame} % @$anim ] 3373 $face = $anim->[ $self->{frame} % @$anim ]
3319 if $anim && @$anim; 3374 if $anim && @$anim;
3320 } 3375 }
3376
3377 my $faceid = $::CONN->{faceid}[$face || $self->{face}]
3378 or return;
3321 3379
3322 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 3380 my $tex = $::CONN->{texture}[$faceid];
3323 3381
3324 if ($tex) { 3382 if ($tex) {
3325 glEnable GL_TEXTURE_2D; 3383 glEnable GL_TEXTURE_2D;
3326 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3384 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3327 glColor 0, 0, 0, 1; 3385 glColor 0, 0, 0, 1;
3328 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3386 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3329 glDisable GL_TEXTURE_2D; 3387 glDisable GL_TEXTURE_2D;
3388 } else {
3389 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3390 $self->update;
3391 });
3330 } 3392 }
3331} 3393}
3332 3394
3333sub destroy { 3395sub destroy {
3334 my ($self) = @_; 3396 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines