class Label(Panel): def __init__(self, text, x, y, **kwargs): self.text = text self.text_color = kwargs.pop('text_color', Color.from_string('blue')) self.font_size = kwargs.pop('font_size', 10) self.font_name = kwargs.pop('font_name', 'Times New Roman') self.bg_color = kwargs.pop('bg_color', Color(255, 255, 255)) self.v_spacing = kwargs.pop('v_spacing', 2) self.h_spacing = kwargs.pop('h_spacing', 2) self.x = x self.y = y self.batch = pyglet.graphics.Batch() self.document = pyglet.text.document.UnformattedDocument(text) self.font = self.document.get_font() height = self.font.ascent - self.font.descent self.label = pyglet.text.Label(self.text, font_name=self.font_name, font_size=self.font_size, x=self.x + self.h_spacing, y=self.y + self.v_spacing) width = self.label.content_width + 1 self.border = Rectangle(self.x, self.y, width + self.h_spacing, height + self.v_spacing) self.size = (width, height) def render(self): self.border.render() self.label.draw()
def render(self, batch): self.frame.render(batch) l_x, l_y = self.calc_label_position(self.label) label = pyglet.text.Label(self.text, x=l_x, y=l_y, batch=batch) box_y = round((self.frame.height - 12) / 2) box = Rectangle(self.x + self.label_x_pad, self.y + box_y, 12, 12, fill=self.selected) box.render(batch)
class Panel(Widget, Clickable): def __init__(self, x, y, width, height, title=None, *args, **kwargs): self.title_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.title_font_size = kwargs.pop('title_font_size', 12) self.title_color = kwargs.pop('title_color', Color(255, 255, 255)) self.title_x_pad = kwargs.pop('title_x_pad', 4) self.title_y_pad = kwargs.pop('title_y_pad', 4) super(Panel, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = width self.height = height self.title = title self.frame = Rectangle(x, y, width, height, *args, **kwargs) self.vertices = self.frame.vertices def __str__(self): return 'Panel ({} x {}) at ({}, {})'.format(self.height, self.width, self.x, self.y) def calc_title_position(self, label): w = label.content_width h = label.content_height x = self.x + self.title_x_pad y = self.y + self.height - (h + self.title_y_pad) return (x, y) def render(self, batch): if self.title: self.vertex_lists = [] label = pyglet.text.Label(self.title) t_x, t_y = self.calc_title_position(label) label = pyglet.text.Label(self.title, x=t_x, y=t_y, batch=batch) sep_y = self.y + self.height - 2 * self.title_y_pad - label.content_height print self.x, sep_y, self.x + self.width separator = Line(self.x, sep_y, self.x + self.width, sep_y) batch.add(*separator.render()) batch.add(*self.frame.render()) self.vertex_lists.append(separator.render()) self.vertex_lists.append(self.frame.render()) # def click(self, x, y, button): # # pass # #
def __init__(self, x, y, options=None, *args, **kwargs): self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) super(ListBox, self).__init__(*args, **kwargs) if not options: self.options = [] else: self.options = options self.selection = None self.x = x self.y = y self.frame = Rectangle(x, y, 0, 0, *args, **kwargs) self.expanded = False
def __init__(self, x, y, width, height, title=None, *args, **kwargs): self.title_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.title_font_size = kwargs.pop('title_font_size', 12) self.title_color = kwargs.pop('title_color', Color(255, 255, 255)) self.title_x_pad = kwargs.pop('title_x_pad', 4) self.title_y_pad = kwargs.pop('title_y_pad', 4) super(Panel, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = width self.height = height self.title = title self.frame = Rectangle(x, y, width, height, *args, **kwargs) self.vertices = self.frame.vertices
class Option(Widget): def __init__(self, text, value, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.bg_color = kwargs.pop('bg_color', Color.from_hex('#0099cc')) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.value = value self.depth = 0 self.expanded = False super(Option, self).__init__(*args, **kwargs) self.label = pyglet.text.Label(text) self.text = text self.width, self.height = self.calc_item_size() self.frame = Rectangle(0, 0, self.width, self.height, *args, **kwargs) def calc_item_size(self): w = round(1.5 * self.label.content_width + 2 * self.label_x_pad) h = round(self.label.content_height + 2 * self.label_y_pad) return w, h def set_geometry(self, x, y, width, height): self.x, self.y, self.width, self.height = x, y, width, height self.frame.set_geometry(x, y, width, height) def calc_label_position(self, label): h = label.content_height mid_y = round(self.y + self.height / 2.0) x = self.x + self.label_x_pad y = mid_y - round(h / 2.0) + 2.0 return x, y def render(self, batch, group=None): self.frame.render(batch, group=group) l_x, l_y = self.calc_label_position(self.label) label = pyglet.text.Label(self.text, x=l_x, y=l_y, batch=batch, group=group)
def __init__(self, text, value, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.bg_color = kwargs.pop('bg_color', Color.from_hex('#0099cc')) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.value = value self.depth = 0 self.expanded = False super(Option, self).__init__(*args, **kwargs) self.label = pyglet.text.Label(text) self.text = text self.width, self.height = self.calc_item_size() self.frame = Rectangle(0, 0, self.width, self.height, *args, **kwargs)
def __init__(self, x, y, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.bg_color = kwargs.pop('bg_color', Color(255, 0, 0)) self.click_color = kwargs.pop('click_color', Color.from_hex('#4286f4')) super(RadioGroup, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = None self.height = None self.selection = None self.options = [] self.width, self.height = 0, 0 self.frame = Rectangle(x, y, self.width, self.height, *args, **kwargs) self.vertices = self.frame.vertices
def __init__(self, label, x, y, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.bg_color = kwargs.pop('bg_color', Color(255, 0, 0)) self.click_color = kwargs.pop('click_color', Color.from_hex('#4286f4')) super(Button, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = None self.height = None self.text = label self.label = pyglet.text.Label(label) self.width, self.height = self.calc_button_size(self.label) self.frame = Rectangle(x, y, self.width, self.height, *args, **kwargs) self.vertices = self.frame.vertices print self.frame
class Checkbox(Widget, Clickable): def __init__(self, label, x, y, value, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.bg_color = kwargs.pop('bg_color', Color(255, 0, 0)) self.click_color = kwargs.pop('click_color', Color.from_hex('#4286f4')) super(Checkbox, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = None self.height = None self.text = label self.value = value self.selected = False self.label = pyglet.text.Label(label) self.width, self.height = self.calc_button_size(self.label) self.frame = Rectangle(x, y, self.width, self.height, *args, **kwargs) self.vertices = self.frame.vertices def __str__(self): return 'Checkbox ({} x {}) at ({}, {})'.format(self.height, self.width, self.x, self.y) def calc_button_size(self, label): w = round(label.content_width + 2 * self.label_x_pad) + 20 h = round(label.content_height + 2 * self.label_y_pad) return w, h def calc_label_position(self, label): w = label.content_width h = label.content_height mid_x = 10 + round(self.x + self.width / 2.0) mid_y = round(self.y + self.height / 2.0) x = mid_x - round(w / 2.0) y = mid_y - round(h / 2.0) + 2.0 return x, y def on_mouse_press(self, x, y, button, modifiers): if self.frame.on_mouse_press(x, y, button, modifiers): self.dispatch_event('on_checkbox_click', self) self.selected = not self.selected def on_mouse_release(self, x, y, button, modifiers): if self.frame.on_mouse_release(x, y, button, modifiers): #print 'released', self pass def render(self, batch): self.frame.render(batch) l_x, l_y = self.calc_label_position(self.label) label = pyglet.text.Label(self.text, x=l_x, y=l_y, batch=batch) box_y = round((self.frame.height - 12) / 2) box = Rectangle(self.x + self.label_x_pad, self.y + box_y, 12, 12, fill=self.selected) box.render(batch)
class ListBox(Widget): def __init__(self, x, y, options=None, *args, **kwargs): self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) super(ListBox, self).__init__(*args, **kwargs) if not options: self.options = [] else: self.options = options self.selection = None self.x = x self.y = y self.frame = Rectangle(x, y, 0, 0, *args, **kwargs) self.expanded = False def add(self, option): self.options.append(option) self.calc_geometry() def calc_item_size(self): # if self.selection: # label = self.selection.label # w = round(1.5 * label.content_width + 2 * self.selection.label_x_pad) # h = round(label.content_height + 2 * self.selection.label_y_pad) # self.frame.width = w # self.frame.height = h # else: self.frame.width = max([option.width for option in self.options]) + 8 self.frame.height = max([option.height for option in self.options]) def calc_label_position(self, label): h = label.content_height mid_y = round(self.y + self.frame.height / 2.0) x = self.x + self.label_x_pad y = mid_y - round(h / 2.0) + 2.0 return x, y def calc_geometry(self): max_w = max([option.width for option in self.options]) + 8 max_h = max([option.height for option in self.options]) current_x, current_y = self.frame.x, self.frame.y for option in self.options: current_y -= option.frame.height w, h = option.calc_item_size() option.set_geometry(current_x, current_y, w, h) def render(self, batch, group=None): self.calc_item_size() self.calc_geometry() self.frame.set_geometry(self.x, self.y, self.frame.width, self.frame.height) print self, self.frame self.frame.render(batch, group=group) chevron = Triangle([(self.x + self.frame.width - 12, self.y + round(self.frame.height * 0.75)), (self.x + self.frame.width - 8, self.y + round(self.frame.height * 0.25)), (self.x + self.frame.width - 4, self.y + round(self.frame.height * 0.75))]) chevron.render(batch, group=group) if self.selection: l_x, l_y = self.calc_label_position(self.selection.label) label = pyglet.text.Label(self.selection.text, x=l_x, y=l_y, batch=batch, group=group) self.dispatch_event('on_listbox_select', self.selection) if self.expanded: for item in self.options: item.render(batch, group=group) def on_mouse_press(self, x, y, button, modifiers): if self.frame.on_mouse_press(x, y, button, modifiers): if self.expanded: self.expanded = False else: self.expanded = True else: if self.expanded: for option in self.options: if option.frame.on_mouse_press(x, y, button, modifiers): self.selection = option self.expanded = False break def on_mouse_release(self, x, y, button, modifiers): pass
class Button(Widget, Clickable): def __init__(self, label, x, y, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.bg_color = kwargs.pop('bg_color', Color(255, 0, 0)) self.click_color = kwargs.pop('click_color', Color.from_hex('#4286f4')) super(Button, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = None self.height = None self.text = label self.label = pyglet.text.Label(label) self.width, self.height = self.calc_button_size(self.label) self.frame = Rectangle(x, y, self.width, self.height, *args, **kwargs) self.vertices = self.frame.vertices print self.frame def __str__(self): return 'Button ({} x {}) at ({}, {})'.format(self.height, self.width, self.x, self.y) def calc_button_size(self, label): w = round(1.5 * label.content_width + 2 * self.label_x_pad) h = round(label.content_height + 2 * self.label_y_pad) return w, h def calc_label_position(self, label): w = label.content_width h = label.content_height mid_x = round(self.x + self.width / 2.0) mid_y = round(self.y + self.height / 2.0) x = mid_x - round(w / 2.0) y = mid_y - round(h / 2.0) + 2.0 return x, y def on_mouse_press(self, x, y, button, modifiers): if self.frame.on_mouse_press(x, y, button, modifiers): #print 'clicked', self self.frame.fill = True print 'dispatching event' self.dispatch_event('on_button_click', self) self.dispatch_event('on_update', self) def on_mouse_release(self, x, y, button, modifiers): if self.frame.on_mouse_release(x, y, button, modifiers): #print 'released', self self.frame.fill = False def render(self, batch): self.frame.render(batch) l_x, l_y = self.calc_label_position(self.label) label = pyglet.text.Label(self.text, x=l_x, y=l_y, batch=batch)
class RadioGroup(Widget, Clickable): def __init__(self, x, y, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.bg_color = kwargs.pop('bg_color', Color(255, 0, 0)) self.click_color = kwargs.pop('click_color', Color.from_hex('#4286f4')) super(RadioGroup, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = None self.height = None self.selection = None self.options = [] self.width, self.height = 0, 0 self.frame = Rectangle(x, y, self.width, self.height, *args, **kwargs) self.vertices = self.frame.vertices def __str__(self): return 'Radio ({} x {}) at ({}, {})'.format(self.height, self.width, self.x, self.y) def add(self, item): self.options.append(item) item.parent = self self.calc_item_size() def calc_item_size(self): self.frame.width = max([option.width for option in self.options]) + 8 self.frame.height = max([option.height for option in self.options]) def calc_geometry(self): max_w = max([option.width for option in self.options]) + 8 max_h = max([option.height for option in self.options]) current_x, current_y = self.frame.x, self.frame.y for option in self.options: w, h = option.calc_item_size(option.label) option.set_geometry(current_x, current_y, w, h) current_y -= option.frame.height def on_mouse_press(self, x, y, button, modifiers): if self.frame.on_mouse_press(x, y, button, modifiers): for option in self.options: option.on_mouse_press(x, y, button, modifiers) def render(self, batch, group=None): self.calc_item_size() self.calc_geometry() self.frame.set_geometry(self.x, self.y, self.frame.width, self.frame.height) self.frame.render(batch, group=group) for item in self.options: item.render(batch, group=group)