def generate_widget(self, item): if item.id is None: # Labels and separators, radial menu can't show these return None e = self.editor.clone_element("menuitem_template") SVGEditor.set_text(e, item.label) e.attrib['id'] = "menuitem_" + item.id return e
def generate_widget(self, item): if isinstance(item, (Separator, Submenu)) or item.id is None: # Labels and separators, radial menu can't show these return None e = self.editor.clone_element("menuitem_template") SVGEditor.set_text(e, item.label) e.attrib['id'] = "menuitem_" + item.id return e
def get_limit(self, id): a = SVGEditor.find_by_id(self.tree, id) width, height = 0, 0 if not hasattr(a, "parent"): a.parent = None x, y = SVGEditor.get_translation(a, absolute=True) if 'width' in a.attrib: width = float(a.attrib['width']) if 'height' in a.attrib: height = float(a.attrib['height']) return x, y, width, height
def pack_items(self, trash, items): index = 0 pb = self.b.get_pixbuf() image_width = pb.get_width() item_width = 360.0 / len(self.items) a1, a2 = (-90.0 - item_width * 0.5) * PI / 180.0, ( -90.0 + item_width * 0.5) * PI / 180.0 for i in items: # Set size of each arc if SVGWidget.get_element(i.widget, "arc") is not None: l = SVGWidget.get_element(i.widget, "arc") radius = float( l.attrib["radius"] ) # TODO: Find how to get value of 'sodipodi:rx' l.attrib["d"] = l.attrib["d-template"] % ( radius * cos(a1) + image_width / 2, radius * sin(a1) + image_width / 2, radius * cos(a2) + image_width / 2, radius * sin(a2) + image_width / 2, ) # Rotate arc to correct position i.a = (360.0 / float(len(self.items))) * float(index) i.widget.attrib['transform'] = "%s rotate(%s, %s, %s)" % ( i.widget.attrib['transform'], i.a, image_width / 2, image_width / 2) # Rotate text in arc to other direction to keep it horisontal if SVGWidget.get_element(i.widget, "menuitem_text") is not None: l = SVGWidget.get_element(i.widget, "menuitem_text") l.attrib['id'] = "text_" + i.id l.attrib['transform'] = "%s rotate(%s)" % ( l.attrib['transform'], -i.a) # Place up to 3 lines of item label label = i.label.split("\n") first_line = 0 if len(label) == 1: self.editor.remove_element( SVGWidget.get_element(i.widget, "line0")) self.editor.remove_element( SVGWidget.get_element(i.widget, "line2")) first_line = 1 elif len(label) == 2: self.editor.remove_element( SVGWidget.get_element(i.widget, "line0")) first_line = 1 for line in xrange(0, len(label)): l = SVGWidget.get_element(i.widget, "line%s" % (first_line + line, )) if l is None: break SVGEditor.set_text(l, label[line]) # Continue with next menu item i.index = index index += 1 self.editor.remove_element("menuitem_template") self.editor.commit() del self.editor
def pack_items(self, trash, items): index = 0 pb = self.b.get_pixbuf() image_width = pb.get_width() item_width = 360.0 / len(self.items) a1, a2 = (-90.0 - item_width * 0.5) * PI / 180.0, (-90.0 + item_width * 0.5) * PI / 180.0 for i in items: # Set size of each arc if SVGWidget.get_element(i.widget, "arc") is not None: l = SVGWidget.get_element(i.widget, "arc") radius = float(l.attrib["radius"]) # TODO: Find how to get value of 'sodipodi:rx' l.attrib["d"] = l.attrib["d-template"] % ( radius * cos(a1) + image_width / 2, radius * sin(a1) + image_width / 2, radius * cos(a2) + image_width / 2, radius * sin(a2) + image_width / 2, ) # Rotate arc to correct position i.a = (360.0 / float(len(self.items))) * float(index) i.widget.attrib['transform'] = "%s rotate(%s, %s, %s)" % ( i.widget.attrib['transform'], i.a, image_width / 2, image_width / 2) # Rotate text in arc to other direction to keep it horisontal if SVGWidget.get_element(i.widget, "menuitem_text") is not None: l = SVGWidget.get_element(i.widget, "menuitem_text") l.attrib['id'] = "text_" + i.id l.attrib['transform'] = "%s rotate(%s)" % (l.attrib['transform'], -i.a) # Place up to 3 lines of item label label = i.label.split("\n") first_line = 0 if len(label) == 1: self.editor.remove_element(SVGWidget.get_element(i.widget, "line0")) self.editor.remove_element(SVGWidget.get_element(i.widget, "line2")) first_line = 1 elif len(label) == 2: self.editor.remove_element(SVGWidget.get_element(i.widget, "line0")) first_line = 1 for line in xrange(0, len(label)): l = SVGWidget.get_element(i.widget, "line%s" % (first_line + line,)) if l is None: break SVGEditor.set_text(l, label[line]) # Continue with next menu item i.index = index index += 1 self.editor.remove_element("menuitem_template") self.editor.commit() del self.editor
def place_marker(self, gen, root): x1, y1 = self.x, self.y x2, y2 = x1 + self.width, y1 + self.height if self.align & (Align.LEFT | Align.RIGHT) == 0: edges = [[x2, y2], [x1, y2]] elif self.align & Align.BOTTOM == Align.BOTTOM: if self.align & Align.LEFT != 0: edges = [[x2, y2], [x1, y1]] elif self.align & Align.RIGHT != 0: edges = [[x2, y1], [x1, y2]] elif self.align & Align.TOP == Align.TOP: if self.align & Align.LEFT != 0: edges = [[x2, y1], [x2, y2]] elif self.align & Align.RIGHT != 0: edges = [[x1, y1], [x1, y2]] else: if self.align & Align.LEFT != 0: edges = [[x2, y1], [x2, y2]] elif self.align & Align.RIGHT != 0: edges = [[x1, y1], [x2, y2]] targets = SVGEditor.get_element(root, "markers_%s" % (self.name, )) if targets is None: return i = 0 for target in targets: tx, ty = float(target.attrib["cx"]), float(target.attrib["cy"]) try: edges[i] += [tx, ty] i += 1 except IndexError: break edges = [i for i in edges if len(i) == 4] for x1, y1, x2, y2 in edges: e = SVGEditor.add_element( root, "line", style="opacity:1;stroke:#06a400;stroke-width:0.5;", # id = "box_%s_line0" % (self.name,), x1=x1, y1=y1, x2=x2, y2=y2)
def __init__(self, image): Gtk.DrawingArea.__init__(self) self.connect('size-allocate', self.on_size_allocate) self.connect('draw', self.on_draw) areas = [] self.color_button1 = 0.8, 0, 0, 1 # Just random mess, self.color_button1_border = 1, 0, 0, 1 # config overrides it anyway self.color_button2 = 0.8, 0.8, 0, 1 self.color_button2_border = 1, 1, 0, 1 self.color_hilight = 0, 1, 1, 1 self.color_pressed = 1, 1, 1, 1 self.color_text = 1, 1, 1, 1 self.overlay = SVGWidget(image, False) self.tree = ET.fromstring(self.overlay.current_svg.encode("utf-8")) SVGWidget.find_areas(self.tree, None, areas, get_colors=True) self._hilight = () self._pressed = () self._button_images = {} self._help_areas = [ self.get_limit("HELP_LEFT"), self.get_limit("HELP_RIGHT") ] self._help_lines = ([], []) # TODO: It would be cool to use user-set font here, but cairo doesn't # have glyph replacement and most of default fonts (Ubuntu, Cantarell, # similar shit) misses pretty-much everything but letters, notably ↲ # # For that reason, DejaVu Sans is hardcoded for now. On systems # where DejaVu Sans is not available, Cairo will automatically fallback # to default font. self.font_face = "DejaVu Sans" # self.font_face = Gtk.Label(label="X").get_style().font_desc.get_family() log.debug("Using font %s", self.font_face) self.buttons = [Button(self.tree, area) for area in areas] background = SVGEditor.find_by_id(self.tree, "BACKGROUND") self.set_size_request(*SVGEditor.get_size(background)) self.overlay.edit().keep("overlay").commit() self.overlay.hilight({})
def show(self, *a): if self.background is None: self.realize() self.background = SVGWidget(self, self.args.image, init_hilighted=True) self.c.add(self.background) self.add(self.c) Generator(SVGEditor(self.background)) OSDWindow.show(self, *a) self.move(*self.compute_position())
def place(self, gen, root): e = SVGEditor.add_element(root, "rect", style = "opacity:1;fill-opacity:0.0;stroke-width:2.0;", fill="#000000", stroke="#06a400", id = "box_%s" % (self.name,), width = self.width, height = self.height, x = self.x, y = self.y, ) y = self.y + self.PADDING for line in self.lines: h = gen.line_height x = self.x + self.PADDING for icon in line.icons: image = find_image(icon) if image: SVGEditor.add_element(root, "image", x = x, y = y, style = "filter:url(#filterInvert)", width = h, height = h, href = image) x += h + self.SPACING x = self.x + self.PADDING + self.icount * (h + self.SPACING) y += h txt = SVGEditor.add_element(root, "text", x = x, y = y, style = gen.label_template.attrib['style'] ) SVGEditor.set_text(txt, line.text) y += self.SPACING
def __init__(self, image): Gtk.DrawingArea.__init__(self) self.connect('size-allocate', self.on_size_allocate) self.connect('draw', self.on_draw) areas = [] self.color_button1 = 0.8, 0, 0, 1 # Just random mess, self.color_button1_border = 1, 0, 0, 1 # config overrides it anyway self.color_button2 = 0.8, 0.8, 0, 1 self.color_button2_border = 1, 1, 0, 1 self.color_hilight = 0, 1, 1, 1 self.color_pressed = 1, 1, 1, 1 self.color_text = 1, 1, 1, 1 self.overlay = SVGWidget(image, False) self.tree = ET.fromstring(self.overlay.current_svg.encode("utf-8")) SVGWidget.find_areas(self.tree, None, areas, get_colors=True) self._hilight = () self._pressed = () self._button_images = {} self._help_areas = [ self.get_limit("HELP_LEFT"), self.get_limit("HELP_RIGHT") ] self._help_lines = ( [], [] ) # TODO: It would be cool to use user-set font here, but cairo doesn't # have glyph replacement and most of default fonts (Ubuntu, Cantarell, # similar shit) misses pretty-much everything but letters, notably ↲ # # For that reason, DejaVu Sans is hardcoded for now. On systems # where DejaVu Sans is not available, Cairo will automatically fallback # to default font. self.font_face = "DejaVu Sans" # self.font_face = Gtk.Label(label="X").get_style().font_desc.get_family() log.debug("Using font %s", self.font_face) self.buttons = [ Button(self.tree, area) for area in areas ] background = SVGEditor.find_by_id(self.tree, "BACKGROUND") self.set_size_request(*SVGEditor.get_size(background)) self.overlay.edit().keep("overlay").commit() self.overlay.hilight({})
def place_marker(self, gen, root): x1, y1 = self.x, self.y x2, y2 = x1 + self.width, y1 + self.height if self.align & (Align.LEFT | Align.RIGHT) == 0: edges = [ [ x2, y2 ], [ x1, y2 ] ] elif self.align & Align.BOTTOM == Align.BOTTOM: if self.align & Align.LEFT != 0: edges = [ [ x2, y2 ], [ x1, y1 ] ] elif self.align & Align.RIGHT != 0: edges = [ [ x2, y1 ], [ x1, y2 ] ] elif self.align & Align.TOP == Align.TOP: if self.align & Align.LEFT != 0: edges = [ [ x2, y1 ], [ x2, y2 ] ] elif self.align & Align.RIGHT != 0: edges = [ [ x1, y1 ], [ x1, y2 ] ] else: if self.align & Align.LEFT != 0: edges = [ [ x2, y1 ], [ x2, y2 ] ] elif self.align & Align.RIGHT != 0: edges = [ [ x1, y1 ], [ x2, y2 ] ] targets = SVGEditor.get_element(root, "markers_%s" % (self.name,)) if targets is None: return i = 0 for target in targets: tx, ty = float(target.attrib["cx"]), float(target.attrib["cy"]) try: edges[i] += [ tx, ty ] i += 1 except IndexError: break edges = [ i for i in edges if len(i) == 4] for x1, y1, x2, y2 in edges: e = SVGEditor.add_element(root, "line", style = "opacity:1;stroke:#06a400;stroke-width:0.5;", # id = "box_%s_line0" % (self.name,), x1 = x1, y1 = y1, x2 = x2, y2 = y2 )
def place(self, gen, root): e = SVGEditor.add_element( root, "rect", style="opacity:1;fill-opacity:0.1;stroke-width:2.0;", fill="#00FF00", stroke="#06a400", id="box_%s" % (self.name, ), width=self.width, height=self.height, x=self.x, y=self.y, ) y = self.y + self.PADDING for line in self.lines: h = gen.line_height x = self.x + self.PADDING for icon in line.icons: image = find_image(icon) if image: # Fix: here stuff goes from weird to awfull, as rsvg # (library that gnome uses to render SVGs) can't render # linked images. Embeding is used instead. image = 'data:image/svg+xml;base64,%s' % (base64.b64encode( file(image, "rb").read())) # Another problem: rsvg will NOT draw image unless href # tag uses namespace. No idea why is that, but I spent # 3 hours finding this, so I'm willing to murder. SVGEditor.add_element(root, "image", x=x, y=y, style="filter:url(#filterInvert)", width=h, height=h, **{"xlink:href": image}) x += h + self.SPACING x = self.x + self.PADDING + self.icount * (h + self.SPACING) y += h txt = SVGEditor.add_element( root, "text", x=x, y=y, style=gen.label_template.attrib['style']) max_line_width = self.max_width - gen.line_height - self.PADDING while line.text and line.get_size(gen)[0] > max_line_width: line.text = line.text[:-1] SVGEditor.set_text(txt, line.text) y += self.SPACING
def place(self, gen, root): e = SVGEditor.add_element(root, "rect", style = "opacity:1;fill-opacity:0.1;stroke-width:2.0;", fill="#00FF00", stroke="#06a400", id = "box_%s" % (self.name,), width = self.width, height = self.height, x = self.x, y = self.y, ) y = self.y + self.PADDING for line in self.lines: h = gen.line_height x = self.x + self.PADDING for icon in line.icons: image = find_image(icon) if image: # Fix: here stuff goes from weird to awfull, as rsvg # (library that gnome uses to render SVGs) can't render # linked images. Embeding is used instead. image = 'data:image/svg+xml;base64,%s' % ( base64.b64encode(file(image, "rb").read()) ) # Another problem: rsvg will NOT draw image unless href # tag uses namespace. No idea why is that, but I spent # 3 hours finding this, so I'm willing to murder. SVGEditor.add_element(root, "image", x = x, y = y, style = "filter:url(#filterInvert)", width = h, height = h, **{"xlink:href" : image} ) x += h + self.SPACING x = self.x + self.PADDING + self.icount * (h + self.SPACING) y += h txt = SVGEditor.add_element(root, "text", x = x, y = y, style = gen.label_template.attrib['style'] ) max_line_width = self.max_width - gen.line_height - self.PADDING while line.text and line.get_size(gen)[0] > max_line_width: line.text = line.text[:-1] SVGEditor.set_text(txt, line.text) y += self.SPACING
def pack_items(self, trash, items): if self._size > 0 and self._size < 100: self.scale = self._size / 100.0 root = SVGEditor.get_element(self.editor, "root") SVGEditor.scale(root, self.scale) pb = self.b.get_pixbuf() # Image width is not scaled as everything bellow operates # in 'root' object coordinate space image_width = pb.get_width() index = 0 item_offset = 360.0 / len(self.items) a1 = (-90.0 - item_offset * 0.5) * PI / 180.0 a2 = (-90.0 + item_offset * 0.5) * PI / 180.0 for i in self.items_with_icon: i.icon_widget.get_parent().remove_child(i.icon_widget) self.items_with_icon = [] for i in items: # Set size of each arc if SVGEditor.get_element(i.widget, "arc") is not None: l = SVGEditor.get_element(i.widget, "arc") radius = float( l.attrib["radius"] ) # TODO: Find how to get value of 'sodipodi:rx' l.attrib["d"] = l.attrib["d-template"] % ( radius * cos(a1) + image_width / 2, radius * sin(a1) + image_width / 2, radius * cos(a2) + image_width / 2, radius * sin(a2) + image_width / 2, ) # Rotate arc to correct position i.a = (360.0 / float(len(self.items))) * float(index) SVGEditor.rotate(i.widget, i.a, image_width * 0.5, image_width * 0.5) # Check if there is any icon icon_file, has_colors = find_icon(i.icon, False) if hasattr( i, "icon") else (None, False) if icon_file: # Icon - hide all text and place MenuIcon widget on top of image self.editor.remove_element( SVGEditor.get_element(i.widget, "menuitem_text")) self.editor.remove_element( SVGEditor.get_element(i.widget, "line0")) self.editor.remove_element( SVGEditor.get_element(i.widget, "line2")) i.icon_widget = MenuIcon(icon_file, has_colors) i.icon_widget.set_name("osd-radial-menu-icon") i.icon_widget.set_size_request(self.ICON_SIZE * self.scale, self.ICON_SIZE * self.scale) self.b.get_parent().put(i.icon_widget, 200, 200) self.items_with_icon.append(i) else: # No icon - rotate text in arc to other direction to keep it horisontal if SVGEditor.get_element(i.widget, "menuitem_text") is not None: l = SVGEditor.get_element(i.widget, "menuitem_text") l.attrib['id'] = "text_" + i.id l.attrib['transform'] = "%s rotate(%s)" % ( l.attrib['transform'], -i.a) # Place up to 3 lines of item label label = i.label.split("\n") first_line = 0 if len(label) == 1: self.editor.remove_element( SVGEditor.get_element(i.widget, "line0")) self.editor.remove_element( SVGEditor.get_element(i.widget, "line2")) first_line = 1 elif len(label) == 2: self.editor.remove_element( SVGEditor.get_element(i.widget, "line0")) first_line = 1 for line in xrange(0, len(label)): l = SVGEditor.get_element(i.widget, "line%s" % (first_line + line, )) if l is None: break SVGEditor.set_text(l, label[line]) # Continue with next menu item i.index = index index += 1 self.editor.remove_element("menuitem_template") self.editor.commit() del self.editor
def on_profile_changed(self, daemon, filename): profile = Profile(TalkingActionParser()).load(filename) Generator(SVGEditor(self.background), profile)
def __init__(self): svg = SVGEditor(file("images/binding-display.svg").read()) background = SVGEditor.get_element(svg, "background") self.label_template = SVGEditor.get_element(svg, "label_template") self.line_height = int(float(self.label_template.attrib.get("height") or 8)) self.char_width = int(float(self.label_template.attrib.get("width") or 8)) self.full_width = int(float(background.attrib.get("width") or 800)) self.full_height = int(float(background.attrib.get("height") or 800)) profile = Profile(TalkingActionParser()).load("test.sccprofile") boxes = [] box_bcs = Box(0, self.PADDING, Align.TOP, "bcs") box_bcs.add("BACK", Action.AC_BUTTON, profile.buttons.get(SCButtons.BACK)) box_bcs.add("C", Action.AC_BUTTON, profile.buttons.get(SCButtons.C)) box_bcs.add("START", Action.AC_BUTTON, profile.buttons.get(SCButtons.START)) boxes.append(box_bcs) box_left = Box(self.PADDING, self.PADDING, Align.LEFT | Align.TOP, "left", min_height = self.full_height * 0.5, min_width = self.full_width * 0.2) box_left.add("LEFT", Action.AC_TRIGGER, profile.triggers.get(profile.LEFT)) box_left.add("LB", Action.AC_BUTTON, profile.buttons.get(SCButtons.LB)) box_left.add("LGRIP", Action.AC_BUTTON, profile.buttons.get(SCButtons.LGRIP)) box_left.add("LPAD", Action.AC_PAD, profile.pads.get(profile.LEFT)) boxes.append(box_left) box_right = Box(self.PADDING, self.PADDING, Align.RIGHT | Align.TOP, "right", min_height = self.full_height * 0.5, min_width = self.full_width * 0.2) box_right.add("RIGHT", Action.AC_TRIGGER, profile.triggers.get(profile.RIGHT)) box_right.add("RB", Action.AC_BUTTON, profile.buttons.get(SCButtons.RB)) box_right.add("RGRIP", Action.AC_BUTTON, profile.buttons.get(SCButtons.RGRIP)) box_right.add("RPAD", Action.AC_PAD, profile.pads.get(profile.RIGHT)) boxes.append(box_right) box_abxy = Box(4 * self.PADDING, self.PADDING, Align.RIGHT | Align.BOTTOM, "abxy") box_abxy.add("A", Action.AC_BUTTON, profile.buttons.get(SCButtons.A)) box_abxy.add("B", Action.AC_BUTTON, profile.buttons.get(SCButtons.B)) box_abxy.add("X", Action.AC_BUTTON, profile.buttons.get(SCButtons.X)) box_abxy.add("Y", Action.AC_BUTTON, profile.buttons.get(SCButtons.Y)) boxes.append(box_abxy) box_stick = Box(4 * self.PADDING, self.PADDING, Align.LEFT | Align.BOTTOM, "stick") box_stick.add("STICK", Action.AC_STICK, profile.stick) boxes.append(box_stick) w = int(float(background.attrib.get("width") or 800)) h = int(float(background.attrib.get("height") or 800)) root = SVGEditor.get_element(svg, "root") for b in boxes: b.calculate(self) # Set ABXY and Stick size & position box_abxy.height = box_stick.height = self.full_height * 0.25 box_abxy.width = box_stick.width = self.full_width * 0.3 box_abxy.y = self.full_height - self.PADDING - box_abxy.height box_stick.y = self.full_height - self.PADDING - box_stick.height box_abxy.x = self.full_width - self.PADDING - box_abxy.width self.equal_width(box_left, box_right) self.equal_height(box_left, box_right) for b in boxes: b.place_marker(self, root) for b in boxes: b.place(self, root) file("out.svg", "w").write(svg.to_string())
def generate_widget(self, item): e = self.editor.clone_element("menuitem_template") SVGEditor.set_text(e, item.label) e.attrib['id'] = "menuitem_" + item.id return e
def _fill_button_images(self, buttons): e = self.edit() SVGEditor.update_parents(e) target = SVGEditor.get_element(e, "controller") target_x, target_y = SVGEditor.get_translation(target) for i in xrange(len(ControllerImage.BUTTONS_WITH_IMAGES)): b = nameof(ControllerImage.BUTTONS_WITH_IMAGES[i]) try: elm = SVGEditor.get_element(e, "AREA_%s" % (b, )) if elm is None: log.warning("Area for button %s not found", b) continue x, y = SVGEditor.get_translation(elm) scale = 1.0 if "scc-button-scale" in elm.attrib: w, h = SVGEditor.get_size(elm) scale = float(elm.attrib['scc-button-scale']) tw, th = w * scale, h * scale if scale < 1.0: x += (w - tw) * 0.5 y += (h - th) * 0.5 else: x -= (tw - w) * 0.25 y -= (th - h) * 0.25 path = os.path.join(self.app.imagepath, "button-images", "%s.svg" % (buttons[i], )) img = SVGEditor.get_element(SVGEditor.load_from_file(path), "button") img.attrib["transform"] = "translate(%s, %s) scale(%s)" % ( x - target_x, y - target_y, scale) img.attrib["id"] = b SVGEditor.add_element(target, img) except Exception, err: log.warning("Failed to add image for button %s", b) log.exception(err)
def pack_items(self, trash, items): if self._size > 0 and self._size < 100: self.scale = self._size / 100.0 root = SVGEditor.get_element(self.editor, "root") SVGEditor.scale(root, self.scale) pb = self.b.get_pixbuf() # Image width is not scaled as everything bellow operates # in 'root' object coordinate space image_width = pb.get_width() index = 0 item_offset = 360.0 / len(self.items) a1 = (-90.0 - item_offset * 0.5) * PI / 180.0 a2 = (-90.0 + item_offset * 0.5) * PI / 180.0 for i in self.items_with_icon: i.icon_widget.get_parent().remove_child(i.icon_widget) self.items_with_icon = [] for i in items: # Set size of each arc if SVGEditor.get_element(i.widget, "arc") is not None: l = SVGEditor.get_element(i.widget, "arc") radius = float(l.attrib["radius"]) # TODO: Find how to get value of 'sodipodi:rx' l.attrib["d"] = l.attrib["d-template"] % ( radius * cos(a1) + image_width / 2, radius * sin(a1) + image_width / 2, radius * cos(a2) + image_width / 2, radius * sin(a2) + image_width / 2, ) # Rotate arc to correct position i.a = (360.0 / float(len(self.items))) * float(index) SVGEditor.rotate(i.widget, i.a, image_width * 0.5, image_width * 0.5) # Check if there is any icon icon_file, has_colors = find_icon(i.icon, False) if hasattr(i, "icon") else (None, False) if icon_file: # Icon - hide all text and place MenuIcon widget on top of image self.editor.remove_element(SVGEditor.get_element(i.widget, "menuitem_text")) self.editor.remove_element(SVGEditor.get_element(i.widget, "line0")) self.editor.remove_element(SVGEditor.get_element(i.widget, "line2")) i.icon_widget = MenuIcon(icon_file, has_colors) i.icon_widget.set_name("osd-radial-menu-icon") i.icon_widget.set_size_request(self.ICON_SIZE * self.scale, self.ICON_SIZE * self.scale) self.b.get_parent().put(i.icon_widget, 200, 200) self.items_with_icon.append(i) else: # No icon - rotate text in arc to other direction to keep it horisontal if SVGEditor.get_element(i.widget, "menuitem_text") is not None: l = SVGEditor.get_element(i.widget, "menuitem_text") l.attrib['id'] = "text_" + i.id l.attrib['transform'] = "%s rotate(%s)" % (l.attrib['transform'], -i.a) # Place up to 3 lines of item label label = i.label.split("\n") first_line = 0 if len(label) == 1: self.editor.remove_element(SVGEditor.get_element(i.widget, "line0")) self.editor.remove_element(SVGEditor.get_element(i.widget, "line2")) first_line = 1 elif len(label) == 2: self.editor.remove_element(SVGEditor.get_element(i.widget, "line0")) first_line = 1 for line in xrange(0, len(label)): l = SVGEditor.get_element(i.widget, "line%s" % (first_line + line,)) if l is None: break SVGEditor.set_text(l, label[line]) # Continue with next menu item i.index = index index += 1 self.editor.remove_element("menuitem_template") self.editor.commit() del self.editor
def __init__(self, editor, profile): background = SVGEditor.get_element(editor, "background") self.label_template = SVGEditor.get_element(editor, "label_template") self.line_height = int(float(self.label_template.attrib.get("height") or 8)) self.char_width = int(float(self.label_template.attrib.get("width") or 8)) self.full_width = int(float(background.attrib.get("width") or 800)) self.full_height = int(float(background.attrib.get("height") or 800)) boxes = [] box_bcs = Box(0, self.PADDING, Align.TOP, "bcs") box_bcs.add("BACK", Action.AC_BUTTON, profile.buttons.get(SCButtons.BACK)) box_bcs.add("C", Action.AC_BUTTON, profile.buttons.get(SCButtons.C)) box_bcs.add("START", Action.AC_BUTTON, profile.buttons.get(SCButtons.START)) boxes.append(box_bcs) box_left = Box(self.PADDING, self.PADDING, Align.LEFT | Align.TOP, "left", min_height = self.full_height * 0.5, min_width = self.full_width * 0.2, max_width = self.full_width * 0.275 ) box_left.add("LEFT", Action.AC_TRIGGER, profile.triggers.get(profile.LEFT)) box_left.add("LB", Action.AC_BUTTON, profile.buttons.get(SCButtons.LB)) box_left.add("LGRIP", Action.AC_BUTTON, profile.buttons.get(SCButtons.LGRIP)) box_left.add("LPAD", Action.AC_PAD, profile.pads.get(profile.LEFT)) boxes.append(box_left) box_right = Box(self.PADDING, self.PADDING, Align.RIGHT | Align.TOP, "right", min_height = self.full_height * 0.5, min_width = self.full_width * 0.2, max_width = self.full_width * 0.275 ) box_right.add("RIGHT", Action.AC_TRIGGER, profile.triggers.get(profile.RIGHT)) box_right.add("RB", Action.AC_BUTTON, profile.buttons.get(SCButtons.RB)) box_right.add("RGRIP", Action.AC_BUTTON, profile.buttons.get(SCButtons.RGRIP)) box_right.add("RPAD", Action.AC_PAD, profile.pads.get(profile.RIGHT)) boxes.append(box_right) box_abxy = Box(4 * self.PADDING, self.PADDING, Align.RIGHT | Align.BOTTOM, "abxy", max_width = self.full_width * 0.45 ) box_abxy.add("A", Action.AC_BUTTON, profile.buttons.get(SCButtons.A)) box_abxy.add("B", Action.AC_BUTTON, profile.buttons.get(SCButtons.B)) box_abxy.add("X", Action.AC_BUTTON, profile.buttons.get(SCButtons.X)) box_abxy.add("Y", Action.AC_BUTTON, profile.buttons.get(SCButtons.Y)) boxes.append(box_abxy) box_stick = Box(4 * self.PADDING, self.PADDING, Align.LEFT | Align.BOTTOM, "stick", max_width = self.full_width * 0.45 ) box_stick.add("STICK", Action.AC_STICK, profile.stick) boxes.append(box_stick) w = int(float(background.attrib.get("width") or 800)) h = int(float(background.attrib.get("height") or 800)) root = SVGEditor.get_element(editor, "root") for b in boxes: b.calculate(self) # Set ABXY and Stick size & position box_abxy.height = box_stick.height = self.full_height * 0.25 box_abxy.width = box_stick.width = self.full_width * 0.3 box_abxy.y = self.full_height - self.PADDING - box_abxy.height box_stick.y = self.full_height - self.PADDING - box_stick.height box_abxy.x = self.full_width - self.PADDING - box_abxy.width self.equal_width(box_left, box_right) self.equal_height(box_left, box_right) for b in boxes: b.place_marker(self, root) for b in boxes: b.place(self, root) editor.commit()
def _fill_button_images(self, buttons): e = self.edit() SVGEditor.update_parents(e) target = SVGEditor.get_element(e, "controller") target_x, target_y = SVGEditor.get_translation(target) for i in xrange(len(ControllerImage.BUTTONS_WITH_IMAGES)): b = nameof(ControllerImage.BUTTONS_WITH_IMAGES[i]) try: elm = SVGEditor.get_element(e, "AREA_%s" % (b,)) if elm is None: log.warning("Area for button %s not found", b) continue x, y = SVGEditor.get_translation(elm) scale = 1.0 if "scc-button-scale" in elm.attrib: w, h = SVGEditor.get_size(elm) scale = float(elm.attrib['scc-button-scale']) tw, th = w * scale, h * scale if scale < 1.0: x += (w - tw) * 0.5 y += (h - th) * 0.5 else: x -= (tw - w) * 0.25 y -= (th - h) * 0.25 path = os.path.join(self.app.imagepath, "button-images", "%s.svg" % (buttons[i], )) img = SVGEditor.get_element(SVGEditor.load_from_file(path), "button") img.attrib["transform"] = "translate(%s, %s) scale(%s)" % ( x - target_x, y - target_y, scale) img.attrib["id"] = b SVGEditor.add_element(target, img) except Exception, err: log.warning("Failed to add image for button %s", b) log.exception(err)