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.34 by elmex, Sun Apr 9 02:44:51 2006 UTC

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 10our $FOCUS; # the widget with current focus
11our @ACTIVE_WIDGETS; 11#our @ACTIVE_WIDGETS;
12 12
13# class methods for events 13# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
16sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 16sub feed_sdl_button_down_event { }
17sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 17sub feed_sdl_button_up_event { }
18 18
19sub new { 19sub new {
20 my $class = shift; 20 my $class = shift;
21 21
22 bless { @_ }, $class 22 bless { @_ }, $class
23} 23}
24 24
25sub activate { 25#sub activate {
26 push @ACTIVE_WIDGETS, $_[0]; 26# push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; 27# Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28} 28#}
29 29
30sub deactivate { 30#sub deactivate {
31 @ACTIVE_WIDGETS = 31# @ACTIVE_WIDGETS =
32 sort { $a->{z} <=> $b->{z} } 32# sort { $a->{z} <=> $b->{z} }
33 grep { $_ && $_ != $_[0] } 33# grep { $_ && $_ != $_[0] }
34 @ACTIVE_WIDGETS; 34# @ACTIVE_WIDGETS;
35} 35#}
36 36
37sub move { 37sub move {
38 my ($self, $x, $y, $z) = @_; 38 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 39 $self->{x} = $x;
40 $self->{y} = $y; 40 $self->{y} = $y;
92sub _draw { 92sub _draw {
93 my ($widget) = @_; 93 my ($widget) = @_;
94} 94}
95 95
96sub bbox { 96sub bbox {
97 my ($widget) = @_; 97 my ($self) = @_;
98 my ($w, $h) = $self->size_request;
99 (
100 $self->{x},
101 $self->{y},
102 $self->{x} = $w,
103 $self->{y} = $h
104 )
105}
106
107sub del_parent { $_[0]->{parent} = undef }
108
109sub set_parent {
110 my ($self, $par) = @_;
111
112 $self->{parent} = $par;
113 Scalar::Util::weaken $self->{parent};
114}
115
116sub get_parent {
117 $_[0]->{parent}
118}
119
120sub update {
121 my ($self) = @_;
122
123 $self->{parent}->update
124 if $self->{parent};
98} 125}
99 126
100sub DESTROY { 127sub DESTROY {
101 my ($self) = @_; 128 my ($self) = @_;
102 129
103 $self->deactivate; 130 #$self->deactivate;
104} 131}
105 132
106package Crossfire::Client::Widget::Container; 133package Crossfire::Client::Widget::Container;
107 134
108our @ISA = Crossfire::Client::Widget::; 135our @ISA = Crossfire::Client::Widget::;
109 136
110use SDL::OpenGL; 137use SDL::OpenGL;
111 138
112sub add { $_[0]->{child} = $_[1] } 139sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update }
113sub get { $_[0]->{child} } 140sub get { $_[0]->{child} }
141sub remove {
142 my ($self, $chld) = @_;
143 delete $self->{child}
144 if $self->{child} == $chld;
145}
114 146
115sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 147sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
116 148
117sub _draw { die "Containers can't be drawn!" } 149sub _draw { die "Containers can't be drawn!" }
150
151package Crossfire::Client::Widget::Toplevel;
152
153our @ISA = Crossfire::Client::Widget::;
154
155use SDL::OpenGL;
156
157sub add {
158 my ($self, $chld) = @_;
159
160 push @{$self->{childs}}, $chld;
161 @{$self->{childs}} =
162 sort { $a->{z} <=> $b->{z} }
163 @{$self->{childs}};
164
165 $chld->set_parent ($self);
166}
167
168sub remove {
169 my ($self, $chld) = @_;
170 @{$self->{childs}} =
171 sort { $a->{z} <=> $b->{z} }
172 grep { $_ && $_ != $_[0] }
173 @{$self->{childs}}
174}
175
176sub update {
177 my ($self) = @_;
178 ::refresh ();
179}
180
181sub _draw {
182 my ($self) = @_;
183
184 $_->draw for @{$self->{childs}};
185}
118 186
119package Crossfire::Client::Widget::Window; 187package Crossfire::Client::Widget::Window;
120 188
121our @ISA = Crossfire::Client::Widget::Container::; 189our @ISA = Crossfire::Client::Widget::Container::;
122 190
123use SDL::OpenGL; 191use SDL::OpenGL;
124 192
125sub add { 193sub add {
126 my ($self, $chld) = @_; 194 my ($self, $chld) = @_;
127 $self->SUPER::add ($chld); 195 $self->SUPER::add ($chld);
196 $chld->set_parent ($self);
128 $self->render_chld; #TODO: Move this to the size_request event propably? 197 $self->update; #TODO: Move this to the size_request event propably?
198}
199
200sub remove {
201 my ($self) = @_;
202 # TODO FIXME: removing a child from a window will crash, see render_chld
203 $self->update;
204}
205
206sub update {
207 my ($self) = @_;
208 $self->render_chld;
129} 209}
130 210
131sub render_chld { 211sub render_chld {
132 my ($self) = @_; 212 my ($self) = @_;
133 my $chld = $self->get; 213 my $chld = $self->get;
211 291
212our @ISA = Crossfire::Client::Widget::Frame::; 292our @ISA = Crossfire::Client::Widget::Frame::;
213 293
214use SDL::OpenGL; 294use SDL::OpenGL;
215 295
216#TODO: implement themed frame 296sub new {
297 my ($self, $theme) = @_;
298 $self = $self->SUPER::new;
299
300 $self->{txts} = [
301 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
302 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/
303 ];
304 $self
305
306}
307
308sub size_request {
309 my ($self) = @_;
310 my ($w, $h) = $self->get->size_request;
311
312 $h += $self->{txts}->[1]->{height};
313 $h += $self->{txts}->[4]->{height};
314 $w += $self->{txts}->[2]->{width};
315 $w += $self->{txts}->[3]->{width};
316
317 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
318
319 ($w, $h)
320}
321
322sub _draw {
323 my ($self) = @_;
324
325 my ($w, $h) = $self->size_request;
326 my ($cw, $ch) = $self->get->size_request;
327
328 glEnable GL_BLEND;
329 glEnable GL_TEXTURE_2D;
330 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
331 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
332
333
334 my $top = $self->{txts}->[1];
335 glBindTexture GL_TEXTURE_2D, $top->{name};
336
337 glColor 1, 1, 1, 0.8;
338
339 glBegin GL_QUADS;
340 glTexCoord 0, 0; glVertex 0 , 0;
341 glTexCoord 0, 1; glVertex 0 , $top->{height};
342 glTexCoord 1, 1; glVertex $w , $top->{height};
343 glTexCoord 1, 0; glVertex $w , 0;
344 glEnd;
345
346 my $left = $self->{txts}->[3];
347 glBindTexture GL_TEXTURE_2D, $left->{name};
348
349 glColor 1, 1, 1, 0.8;
350
351 glBegin GL_QUADS;
352 glTexCoord 0, 0; glVertex 0 , $top->{height};
353 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
354 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
355 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
356 glEnd;
357
358 my $right = $self->{txts}->[2];
359 glBindTexture GL_TEXTURE_2D, $right->{name};
360
361 glColor 1, 1, 1, 0.8;
362
363 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
365 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
366 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
367 glTexCoord 1, 0; glVertex $w , $top->{height};
368 glEnd;
369
370 my $bottom = $self->{txts}->[4];
371 glBindTexture GL_TEXTURE_2D, $bottom->{name};
372
373 glColor 1, 1, 1, 0.8;
374
375 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
377 glTexCoord 0, 1; glVertex 0 , $h;
378 glTexCoord 1, 1; glVertex $w , $h;
379 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
380 glEnd;
381
382 my $bg = $self->{txts}->[0];
383 glBindTexture GL_TEXTURE_2D, $bg->{name};
384 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
385 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
386
387 glColor 1, 1, 1, 0.8;
388
389 my $rep_x = $cw / $bg->{width};
390 my $rep_y = $ch / $bg->{height};
391
392 glBegin GL_QUADS;
393 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
394 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
395 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
396 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
397 glEnd;
398
399 $self->get->draw;
400
401 glDisable GL_BLEND;
402 glDisable GL_TEXTURE_2D;
403}
217 404
218package Crossfire::Client::Widget::Table; 405package Crossfire::Client::Widget::Table;
219 406
220our @ISA = Crossfire::Client::Widget::Container::; 407our @ISA = Crossfire::Client::Widget::Container::;
221 408
222use SDL::OpenGL; 409use SDL::OpenGL;
223 410
224sub add { 411sub add {
225 my ($self, $x, $y, $chld) = @_; 412 my ($self, $x, $y, $chld) = @_;
413 my $old_chld = $self->{childs}[$y][$x];
414
226 $self->{childs}[$y][$x] = $chld; 415 $self->{childs}[$y][$x] = $chld;
416 $chld->set_parent ($self);
417 $self->update;
227} 418}
228 419
229sub max_row_height { 420sub max_row_height {
230 my ($self, $row) = @_; 421 my ($self, $row) = @_;
231 422
303use SDL::OpenGL; 494use SDL::OpenGL;
304 495
305sub add { 496sub add {
306 my ($self, $chld) = @_; 497 my ($self, $chld) = @_;
307 push @{$self->{childs}}, $chld; 498 push @{$self->{childs}}, $chld;
499 $chld->set_parent ($self);
500 $self->update;
308} 501}
309 502
310sub size_request { 503sub size_request {
311 my ($self) = @_; 504 my ($self) = @_;
312 505
339use SDL::OpenGL; 532use SDL::OpenGL;
340 533
341sub new { 534sub new {
342 my ($class, $x, $y, $z, $height, $text) = @_; 535 my ($class, $x, $y, $z, $height, $text) = @_;
343 536
537 # TODO: color, and make height, xyz etc. optional
344 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 538 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
345 539
346 $self->set_text ($text); 540 $self->set_text ($text);
347 541
348 $self 542 $self
350 544
351sub set_text { 545sub set_text {
352 my ($self, $text) = @_; 546 my ($self, $text) = @_;
353 547
354 $self->{text} = $text; 548 $self->{text} = $text;
355
356 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 549 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
550
551 $self->update;
357} 552}
358 553
359sub get_text { 554sub get_text {
360 my ($self, $text) = @_; 555 my ($self, $text) = @_;
361 556
380 glEnable GL_TEXTURE_2D; 575 glEnable GL_TEXTURE_2D;
381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 576 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
382 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 577 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
383 glBindTexture GL_TEXTURE_2D, $tex->{name}; 578 glBindTexture GL_TEXTURE_2D, $tex->{name};
384 579
385 glColor 1, 1, 1, 0.8; 580 glColor 1, 1, 1, 0.6; # TODO color
386 581
387 glBegin GL_QUADS; 582 glBegin GL_QUADS;
388 glTexCoord 0, 0; glVertex 0 , 0; 583 glTexCoord 0, 0; glVertex 0 , 0;
389 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 584 glTexCoord 0, 1; glVertex 0 , $tex->{height};
390 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 585 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines