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.35 by root, Sun Apr 9 17:34:15 2006 UTC vs.
Revision 1.51 by root, Mon Apr 10 11:41:17 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;
11#our @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;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
16sub 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) if $GRAB;
40}
41
17sub 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 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} 77}
24
25#sub activate {
26# push @ACTIVE_WIDGETS, $_[0];
27# Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28#}
29
30#sub deactivate {
31# @ACTIVE_WIDGETS =
32# sort { $a->{z} <=> $b->{z} }
33# grep { $_ && $_ != $_[0] }
34# @ACTIVE_WIDGETS;
35#}
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;
40 $self->{y} = $y; 82 $self->{y} = $y;
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 ($self) = @_; 158 my ($self) = @_;
98 my ($w, $h) = $self->size_request; 159 my ($w, $h) = $self->size_request;
102 $self->{x} = $w, 163 $self->{x} = $w,
103 $self->{y} = $h 164 $self->{y} = $h
104 ) 165 )
105} 166}
106 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
107sub del_parent { $_[0]->{parent} = undef } 178sub del_parent { $_[0]->{parent} = undef }
108 179
109sub set_parent { 180sub set_parent {
110 my ($self, $par) = @_; 181 my ($self, $par) = @_;
111 182
128 my ($self) = @_; 199 my ($self) = @_;
129 200
130 #$self->deactivate; 201 #$self->deactivate;
131} 202}
132 203
204#############################################################################
205
206package Crossfire::Client::Widget::Container;
207
208our @ISA = Crossfire::Client::Widget::;
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
133package Crossfire::Client::Widget::Bin; 265package Crossfire::Client::Widget::Bin;
134 266
135our @ISA = Crossfire::Client::Widget::; 267our @ISA = Crossfire::Client::Widget::Container::;
136 268
137sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update }
138
139sub get { $_[0]->{child} } 269sub child { $_[0]->{children}[0] }
140
141sub remove {
142 my ($self, $chld) = @_;
143 delete $self->{child}
144 if $self->{child} == $chld;
145}
146 270
147sub size_request { 271sub size_request {
148 $_[0]->{child}->size_request if $_[0]->{child} 272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
149} 273}
150 274
151sub _draw { 275sub size_allocate {
152 my ($self) = @_; 276 my ($self, $w, $h) = @_;
153 277
154 $self->{child}->draw; 278 $self->SUPER::size_allocate ($w, $h);
279 $self->{children}[0]->size_allocate ($w, $h)
280 if $self->{children}[0]
155} 281}
156 282
283#############################################################################
284
157package Crossfire::Client::Widget::Toplevel; 285package Crossfire::Client::Widget::Window;
158 286
159our @ISA = Crossfire::Client::Widget::; 287our @ISA = Crossfire::Client::Widget::Bin::;
160 288
161use SDL::OpenGL; 289use SDL::OpenGL;
162 290
163sub add { 291sub new {
164 my ($self, $chld) = @_; 292 my ($class, $x, $y, $z, $w, $h) = @_;
165 293
166 push @{$self->{childs}}, $chld; 294 my $self = $class->SUPER::new;
167 @{$self->{childs}} =
168 sort { $a->{z} <=> $b->{z} }
169 @{$self->{childs}};
170 295
171 $chld->set_parent ($self); 296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
172}
173
174sub remove {
175 my ($self, $chld) = @_;
176 @{$self->{childs}} =
177 sort { $a->{z} <=> $b->{z} }
178 grep { $_ && $_ != $_[0] }
179 @{$self->{childs}}
180} 297}
181 298
182sub update { 299sub update {
183 my ($self) = @_; 300 my ($self) = @_;
184 ::refresh ();
185}
186 301
187sub _draw {
188 my ($self) = @_;
189
190 $_->draw for @{$self->{childs}};
191}
192
193package Crossfire::Client::Widget::Window;
194
195our @ISA = Crossfire::Client::Widget::Bin::;
196
197use SDL::OpenGL;
198
199sub add {
200 my ($self, $chld) = @_;
201 $self->SUPER::add ($chld);
202 $chld->set_parent ($self);
203 $self->update; #TODO: Move this to the size_request event propably?
204}
205
206sub remove {
207 my ($self) = @_;
208 # TODO FIXME: removing a child from a window will crash, see render_chld
209 $self->update;
210}
211
212sub update {
213 my ($self) = @_;
214 $self->render_chld; 302 $self->render_chld;
303 $self->SUPER::update;
215} 304}
216 305
217sub render_chld { 306sub render_chld {
218 my ($self) = @_; 307 my ($self) = @_;
219 my $chld = $self->get;
220 my ($w, $h) = $self->size_request;
221 308
222 $self->{texture} = 309 $self->{texture} =
223 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
224 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
225 ); 312 );
226 $self->{texture}->upload;
227} 313}
228 314
229sub size_request { 315sub size_allocate {
230 my ($self) = @_; 316 my ($self, $w, $h) = @_;
231 my $chld = $self->get 317
232 or return (0, 0); 318 $self->{w} = $w;
233 $chld->size_request 319 $self->{h} = $h;
320
321 $self->child->size_allocate ($w, $h);
322
323 $self->render_chld;
234} 324}
235 325
236sub _draw { 326sub _draw {
237 my ($self) = @_; 327 my ($self) = @_;
238 328
239 my ($w, $h) = $self->size_request;#TODO# use width/height of texture 329 my ($w, $h) = ($self->w, $self->h);
240 330
241 my $tex = $self->{texture} 331 my $tex = $self->{texture}
242 or return; 332 or return;
243 333
244 glEnable GL_BLEND; 334 glEnable GL_BLEND;
255 345
256 glDisable GL_BLEND; 346 glDisable GL_BLEND;
257 glDisable GL_TEXTURE_2D; 347 glDisable GL_TEXTURE_2D;
258} 348}
259 349
350#############################################################################
351
260package Crossfire::Client::Widget::Frame; 352package Crossfire::Client::Widget::Frame;
261 353
262our @ISA = Crossfire::Client::Widget::Bin::; 354our @ISA = Crossfire::Client::Widget::Bin::;
263 355
264use SDL::OpenGL; 356use SDL::OpenGL;
265 357
266sub size_request { 358sub size_request {
267 my ($self) = @_; 359 my ($self) = @_;
268 my $chld = $self->get 360 my $chld = $self->child
269 or return (0, 0); 361 or return (0, 0);
270 362
271 $chld->move (2, 2); 363 $chld->move (2, 2);
272 364
273 map { $_ + 4 } $chld->size_request; 365 map { $_ + 4 } $chld->size_request;
274} 366}
275 367
368sub size_allocate {
369 my ($self, $w, $h) = @_;
370
371 $self->{w} = $w;
372 $self->{h} = $h;
373
374 $self->child->size_allocate ($w - 4, $h - 4);
375 $self->child->move (2, 2);
376}
377
276sub _draw { 378sub _draw {
277 my ($self) = @_; 379 my ($self) = @_;
278 380
279 my $chld = $self->get; 381 my $chld = $self->child;
280 382
281 my ($w, $h) = $chld->size_request; 383 my ($w, $h) = $chld->size_request;
282 384
283 glBegin GL_QUADS; 385 glBegin GL_QUADS;
284 glColor 0, 0, 0; 386 glColor 0, 0, 0;
289 glEnd; 391 glEnd;
290 392
291 $chld->draw; 393 $chld->draw;
292} 394}
293 395
396#############################################################################
397
294package Crossfire::Client::Widget::FancyFrame; 398package Crossfire::Client::Widget::FancyFrame;
295 399
296our @ISA = Crossfire::Client::Widget::Frame::; 400our @ISA = Crossfire::Client::Widget::Bin::;
297 401
298use SDL::OpenGL; 402use SDL::OpenGL;
299 403
300sub new { 404my @tex =
301 my ($self, $theme) = @_;
302 $self = $self->SUPER::new;
303
304 $self->{txts} = [
305 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 405 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
306 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 406 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
307 ];
308 $self
309
310}
311 407
312sub size_request { 408sub size_request {
313 my ($self) = @_; 409 my ($self) = @_;
410
314 my ($w, $h) = $self->get->size_request; 411 my ($w, $h) = $self->SUPER::size_request;
315 412
316 $h += $self->{txts}->[1]->{height}; 413 $h += $tex[1]->{height};
317 $h += $self->{txts}->[4]->{height}; 414 $h += $tex[4]->{height};
318 $w += $self->{txts}->[2]->{width}; 415 $w += $tex[2]->{width};
319 $w += $self->{txts}->[3]->{width}; 416 $w += $tex[3]->{width};
320
321 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
322 417
323 ($w, $h) 418 ($w, $h)
324} 419}
325 420
421sub size_allocate {
422 my ($self, $w, $h) = @_;
423
424 $self->SUPER::size_allocate ($w, $h);
425
426 $h -= $tex[1]->{height};
427 $h -= $tex[4]->{height};
428 $w -= $tex[2]->{width};
429 $w -= $tex[3]->{width};
430
431 $h = $h < 0 ? 0 : $h;
432 $w = $w < 0 ? 0 : $w;
433
434 $self->child->size_allocate ($w, $h);
435 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
436}
437
326sub _draw { 438sub _draw {
327 my ($self) = @_; 439 my ($self) = @_;
328 440
329 my ($w, $h) = $self->size_request; 441 my ($w, $h) = ($self->{w}, $self->{h});
330 my ($cw, $ch) = $self->get->size_request; 442 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
331 443
332 glEnable GL_BLEND; 444 glEnable GL_BLEND;
333 glEnable GL_TEXTURE_2D; 445 glEnable GL_TEXTURE_2D;
334 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 446 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
335 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
336 448
337
338 my $top = $self->{txts}->[1]; 449 my $top = $tex[1];
339 glBindTexture GL_TEXTURE_2D, $top->{name}; 450 glBindTexture GL_TEXTURE_2D, $top->{name};
340 451
341 glColor 1, 1, 1, 0.8;
342
343 glBegin GL_QUADS; 452 glBegin GL_QUADS;
344 glTexCoord 0, 0; glVertex 0 , 0; 453 glTexCoord 0, 0; glVertex 0 , 0;
345 glTexCoord 0, 1; glVertex 0 , $top->{height}; 454 glTexCoord 0, 1; glVertex 0 , $top->{height};
346 glTexCoord 1, 1; glVertex $w , $top->{height}; 455 glTexCoord 1, 1; glVertex $w , $top->{height};
347 glTexCoord 1, 0; glVertex $w , 0; 456 glTexCoord 1, 0; glVertex $w , 0;
348 glEnd; 457 glEnd;
349 458
350 my $left = $self->{txts}->[3]; 459 my $left = $tex[3];
351 glBindTexture GL_TEXTURE_2D, $left->{name}; 460 glBindTexture GL_TEXTURE_2D, $left->{name};
352 461
353 glColor 1, 1, 1, 0.8;
354
355 glBegin GL_QUADS; 462 glBegin GL_QUADS;
356 glTexCoord 0, 0; glVertex 0 , $top->{height}; 463 glTexCoord 0, 0; glVertex 0 , $top->{height};
357 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 464 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
358 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 465 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
359 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 466 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
360 glEnd; 467 glEnd;
361 468
362 my $right = $self->{txts}->[2]; 469 my $right = $tex[2];
363 glBindTexture GL_TEXTURE_2D, $right->{name}; 470 glBindTexture GL_TEXTURE_2D, $right->{name};
364 471
365 glColor 1, 1, 1, 0.8;
366
367 glBegin GL_QUADS; 472 glBegin GL_QUADS;
368 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 473 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
369 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 474 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
370 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 475 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
371 glTexCoord 1, 0; glVertex $w , $top->{height}; 476 glTexCoord 1, 0; glVertex $w , $top->{height};
372 glEnd; 477 glEnd;
373 478
374 my $bottom = $self->{txts}->[4]; 479 my $bottom = $tex[4];
375 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 480 glBindTexture GL_TEXTURE_2D, $bottom->{name};
376 481
377 glColor 1, 1, 1, 0.8;
378
379 glBegin GL_QUADS; 482 glBegin GL_QUADS;
380 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 483 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
381 glTexCoord 0, 1; glVertex 0 , $h; 484 glTexCoord 0, 1; glVertex 0 , $h;
382 glTexCoord 1, 1; glVertex $w , $h; 485 glTexCoord 1, 1; glVertex $w , $h;
383 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 486 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
384 glEnd; 487 glEnd;
385 488
386 my $bg = $self->{txts}->[0]; 489 my $bg = $tex[0];
387 glBindTexture GL_TEXTURE_2D, $bg->{name}; 490 glBindTexture GL_TEXTURE_2D, $bg->{name};
491 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
388 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 492 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
389 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 493 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
390 494
391 glColor 1, 1, 1, 0.8;
392
393 my $rep_x = $cw / $bg->{width}; 495 my $rep_x = $cw / $bg->{width};
394 my $rep_y = $ch / $bg->{height}; 496 my $rep_y = $ch / $bg->{height};
395 497
396 glBegin GL_QUADS; 498 glBegin GL_QUADS;
397 glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; 499 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
398 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; 500 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
399 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; 501 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
400 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; 502 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
401 glEnd; 503 glEnd;
402
403 $self->get->draw;
404 504
405 glDisable GL_BLEND; 505 glDisable GL_BLEND;
406 glDisable GL_TEXTURE_2D; 506 glDisable GL_TEXTURE_2D;
507
508 $self->child->draw;
509
407} 510}
511
512#############################################################################
408 513
409package Crossfire::Client::Widget::Table; 514package Crossfire::Client::Widget::Table;
410 515
411our @ISA = Crossfire::Client::Widget::Bin::; 516our @ISA = Crossfire::Client::Widget::Bin::;
412 517
413use SDL::OpenGL; 518use SDL::OpenGL;
414 519
415sub add { 520sub add {
416 my ($self, $x, $y, $chld) = @_; 521 my ($self, $x, $y, $chld) = @_;
417 my $old_chld = $self->{childs}[$y][$x]; 522 my $old_chld = $self->{children}[$y][$x];
418 523
419 $self->{childs}[$y][$x] = $chld; 524 $self->{children}[$y][$x] = $chld;
420 $chld->set_parent ($self); 525 $chld->set_parent ($self);
421 $self->update; 526 $self->update;
422} 527}
423 528
424sub max_row_height { 529sub max_row_height {
425 my ($self, $row) = @_; 530 my ($self, $row) = @_;
426 531
427 my $hs = 0; 532 my $hs = 0;
428 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 533 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
429 my $c = $self->{childs}->[$row]->[$xi]; 534 my $c = $self->{children}->[$row]->[$xi];
430 if ($c) { 535 if ($c) {
431 my ($w, $h) = $c->size_request; 536 my ($w, $h) = $c->size_request;
432 if ($hs < $h) { $hs = $h } 537 if ($hs < $h) { $hs = $h }
433 } 538 }
434 } 539 }
437 542
438sub max_col_width { 543sub max_col_width {
439 my ($self, $col) = @_; 544 my ($self, $col) = @_;
440 545
441 my $ws = 0; 546 my $ws = 0;
442 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 547 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
443 my $c = ($self->{childs}->[$yi] || [])->[$col]; 548 my $c = ($self->{children}->[$yi] || [])->[$col];
444 if ($c) { 549 if ($c) {
445 my ($w, $h) = $c->size_request; 550 my ($w, $h) = $c->size_request;
446 if ($ws < $w) { $ws = $w } 551 if ($ws < $w) { $ws = $w }
447 } 552 }
448 } 553 }
452sub size_request { 557sub size_request {
453 my ($self) = @_; 558 my ($self) = @_;
454 559
455 my ($hs, $ws) = (0, 0); 560 my ($hs, $ws) = (0, 0);
456 561
457 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 562 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
458 $hs += $self->max_row_height ($yi); 563 $hs += $self->max_row_height ($yi);
459 } 564 }
460 565
461 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 566 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
462 my $wm = 0; 567 my $wm = 0;
463 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 568 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
464 $wm += $self->max_col_width ($xi) 569 $wm += $self->max_col_width ($xi)
465 } 570 }
466 if ($ws < $wm) { $ws = $wm } 571 if ($ws < $wm) { $ws = $wm }
467 } 572 }
468 573
471 576
472sub _draw { 577sub _draw {
473 my ($self) = @_; 578 my ($self) = @_;
474 579
475 my $y = 0; 580 my $y = 0;
476 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 581 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
477 my $x = 0; 582 my $x = 0;
478 583
479 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 584 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
480 585
481 my $c = $self->{childs}->[$yi]->[$xi]; 586 my $c = $self->{children}->[$yi]->[$xi];
482 if ($c) { 587 if ($c) {
483 $c->move ($x, $y, 0); #TODO: Move to size_request 588 $c->move ($x, $y, 0); #TODO: Move to size_request
484 $c->draw if $c; 589 $c->draw if $c;
485 } 590 }
486 591
489 594
490 $y += $self->max_row_height ($yi); 595 $y += $self->max_row_height ($yi);
491 } 596 }
492} 597}
493 598
599#############################################################################
600
494package Crossfire::Client::Widget::VBox; 601package Crossfire::Client::Widget::VBox;
495 602
496our @ISA = Crossfire::Client::Widget::Bin::; 603our @ISA = Crossfire::Client::Widget::Container::;
497 604
498use SDL::OpenGL; 605use SDL::OpenGL;
499 606
500sub add {
501 my ($self, $chld) = @_;
502 push @{$self->{childs}}, $chld;
503 $chld->set_parent ($self);
504 $self->update;
505}
506
507sub size_request { 607sub size_request {
508 my ($self) = @_; 608 my ($self) = @_;
509 609
510 my ($hs, $ws) = (0, 0); 610 my @alloc = map [$_->size_request], @{$self->{children}};
611
612 (
613 (List::Util::max map $_->[0], @alloc),
614 (List::Util::sum map $_->[1], @alloc),
615 )
616}
617
618sub size_allocate {
619 my ($self, $w, $h) = @_;
620
621 $self->w ($w);
622 $self->h ($h);
623
624 my $exp;
625 my @oth;
626 # find expand widget
511 for (@{$self->{childs} || []}) { 627 for (@{$self->{children}}) {
628 if ($_->{expand}) {
629 $exp = $_;
630 last;
631 }
632 push @oth, $_;
633 }
634
635 my ($ow, $oh);
636
637 # get sizes of other widgets
638 for (@oth) {
512 my ($w, $h) = $_->size_request; 639 my ($w, $h) = $_->size_request;
513 $hs += $h; 640 $oh += $h;
514 if ($ws < $w) { $ws = $w } 641 if ($ow < $w) { $ow = $w }
515 } 642 }
516 643
517 return ($ws, $hs); 644 my $y = 0;
518}
519
520sub _draw {
521 my ($self) = @_;
522
523 my ($x, $y);
524 for (@{$self->{childs} || []}) { 645 for (@{$self->{children}}) {
525 $_->move (0, $y, 0); #TODO: move to size_request 646 $_->move (0, $y);
526 $_->draw; 647
648 if ($_ == $exp) {
649 $_->size_allocate ($w, $h - $oh);
650 $y += $h - $oh;
651 } else {
527 my ($w, $h) = $_->size_request; 652 my ($cw, $h) = $_->size_request;
653 $_->size_allocate ($w, $h);
528 $y += $h; 654 $y += $h;
655 }
529 } 656 }
530} 657}
658
659#############################################################################
531 660
532package Crossfire::Client::Widget::Label; 661package Crossfire::Client::Widget::Label;
533 662
534our @ISA = Crossfire::Client::Widget::; 663our @ISA = Crossfire::Client::Widget::;
535 664
536use SDL::OpenGL; 665use SDL::OpenGL;
537 666
538sub new { 667sub new {
539 my ($class, $x, $y, $z, $height, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
540 669
670 $height ||= $::FONTSIZE;
671
541 # TODO: color, and make height, xyz etc. optional 672 # TODO: color, and make height, xyz etc. optional
542 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
543 674
544 $self->set_text ($text); 675 $self->set_text ($text);
545 676
562} 693}
563 694
564sub size_request { 695sub size_request {
565 my ($self) = @_; 696 my ($self) = @_;
566 697
567 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
568 $self->{texture}{width}, 700 $self->{texture}{width},
569 $self->{texture}{height}, 701 $self->{texture}{height},
702 )
703 } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705
706 ($w, $h)
570 ) 707 }
708
571} 709}
572 710
573sub _draw { 711sub _draw {
574 my ($self) = @_; 712 my ($self) = @_;
575 713
579 glEnable GL_TEXTURE_2D; 717 glEnable GL_TEXTURE_2D;
580 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 718 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
581 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 719 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
582 glBindTexture GL_TEXTURE_2D, $tex->{name}; 720 glBindTexture GL_TEXTURE_2D, $tex->{name};
583 721
584 glColor 1, 1, 1, 0.6; # TODO color 722 glColor 1, 0, 0, 1; # TODO color
585 723
586 glBegin GL_QUADS; 724 glBegin GL_QUADS;
587 glTexCoord 0, 0; glVertex 0 , 0; 725 glTexCoord 0, 0; glVertex 0 , 0;
588 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 726 glTexCoord 0, 1; glVertex 0 , $tex->{height};
589 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 727 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
592 730
593 glDisable GL_BLEND; 731 glDisable GL_BLEND;
594 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
595} 733}
596 734
735#############################################################################
736
597package Crossfire::Client::Widget::TextEntry; 737package Crossfire::Client::Widget::Entry;
598 738
599our @ISA = Crossfire::Client::Widget::Label::; 739our @ISA = Crossfire::Client::Widget::Label::;
600 740
601use SDL; 741use SDL;
602use SDL::OpenGL; 742use SDL::OpenGL;
605 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
606 746
607 my $mod = $ev->key_mod; 747 my $mod = $ev->key_mod;
608 my $sym = $ev->key_sym; 748 my $sym = $ev->key_sym;
609 749
610 $ev->set_unicode (1);
611 my $uni = $ev->key_unicode; 750 my $uni = $ev->key_unicode;
612 751
613 my $text = $self->get_text; 752 my $text = $self->get_text;
614 753
615 if ($sym == SDLK_BACKSPACE) { 754 if ($sym == SDLK_BACKSPACE) {
616 substr $text, -1, 1, ''; 755 substr $text, -1, 1, '';
617
618 } elsif ($uni) { 756 } elsif ($uni) {
619 $text .= chr $uni; 757 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
620 } 759 }
760
621 $self->set_text ($text); 761 $self->set_text ($text);
622} 762}
623 763
764sub button_down {
765 my ($self, $ev) = @_;
624 766
625# XXX: TextView isn't neccessary with pango multiline text rendering 767 $self->focus_in;
626package Crossfire::Client::Widget::TextView;
627
628use strict;
629
630our @ISA = qw/Crossfire::Client::Widget/;
631
632use SDL::OpenGL;
633use SDL::OpenGL::Constants;
634
635sub new {
636 my ($class, $text, $h) = @_;
637 my $self = $class->SUPER::new ();
638
639 $self->{txt_height} = $h;
640 @{$self->{lines}} = split /\r?\n/, $text;
641
642 for (split /\r?\n/, $text) {
643 $self->add_line ($_);
644 }
645 $self
646} 768}
647 769
648#sub render_lines { 770sub _draw {
649# my ($self) = @_; 771 my ($self) = @_;
650# 772
651# $self->{txt_lines} = []; 773 if ($FOCUS == $self) {
652# 774 glColor 1, 1, 1;
653# for (@{$self->{lines}}) { 775 } else {
654# push @{$self->{txt_lines}}, 776 glColor 0.7, 0.7, 0.7;
655# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
656# } 777 }
657#}
658
659sub add_line {
660 my ($self, $line) = @_;
661 push @{$self->{lines}}, $line;
662
663 push @{$self->{txt_lines}},
664 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
665}
666
667sub size_request {
668 my ($self) = @_;
669
670 my $w = 0;
671 my $h = 0;
672
673 for (@{$self->{txt_lines}}) {
674 if ($w < $_->{width}) { $w = $_->{width} }
675 $h += $_->{height};
676 }
677
678 return ($w, $h);
679}
680
681sub draw_line {
682 my ($self, $tex, $y) = @_;
683
684 glBindTexture GL_TEXTURE_2D, $tex->{name};
685
686 glColor 1, 0, 1;
687 778
688 glBegin GL_QUADS; 779 glBegin GL_QUADS;
689 glTexCoord 0, 0; glVertex 0 , $y; 780 glVertex 0 , 0;
690 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height}; 781 glVertex 0 , $self->{h} - 1;
691 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height}; 782 glVertex $self->{w} - 1, $self->{h} - 1;
692 glTexCoord 1, 0; glVertex $tex->{width}, $y; 783 glVertex $self->{w} - 1, 0;
693 glEnd; 784 glEnd;
694}
695 785
696sub _draw { 786 $self->SUPER::_draw;
697 my ($self) = @_;
698
699 glEnable GL_BLEND;
700 glEnable GL_TEXTURE_2D;
701 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
702
703 my $l = 0;
704 for (@{$self->{txt_lines}}) {
705 $self->draw_line ($_, $l);
706 $l += $_->{height};
707 }
708
709 glDisable GL_BLEND;
710 glDisable GL_TEXTURE_2D;
711} 787}
788
789#############################################################################
712 790
713package Crossfire::Client::Widget::MapWidget; 791package Crossfire::Client::Widget::MapWidget;
714 792
715use strict; 793use strict;
716 794
725sub key_down { 803sub key_down {
726 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
727} 805}
728 806
729sub key_up { 807sub key_up {
808}
809
810sub size_request {
811
812}
813
814sub size_allocate {
730} 815}
731 816
732sub _draw { 817sub _draw {
733 my ($self) = @_; 818 my ($self) = @_;
734 819
876 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 961 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
877 $::CONN->send ("command run_stop"); 962 $::CONN->send ("command run_stop");
878 } 963 }
879} 964}
880 965
966#############################################################################
967
881package Crossfire::Client::Widget::Animator; 968package Crossfire::Client::Widget::Animator;
882 969
883use SDL::OpenGL; 970use SDL::OpenGL;
884 971
885our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = Crossfire::Client::Widget::Bin::;
886 973
887sub moveto { 974sub moveto {
888 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
889 976
890 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
891 $self->{speed} = 0.01; 978 $self->{speed} = 2;
892 $self->{time} = 1; 979 $self->{time} = 1;
893 980
894 ::animation_start $self; 981 ::animation_start $self;
895} 982}
896 983
911 998
912sub _draw { 999sub _draw {
913 my ($self) = @_; 1000 my ($self) = @_;
914 1001
915 glPushMatrix; 1002 glPushMatrix;
916 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
917 $self->{child}->draw; 1004 $self->{children}[0]->draw;
918 glPopMatrix; 1005 glPopMatrix;
919} 1006}
920 1007
9211; 1008#############################################################################
922 1009
1010package Crossfire::Client::Widget::Toplevel;
1011
1012our @ISA = Crossfire::Client::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub size_allocate {
1019 my ($self, $w, $h) = @_;
1020
1021 $self->SUPER::size_allocate ($w, $h);
1022
1023 $_->size_allocate ($_->size_request)
1024 for @{$self->{children}};
1025}
1026
1027sub update {
1028 my ($self) = @_;
1029
1030 $self->size_allocate ($self->size_request);
1031 ::refresh ();
1032}
1033
1034sub add {
1035 my ($self, $widget) = @_;
1036
1037 $self->SUPER::add ($widget);
1038
1039 $widget->size_allocate ($widget->size_request);
1040}
1041
1042sub draw {
1043 my ($self) = @_;
1044
1045 $self->_draw;
1046}
1047
1048#############################################################################
1049
1050package Crossfire::Client::Widget;
1051
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1053
10541
1055

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines