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.31 by elmex, Sun Apr 9 00:43:11 2006 UTC vs.
Revision 1.56 by root, Mon Apr 10 22:16:34 2006 UTC

5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10our $FOCUS; # the widget with current focus 10use Crossfire::Client;
11our @ACTIVE_WIDGETS; 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
12 16
13# class methods for events 17# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
16sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 20}
17sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev) if $GRAB;
40}
41
42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev) if $HOVER;
71}
18 72
19sub new { 73sub new {
20 my $class = shift; 74 my $class = shift;
21 75
22 bless { @_ }, $class 76 bless { @_ }, $class
23}
24
25sub activate {
26 push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28}
29
30sub deactivate {
31 @ACTIVE_WIDGETS =
32 sort { $a->{z} <=> $b->{z} }
33 grep { $_ && $_ != $_[0] }
34 @ACTIVE_WIDGETS;
35} 77}
36 78
37sub move { 79sub move {
38 my ($self, $x, $y, $z) = @_; 80 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 81 $self->{x} = $x;
44sub needs_redraw { 86sub needs_redraw {
45 0 87 0
46} 88}
47 89
48sub size_request { 90sub size_request {
91 require Carp;
49 die "size_request is abtract"; 92 Carp::confess "size_request is abtract";
93}
94
95sub size_allocate {
96 my ($self, $w, $h) = @_;
97
98 $self->{w} = $w;
99 $self->{h} = $h;
50} 100}
51 101
52sub focus_in { 102sub focus_in {
53 my ($widget) = @_; 103 my ($self) = @_;
54 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
55} 108}
56 109
57sub focus_out { 110sub focus_out {
58 my ($widget) = @_; 111 my ($self) = @_;
59}
60 112
61sub key_down { 113 return unless $FOCUS == $self;
62 my ($widget, $sdlev) = @_;
63}
64 114
65sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
66 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
67} 117}
68 118
119sub mouse_motion { }
120sub button_up { }
69sub button_down { 121sub button_down { }
70 my ($widget, $sdlev) = @_; 122sub key_down { }
71} 123sub key_up { }
72 124
73sub button_up { 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
74 my ($widget, $sdlev) = @_; 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
75}
76
77sub w { $_[0]->{w} }
78sub h { $_[0]->{h} }
79sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
80sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
81sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
82 130
83sub draw { 131sub draw {
84 my ($self) = @_; 132 my ($self) = @_;
85 133
86 glPushMatrix; 134 glPushMatrix;
87 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
88 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
89 glPopMatrix; 148 glPopMatrix;
90} 149}
91 150
92sub _draw { 151sub _draw {
93 my ($widget) = @_; 152 my ($self) = @_;
153
154 warn "no draw defined for $self\n";
94} 155}
95 156
96sub bbox { 157sub bbox {
97 my ($widget) = @_; 158 my ($self) = @_;
159 my ($w, $h) = $self->size_request;
160 (
161 $self->{x},
162 $self->{y},
163 $self->{x} = $w,
164 $self->{y} = $h
165 )
166}
167
168sub find_widget {
169 my ($self, $x, $y) = @_;
170
171 return $self
172 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
173 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
174
175 ()
176}
177
178sub del_parent { $_[0]->{parent} = undef }
179
180sub set_parent {
181 my ($self, $par) = @_;
182
183 $self->{parent} = $par;
184 Scalar::Util::weaken $self->{parent};
185}
186
187sub get_parent {
188 $_[0]->{parent}
189}
190
191sub update {
192 my ($self) = @_;
193
194 $self->{parent}->update
195 if $self->{parent};
98} 196}
99 197
100sub DESTROY { 198sub DESTROY {
101 my ($self) = @_; 199 my ($self) = @_;
102 200
103 $self->deactivate; 201 #$self->deactivate;
104} 202}
203
204#############################################################################
105 205
106package Crossfire::Client::Widget::Container; 206package Crossfire::Client::Widget::Container;
107 207
108our @ISA = Crossfire::Client::Widget::; 208our @ISA = Crossfire::Client::Widget::;
109 209
210sub new {
211 my ($class, @widgets) = @_;
212
213 my $self = $class->SUPER::new (children => []);
214 $self->add ($_) for @widgets;
215
216 $self
217}
218
219sub add {
220 my ($self, $chld, $expand) = @_;
221
222 $chld->{expand} = $expand;
223 $chld->set_parent ($self);
224
225 @{$self->{children}} =
226 sort { $a->{z} <=> $b->{z} }
227 @{$self->{children}}, $chld;
228
229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
231}
232
233sub remove {
234 my ($self, $widget) = @_;
235
236 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
237
238 $self->size_allocate ($self->{w}, $self->{h});
239}
240
241sub find_widget {
242 my ($self, $x, $y) = @_;
243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
247 my $res;
248
249 for (reverse @{ $self->{children} }) {
250 $res = $_->find_widget ($x, $y)
251 and return $res;
252 }
253
254 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
255}
256
257sub _draw {
258 my ($self) = @_;
259
260 $_->draw for @{$self->{children}};
261}
262
263#############################################################################
264
265package Crossfire::Client::Widget::Bin;
266
267our @ISA = Crossfire::Client::Widget::Container::;
268
269sub child { $_[0]->{children}[0] }
270
271sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
273}
274
275sub size_allocate {
276 my ($self, $w, $h) = @_;
277
278 $self->SUPER::size_allocate ($w, $h);
279 $self->{children}[0]->size_allocate ($w, $h)
280 if $self->{children}[0]
281}
282
283#############################################################################
284
285package Crossfire::Client::Widget::Window;
286
287our @ISA = Crossfire::Client::Widget::Bin::;
288
110use SDL::OpenGL; 289use SDL::OpenGL;
111 290
112sub add { $_[0]->{child} = $_[1] } 291sub new {
113sub get { $_[0]->{child} } 292 my ($class, $x, $y, $z, $w, $h) = @_;
114 293
115sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 294 my $self = $class->SUPER::new;
116 295
117sub _draw { die "Containers can't be drawn!" } 296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297}
118 298
119package Crossfire::Client::Widget::Window; 299sub update {
120
121our @ISA = Crossfire::Client::Widget::Container::;
122
123use SDL::OpenGL;
124
125sub add {
126 my ($self, $chld) = @_; 300 my ($self) = @_;
127 $self->SUPER::add ($chld); 301
128 $self->render_chld; #TODO: Move this to the size_request event propably? 302 $self->render_chld;
303 $self->SUPER::update;
129} 304}
130 305
131sub render_chld { 306sub render_chld {
132 my ($self) = @_; 307 my ($self) = @_;
133 my $chld = $self->get;
134 my ($w, $h) = $self->size_request;
135 308
136 $self->{texture} = 309 $self->{texture} =
137 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
138 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
139 ); 312 );
140 $self->{texture}->upload;
141} 313}
142 314
143sub size_request { 315sub size_allocate {
144 my ($self) = @_; 316 my ($self, $w, $h) = @_;
145 my $chld = $self->get 317
146 or return (0, 0); 318 $self->{w} = $w;
147 $chld->size_request 319 $self->{h} = $h;
320
321 $self->child->size_allocate ($w, $h);
322
323 $self->render_chld;
148} 324}
149 325
150sub _draw { 326sub _draw {
151 my ($self) = @_; 327 my ($self) = @_;
152 328
153 my ($w, $h) = $self->size_request;#TODO# use width/height of texture 329 my ($w, $h) = ($self->w, $self->h);
154 330
155 my $tex = $self->{texture} 331 my $tex = $self->{texture}
156 or return; 332 or return;
157 333
158 glEnable GL_BLEND; 334 glEnable GL_BLEND;
159 glEnable GL_TEXTURE_2D; 335 glEnable GL_TEXTURE_2D;
160 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
161 glBindTexture GL_TEXTURE_2D, $tex->{name};
162 337
163 glColor 1, 0, 1; 338 $tex->draw_quad (0, 0, $w, $h);
164
165 glBegin GL_QUADS;
166 glTexCoord 0, 0; glVertex 0, 0;
167 glTexCoord 0, 1; glVertex 0, $h;
168 glTexCoord 1, 1; glVertex $w, $h;
169 glTexCoord 1, 0; glVertex $w, 0;
170 glEnd;
171 339
172 glDisable GL_BLEND; 340 glDisable GL_BLEND;
173 glDisable GL_TEXTURE_2D; 341 glDisable GL_TEXTURE_2D;
174} 342}
175 343
344#############################################################################
345
176package Crossfire::Client::Widget::Frame; 346package Crossfire::Client::Widget::Frame;
177 347
178our @ISA = Crossfire::Client::Widget::Container::; 348our @ISA = Crossfire::Client::Widget::Bin::;
179 349
180use SDL::OpenGL; 350use SDL::OpenGL;
181 351
182sub size_request { 352sub size_request {
183 my ($self) = @_; 353 my ($self) = @_;
184 my $chld = $self->get 354 my $chld = $self->child
185 or return (0, 0); 355 or return (0, 0);
186 356
187 $chld->move (2, 2); 357 $chld->move (2, 2);
188 358
189 map { $_ + 4 } $chld->size_request; 359 map { $_ + 4 } $chld->size_request;
190} 360}
191 361
362sub size_allocate {
363 my ($self, $w, $h) = @_;
364
365 $self->{w} = $w;
366 $self->{h} = $h;
367
368 $self->child->size_allocate ($w - 4, $h - 4);
369 $self->child->move (2, 2);
370}
371
192sub _draw { 372sub _draw {
193 my ($self) = @_; 373 my ($self) = @_;
194 374
195 my $chld = $self->get; 375 my $chld = $self->child;
196 376
197 my ($w, $h) = $chld->size_request; 377 my ($w, $h) = $chld->size_request;
198 378
199 glBegin GL_QUADS; 379 glBegin GL_QUADS;
200 glColor 0, 0, 0; 380 glColor 0, 0, 0;
201 glTexCoord 0, 0; glVertex 0 , 0; 381 glVertex 0 , 0;
202 glTexCoord 0, 1; glVertex 0 , $h + 4; 382 glVertex 0 , $h + 4;
203 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 383 glVertex $w + 4 , $h + 4;
204 glTexCoord 1, 0; glVertex $w + 4 , 0; 384 glVertex $w + 4 , 0;
205 glEnd; 385 glEnd;
206 386
207 $chld->draw; 387 $chld->draw;
208} 388}
209 389
390#############################################################################
391
210package Crossfire::Client::Widget::FancyFrame; 392package Crossfire::Client::Widget::FancyFrame;
211 393
212our @ISA = Crossfire::Client::Widget::Frame::; 394our @ISA = Crossfire::Client::Widget::Bin::;
213 395
214use SDL::OpenGL; 396use SDL::OpenGL;
215 397
216#TODO: implement themed frame 398my @tex =
399 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
400 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
401
402sub size_request {
403 my ($self) = @_;
404
405 my ($w, $h) = $self->SUPER::size_request;
406
407 $h += $tex[1]->{height};
408 $h += $tex[4]->{height};
409 $w += $tex[2]->{width};
410 $w += $tex[3]->{width};
411
412 ($w, $h)
413}
414
415sub size_allocate {
416 my ($self, $w, $h) = @_;
417
418 $self->SUPER::size_allocate ($w, $h);
419
420 $h -= $tex[1]->{height};
421 $h -= $tex[4]->{height};
422 $w -= $tex[2]->{width};
423 $w -= $tex[3]->{width};
424
425 $h = $h < 0 ? 0 : $h;
426 $w = $w < 0 ? 0 : $w;
427
428 $self->child->size_allocate ($w, $h);
429 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
430}
431
432sub _draw {
433 my ($self) = @_;
434
435 my ($w, $h) = ($self->{w}, $self->{h});
436 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
437
438 glEnable GL_BLEND;
439 glEnable GL_TEXTURE_2D;
440 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
441 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
442
443 my $top = $tex[1];
444 $top->draw_quad (0, 0, $w, $top->{height});
445
446 my $left = $tex[3];
447 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
448
449 my $right = $tex[2];
450 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
451
452 my $bottom = $tex[4];
453 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
454
455 my $bg = $tex[0];
456 glBindTexture GL_TEXTURE_2D, $bg->{name};
457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
458 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
460
461 my $rep_x = $cw / $bg->{width};
462 my $rep_y = $ch / $bg->{height};
463
464 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
465
466 glDisable GL_BLEND;
467 glDisable GL_TEXTURE_2D;
468
469 $self->child->draw;
470
471}
472
473#############################################################################
217 474
218package Crossfire::Client::Widget::Table; 475package Crossfire::Client::Widget::Table;
219 476
220our @ISA = Crossfire::Client::Widget::Container::; 477our @ISA = Crossfire::Client::Widget::Bin::;
221 478
222use SDL::OpenGL; 479use SDL::OpenGL;
223 480
224sub add { 481sub add {
225 my ($self, $x, $y, $chld) = @_; 482 my ($self, $x, $y, $chld) = @_;
483 my $old_chld = $self->{children}[$y][$x];
484
226 $self->{childs}[$y][$x] = $chld; 485 $self->{children}[$y][$x] = $chld;
486 $chld->set_parent ($self);
487 $self->update;
227} 488}
228 489
229sub max_row_height { 490sub max_row_height {
230 my ($self, $row) = @_; 491 my ($self, $row) = @_;
231 492
232 my $hs = 0; 493 my $hs = 0;
233 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 494 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
234 my $c = $self->{childs}->[$row]->[$xi]; 495 my $c = $self->{children}->[$row]->[$xi];
235 if ($c) { 496 if ($c) {
236 my ($w, $h) = $c->size_request; 497 my ($w, $h) = $c->size_request;
237 if ($hs < $h) { $hs = $h } 498 if ($hs < $h) { $hs = $h }
238 } 499 }
239 } 500 }
242 503
243sub max_col_width { 504sub max_col_width {
244 my ($self, $col) = @_; 505 my ($self, $col) = @_;
245 506
246 my $ws = 0; 507 my $ws = 0;
247 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 508 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
248 my $c = ($self->{childs}->[$yi] || [])->[$col]; 509 my $c = ($self->{children}->[$yi] || [])->[$col];
249 if ($c) { 510 if ($c) {
250 my ($w, $h) = $c->size_request; 511 my ($w, $h) = $c->size_request;
251 if ($ws < $w) { $ws = $w } 512 if ($ws < $w) { $ws = $w }
252 } 513 }
253 } 514 }
257sub size_request { 518sub size_request {
258 my ($self) = @_; 519 my ($self) = @_;
259 520
260 my ($hs, $ws) = (0, 0); 521 my ($hs, $ws) = (0, 0);
261 522
262 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 523 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
263 $hs += $self->max_row_height ($yi); 524 $hs += $self->max_row_height ($yi);
264 } 525 }
265 526
266 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 527 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
267 my $wm = 0; 528 my $wm = 0;
268 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 529 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
269 $wm += $self->max_col_width ($xi) 530 $wm += $self->max_col_width ($xi)
270 } 531 }
271 if ($ws < $wm) { $ws = $wm } 532 if ($ws < $wm) { $ws = $wm }
272 } 533 }
273 534
276 537
277sub _draw { 538sub _draw {
278 my ($self) = @_; 539 my ($self) = @_;
279 540
280 my $y = 0; 541 my $y = 0;
281 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 542 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
282 my $x = 0; 543 my $x = 0;
283 544
284 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 545 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
285 546
286 my $c = $self->{childs}->[$yi]->[$xi]; 547 my $c = $self->{children}->[$yi]->[$xi];
287 if ($c) { 548 if ($c) {
288 $c->move ($x, $y, 0); #TODO: Move to size_request 549 $c->move ($x, $y, 0); #TODO: Move to size_request
289 $c->draw if $c; 550 $c->draw if $c;
290 } 551 }
291 552
294 555
295 $y += $self->max_row_height ($yi); 556 $y += $self->max_row_height ($yi);
296 } 557 }
297} 558}
298 559
560#############################################################################
561
299package Crossfire::Client::Widget::VBox; 562package Crossfire::Client::Widget::VBox;
300 563
301our @ISA = Crossfire::Client::Widget::Container::; 564our @ISA = Crossfire::Client::Widget::Container::;
302 565
303use SDL::OpenGL; 566use SDL::OpenGL;
304 567
305sub add {
306 my ($self, $chld) = @_;
307 push @{$self->{childs}}, $chld;
308}
309
310sub size_request { 568sub size_request {
311 my ($self) = @_; 569 my ($self) = @_;
312 570
313 my ($hs, $ws) = (0, 0); 571 my @alloc = map [$_->size_request], @{$self->{children}};
572
573 (
574 (List::Util::max map $_->[0], @alloc),
575 (List::Util::sum map $_->[1], @alloc),
576 )
577}
578
579sub size_allocate {
580 my ($self, $w, $h) = @_;
581
582 $self->w ($w);
583 $self->h ($h);
584
585 my $exp;
586 my @oth;
587 # find expand widget
314 for (@{$self->{childs} || []}) { 588 for (@{$self->{children}}) {
589 if ($_->{expand}) {
590 $exp = $_;
591 last;
592 }
593 push @oth, $_;
594 }
595
596 my ($ow, $oh);
597
598 # get sizes of other widgets
599 for (@oth) {
315 my ($w, $h) = $_->size_request; 600 my ($w, $h) = $_->size_request;
316 $hs += $h; 601 $oh += $h;
317 if ($ws < $w) { $ws = $w } 602 if ($ow < $w) { $ow = $w }
318 } 603 }
319 604
320 return ($ws, $hs); 605 my $y = 0;
321}
322
323sub _draw {
324 my ($self) = @_;
325
326 my ($x, $y);
327 for (@{$self->{childs} || []}) { 606 for (@{$self->{children}}) {
328 $_->move (0, $y, 0); #TODO: move to size_request 607 $_->move (0, $y);
329 $_->draw; 608
609 if ($_ == $exp) {
610 $_->size_allocate ($w, $h - $oh);
611 $y += $h - $oh;
612 } else {
330 my ($w, $h) = $_->size_request; 613 my ($cw, $h) = $_->size_request;
614 $_->size_allocate ($w, $h);
331 $y += $h; 615 $y += $h;
616 }
332 } 617 }
333} 618}
619
620#############################################################################
334 621
335package Crossfire::Client::Widget::Label; 622package Crossfire::Client::Widget::Label;
336 623
337our @ISA = Crossfire::Client::Widget::; 624our @ISA = Crossfire::Client::Widget::;
338 625
339use SDL::OpenGL; 626use SDL::OpenGL;
340 627
341sub new { 628sub new {
342 my ($class, $x, $y, $z, $height, $text) = @_; 629 my ($class, $x, $y, $z, $height, $text) = @_;
343 630
631 $height ||= $::FONTSIZE;
632
633 # TODO: color, and make height, xyz etc. optional
344 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
345 635
346 $self->set_text ($text); 636 $self->set_text ($text);
347 637
348 $self 638 $self
350 640
351sub set_text { 641sub set_text {
352 my ($self, $text) = @_; 642 my ($self, $text) = @_;
353 643
354 $self->{text} = $text; 644 $self->{text} = $text;
355
356 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 645 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
646
647 $self->update;
357} 648}
358 649
359sub get_text { 650sub get_text {
360 my ($self, $text) = @_; 651 my ($self, $text) = @_;
361 652
363} 654}
364 655
365sub size_request { 656sub size_request {
366 my ($self) = @_; 657 my ($self) = @_;
367 658
368 ( 659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 (
369 $self->{texture}{width}, 661 $self->{texture}{width},
370 $self->{texture}{height}, 662 $self->{texture}{height},
663 )
664 } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
666
667 ($w, $h)
371 ) 668 }
669
372} 670}
373 671
374sub _draw { 672sub _draw {
375 my ($self) = @_; 673 my ($self) = @_;
376 674
378 676
379 glEnable GL_BLEND; 677 glEnable GL_BLEND;
380 glEnable GL_TEXTURE_2D; 678 glEnable GL_TEXTURE_2D;
381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
382 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
383 glBindTexture GL_TEXTURE_2D, $tex->{name};
384 681
385 glColor 1, 1, 1, 0.8; 682 glColor 1, 0, 0, 1; # TODO color
386 683
387 glBegin GL_QUADS; 684 $tex->draw_quad (0, 0);
388 glTexCoord 0, 0; glVertex 0 , 0;
389 glTexCoord 0, 1; glVertex 0 , $tex->{height};
390 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
391 glTexCoord 1, 0; glVertex $tex->{width}, 0;
392 glEnd;
393 685
394 glDisable GL_BLEND; 686 glDisable GL_BLEND;
395 glDisable GL_TEXTURE_2D; 687 glDisable GL_TEXTURE_2D;
396} 688}
397 689
690#############################################################################
691
398package Crossfire::Client::Widget::TextEntry; 692package Crossfire::Client::Widget::Entry;
399 693
400our @ISA = Crossfire::Client::Widget::Label::; 694our @ISA = Crossfire::Client::Widget::Label::;
401 695
402use SDL; 696use SDL;
403use SDL::OpenGL; 697use SDL::OpenGL;
406 my ($self, $ev) = @_; 700 my ($self, $ev) = @_;
407 701
408 my $mod = $ev->key_mod; 702 my $mod = $ev->key_mod;
409 my $sym = $ev->key_sym; 703 my $sym = $ev->key_sym;
410 704
411 $ev->set_unicode (1);
412 my $uni = $ev->key_unicode; 705 my $uni = $ev->key_unicode;
413 706
414 my $text = $self->get_text; 707 my $text = $self->get_text;
415 708
416 if ($sym == SDLK_BACKSPACE) { 709 if ($sym == SDLK_BACKSPACE) {
417 substr $text, -1, 1, ''; 710 substr $text, -1, 1, '';
418
419 } elsif ($uni) { 711 } elsif ($uni) {
420 $text .= chr $uni; 712 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
421 } 714 }
715
422 $self->set_text ($text); 716 $self->set_text ($text);
423} 717}
424 718
719sub button_down {
720 my ($self, $ev) = @_;
425 721
426# XXX: TextView isn't neccessary with pango multiline text rendering 722 $self->focus_in;
427package Crossfire::Client::Widget::TextView;
428
429use strict;
430
431our @ISA = qw/Crossfire::Client::Widget/;
432
433use SDL::OpenGL;
434use SDL::OpenGL::Constants;
435
436sub new {
437 my ($class, $text, $h) = @_;
438 my $self = $class->SUPER::new ();
439
440 $self->{txt_height} = $h;
441 @{$self->{lines}} = split /\r?\n/, $text;
442
443 for (split /\r?\n/, $text) {
444 $self->add_line ($_);
445 }
446 $self
447} 723}
448 724
449#sub render_lines { 725sub _draw {
450# my ($self) = @_; 726 my ($self) = @_;
451# 727
452# $self->{txt_lines} = []; 728 if ($FOCUS == $self) {
453# 729 glColor 1, 1, 1;
454# for (@{$self->{lines}}) { 730 } else {
455# push @{$self->{txt_lines}}, 731 glColor 0.7, 0.7, 0.7;
456# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
457# } 732 }
458#}
459
460sub add_line {
461 my ($self, $line) = @_;
462 push @{$self->{lines}}, $line;
463
464 push @{$self->{txt_lines}},
465 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
466}
467
468sub size_request {
469 my ($self) = @_;
470
471 my $w = 0;
472 my $h = 0;
473
474 for (@{$self->{txt_lines}}) {
475 if ($w < $_->{width}) { $w = $_->{width} }
476 $h += $_->{height};
477 }
478
479 return ($w, $h);
480}
481
482sub draw_line {
483 my ($self, $tex, $y) = @_;
484
485 glBindTexture GL_TEXTURE_2D, $tex->{name};
486
487 glColor 1, 0, 1;
488 733
489 glBegin GL_QUADS; 734 glBegin GL_QUADS;
490 glTexCoord 0, 0; glVertex 0 , $y; 735 glVertex 0 , 0;
491 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height}; 736 glVertex 0 , $self->{h} - 1;
492 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height}; 737 glVertex $self->{w} - 1, $self->{h} - 1;
493 glTexCoord 1, 0; glVertex $tex->{width}, $y; 738 glVertex $self->{w} - 1, 0;
494 glEnd; 739 glEnd;
495}
496 740
497sub _draw { 741 $self->SUPER::_draw;
498 my ($self) = @_;
499
500 glEnable GL_BLEND;
501 glEnable GL_TEXTURE_2D;
502 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
503
504 my $l = 0;
505 for (@{$self->{txt_lines}}) {
506 $self->draw_line ($_, $l);
507 $l += $_->{height};
508 }
509
510 glDisable GL_BLEND;
511 glDisable GL_TEXTURE_2D;
512} 742}
743
744#############################################################################
513 745
514package Crossfire::Client::Widget::MapWidget; 746package Crossfire::Client::Widget::MapWidget;
515 747
516use strict; 748use strict;
517 749
526sub key_down { 758sub key_down {
527 print "MAPKEYDOWN\n"; 759 print "MAPKEYDOWN\n";
528} 760}
529 761
530sub key_up { 762sub key_up {
763}
764
765sub size_request {
766 (
767 1 + int $::WIDTH / 32,
768 1 + int $::HEIGHT / 32,
769 )
531} 770}
532 771
533sub _draw { 772sub _draw {
534 my ($self) = @_; 773 my ($self) = @_;
535 774
555 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 794 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
556 } 795 }
557 796
558 glEnable GL_TEXTURE_2D; 797 glEnable GL_TEXTURE_2D;
559 glEnable GL_BLEND; 798 glEnable GL_BLEND;
560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
800
801 my $sw4 = ($sw + 3) & ~3;
802 my $lighting = "\x00" x ($sw4 * $sh);
561 803
562 for my $x (0 .. $sw - 1) { 804 for my $x (0 .. $sw - 1) {
563 for my $y (0 .. $sh - 1) { 805 for my $y (0 .. $sh - 1) {
564 806
565 my $cell = $map->[$x + $xofs][$y + $yofs] 807 my $cell = $map->[$x + $xofs][$y + $yofs]
566 or next; 808 or next;
567 809
568 my $darkness = $cell->[0] * (1 / 255); 810 my $darkness = $cell->[0] * (1 / 255);
569 if ($darkness < 0) { 811 if ($darkness < 0) {
570 glColor 0.3, 0.3, 0.3; 812 $darkness = $cell->[1] ? 0.1 : 0;
571 } else {
572 glColor $darkness, $darkness, $darkness;
573 } 813 }
814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
574 815
575 for my $num (grep $_, @$cell[1,2,3]) { 816 for my $num (grep $_, @$cell[1,2,3]) {
576 my $tex = $::CONN->{face}[$num]{texture} || next; 817 my $tex = $::CONN->{face}[$num]{texture} || next;
577 818
578 glBindTexture GL_TEXTURE_2D, $tex->{name};
579
580 my $w = $tex->{width}; 819 my $w = $tex->{width};
581 my $h = $tex->{height}; 820 my $h = $tex->{height};
582 821
583 my $px = ($x + 1) * 32 - $w; 822 my $px = ($x + 1) * 32 - $w;
584 my $py = ($y + 1) * 32 - $h; 823 my $py = ($y + 1) * 32 - $h;
585 824
586 glBegin GL_QUADS; 825 $tex->draw_quad ($px, $py, $w, $h);
587 glTexCoord 0, 0; glVertex $px , $py;
588 glTexCoord 0, 1; glVertex $px , $py + $h;
589 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
590 glTexCoord 1, 0; glVertex $px + $w, $py;
591 glEnd;
592 } 826 }
593 } 827 }
594 } 828 }
829
830# if (1) { # higher quality darkness
831# $lighting =~ s/(.)/$1$1$1/gs;
832# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
833#
834# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
835#
836# $lighting = $pb->get_pixels;
837# $lighting =~ s/(.)../$1/gs;
838# }
839
840 $lighting = new Crossfire::Client::Texture
841 width => $sw4,
842 height => $sh,
843 data => $lighting,
844 internalformat => GL_ALPHA,
845 format => GL_ALPHA;
846
847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
848 glColor 0.45, 0.45, 0.45, 1;
849 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
850 glBindTexture GL_TEXTURE_2D, $lighting->{name};
851 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
852
853 $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32);
595 854
596 glDisable GL_TEXTURE_2D; 855 glDisable GL_TEXTURE_2D;
597 glDisable GL_BLEND; 856 glDisable GL_BLEND;
598} 857}
599 858
646 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 905 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
647 $::CONN->send ("command run_stop"); 906 $::CONN->send ("command run_stop");
648 } 907 }
649} 908}
650 909
6511; 910#############################################################################
652 911
912package Crossfire::Client::Widget::Animator;
913
914use SDL::OpenGL;
915
916our @ISA = Crossfire::Client::Widget::Bin::;
917
918sub moveto {
919 my ($self, $x, $y) = @_;
920
921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
922 $self->{speed} = 0.001;
923 $self->{time} = 1;
924
925 ::animation_start $self;
926}
927
928sub animate {
929 my ($self, $interval) = @_;
930
931 printf "%5.2f\n", 1 / $interval;#d#
932
933 $self->{time} -= $interval * $self->{speed};
934 if ($self->{time} <= 0) {
935 $self->{time} = 0;
936 ::animation_stop $self;
937 }
938
939 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
940
941 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
942 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
943}
944
945sub _draw {
946 my ($self) = @_;
947
948 glPushMatrix;
949 glRotate $self->{time} * 1000, 0, 1, 0;
950 $self->{children}[0]->draw;
951 glPopMatrix;
952}
953
954#############################################################################
955
956package Crossfire::Client::Widget::Toplevel;
957
958our @ISA = Crossfire::Client::Widget::Container::;
959
960sub size_request {
961 ($::WIDTH, $::HEIGHT)
962}
963
964sub size_allocate {
965 my ($self, $w, $h) = @_;
966
967 $self->SUPER::size_allocate ($w, $h);
968
969 $_->size_allocate ($_->size_request)
970 for @{$self->{children}};
971}
972
973sub update {
974 my ($self) = @_;
975
976 $self->size_allocate ($self->size_request);
977 ::refresh ();
978}
979
980sub add {
981 my ($self, $widget) = @_;
982
983 $self->SUPER::add ($widget);
984
985 $widget->size_allocate ($widget->size_request);
986}
987
988sub draw {
989 my ($self) = @_;
990
991 $self->_draw;
992}
993
994#############################################################################
995
996package Crossfire::Client::Widget;
997
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
999
10001
1001

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines