Created
September 20, 2018 09:41
-
-
Save encela95dus/8f81907e4844d579ef8eda2ed5d65d5e to your computer and use it in GitHub Desktop.
sketch2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A very simple drawing 'app' that demonstrates | |
custom views and saving images to the camera roll. | |
''' | |
import ui | |
import photos | |
import console | |
# 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 = 8.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 = 'white' | |
iv = ui.ImageView(frame=(0, 0, width, height)) | |
pv = PathView(frame=self.bounds) | |
pv.action = self.path_action | |
self.add_subview(iv) | |
self.add_subview(pv) | |
self.image_view = iv | |
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') | |
v = ui.load_view() | |
v['imageview1'].image = ui.Image('Dog_Face') | |
save_button = ui.ButtonItem() | |
save_button.title = 'Save Image' | |
save_button.action = v['view1'].save_action | |
clear_button = ui.ButtonItem() | |
clear_button.title = 'Clear' | |
clear_button.tint_color = 'red' | |
clear_button.action = v['view1'].clear_action | |
v.right_button_items = [save_button, clear_button] | |
v.present('sheet') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"class" : "View", | |
"attributes" : { | |
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)", | |
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)", | |
"enabled" : true, | |
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)", | |
"flex" : "" | |
}, | |
"frame" : "{{0, 0}, {473, 489}}", | |
"selected" : false, | |
"nodes" : [ | |
{ | |
"class" : "ImageView", | |
"attributes" : { | |
"class" : "ImageView", | |
"name" : "imageview1", | |
"uuid" : "AC3FE861-965E-48E2-93AF-C0DA6A7BE69B", | |
"frame" : "{{187, 195}, {100, 100}}", | |
"border_width" : 1, | |
"corner_radius" : 0 | |
}, | |
"frame" : "{{81, 28}, {257, 162}}", | |
"selected" : true, | |
"nodes" : [ | |
] | |
}, | |
{ | |
"class" : "View", | |
"attributes" : { | |
"name" : "view1", | |
"corner_radius" : 0, | |
"border_width" : 1, | |
"class" : "View", | |
"frame" : "{{187, 195}, {100, 100}}", | |
"custom_class" : "SketchView", | |
"uuid" : "1CAADA59-CDE0-4D70-8398-6DF93E27DAF9" | |
}, | |
"frame" : "{{79, 243}, {259, 216}}", | |
"selected" : false, | |
"nodes" : [ | |
] | |
} | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment