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 _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 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_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): 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 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 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