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.254 by root, Mon May 29 01:53:23 2006 UTC vs.
Revision 1.256 by root, Tue May 30 01:42:16 2006 UTC

17our $BUTTON_STATE; 17our $BUTTON_STATE;
18 18
19our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20 20
21sub get_layout { 21sub get_layout {
22 my $layout;
23
22 for (grep { $_->{name} } values %WIDGET) { 24 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = { 25 my $win = $layout->{$_->{name}} = { };
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 }; 26
27 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
28 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
29 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
30 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
29 } 31 }
30 32
31 return $LAYOUT; 33 $layout
32} 34}
33 35
34sub set_layout { 36sub set_layout {
35 my ($layout) = @_; 37 my ($layout) = @_;
36 $LAYOUT = $layout; 38 $LAYOUT = $layout;
165sub rescale_widgets { 167sub rescale_widgets {
166 my ($sx, $sy) = @_; 168 my ($sx, $sy) = @_;
167 169
168 for my $widget (values %WIDGET) { 170 for my $widget (values %WIDGET) {
169 if ($widget->{is_toplevel}) { 171 if ($widget->{is_toplevel}) {
172 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
173 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
174
170 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 175 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
171 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 176 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
172 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 177 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
173 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 178 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
174 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 179 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
175 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 180 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
181
182 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
183 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
184
176 } 185 }
177 } 186 }
178 187
179 reconfigure_widgets; 188 reconfigure_widgets;
180} 189}
189 198
190sub new { 199sub new {
191 my $class = shift; 200 my $class = shift;
192 201
193 my $self = bless { 202 my $self = bless {
194 x => 0, 203 x => "center",
195 y => 0, 204 y => "center",
196 z => 0, 205 z => 0,
206 w => undef,
207 h => undef,
197 can_events => 1, 208 can_events => 1,
198 @_ 209 @_
199 }, $class; 210 }, $class;
211
212 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
213 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
214 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
215 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
216 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
217
218 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
219 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
220 }
221
222 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
200 223
201 for (keys %$self) { 224 for (keys %$self) {
202 if (/^on_(.*)$/) { 225 if (/^on_(.*)$/) {
203 $self->connect ($1 => delete $self->{$_}); 226 $self->connect ($1 => delete $self->{$_});
204 } 227 }
205 } 228 }
206 229
207 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
208
209 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
210 $self->{req_x} = $layout->{x} * $::WIDTH;
211 $self->{req_y} = $layout->{y} * $::HEIGHT;
212 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
213 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
214 }
215
216 $self 230 $self
217} 231}
218 232
219sub destroy { 233sub destroy {
220 my ($self) = @_; 234 my ($self) = @_;
227 my ($self) = @_; 241 my ($self) = @_;
228 242
229 return if $self->{parent}; 243 return if $self->{parent};
230 244
231 $CFClient::UI::ROOT->add ($self); 245 $CFClient::UI::ROOT->add ($self);
232}
233
234sub center {
235 my ($self) = @_;
236
237 $CFClient::UI::ROOT->on_post_alloc (
238 "center_$self" => sub {
239 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
240 },
241 );
242
243 $self->update;
244} 246}
245 247
246sub set_visible { 248sub set_visible {
247 my ($self) = @_; 249 my ($self) = @_;
248 250
303 305
304 $self->{parent}->remove ($self) 306 $self->{parent}->remove ($self)
305 if $self->{parent}; 307 if $self->{parent};
306} 308}
307 309
308sub move { 310sub move_abs {
309 my ($self, $x, $y, $z) = @_; 311 my ($self, $x, $y, $z) = @_;
310 312
311 $self->{x} = int $x; 313 $self->{x} = List::Util::max 0, int $x;
312 $self->{y} = int $y; 314 $self->{y} = List::Util::max 0, int $y;
313 $self->{z} = $z if defined $z; 315 $self->{z} = $z if defined $z;
314 316
315 $self->update; 317 $self->update;
316} 318}
317 319
318sub set_size { 320sub set_size {
319 my ($self, $w, $h) = @_; 321 my ($self, $w, $h) = @_;
320 322
321 $self->{def_w} = $w; 323 $self->{force_w} = $w;
322 $self->{def_h} = $h; 324 $self->{force_h} = $h;
323 325
324 $self->realloc; 326 $self->realloc;
325} 327}
326 328
327sub size_request { 329sub size_request {
331 333
332sub configure { 334sub configure {
333 my ($self, $x, $y, $w, $h) = @_; 335 my ($self, $x, $y, $w, $h) = @_;
334 336
335 if ($self->{aspect}) { 337 if ($self->{aspect}) {
338 my ($ow, $oh) = ($w, $h);
339
336 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 340 $w = List::Util::min $w, int $h * $self->{aspect};
337 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 341 $h = List::Util::min $h, int $w / $self->{aspect};
338 342
339 # use alignment to adjust x, y 343 # use alignment to adjust x, y
340 344
341 $x += int +($w - $w2) * 0.5; 345 $x += int 0.5 * ($ow - $w);
342 $y += int +($h - $h2) * 0.5; 346 $y += int 0.5 * ($oh - $h);
343
344 ($w, $h) = ($w2, $h2);
345 } 347 }
346 348
347 if ($self->{x} != $x || $self->{y} != $y) { 349 if ($self->{x} ne $x || $self->{y} ne $y) {
348 $self->{x} = $x; 350 $self->{x} = $x;
349 $self->{y} = $y; 351 $self->{y} = $y;
350 $self->update; 352 $self->update;
351 } 353 }
352 354
506sub update { 508sub update {
507 my ($self) = @_; 509 my ($self) = @_;
508 510
509 $self->{parent}->update 511 $self->{parent}->update
510 if $self->{parent}; 512 if $self->{parent};
513}
514
515sub reconfigure {
516 my ($self) = @_;
517
518 $self->realloc;
519 $self->update;
511} 520}
512 521
513sub draw { 522sub draw {
514 my ($self) = @_; 523 my ($self) = @_;
515 524
533 glVertex $x , $y + $self->{h}; 542 glVertex $x , $y + $self->{h};
534 glEnd; 543 glEnd;
535 glDisable GL_BLEND; 544 glDisable GL_BLEND;
536 } 545 }
537 546
538 if ($ENV{PCLIENT_DEBUG}) { 547 if ($ENV{CFPLUS_DEBUG}) {
539 glPushMatrix; 548 glPushMatrix;
540 glColor 1, 1, 0, 1; 549 glColor 1, 1, 0, 1;
541 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 550 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
542 glBegin GL_LINE_LOOP; 551 glBegin GL_LINE_LOOP;
543 glVertex 0 , 0; 552 glVertex 0 , 0;
619 my ($class, %arg) = @_; 628 my ($class, %arg) = @_;
620 $class->SUPER::new (can_events => 0, %arg); 629 $class->SUPER::new (can_events => 0, %arg);
621} 630}
622 631
623sub size_request { 632sub size_request {
624 (0, 0) 633 my ($self) = @_;
634
635 ($self->{w} + 0, $self->{h} + 0)
625} 636}
626 637
627sub draw { } 638sub draw { }
628 639
629############################################################################# 640#############################################################################
1000 1011
1001our @ISA = CFClient::UI::Bin::; 1012our @ISA = CFClient::UI::Bin::;
1002 1013
1003use CFClient::OpenGL; 1014use CFClient::OpenGL;
1004 1015
1005my @tex = 1016my $bg =
1017 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1018 mipmap => 1, wrap => 1;
1019
1020my @border =
1006 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1021 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1007 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1022 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1008 1023
1009sub new { 1024sub new {
1010 my $class = shift; 1025 my $class = shift;
1011 1026
1012 my $self = $class->SUPER::new ( 1027 my $self = $class->SUPER::new (
1077 my ($ev, $x, $y) = @_; 1092 my ($ev, $x, $y) = @_;
1078 1093
1079 my $dx = $ev->{x} - $ox; 1094 my $dx = $ev->{x} - $ox;
1080 my $dy = $ev->{y} - $oy; 1095 my $dy = $ev->{y} - $oy;
1081 1096
1082 $self->{user_x} = $wx + $dx * $mx;
1083 $self->{user_y} = $wy + $dy * $my;
1084 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); 1097 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1085 $self->{def_h} = $bh + $dy * ($my ? -1 : 1); 1098 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1086 $self->move ($self->{user_x}, $self->{user_y}); 1099
1087 $self->realloc; 1100 $self->realloc;
1101 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1088 }; 1102 };
1089 1103
1090 } elsif ($lr ^ $td) { 1104 } elsif ($lr ^ $td) {
1091 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1105 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1092 my ($bx, $by) = ($self->{x}, $self->{y}); 1106 my ($bx, $by) = ($self->{x}, $self->{y});
1094 $self->{motion} = sub { 1108 $self->{motion} = sub {
1095 my ($ev, $x, $y) = @_; 1109 my ($ev, $x, $y) = @_;
1096 1110
1097 ($x, $y) = ($ev->{x}, $ev->{y}); 1111 ($x, $y) = ($ev->{x}, $ev->{y});
1098 1112
1099 $self->{user_x} = $bx + $x - $ox; 1113 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1100 $self->{user_y} = $by + $y - $oy;
1101
1102 $self->move ($self->{user_x}, $self->{user_y});
1103 $self->realloc;
1104 }; 1114 };
1105 } 1115 }
1106} 1116}
1107 1117
1108sub button_up { 1118sub button_up {
1127 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1128 1138
1129 my $border = $self->border; 1139 my $border = $self->border;
1130 1140
1131 glColor @{ $self->{border_bg} }; 1141 glColor @{ $self->{border_bg} };
1132 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1142 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1133 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1143 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1134 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1144 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1135 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1145 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1136 1146
1137 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1147 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1138 my $bg = $tex[0]; 1148 glColor @{ $self->{bg} };
1139 1149
1140 # TODO: repeat texture not scale 1150 # TODO: repeat texture not scale
1151 # solve this better(?)
1141 my $rep_x = $cw / $bg->{w}; 1152 $bg->{s} = $cw / $bg->{w};
1142 my $rep_y = $ch / $bg->{h}; 1153 $bg->{t} = $ch / $bg->{h};
1143
1144 glColor @{ $self->{bg} };
1145
1146 $bg->{s} = $rep_x;
1147 $bg->{t} = $rep_y;
1148 $bg->{wrap_mode} = 1;
1149 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1154 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1150 } 1155 }
1151 1156
1152 glDisable GL_TEXTURE_2D; 1157 glDisable GL_TEXTURE_2D;
1153 1158
2650 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 2655 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2651 2656
2652 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 2657 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2653 if $x + $self->{w} > $::WIDTH; 2658 if $x + $self->{w} > $::WIDTH;
2654 2659
2655 $self->move ($x, $y); 2660 $self->move_abs ($x, $y);
2656 }); 2661 });
2657} 2662}
2658 2663
2659sub _draw { 2664sub _draw {
2660 my ($self) = @_; 2665 my ($self) = @_;
3029 my ($self) = @_; 3034 my ($self) = @_;
3030 3035
3031 ($self->{w}, $self->{h}) 3036 ($self->{w}, $self->{h})
3032} 3037}
3033 3038
3039sub _to_pixel {
3040 my ($coord, $size, $max) = @_;
3041
3042 $coord =
3043 $coord eq "center" ? ($max - $size) * 0.5
3044 : $coord eq "max" ? $max
3045 : $coord;
3046
3047 $coord = 0 if $coord < 0;
3048 $coord = $max - $size if $coord > $max - $size;
3049
3050 int $coord + 0.5
3051}
3052
3034sub size_allocate { 3053sub size_allocate {
3035 my ($self, $w, $h, $changed) = @_; 3054 my ($self, $w, $h, $changed) = @_;
3036 3055
3037 for my $child ($self->children) { 3056 for my $child ($self->children) {
3038 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3057 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3039 3058
3040 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3059 $X = $child->{force_x} if exists $child->{force_x};
3041 if exists $child->{req_x}; 3060 $Y = $child->{force_y} if exists $child->{force_y};
3042 3061
3043 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3062 $X = _to_pixel $X, $W, $self->{w};
3044 if exists $child->{req_y}; 3063 $Y = _to_pixel $Y, $H, $self->{h};
3045
3046 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3047
3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3050 3064
3051 $child->configure ($X, $Y, $W, $H); 3065 $child->configure ($X, $Y, $W, $H);
3052 } 3066 }
3053} 3067}
3054 3068
3071} 3085}
3072 3086
3073sub add { 3087sub add {
3074 my ($self, @children) = @_; 3088 my ($self, @children) = @_;
3075 3089
3076 for my $child (@children) {
3077 $child->{is_toplevel} = 1; 3090 $_->{is_toplevel} = 1
3078 3091 for @children;
3079 # integerise window positions
3080 $child->{x} = int $child->{x};
3081 $child->{y} = int $child->{y};
3082 }
3083 3092
3084 $self->SUPER::add (@children); 3093 $self->SUPER::add (@children);
3085} 3094}
3086 3095
3087sub remove { 3096sub remove {
3088 my ($self, @children) = @_; 3097 my ($self, @children) = @_;
3089 3098
3090 $self->SUPER::remove (@children); 3099 $self->SUPER::remove (@children);
3100
3101 delete $self->{is_toplevel}
3102 for @children;
3091 3103
3092 while (@children) { 3104 while (@children) {
3093 my $w = pop @children; 3105 my $w = pop @children;
3094 push @children, $w->children; 3106 push @children, $w->children;
3095 $w->set_invisible; 3107 $w->set_invisible;
3129 3141
3130 my $widget = pop @queue || last; 3142 my $widget = pop @queue || last;
3131 3143
3132 $widget->{visible} or last; # do not resize invisible widgets 3144 $widget->{visible} or last; # do not resize invisible widgets
3133 3145
3134 my ($w, $h) = $widget->{def_w} && $widget->{def_h} 3146 my ($w, $h) = exists $widget->{force_w} && exists $widget->{force_h}
3135 ? @$widget{qw(def_w def_h)} 3147 ? @$widget{qw(force_w force_h)}
3136 : $widget->size_request; 3148 : $widget->size_request;
3137 3149
3138 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3150 my $min_size = $widget->{is_toplevel} ? 16 : 0;
3139 3151
3152 $w = $min_size if $w < $min_size;
3153 $h = $min_size if $h < $min_size;
3154
3140 $widget->{req_w} = $w; 3155 $widget->{req_w} = $w;
3141 $widget->{req_h} = $h; 3156 $widget->{req_h} = $h;
3142 3157
3143 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3158 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3144 3159
3145 push @queue, $widget->{parent} 3160 push @queue, $widget->{parent}
3146 if $widget->{parent}; 3161 if $widget->{parent};
3147 } 3162 }
3148 } 3163 }
3152 values %$size_alloc; 3167 values %$size_alloc;
3153 3168
3154 while () { 3169 while () {
3155 my ($widget, $w, $h) = @{ pop @queue or last }; 3170 my ($widget, $w, $h) = @{ pop @queue or last };
3156 3171
3172 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3173 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3174
3157 $w = 0 if $w < 0; 3175 $w = 0 if $w < 0;
3158 $h = 0 if $h < 0; 3176 $h = 0 if $h < 0;
3177
3178 $w = int $w + 0.5;
3179 $h = int $h + 0.5;
3159 3180
3160 my $changed = $widget->{w} != $w || $widget->{h} != $h; 3181 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3161 3182
3162 $widget->{w} = $w; 3183 $widget->{w} = $w;
3163 $widget->{h} = $h; 3184 $widget->{h} = $h;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines