You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.4 KiB
Python

###
#util functions
###
import pyglet
def center_image(image):
"""Sets an image's anchor point to its center"""
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
def fadeIn_sprite(sprite , currentOpacity ,speed):
if sprite.opacity < 255:
if sprite.opacity + speed > 255:
sprite.opacity = 255
else:
sprite.opacity = currentOpacity + speed
else:
return False # returns False if max reached
def fadeOut_sprite(sprite, currentOpacity ,speed):
if sprite.opacity > 0:
if sprite.opacity - speed < 0:
sprite.opacity = 0
else:
sprite.opacity = currentOpacity - speed
else:
return False # returns False if 0 reached
def fadeIn_label(label, currentColor,speed):
red = currentColor[0]
green = currentColor[1]
blue = currentColor[2]
alpha = currentColor[3]
inProgess = True
if alpha + speed > 255:
alpha = 255
inProgess = False
else:
alpha += speed
toggledColor = (red,green,blue,alpha)
label.color = toggledColor
return inProgess
def toggle_label(label, currentColor):
red = currentColor[0]
green = currentColor[1]
blue = currentColor[2]
alpha = currentColor[3]
if alpha > 0:
alpha = 0
else:
alpha = 255
toggledColor = (red,green,blue,alpha)
label.color = toggledColor
return True
def fading_toggle_label(label, currentColor,remaining_alpha):
if remaining_alpha is 0:
return True
red = currentColor[0]
green = currentColor[1]
blue = currentColor[2]
alpha = currentColor[3]
if alpha > 0:
alpha = 0
else:
alpha = remaining_alpha
toggledColor = (red,green,blue,alpha)
label.color = toggledColor
return True
def fadeIn_widget(widget, Alpha,speed):
inProgess = True
if Alpha + speed > 255:
widget.set_opacity(255)
inProgess = False
else:
widget.set_opacity(Alpha + speed)
return inProgess
def fadeOut_widget(widget, Alpha,speed):
inProgess = True
if Alpha > 0:
if Alpha - speed < 0:
widget.set_opacity(0)
inProgess = False
else:
widget.set_opacity(Alpha - speed)
return inProgress
class Rectangle(object):
'''Draws a rectangle into a batch.'''
def __init__(self, x1, y1, x2, y2, batch):
self.vertex_list = batch.add(4, pyglet.gl.GL_QUADS, None,
('v2i', [x1, y1, x2, y1, x2, y2, x1, y2]),
('c4B', [200, 200, 220, 255] * 4)
)
class TextWidget(object):
"""
Texteingabe Feld
"""
def __init__(self, text, x, y, width, batch):
self.document = pyglet.text.document.UnformattedDocument(text)
self.document.set_style(0, len(self.document.text),
dict(color=(0, 0, 0, 0))
)
font = self.document.get_font()
height = font.ascent - font.descent
# checken wo groups integriert werden können da sich einige sachen überlagern
self.layout = pyglet.text.layout.IncrementalTextLayout(
self.document, width, height, multiline=False, batch=batch)
self.caret = pyglet.text.caret.Caret(self.layout)
self.layout.x = x
self.layout.y = y
# Rectangular outline
pad = 2
#self.rectangle = Rectangle(x - pad, y - pad,
#x + width + pad, y + height + pad, batch)
def hit_test(self, x, y):
return (0 < x - self.layout.x < self.layout.width and
0 < y - self.layout.y < self.layout.height)
def set_opacity (self,alpha):
self.document.set_style(0,len(self.document.text), dict(color=(0,0,0,alpha)))
#self.rectangle.opacity = alpha
def get_opacity (self):
color = self.document.get_style_range('color', 0, len(self.document.text))
#color = self.rectangle.opacity
return color[3]
class ControledTextBox(object):
"""
controller for textdisplay, input via text/button selection, sitewise type specific handling
3 Data feeds are needed:
-> a) basic information about the controller (general size, how many lines, how many columns[and their specific sizes])
-> b) dictionary which element
-> c) dictionary describing a site-tree with indexes
"""
def __init__(self, x, y, width, height,batch):
"""
constructs the basic frame
"""
self.ContentTree = None
self.currentPage = 1
self.xPos = x
self.yPos = y
self.width = width
self.height = height
def render(self):
pass
#todo: anpassen dass mouse daten übergeben werden
"""def on_mouse_motion(self, x, y, dx, dy):
# forward mouse information to active scene
self.activeScene.on_mouse_motion(self, x, y, dx, dy)
self.mouse_x = x
self.mouse_y = y
if self.d_active_widgets is not None:
for key,widget in self.d_active_widgets.items():
if widget.hit_test(x, y):
self.set_mouse_cursor(self.text_cursor)
break
else:
self.set_mouse_cursor(None)
def on_mouse_press(self, x, y, button, modifiers):
# forward mouse information to active scene
self.activeScene.on_mouse_press(self, x, y, button, modifiers)
if button == 1: # Left click
pass
for key,widget in self.d_active_widgets.items():
if widget.hit_test(x, y):
self.set_focus(widget)
break
else:
self.set_focus(None)
if self.focus:
self.focus.caret.on_mouse_press(x, y, button, modifiers)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.focus:
self.focus.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers)
def on_key_press(self, symbol, modifiers):
# forward keypress information to active scene
self.activeScene.on_key_press(self, symbol, modifiers)
if symbol == key.ESCAPE:
self.alive = 0
if symbol == key.TAB:
if modifiers & key.MOD_SHIFT:
dir = -1
else:
dir = 1
if self.focus in self.d_active_widgets:
i = self.d_active_widgets.index(self.focus)
else:
i = 0
dir = 0
self.set_focus(self.d_active_widgets[(i + dir) % len(self.d_active_widgets)])
"""
def setPageStructure(self, d_structure):
"""
sets the structure of the containing pages
types:
-TextSelection -> pick a text from a list of texts (returns index of text in the current page)
-ListSelection -> pick a listentry gathered from a defined datapool (returns the text of the entry)
e.g.: [pageIndex]:(type,typeSpecificDetails)
e.g.: "1.1":(TextSelection,rows,maxVisibleRows)
"""
pass
def setContent(self, d_pageToContent):
"""
sets the content of each page (structure must be considered while filling it)
e.g.: [pageIndex]:{[index1]:(TEXT1), [index2]:(TEXT2)}
"""
pass
def checkInput(self):
"""
check if there is input to give back to the requester
"""
pass