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.320 by root, Tue Jul 4 23:23:32 2006 UTC vs.
Revision 1.331 by root, Sun Jul 23 04:46:02 2006 UTC

79sub feed_sdl_key_up_event { 79sub feed_sdl_key_up_event {
80 $FOCUS->emit (key_up => $_[0]) 80 $FOCUS->emit (key_up => $_[0])
81 if $FOCUS; 81 if $FOCUS;
82} 82}
83 83
84sub check_hover {
85 my ($widget) = @_;
86
87 if ($widget != $HOVER) {
88 my $hover = $HOVER; $HOVER = $widget;
89
90 $hover->update if $hover && $hover->{can_hover};
91 $HOVER->update if $HOVER && $HOVER->{can_hover};
92
93 $TOOLTIP_WATCHER->start;
94 }
95}
96
84sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
85 my ($ev) = @_; 98 my ($ev) = @_;
86 my ($x, $y) = ($ev->{x}, $ev->{y}); 99 my ($x, $y) = ($ev->{x}, $ev->{y});
87 100
88 if (!$BUTTON_STATE) { 101 unless ($BUTTON_STATE) {
89 my $widget = $ROOT->find_widget ($x, $y); 102 my $widget = $ROOT->find_widget ($x, $y);
90 103
91 $GRAB = $widget; 104 $GRAB = $widget;
92 $GRAB->update if $GRAB; 105 $GRAB->update if $GRAB;
93 106
94 $TOOLTIP_WATCHER->cb->(); 107 $TOOLTIP_WATCHER->cb->();
95 } 108 }
96 109
97 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 110 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
98 111
99 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) 112 if ($GRAB) {
100 if $GRAB; 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel
115 $ev->{dx} = 0;
116 $ev->{dy} = $ev->{button} * 2 - 9;
117 $GRAB->emit (mouse_wheel => $ev);
118 } else {
119 $GRAB->emit (button_down => $ev)
120 }
121 }
101} 122}
102 123
103sub feed_sdl_button_up_event { 124sub feed_sdl_button_up_event {
104 my ($ev) = @_; 125 my ($ev) = @_;
105 my ($x, $y) = ($ev->{x}, $ev->{y});
106 126
107 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 127 my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y});
108 128
109 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 129 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
110 130
111 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) 131 $GRAB->emit (button_up => $ev)
112 if $GRAB; 132 if $GRAB && $ev->{button} != 4 && $ev->{button} != 5;
113 133
114 if (!$BUTTON_STATE) { 134 unless ($BUTTON_STATE) {
115 my $grab = $GRAB; undef $GRAB; 135 my $grab = $GRAB; undef $GRAB;
116 $grab->update if $grab; 136 $grab->update if $grab;
117 $GRAB->update if $GRAB; 137 $GRAB->update if $GRAB;
118 138
139 check_hover $widget;
119 $TOOLTIP_WATCHER->cb->(); 140 $TOOLTIP_WATCHER->cb->();
120 } 141 }
121} 142}
122 143
123sub feed_sdl_motion_event { 144sub feed_sdl_motion_event {
124 my ($ev) = @_; 145 my ($ev) = @_;
125 my ($x, $y) = ($ev->{x}, $ev->{y}); 146 my ($x, $y) = ($ev->{x}, $ev->{y});
126 147
127 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 148 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
128 149
129 if ($widget != $HOVER) { 150 check_hover $widget;
130 my $hover = $HOVER; $HOVER = $widget;
131 151
132 $hover->update if $hover && $hover->{can_hover}; 152 $HOVER->emit (mouse_motion => $ev)
133 $HOVER->update if $HOVER && $HOVER->{can_hover};
134
135 $TOOLTIP_WATCHER->start;
136 }
137
138 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
139 if $HOVER; 153 if $HOVER;
140} 154}
141 155
142# convert position array to integers 156# convert position array to integers
143sub harmonize { 157sub harmonize {
189 203
190 } 204 }
191 } 205 }
192 206
193 reconfigure_widgets; 207 reconfigure_widgets;
208}
209
210#############################################################################
211
212package CFClient::UI::Event;
213
214sub xy {
215 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
194} 216}
195 217
196############################################################################# 218#############################################################################
197 219
198package CFClient::UI::Base; 220package CFClient::UI::Base;
407 429
408# translate global coordinates to local coordinate system 430# translate global coordinates to local coordinate system
409sub coord2local { 431sub coord2local {
410 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
411 433
434 Carp::confess unless $self->{parent};#d#
435
412 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 436 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
413} 437}
414 438
415# translate local coordinates to global coordinate system 439# translate local coordinates to global coordinate system
416sub coord2global { 440sub coord2global {
452 476
453 $FOCUS->emit ("focus_out") if $FOCUS; 477 $FOCUS->emit ("focus_out") if $FOCUS;
454 $self->emit ("focus_in"); 478 $self->emit ("focus_in");
455} 479}
456 480
457sub invoke_mouse_motion { 1 } 481sub invoke_mouse_motion { 0 }
458sub invoke_button_up { 1 } 482sub invoke_button_up { 0 }
459sub invoke_key_down { 1 } 483sub invoke_key_down { 0 }
460sub invoke_key_up { 1 } 484sub invoke_key_up { 0 }
485sub invoke_mouse_wheel { 0 }
461 486
462sub invoke_button_down { 487sub invoke_button_down {
463 my ($self, $ev, $x, $y) = @_; 488 my ($self, $ev, $x, $y) = @_;
464 489
465 $self->grab_focus; 490 $self->grab_focus;
466 491
467 1 492 0
468} 493}
469 494
470sub connect { 495sub connect {
471 my ($self, $signal, $cb) = @_; 496 my ($self, $signal, $cb) = @_;
472 497
473 push @{ $self->{signal_cb}{$signal} }, $cb; 498 push @{ $self->{signal_cb}{$signal} }, $cb;
474} 499}
475 500
501my %has_coords = (
502 button_down => 1,
503 button_up => 1,
504 mouse_motion => 1,
505 mouse_wheel => 1,
506);
507
476sub emit { 508sub emit {
477 my ($self, $signal, @args) = @_; 509 my ($self, $signal, @args) = @_;
478 510
511 my @append
512 = $has_coords{$signal}
513 ? $args[0]->xy ($self)
514 : ();
515
516 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
517
479 #d##TODO# stop propagating at first true, do not use sum 518 #d##TODO# stop propagating at first true, do not use sum
480 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before 519 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before
481 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure 520 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure
482 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 521 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
483} 522}
484 523
485sub find_widget { 524sub find_widget {
486 my ($self, $x, $y) = @_; 525 my ($self, $x, $y) = @_;
487 526
587} 626}
588 627
589sub DESTROY { 628sub DESTROY {
590 my ($self) = @_; 629 my ($self) = @_;
591 630
631 return if CFClient::in_destruct;
632
592 delete $WIDGET{$self+0}; 633 delete $WIDGET{$self+0};
593 634
594 eval { $self->destroy }; 635 eval { $self->destroy };
595 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
596} 637}
975 $self->{vp}->set_offset (0, $_[1]); 1016 $self->{vp}->set_offset (0, $_[1]);
976 }, 1017 },
977 ; 1018 ;
978 1019
979 $self = $class->SUPER::new ( 1020 $self = $class->SUPER::new (
980 vp => (new CFClient::UI::ViewPort expand => 1), 1021 vp => (new CFClient::UI::ViewPort expand => 1),
1022 can_events => 1,
981 slider => $slider, 1023 slider => $slider,
982 %arg, 1024 %arg,
983 ); 1025 );
984 1026
985 $self->SUPER::add ($self->{vp}, $self->{slider}); 1027 $self->SUPER::add ($self->{vp}, $self->{slider});
986 $self->add ($child) if $child; 1028 $self->add ($child) if $child;
992 1034
993sub add { 1035sub add {
994 my ($self, $widget) = @_; 1036 my ($self, $widget) = @_;
995 1037
996 $self->{vp}->add ($self->{child} = $widget); 1038 $self->{vp}->add ($self->{child} = $widget);
1039}
1040
1041sub invoke_mouse_wheel {
1042 my ($self, $ev) = @_;
1043
1044 return 0 unless $ev->{dy}; # only vertical movements
1045
1046 $self->{slider}->emit (mouse_wheel => $ev);
1047
1048 1
997} 1049}
998 1050
999sub update_slider { 1051sub update_slider {
1000 my ($self) = @_; 1052 my ($self) = @_;
1001 1053
1080 my $self = $class->SUPER::new ( 1132 my $self = $class->SUPER::new (
1081 bg => [1, 1, 1, 1], 1133 bg => [1, 1, 1, 1],
1082 border_bg => [1, 1, 1, 1], 1134 border_bg => [1, 1, 1, 1],
1083 border => 0.6, 1135 border => 0.6,
1084 can_events => 1, 1136 can_events => 1,
1085 min_w => 16, 1137 min_w => 64,
1086 min_h => 16, 1138 min_h => 32,
1087 %arg, 1139 %arg,
1088 ); 1140 );
1089 1141
1090 $self->{title_widget} = new CFClient::UI::Label 1142 $self->{title_widget} = new CFClient::UI::Label
1091 align => 0, 1143 align => 0,
1536 #font => default_font 1588 #font => default_font
1537 #text => initial text 1589 #text => initial text
1538 #markup => initial narkup 1590 #markup => initial narkup
1539 #max_w => maximum pixel width 1591 #max_w => maximum pixel width
1540 ellipsise => 3, # end 1592 ellipsise => 3, # end
1541 layout => (new CFClient::Layout 2), 1593 layout => (new CFClient::Layout),
1542 fontsize => 1, 1594 fontsize => 1,
1543 align => -1, 1595 align => -1,
1544 valign => -1, 1596 valign => -1,
1545 padding_x => 2, 1597 padding_x => 2,
1546 padding_y => 2, 1598 padding_y => 2,
1547 can_events => 0, 1599 can_events => 0,
1548 %arg 1600 %arg
1549 ); 1601 );
1550 1602
1551 if (exists $self->{template}) { 1603 if (exists $self->{template}) {
1552 my $layout = new CFClient::Layout 2; 1604 my $layout = new CFClient::Layout;
1553 $layout->set_text (delete $self->{template}); 1605 $layout->set_text (delete $self->{template});
1554 $self->{template} = $layout; 1606 $self->{template} = $layout;
1555 } 1607 }
1556 1608
1557 if (exists $self->{markup}) { 1609 if (exists $self->{markup}) {
1627 1679
1628 my ($w, $h) = $self->{layout}->size; 1680 my ($w, $h) = $self->{layout}->size;
1629 1681
1630 if (exists $self->{template}) { 1682 if (exists $self->{template}) {
1631 $self->{template}->set_font ($self->{font}) if $self->{font}; 1683 $self->{template}->set_font ($self->{font}) if $self->{font};
1684 $self->{template}->set_width ($self->{max_w} || -1);
1632 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1685 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1633 1686
1634 my ($w2, $h2) = $self->{template}->size; 1687 my ($w2, $h2) = $self->{template}->size;
1635 1688
1636 $w = List::Util::max $w, $w2; 1689 $w = List::Util::max $w, $w2;
1688 $self->{layout}->set_width ($self->{w}); 1741 $self->{layout}->set_width ($self->{w});
1689 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1742 $self->{layout}->set_ellipsise ($self->{ellipsise});
1690 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1743 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1691 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1744 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1692 1745
1693 $self->{size_req} 1746 [$self->{layout}->size]
1694 }; 1747 };
1695 1748
1696 unless (exists $self->{ox}) { 1749 unless (exists $self->{ox}) {
1697 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1750 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1698 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 1751 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x}
1825 1878
1826 my $idx = $self->{layout}->xy_to_index ($x, $y); 1879 my $idx = $self->{layout}->xy_to_index ($x, $y);
1827 1880
1828 # byte-index to char-index 1881 # byte-index to char-index
1829 my $text = $self->{text}; 1882 my $text = $self->{text};
1830 utf8::encode $text; 1883 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1831 $self->{cursor} = length substr $text, 0, $idx; 1884 $self->{cursor} = length $text;
1832 1885
1833 $self->_set_text ($self->{text}); 1886 $self->_set_text ($self->{text});
1834 $self->update; 1887 $self->update;
1835 1888
1836 1 1889 1
2115 my $self = $class->SUPER::new ( 2168 my $self = $class->SUPER::new (
2116 can_events => 0, 2169 can_events => 0,
2117 @_, 2170 @_,
2118 ); 2171 );
2119 2172
2120 $self->{path} 2173 $self->{path} || $self->{tex}
2121 or Carp::croak "required attribute 'path' not set"; 2174 or Carp::croak "'path' or 'tex' attributes required";
2122 2175
2123 $self->{tex} = $texture_cache{$self->{path}} ||= 2176 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2124 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; 2177 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2125 2178
2126 Scalar::Util::weaken $texture_cache{$self->{path}}; 2179 Scalar::Util::weaken $texture_cache{$self->{path}};
2127 2180
2128 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2181 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2448 } 2501 }
2449 2502
2450 1 2503 1
2451} 2504}
2452 2505
2506sub invoke_mouse_wheel {
2507 my ($self, $ev) = @_;
2508
2509 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2510
2511 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1);
2512
2513 ! ! $delta
2514}
2515
2453sub update { 2516sub update {
2454 my ($self) = @_; 2517 my ($self) = @_;
2455 2518
2456 delete $self->{knob_w}; 2519 delete $self->{knob_w};
2457 $self->SUPER::update; 2520 $self->SUPER::update;
2556sub new { 2619sub new {
2557 my $class = shift; 2620 my $class = shift;
2558 2621
2559 my $self = $class->SUPER::new ( 2622 my $self = $class->SUPER::new (
2560 fontsize => 1, 2623 fontsize => 1,
2561 can_events => 0, 2624 can_events => 1,
2562 indent => 0, 2625 indent => 0,
2563 #font => default_font 2626 #font => default_font
2564 @_, 2627 @_,
2565 2628
2566 layout => (new CFClient::Layout 2), 2629 layout => (new CFClient::Layout),
2567 par => [], 2630 par => [],
2568 height => 0, 2631 height => 0,
2569 children => [ 2632 children => [
2570 (new CFClient::UI::Empty expand => 1), 2633 (new CFClient::UI::Empty expand => 1),
2571 (new CFClient::UI::Slider vertical => 1), 2634 (new CFClient::UI::Slider vertical => 1),
2606 2669
2607 $self->reflow; 2670 $self->reflow;
2608 2671
2609 local $self->{children} = [$empty, $slider]; 2672 local $self->{children} = [$empty, $slider];
2610 $self->SUPER::invoke_size_allocate ($w, $h) 2673 $self->SUPER::invoke_size_allocate ($w, $h)
2674}
2675
2676sub invoke_mouse_wheel {
2677 my ($self, $ev) = @_;
2678
2679 return 0 unless $ev->{dy}; # only vertical movements
2680
2681 $self->{children}[1]->emit (mouse_wheel => $ev);
2682
2683 1
2611} 2684}
2612 2685
2613sub get_layout { 2686sub get_layout {
2614 my ($self, $para) = @_; 2687 my ($self, $para) = @_;
2615 2688
3062 for my $item (@{ $self->{items} }) { 3135 for my $item (@{ $self->{items} }) {
3063 my ($widget, $cb, $tooltip) = @$item; 3136 my ($widget, $cb, $tooltip) = @$item;
3064 3137
3065 # handle various types of items, only text for now 3138 # handle various types of items, only text for now
3066 if (!ref $widget) { 3139 if (!ref $widget) {
3140 if ($widget =~ /\t/) {
3141 my ($left, $right) = split /\t/, $widget, 2;
3142
3143 $widget = new CFClient::UI::HBox
3144 can_hover => 1,
3145 can_events => 1,
3146 tooltip => $tooltip,
3147 children => [
3148 (new CFClient::UI::Label markup => $left, expand => 1),
3149 (new CFClient::UI::Label markup => $right, align => +1),
3150 ],
3151 ;
3152
3153 } else {
3067 $widget = new CFClient::UI::Label 3154 $widget = new CFClient::UI::Label
3068 can_hover => 1, 3155 can_hover => 1,
3069 can_events => 1, 3156 can_events => 1,
3070 markup => $widget, 3157 markup => $widget,
3071 tooltip => $tooltip 3158 tooltip => $tooltip;
3159 }
3072 } 3160 }
3073 3161
3074 $self->{item}{$widget} = $item; 3162 $self->{item}{$widget} = $item;
3075 3163
3076 $self->{vbox}->add ($widget); 3164 $self->{vbox}->add ($widget);
3798 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 3886 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3799 3887
3800 $w = 0 if $w < 0; 3888 $w = 0 if $w < 0;
3801 $h = 0 if $h < 0; 3889 $h = 0 if $h < 0;
3802 3890
3891 $w = max $widget->{min_w}, $w;
3892 $h = max $widget->{min_h}, $h;
3893
3894 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3895 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3896
3803 $w = int $w + 0.5; 3897 $w = int $w + 0.5;
3804 $h = int $h + 0.5; 3898 $h = int $h + 0.5;
3805 3899
3806 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 3900 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3807 $widget->{old_w} = $widget->{w}; 3901 $widget->{old_w} = $widget->{w};
3837 ($draw_x, $draw_y, $draw_w, $draw_h) = 3931 ($draw_x, $draw_y, $draw_w, $draw_h) =
3838 (0, 0, $self->{w}, $self->{h}); 3932 (0, 0, $self->{w}, $self->{h});
3839 } 3933 }
3840 3934
3841 $self->_draw; 3935 $self->_draw;
3842
3843 #TODO#d# display texture cache
3844 {
3845 glEnable GL_TEXTURE_2D;
3846 glBindTexture GL_TEXTURE_2D, 41;
3847 glColor 1, 1, 1, 1;
3848 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
3849 glEnable GL_BLEND;
3850 glBlendFunc GL_SRC_ALPHA, GL_ZERO;
3851 glBegin GL_QUADS;
3852 glTexCoord 0,1; glVertex 0,0;
3853 glTexCoord 1,1; glVertex 255,0;
3854 glTexCoord 1,0; glVertex 255,255;
3855 glTexCoord 0,0; glVertex 0,255;
3856 glEnd;
3857 glDisable GL_BLEND;
3858 glDisable GL_TEXTURE_2D;
3859 }
3860
3861} 3936}
3862 3937
3863############################################################################# 3938#############################################################################
3864 3939
3865package CFClient::UI; 3940package CFClient::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines