pythonista3で製図アプリを作りたいと思っています。
具体的にはiPad純正メモアプリでできる定規に沿ってペンで直線を描いたりできるアプリです。
デモのSketch.pyと自作した定規を移動させるクラスを合体させて以下のようなプログラムを組みました。SceneViewを背面に移動させることでペンと定規を別々に動作させています。2種類以上の定規を作成した際(SceneViewを2つ以上用意)、背面に移動させたSceneViewの画像が表示できません。前面を透過させたら良いと思うのですがうまくいきません。ご教示いただければ幸いです。
import ui import photos import console from scene import* from scene import * class MainScene (Scene): def setup(self): self.background_color = (256, 256, 256, 0) self.ruler = SpriteNode('画像1.PNG',scale=0.3, alpha = 0.5) self.ruler.anchor_point = (0.5,0.5) self.ruler.position = (self.size.w/2, self.size.h/2) self.add_child(self.ruler) def update(self): pass def touch_began(self, touch): touch_loc = self.point_from_scene(touch.location) if touch_loc in self.ruler.frame: self.ruler.anchor_point = ((touch.location.x-self.ruler.frame.x)/self.ruler.frame.w ,(touch.location.y-self.ruler.frame.y)/self.ruler.frame.h) self.ruler.position = (touch.location.x,touch.location.y) def touch_moved(self, touch): touch_loc = self.point_from_scene(touch.location) if touch_loc in self.ruler.frame: self.ruler.position = (touch.location.x,touch.location.y) def touch_ended(self, touch): pass class rulerScene (Scene): def setup(self): self.background_color = (256, 256, 256, 0) self.ruler = SpriteNode('画像2.PNG',scale=0.3, alpha = 0.5) self.ruler.anchor_point = (0.5,0.5) self.ruler.position = (self.size.w/2, self.size.h/2) self.add_child(self.ruler) def update(self): pass def touch_began(self, touch): touch_loc = self.point_from_scene(touch.location) if touch_loc in self.ruler.frame: self.ruler.anchor_point = ((touch.location.x-self.ruler.frame.x)/self.ruler.frame.w ,(touch.location.y-self.ruler.frame.y)/self.ruler.frame.h) self.ruler.position = (touch.location.x,touch.location.y) def touch_moved(self, touch): touch_loc = self.point_from_scene(touch.location) if touch_loc in self.ruler.frame: self.ruler.position = (touch.location.x,touch.location.y) def touch_ended(self, touch): pass # The PathView class is responsible for tracking # touches and drawing the current stroke. # It is used by SketchView. class PathView (ui.View): def __init__(self, frame): self.frame = frame self.flex = 'WH' self.path = None self.action = None def touch_began(self, touch): x, y = touch.location self.path = ui.Path() self.path.line_width = 4.0 self.path.line_join_style = ui.LINE_JOIN_ROUND self.path.line_cap_style = ui.LINE_CAP_ROUND self.path.move_to(x, y) def touch_moved(self, touch): x, y = touch.location self.path.line_to(x, y) self.set_needs_display() def touch_ended(self, touch): # Send the current path to the SketchView: if callable(self.action): self.action(self) # Clear the view (the path has now been rendered # into the SketchView's image view): self.path = None self.set_needs_display() def draw(self): if self.path: self.path.stroke() # The main SketchView contains a PathView for the current # line and an ImageView for rendering completed strokes. # It also manages the 'Clear' and 'Save' ButtonItems that # are shown in the title bar. class SketchView (ui.View): def __init__(self, width=1024, height=1024): self.bg_color = (256,256,256,0) self.iv = ui.ImageView(frame=(0, 0, width, height)) self.pv = PathView(frame=self.bounds) self.pv.action = self.path_action self.ruler_sv = SceneView(frame=(0,0,width,height)) self.ruler_sv.scene = rulerScene() self.m_sv = SceneView(frame=(0,0,width,height)) self.m_sv.scene = MainScene() self.add_subview(self.m_sv) self.add_subview(self.ruler_sv) self.add_subview(self.iv) self.add_subview(self.pv) m_button = ui.ButtonItem() m_button.title = 'm' m_button.action = self.m_action ruler_button = ui.ButtonItem() ruler_button.title = 'Ruler' ruler_button.action = self.ruler_action pen_button = ui.ButtonItem() pen_button.title = 'Pen' pen_button.action = self.pen_action save_button = ui.ButtonItem() save_button.title = 'Save Image' save_button.action = self.save_action clear_button = ui.ButtonItem() clear_button.title = 'Clear' clear_button.tint_color = 'red' clear_button.action = self.clear_action self.right_button_items = [save_button, clear_button, ruler_button, pen_button, m_button] self.image_view = self.iv def m_action(self,sender): self.ruler_sv.send_to_back() self.pv.send_to_back() def pen_action(self,sender): self.ruler_sv.send_to_back() self.m_sv.send_to_back() def ruler_action(self,sender): self.pv.send_to_back() self.m_sv.send_to_back() def path_action(self, sender): path = sender.path old_img = self.image_view.image width, height = self.image_view.width, self.image_view.height with ui.ImageContext(width, height) as ctx: if old_img: old_img.draw() path.stroke() self.image_view.image = ctx.get_image() def clear_action(self, sender): self.image_view.image = None def save_action(self, sender): if self.image_view.image: # We draw a new image here, so that it has the current # orientation (the canvas is quadratic). with ui.ImageContext(self.width, self.height) as ctx: self.image_view.image.draw() img = ctx.get_image() photos.save_image(img) console.hud_alert('Saved') else: console.hud_alert('No Image', 'error') # We use a square canvas, so that the same image # can be used in portrait and landscape orientation. w, h = ui.get_screen_size() canvas_size = max(w, h) sv = SketchView(canvas_size, canvas_size) sv.name = 'Sketch' sv.present('fullscreen')
あなたの回答
tips
プレビュー