/usr/share/pyshared/soya/widget.py is in python-soya 0.15~rc1-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 | # -*- indent-tabs-mode: t -*-
# -*- coding: latin -*-
# Soya 3D
# Copyright (C) 2001-2002 Jean-Baptiste LAMY -- jiba@tuxfamily.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os, os.path
import soya, soya.sdlconst
from soya.opengl import *
WIDGET_RESIZE_MAXIMIZE = ('maximize', )
default_font = soya.Font(os.path.join(soya.DATADIR, "FreeSans.ttf"), 25, 30)
default_font.filename = "DEFAULT_FONT"
# for font_filename in (
# # FreeFont
# "/usr/share/fonts/freefont/FreeSans.ttf", # Mandrake
# "/usr/share/fonts/truetype/freefont/FreeSans.ttf", # Debian
# os.path.join(os.environ.get("WINDIR", ""), "Fonts", "FreeSans.ttf"), # Windows
# os.path.join(soya.DATADIR, "FreeSans.ttf"), # soya's DATADIR
# os.path.join(os.getcwd(), "data", "fonts", "FreeSans.ttf"), # working directory
# # Bluehigh
# "/usr/share/fonts/ttf/western/Bluehigh.ttf", # Mandrake
# # Vera
# "/usr/share/fonts/ttf/vera/Vera.ttf", # Mandrake
# "/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf", # Debian
# "/usr/share/fonts/ttf-bitstream-vera/Vera.ttf", # Gentoo
# "/usr/share/fonts/TTF/Vera.ttf", # Gentoo
# os.path.join(os.environ.get("WINDIR", ""), "Fonts", "Vera.ttf"), # Windows
# os.path.join(soya.DATADIR, "Vera.ttf"), # soya's DATADIR
# os.path.join(os.getcwd(), "data", "fonts", "Vera.ttf"), # working directory
# ):
# if os.path.exists(font_filename):
# default_font = soya.Font(font_filename, 25, 30)
# break
# else:
# print "Soya * WARNING : no font file found !!!! *"
# print "Soya * Please do the following :"
# print "Soya * search a file called 'FreeSans.ttf' on your computer,"
# print "Soya * and send the location of this file and your OS name"
# print "Soya * to soya@oomadness.tuxfamily.org"
# print "Soya * Thanks !"
# default_font = None
# ----------------------------------------
class Widget(object):
def __init__(self, master = None, resize_style = None):
# these attributes are named (left,top) and not (x,y) to avoid confusion with
# the (x,y,z) attributes of 3D objects
self.left = 0
self.top = 0
self.width = 0
self.height = 0
self._resize_style = resize_style
# this attribute is not called parent for avoiding confusion with 3D objects parent
# attribute and also because Camera is a 3D object and a Widget
if master: master.add(self)
else: self.master = None
def get_resize_style(self): return self._resize_style
def set_resize_style(self, resize_style):
self._resize_style = resize_style
if self.master: self.resize(self.master.left, self.master.top, self.master.width, self.master.height)
resize_style = property(get_resize_style, set_resize_style)
def resize(self, parent_left, parent_top, parent_width, parent_height):
# this method can be rewritten for each widget instance or you can use the standard
# resize style statement
if self.resize_style:
for e in self.resize_style:
if type(e) == str:
if e == 'maximize':
self.left = parent_left
self.top = parent_top
self.width = parent_width
self.height = parent_height
elif e == 'parent left':
self.left = parent_left
elif e == 'parent top':
self.top = parent_top
elif e == 'maximize width':
self.width = parent_width - self.left + parent_left
elif e == 'maximize height':
self.height = parent_height - self.top + parent_top
elif e == 'center x':
self.left = int((parent_width - self.width) * 0.5)
elif e == 'center y':
self.top = int((parent_height - self.height) * 0.5)
else:
if e[0] == 'percent left':
self.left = parent_left + int(parent_width * e[1])
elif e[0] == 'percent top':
self.top = parent_top + int(parent_height * e[1])
elif e[0] == 'percent width':
self.width = int(parent_width * e[1])
elif e[0] == 'percent height':
self.height = int(parent_height * e[1])
elif e[0] == 'margin left':
self.left += e[1]
self.width -= e[1]
elif e[0] == 'margin top':
self.top += e[1]
self.height -= e[1]
elif e[0] == 'margin right':
self.width -= e[1]
elif e[0] == 'margin bottom':
self.height -= e[1]
elif e[0] == 'max width':
if self.width > e[1]: self.width = e[1]
elif e[0] == 'max height':
if self.height > e[1]: self.height = e[1]
elif e[0] == 'keep ratio':
# ratio is width / height
if self.height * e[1] < self.width:
self.width = int(self.height * e[1])
else:
self.height = int(self.width / e[1])
def widget_begin_round(self): pass
def widget_advance_time(self, proportion): pass
def widget_end_round(self): pass
# ----------------------------------------
class Group(Widget):
def __init__(self, master = None):
self.children = []
Widget.__init__(self, master, WIDGET_RESIZE_MAXIMIZE)
self.visible = 1
def render(self):
if self.visible:
for widget in self.children:
widget.render()
def resize(self, parent_left, parent_top, parent_width, parent_height):
Widget.resize(self, parent_left, parent_top, parent_width, parent_height)
for widget in self.children: widget.resize(parent_left, parent_top, parent_width, parent_height)
def add(self, widget):
self.children.append(widget)
widget.master = self
widget.resize(self.left, self.top, self.width, self.height)
def insert(self, index, widget):
self.children.insert(index, widget)
widget.master = self
widget.resize(self.left, self.top, self.width, self.height)
def remove(self, widget):
self.children.remove(widget)
widget.master = None
def widget_begin_round(self):
for widget in self.children: widget.widget_begin_round()
def widget_advance_time(self, proportion):
for widget in self.children: widget.widget_advance_time(proportion)
def widget_end_round(self):
for widget in self.children: widget.widget_end_round()
class Clearer(Widget):
def __init__(self, master = None, color = soya.BLACK):
Widget.__init__(self)
self.color = color
if master: master.add(self)
def render(self):
glClearColor(self.color[0], self.color[1], self.color[2], self.color[3])
# ----------------------------------------
# class Label(Widget):
# def __init__(self, master = None, text = "", align = 0, color = (1.0, 1.0, 1.0, 1.0), font = default_font):
# self.text = text
# self.color = color
# self.font = font
# # align: 0 left, 1 center
# self.align = align
# Widget.__init__(self, master)
# def render(self):
# if self.text:
# soya.DEFAULT_MATERIAL.activate()
# glColor4f(*self.color)
# self.font.draw_area(self.text, self.left, self.top, 0.0, self.width, self.height, self.align)
class Label(Widget):
def get_font (self): return self._font
def set_font (self, x): self._font = x; self._changed = -2
def get_color(self): return self._color
def set_color(self, x): self._color = x; self._changed = -2
def get_text (self): return self._text
def set_text (self, x): self._text = x; self._changed = -2
def get_align(self): return self._align
def set_align(self, x): self._align = x; self._changed = -2
font = property(get_font , set_font )
color = property(get_color, set_color)
text = property(get_text , set_text )
align = property(get_align, set_align)
def resize(self, parent_left, parent_top, parent_width, parent_height):
self._changed = -2
Widget.resize(self, parent_left, parent_top, parent_width, parent_height)
def __init__(self, master = None, text = "", align = 0, color = (1.0, 1.0, 1.0, 1.0), font = None, resize_style = None):
self._text = text
self._color = color
self._font = font or default_font
self._align = align # align: 0 left, 1 center
self._changed = -2
self._display_list = soya.DisplayList()
Widget.__init__(self, master, resize_style)
def build_display_list(self):
"""Label.build_display_list()
Really renders the text.
If you override this method, DO NOT SET self.text or self._set here !!!
It might call glyph tessellation that would be included in the display list !"""
glColor4f(*self._color)
self._font.draw_area(self._text, self.left, self.top, 0.0, self.width, self.height, self._align)
def render(self):
soya.DEFAULT_MATERIAL.activate()
if self._changed != self._font._pixels_height:
self._font.create_glyphs(self._text)
glNewList(self._display_list.id, GL_COMPILE_AND_EXECUTE)
self.build_display_list()
glEndList()
self._changed = self._font._pixels_height
else:
glCallList(self._display_list.id)
# class FPSLabel(Widget):
# """FPSLabel
# A label that shows the FPS.
# It works ONLY with soya.main_loop !!!"""
# def resize(self, parent_left, parent_top, parent_width, parent_height):
# self.x = parent_left + parent_width - 120
# self.y = parent_top + parent_height - 37
# def render(self):
# soya.DEFAULT_MATERIAL.activate()
# glColor4f(1.0, 1.0, 1.0, 1.0)
# if soya.MAIN_LOOP:
# default_font.draw("%.1f FPS" % soya.MAIN_LOOP.fps, self.x, self.y)
# else:
# default_font.draw("No main_loop", self.x, self.y)
class FPSLabel(Label):
"""FPSLabel
A label that shows the FPS.
It works ONLY with soya.main_loop !!!"""
def __init__(self, master = None):
Label.__init__(self, master)
self.fps = -1.0
def resize(self, parent_left, parent_top, parent_width, parent_height):
self.width, self.height = self.font.get_print_size("30.0 FPS")
self.width += 10
self.height += 5
self.left = parent_left + parent_width - self.width
self.top = parent_top + parent_height - self.height
self._changed = -2
def widget_begin_round(self):
if soya.MAIN_LOOP:
if self.fps != soya.MAIN_LOOP.fps:
self.fps = soya.MAIN_LOOP.fps
self.text = "%.1f FPS" % self.fps
else:
if self.fps != -2.0:
self.fps = -2.0
self.text = "No main_loop"
class Image(Widget):
def __init__(self, master = None, material = None, color = soya.WHITE, resize_style = None):
# mode 0 : normal
# mode 1 : texcoords follow the height and width of the widget
self.tex_mode = 0
self.material = material or soya.DEFAULT_MATERIAL
self.tex_top = 0.0
self.tex_left = 0.0
self.tex_bottom = 1.0
self.tex_right = 1.0
self.color = color
self.rotz = 0.0
Widget.__init__(self, master, resize_style)
def render(self):
self.material.activate()
if self.material.is_alpha() or (self.color[3] < 1.0): glEnable(GL_BLEND)
glColor4f (*self.color)
glPushMatrix()
hw = self.width / 2
hh = self.height / 2
glTranslatef(float(self.left + hw), float(self.top + hh), 0.0)
if self.rotz: glRotatef (self.rotz, 0.0, 0.0, 1.0)
glBegin (GL_QUADS)
glTexCoord2f(self.tex_left, self.tex_top)
glVertex2i (-hw, -hh)
glTexCoord2f(self.tex_left, self.tex_bottom)
glVertex2i (-hw, -hh + self.height)
glTexCoord2f(self.tex_right, self.tex_bottom)
glVertex2i (-hw + self.width, -hh + self.height)
glTexCoord2f(self.tex_right, self.tex_top)
glVertex2i (-hw + self.width, -hh)
glEnd()
soya.DEFAULT_MATERIAL.activate()
glPopMatrix()
if self.material.is_alpha() or (self.color[3] < 1.0): glDisable(GL_BLEND)
def resize(self, parent_left, parent_top, parent_width, parent_height):
Widget.resize(self, parent_left, parent_top, parent_width, parent_height)
if self.tex_mode == 1:
self.tex_right = float(self.width ) / self.material.texture.width + self.tex_left
self.tex_bottom = float(self.height) / self.material.texture.height + self.tex_top
# ----------------------------------------
class BannerElement:
def __init__(self, text = "", color = (1.0, 1.0, 1.0, 1.0), font = None):
self.text = text
self.color = color
self.font = font or default_font
def compute_size(self, width = None):
if width == None:
t = self.font.get_print_size(self.text)
else:
t = self.font.wordwrap(self.text, width)
self.width = t[0]
self.height = t[1]
class Banner(Widget):
def __init__(self, master = None, elements = []):
self.valid = 0
self.set_elements(elements)
Widget.__init__(self, master, WIDGET_RESIZE_MAXIMIZE)
self.loop = 1
self.total = 0
self.speed = 4.0
self.visible = 1
def set_elements(self, elements, separator = None):
if separator:
for e in elements:
self.elements.append(separator)
self.elements.append(e)
else:
self.elements = elements
self.init()
def add_elements_from_strings(self, strings = [], attribs = {}, separator = None):
try: color = attribs['color']
except: color = (1.0, 1.0, 1.0, 1.0)
try: font = attribs['font']
except: font = default_font
if type(separator) == str:
separator = BannerElement(separator, color, font)
for s in strings:
if separator: self.elements.append(separator)
self.elements.append(BannerElement(s, color, font))
self.init()
def add_elements_from_tuples(self, tuples = [], key_attribs = {}, value_attribs = {}, separator = None):
try: k_color = key_attribs['color']
except: k_color = (1.0, 1.0, 1.0, 1.0)
try: k_font = key_attribs['font']
except: k_font = default_font
try: v_color = value_attribs['color']
except: v_color = (1.0, 1.0, 1.0, 1.0)
try: v_font = value_attribs['font']
except: v_font = default_font
if type(separator) == str:
separator = BannerElement(separator, v_color, v_font)
for t in tuples:
if separator: self.elements.append(separator)
self.elements.append(BannerElement(t[0], k_color, k_font))
self.elements.append(BannerElement(t[1], v_color, v_font))
self.init()
def widget_advance_time(self, proportion):
self._position += proportion * self.speed
self.position = int(self._position)
if self.position >= self.total:
if self.loop:
self._position -= self.total
# self.position -= self.total
else:
if self.master: self.master.remove(self)
class HorizontalBanner(Banner):
def __init__(self, master = None, elements = []):
Banner.__init__(self, master, elements)
self.position = - self.width
self._position = self.position
self.top = 0
self.height = 0
def init(self):
for e in self.elements:
e.compute_size()
if e.font.height > self.height: self.height = e.font.height
self.total += e.width
def render(self):
if self.visible:
n = 0
i = 0
while (i < len(self.elements)):
e = self.elements[i]
n += e.width
if n >= self.position:
# element e is after position
glColor4f(*e.color)
e.font.draw(e.text, n - e.width - self.position, self.top)
while (n - self.position < self.width):
i += 1
if i >= len(self.elements):
if self.loop:
i = 0
else:
break
e = self.elements[i]
glColor4f(*e.color)
e.font.draw(e.text, n - self.position, self.top)
n += e.width
break
i += 1
glColor4f(1.0, 1.0, 1.0, 1.0)
self.valid = 1
def resize(self, parent_left, parent_top, parent_width, parent_height):
Widget.resize(self, parent_left, parent_top, parent_width, parent_height)
if self.valid == 0:
self.position = - self.width
self._position = -self.width
class VerticalBanner(Banner):
def __init__(self, master = None, elements = []):
Banner.__init__(self, master, elements)
self.position = - self.height
self._position = self.position
self.alpha_zone = 150
self.center_text = 1
def init(self):
self.total = 0
for e in self.elements:
e.compute_size(self.width)
self.total += e.height
def render(self):
def render_element(e, y):
def color_for_y(y, color):
return (e.color[0], e.color[1], e.color[2], )
if self.alpha_zone == 0:
glColor4f(*e.color)
e.font.draw_area(e.text, self.left, y, 0.0, self.width, self.height, self.center_text)
else:
if y < self.alpha_zone + self.top:
glColor4f(e.color[0], e.color[1], e.color[2], float(y - self.top) / self.alpha_zone)
elif y > self.top + self.height - self.alpha_zone - e.height:
glColor4f(e.color[0], e.color[1], e.color[2], float(self.height - y - e.height + self.top) / self.alpha_zone)
else:
glColor4f(*e.color)
e.font.draw_area(e.text, self.left, y, 0.0, self.width, self.height, self.center_text)
if self.visible:
n = self.top
i = 0
while (i < len(self.elements)):
e = self.elements[i]
o = n - self.position
n += e.height
if n >= self.position:
# element e is after position
render_element(e, o)
while (n - self.position < self.height + self.top):
i += 1
if i >= len(self.elements):
if self.loop:
i = 0
else:
break
e = self.elements[i]
render_element(e, n - self.position)
n += e.height
break
i += 1
glColor4f(1.0, 1.0, 1.0, 1.0)
self.valid = 1
def resize(self, parent_left, parent_top, parent_width, parent_height):
Widget.resize(self, parent_left, parent_top, parent_width, parent_height)
if self.valid == 0:
self.position = - self.height
self._position = - self.height
self.init()
# ----------------------------------------
class Choice:
def __init__(self, label = '', action = None, value = None, range = None, incr = None):
self.label = label
self.action = action
self.value = value
self.range = range
self.incr = incr
def get_label(self):
if (self.value != None): return '%s - %s' % (self.label, self.value)
else: return self.label
def increment(self):
if (self.incr):
self.value = self.value + self.incr
if (self.range):
if (self.value < self.range[0]): self.value = self.range[0]
if (self.value > self.range[1]): self.value = self.range[1]
else:
nb = len(self.range)
i = 0
while (i < nb):
if (self.range[i] == self.value):
i = i + 1
if (i >= nb): i = 0
self.value = self.range[i]
break
i = i + 1
def decrement(self):
if (self.incr):
self.value = self.value - self.incr
if (self.range):
if (self.value < self.range[0]): self.value = self.range[0]
if (self.value > self.range[1]): self.value = self.range[1]
else:
nb = len(self.range)
i = 0
while (i < nb):
if (self.range[i] == self.value):
i = i - 1
if (i < 0): i = nb - 1
self.value = self.range[i]
break
i = i + 1
def mouse_click(self, button):
if self.value == None:
if button == soya.sdlconst.BUTTON_RIGHT or button == soya.sdlconst.BUTTON_LEFT:
if self.action: self.action()
else:
if button == soya.sdlconst.BUTTON_RIGHT or button == soya.sdlconst.BUTTON_WHEELDOWN:
self.decrement()
elif button == soya.sdlconst.BUTTON_LEFT or button == soya.sdlconst.BUTTON_WHEELUP:
self.increment()
def key_down(self, key_id, mods):
if self.value == None:
if (key_id == soya.sdlconst.K_RETURN or key_id == soya.sdlconst.K_KP_ENTER or key_id == soya.sdlconst.K_SPACE):
if self.action: self.action()
else:
if (key_id == soya.sdlconst.K_RIGHT or key_id == soya.sdlconst.K_PLUS or key_id == soya.sdlconst.K_KP_PLUS or key_id == soya.sdlconst.K_RETURN or key_id == soya.sdlconst.K_KP_ENTER or key_id == soya.sdlconst.K_SPACE):
self.increment()
elif (key_id == soya.sdlconst.K_LEFT or key_id == soya.sdlconst.K_MINUS or key_id == soya.sdlconst.K_KP_MINUS):
self.decrement()
class ChoiceInput:
def __init__(self, label = '', value = ''):
self.label = label
self.value = value
self.grab = 0
def get_label(self):
if (self.value != None):
return '%s - %s' % (self.label, self.value)
else: return self.label
def mouse_click(self, button): pass
def key_down(self, key_id, mods):
if (key_id == soya.sdlconst.K_DELETE or key_id == soya.sdlconst.K_BACKSPACE or key_id == soya.sdlconst.K_CLEAR):
self.value = self.value[:-1]
elif (key_id == soya.sdlconst.K_KP0) or (key_id == soya.sdlconst.K_0): self.value += '0'
elif (key_id == soya.sdlconst.K_KP1) or (key_id == soya.sdlconst.K_1): self.value += '1'
elif (key_id == soya.sdlconst.K_KP2) or (key_id == soya.sdlconst.K_2): self.value += '2'
elif (key_id == soya.sdlconst.K_KP3) or (key_id == soya.sdlconst.K_3): self.value += '3'
elif (key_id == soya.sdlconst.K_KP4) or (key_id == soya.sdlconst.K_4): self.value += '4'
elif (key_id == soya.sdlconst.K_KP5) or (key_id == soya.sdlconst.K_5): self.value += '5'
elif (key_id == soya.sdlconst.K_KP6) or (key_id == soya.sdlconst.K_6): self.value += '6'
elif (key_id == soya.sdlconst.K_KP7) or (key_id == soya.sdlconst.K_7): self.value += '7'
elif (key_id == soya.sdlconst.K_KP8) or (key_id == soya.sdlconst.K_8): self.value += '8'
elif (key_id == soya.sdlconst.K_KP9) or (key_id == soya.sdlconst.K_9): self.value += '9'
elif (key_id == soya.sdlconst.K_KP_PERIOD or key_id == soya.sdlconst.K_PERIOD): self.value += '.'
else:
if mods & (soya.sdlconst.MOD_SHIFT | soya.sdlconst.MOD_CAPS):
if (key_id >= soya.sdlconst.K_a and key_id <= soya.sdlconst.K_z):
self.value += chr(key_id - 32)
elif (key_id == soya.sdlconst.K_SEMICOLON): self.value += '.'
else:
if (key_id >= soya.sdlconst.K_a and key_id <= soya.sdlconst.K_z):
self.value += chr(key_id)
elif (key_id >= 0 and key_id < 256):
self.value += chr(key_id)
class ChoiceList(Widget):
def __init__(self, master = None, choices = [], font = None, color = (1.0, 0.5, 0.5, 1.0), highlight = (1.0, 1.0, 0.0, 1.0), cancel = None, align=1):
self.x_percent = 0.0
self.y_percent = 0.0
self.w_percent = 1.0
self.h_percent = 1.0
Widget.__init__(self, master)
self.choices = choices
self.font = font or default_font
self.color = color
self.highlight = highlight
self.selected = 0
self.visible = 1
self.align=align
if master: master.add(self)
if cancel != None: self.cancel = self.choices[cancel]
else: self.cancel = None
def process_event(self, event):
if (event[0] == soya.sdlconst.KEYDOWN):
self.key_down(event[1], event[2])
elif (event[0] == soya.sdlconst.MOUSEMOTION):
self.mouse_move(event[1], event[2])
elif (event[0] == soya.sdlconst.MOUSEBUTTONDOWN):
self.choices[self.selected].mouse_click(event[1])
elif (event[0] == soya.sdlconst.JOYAXISMOTION):
if event[1] == 0:
if event[2] < 0:
self.key_down(soya.sdlconst.K_LEFT, 0)
elif event[2] > 0:
self.key_down(soya.sdlconst.K_RIGHT, 0)
elif event[1] == 1:
if event[2] < 0:
self.key_down(soya.sdlconst.K_UP, 0)
elif event[2] > 0:
self.key_down(soya.sdlconst.K_DOWN, 0)
elif (event[0] == soya.sdlconst.JOYBUTTONDOWN):
if event[1] // 2 == event[1] / 2.0:
self.key_down(soya.sdlconst.K_RETURN, 0)
else:
self.key_down(soya.sdlconst.K_LEFT, 0)
def key_down(self, key_id, mods):
if key_id == soya.sdlconst.K_DOWN:
self.selected = self.selected + 1
if (self.selected >= len(self.choices)): self.selected = 0
elif key_id == soya.sdlconst.K_UP:
self.selected = self.selected - 1
if (self.selected < 0): self.selected = len(self.choices) - 1
elif key_id == soya.sdlconst.K_ESCAPE :
if self.cancel:
self.cancel.key_down(soya.sdlconst.K_RETURN, mods)
else: self.choices[self.selected].key_down(key_id, mods)
def mouse_move(self, x, y):
if (x >= self.left) and (x <= self.left + self.width):
i = 0
nb = len(self.choices)
h1 = int (self.height / nb)
h2 = int (h1 * 0.5)
t = int (self.top + h1 * 0.5)
while (i < nb):
if (y >= t - h2 and y <= t + h2):
self.selected = i
break
t = t + h1
i = i + 1
# else: self.selected = -1
# else: self.selected = -1
def render(self):
if self.visible:
nb = len (self.choices)
hi = self.height / nb
h = int (self.top + hi * 0.5) - 18
i = 0
while (i < nb):
if (i == self.selected):
glColor4f(*self.highlight)
else:
glColor4f(*self.color)
# Why ? Useless ???
#glDisable(GL_LIGHTING)
self.font.draw_area(self.choices[i].get_label(), self.left, h, 0.0, self.width, h + hi, self.align)
h = h + hi
i = i + 1
# ----------------------------------------
class Selector(Widget):
def __init__(self, master = None, choices = [], title = None, font = None, color = (1.0, 0.5, 0.5, 1.0), highlight = (1.0, 1.0, 0.0, 1.0), arrows = None):
self.x_percent = 0.0
self.y_percent = 0.0
self.w_percent = 1.0
self.h_percent = 1.0
self.arrow_size = 32
self.choices = choices
self.title = title
self.nb_displayed = 5
self.font = font or default_font
self.arrows = arrows
Widget.__init__(self, master)
self.selected = 0
self.display_from = 0
self.selected_arrow = 0
self.color = color
self.highlight = highlight
self.visible = 1
self.enabled = 0
self.scrolling = 0
def resize(self, parent_left, parent_top, parent_width, parent_height):
Widget.resize(self, parent_left, parent_top, parent_width, parent_height)
a = self.height - self.font.height
if self.arrows: a -= 2 * self.arrow_size
self.nb_displayed = int(a / self.font.height)
if not self.nb_displayed & 1: self.nb_displayed -= 1
if self.arrows:
self._h = int(self.top + (self.height - self.font.height * (self.nb_displayed + 1) - 2 * self.arrow_size) * 0.5)
else:
self._h = int(self.top + (self.height - self.font.height * (self.nb_displayed + 1)) * 0.5)
def get_choice_at(self, y):
h = self._h
if y < h: return -4
if self.title:
h += self.font.height
if (y < h):
return -1
h += self.arrow_size
if (y < h): return -2
else:
i = 0
while(i < self.nb_displayed):
h += self.font.height
if (y < h): return self.display_from + i
i += 1
h += self.arrow_size
if (y < h): return -3
return -4
def scroll_up(self):
if (self.display_from > 0): self.display_from -= 1
def scroll_down(self):
if (self.display_from < len(self.choices) - self.nb_displayed): self.display_from += 1
def scroll(self):
if (self.scrolling > 0):
if (self.selected + 1 < len(self.choices)): self.selected += 1
if (self.selected - self.display_from > (self.nb_displayed - 1) * 0.5):
self.scroll_down()
elif (self.scrolling < 0):
if (self.selected > 0): self.selected -= 1
if (self.selected - self.display_from < (self.nb_displayed - 1) * 0.5):
self.scroll_up()
def select(self, index):
if (index >= 0 and index < len(self.choices)):
self.selected = index
self.display_from = int(index - (self.nb_displayed - 1) * 0.5)
if (self.display_from < 0): self.display_from = 0
elif (self.display_from > len(self.choices) - self.nb_displayed): self.display_from = len(self.choices) - self.nb_displayed
def process_event(self, event):
if (event[0] == soya.sdlconst.KEYDOWN):
if (event[1] == soya.sdlconst.K_DOWN):
self.scrolling = 1
# if (self.selected + 1 < len(self.choices)): self.selected += 1
# if (self.selected - self.display_from > (self.nb_displayed - 1) * 0.5):
# self.scroll_down()
elif (event[1] == soya.sdlconst.K_UP):
self.scrolling = -1
# if (self.selected > 0): self.selected -= 1
# if (self.selected - self.display_from < (self.nb_displayed - 1) * 0.5):
# self.scroll_up()
elif (event[0] == soya.sdlconst.KEYUP):
self.scrolling = 0
elif (event[0] == soya.sdlconst.MOUSEMOTION):
i = self.get_choice_at(event[2])
if (i == -2): self.selected_arrow = 1
elif (i == -3): self.selected_arrow = 2
else: self.selected_arrow = 0
elif (event[0] == soya.sdlconst.MOUSEBUTTONDOWN):
if (event[1] == 4): self.scroll_up()
elif (event[1] == 5): self.scroll_down()
else:
i = self.get_choice_at(event[3])
if (i >= 0): self.select(i)
elif (i == -2): self.scroll_up()
elif (i == -3): self.scroll_down()
elif (event[0] == soya.sdlconst.JOYAXISMOTION):
if (event[1] == 1):
if (event[2] == 0):
self.scrolling = 0
elif (event[2] > 0):
self.scrolling = 1
# if (self.selected + 1 < len(self.choices)): self.selected += 1
# if (self.selected - self.display_from > (self.nb_displayed - 1) * 0.5):
# self.scroll_down()
elif (event[2] < 0):
self.scrolling = -1
# if (self.selected > 0): self.selected -= 1
# if (self.selected - self.display_from < (self.nb_displayed - 1) * 0.5):
# self.scroll_up()
self.scroll()
def draw_arrow(self, top, yoffset):
x = int((self.width - self.left - self.arrow_size) * 0.5)
glTexCoord2f(0.0, yoffset)
glVertex2f(x, top)
glTexCoord2f(0.0, yoffset + 0.5)
glVertex2f(x, top + self.arrow_size)
glTexCoord2f(1.0, yoffset + 0.5)
glVertex2f(x + self.arrow_size, top + self.arrow_size)
glTexCoord2f(1.0, yoffset)
glVertex2f(x + self.arrow_size, top)
def render(self):
if self.visible:
if (self.scrolling > 0):
self.scrolling += 1
if (self.scrolling > 8):
self.scrolling = 1
self.scroll()
elif (self.scrolling < 0):
self.scrolling -= 1
if (self.scrolling < -8):
self.scrolling = -1
self.scroll()
h = self._h
if self.title:
if self.enabled: glColor4f(*self.highlight)
else: glColor4f(*self.color)
self.font.draw_area(self.title, self.left, h, 0.0, self.width, self.font.height, 1)
h += self.font.height
if self.arrows:
self.arrows.activate()
if self.arrows.is_alpha(): glEnable(GL_BLEND)
glBegin(GL_QUADS)
if self.display_from > 0:
if self.selected_arrow == 1: glColor4f(*self.highlight)
else: glColor4f(*self.color)
self.draw_arrow(h, 0)
h += self.arrow_size
if self.display_from < len(self.choices) - self.nb_displayed:
if self.selected_arrow == 2: glColor4f(*self.highlight)
else: glColor4f(*self.color)
self.draw_arrow(h + self.font.height * self.nb_displayed, 0.5)
glEnd()
soya.DEFAULT_MATERIAL.activate()
if self.arrows.is_alpha(): glDisable(GL_BLEND)
for i in range(self.display_from, self.display_from + min(self.nb_displayed, len(self.choices))):
if self.selected == i: glColor4f(*self.highlight)
else: glColor4f(*self.color)
self.font.draw_area(self.choices[i], self.left, h, 0.0, self.width, h + self.font.height, 1)
h += self.font.height
|