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