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.6 by elmex, Fri Apr 7 19:58:55 2006 UTC vs.
Revision 1.16 by root, Fri Apr 7 23:16:29 2006 UTC

1package Crossfire::Client::Widget; 1package Crossfire::Client::Widget;
2
2use strict; 3use strict;
4use SDL::OpenGL;
5use SDL::OpenGL::Constants;
3 6
4our $FOCUS; # the widget with current focus 7our $FOCUS; # the widget with current focus
5our %ACTIVE_WIDGETS; 8our @ACTIVE_WIDGETS;
6 9
7# class methods for events 10# class methods for events
8sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
9sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
10sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
11sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 14sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS }
12 15
13sub new { 16sub new {
14 my $class = shift; 17 my $class = shift;
15 my $self = { @_ }; 18
16 bless $self, $class; 19 bless { @_ }, $class
17 return $self;
18} 20}
19 21
20sub activate { 22sub activate {
21 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 23 push @ACTIVE_WIDGETS, $_[0];
22} 24}
23 25
24sub deactivate { 26sub deactivate {
25 delete $ACTIVE_WIDGETS{$_[0]}; 27 @ACTIVE_WIDGETS =
28 sort { $a->{z} <=> $b->{z} }
29 grep { $_ != $_[0] }
30 @ACTIVE_WIDGETS;
31}
32
33sub size_request {
34 die "size_request is abtract";
26} 35}
27 36
28sub focus_in { 37sub focus_in {
29 my ($widget) = @_; 38 my ($widget) = @_;
30 $FOCUS = $widget; 39 $FOCUS = $widget;
48 57
49sub button_up { 58sub button_up {
50 my ($widget, $sdlev) = @_; 59 my ($widget, $sdlev) = @_;
51} 60}
52 61
62sub w { $_[0]->{w} }
63sub h { $_[0]->{h} }
64sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
65sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
66sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
67
53sub draw { 68sub draw {
69 my ($self) = @_;
70
71 glPushMatrix;
72 glTranslate $self->{x}, $self->{y}, 0;
73 $self->_draw;
74 glPopMatrix;
75}
76
77sub _draw {
54 my ($widget) = @_; 78 my ($widget) = @_;
55} 79}
56 80
57sub bbox { 81sub bbox {
58 my ($widget) = @_; 82 my ($widget) = @_;
59} 83}
60 84
61package Client::TextView; 85package Crossfire::Client::Widget::Container;
86
87our @ISA = Crossfire::Client::Widget::;
88
89use SDL::OpenGL;
90
91sub add { $_[0]->{child} = $_[1] }
92sub get { $_[0]->{child} }
93
94sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
95
96sub _draw { die "Containers can't be drawn!" }
97
98package Crossfire::Client::Widget::Frame;
99
100our @ISA = Crossfire::Client::Widget::Container::;
101
102use SDL::OpenGL;
103
104sub size_request {
105 my ($self) = @_;
106 my $chld = $self->get
107 or return (0, 0);
108 map { $_ + 4 } $chld->size_request;
109}
110
111sub _draw {
112 my ($self) = @_;
113
114 my $chld = $self->get;
115
116 my ($w, $h) = $chld->size_request;
117
118 glColor 1, 0, 0;
119 glBegin GL_QUADS;
120 glTexCoord 0, 0; glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0;
124 glEnd;
125
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw;
129 glPopMatrix;
130}
131
132package Crossfire::Client::Widget::Table;
133
134our @ISA = Crossfire::Client::Widget::Container::;
135
136use SDL::OpenGL;
137
138sub add {
139 my ($self, $x, $y, $chld) = @_;
140 $self->{childs}[$y][$x] = $chld;
141}
142
143sub max_row_height {
144 my ($self, $row) = @_;
145
146 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi];
149 my ($w, $h) = $c->size_request if $c;
150 if ($hs < $h) { $hs = $h }
151 }
152 return $hs;
153}
154
155sub max_col_width {
156 my ($self, $col) = @_;
157
158 my $ws = 0;
159 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
160 my $c = ($self->{childs}->[$yi] || [])->[$col];
161 my ($w, $h) = $c->size_request if $c;
162 if ($ws < $w) { $ws = $w }
163 }
164 return $ws;
165}
166
167sub size_request {
168 my ($self) = @_;
169
170 my ($hs, $ws) = (0, 0);
171
172 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
173 $hs += $self->max_row_height ($yi);
174 }
175
176 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
177 my $wm = 0;
178 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
179 $wm += $self->max_col_width ($xi)
180 }
181 if ($ws < $wm) { $ws = $wm }
182 }
183
184 return ($ws, $hs);
185}
186
187sub _draw {
188 my ($self) = @_;
189
190 my $y = 0;
191 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
192 my $x = 0;
193
194 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
195
196 glPushMatrix;
197 glTranslate ($x, $y, 0);
198 my $c = $self->{childs}->[$yi]->[$xi];
199 $c->_draw if $c;
200 glPopMatrix;
201
202 $x += $self->max_col_width ($xi);
203 }
204
205 $y += $self->max_row_height ($yi);
206 }
207}
208
209package Crossfire::Client::Widget::VBox;
210
211our @ISA = Crossfire::Client::Widget::Container::;
212
213use SDL::OpenGL;
214
215sub add {
216 my ($self, $chld) = @_;
217 push @{$self->{childs}}, $chld;
218}
219
220sub size_request {
221 my ($self) = @_;
222
223 my ($hs, $ws) = (0, 0);
224 for (@{$self->{childs} || []}) {
225 my ($w, $h) = $_->size_request;
226 $hs += $h;
227 if ($ws < $w) { $ws = $w }
228 }
229
230 return ($ws, $hs);
231}
232
233sub _draw {
234 my ($self) = @_;
235
236 my ($x, $y);
237 for (@{$self->{childs} || []}) {
238 glPushMatrix;
239 glTranslate (0, $y, 0);
240 $_->_draw;
241 glPopMatrix;
242 my ($w, $h) = $_->size_request;
243 $y += $h;
244 }
245}
246
247package Crossfire::Client::Widget::Label;
248
249our @ISA = Crossfire::Client::Widget::;
250
251use SDL::OpenGL;
252
253sub new {
254 my ($class, $x, $y, $z, $ttf, $text) = @_;
255
256 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
257
258 $self->set_text ($text);
259
260 $self
261}
262
263sub set_text {
264 my ($self, $text) = @_;
265 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
266}
267
268sub get_text {
269 my ($self, $text) = @_;
270 $self->{text}
271}
272
273sub size_request {
274 my ($self) = @_;
275
276 (
277 $self->{texture}{width},
278 $self->{texture}{height},
279 )
280}
281
282sub _draw {
283 my ($self) = @_;
284
285 my $tex = $self->{texture};
286
287 $self->{x}--;
288
289 glEnable GL_BLEND;
290 glEnable GL_TEXTURE_2D;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293
294 glColor 1, 1, 1;
295
296 glBegin GL_QUADS;
297 glTexCoord 0, 0; glVertex 0 , 0;
298 glTexCoord 0, 1; glVertex 0 , $tex->{height};
299 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
300 glTexCoord 1, 0; glVertex $tex->{width}, 0;
301 glEnd;
302
303 glDisable GL_BLEND;
304 glDisable GL_TEXTURE_2D;
305}
306
307package Crossfire::Client::Widget::TextView;
62 308
63use strict; 309use strict;
310
64our @ISA = qw/Client::Widget/; 311our @ISA = qw/Crossfire::Client::Widget/;
65 312
66use SDL::OpenGL; 313use SDL::OpenGL;
67use SDL::OpenGL::Constants; 314use SDL::OpenGL::Constants;
68 315
69sub add_line { 316sub add_line {
70 my ($self, $line) = @_; 317 my ($self, $line) = @_;
71 push @{$self->{lines}}, $line; 318 push @{$self->{lines}}, $line;
72} 319}
73 320
74sub draw { 321sub _draw {
75 my ($self) = @_; 322 my ($self) = @_;
76 323
77} 324}
78 325
79package Client::MapWidget; 326package Crossfire::Client::Widget::MapWidget;
80 327
81use strict; 328use strict;
329
82our @ISA = qw/Client::Widget/; 330our @ISA = qw/Crossfire::Client::Widget/;
83 331
332use SDL;
84use SDL::OpenGL; 333use SDL::OpenGL;
85use SDL::OpenGL::Constants; 334use SDL::OpenGL::Constants;
86 335
87sub key_down { 336sub key_down {
88 print "MAPKEYDOWN\n"; 337 print "MAPKEYDOWN\n";
89} 338}
90 339
91sub key_up { 340sub key_up {
92} 341}
93 342
94my $x;
95
96sub draw { 343sub _draw {
97 glEnable GL_TEXTURE_2D; 344 glEnable GL_TEXTURE_2D;
98 glEnable GL_BLEND; 345 glEnable GL_BLEND;
99 346 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
100 glPushMatrix;
101 347
102 my $map = $::CONN->{map}; 348 my $map = $::CONN->{map};
103 349
104 for my $x (0 .. $::CONN->{mapw} - 1) { 350 for my $x (0 .. $::CONN->{mapw} - 1) {
105 for my $y (0 .. $::CONN->{maph} - 1) { 351 for my $y (0 .. $::CONN->{maph} - 1) {
114 my $tex = $::CONN->{face}[$num]{texture} || next; 360 my $tex = $::CONN->{face}[$num]{texture} || next;
115 361
116 glBindTexture GL_TEXTURE_2D, $tex->{name}; 362 glBindTexture GL_TEXTURE_2D, $tex->{name};
117 363
118 glBegin GL_QUADS; 364 glBegin GL_QUADS;
119 glTexCoord 0, 0; glVertex $x, $y; 365 glTexCoord 0, 0; glVertex $x * 32 , $y * 32;
120 glTexCoord 0, 1; glVertex $x, $y + 1; 366 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32;
121 glTexCoord 1, 1; glVertex $x + 1, $y + 1; 367 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32;
122 glTexCoord 1, 0; glVertex $x + 1, $y; 368 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32;
123 glEnd; 369 glEnd;
124 } 370 }
125 } 371 }
126 } 372 }
127 373
128 glPopMatrix;
129
130 glDisable GL_TEXTURE_2D; 374 glDisable GL_TEXTURE_2D;
131 glDisable GL_BLEND; 375 glDisable GL_BLEND;
132} 376}
133 377
378my %DIR = (
379 SDLK_KP8, [1, "north"],
380 SDLK_KP9, [2, "northest"],
381 SDLK_KP6, [3, "east"],
382 SDLK_KP3, [4, "southeast"],
383 SDLK_KP2, [5, "south"],
384 SDLK_KP1, [6, "southwest"],
385 SDLK_KP4, [7, "west"],
386 SDLK_KP7, [8, "northwest"],
387);
388
389sub key_down {
390 my ($self, $ev) = @_;
391
392 my $mod = $ev->key_mod;
393 my $sym = $ev->key_sym;
394
395 if ($sym == SDLK_KP5) {
396 $::CONN->send ("command stay fire");
397 } elsif (exists $DIR{$sym}) {
398 if ($mod & KMOD_SHIFT) {
399 $::CONN->send ("command fire $DIR{$sym}[0]");
400 } elsif ($mod & KMOD_CTRL) {
401 $::CONN->send ("command run $DIR{$sym}[0]");
402 } else {
403 }
404 }
405}
406
407sub key_up {
408 my ($self, $ev) = @_;
409
410 my $mod = $ev->key_mod;
411 my $sym = $ev->key_sym;
412
413 if (exists $DIR{$sym}) {
414 if ($mod & KMOD_SHIFT) {
415 $::CONN->send ("command fire_stop");
416 } else {
417 $::CONN->send ("command run_stop");
418 }
419 }
420}
421
1341; 4221;
135 423

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines