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.316 by root, Sun Jul 2 13:57:58 2006 UTC vs.
Revision 1.333 by root, Sun Jul 23 11:09:58 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 {
417 my ($self, $x, $y) = @_; 441 my ($self, $x, $y) = @_;
418 442
443 Carp::confess unless $self->{parent};#d#
444
419 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 445 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
420} 446}
421 447
422sub invoke_focus_in { 448sub invoke_focus_in {
423 my ($self) = @_; 449 my ($self) = @_;
424 450
425 return if $FOCUS == $self; 451 return if $FOCUS == $self;
426 return unless $self->{can_focus}; 452 return unless $self->{can_focus};
427 453
428 my $focus = $FOCUS; $FOCUS = $self; 454 $FOCUS = $self;
429 455
430 $focus->update if $focus; 456 $self->update;
431 $FOCUS->update;
432 457
433 0 458 0
434} 459}
435 460
436sub invoke_focus_out { 461sub invoke_focus_out {
437 my ($self) = @_; 462 my ($self) = @_;
438 463
439 return unless $FOCUS == $self; 464 return unless $FOCUS == $self;
440 465
441 my $focus = $FOCUS; undef $FOCUS; 466 undef $FOCUS;
442 467
443 $focus->update if $focus; #? 468 $self->update;
444 469
445 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus 470 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus
446 unless $FOCUS; 471 unless $FOCUS;
447 472
448 0 473 0
449} 474}
450 475
451sub grab_focus { 476sub grab_focus {
452 my ($self) = @_; 477 my ($self) = @_;
453 478
479 $FOCUS->emit ("focus_out") if $FOCUS;
454 $self->emit ("focus_in"); 480 $self->emit ("focus_in");
455} 481}
456 482
457sub invoke_mouse_motion { 1 } 483sub invoke_mouse_motion { 0 }
458sub invoke_button_up { 1 } 484sub invoke_button_up { 0 }
459sub invoke_key_down { 1 } 485sub invoke_key_down { 0 }
460sub invoke_key_up { 1 } 486sub invoke_key_up { 0 }
487sub invoke_mouse_wheel { 0 }
461 488
462sub invoke_button_down { 489sub invoke_button_down {
463 my ($self, $ev, $x, $y) = @_; 490 my ($self, $ev, $x, $y) = @_;
464 491
465 $self->grab_focus; 492 $self->grab_focus;
466 493
467 1 494 0
468} 495}
469 496
470sub connect { 497sub connect {
471 my ($self, $signal, $cb) = @_; 498 my ($self, $signal, $cb) = @_;
472 499
473 push @{ $self->{signal_cb}{$signal} }, $cb; 500 push @{ $self->{signal_cb}{$signal} }, $cb;
501
502 defined wantarray and CFClient::guard {
503 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
504 @{ $self->{signal_cb}{$signal} };
505 }
474} 506}
507
508my %has_coords = (
509 button_down => 1,
510 button_up => 1,
511 mouse_motion => 1,
512 mouse_wheel => 1,
513);
475 514
476sub emit { 515sub emit {
477 my ($self, $signal, @args) = @_; 516 my ($self, $signal, @args) = @_;
478 517
518 # I do not really like this solution, but I dislike duplication
519 # and needlessly verbose code, too.
520 my @append
521 = $has_coords{$signal}
522 ? $args[0]->xy ($self)
523 : ();
524
525 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
526
479 #d##TODO# stop propagating at first true, do not use sum 527 #d##TODO# stop propagating at first true, do not use sum
480 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before 528 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before
481 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure 529 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure
482 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 530 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
483} 531}
484 532
485sub find_widget { 533sub find_widget {
486 my ($self, $x, $y) = @_; 534 my ($self, $x, $y) = @_;
487 535
587} 635}
588 636
589sub DESTROY { 637sub DESTROY {
590 my ($self) = @_; 638 my ($self) = @_;
591 639
640 return if CFClient::in_destruct;
641
592 delete $WIDGET{$self+0}; 642 delete $WIDGET{$self+0};
593 643
594 eval { $self->destroy }; 644 eval { $self->destroy };
595 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 645 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
596} 646}
679 729
680 $self->add (@$children) 730 $self->add (@$children)
681 if $children; 731 if $children;
682 732
683 $self 733 $self
734}
735
736sub realloc {
737 my ($self) = @_;
738
739 $self->{force_realloc} = 1;
740 $self->{force_size_alloc} = 1;
741 $self->SUPER::realloc;
684} 742}
685 743
686sub add { 744sub add {
687 my ($self, @widgets) = @_; 745 my ($self, @widgets) = @_;
688 746
975 $self->{vp}->set_offset (0, $_[1]); 1033 $self->{vp}->set_offset (0, $_[1]);
976 }, 1034 },
977 ; 1035 ;
978 1036
979 $self = $class->SUPER::new ( 1037 $self = $class->SUPER::new (
980 vp => (new CFClient::UI::ViewPort expand => 1), 1038 vp => (new CFClient::UI::ViewPort expand => 1),
1039 can_events => 1,
981 slider => $slider, 1040 slider => $slider,
982 %arg, 1041 %arg,
983 ); 1042 );
984 1043
985 $self->SUPER::add ($self->{vp}, $self->{slider}); 1044 $self->SUPER::add ($self->{vp}, $self->{slider});
986 $self->add ($child) if $child; 1045 $self->add ($child) if $child;
992 1051
993sub add { 1052sub add {
994 my ($self, $widget) = @_; 1053 my ($self, $widget) = @_;
995 1054
996 $self->{vp}->add ($self->{child} = $widget); 1055 $self->{vp}->add ($self->{child} = $widget);
1056}
1057
1058sub invoke_mouse_wheel {
1059 my ($self, $ev) = @_;
1060
1061 return 0 unless $ev->{dy}; # only vertical movements
1062
1063 $self->{slider}->emit (mouse_wheel => $ev);
1064
1065 1
997} 1066}
998 1067
999sub update_slider { 1068sub update_slider {
1000 my ($self) = @_; 1069 my ($self) = @_;
1001 1070
1080 my $self = $class->SUPER::new ( 1149 my $self = $class->SUPER::new (
1081 bg => [1, 1, 1, 1], 1150 bg => [1, 1, 1, 1],
1082 border_bg => [1, 1, 1, 1], 1151 border_bg => [1, 1, 1, 1],
1083 border => 0.6, 1152 border => 0.6,
1084 can_events => 1, 1153 can_events => 1,
1085 min_w => 16, 1154 min_w => 64,
1086 min_h => 16, 1155 min_h => 32,
1087 %arg, 1156 %arg,
1088 ); 1157 );
1089 1158
1090 $self->{title_widget} = new CFClient::UI::Label 1159 $self->{title_widget} = new CFClient::UI::Label
1091 align => 0, 1160 align => 0,
1096 1165
1097 if ($self->{has_close_button}) { 1166 if ($self->{has_close_button}) {
1098 $self->{close_button} = 1167 $self->{close_button} =
1099 new CFClient::UI::ImageButton 1168 new CFClient::UI::ImageButton
1100 path => 'x1_close.png', 1169 path => 'x1_close.png',
1101 on_activate => sub { $self->hide }; 1170 on_activate => sub { $self->emit ("delete") };
1102 1171
1103 $self->CFClient::UI::Container::add ($self->{close_button}); 1172 $self->CFClient::UI::Container::add ($self->{close_button});
1104 } 1173 }
1105 1174
1106 $self 1175 $self
1152 $self->child->configure ($border, $border, $w, $h); 1221 $self->child->configure ($border, $border, $w, $h);
1153 1222
1154 $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border) 1223 $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border)
1155 if $self->{close_button}; 1224 if $self->{close_button};
1156 1225
1226 1
1227}
1228
1229sub invoke_delete {
1230 my ($self) = @_;
1231
1232 $self->hide;
1233
1157 1 1234 1
1158} 1235}
1159 1236
1160sub invoke_button_down { 1237sub invoke_button_down {
1161 my ($self, $ev, $x, $y) = @_; 1238 my ($self, $ev, $x, $y) = @_;
1287sub children { 1364sub children {
1288 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1365 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1289} 1366}
1290 1367
1291sub add { 1368sub add {
1292 my ($self, $x, $y, $child) = @_; 1369 my ($self) = shift;
1293 1370
1371 while (@_) {
1372 my ($x, $y, $child) = splice @_, 0, 3, ();
1294 $child->set_parent ($self); 1373 $child->set_parent ($self);
1295 $self->{children}[$y][$x] = $child; 1374 $self->{children}[$y][$x] = $child;
1375 }
1296 1376
1377 $self->{force_realloc} = 1;
1378 $self->{force_size_alloc} = 1;
1297 $self->realloc; 1379 $self->realloc;
1298} 1380}
1299 1381
1300sub remove { 1382sub remove {
1301 my ($self, $child) = @_; 1383 my ($self, $child) = @_;
1583 my ($self, $text) = @_; 1665 my ($self, $text) = @_;
1584 1666
1585 return if $self->{text} eq "T$text"; 1667 return if $self->{text} eq "T$text";
1586 $self->{text} = "T$text"; 1668 $self->{text} = "T$text";
1587 1669
1588 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1589 $self->{layout}->set_text ($text); 1670 $self->{layout}->set_text ($text);
1590 1671
1591 delete $self->{size_req}; 1672 delete $self->{size_req};
1592 $self->realloc; 1673 $self->realloc;
1593 $self->update; 1674 $self->update;
1599 return if $self->{text} eq "M$markup"; 1680 return if $self->{text} eq "M$markup";
1600 $self->{text} = "M$markup"; 1681 $self->{text} = "M$markup";
1601 1682
1602 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1683 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1603 1684
1604 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1605 $self->{layout}->set_markup ($markup); 1685 $self->{layout}->set_markup ($markup);
1606 1686
1607 delete $self->{size_req}; 1687 delete $self->{size_req};
1608 $self->realloc; 1688 $self->realloc;
1609 $self->update; 1689 $self->update;
1621 1701
1622 my ($w, $h) = $self->{layout}->size; 1702 my ($w, $h) = $self->{layout}->size;
1623 1703
1624 if (exists $self->{template}) { 1704 if (exists $self->{template}) {
1625 $self->{template}->set_font ($self->{font}) if $self->{font}; 1705 $self->{template}->set_font ($self->{font}) if $self->{font};
1706 $self->{template}->set_width ($self->{max_w} || -1);
1626 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1707 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1627 1708
1628 my ($w2, $h2) = $self->{template}->size; 1709 my ($w2, $h2) = $self->{template}->size;
1629 1710
1630 $w = List::Util::max $w, $w2; 1711 $w = List::Util::max $w, $w2;
1664 1745
1665sub reconfigure { 1746sub reconfigure {
1666 my ($self) = @_; 1747 my ($self) = @_;
1667 1748
1668 delete $self->{size_req}; 1749 delete $self->{size_req};
1750 delete $self->{texture};
1669 1751
1670 $self->SUPER::reconfigure; 1752 $self->SUPER::reconfigure;
1671} 1753}
1672 1754
1673sub _draw { 1755sub _draw {
1674 my ($self) = @_; 1756 my ($self) = @_;
1675 1757
1676 $self->SUPER::_draw; # draw background, if applicable 1758 $self->SUPER::_draw; # draw background, if applicable
1677 1759
1678 my $tex = $self->{texture} ||= do { 1760 my $size = $self->{texture} ||= do {
1679 $self->{layout}->set_foreground (@{$self->{fg}}); 1761 $self->{layout}->set_foreground (@{$self->{fg}});
1680 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1762 $self->{layout}->set_font ($self->{font}) if $self->{font};
1681 $self->{layout}->set_width ($self->{w}); 1763 $self->{layout}->set_width ($self->{w});
1682 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1764 $self->{layout}->set_ellipsise ($self->{ellipsise});
1683 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1765 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1684 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1766 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1685 1767
1686 new_from_layout CFClient::Texture $self->{layout} 1768 [$self->{layout}->size]
1687 }; 1769 };
1688 1770
1689 unless (exists $self->{ox}) { 1771 unless (exists $self->{ox}) {
1690 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1772 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1691 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1773 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x}
1692 : ($self->{w} - $tex->{w}) * 0.5); 1774 : ($self->{w} - $size->[0]) * 0.5);
1693 1775
1694 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1776 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1695 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1777 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1696 : ($self->{h} - $tex->{h}) * 0.5); 1778 : ($self->{h} - $size->[1]) * 0.5);
1697 }; 1779 };
1698 1780
1699 glEnable GL_TEXTURE_2D;
1700
1701 my $w = List::Util::min $self->{w} + 4, $tex->{w}; 1781 my $w = List::Util::min $self->{w} + 4, $size->[0];
1702 my $h = List::Util::min $self->{h} + 2, $tex->{h}; 1782 my $h = List::Util::min $self->{h} + 2, $size->[1];
1703 1783
1704 if ($tex->{format} == GL_ALPHA) { 1784 $self->{layout}->render ($self->{ox}, $self->{oy});
1705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1706 glColor @{$self->{fg}};
1707 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h);
1708 } else {
1709 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1710 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h);
1711 }
1712
1713 glDisable GL_TEXTURE_2D;
1714} 1785}
1715 1786
1716############################################################################# 1787#############################################################################
1717 1788
1718package CFClient::UI::EntryBase; 1789package CFClient::UI::EntryBase;
1829 1900
1830 my $idx = $self->{layout}->xy_to_index ($x, $y); 1901 my $idx = $self->{layout}->xy_to_index ($x, $y);
1831 1902
1832 # byte-index to char-index 1903 # byte-index to char-index
1833 my $text = $self->{text}; 1904 my $text = $self->{text};
1834 utf8::encode $text; 1905 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1835 $self->{cursor} = length substr $text, 0, $idx; 1906 $self->{cursor} = length $text;
1836 1907
1837 $self->_set_text ($self->{text}); 1908 $self->_set_text ($self->{text});
1838 $self->update; 1909 $self->update;
1839 1910
1840 1 1911 1
2060 my ($self) = @_; 2131 my ($self) = @_;
2061 2132
2062 (6) x 2 2133 (6) x 2
2063} 2134}
2064 2135
2136sub toggle {
2137 my ($self) = @_;
2138
2139 $self->{state} = !$self->{state};
2140 $self->emit (changed => $self->{state});
2141 $self->update;
2142}
2143
2065sub invoke_button_down { 2144sub invoke_button_down {
2066 my ($self, $ev, $x, $y) = @_; 2145 my ($self, $ev, $x, $y) = @_;
2067 2146
2068 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 2147 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2069 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 2148 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2070 $self->{state} = !$self->{state}; 2149 $self->toggle;
2071 $self->emit (changed => $self->{state});
2072 } else { 2150 } else {
2073 return 0 2151 return 0
2074 } 2152 }
2075 2153
2076 1 2154 1
2112 my $self = $class->SUPER::new ( 2190 my $self = $class->SUPER::new (
2113 can_events => 0, 2191 can_events => 0,
2114 @_, 2192 @_,
2115 ); 2193 );
2116 2194
2117 $self->{path} 2195 $self->{path} || $self->{tex}
2118 or Carp::croak "required attribute 'path' not set"; 2196 or Carp::croak "'path' or 'tex' attributes required";
2119 2197
2120 $self->{tex} = $texture_cache{$self->{path}} ||= 2198 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2121 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; 2199 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2122 2200
2123 Scalar::Util::weaken $texture_cache{$self->{path}}; 2201 Scalar::Util::weaken $texture_cache{$self->{path}};
2124 2202
2125 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2203 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2247 my $ycut1 = max 0, min 1, $ycut; 2325 my $ycut1 = max 0, min 1, $ycut;
2248 my $ycut2 = max 0, min 1, $ycut - 1; 2326 my $ycut2 = max 0, min 1, $ycut - 1;
2249 2327
2250 my $h1 = $self->{h} * (1 - $ycut1); 2328 my $h1 = $self->{h} * (1 - $ycut1);
2251 my $h2 = $self->{h} * (1 - $ycut2); 2329 my $h2 = $self->{h} * (1 - $ycut2);
2330 my $h3 = $self->{h};
2331
2332 $_ = $_ * (284-4)/288 + 4/288 for ($h1, $h2, $h3);
2252 2333
2253 glEnable GL_BLEND; 2334 glEnable GL_BLEND;
2254 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 2335 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2255 GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2336 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2256 glEnable GL_TEXTURE_2D; 2337 glEnable GL_TEXTURE_2D;
2275 2356
2276 if ($t3) { 2357 if ($t3) {
2277 glBindTexture GL_TEXTURE_2D, $t3->{name}; 2358 glBindTexture GL_TEXTURE_2D, $t3->{name};
2278 glBegin GL_QUADS; 2359 glBegin GL_QUADS;
2279 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; 2360 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
2280 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; 2361 glTexCoord 0 , $t3->{t}; glVertex 0 , $h3;
2281 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; 2362 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $h3;
2282 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; 2363 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
2283 glEnd; 2364 glEnd;
2284 } 2365 }
2285 2366
2286 glDisable GL_BLEND; 2367 glDisable GL_BLEND;
2442 } 2523 }
2443 2524
2444 1 2525 1
2445} 2526}
2446 2527
2528sub invoke_mouse_wheel {
2529 my ($self, $ev) = @_;
2530
2531 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2532
2533 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2);
2534
2535 ! ! $delta
2536}
2537
2447sub update { 2538sub update {
2448 my ($self) = @_; 2539 my ($self) = @_;
2449 2540
2450 delete $self->{knob_w}; 2541 delete $self->{knob_w};
2451 $self->SUPER::update; 2542 $self->SUPER::update;
2550sub new { 2641sub new {
2551 my $class = shift; 2642 my $class = shift;
2552 2643
2553 my $self = $class->SUPER::new ( 2644 my $self = $class->SUPER::new (
2554 fontsize => 1, 2645 fontsize => 1,
2555 can_events => 0, 2646 can_events => 1,
2556 indent => 0, 2647 indent => 0,
2557 #font => default_font 2648 #font => default_font
2558 @_, 2649 @_,
2559 2650
2560 layout => (new CFClient::Layout 1), 2651 layout => (new CFClient::Layout),
2561 par => [], 2652 par => [],
2562 height => 0, 2653 height => 0,
2563 children => [ 2654 children => [
2564 (new CFClient::UI::Empty expand => 1), 2655 (new CFClient::UI::Empty expand => 1),
2565 (new CFClient::UI::Slider vertical => 1), 2656 (new CFClient::UI::Slider vertical => 1),
2600 2691
2601 $self->reflow; 2692 $self->reflow;
2602 2693
2603 local $self->{children} = [$empty, $slider]; 2694 local $self->{children} = [$empty, $slider];
2604 $self->SUPER::invoke_size_allocate ($w, $h) 2695 $self->SUPER::invoke_size_allocate ($w, $h)
2696}
2697
2698sub invoke_mouse_wheel {
2699 my ($self, $ev) = @_;
2700
2701 return 0 unless $ev->{dy}; # only vertical movements
2702
2703 $self->{children}[1]->emit (mouse_wheel => $ev);
2704
2705 1
2605} 2706}
2606 2707
2607sub get_layout { 2708sub get_layout {
2608 my ($self, $para) = @_; 2709 my ($self, $para) = @_;
2609 2710
2726 my $y0 = $top; 2827 my $y0 = $top;
2727 my $y1 = $top + $H; 2828 my $y1 = $top + $H;
2728 2829
2729 my $y = 0; 2830 my $y = 0;
2730 2831
2731 glEnable GL_BLEND;
2732 #TODO# not correct in windows where rgba is forced off
2733 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2734
2735 for my $para (@{$self->{par}}) { 2832 for my $para (@{$self->{par}}) {
2736 my $h = $para->{h}; 2833 my $h = $para->{h};
2737 2834
2738 if ($y0 < $y + $h && $y < $y1) { 2835 if ($y0 < $y + $h && $y < $y1) {
2739 2836
2740 my $layout = $self->get_layout ($para); 2837 my $layout = $self->get_layout ($para);
2741 2838
2742 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2743
2744 glRasterPos $para->{indent}, $y - $y0; 2839 $layout->render ($para->{indent}, $y - $y0);
2745 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2746 2840
2747 if (my @w = @{ $para->{widget} }) { 2841 if (my @w = @{ $para->{widget} }) {
2748 my @s = $layout->get_shapes; 2842 my @s = $layout->get_shapes;
2749 2843
2750 glDisable GL_BLEND;
2751 for (@w) { 2844 for (@w) {
2752 my ($dx, $dy) = splice @s, 0, 2, (); 2845 my ($dx, $dy) = splice @s, 0, 2, ();
2753 2846
2754 $_->{x} = $dx + $para->{indent}; 2847 $_->{x} = $dx + $para->{indent};
2755 $_->{y} = $dy + $y - $y0; 2848 $_->{y} = $dy + $y - $y0;
2756 2849
2757 $_->draw; 2850 $_->draw;
2758 } 2851 }
2759 glEnable GL_BLEND;
2760 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2761 } 2852 }
2762 } 2853 }
2763 2854
2764 $y += $h; 2855 $y += $h;
2765 } 2856 }
2766
2767 glDisable GL_BLEND;
2768 }; 2857 };
2769 }); 2858 });
2770} 2859}
2771 2860
2772sub reconfigure { 2861sub reconfigure {
2919 3008
2920 $self->{root}->on_post_alloc ("move_$self" => sub { 3009 $self->{root}->on_post_alloc ("move_$self" => sub {
2921 my $widget = $self->{owner} 3010 my $widget = $self->{owner}
2922 or return; 3011 or return;
2923 3012
3013 if ($widget->{visible}) {
2924 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3014 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2925 3015
2926 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3016 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2927 if $x + $self->{w} > $self->{root}{w}; 3017 if $x + $self->{w} > $self->{root}{w};
2928 3018
2929 $self->move_abs ($x, $y); 3019 $self->move_abs ($x, $y);
3020 } else {
3021 $self->hide;
3022 }
2930 }); 3023 });
2931} 3024}
2932 3025
2933sub _draw { 3026sub _draw {
2934 my ($self) = @_; 3027 my ($self) = @_;
3068 for my $item (@{ $self->{items} }) { 3161 for my $item (@{ $self->{items} }) {
3069 my ($widget, $cb, $tooltip) = @$item; 3162 my ($widget, $cb, $tooltip) = @$item;
3070 3163
3071 # handle various types of items, only text for now 3164 # handle various types of items, only text for now
3072 if (!ref $widget) { 3165 if (!ref $widget) {
3166 if ($widget =~ /\t/) {
3167 my ($left, $right) = split /\t/, $widget, 2;
3168
3169 $widget = new CFClient::UI::HBox
3170 can_hover => 1,
3171 can_events => 1,
3172 tooltip => $tooltip,
3173 children => [
3174 (new CFClient::UI::Label markup => $left, expand => 1),
3175 (new CFClient::UI::Label markup => $right, align => +1),
3176 ],
3177 ;
3178
3179 } else {
3073 $widget = new CFClient::UI::Label 3180 $widget = new CFClient::UI::Label
3074 can_hover => 1, 3181 can_hover => 1,
3075 can_events => 1, 3182 can_events => 1,
3076 markup => $widget, 3183 markup => $widget,
3077 tooltip => $tooltip 3184 tooltip => $tooltip;
3185 }
3078 } 3186 }
3079 3187
3080 $self->{item}{$widget} = $item; 3188 $self->{item}{$widget} = $item;
3081 3189
3082 $self->{vbox}->add ($widget); 3190 $self->{vbox}->add ($widget);
3149 3257
3150 $self->SUPER::add (@widgets); 3258 $self->SUPER::add (@widgets);
3151 3259
3152 $self->{current} = $self->{children}[0] 3260 $self->{current} = $self->{children}[0]
3153 if @{ $self->{children} }; 3261 if @{ $self->{children} };
3262}
3263
3264sub get_current_page {
3265 my ($self) = @_;
3266
3267 $self->{current}
3154} 3268}
3155 3269
3156sub set_current_page { 3270sub set_current_page {
3157 my ($self, $page_or_widget) = @_; 3271 my ($self, $page_or_widget) = @_;
3158 3272
3224 tooltip => $tooltip, 3338 tooltip => $tooltip,
3225 on_activate => sub { $self->set_current_page ($widget) }, 3339 on_activate => sub { $self->set_current_page ($widget) },
3226 ); 3340 );
3227 3341
3228 $self->{multiplexer}->add ($widget); 3342 $self->{multiplexer}->add ($widget);
3343}
3344
3345sub get_current_page {
3346 my ($self) = @_;
3347
3348 $self->{multiplexer}->get_current_page
3229} 3349}
3230 3350
3231sub set_current_page { 3351sub set_current_page {
3232 my ($self, $page) = @_; 3352 my ($self, $page) = @_;
3233 3353
3438 3558
3439############################################################################# 3559#############################################################################
3440 3560
3441package CFClient::UI::Inventory; 3561package CFClient::UI::Inventory;
3442 3562
3443our @ISA = CFClient::UI::ScrolledWindow::; 3563our @ISA = CFClient::UI::Table::;
3444 3564
3445sub new { 3565sub new {
3446 my $class = shift; 3566 my $class = shift;
3447 3567
3448 my $self = $class->SUPER::new ( 3568 my $self = $class->SUPER::new (
3449 child => (new CFClient::UI::Table col_expand => [0, 1, 0]), 3569 col_expand => [0, 1, 0],
3570 items => [],
3450 @_, 3571 @_,
3451 ); 3572 );
3452 3573
3574 $self->set_sort_order (undef);
3575
3453 $self 3576 $self
3577}
3578
3579sub update_items {
3580 my ($self) = @_;
3581
3582 $self->clear;
3583
3584 my @item = $self->{sort}->(@{ $self->{items} });
3585
3586 my @adds;
3587 my $row = 0;
3588 for my $item ($self->{sort}->(@{ $self->{items} })) {
3589 CFClient::Item::update_widgets $item;
3590
3591 push @adds, 0, $row, $item->{face_widget};
3592 push @adds, 1, $row, $item->{desc_widget};
3593 push @adds, 2, $row, $item->{weight_widget};
3594
3595 $row++;
3596 }
3597
3598 $self->add (@adds);
3599}
3600
3601sub set_sort_order {
3602 my ($self, $order) = @_;
3603
3604 $self->{sort} = $order ||= sub {
3605 sort {
3606 $a->{type} <=> $b->{type}
3607 or $a->{name} cmp $b->{name}
3608 } @_
3609 };
3610
3611 $self->update_items;
3454} 3612}
3455 3613
3456sub set_items { 3614sub set_items {
3457 my ($self, $items) = @_; 3615 my ($self, $items) = @_;
3458 3616
3459 $self->{child}->clear; 3617 $self->{items} = [$items ? values %$items : ()];
3460 return unless $items; 3618 $self->update_items;
3461
3462 my @items = sort {
3463 ($a->{type} <=> $b->{type})
3464 or ($a->{name} cmp $b->{name})
3465 } values %$items;
3466
3467 $self->{real_items} = \@items;
3468
3469 my $row = 0;
3470 for my $item (@items) {
3471 CFClient::Item::update_widgets $item;
3472
3473 $self->{child}->add (0, $row, $item->{face_widget});
3474 $self->{child}->add (1, $row, $item->{desc_widget});
3475 $self->{child}->add (2, $row, $item->{weight_widget});
3476
3477 $row++;
3478 }
3479} 3619}
3480 3620
3481############################################################################# 3621#############################################################################
3482 3622
3483package CFClient::UI::SpellList; 3623package CFClient::UI::SpellList;
3792 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 3932 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3793 3933
3794 $w = 0 if $w < 0; 3934 $w = 0 if $w < 0;
3795 $h = 0 if $h < 0; 3935 $h = 0 if $h < 0;
3796 3936
3937 $w = max $widget->{min_w}, $w;
3938 $h = max $widget->{min_h}, $h;
3939
3940 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3941 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3942
3797 $w = int $w + 0.5; 3943 $w = int $w + 0.5;
3798 $h = int $h + 0.5; 3944 $h = int $h + 0.5;
3799 3945
3800 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 3946 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3801 $widget->{old_w} = $widget->{w}; 3947 $widget->{old_w} = $widget->{w};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines