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.37 by root, Sun Apr 9 21:05:50 2006 UTC vs.
Revision 1.72 by root, Tue Apr 11 18:00:45 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 {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
22} 84}
23 85
24sub move { 86sub move {
25 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
26 $self->{x} = $x; 88 $self->{x} = $x;
37 Carp::confess "size_request is abtract"; 99 Carp::confess "size_request is abtract";
38} 100}
39 101
40sub size_allocate { 102sub size_allocate {
41 my ($self, $w, $h) = @_; 103 my ($self, $w, $h) = @_;
104
42 $self->w ($w); 105 $self->{w} = $w;
43 $self->h ($h); 106 $self->{h} = $h;
107}
108
109# translate global koordinates to local coordinate system
110sub translate {
111 my ($self, $x, $y) = @_;
112
113 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
44} 114}
45 115
46sub focus_in { 116sub focus_in {
47 my ($widget) = @_; 117 my ($self) = @_;
48 $FOCUS = $widget; 118
119 return if $FOCUS == $self;
120
121 my $focus = $FOCUS; $FOCUS = $self;
122 $focus->update if $focus;
123 $FOCUS->update;
49} 124}
50 125
51sub focus_out { 126sub focus_out {
52 my ($widget) = @_; 127 my ($self) = @_;
53}
54 128
129 return unless $FOCUS == $self;
130
131 my $focus = $FOCUS; undef $FOCUS;
132 $focus->update if $focus; #?
133}
134
135sub mouse_motion { }
136sub button_up { }
55sub key_down { 137sub key_down { }
56 my ($widget, $sdlev) = @_;
57}
58
59sub key_up { 138sub key_up { }
60 my ($widget, $sdlev) = @_;
61}
62 139
63sub button_down { 140sub button_down {
64 my ($widget, $sdlev) = @_; 141 my ($self, $ev, $x, $y) = @_;
65}
66 142
67sub button_up { 143 $self->focus_in;
68 my ($widget, $sdlev) = @_;
69} 144}
70 145
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 146sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 147sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 148sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 149sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 150sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 151
77sub draw { 152sub draw {
78 my ($self) = @_; 153 my ($self) = @_;
154
155 return unless $self->{h} && $self->{w};
79 156
80 glPushMatrix; 157 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 158 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 159 $self->_draw;
83 glPopMatrix; 160 glPopMatrix;
161
162 if ($self == $HOVER) {
163 my ($x, $y) = @$self->{qw(x y)};
164
165 glColor 1, 1, 1, 0.1;
166 glEnable GL_BLEND;
167 glBegin GL_QUADS;
168 glVertex $x , $y;
169 glVertex $x + $self->{w}, $y;
170 glVertex $x + $self->{w}, $y + $self->{h};
171 glVertex $x , $y + $self->{h};
172 glEnd;
173 glDisable GL_BLEND;
174 }
84} 175}
85 176
86sub _draw { 177sub _draw {
87 my ($widget) = @_; 178 my ($self) = @_;
179
180 warn "no draw defined for $self\n";
88} 181}
89 182
90sub bbox { 183sub bbox {
91 my ($self) = @_; 184 my ($self) = @_;
92 my ($w, $h) = $self->size_request; 185 my ($w, $h) = $self->size_request;
96 $self->{x} = $w, 189 $self->{x} = $w,
97 $self->{y} = $h 190 $self->{y} = $h
98 ) 191 )
99} 192}
100 193
194sub find_widget {
195 my ($self, $x, $y) = @_;
196
197 return $self
198 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
199 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
200
201 ()
202}
203
101sub del_parent { $_[0]->{parent} = undef } 204sub del_parent { $_[0]->{parent} = undef }
102 205
103sub set_parent { 206sub set_parent {
104 my ($self, $par) = @_; 207 my ($self, $par) = @_;
105 208
116 219
117 $self->{parent}->update 220 $self->{parent}->update
118 if $self->{parent}; 221 if $self->{parent};
119} 222}
120 223
224sub connect {
225 my ($self, $signal, $cb) = @_;
226
227 push @{ $self->{cb}{$signal} }, $cb;
228}
229
230sub emit {
231 my ($self, $signal, @args) = @_;
232
233 $_->($self, @args)
234 for @{$self->{cb}{$signal} || []};
235}
236
121sub DESTROY { 237sub DESTROY {
122 my ($self) = @_; 238 my ($self) = @_;
123 239
124 #$self->deactivate; 240 #$self->deactivate;
125} 241}
126 242
243#############################################################################
244
127package Crossfire::Client::Widget::Bin; 245package CFClient::Widget::DrawBG;
128 246
129our @ISA = Crossfire::Client::Widget::; 247our @ISA = CFClient::Widget::;
130 248
131sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->{expand} = $_[2] } 249use strict;
132sub get { $_[0]->{child} } 250use SDL::OpenGL;
251
252sub new {
253 my $class = shift;
254
255 # range [value, low, high, page]
256
257 $class->SUPER::new (
258 bg => [0, 0, 0, 0.4],
259 active_bg => [1, 1, 1],
260 @_
261 )
262}
263
264sub _draw {
265 my ($self) = @_;
266
267 my ($w, $h) = @$self{qw(w h)};
268
269 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
270 glBegin GL_QUADS;
271 glVertex 0 , 0;
272 glVertex 0 , $h;
273 glVertex $w, $h;
274 glVertex $w, 0;
275 glEnd;
276}
277
278#############################################################################
279
280package CFClient::Widget::Empty;
281
282our @ISA = CFClient::Widget::;
283
284sub size_request {
285 (0, 0)
286}
287
288sub draw { }
289
290#############################################################################
291
292package CFClient::Widget::Container;
293
294our @ISA = CFClient::Widget::;
295
296sub new {
297 my ($class, %arg) = @_;
298
299 my $children = delete $arg{children} || [];
300
301 my $self = $class->SUPER::new (children => [], %arg);
302 $self->add ($_) for @$children;
303
304 $self
305}
306
307sub add {
308 my ($self, $chld, $expand) = @_;
309
310 $chld->{expand} = $expand;
311 $chld->set_parent ($self);
312
313 $self->{children} = [
314 sort { $a->{z} <=> $b->{z} }
315 @{$self->{children}}, $chld
316 ];
317
318 $self->size_allocate ($self->{w}, $self->{h})
319 if $self->{w}; #TODO: check for "realised state"
320}
133 321
134sub remove { 322sub remove {
135 my ($self, $chld) = @_; 323 my ($self, $widget) = @_;
136 delete $self->{child} 324
137 if $self->{child} == $chld; 325 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
326
327 $self->size_allocate ($self->{w}, $self->{h});
138} 328}
329
330sub find_widget {
331 my ($self, $x, $y) = @_;
332
333 $x -= $self->{x};
334 $y -= $self->{y};
335
336 my $res;
337
338 for (reverse @{ $self->{children} }) {
339 $res = $_->find_widget ($x, $y)
340 and return $res;
341 }
342
343 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
344}
345
346sub _draw {
347 my ($self) = @_;
348
349 $_->draw for @{$self->{children}};
350}
351
352#############################################################################
353
354package CFClient::Widget::Bin;
355
356our @ISA = CFClient::Widget::Container::;
357
358sub new {
359 my ($class, %arg) = @_;
360
361 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::;
362
363 $class->SUPER::new (children => [$child], %arg)
364}
365
366sub add {
367 my ($self, $widget) = @_;
368
369 $self->{children} = [];
370
371 $self->SUPER::add ($widget);
372}
373
374sub remove {
375 my ($self, $widget) = @_;
376
377 $self->SUPER::remove ($widget);
378
379 $self->{children} = [new CFClient::Widget::Empty]
380 unless @{$self->{children}};
381}
382
383sub child { $_[0]->{children}[0] }
139 384
140sub size_request { 385sub size_request {
141 $_[0]->{child}->size_request if $_[0]->{child} 386 $_[0]{children}[0]->size_request
142} 387}
388
143sub size_allocate { 389sub size_allocate {
144 my ($self, $w, $h) = @_; 390 my ($self, $w, $h) = @_;
391
392 return unless $self->{w} != $w || $self->{h} != $h;
393
145 $self->SUPER::size_allocate ($w, $h); 394 $self->SUPER::size_allocate ($w, $h);
146 $self->{child}->size_allocate ($w, $h) 395 $self->{children}[0]->size_allocate ($w, $h);
147 if $self->{child}
148} 396}
149 397
150sub _draw { 398#############################################################################
151 my ($self) = @_;
152 399
153 $self->{child}->draw; 400package CFClient::Widget::Window;
154}
155 401
156package Crossfire::Client::Widget::Toplevel;
157
158our @ISA = Crossfire::Client::Widget::; 402our @ISA = CFClient::Widget::Bin::;
159 403
160use SDL::OpenGL; 404use SDL::OpenGL;
161 405
162sub add { 406sub new {
163 my ($self, $chld) = @_; 407 my ($class, %arg) = @_;
164 408
165 push @{$self->{childs}}, $chld; 409 my $self = $class->SUPER::new (%arg);
166 @{$self->{childs}} =
167 sort { $a->{z} <=> $b->{z} }
168 @{$self->{childs}};
169
170 $chld->set_parent ($self);
171 $chld->size_allocate ($chld->size_request);
172}
173
174sub remove {
175 my ($self, $chld) = @_;
176 @{$self->{childs}} =
177 sort { $a->{z} <=> $b->{z} }
178 grep { $_ && $_ != $_[0] }
179 @{$self->{childs}}
180} 410}
181 411
182sub update { 412sub update {
183 my ($self) = @_; 413 my ($self) = @_;
184 ::refresh ();
185}
186 414
187sub _draw { 415 # we want to do this delayed...
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 warn "ADD $chld\n";
202 $self->SUPER::add ($chld);
203 $chld->set_parent ($self);
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; 416 $self->render_chld;
417 $self->SUPER::update;
215} 418}
216 419
217sub render_chld { 420sub render_chld {
218 my ($self) = @_; 421 my ($self) = @_;
219 my $chld = $self->get;
220 my ($w, $h) = $self->size_request;
221 422
222 require Carp;
223 Carp::cluck "RENDERCHI $w $h";
224 warn "RENDERCHI $w $h\n";
225 $self->{texture} = 423 $self->{texture} =
226 Crossfire::Client::Texture->new_from_opengl ( 424 CFClient::Texture->new_from_opengl (
227 $w, $h, sub { $chld->draw } 425 $self->{w}, $self->{h}, sub { $self->child->draw }
228 ); 426 );
229 $self->{texture}->upload;
230}
231
232sub size_request {
233 my ($self) = @_;
234 ($self->w, $self->h)
235} 427}
236 428
237sub size_allocate { 429sub size_allocate {
238 my ($self, $w, $h) = @_; 430 my ($self, $w, $h) = @_;
239 431
432 return unless $self->{w} != $w || $self->{h} != $h;
433
240 $self->w ($w); 434 $self->{w} = $w;
241 $self->h ($h); 435 $self->{h} = $h;
436
242 $self->get->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
243 438
244 $self->update; #TODO: Move this to the size_request event propably? 439 $self->render_chld;
245} 440}
246 441
247sub _draw { 442sub _draw {
248 my ($self) = @_; 443 my ($self) = @_;
249 444
253 or return; 448 or return;
254 449
255 glEnable GL_BLEND; 450 glEnable GL_BLEND;
256 glEnable GL_TEXTURE_2D; 451 glEnable GL_TEXTURE_2D;
257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 452 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
258 glBindTexture GL_TEXTURE_2D, $tex->{name};
259 453
260 glBegin GL_QUADS; 454 $tex->draw_quad (0, 0, $w, $h);
261 glTexCoord 0, 0; glVertex 0, 0;
262 glTexCoord 0, 1; glVertex 0, $h;
263 glTexCoord 1, 1; glVertex $w, $h;
264 glTexCoord 1, 0; glVertex $w, 0;
265 glEnd;
266 455
267 glDisable GL_BLEND; 456 glDisable GL_BLEND;
268 glDisable GL_TEXTURE_2D; 457 glDisable GL_TEXTURE_2D;
269} 458}
270 459
460#############################################################################
461
271package Crossfire::Client::Widget::Frame; 462package CFClient::Widget::Frame;
272 463
273our @ISA = Crossfire::Client::Widget::Bin::; 464our @ISA = CFClient::Widget::Bin::;
274 465
275use SDL::OpenGL; 466use SDL::OpenGL;
276 467
277sub size_request { 468sub size_request {
278 my ($self) = @_; 469 my ($self) = @_;
279 my $chld = $self->get 470 my $chld = $self->child
280 or return (0, 0); 471 or return (0, 0);
281 472
282 $chld->move (2, 2); 473 $chld->move (2, 2);
283 474
284 map { $_ + 4 } $chld->size_request; 475 map { $_ + 4 } $chld->size_request;
285} 476}
286 477
287sub size_allocate { 478sub size_allocate {
288 my ($self, $w, $h) = @_; 479 my ($self, $w, $h) = @_;
480
481 return unless $self->{w} != $w || $self->{h} != $h;
289 482
290 $self->w ($w); 483 $self->{w} = $w;
291 $self->h ($h); 484 $self->{h} = $h;
292 485
293 $self->get->size_allocate ($w - 4, $h - 4); 486 $self->child->size_allocate ($w - 4, $h - 4);
294 $self->get->move (2, 2); 487 $self->child->move (2, 2);
295} 488}
296 489
297sub _draw { 490sub _draw {
298 my ($self) = @_; 491 my ($self) = @_;
299 492
300 my $chld = $self->get; 493 my $chld = $self->child;
301 494
302 my ($w, $h) = $chld->size_request; 495 my ($w, $h) = $chld->size_request;
303 496
304 glBegin GL_QUADS; 497 glBegin GL_QUADS;
305 glColor 0, 0, 0; 498 glColor 0, 0, 0;
306 glTexCoord 0, 0; glVertex 0 , 0; 499 glVertex 0 , 0;
307 glTexCoord 0, 1; glVertex 0 , $h + 4; 500 glVertex 0 , $h + 4;
308 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 501 glVertex $w + 4 , $h + 4;
309 glTexCoord 1, 0; glVertex $w + 4 , 0; 502 glVertex $w + 4 , 0;
310 glEnd; 503 glEnd;
311 504
312 $chld->draw; 505 $chld->draw;
313} 506}
314 507
508#############################################################################
509
315package Crossfire::Client::Widget::FancyFrame; 510package CFClient::Widget::FancyFrame;
316 511
317our @ISA = Crossfire::Client::Widget::Frame::; 512our @ISA = CFClient::Widget::Bin::;
318 513
319use SDL::OpenGL; 514use SDL::OpenGL;
320 515
321sub new { 516my @tex =
322 my ($self, $theme) = @_;
323 $self = $self->SUPER::new;
324
325 $self->{txts} = [
326 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 517 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
327 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 518 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
328 ];
329 $self
330}
331 519
332sub size_request { 520sub size_request {
333 my ($self) = @_; 521 my ($self) = @_;
522
334 my ($w, $h) = $self->get->size_request; 523 my ($w, $h) = $self->SUPER::size_request;
335 524
336 $h += $self->{txts}->[1]->{height}; 525 $h += $tex[1]->{h};
337 $h += $self->{txts}->[4]->{height}; 526 $h += $tex[4]->{h};
338 $w += $self->{txts}->[2]->{width}; 527 $w += $tex[2]->{w};
339 $w += $self->{txts}->[3]->{width}; 528 $w += $tex[3]->{w};
340 529
341 ($w, $h) 530 ($w, $h)
342} 531}
343 532
344sub size_allocate { 533sub size_allocate {
345 my ($self, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
346 535
347 $self->w ($w); 536 return unless $self->{w} != $w || $self->{h} != $h;
348 $self->h ($h); 537
349 $h -= $self->{txts}->[1]->{height}; 538 $self->SUPER::size_allocate ($w, $h);
350 $h -= $self->{txts}->[4]->{height}; 539
351 $w -= $self->{txts}->[2]->{width}; 540 $h -= $tex[1]->{h};
352 $w -= $self->{txts}->[3]->{width}; 541 $h -= $tex[4]->{h};
542 $w -= $tex[2]->{w};
543 $w -= $tex[3]->{w};
353 544
354 $h = $h < 0 ? 0 : $h; 545 $h = $h < 0 ? 0 : $h;
355 $w = $w < 0 ? 0 : $w; 546 $w = $w < 0 ? 0 : $w;
356 warn "CHILD:$w $h\n"; 547
548 my $child = $self->child;
549
357 $self->get->size_allocate ($w, $h); 550 $child->size_allocate ($w, $h);
358 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 551 $child->move ($tex[3]->{w}, $tex[1]->{h});
359} 552}
360 553
361sub _draw { 554sub _draw {
362 my ($self) = @_; 555 my ($self) = @_;
363 556
364 my ($w, $h) = ($self->w, $self->h); 557 my ($w, $h) = ($self->{w}, $self->{h});
365 my ($cw, $ch) = ($self->get->w, $self->get->h); 558 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
366 559
367 glEnable GL_BLEND; 560 glEnable GL_BLEND;
368 glEnable GL_TEXTURE_2D; 561 glEnable GL_TEXTURE_2D;
369 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 562 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
370 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
371 564
372 my $top = $self->{txts}->[1]; 565 my $top = $tex[1];
373 glBindTexture GL_TEXTURE_2D, $top->{name}; 566 $top->draw_quad (0, 0, $w, $top->{h});
374 567
375 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , 0;
377 glTexCoord 0, 1; glVertex 0 , $top->{height};
378 glTexCoord 1, 1; glVertex $w , $top->{height};
379 glTexCoord 1, 0; glVertex $w , 0;
380 glEnd;
381
382 my $left = $self->{txts}->[3]; 568 my $left = $tex[3];
383 glBindTexture GL_TEXTURE_2D, $left->{name}; 569 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
384 570
385 glBegin GL_QUADS;
386 glTexCoord 0, 0; glVertex 0 , $top->{height};
387 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
388 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
389 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
390 glEnd;
391
392 my $right = $self->{txts}->[2]; 571 my $right = $tex[2];
393 glBindTexture GL_TEXTURE_2D, $right->{name}; 572 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
394 573
395 glBegin GL_QUADS;
396 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
397 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
398 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
399 glTexCoord 1, 0; glVertex $w , $top->{height};
400 glEnd;
401
402 my $bottom = $self->{txts}->[4]; 574 my $bottom = $tex[4];
403 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 575 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
404 576
405 glBegin GL_QUADS; 577 my $bg = $tex[0];
406 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
407 glTexCoord 0, 1; glVertex 0 , $h;
408 glTexCoord 1, 1; glVertex $w , $h;
409 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
410 glEnd;
411
412 my $bg = $self->{txts}->[0];
413 glBindTexture GL_TEXTURE_2D, $bg->{name}; 578 glBindTexture GL_TEXTURE_2D, $bg->{name};
414 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 579 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
415 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 580 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
416 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 581 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
417 582
418 my $rep_x = $cw / $bg->{width}; 583 my $rep_x = $cw / $bg->{w};
419 my $rep_y = $ch / $bg->{height}; 584 my $rep_y = $ch / $bg->{h};
420 585
421 glBegin GL_QUADS; 586 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
422 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
423 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
424 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
425 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
426 glEnd;
427 587
428 glDisable GL_BLEND; 588 glDisable GL_BLEND;
429 glDisable GL_TEXTURE_2D; 589 glDisable GL_TEXTURE_2D;
430 590
431 $self->get->draw; 591 $self->child->draw;
432 592
433} 593}
434 594
595#############################################################################
596
435package Crossfire::Client::Widget::Table; 597package CFClient::Widget::Table;
436 598
437our @ISA = Crossfire::Client::Widget::Bin::; 599our @ISA = CFClient::Widget::;
438 600
439use SDL::OpenGL; 601use SDL::OpenGL;
440 602
441sub add { 603sub add {
442 my ($self, $x, $y, $chld) = @_; 604 my ($self, $x, $y, $chld) = @_;
443 my $old_chld = $self->{childs}[$y][$x]; 605 my $old_chld = $self->{children}[$y][$x];
444 606
445 $self->{childs}[$y][$x] = $chld; 607 $self->{children}[$y][$x] = $chld;
446 $chld->set_parent ($self); 608 $chld->set_parent ($self);
447 $self->update; 609 $self->update;
448} 610}
449 611
450sub max_row_height { 612sub max_row_height {
451 my ($self, $row) = @_; 613 my ($self, $row) = @_;
452 614
453 my $hs = 0; 615 my $hs = 0;
454 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 616 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
455 my $c = $self->{childs}->[$row]->[$xi]; 617 my $c = $self->{children}->[$row]->[$xi];
456 if ($c) { 618 if ($c) {
457 my ($w, $h) = $c->size_request; 619 my ($w, $h) = $c->size_request;
458 if ($hs < $h) { $hs = $h } 620 if ($hs < $h) { $hs = $h }
459 } 621 }
460 } 622 }
463 625
464sub max_col_width { 626sub max_col_width {
465 my ($self, $col) = @_; 627 my ($self, $col) = @_;
466 628
467 my $ws = 0; 629 my $ws = 0;
468 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 630 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
469 my $c = ($self->{childs}->[$yi] || [])->[$col]; 631 my $c = ($self->{children}->[$yi] || [])->[$col];
470 if ($c) { 632 if ($c) {
471 my ($w, $h) = $c->size_request; 633 my ($w, $h) = $c->size_request;
472 if ($ws < $w) { $ws = $w } 634 if ($ws < $w) { $ws = $w }
473 } 635 }
474 } 636 }
478sub size_request { 640sub size_request {
479 my ($self) = @_; 641 my ($self) = @_;
480 642
481 my ($hs, $ws) = (0, 0); 643 my ($hs, $ws) = (0, 0);
482 644
483 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 645 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
484 $hs += $self->max_row_height ($yi); 646 $hs += $self->max_row_height ($yi);
485 } 647 }
486 648
487 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 649 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
488 my $wm = 0; 650 my $wm = 0;
489 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 651 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
490 $wm += $self->max_col_width ($xi) 652 $wm += $self->max_col_width ($xi)
491 } 653 }
492 if ($ws < $wm) { $ws = $wm } 654 if ($ws < $wm) { $ws = $wm }
493 } 655 }
494 656
497 659
498sub _draw { 660sub _draw {
499 my ($self) = @_; 661 my ($self) = @_;
500 662
501 my $y = 0; 663 my $y = 0;
502 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 664 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
503 my $x = 0; 665 my $x = 0;
504 666
505 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 667 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
506 668
507 my $c = $self->{childs}->[$yi]->[$xi]; 669 my $c = $self->{children}->[$yi]->[$xi];
508 if ($c) { 670 if ($c) {
509 $c->move ($x, $y, 0); #TODO: Move to size_request 671 $c->move ($x, $y, 0); #TODO: Move to size_request
510 $c->draw if $c; 672 $c->draw if $c;
511 } 673 }
512 674
515 677
516 $y += $self->max_row_height ($yi); 678 $y += $self->max_row_height ($yi);
517 } 679 }
518} 680}
519 681
682#############################################################################
683
520package Crossfire::Client::Widget::VBox; 684package CFClient::Widget::VBox;
521 685
522our @ISA = Crossfire::Client::Widget::Bin::; 686our @ISA = CFClient::Widget::Container::;
523 687
524use SDL::OpenGL; 688use SDL::OpenGL;
525 689
526sub add {
527 my ($self, $chld, $expand) = @_;
528 push @{$self->{childs}}, $chld;
529 $chld->{expand} = $expand;
530 $chld->set_parent ($self);
531 $self->update;
532}
533
534sub size_request { 690sub size_request {
535 my ($self) = @_; 691 my ($self) = @_;
536 692
537 my ($hs, $ws) = (0, 0); 693 my @alloc = map [$_->size_request], @{$self->{children}};
538 for (@{$self->{childs} || []}) { 694
539 my ($w, $h) = $_->size_request;
540 $hs += $h;
541 if ($ws < $w) { $ws = $w }
542 } 695 (
543 696 (List::Util::max map $_->[0], @alloc),
544 return ($ws, $hs); 697 (List::Util::sum map $_->[1], @alloc),
698 )
545} 699}
546 700
547sub size_allocate { 701sub size_allocate {
548 my ($self, $w, $h) = @_; 702 my ($self, $w, $h) = @_;
549 703
704 return unless $self->{w} != $w || $self->{h} != $h;
705
550 $self->w ($w); 706 $self->{w} = $w;
551 $self->h ($h); 707 $self->{h} = $h;
552 708
553 my $exp; 709 return unless $self->{h};
554 my @oth; 710
555 # find expand widget 711 my $children = $self->{children};
556 for (@{$self->{childs}}) { 712
557 if ($_->{expand}) { 713 my @h = map +($_->size_request)[1], @$children;
558 $exp = $_; 714
559 last; 715 my $req_h = List::Util::sum @h;
716
717 if ($req_h > $h) {
718 # ah well, not enough space
719 $_ = $h[$_] * $h / $req_h for @h;
720 } else {
721 my @exp = grep $_->{expand}, @$children;
722 @exp = @$children unless @exp;
723
724 my %exp = map +($_ => 1), @exp;
725
726 for (0 .. $#$children) {
727 my $child = $children->[$_];
728
729 my $alloc_h = $h[$_];
730 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
731 $h[$_] = $alloc_h;
560 } 732 }
561 push @oth, $_;
562 }
563
564 my ($ow, $oh);
565
566 # get sizes of other widgets
567 for (@oth) {
568 my ($w, $h) = $_->size_request;
569 $oh += $h;
570 if ($ow < $w) { $ow = $w }
571 } 733 }
572 734
573 my $y = 0; 735 my $y = 0;
574 for (@{$self->{childs}}) { 736 for (0 .. $#$children) {
737 my $child = $children->[$_];
738 my $h = $h[$_];
575 $_->move (0, $y); 739 $child->move (0, $y);
576
577 if ($_ == $exp) {
578 $_->size_allocate ($w, $h - $oh);
579 $y += $h - $oh;
580 } else {
581 my ($cw, $h) = $_->size_request;
582 $_->size_allocate ($w, $h); 740 $child->size_allocate ($w, $h);
741
583 $y += $h; 742 $y += $h;
584 }
585 } 743 }
586} 744}
587 745
588sub _draw { 746#############################################################################
589 my ($self) = @_;
590 747
591 my ($x, $y);
592 for (@{$self->{childs} || []}) {
593 $_->draw;
594 $y += $_->h;
595 }
596}
597
598package Crossfire::Client::Widget::Label; 748package CFClient::Widget::Label;
599 749
600our @ISA = Crossfire::Client::Widget::; 750our @ISA = CFClient::Widget::;
601 751
602use SDL::OpenGL; 752use SDL::OpenGL;
603 753
604sub new { 754sub new {
605 my ($class, $x, $y, $z, $height, $text) = @_; 755 my ($class, %arg) = @_;
606 756
607 # TODO: color, and make height, xyz etc. optional 757 my $self = $class->SUPER::new (
608 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 758 fg => [1, 1, 1],
759 height => $::FONTSIZE,
760 text => "",
761 align => -1,
762 layout => new CFClient::Layout,
763 %arg
764 );
609 765
610 $self->set_text ($text); 766 $self->set_text ($self->{text});
611 767
612 $self 768 $self
769}
770
771sub escape_text {
772 local $_ = $_[1];
773
774 s/&/&amp;/g;
775 s/>/&gt;/g;
776 s/</&lt;/g;
777
778 $_[1]
613} 779}
614 780
615sub set_text { 781sub set_text {
616 my ($self, $text) = @_; 782 my ($self, $text) = @_;
617 783
618 $self->{text} = $text; 784 $self->{text} = $text;
619 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 785 $self->{layout}->set_markup ($text);
620 786
787 delete $self->{texture};
621 $self->update; 788 $self->update;
622} 789}
623 790
624sub get_text { 791sub get_text {
625 my ($self, $text) = @_; 792 my ($self, $text) = @_;
628} 795}
629 796
630sub size_request { 797sub size_request {
631 my ($self) = @_; 798 my ($self) = @_;
632 799
633 ( 800 $self->{layout}->set_width;
801 $self->{layout}->set_height ($self->{height});
802 $self->{layout}->size
803# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
804# (
634 $self->{texture}{width}, 805# $self->{texture}{w},
635 $self->{texture}{height}, 806# $self->{texture}{h},
636 ) 807# )
808# } else {
809# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
810#
811# ($w, $h)
812# }
813}
814
815sub size_allocate {
816 my ($self, $w, $h) = @_;
817
818 return unless $self->{w} != $w || $self->{h} != $h;
819
820 $self->SUPER::size_allocate ($w, $h);
821 delete $self->{texture};
822}
823
824sub update {
825 my ($self) = @_;
826
827 delete $self->{texture};
828 $self->SUPER::update;
637} 829}
638 830
639sub _draw { 831sub _draw {
640 my ($self) = @_; 832 my ($self) = @_;
641 833
642 my $tex = $self->{texture}; 834 my $tex = $self->{texture} ||= do {
835 $self->{layout}->set_width ($self->{w});
836 new_from_layout CFClient::Texture $self->{layout};
837 };
643 838
644 glEnable GL_BLEND; 839 glEnable GL_BLEND;
645 glEnable GL_TEXTURE_2D; 840 glEnable GL_TEXTURE_2D;
646 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
648 glBindTexture GL_TEXTURE_2D, $tex->{name};
649 843
650 glColor 1, 0, 0, 1; # TODO color 844 glColor @{$self->{fg}};
651 845
652 glBegin GL_QUADS; 846 my $x =
653 glTexCoord 0, 0; glVertex 0 , 0; 847 $self->{align} < 0 ? 0
654 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 848 : $self->{align} > 0 ? $self->{w} - $self->{texture}{w}
655 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 849 : ($self->{w} + $self->{texture}{w}) * 0.5;
656 glTexCoord 1, 0; glVertex $tex->{width}, 0; 850
657 glEnd; 851 $tex->draw_quad ($x, 0);
658 852
659 glDisable GL_BLEND; 853 glDisable GL_BLEND;
660 glDisable GL_TEXTURE_2D; 854 glDisable GL_TEXTURE_2D;
661} 855}
662 856
857#############################################################################
858
663package Crossfire::Client::Widget::TextEntry; 859package CFClient::Widget::Entry;
664 860
665our @ISA = Crossfire::Client::Widget::Label::; 861our @ISA = CFClient::Widget::Label::;
666 862
667use SDL; 863use SDL;
668use SDL::OpenGL; 864use SDL::OpenGL;
669 865
866sub new {
867 my $class = shift;
868
869 $class->SUPER::new (
870 fg => [1, 1, 1],
871 bg => [0, 0, 0, 0.4],
872 active_bg => [1, 1, 1],
873 active_fg => [0, 0, 0],
874 @_
875 )
876}
877
878sub _set_text {
879 my ($self, $text) = @_;
880
881 $self->{last_activity} = $::NOW;
882
883 $self->{text} = $text;
884 $self->{layout}->set_width ($self->{w});
885
886 $text =~ s/./*/g if $self->{hidden};
887
888
889 $self->{layout}->set_markup ($self->escape_text ($text));
890
891 $text = substr $text, 0, $self->{cursor};
892 utf8::encode $text;
893
894 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
895}
896
897sub size_request {
898 my ($self) = @_;
899
900 my ($w, $h) = $self->SUPER::size_request;
901
902 ($w + 1, $h) # add 1 for cursor
903}
904
905sub size_allocate {
906 my ($self, $w, $h) = @_;
907
908 return unless $self->{w} != $w || $self->{h} != $h;
909
910 $self->SUPER::size_allocate ($w, $h);
911
912 $self->_set_text ($self->{text});
913}
914
915sub set_text {
916 my ($self, $text) = @_;
917
918 $self->{cursor} = length $text;
919 $self->_set_text ($text);
920 $self->update;
921}
922
670sub key_down { 923sub key_down {
671 my ($self, $ev) = @_; 924 my ($self, $ev) = @_;
672 925
673 my $mod = $ev->key_mod; 926 my $mod = $ev->key_mod;
674 my $sym = $ev->key_sym; 927 my $sym = $ev->key_sym;
675 928
676 $ev->set_unicode (1);
677 my $uni = $ev->key_unicode; 929 my $uni = $ev->key_unicode;
678 930
679 my $text = $self->get_text; 931 my $text = $self->get_text;
680 932
681 if ($sym == SDLK_BACKSPACE) { 933 if ($sym == SDLK_BACKSPACE) {
682 substr $text, -1, 1, ''; 934 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
683 935 } elsif ($sym == SDLK_DELETE) {
936 substr $text, $self->{cursor}, 1, "";
937 } elsif ($sym == SDLK_LEFT) {
938 --$self->{cursor} if $self->{cursor};
939 } elsif ($sym == SDLK_RIGHT) {
940 ++$self->{cursor} if $self->{cursor} < length $self->{text};
684 } elsif ($uni) { 941 } elsif ($uni) {
685 $text .= chr $uni; 942 substr $text, $self->{cursor}++, 0, chr $uni;
686 } 943 }
944
687 $self->set_text ($text); 945 $self->_set_text ($text);
946 $self->update;
688} 947}
689 948
949sub focus_in {
950 my ($self) = @_;
951
952 $self->{last_activity} = $::NOW;
953
954 $self->SUPER::focus_in;
955}
956
957sub button_down {
958 my ($self, $ev, $x, $y) = @_;
959
960 $self->SUPER::button_down ($ev, $x, $y);
961
962 my $idx = $self->{layout}->xy_to_index ($x, $y);
963
964 # byte-index to char-index
965 my $text = $self->{layout};
966 utf8::encode $text;
967 $self->{cursor} = length substr $text, 0, $idx;
968
969 $self->_set_text ($self->{text});
970 $self->update;
971}
972
973sub mouse_motion {
974 my ($self, $ev, $x, $y) = @_;
975# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
976}
977
978sub _draw {
979 my ($self) = @_;
980
981 local $self->{fg} = $self->{fg};
982
983 if ($FOCUS == $self) {
984 glColor @{$self->{active_bg}};
985 $self->{fg} = $self->{active_fg};
986 } else {
987 glColor @{$self->{bg}};
988 }
989
990 glBegin GL_QUADS;
991 glVertex 0 , 0;
992 glVertex 0 , $self->{h};
993 glVertex $self->{w}, $self->{h};
994 glVertex $self->{w}, 0;
995 glEnd;
996
997 $self->SUPER::_draw;
998
999 #TODO: force update every cursor change :(
1000 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1001 glColor @{$self->{fg}};
1002 glBegin GL_LINES;
1003 glVertex $self->{cur_x}, $self->{cur_y};
1004 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1005 glEnd;
1006 }
1007}
1008
1009#############################################################################
1010
1011package CFClient::Widget::Slider;
1012
1013use strict;
1014
1015use SDL::OpenGL;
1016use SDL::OpenGL::Constants;
1017
1018our @ISA = CFClient::Widget::DrawBG::;
1019
1020sub size_request {
1021 my ($self) = @_;
1022
1023 my $w = 50;
1024 my $h = 10;
1025
1026 $self->{vertical} ? ($h, $w) : ($w, $h)
1027}
1028
1029sub new {
1030 my $class = shift;
1031
1032 # range [value, low, high, page]
1033
1034 $class->SUPER::new (
1035 fg => [1, 1, 1],
1036 active_fg => [0, 0, 0],
1037 range => [0, 0, 100, 10],
1038 vertical => 1,
1039 @_
1040 )
1041}
1042
1043sub button_down {
1044 my ($self, $ev, $x, $y) = @_;
1045
1046 $self->SUPER::button_down ($ev, $x, $y);
1047 $self->mouse_motion ($ev, $x, $y);
1048}
1049
1050sub mouse_motion {
1051 my ($self, $ev, $x, $y) = @_;
1052
1053 if ($GRAB == $self) {
1054 my ($value, $lo, $hi, $page) = @{$self->{range}};
1055
1056 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1057
1058 $x = $x * ($hi - $lo) / $w + $lo;
1059 $x = $lo if $x < $lo;
1060 $x = $hi - $page if $x > $hi - $page;
1061 $self->{range}[0] = $x;
1062
1063 $self->emit (changed => $x);
1064 $self->update;
1065 }
1066}
1067
1068sub _draw {
1069 my ($self) = @_;
1070
1071 $self->SUPER::_draw ();
1072
1073 my ($w, $h) = @$self{qw(w h)};
1074
1075 if ($self->{vertical}) {
1076 # draw a vertical slider like a rotated horizontal slider
1077
1078 glRotate 90, 0, 0, 1;
1079 glTranslate 0, -$self->{w}, 0;
1080
1081 ($w, $h) = ($h, $w);
1082 }
1083
1084 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1085 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1086
1087 my ($value, $lo, $hi, $page) = @{$self->{range}};
1088
1089 $page = int $page * $w / ($hi - $lo);
1090 $value = int +($value - $lo) * $w / ($hi - $lo);
1091
1092 $w -= $page;
1093 $page &= ~1;
1094 glTranslate $page * 0.5, 0, 0;
1095
1096 glColor @$fg;
1097 glBegin GL_LINES;
1098 glVertex 0, 0; glVertex 0, $h;
1099 glVertex $w - 1, 0; glVertex $w - 1, $h;
1100 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1101 glEnd;
1102
1103 my $knob_a = $value - $page * 0.5;
1104 my $knob_b = $value + $page * 0.5;
1105
1106 glBegin GL_QUADS;
1107 glColor @$fg;
1108 glVertex $knob_a, 0;
1109 glVertex $knob_a, $h;
1110 glVertex $knob_b, $h;
1111 glVertex $knob_b, 0;
1112
1113 if ($knob_a < $knob_b - 2) {
1114 glColor @$bg;
1115 glVertex $knob_a + 1, 1;
1116 glVertex $knob_a + 1, $h - 1;
1117 glVertex $knob_b - 1, $h - 1;
1118 glVertex $knob_b - 1, 1;
1119 }
1120 glEnd;
1121}
1122
1123#############################################################################
1124
690package Crossfire::Client::Widget::MapWidget; 1125package CFClient::Widget::MapWidget;
691 1126
692use strict; 1127use strict;
693 1128
694use List::Util qw(min max); 1129use List::Util qw(min max);
695 1130
696use SDL; 1131use SDL;
697use SDL::OpenGL; 1132use SDL::OpenGL;
698use SDL::OpenGL::Constants; 1133use SDL::OpenGL::Constants;
699 1134
700our @ISA = Crossfire::Client::Widget::; 1135our @ISA = CFClient::Widget::;
1136
1137sub new {
1138 my $class = shift;
1139
1140 $class->SUPER::new (
1141 z => -1,
1142 list => (glGenLists 1),
1143 @_
1144 )
1145}
701 1146
702sub key_down { 1147sub key_down {
703 print "MAPKEYDOWN\n"; 1148 print "MAPKEYDOWN\n";
704} 1149}
705 1150
706sub key_up { 1151sub key_up {
707} 1152}
708 1153
709sub size_request { 1154sub size_request {
710 1155 (
1156 1 + int $::WIDTH / 32,
1157 1 + int $::HEIGHT / 32,
1158 )
711} 1159}
712 1160
713sub size_allocate { 1161sub update {
1162 my ($self) = @_;
1163
1164 $self->{need_update} = 1;
714} 1165}
715 1166
716sub _draw { 1167sub _draw {
717 my ($self) = @_; 1168 my ($self) = @_;
718 1169
1170 if (delete $self->{need_update}) {
1171 glNewList $self->{list}, GL_COMPILE;
1172
719 my $mx = $::CONN->{mapx}; 1173 my $mx = $::CONN->{mapx};
720 my $my = $::CONN->{mapy}; 1174 my $my = $::CONN->{mapy};
721 1175
722 my $map = $::CONN->{map}; 1176 my $map = $::CONN->{map};
723 1177
724 my ($xofs, $yofs); 1178 my ($xofs, $yofs);
725 1179
726 my $sw = 1 + int $::WIDTH / 32; 1180 my $sw = 1 + int $::WIDTH / 32;
727 my $sh = 1 + int $::HEIGHT / 32; 1181 my $sh = 1 + int $::HEIGHT / 32;
728 1182
729 if ($::CONN->{mapw} > $sw) { 1183 if ($::CONN->{mapw} > $sw) {
730 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1184 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
731 } else { 1185 } else {
732 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1186 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
733 } 1187 }
734 1188
735 if ($::CONN->{maph} > $sh) { 1189 if ($::CONN->{maph} > $sh) {
736 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1190 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
737 } else { 1191 } else {
738 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1192 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
739 } 1193 }
740 1194
741 glEnable GL_TEXTURE_2D; 1195 glEnable GL_TEXTURE_2D;
742 glEnable GL_BLEND; 1196 glEnable GL_BLEND;
743 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1197 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
744 1198
745 my $sw4 = ($sw + 3) & ~3; 1199 my $sw4 = ($sw + 3) & ~3;
746 my $lighting = "\x00" x ($sw4 * $sh); 1200 my $darkness = "\x00" x ($sw4 * $sh);
747 1201
748 for my $x (0 .. $sw - 1) { 1202 for my $x (0 .. $sw - 1) {
1203 my $row = $map->[$x + $xofs];
749 for my $y (0 .. $sh - 1) { 1204 for my $y (0 .. $sh - 1) {
750 1205
751 my $cell = $map->[$x + $xofs][$y + $yofs] 1206 my $cell = $row->[$y + $yofs]
752 or next; 1207 or next;
753 1208
754 my $darkness = $cell->[0] * (1 / 255); 1209 my $dark = $cell->[0];
755 if ($darkness < 0) { 1210 if ($dark < 0) {
756 $darkness = 0.15; 1211 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1212 } else {
1213 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
757 } 1214 }
758 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
759 1215
760 for my $num (grep $_, @$cell[1,2,3]) { 1216 for my $num (grep $_, @$cell[1,2,3]) {
761 my $tex = $::CONN->{face}[$num]{texture} || next; 1217 my $tex = $::CONN->{face}[$num]{texture} || next;
1218
1219 my ($w, $h) = @$tex{qw(w h)};
762 1220
763 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1221 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
764
765 my $w = $tex->{width};
766 my $h = $tex->{height};
767
768 my $px = ($x + 1) * 32 - $w;
769 my $py = ($y + 1) * 32 - $h;
770
771 glBegin GL_QUADS;
772 glTexCoord 0, 0; glVertex $px , $py;
773 glTexCoord 0, 1; glVertex $px , $py + $h;
774 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
775 glTexCoord 1, 0; glVertex $px + $w, $py;
776 glEnd; 1222 }
777 } 1223 }
778 } 1224 }
779 }
780 1225
781# if (1) { # higher quality darkness 1226# if (1) { # higher quality darkness
782# $lighting =~ s/(.)/$1$1$1/gs; 1227# $lighting =~ s/(.)/$1$1$1/gs;
783# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 1228# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
784# 1229#
786# 1231#
787# $lighting = $pb->get_pixels; 1232# $lighting = $pb->get_pixels;
788# $lighting =~ s/(.)../$1/gs; 1233# $lighting =~ s/(.)../$1/gs;
789# } 1234# }
790 1235
791 $lighting = new Crossfire::Client::Texture
792 width => $sw4,
793 height => $sh,
794 data => $lighting,
795 internalformat => GL_ALPHA4,
796 format => GL_ALPHA;
797
798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1236 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
799 glColor 0, 0, 0, 0.75;
800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1237 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
801 glBindTexture GL_TEXTURE_2D, $lighting->{name};
802 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
803 glBegin GL_QUADS;
804 glTexCoord 0, 0; glVertex 0 , 0;
805 glTexCoord 0, 1; glVertex 0 , $sh * 32;
806 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
807 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
808 glEnd;
809 1238
1239 $darkness = new CFClient::Texture
1240 width => $sw4,
1241 height => $sh,
1242 data => $darkness,
1243 internalformat => GL_ALPHA,
1244 format => GL_ALPHA;
1245
1246 glColor 0.45, 0.45, 0.45, 1;
1247 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1248
810 glDisable GL_TEXTURE_2D; 1249 glDisable GL_TEXTURE_2D;
811 glDisable GL_BLEND; 1250 glDisable GL_BLEND;
1251
1252 glEndList;
1253 }
1254
1255 glCallList $self->{list};
812} 1256}
813 1257
814my %DIR = ( 1258my %DIR = (
815 SDLK_KP8, [1, "north"], 1259 SDLK_KP8, [1, "north"],
816 SDLK_KP9, [2, "northeast"], 1260 SDLK_KP9, [2, "northeast"],
860 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1304 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
861 $::CONN->send ("command run_stop"); 1305 $::CONN->send ("command run_stop");
862 } 1306 }
863} 1307}
864 1308
1309#############################################################################
1310
865package Crossfire::Client::Widget::Animator; 1311package CFClient::Widget::Animator;
866 1312
867use SDL::OpenGL; 1313use SDL::OpenGL;
868 1314
869our @ISA = Crossfire::Client::Widget::Bin::; 1315our @ISA = CFClient::Widget::Bin::;
870 1316
871sub moveto { 1317sub moveto {
872 my ($self, $x, $y) = @_; 1318 my ($self, $x, $y) = @_;
873 1319
874 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1320 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
875 $self->{speed} = 0.2; 1321 $self->{speed} = 0.001;
876 $self->{time} = 1; 1322 $self->{time} = 1;
877 1323
878 ::animation_start $self; 1324 ::animation_start $self;
879} 1325}
880 1326
895 1341
896sub _draw { 1342sub _draw {
897 my ($self) = @_; 1343 my ($self) = @_;
898 1344
899 glPushMatrix; 1345 glPushMatrix;
900 glRotate $self->{time} * 10000, 0, 1, 0; 1346 glRotate $self->{time} * 1000, 0, 1, 0;
901 $self->{child}->draw; 1347 $self->{children}[0]->draw;
902 glPopMatrix; 1348 glPopMatrix;
903} 1349}
904 1350
9051; 1351#############################################################################
906 1352
1353package CFClient::Widget::Toplevel;
1354
1355our @ISA = CFClient::Widget::Container::;
1356
1357sub size_request {
1358 ($::WIDTH, $::HEIGHT)
1359}
1360
1361sub size_allocate {
1362 my ($self, $w, $h) = @_;
1363
1364 $self->SUPER::size_allocate ($w, $h);
1365
1366 $_->size_allocate ($_->size_request)
1367 for @{$self->{children}};
1368}
1369
1370sub translate {
1371 my ($self, $x, $y) = @_;
1372
1373 ($x, $y)
1374}
1375
1376sub update {
1377 my ($self) = @_;
1378
1379 $self->size_allocate ($self->size_request);
1380 ::refresh ();
1381}
1382
1383sub add {
1384 my ($self, $widget) = @_;
1385
1386 $self->SUPER::add ($widget);
1387
1388 $widget->size_allocate ($widget->size_request);
1389}
1390
1391sub draw {
1392 my ($self) = @_;
1393
1394 $self->_draw;
1395}
1396
1397#############################################################################
1398
1399package CFClient::Widget;
1400
1401$TOPLEVEL = new CFClient::Widget::Toplevel;
1402
14031
1404

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines