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.39 by root, Sun Apr 9 21:39:08 2006 UTC vs.
Revision 1.61 by root, Tue Apr 11 13:17:24 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
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 CFClient;
11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
11 16
12# class methods for events 17# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
14sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
15sub feed_sdl_button_down_event { } 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, $GRAB->translate ($x, $y)) if $GRAB;
40}
41
16sub feed_sdl_button_up_event { } 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 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51
52 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab;
55 $GRAB->update if $GRAB;
56 }
57}
58
59sub feed_sdl_motion_event {
60 my ($ev) = @_;
61 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
62
63 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
64
65 if ($widget != $HOVER) {
66 my $hover = $HOVER; $HOVER = $widget;
67
68 $hover->update if $hover;
69 $HOVER->update if $HOVER;
70 }
71
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73}
17 74
18sub new { 75sub new {
19 my $class = shift; 76 my $class = shift;
20 77
21 bless { @_ }, $class 78 bless { @_ }, $class
37 Carp::confess "size_request is abtract"; 94 Carp::confess "size_request is abtract";
38} 95}
39 96
40sub size_allocate { 97sub size_allocate {
41 my ($self, $w, $h) = @_; 98 my ($self, $w, $h) = @_;
99
42 $self->w ($w); 100 $self->{w} = $w;
43 $self->h ($h); 101 $self->{h} = $h;
102}
103
104# translate global koordinates to local coordinate system
105sub translate {
106 my ($self, $x, $y) = @_;
107
108 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
44} 109}
45 110
46sub focus_in { 111sub focus_in {
47 my ($widget) = @_; 112 my ($self) = @_;
48 $FOCUS = $widget; 113
114 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus;
116 $FOCUS->update;
49} 117}
50 118
51sub focus_out { 119sub focus_out {
52 my ($widget) = @_; 120 my ($self) = @_;
53}
54 121
55sub key_down { 122 return unless $FOCUS == $self;
56 my ($widget, $sdlev) = @_;
57}
58 123
59sub key_up { 124 my $focus = $FOCUS; undef $FOCUS;
60 my ($widget, $sdlev) = @_; 125 $focus->update if $focus; #?
61} 126}
62 127
128sub mouse_motion { }
129sub button_up { }
63sub button_down { 130sub button_down { }
64 my ($widget, $sdlev) = @_; 131sub key_down { }
65} 132sub key_up { }
66 133
67sub button_up {
68 my ($widget, $sdlev) = @_;
69}
70
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 139
77sub draw { 140sub draw {
78 my ($self) = @_; 141 my ($self) = @_;
79 142
80 glPushMatrix; 143 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 144 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 145 $self->_draw;
146 if ($self == $HOVER) {
147 glColor 1, 1, 1, 0.4;
148 glEnable GL_BLEND;
149 glBegin GL_QUADS;
150 glVertex 0 , 0;
151 glVertex $self->{w}, 0;
152 glVertex $self->{w}, $self->{h};
153 glVertex 0 , $self->{h};
154 glEnd;
155 glDisable GL_BLEND;
156 }
83 glPopMatrix; 157 glPopMatrix;
84} 158}
85 159
86sub _draw { 160sub _draw {
87 my ($self) = @_; 161 my ($self) = @_;
136 #$self->deactivate; 210 #$self->deactivate;
137} 211}
138 212
139############################################################################# 213#############################################################################
140 214
141package Crossfire::Client::Widget::Container; 215package CFClient::Widget::Container;
142 216
143our @ISA = Crossfire::Client::Widget::; 217our @ISA = CFClient::Widget::;
144 218
145sub new { 219sub new {
146 my ($class, @widgets) = @_; 220 my ($class, @widgets) = @_;
147 221
148 my $self = $class->SUPER::new (children => []); 222 my $self = $class->SUPER::new (children => []);
159 233
160 @{$self->{children}} = 234 @{$self->{children}} =
161 sort { $a->{z} <=> $b->{z} } 235 sort { $a->{z} <=> $b->{z} }
162 @{$self->{children}}, $chld; 236 @{$self->{children}}, $chld;
163 237
164 $self->size_allocate ($self->{w}, $self->{h}); 238 $self->size_allocate ($self->{w}, $self->{h})
239 if $self->{w}; #TODO: check for "realised state"
165} 240}
166 241
167sub remove { 242sub remove {
168 my ($self, $widget) = @_; 243 my ($self, $widget) = @_;
169 244
173} 248}
174 249
175sub find_widget { 250sub find_widget {
176 my ($self, $x, $y) = @_; 251 my ($self, $x, $y) = @_;
177 252
253 $x -= $self->{x};
254 $y -= $self->{y};
255
178 my $res; 256 my $res;
179 257
180 for (@{ $self->{children} }) { 258 for (reverse @{ $self->{children} }) {
181 $res = $_->find_widget ($x, $y) 259 $res = $_->find_widget ($x, $y)
182 and return $res; 260 and return $res;
183 } 261 }
184 262
185 () 263 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
186}
187
188sub size_request {
189 my ($self) = @_;
190
191 my ($hs, $ws) = (0, 0);
192 for (@{$self->{children} || []}) {
193 my ($w, $h) = $_->size_request;
194 $hs += $h;
195 if ($ws < $w) { $ws = $w }
196 }
197
198 return ($ws, $hs);
199} 264}
200 265
201sub _draw { 266sub _draw {
202 my ($self) = @_; 267 my ($self) = @_;
203 268
204 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
205} 270}
206 271
207############################################################################# 272#############################################################################
208 273
209package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
210 275
211our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
212 277
213sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
214 279
215sub size_request { 280sub size_request {
216 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
217} 282}
218 283
219sub size_allocate { 284sub size_allocate {
220 my ($self, $w, $h) = @_; 285 my ($self, $w, $h) = @_;
286
221 $self->SUPER::size_allocate ($w, $h); 287 $self->SUPER::size_allocate ($w, $h);
222 $self->{children}[0]->size_allocate ($w, $h) 288 $self->{children}[0]->size_allocate ($w, $h)
223 if $self->{children}[0] 289 if $self->{children}[0]
224} 290}
225 291
226############################################################################# 292#############################################################################
227 293
228package Crossfire::Client::Widget::Toplevel; 294package CFClient::Widget::Window;
229 295
230our @ISA = Crossfire::Client::Widget::Container::; 296our @ISA = CFClient::Widget::Bin::;
297
298use SDL::OpenGL;
299
300sub new {
301 my ($class, $x, $y, $z, $w, $h) = @_;
302
303 my $self = $class->SUPER::new;
304
305 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
306}
231 307
232sub update { 308sub update {
233 my ($self) = @_; 309 my ($self) = @_;
234 310
235 ::refresh ();
236}
237
238#############################################################################
239
240package Crossfire::Client::Widget::Window;
241
242our @ISA = Crossfire::Client::Widget::Bin::;
243
244use SDL::OpenGL;
245
246sub add {
247 my ($self, $chld) = @_;
248 warn "ADD $chld\n";
249 $self->SUPER::add ($chld);
250 $chld->set_parent ($self);
251}
252
253sub remove {
254 my ($self) = @_;
255 # TODO FIXME: removing a child from a window will crash, see render_chld
256 # $self->update;
257}
258
259sub update {
260 my ($self) = @_;
261 $self->render_chld; 311 $self->render_chld;
312 $self->SUPER::update;
262} 313}
263 314
264sub render_chld { 315sub render_chld {
265 my ($self) = @_; 316 my ($self) = @_;
266 my $chld = $self->get;
267 my ($w, $h) = $self->size_request;
268 317
269 require Carp;
270 Carp::cluck "RENDERCHI $w $h";
271 warn "RENDERCHI $w $h\n";
272 $self->{texture} = 318 $self->{texture} =
273 Crossfire::Client::Texture->new_from_opengl ( 319 CFClient::Texture->new_from_opengl (
274 $w, $h, sub { $chld->draw } 320 $self->{w}, $self->{h}, sub { $self->child->draw }
275 ); 321 );
276 $self->{texture}->upload;
277}
278
279sub size_request {
280 my ($self) = @_;
281 ($self->w, $self->h)
282} 322}
283 323
284sub size_allocate { 324sub size_allocate {
285 my ($self, $w, $h) = @_; 325 my ($self, $w, $h) = @_;
286 326
287 $self->w ($w); 327 $self->{w} = $w;
288 $self->h ($h); 328 $self->{h} = $h;
329
289 $self->get->size_allocate ($w, $h); 330 $self->child->size_allocate ($w, $h);
290 331
291 $self->update; #TODO: Move this to the size_request event propably? 332 $self->render_chld;
292} 333}
293 334
294sub _draw { 335sub _draw {
295 my ($self) = @_; 336 my ($self) = @_;
296 337
300 or return; 341 or return;
301 342
302 glEnable GL_BLEND; 343 glEnable GL_BLEND;
303 glEnable GL_TEXTURE_2D; 344 glEnable GL_TEXTURE_2D;
304 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
305 glBindTexture GL_TEXTURE_2D, $tex->{name};
306 346
307 glBegin GL_QUADS; 347 $tex->draw_quad (0, 0, $w, $h);
308 glTexCoord 0, 0; glVertex 0, 0;
309 glTexCoord 0, 1; glVertex 0, $h;
310 glTexCoord 1, 1; glVertex $w, $h;
311 glTexCoord 1, 0; glVertex $w, 0;
312 glEnd;
313 348
314 glDisable GL_BLEND; 349 glDisable GL_BLEND;
315 glDisable GL_TEXTURE_2D; 350 glDisable GL_TEXTURE_2D;
316} 351}
317 352
318############################################################################# 353#############################################################################
319 354
320package Crossfire::Client::Widget::Frame; 355package CFClient::Widget::Frame;
321 356
322our @ISA = Crossfire::Client::Widget::Bin::; 357our @ISA = CFClient::Widget::Bin::;
323 358
324use SDL::OpenGL; 359use SDL::OpenGL;
325 360
326sub size_request { 361sub size_request {
327 my ($self) = @_; 362 my ($self) = @_;
333 map { $_ + 4 } $chld->size_request; 368 map { $_ + 4 } $chld->size_request;
334} 369}
335 370
336sub size_allocate { 371sub size_allocate {
337 my ($self, $w, $h) = @_; 372 my ($self, $w, $h) = @_;
338 373
339 $self->w ($w); 374 $self->{w} = $w;
340 $self->h ($h); 375 $self->{h} = $h;
341 376
342 $self->child->size_allocate ($w - 4, $h - 4); 377 $self->child->size_allocate ($w - 4, $h - 4);
343 $self->child->move (2, 2); 378 $self->child->move (2, 2);
344} 379}
345 380
350 385
351 my ($w, $h) = $chld->size_request; 386 my ($w, $h) = $chld->size_request;
352 387
353 glBegin GL_QUADS; 388 glBegin GL_QUADS;
354 glColor 0, 0, 0; 389 glColor 0, 0, 0;
355 glTexCoord 0, 0; glVertex 0 , 0; 390 glVertex 0 , 0;
356 glTexCoord 0, 1; glVertex 0 , $h + 4; 391 glVertex 0 , $h + 4;
357 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 392 glVertex $w + 4 , $h + 4;
358 glTexCoord 1, 0; glVertex $w + 4 , 0; 393 glVertex $w + 4 , 0;
359 glEnd; 394 glEnd;
360 395
361 $chld->draw; 396 $chld->draw;
362} 397}
363 398
364############################################################################# 399#############################################################################
365 400
366package Crossfire::Client::Widget::FancyFrame; 401package CFClient::Widget::FancyFrame;
367 402
368our @ISA = Crossfire::Client::Widget::Frame::; 403our @ISA = CFClient::Widget::Bin::;
369 404
370use SDL::OpenGL; 405use SDL::OpenGL;
371 406
372sub new { 407my @tex =
373 my ($self, $theme) = @_;
374 $self = $self->SUPER::new;
375
376 $self->{txts} = [
377 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 408 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
378 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 409 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
379 ];
380 $self
381}
382 410
383sub size_request { 411sub size_request {
384 my ($self) = @_; 412 my ($self) = @_;
385 413
386 my ($w, $h) = $self->SUPER::size_request; 414 my ($w, $h) = $self->SUPER::size_request;
387 415
388 $h += $self->{txts}->[1]->{height}; 416 $h += $tex[1]->{height};
389 $h += $self->{txts}->[4]->{height}; 417 $h += $tex[4]->{height};
390 $w += $self->{txts}->[2]->{width}; 418 $w += $tex[2]->{width};
391 $w += $self->{txts}->[3]->{width}; 419 $w += $tex[3]->{width};
392 420
393 ($w, $h) 421 ($w, $h)
394} 422}
395 423
396sub size_allocate { 424sub size_allocate {
397 my ($self, $w, $h) = @_; 425 my ($self, $w, $h) = @_;
398 426
399 $self->w ($w); 427 $self->SUPER::size_allocate ($w, $h);
400 $self->h ($h); 428
401 $h -= $self->{txts}->[1]->{height}; 429 $h -= $tex[1]->{height};
402 $h -= $self->{txts}->[4]->{height}; 430 $h -= $tex[4]->{height};
403 $w -= $self->{txts}->[2]->{width}; 431 $w -= $tex[2]->{width};
404 $w -= $self->{txts}->[3]->{width}; 432 $w -= $tex[3]->{width};
405 433
406 $h = $h < 0 ? 0 : $h; 434 $h = $h < 0 ? 0 : $h;
407 $w = $w < 0 ? 0 : $w; 435 $w = $w < 0 ? 0 : $w;
408 warn "CHILD:$w $h\n"; 436
409 $self->child->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
410 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 438 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
411} 439}
412 440
413sub _draw { 441sub _draw {
414 my ($self) = @_; 442 my ($self) = @_;
415 443
416 my ($w, $h) = ($self->w, $self->h); 444 my ($w, $h) = ($self->{w}, $self->{h});
417 my ($cw, $ch) = ($self->child->w, $self->child->h); 445 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
418 446
419 glEnable GL_BLEND; 447 glEnable GL_BLEND;
420 glEnable GL_TEXTURE_2D; 448 glEnable GL_TEXTURE_2D;
421 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
422 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 450 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
423 451
424 my $top = $self->{txts}->[1]; 452 my $top = $tex[1];
425 glBindTexture GL_TEXTURE_2D, $top->{name}; 453 $top->draw_quad (0, 0, $w, $top->{height});
426 454
427 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex 0 , 0;
429 glTexCoord 0, 1; glVertex 0 , $top->{height};
430 glTexCoord 1, 1; glVertex $w , $top->{height};
431 glTexCoord 1, 0; glVertex $w , 0;
432 glEnd;
433
434 my $left = $self->{txts}->[3]; 455 my $left = $tex[3];
435 glBindTexture GL_TEXTURE_2D, $left->{name}; 456 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
436 457
437 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex 0 , $top->{height};
439 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
440 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
441 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
442 glEnd;
443
444 my $right = $self->{txts}->[2]; 458 my $right = $tex[2];
445 glBindTexture GL_TEXTURE_2D, $right->{name}; 459 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
446 460
447 glBegin GL_QUADS;
448 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
449 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
450 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
451 glTexCoord 1, 0; glVertex $w , $top->{height};
452 glEnd;
453
454 my $bottom = $self->{txts}->[4]; 461 my $bottom = $tex[4];
455 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 462 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
456 463
457 glBegin GL_QUADS; 464 my $bg = $tex[0];
458 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
459 glTexCoord 0, 1; glVertex 0 , $h;
460 glTexCoord 1, 1; glVertex $w , $h;
461 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
462 glEnd;
463
464 my $bg = $self->{txts}->[0];
465 glBindTexture GL_TEXTURE_2D, $bg->{name}; 465 glBindTexture GL_TEXTURE_2D, $bg->{name};
466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
469 469
470 my $rep_x = $cw / $bg->{width}; 470 my $rep_x = $cw / $bg->{width};
471 my $rep_y = $ch / $bg->{height}; 471 my $rep_y = $ch / $bg->{height};
472 472
473 glBegin GL_QUADS; 473 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
474 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
475 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
476 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
477 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
478 glEnd;
479 474
480 glDisable GL_BLEND; 475 glDisable GL_BLEND;
481 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
482 477
483 $self->child->draw; 478 $self->child->draw;
484 479
485} 480}
486 481
487############################################################################# 482#############################################################################
488 483
489package Crossfire::Client::Widget::Table; 484package CFClient::Widget::Table;
490 485
491our @ISA = Crossfire::Client::Widget::Bin::; 486our @ISA = CFClient::Widget::Bin::;
492 487
493use SDL::OpenGL; 488use SDL::OpenGL;
494 489
495sub add { 490sub add {
496 my ($self, $x, $y, $chld) = @_; 491 my ($self, $x, $y, $chld) = @_;
571 } 566 }
572} 567}
573 568
574############################################################################# 569#############################################################################
575 570
576package Crossfire::Client::Widget::VBox; 571package CFClient::Widget::VBox;
577 572
578our @ISA = Crossfire::Client::Widget::Container::; 573our @ISA = CFClient::Widget::Container::;
579 574
580use SDL::OpenGL; 575use SDL::OpenGL;
576
577sub size_request {
578 my ($self) = @_;
579
580 my @alloc = map [$_->size_request], @{$self->{children}};
581
582 (
583 (List::Util::max map $_->[0], @alloc),
584 (List::Util::sum map $_->[1], @alloc),
585 )
586}
581 587
582sub size_allocate { 588sub size_allocate {
583 my ($self, $w, $h) = @_; 589 my ($self, $w, $h) = @_;
584 590
585 $self->w ($w); 591 $self->w ($w);
618 $y += $h; 624 $y += $h;
619 } 625 }
620 } 626 }
621} 627}
622 628
623sub _draw {
624 my ($self) = @_;
625
626 my ($x, $y);
627 for (@{$self->{children} || []}) {
628 $_->draw;
629 $y += $_->h;
630 }
631}
632
633############################################################################# 629#############################################################################
634 630
635package Crossfire::Client::Widget::Label; 631package CFClient::Widget::Label;
636 632
637our @ISA = Crossfire::Client::Widget::; 633our @ISA = CFClient::Widget::;
638 634
639use SDL::OpenGL; 635use SDL::OpenGL;
640 636
641sub new { 637sub new {
642 my ($class, $x, $y, $z, $height, $text) = @_; 638 my ($class, $x, $y, $z, $height, $text) = @_;
643 639
640 $height ||= $::FONTSIZE;
641
644 # TODO: color, and make height, xyz etc. optional 642 # TODO: color, and make height, xyz etc. optional
645 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 643 my $self = $class->SUPER::new (
644 x => $x,
645 y => $y,
646 z => $z,
647 height => $height,
648 layout => new CFClient::Layout $height,
649 );
646 650
647 $self->set_text ($text); 651 $self->set_text ($text);
648 652
649 $self 653 $self
650} 654}
651 655
652sub set_text { 656sub set_text {
653 my ($self, $text) = @_; 657 my ($self, $text) = @_;
654 658
655 $self->{text} = $text; 659 $self->{text} = $text;
656 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 660 $self->{layout}->set_markup ($text);
657 661
658 $self->update; 662 delete $self->{texture};
659} 663}
660 664
661sub get_text { 665sub get_text {
662 my ($self, $text) = @_; 666 my ($self, $text) = @_;
663 667
665} 669}
666 670
667sub size_request { 671sub size_request {
668 my ($self) = @_; 672 my ($self) = @_;
669 673
670 ( 674 $self->{layout}->set_width;
675 $self->{layout}->size
676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
677# (
671 $self->{texture}{width}, 678# $self->{texture}{width},
672 $self->{texture}{height}, 679# $self->{texture}{height},
673 ) 680# )
681# } else {
682# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
683#
684# ($w, $h)
685# }
686}
687
688sub size_allocate {
689 my ($self, $w, $h) = @_;
690
691 $self->SUPER::size_allocate ($w, $h);
692 delete $self->{texture};
674} 693}
675 694
676sub _draw { 695sub _draw {
677 my ($self) = @_; 696 my ($self) = @_;
678 697
679 my $tex = $self->{texture}; 698 my $tex = $self->{texture} ||= do {
699 $self->{layout}->set_width ($self->{w});
700 new_from_layout CFClient::Texture $self->{layout};
701 };
680 702
681 glEnable GL_BLEND; 703 glEnable GL_BLEND;
682 glEnable GL_TEXTURE_2D; 704 glEnable GL_TEXTURE_2D;
683 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
684 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
685 glBindTexture GL_TEXTURE_2D, $tex->{name};
686 707
687 glColor 1, 0, 0, 1; # TODO color 708 glColor 1, 0, 0, 1; # TODO color
688 709
689 glBegin GL_QUADS; 710 $tex->draw_quad (0, 0);
690 glTexCoord 0, 0; glVertex 0 , 0;
691 glTexCoord 0, 1; glVertex 0 , $tex->{height};
692 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
693 glTexCoord 1, 0; glVertex $tex->{width}, 0;
694 glEnd;
695 711
696 glDisable GL_BLEND; 712 glDisable GL_BLEND;
697 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
698} 714}
699 715
700############################################################################# 716#############################################################################
701 717
702package Crossfire::Client::Widget::TextEntry; 718package CFClient::Widget::Entry;
703 719
704our @ISA = Crossfire::Client::Widget::Label::; 720our @ISA = CFClient::Widget::Label::;
705 721
706use SDL; 722use SDL;
707use SDL::OpenGL; 723use SDL::OpenGL;
708 724
709sub key_down { 725sub key_down {
710 my ($self, $ev) = @_; 726 my ($self, $ev) = @_;
711 727
712 my $mod = $ev->key_mod; 728 my $mod = $ev->key_mod;
713 my $sym = $ev->key_sym; 729 my $sym = $ev->key_sym;
714 730
715 $ev->set_unicode (1);
716 my $uni = $ev->key_unicode; 731 my $uni = $ev->key_unicode;
717 732
718 my $text = $self->get_text; 733 my $text = $self->get_text;
719 734
720 if ($sym == SDLK_BACKSPACE) { 735 if ($sym == SDLK_BACKSPACE) {
721 substr $text, -1, 1, ''; 736 substr $text, -1, 1, '';
722
723 } elsif ($uni) { 737 } elsif ($uni) {
724 $text .= chr $uni; 738 $text .= chr $uni;
725 } 739 }
740
726 $self->set_text ($text); 741 $self->set_text ($text);
727} 742}
728 743
729############################################################################# 744sub button_down {
745 my ($self, $ev) = @_;
730 746
747 $self->focus_in;
748}
749
750sub mouse_motion {
751 my ($self, $ev, $x, $y) = @_;
752 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
753}
754
755sub _draw {
756 my ($self) = @_;
757
758 if ($FOCUS == $self) {
759 glColor 1, 1, 1;
760 } else {
761 glColor 0.7, 0.7, 0.7;
762 }
763
764 glBegin GL_QUADS;
765 glVertex 0 , 0;
766 glVertex 0 , $self->{h} - 1;
767 glVertex $self->{w} - 1, $self->{h} - 1;
768 glVertex $self->{w} - 1, 0;
769 glEnd;
770
771 $self->SUPER::_draw;
772}
773
774#############################################################################
775
731package Crossfire::Client::Widget::MapWidget; 776package CFClient::Widget::MapWidget;
732 777
733use strict; 778use strict;
734 779
735use List::Util qw(min max); 780use List::Util qw(min max);
736 781
737use SDL; 782use SDL;
738use SDL::OpenGL; 783use SDL::OpenGL;
739use SDL::OpenGL::Constants; 784use SDL::OpenGL::Constants;
740 785
741our @ISA = Crossfire::Client::Widget::; 786our @ISA = CFClient::Widget::;
742 787
743sub key_down { 788sub key_down {
744 print "MAPKEYDOWN\n"; 789 print "MAPKEYDOWN\n";
745} 790}
746 791
747sub key_up { 792sub key_up {
748} 793}
749 794
750sub size_request { 795sub size_request {
751 796 (
752} 797 1 + int $::WIDTH / 32,
753 798 1 + int $::HEIGHT / 32,
754sub size_allocate { 799 )
755} 800}
756 801
757sub _draw { 802sub _draw {
758 my ($self) = @_; 803 my ($self) = @_;
759 804
782 glEnable GL_TEXTURE_2D; 827 glEnable GL_TEXTURE_2D;
783 glEnable GL_BLEND; 828 glEnable GL_BLEND;
784 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 829 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
785 830
786 my $sw4 = ($sw + 3) & ~3; 831 my $sw4 = ($sw + 3) & ~3;
787 my $lighting = "\x00" x ($sw4 * $sh); 832 my $darkness = "\x00" x ($sw4 * $sh);
788 833
789 for my $x (0 .. $sw - 1) { 834 for my $x (0 .. $sw - 1) {
835 my $row = $map->[$x + $xofs];
790 for my $y (0 .. $sh - 1) { 836 for my $y (0 .. $sh - 1) {
791 837
792 my $cell = $map->[$x + $xofs][$y + $yofs] 838 my $cell = $row->[$y + $yofs]
793 or next; 839 or next;
794 840
795 my $darkness = $cell->[0] * (1 / 255); 841 my $dark = $cell->[0];
796 if ($darkness < 0) { 842 if ($dark < 0) {
797 $darkness = 0.15; 843 substr $darkness, $y * $sw4 + $x, 1, chr 224;
844 } else {
845 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
798 } 846 }
799 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
800 847
801 for my $num (grep $_, @$cell[1,2,3]) { 848 for my $num (grep $_, @$cell[1,2,3]) {
802 my $tex = $::CONN->{face}[$num]{texture} || next; 849 my $tex = $::CONN->{face}[$num]{texture} || next;
803 850
804 glBindTexture GL_TEXTURE_2D, $tex->{name};
805
806 my $w = $tex->{width}; 851 my $w = $tex->{width};
807 my $h = $tex->{height}; 852 my $h = $tex->{height};
808 853
809 my $px = ($x + 1) * 32 - $w; 854 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
810 my $py = ($y + 1) * 32 - $h;
811
812 glBegin GL_QUADS;
813 glTexCoord 0, 0; glVertex $px , $py;
814 glTexCoord 0, 1; glVertex $px , $py + $h;
815 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
816 glTexCoord 1, 0; glVertex $px + $w, $py;
817 glEnd;
818 } 855 }
819 } 856 }
820 } 857 }
821 858
822# if (1) { # higher quality darkness 859# if (1) { # higher quality darkness
827# 864#
828# $lighting = $pb->get_pixels; 865# $lighting = $pb->get_pixels;
829# $lighting =~ s/(.)../$1/gs; 866# $lighting =~ s/(.)../$1/gs;
830# } 867# }
831 868
832 $lighting = new Crossfire::Client::Texture 869 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
870 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
871
872 $darkness = new CFClient::Texture
833 width => $sw4, 873 width => $sw4,
834 height => $sh, 874 height => $sh,
835 data => $lighting, 875 data => $darkness,
836 internalformat => GL_ALPHA4, 876 internalformat => GL_ALPHA,
837 format => GL_ALPHA; 877 format => GL_ALPHA;
838 878
839 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 879 glColor 0.45, 0.45, 0.45, 1;
840 glColor 0, 0, 0, 0.75; 880 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
841 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
842 glBindTexture GL_TEXTURE_2D, $lighting->{name};
843 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
844 glBegin GL_QUADS;
845 glTexCoord 0, 0; glVertex 0 , 0;
846 glTexCoord 0, 1; glVertex 0 , $sh * 32;
847 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
848 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
849 glEnd;
850 881
851 glDisable GL_TEXTURE_2D; 882 glDisable GL_TEXTURE_2D;
852 glDisable GL_BLEND; 883 glDisable GL_BLEND;
853} 884}
854 885
903 } 934 }
904} 935}
905 936
906############################################################################# 937#############################################################################
907 938
908package Crossfire::Client::Widget::Animator; 939package CFClient::Widget::Animator;
909 940
910use SDL::OpenGL; 941use SDL::OpenGL;
911 942
912our @ISA = Crossfire::Client::Widget::Bin::; 943our @ISA = CFClient::Widget::Bin::;
913 944
914sub moveto { 945sub moveto {
915 my ($self, $x, $y) = @_; 946 my ($self, $x, $y) = @_;
916 947
917 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 948 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
918 $self->{speed} = 0.2; 949 $self->{speed} = 0.001;
919 $self->{time} = 1; 950 $self->{time} = 1;
920 951
921 ::animation_start $self; 952 ::animation_start $self;
922} 953}
923 954
938 969
939sub _draw { 970sub _draw {
940 my ($self) = @_; 971 my ($self) = @_;
941 972
942 glPushMatrix; 973 glPushMatrix;
943 glRotate $self->{time} * 10000, 0, 1, 0; 974 glRotate $self->{time} * 1000, 0, 1, 0;
944 $self->{children}[0]->draw; 975 $self->{children}[0]->draw;
945 glPopMatrix; 976 glPopMatrix;
946} 977}
947 978
9481; 979#############################################################################
949 980
981package CFClient::Widget::Toplevel;
982
983our @ISA = CFClient::Widget::Container::;
984
985sub size_request {
986 ($::WIDTH, $::HEIGHT)
987}
988
989sub size_allocate {
990 my ($self, $w, $h) = @_;
991
992 $self->SUPER::size_allocate ($w, $h);
993
994 $_->size_allocate ($_->size_request)
995 for @{$self->{children}};
996}
997
998sub translate {
999 my ($self, $x, $y) = @_;
1000
1001 ($x, $y)
1002}
1003
1004sub update {
1005 my ($self) = @_;
1006
1007 $self->size_allocate ($self->size_request);
1008 ::refresh ();
1009}
1010
1011sub add {
1012 my ($self, $widget) = @_;
1013
1014 $self->SUPER::add ($widget);
1015
1016 $widget->size_allocate ($widget->size_request);
1017}
1018
1019sub draw {
1020 my ($self) = @_;
1021
1022 $self->_draw;
1023}
1024
1025#############################################################################
1026
1027package CFClient::Widget;
1028
1029$TOPLEVEL = new CFClient::Widget::Toplevel;
1030
10311
1032

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines