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.262 by elmex, Wed May 31 13:44:26 2006 UTC vs.
Revision 1.266 by root, Thu Jun 1 03:42:58 2006 UTC

40 40
41 $LAYOUT = $layout; 41 $LAYOUT = $layout;
42} 42}
43 43
44sub check_tooltip { 44sub check_tooltip {
45 return if $ENV{CFPLUS_DEBUG} & 8;
46
45 if (!$GRAB) { 47 if (!$GRAB) {
46 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 48 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
47 if (length $widget->{tooltip}) { 49 if (length $widget->{tooltip}) {
48
49 if ($TOOLTIP->{owner} != $widget) { 50 if ($TOOLTIP->{owner} != $widget) {
50 $TOOLTIP->hide; 51 $TOOLTIP->hide;
51 52
52 $TOOLTIP->{owner} = $widget; 53 $TOOLTIP->{owner} = $widget;
53 54
1522} 1523}
1523 1524
1524sub size_allocate { 1525sub size_allocate {
1525 my ($self, $w, $h) = @_; 1526 my ($self, $w, $h) = @_;
1526 1527
1527 delete $self->{texture}; 1528 delete $self->{texture}
1529 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1528} 1530}
1529 1531
1530sub set_fontsize { 1532sub set_fontsize {
1531 my ($self, $fontsize) = @_; 1533 my ($self, $fontsize) = @_;
1532 1534
2776 2778
2777 $self->{timer}->cancel 2779 $self->{timer}->cancel
2778 if $self->{timer}; 2780 if $self->{timer};
2779 2781
2780 $self->SUPER::DESTROY; 2782 $self->SUPER::DESTROY;
2781}
2782
2783#############################################################################
2784
2785package CFClient::UI::Inventory;
2786
2787our @ISA = CFClient::UI::ScrolledWindow::;
2788
2789sub new {
2790 my $class = shift;
2791
2792 my $self = $class->SUPER::new (
2793 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2794 @_,
2795 );
2796
2797 $self
2798}
2799
2800sub set_items {
2801 my ($self, $items) = @_;
2802
2803 $self->{scrolled}->clear;
2804 return unless $items;
2805
2806 my @items = sort {
2807 ($a->{type} <=> $b->{type})
2808 or ($a->{name} cmp $b->{name})
2809 } @$items;
2810
2811 $self->{real_items} = \@items;
2812
2813 my $row = 0;
2814 for my $item (@items) {
2815 CFClient::Item::update_widgets $item;
2816
2817 $self->{scrolled}->add (0, $row, $item->{face_widget});
2818 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2819 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2820
2821 $row++;
2822 }
2823} 2783}
2824 2784
2825############################################################################# 2785#############################################################################
2826 2786
2827package CFClient::UI::Menu; 2787package CFClient::UI::Menu;
3004 $self->SUPER::reconfigure; 2964 $self->SUPER::reconfigure;
3005} 2965}
3006 2966
3007############################################################################# 2967#############################################################################
3008 2968
3009package CFClient::UI::Root; 2969package CFClient::UI::Inventory;
3010 2970
3011our @ISA = CFClient::UI::Container::; 2971our @ISA = CFClient::UI::ScrolledWindow::;
3012
3013use CFClient::OpenGL;
3014 2972
3015sub new { 2973sub new {
3016 my $class = shift; 2974 my $class = shift;
3017 2975
3018 my $self = $class->SUPER::new ( 2976 my $self = $class->SUPER::new (
3019 visible => 1, 2977 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
3020 @_, 2978 @_,
3021 ); 2979 );
3022 2980
3023 Scalar::Util::weaken ($self->{root} = $self);
3024
3025 $self 2981 $self
3026} 2982}
3027 2983
3028sub size_request { 2984sub set_items {
3029 my ($self) = @_; 2985 my ($self, $items) = @_;
3030 2986
3031 ($self->{w}, $self->{h}) 2987 $self->{scrolled}->clear;
3032} 2988 return unless $items;
3033 2989
3034sub _to_pixel { 2990 my @items = sort {
3035 my ($coord, $size, $max) = @_; 2991 ($a->{type} <=> $b->{type})
2992 or ($a->{name} cmp $b->{name})
2993 } @$items;
3036 2994
3037 $coord = 2995 $self->{real_items} = \@items;
3038 $coord eq "center" ? ($max - $size) * 0.5
3039 : $coord eq "max" ? $max
3040 : $coord;
3041 2996
3042 $coord = 0 if $coord < 0; 2997 my $row = 0;
3043 $coord = $max - $size if $coord > $max - $size; 2998 for my $item (@items) {
2999 CFClient::Item::update_widgets $item;
3044 3000
3045 int $coord + 0.5 3001 $self->{scrolled}->add (0, $row, $item->{face_widget});
3046} 3002 $self->{scrolled}->add (1, $row, $item->{desc_widget});
3003 $self->{scrolled}->add (2, $row, $item->{weight_widget});
3047 3004
3048sub size_allocate { 3005 $row++;
3049 my ($self, $w, $h) = @_;
3050
3051 for my $child ($self->children) {
3052 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3053
3054 $X = $child->{force_x} if exists $child->{force_x};
3055 $Y = $child->{force_y} if exists $child->{force_y};
3056
3057 $X = _to_pixel $X, $W, $self->{w};
3058 $Y = _to_pixel $Y, $H, $self->{h};
3059
3060 $child->configure ($X, $Y, $W, $H);
3061 } 3006 }
3062}
3063
3064sub coord2local {
3065 my ($self, $x, $y) = @_;
3066
3067 ($x, $y)
3068}
3069
3070sub coord2global {
3071 my ($self, $x, $y) = @_;
3072
3073 ($x, $y)
3074}
3075
3076sub update {
3077 my ($self) = @_;
3078
3079 $::WANT_REFRESH++;
3080}
3081
3082sub add {
3083 my ($self, @children) = @_;
3084
3085 $_->{is_toplevel} = 1
3086 for @children;
3087
3088 $self->SUPER::add (@children);
3089}
3090
3091sub remove {
3092 my ($self, @children) = @_;
3093
3094 $self->SUPER::remove (@children);
3095
3096 delete $self->{is_toplevel}
3097 for @children;
3098
3099 while (@children) {
3100 my $w = pop @children;
3101 push @children, $w->children;
3102 $w->set_invisible;
3103 }
3104}
3105
3106sub on_refresh {
3107 my ($self, $id, $cb) = @_;
3108
3109 $self->{refresh_hook}{$id} = $cb;
3110}
3111
3112sub on_post_alloc {
3113 my ($self, $id, $cb) = @_;
3114
3115 $self->{post_alloc_hook}{$id} = $cb;
3116}
3117
3118sub draw {
3119 my ($self) = @_;
3120
3121 while ($self->{refresh_hook}) {
3122 $_->()
3123 for values %{delete $self->{refresh_hook}};
3124 }
3125
3126 if ($self->{realloc}) {
3127 my @queue;
3128
3129 while () {
3130 if ($self->{realloc}) {
3131 #TODO use array-of-depth approach
3132
3133 use sort 'stable';
3134
3135 @queue = sort { $a->{visible} <=> $b->{visible} }
3136 @queue, values %{delete $self->{realloc}};
3137 }
3138
3139 my $widget = pop @queue || last;
3140
3141 $widget->{visible} or last; # do not resize invisible widgets
3142
3143 my ($w, $h) = $widget->size_request;
3144
3145 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3146 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3147
3148 $w = $widget->{force_w} if exists $widget->{force_w};
3149 $h = $widget->{force_h} if exists $widget->{force_h};
3150
3151 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3152 || delete $widget->{force_realloc}) {
3153 $widget->{req_w} = $w;
3154 $widget->{req_h} = $h;
3155
3156 $self->{size_alloc}{$widget+0} = $widget;
3157
3158 if (my $parent = $widget->{parent}) {
3159 $self->{realloc}{$parent+0} = $parent;
3160 #unshift @queue, $parent;
3161 $parent->{force_size_alloc} = 1;
3162 $self->{size_alloc}{$parent+0} = $parent;
3163 }
3164 }
3165
3166 delete $self->{realloc}{$widget+0};
3167 }
3168 }
3169
3170 while (my $size_alloc = delete $self->{size_alloc}) {
3171 my @queue = sort { $b->{visible} <=> $a->{visible} }
3172 values %$size_alloc;
3173
3174 while () {
3175 my $widget = pop @queue || last;
3176
3177 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3178
3179 $w = 0 if $w < 0;
3180 $h = 0 if $h < 0;
3181
3182 $w = int $w + 0.5;
3183 $h = int $h + 0.5;
3184
3185 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3186 $widget->{w} = $w;
3187 $widget->{h} = $h;
3188
3189 $widget->emit (size_allocate => $w, $h);
3190 }
3191 }
3192 }
3193
3194 while ($self->{post_alloc_hook}) {
3195 $_->()
3196 for values %{delete $self->{post_alloc_hook}};
3197 }
3198
3199
3200 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3201 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3202 glClear GL_COLOR_BUFFER_BIT;
3203
3204 glMatrixMode GL_PROJECTION;
3205 glLoadIdentity;
3206 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3207 glMatrixMode GL_MODELVIEW;
3208 glLoadIdentity;
3209
3210 $self->_draw;
3211} 3007}
3212 3008
3213############################################################################# 3009#############################################################################
3214 3010
3215package CFClient::UI::BindEditor; 3011package CFClient::UI::BindEditor;
3386 3182
3387 for (@$cmds) { 3183 for (@$cmds) {
3388 $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox); 3184 $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox);
3389 3185
3390 my $i = $idx; 3186 my $i = $idx;
3187 $hb->add (new CFClient::UI::Label text => $_);
3391 $hb->add (new CFClient::UI::Button 3188 $hb->add (new CFClient::UI::Button
3392 text => "delete", 3189 text => "delete",
3393 tooltip => "Deletes the action from the record", 3190 tooltip => "Deletes the action from the record",
3394 on_activate => sub { 3191 on_activate => sub {
3395 $self->{cmdbox}->remove ($hb); 3192 $self->{cmdbox}->remove ($hb);
3396 $cmds->[$i] = undef; 3193 $cmds->[$i] = undef;
3397 }); 3194 });
3398 3195
3399 $hb->add (new CFClient::UI::Label text => $_);
3400 3196
3401 $idx++ 3197 $idx++
3402 } 3198 }
3403} 3199}
3404
3405 3200
3406############################################################################# 3201#############################################################################
3407 3202
3408package CFClient::UI::SpellList; 3203package CFClient::UI::SpellList;
3409 3204
3435 text => $spell->{name}, 3230 text => $spell->{name},
3436 can_hover => 1, 3231 can_hover => 1,
3437 can_events => 1, 3232 can_events => 1,
3438 tooltip => $spell->{message}, 3233 tooltip => $spell->{message},
3439 expand => 1); 3234 expand => 1);
3235
3236 $self->{spellbox}->add (2, $self->{tbl_idx}, new CFClient::UI::Label
3237 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3238 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3239 expand => 1);
3240
3440 $self->{spellbox}->add (2, $self->{tbl_idx}++, new CFClient::UI::Button 3241 $self->{spellbox}->add (3, $self->{tbl_idx}++, new CFClient::UI::Button
3441 text => "bind to key", 3242 text => "bind to key",
3442 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }); 3243 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) });
3443} 3244}
3444 3245
3445sub rebuild_spell_list { 3246sub rebuild_spell_list {
3454 $self->rebuild_spell_list; 3255 $self->rebuild_spell_list;
3455} 3256}
3456 3257
3457############################################################################# 3258#############################################################################
3458 3259
3260package CFClient::UI::Root;
3261
3262our @ISA = CFClient::UI::Container::;
3263
3264use CFClient::OpenGL;
3265
3266sub new {
3267 my $class = shift;
3268
3269 my $self = $class->SUPER::new (
3270 visible => 1,
3271 @_,
3272 );
3273
3274 Scalar::Util::weaken ($self->{root} = $self);
3275
3276 $self
3277}
3278
3279sub size_request {
3280 my ($self) = @_;
3281
3282 ($self->{w}, $self->{h})
3283}
3284
3285sub _to_pixel {
3286 my ($coord, $size, $max) = @_;
3287
3288 $coord =
3289 $coord eq "center" ? ($max - $size) * 0.5
3290 : $coord eq "max" ? $max
3291 : $coord;
3292
3293 $coord = 0 if $coord < 0;
3294 $coord = $max - $size if $coord > $max - $size;
3295
3296 int $coord + 0.5
3297}
3298
3299sub size_allocate {
3300 my ($self, $w, $h) = @_;
3301
3302 for my $child ($self->children) {
3303 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3304
3305 $X = $child->{force_x} if exists $child->{force_x};
3306 $Y = $child->{force_y} if exists $child->{force_y};
3307
3308 $X = _to_pixel $X, $W, $self->{w};
3309 $Y = _to_pixel $Y, $H, $self->{h};
3310
3311 $child->configure ($X, $Y, $W, $H);
3312 }
3313}
3314
3315sub coord2local {
3316 my ($self, $x, $y) = @_;
3317
3318 ($x, $y)
3319}
3320
3321sub coord2global {
3322 my ($self, $x, $y) = @_;
3323
3324 ($x, $y)
3325}
3326
3327sub update {
3328 my ($self) = @_;
3329
3330 $::WANT_REFRESH++;
3331}
3332
3333sub add {
3334 my ($self, @children) = @_;
3335
3336 $_->{is_toplevel} = 1
3337 for @children;
3338
3339 $self->SUPER::add (@children);
3340}
3341
3342sub remove {
3343 my ($self, @children) = @_;
3344
3345 $self->SUPER::remove (@children);
3346
3347 delete $self->{is_toplevel}
3348 for @children;
3349
3350 while (@children) {
3351 my $w = pop @children;
3352 push @children, $w->children;
3353 $w->set_invisible;
3354 }
3355}
3356
3357sub on_refresh {
3358 my ($self, $id, $cb) = @_;
3359
3360 $self->{refresh_hook}{$id} = $cb;
3361}
3362
3363sub on_post_alloc {
3364 my ($self, $id, $cb) = @_;
3365
3366 $self->{post_alloc_hook}{$id} = $cb;
3367}
3368
3369sub draw {
3370 my ($self) = @_;
3371
3372 while ($self->{refresh_hook}) {
3373 $_->()
3374 for values %{delete $self->{refresh_hook}};
3375 }
3376
3377 if ($self->{realloc}) {
3378 my %queue;
3379 my @queue;
3380 my $widget;
3381
3382 outer:
3383 while () {
3384 if (my $realloc = delete $self->{realloc}) {
3385 for $widget (values %$realloc) {
3386 $widget->{visible} or next; # do not resize invisible widgets
3387
3388 $queue{$widget+0}++ and next; # duplicates are common
3389
3390 push @{ $queue[$widget->{visible}] }, $widget;
3391 }
3392 }
3393
3394 while () {
3395 @queue or last outer;
3396
3397 $widget = pop @{ $queue[-1] || [] }
3398 and last;
3399
3400 pop @queue;
3401 }
3402
3403 delete $queue{$widget+0};
3404
3405 my ($w, $h) = $widget->size_request;
3406
3407 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3408 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3409
3410 $w = $widget->{force_w} if exists $widget->{force_w};
3411 $h = $widget->{force_h} if exists $widget->{force_h};
3412
3413 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3414 || delete $widget->{force_realloc}) {
3415 $widget->{req_w} = $w;
3416 $widget->{req_h} = $h;
3417
3418 $self->{size_alloc}{$widget+0} = $widget;
3419
3420 if (my $parent = $widget->{parent}) {
3421 $self->{realloc}{$parent+0} = $parent
3422 unless $queue{$parent+0};
3423
3424 $parent->{force_size_alloc} = 1;
3425 $self->{size_alloc}{$parent+0} = $parent;
3426 }
3427 }
3428
3429 delete $self->{realloc}{$widget+0};
3430 }
3431 }
3432
3433 while (my $size_alloc = delete $self->{size_alloc}) {
3434 my @queue = sort { $b->{visible} <=> $a->{visible} }
3435 values %$size_alloc;
3436
3437 while () {
3438 my $widget = pop @queue || last;
3439
3440 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3441
3442 $w = 0 if $w < 0;
3443 $h = 0 if $h < 0;
3444
3445 $w = int $w + 0.5;
3446 $h = int $h + 0.5;
3447
3448 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3449 $widget->{old_w} = $widget->{w};
3450 $widget->{old_h} = $widget->{h};
3451
3452 $widget->{w} = $w;
3453 $widget->{h} = $h;
3454
3455 $widget->emit (size_allocate => $w, $h);
3456 }
3457 }
3458 }
3459
3460 while ($self->{post_alloc_hook}) {
3461 $_->()
3462 for values %{delete $self->{post_alloc_hook}};
3463 }
3464
3465
3466 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3467 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3468 glClear GL_COLOR_BUFFER_BIT;
3469
3470 glMatrixMode GL_PROJECTION;
3471 glLoadIdentity;
3472 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3473 glMatrixMode GL_MODELVIEW;
3474 glLoadIdentity;
3475
3476 $self->_draw;
3477}
3478
3479#############################################################################
3480
3459package CFClient::UI; 3481package CFClient::UI;
3460 3482
3461$ROOT = new CFClient::UI::Root; 3483$ROOT = new CFClient::UI::Root;
3462$TOOLTIP = new CFClient::UI::Tooltip z => 900; 3484$TOOLTIP = new CFClient::UI::Tooltip z => 900;
3463 3485

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines